Changeset 4063 in Sophya for trunk/SophyaLib/SysTools
- Timestamp:
- Apr 27, 2012, 12:34:31 AM (13 years ago)
- Location:
- trunk/SophyaLib/SysTools
- Files:
-
- 4 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); -
trunk/SophyaLib/SysTools/cexpre.h
r3586 r4063 9 9 #include <iostream> 10 10 #include <string> 11 #include <vector> 12 #include <map> 11 13 #include "pexceptions.h" 12 14 … … 46 48 }; 47 49 50 48 51 /*! 49 52 \ingroup SysTools … … 53 56 { ex.Print(s); return(s); } 54 57 58 /*! 59 \ingroup SysTools 60 \brief Interface class used by the CExpressionEvaluator class to represent a list of variables 61 or named values accessible trough a pointer. 62 63 Inherited class should define the \b GetVarPointer() method, and can optionaly redefine 64 the \b Update() method. 65 \sa CExpressionEvaluator 66 */ 67 class CE_VarListInterface { 68 public: 69 //! To be called to update values corresponding to the pointers. Default implementation does nothing. 70 virtual void Update() { return; } 71 //! Return a pointer to a double containing the value associated with \t name. 72 virtual double* GetVarPointer(std::string const& name) = 0; 73 }; 74 75 55 76 class CE_BinExp; 56 77 class CE_FuncExp; … … 58 79 class CExpressionEvaluator : public CExprBase { 59 80 public: 60 CExpressionEvaluator(string const & sex); 81 CExpressionEvaluator(const char* sexp, CE_VarListInterface* pvlist=NULL); 82 CExpressionEvaluator(string const & sex, CE_VarListInterface* pvlist=NULL); 83 61 84 virtual ~CExpressionEvaluator(); 62 85 //! Evaluate the expression and returns the corresponding value. … … 68 91 69 92 protected: 93 //! Parse variable names or constants and return CE_NumberExp object. 94 CExprBase* VarNameOrNumber(string const & s); 70 95 //! Does the parsing and builds an CExprBase object. 71 96 CExprBase* ParseString(int extype, string fname, string const & sex, … … 73 98 74 99 CExprBase * _exp; 100 CE_VarListInterface* _varlist; 75 101 }; 76 102 -
trunk/SophyaLib/SysTools/commander.cc
r4034 r4063 442 442 443 443 // ------------------------------------------------------------ 444 // Classe Cmd_CE_VarList 445 // ------------------------------------------------------------ 446 /*! 447 \internal 448 \class SOPHYA::Cmd_CE_VarList 449 \ingroup SysTools 450 This class, reserved for internal use by class Commander, enables the CExpressionEvaluator class 451 to access the interpreter variable values. 452 */ 453 454 class Cmd_CE_VarList : public CE_VarListInterface { 455 public: 456 Cmd_CE_VarList(Commander& cmd) : cmd_(cmd) { } 457 virtual void Update(); 458 virtual double* GetVarPointer(std::string const& name); 459 private: 460 Commander& cmd_; 461 std::map<std::string, double> namevals_; 462 }; 463 464 /* --Methode-- */ 465 void Cmd_CE_VarList::Update() 466 { 467 bool exist=false; 468 string sval; 469 std::map<std::string, double>::iterator it; 470 for(it=namevals_.begin(); it!=namevals_.end(); it++) { 471 exist=cmd_.GetVar((*it).first, sval); 472 if (!exist) throw NotFoundExc("Cmd_CE_VarList::Update() missing variable !"); 473 (*it).second=atof(sval.c_str()); 474 } 475 return; 476 } 477 478 /* --Methode-- */ 479 double* Cmd_CE_VarList::GetVarPointer(std::string const& name) 480 { 481 std::map<std::string, double>::iterator it=namevals_.find(name); 482 if (it!=namevals_.end()) return &((*it).second); 483 bool exist=false; 484 string sval; 485 exist=cmd_.GetVar(name, sval); 486 if (!exist) return NULL; 487 namevals_[name]=atof(sval.c_str()); 488 it=namevals_.find(name); 489 if (it!=namevals_.end()) return &((*it).second); 490 return NULL; 491 } 492 493 494 // ------------------------------------------------------------ 444 495 // Classe Commander 445 496 // ------------------------------------------------------------ … … 492 543 hist.open("history.pic"); 493 544 histon = true; 494 trace = false; timing = false; 545 trace = false; timing = false; varcexp = true; 495 546 gltimer = NULL; 496 547 felevel_ = 0; … … 561 612 usage += " > sleep nsec # sleep nsec seconds \n"; 562 613 usage += " > readstdin varname # reads a line from stdin into $varname \n"; 563 usage += " > timingon timingoff traceon traceoff \n";614 usage += " > timingon timingoff traceon traceoff varcexpon varcexpoff \n"; 564 615 RegisterHelp(kw, usage, grp); 565 616 … … 1085 1136 try { 1086 1137 double res = 0.; 1087 if (tokens.size() > 2) {1088 string sex = tokens[1]; 1138 string sex=tokens[1]; 1139 if (tokens.size() > 2) 1089 1140 for(unsigned int js=2; js<tokens.size(); js++) sex += tokens[js]; 1090 CExpressionEvaluator cex(sex); 1141 if (varcexp) { //--- Evaluation d'expression avec decodage de noms de variables 1142 Cmd_CE_VarList cevl(*this); 1143 CExpressionEvaluator cex(sex,&cevl); 1091 1144 res = cex.Value(); 1092 1145 } 1093 else { 1094 CExpressionEvaluator cex( tokens[1]);1146 else { //--- Evaluation d'expression SANS decodage des noms de variables 1147 CExpressionEvaluator cex(sex); 1095 1148 res = cex.Value(); 1096 1149 } … … 1957 2010 if (gltimer) delete gltimer; gltimer = NULL; timing = false; 1958 2011 } 2012 else if (kw == "varcexpon") 2013 { cout << "Commander::Interpret() -> Activating variable name decoding in expression evaluation " << endl; varcexp = true; } 2014 else if (kw == "varcexpoff") 2015 { cout << "Commander::Interpret() -> Deactivating variable name decoding in expression evaluation " << endl; varcexp = false; } 1959 2016 else if (kw == "exec") { 1960 2017 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: exec filename" << endl; return(0); } -
trunk/SophyaLib/SysTools/commander.h
r4034 r4063 257 257 bool timing; // Display CPU Time 258 258 Timer* gltimer; // pour Display CPU Time 259 259 bool varcexp; // true -> decodage nom de variables lors d'evaluation d'expression 260 260 friend class CommanderBloc; 261 261 friend class CommanderScript;
Note:
See TracChangeset
for help on using the changeset viewer.