Changeset 4063 in Sophya for trunk/SophyaLib/SysTools/cexpre.cc


Ignore:
Timestamp:
Apr 27, 2012, 12:34:31 AM (13 years ago)
Author:
ansari
Message:

Introduction de la classe interface CE_VarListInterface et modifs classe CExpressionEvaluator pour permettre l'inclusion de variables nommees dans les expressions traitees par CExpressionEvaluator, adaptation de la classe Commander afin que les variables de l'interpreteur soit visible par l'evaluateur CExpressionEvaluator, Reza 27/04/2012

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/cexpre.cc

    r3617 r4063  
    4545class CE_NumberExp : public CExprBase {
    4646public:
    47   CE_NumberExp(double v) { _val = v; }
     47  CE_NumberExp(double v) { _val = v; _pval=&_val; }
     48  CE_NumberExp(double* pv) { _pval=pv; _val = *_pval; }
    4849  CE_NumberExp(string const & s);
    4950  virtual ~CE_NumberExp() {}
    50   virtual double Evaluate() const { return _val; } 
    51   virtual void  Print(ostream& os) const { os << _val; }
     51  virtual double Evaluate() const { return (*_pval); } 
     52  virtual void  Print(ostream& os) const { os << (*_pval); }
    5253protected:
    5354  double _val;
     55  double* _pval;
    5456};
    5557
    5658CE_NumberExp::CE_NumberExp(string const & s)
    5759{
     60  _pval=&_val;
    5861  size_t l = s.length();
    5962  if (l < 1) {
     
    6467  else if (s == "E") _val = M_E;
    6568  else {
    66     string errmsg = "CE_NumberExp::CE_NumberExp() Bad numerical constant: ";
     69    string errmsg = "CE_NumberExp::CE_NumberExp() Bad numerical constant or unknown variable: ";
    6770    errmsg += s;
    6871   
     
    349352
    350353/*!
    351   Parse the string \c sex and builds an expression.
    352   Can throw CExprException exception.
    353 */
    354 CExpressionEvaluator::CExpressionEvaluator(string const & sex)
     354  Parse the character string \c exp and builds an expression. Uses the given list of variables
     355  \c varlist if a valid pointer is provided. Can throw CExprException exception.
     356*/
     357CExpressionEvaluator::CExpressionEvaluator(const char* exp, CE_VarListInterface* varlist)
    355358{
    356359  _exp = NULL;
     360  _varlist = varlist;
     361  size_t off=0,stop=0;
     362  string fname = "";
     363  string errmsg;
     364  string sexp=exp;
     365  _exp=  ParseString(0,fname,sexp,off,stop,errmsg);
     366  if (_exp == NULL)  throw CExprException(errmsg);
     367}
     368
     369/*!
     370  Parse the string \c sex and builds an expression. Uses the given list of variables
     371  \c varlist if a valid pointer is provided. Can throw CExprException exception.
     372*/
     373CExpressionEvaluator::CExpressionEvaluator(string const & sex, CE_VarListInterface* varlist)
     374{
     375  _exp = NULL;
     376  _varlist = varlist;
    357377  size_t off=0,stop=0;
    358378  string fname = "";
     
    402422}
    403423
     424CExprBase* CExpressionEvaluator::VarNameOrNumber(string const & s)
     425{
     426  if (_varlist==NULL)  return new CE_NumberExp(s);
     427  double* pv=_varlist->GetVarPointer(s);
     428  if (pv!=NULL) return new CE_NumberExp(pv);
     429  return new CE_NumberExp(s);
     430}
    404431
    405432CExprBase* CExpressionEvaluator::ParseString(int extype, string fname, string const & sex,
     
    506533            errmsg = "CExpressionEvaluator::ParseString()/ Syntax Error - rx&&cx (B)";
    507534          }
    508           if (q > p) cx = new CE_NumberExp(sex.substr(p-osn,q-p+osn));
     535          if (q > p) cx = VarNameOrNumber(sex.substr(p-osn,q-p+osn));
    509536          else cx = rx;
    510537         
     
    551578            else {
    552579              if (p == q)  cx = rx;
    553               else cx = new CE_NumberExp(sex.substr(p-osn,q-p+osn));
     580              else cx = VarNameOrNumber(sex.substr(p-osn,q-p+osn));
    554581              rx = Arrange_CE_BinExpStack(sbx, cx, nbx);
    555582              p = q+1;  osn = 0; lastopc = opc;
     
    573600      }
    574601      else {
    575         if (p<len)  cx = new CE_NumberExp(sex.substr(p-osn));
     602        if (p<len)  cx = VarNameOrNumber(sex.substr(p-osn));
    576603        else cx = rx;
    577604        rx = Arrange_CE_BinExpStack(sbx, cx);
Note: See TracChangeset for help on using the changeset viewer.