SCalc
lib/expression.hh
00001 /*
00002   expression.hh, copyright (c) 2006 by Vincent Fourmond: 
00003   The main header file for SCalc.
00004   
00005   This program is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009   
00010   This program is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013   GNU General Public License for more details (in the COPYING file).
00014   
00015 */
00016 
00017 namespace SCalc {
00018   // I like Javadoc-like comments the best
00019 
00020   class Expression;
00021   class FuncDef;
00022   class SyntaxError;
00023 
00036   class ParserResult {
00038     Session * sess;
00039   public:
00041     ParserResult(Session * s) {sess = s;};
00042     virtual ~ParserResult() {;};
00043 
00048     Session * session() { return sess;};
00049 
00050     // A whole set of what am I functions, all returning false 
00051     // (so that the children don't actually need to redefine anything.
00052     
00053     
00055 
00056 
00057 
00058 
00059 
00060 
00065     virtual int is_expression() { return 0;}
00066 
00071     Expression * to_expression() { 
00072       if(is_expression())
00073         return (Expression *) this;
00074       else
00075         return NULL;
00076     };
00077 
00078 
00083     virtual int is_syntax_error() { return 0;}
00084 
00089     SyntaxError * to_syntax_error() { 
00090       if(is_syntax_error())
00091         return (SyntaxError *) this;
00092       else
00093         return NULL;
00094     };
00095 
00100     virtual int is_func_def() { return 0;}
00101 
00106     FuncDef * to_func_def() { 
00107       if(is_func_def())
00108         return (FuncDef *) this;
00109       else
00110         return NULL;
00111     };
00112 
00114 
00115 
00117     virtual std::string pretty_print() = 0;
00118 
00121     virtual int can_delete() { return 1;};
00122   };
00123 
00124 
00131   class Expression : public ParserResult{
00132   public:
00133     Expression(Session * s) : ParserResult(s) {;};
00134     virtual ~Expression() {;};
00135 
00137     virtual int is_expression() { return 1;};
00138 
00155     virtual double evaluate(const double * values, 
00156                             const double * s = NULL) = 0;
00157     
00163     virtual void dump(::std::ostream & stream = ::std::cerr);
00164 
00170     virtual std::set<int> used_variables() { std::set<int> a; return a;};
00171 
00173     int evaluable() { return session()->evaluable(this);};
00175     double evaluate();
00176 
00179     virtual int is_null() { return 0;};
00180 
00182     virtual int is_id() { return 0;};
00183 
00185     virtual int is_const() { return 0;};
00186 
00189     virtual int is_valid() { return 1;};
00190 
00204     virtual Expression * derive(int id);
00205 
00207     virtual Expression * copy() { return NULL;};
00208 
00215     virtual std::string pretty_print() = 0;
00216 
00217 
00228     virtual Expression * simplify() { return copy();};
00229 
00242     virtual double * mass_evaluate(int nb, double * target, const double **variables);
00243 
00249     
00251     static Expression * add(Expression *, Expression* );
00252     static Expression * sub(Expression *, Expression* );
00253     static Expression * mul(Expression *, Expression* );
00254     static Expression * div(Expression *, Expression* );
00257   };
00258 
00259 
00260 };
00261