Changeset 4063 in Sophya for trunk/SophyaLib/SysTools/cexpre.cc
- Timestamp:
- Apr 27, 2012, 12:34:31 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SysTools/cexpre.cc
r3617 r4063 45 45 class CE_NumberExp : public CExprBase { 46 46 public: 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; } 48 49 CE_NumberExp(string const & s); 49 50 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); } 52 53 protected: 53 54 double _val; 55 double* _pval; 54 56 }; 55 57 56 58 CE_NumberExp::CE_NumberExp(string const & s) 57 59 { 60 _pval=&_val; 58 61 size_t l = s.length(); 59 62 if (l < 1) { … … 64 67 else if (s == "E") _val = M_E; 65 68 else { 66 string errmsg = "CE_NumberExp::CE_NumberExp() Bad numerical constant : ";69 string errmsg = "CE_NumberExp::CE_NumberExp() Bad numerical constant or unknown variable: "; 67 70 errmsg += s; 68 71 … … 349 352 350 353 /*! 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 */ 357 CExpressionEvaluator::CExpressionEvaluator(const char* exp, CE_VarListInterface* varlist) 355 358 { 356 359 _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 */ 373 CExpressionEvaluator::CExpressionEvaluator(string const & sex, CE_VarListInterface* varlist) 374 { 375 _exp = NULL; 376 _varlist = varlist; 357 377 size_t off=0,stop=0; 358 378 string fname = ""; … … 402 422 } 403 423 424 CExprBase* 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 } 404 431 405 432 CExprBase* CExpressionEvaluator::ParseString(int extype, string fname, string const & sex, … … 506 533 errmsg = "CExpressionEvaluator::ParseString()/ Syntax Error - rx&&cx (B)"; 507 534 } 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)); 509 536 else cx = rx; 510 537 … … 551 578 else { 552 579 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)); 554 581 rx = Arrange_CE_BinExpStack(sbx, cx, nbx); 555 582 p = q+1; osn = 0; lastopc = opc; … … 573 600 } 574 601 else { 575 if (p<len) cx = new CE_NumberExp(sex.substr(p-osn));602 if (p<len) cx = VarNameOrNumber(sex.substr(p-osn)); 576 603 else cx = rx; 577 604 rx = Arrange_CE_BinExpStack(sbx, cx);
Note:
See TracChangeset
for help on using the changeset viewer.