expression.hh
/*
expression.hh, copyright (c) 2006 by Vincent Fourmond:
The main header file for SCalc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details (in the COPYING file).
*/
namespace SCalc {
// I like Javadoc-like comments the best
class Expression;
class FuncDef;
class SyntaxError;
class ParserResult {
Session * sess;
public:
ParserResult(Session * s) {sess = s;};
virtual ~ParserResult() {;};
Session * session() { return sess;};
// A whole set of what am I functions, all returning false
// (so that the children don't actually need to redefine anything.
virtual int is_expression() { return 0;}
Expression * to_expression() {
if(is_expression())
return (Expression *) this;
else
return NULL;
};
virtual int is_syntax_error() { return 0;}
SyntaxError * to_syntax_error() {
if(is_syntax_error())
return (SyntaxError *) this;
else
return NULL;
};
virtual int is_func_def() { return 0;}
FuncDef * to_func_def() {
if(is_func_def())
return (FuncDef *) this;
else
return NULL;
};
virtual std::string pretty_print() = 0;
virtual int can_delete() { return 1;};
};
class Expression : public ParserResult{
public:
Expression(Session * s) : ParserResult(s) {;};
virtual ~Expression() {;};
virtual int is_expression() { return 1;};
virtual double evaluate(const double * values,
const double * s = NULL) = 0;
virtual void dump(::std::ostream & stream = ::std::cerr);
virtual std::set<int> used_variables() { std::set<int> a; return a;};
int evaluable() { return session()->evaluable(this);};
double evaluate();
virtual int is_null() { return 0;};
virtual int is_id() { return 0;};
virtual int is_const() { return 0;};
virtual int is_valid() { return 1;};
virtual Expression * derive(int id);
virtual Expression * copy() { return NULL;};
virtual std::string pretty_print() = 0;
virtual Expression * simplify() { return copy();};
virtual double * mass_evaluate(int nb, double * target, const double **variables);
static Expression * add(Expression *, Expression* );
static Expression * sub(Expression *, Expression* );
static Expression * mul(Expression *, Expression* );
static Expression * div(Expression *, Expression* );
};
};
SCalc::Expression::copy
virtual Expression * copy()
Returns a freshly allocated copy of the expression.
Definition: expression.hh:207
SCalc::ParserResult::to_expression
Expression * to_expression()
Converts to SCalc::Expression.
Definition: expression.hh:71
SCalc::Session
A class representing a whole session.
Definition: session.hh:75
SCalc::ParserResult::pretty_print
virtual std::string pretty_print()=0
Pretty printing of the result ?
SCalc::Expression
An expression !
Definition: expression.hh:131
SCalc::ParserResult::ParserResult
ParserResult(Session *s)
Constructors/desctructors:
Definition: expression.hh:41
SCalc::Expression::is_valid
virtual int is_valid()
Definition: expression.hh:189
SCalc::Session::evaluable
int evaluable(Expression *expr)
SCalc::Expression::evaluate
double evaluate()
If it can be evaluated, returns the given value.
SCalc::Expression::pretty_print
virtual std::string pretty_print()=0
Pretty prints to a string.
SCalc::Expression::dump
virtual void dump(::std::ostream &stream=::std::cerr)
Dumps the contents of the expression to a stream.
SCalc::Expression::used_variables
virtual std::set< int > used_variables()
Variable used by an expression.
Definition: expression.hh:170
SCalc::ParserResult::is_func_def
virtual int is_func_def()
Is it a SCalc::FuncDef ?
Definition: expression.hh:100
SCalc::Expression::is_expression
virtual int is_expression()
Yes, this is an expression:
Definition: expression.hh:137
SCalc::ParserResult
The result of an SCalc::Session::eval().
Definition: expression.hh:36
SCalc::ParserResult::to_func_def
FuncDef * to_func_def()
Converts to SCalc::FuncDef.
Definition: expression.hh:106
SCalc::Expression::evaluable
int evaluable()
Tells if this expression can be evaluated.
Definition: expression.hh:173
SCalc::Expression::derive
virtual Expression * derive(int id)
Derive an expression with regards to one variable.
SCalc::ParserResult::is_expression
virtual int is_expression()
Is it a SCalc::Expression ?
Definition: expression.hh:65
SCalc::SyntaxError
A syntax error This class represents a syntax error. You can get several informations about this erro...
Definition: syntax.hh:26
SCalc::ParserResult::session
Session * session()
The SCalc::Session object used.
Definition: expression.hh:48
SCalc::FuncDef
A function definition with any number of parameters.
Definition: functions.hh:39
SCalc::Expression::is_const
virtual int is_const()
If const, then the value is always 1.
Definition: expression.hh:185
SCalc::Expression::is_null
virtual int is_null()
Definition: expression.hh:179
SCalc::ParserResult::to_syntax_error
SyntaxError * to_syntax_error()
Converts to SCalc::SyntaxError.
Definition: expression.hh:89
SCalc
Definition: session.hh:45
SCalc::Expression::is_id
virtual int is_id()
If Id, then the value is always 1.
Definition: expression.hh:182
SCalc::Expression::mass_evaluate
virtual double * mass_evaluate(int nb, double *target, const double **variables)
Evaluates the Expression for different parameters.
SCalc::ParserResult::is_syntax_error
virtual int is_syntax_error()
Is it a SCalc::SyntaxError ?
Definition: expression.hh:83
SCalc::ParserResult::can_delete
virtual int can_delete()
Definition: expression.hh:121
SCalc::Expression::simplify
virtual Expression * simplify()
Simplifies the expression.
Definition: expression.hh:228