SCalc
expression.hh
1 /*
2  expression.hh, copyright (c) 2006 by Vincent Fourmond:
3  The main header file for SCalc.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details (in the COPYING file).
14 
15 */
16 
17 namespace SCalc {
18  // I like Javadoc-like comments the best
19 
20  class Expression;
21  class FuncDef;
22  class SyntaxError;
23 
36  class ParserResult {
38  Session * sess;
39  public:
41  ParserResult(Session * s) {sess = s;};
42  virtual ~ParserResult() {;};
43 
48  Session * session() { return sess;};
49 
50  // A whole set of what am I functions, all returning false
51  // (so that the children don't actually need to redefine anything.
52 
53 
55 
65  virtual int is_expression() { return 0;}
66 
72  if(is_expression())
73  return (Expression *) this;
74  else
75  return NULL;
76  };
77 
78 
83  virtual int is_syntax_error() { return 0;}
84 
90  if(is_syntax_error())
91  return (SyntaxError *) this;
92  else
93  return NULL;
94  };
95 
100  virtual int is_func_def() { return 0;}
101 
107  if(is_func_def())
108  return (FuncDef *) this;
109  else
110  return NULL;
111  };
112 
114 
115 
117  virtual std::string pretty_print() = 0;
118 
121  virtual int can_delete() { return 1;};
122  };
123 
124 
131  class Expression : public ParserResult{
132  public:
133  Expression(Session * s) : ParserResult(s) {;};
134  virtual ~Expression() {;};
135 
137  virtual int is_expression() { return 1;};
138 
155  virtual double evaluate(const double * values,
156  const double * s = NULL) = 0;
157 
163  virtual void dump(::std::ostream & stream = ::std::cerr);
164 
170  virtual std::set<int> used_variables() { std::set<int> a; return a;};
171 
173  int evaluable() { return session()->evaluable(this);};
175  double evaluate();
176 
179  virtual int is_null() { return 0;};
180 
182  virtual int is_id() { return 0;};
183 
185  virtual int is_const() { return 0;};
186 
189  virtual int is_valid() { return 1;};
190 
204  virtual Expression * derive(int id);
205 
207  virtual Expression * copy() { return NULL;};
208 
215  virtual std::string pretty_print() = 0;
216 
217 
228  virtual Expression * simplify() { return copy();};
229 
242  virtual double * mass_evaluate(int nb, double * target, const double **variables);
243 
249 
251  static Expression * add(Expression *, Expression* );
252  static Expression * sub(Expression *, Expression* );
253  static Expression * mul(Expression *, Expression* );
254  static Expression * div(Expression *, Expression* );
257  };
258 
259 
260 };
261 
virtual Expression * copy()
Returns a freshly allocated copy of the expression.
Definition: expression.hh:207
Expression * to_expression()
Converts to SCalc::Expression.
Definition: expression.hh:71
A class representing a whole session.
Definition: session.hh:75
virtual std::string pretty_print()=0
Pretty printing of the result ?
An expression !
Definition: expression.hh:131
ParserResult(Session *s)
Constructors/desctructors:
Definition: expression.hh:41
virtual int is_valid()
Definition: expression.hh:189
int evaluable(Expression *expr)
virtual std::set< int > used_variables()
Variable used by an expression.
Definition: expression.hh:170
virtual int is_func_def()
Is it a SCalc::FuncDef ?
Definition: expression.hh:100
virtual int is_expression()
Yes, this is an expression:
Definition: expression.hh:137
The result of an SCalc::Session::eval().
Definition: expression.hh:36
FuncDef * to_func_def()
Converts to SCalc::FuncDef.
Definition: expression.hh:106
int evaluable()
Tells if this expression can be evaluated.
Definition: expression.hh:173
virtual int is_expression()
Is it a SCalc::Expression ?
Definition: expression.hh:65
A syntax error This class represents a syntax error. You can get several informations about this erro...
Definition: syntax.hh:26
Session * session()
The SCalc::Session object used.
Definition: expression.hh:48
A function definition with any number of parameters.
Definition: functions.hh:39
virtual int is_const()
If const, then the value is always 1.
Definition: expression.hh:185
virtual int is_null()
Definition: expression.hh:179
SyntaxError * to_syntax_error()
Converts to SCalc::SyntaxError.
Definition: expression.hh:89
Definition: session.hh:45
virtual int is_id()
If Id, then the value is always 1.
Definition: expression.hh:182
virtual int is_syntax_error()
Is it a SCalc::SyntaxError ?
Definition: expression.hh:83
virtual int can_delete()
Definition: expression.hh:121
virtual Expression * simplify()
Simplifies the expression.
Definition: expression.hh:228