Changeset 2215 in Sophya for trunk/SophyaPI/PIext
- Timestamp:
- Oct 20, 2002, 4:12:08 PM (23 years ago)
- Location:
- trunk/SophyaPI/PIext
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaPI/PIext/piacmd.cc
r2214 r2215 10 10 #include "ctimer.h" 11 11 #include "strutil.h" 12 #include "strutilxx.h" 12 13 // #include "dlftypes.h" 13 14 … … 141 142 inline void AddLine(string& line) 142 143 { lines.push_back(line); bloclineid.push_back(lines.size()); } 144 void AddLine(string& line, string& kw); 143 145 inline void AddBloc(PIACmdBloc* blk) 144 146 { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); } 145 147 PIACmdBloc* Execute(); 148 inline int& TestLevel() { return testlevel; } 146 149 protected: 147 150 PIACmd* piacmd; … … 156 159 int i1,i2,di; 157 160 float f1,f2,df; 161 int testlevel; // niveau d'imbrication des if 158 162 }; 159 163 … … 167 171 i1 = 0; i2 = -1; di = 1; 168 172 f1 = 0.; f2 = -1.; df = 1.; 173 testlevel = 0; 169 174 if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return; 170 175 if ((kw != "foreach") && (kw != "for")) return; … … 172 177 //if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list 173 178 if (kw == "foreach" ) { // This is a foreach bloc with string list 174 for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]); 179 if ((args[1] != "(") || (args[args.size()-1] != ")") ) return; 180 for(int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]); 175 181 typ = BT_ForeachList; 176 182 blkok = true; … … 219 225 220 226 /* --Methode-- */ 227 void PIACmdBloc::AddLine(string& line, string& kw) 228 { 229 AddLine(line); 230 if (kw == "if") testlevel++; 231 else if (kw == "endif") testlevel--; 232 } 233 234 /* --Methode-- */ 221 235 PIACmdBloc* PIACmdBloc::Execute() 222 236 { … … 228 242 int kk=0; 229 243 char buff[32]; 244 int rcc = 0; 245 230 246 if (typ == BT_ForeachList) // foreach string loop 231 247 for(k=0; k<strlist.size(); k++) { … … 234 250 for(kj=0; kj<bloclineid.size(); kj++) { 235 251 kk = bloclineid[kj]; 236 if (kk > 0) piacmd->Interpret(lines[kk-1]); 252 if (kk > 0) { 253 rcc = piacmd->Interpret(lines[kk-1]); 254 if (rcc == 77766) break; 255 } 237 256 else blocs[-kk-1]->Execute(); 238 } 257 } 258 if (rcc == 77766) break; 239 259 } 240 260 else if (typ == BT_ForeachInt) // Integer loop … … 250 270 for(kj=0; kj<bloclineid.size(); kj++) { 251 271 kk = bloclineid[kj]; 252 if (kk > 0) piacmd->Interpret(lines[kk-1]); 272 if (kk > 0) { 273 rcc = piacmd->Interpret(lines[kk-1]); 274 if (rcc == 77766) break; 275 } 253 276 else blocs[-kk-1]->Execute(); 254 } 277 } 278 if (rcc == 77766) break; 255 279 } 256 280 else if (typ == BT_ForeachFloat) // float loop … … 266 290 for(kj=0; kj<bloclineid.size(); kj++) { 267 291 kk = bloclineid[kj]; 268 if (kk > 0) piacmd->Interpret(lines[kk-1]); 292 if (kk > 0) { 293 rcc = piacmd->Interpret(lines[kk-1]); 294 if (rcc == 77766) break; 295 } 269 296 else blocs[-kk-1]->Execute(); 270 } 297 } 298 if (rcc == 77766) break; 271 299 } 272 300 … … 298 326 trace = false; timing = false; 299 327 gltimer = NULL; 300 curblk = NULL;301 328 felevel = 0; 329 302 330 mulinecmd = ""; 303 331 mulinefg = false; 332 333 CmdBlks.push(NULL); 334 list<char> xtx; 335 TestsStack.push(xtx); 336 curtestresult = true; 304 337 305 338 cmdhgrp["All"] = 0; … … 517 550 NamedObjMgr omg; 518 551 552 519 553 // On saute de commandes vides 520 554 size_t l; … … 541 575 mulinecmd = ""; 542 576 mulinefg = false; 543 const char * rprompt = ( curblk == NULL) ? "Cmd> " : "for...? ";577 const char * rprompt = (CmdBlks.empty()) ? "Cmd> " : "for...? "; 544 578 mImgApp->GetConsole()->SetPrompt(rprompt); 545 579 } … … 583 617 } 584 618 585 if (kw == "break") {586 PIACmdBloc* curb = curblk;587 while (curb) {588 PIACmdBloc* dblk = curb;589 curb = curb->Parent();590 delete dblk;591 }592 testresult.clear();593 return(0);594 }595 else if (kw == "return") return(77777);596 597 619 // On verifie si nous sommes dans un bloc (for , foreach) 598 if ( curblk!= NULL) { // On est dans un bloc620 if (CmdBlks.top() != NULL) { // On est dans un bloc 599 621 if ( (kw == "for") || (kw == "foreach")) felevel++; 600 622 else if (kw == "end") felevel--; 601 623 if (felevel == 0) { // Il faut executer le bloc 602 PIACmdBloc* curb = curblk;603 curblk= curb->Parent();624 PIACmdBloc* curb = CmdBlks.top(); 625 CmdBlks.top() = curb->Parent(); 604 626 mImgApp->GetConsole()->SetPrompt("Cmd> "); 627 if (curb->TestLevel() != 0) { 628 cerr << "PIACmd::Interpret()/syntax error - unbalenced if ... endif" 629 << " within for/foreach bloc ! " << endl; 630 delete curb; 631 return(2); 632 } 605 633 // cout << " *DBG* Executing bloc " << endl; 606 634 bool ohv = histon; 607 635 histon = false; 608 curb->Execute(); 636 if (curtestresult) { 637 // We push also PIACmdBloc and testresult on the stack 638 CmdBlks.push(NULL); 639 list<char> xtx; 640 TestsStack.push(xtx); 641 curb->Execute(); 642 // And PIACmdBloc and TestResult from the corresponding stacks 643 PopStack(false); 644 } 645 delete curb; 609 646 histon = ohv; 610 647 } 611 else curblk->AddLine(s);648 else CmdBlks.top()->AddLine(s, kw); 612 649 return(0); 613 650 } … … 619 656 620 657 // Sommes-nous dans un bloc de test if then else 621 if ( testresult.size() > 0) { // Nous sommes ds un bloc if658 if (TestsStack.top().size() > 0) { // Nous sommes ds un bloc if 622 659 if (kw == "else") { 623 660 if ((*tresit) & 2) { … … 628 665 else { 629 666 const char * npr = ((*tresit)&1) ? "else-F> " : "else-T> "; 667 if ((*tresit)&1) curtestresult = false; 630 668 mImgApp->GetConsole()->SetPrompt(npr); 631 669 (*tresit) |= 2; … … 636 674 list<char>::iterator dbit = tresit; 637 675 tresit--; 638 testresult.erase(dbit);676 TestsStack.top().erase(dbit); 639 677 const char * npr = "Cmd> "; 640 if (testresult.size() > 1) { 678 if (TestsStack.top().size() > 1) { 679 curtestresult = true; 680 list<char>::iterator it; 681 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) { 682 // Si on n'est pas ds le else et le if est faux 683 if ( !((*it)&2) && !((*it)&1) ) curtestresult = false; 684 // Si on est ds else et le if etait vrai ! 685 if ( ((*it)&2) && ((*it)&1) ) curtestresult = false; 686 if (!curtestresult) break; 687 } 688 641 689 if (!((*tresit)&2)) 642 690 npr = ((*tresit)&1) ? "if-T> " : "if-F> "; … … 644 692 npr = ((*tresit)&1) ? "else-F> " : "else-T> "; 645 693 } 694 else curtestresult = true; 646 695 mImgApp->GetConsole()->SetPrompt(npr); 647 696 return(0); … … 655 704 656 705 bool fgcont = true; 657 if ( testresult.size() > 0) { // Resultat de if ou else706 if (TestsStack.top().size() > 0) { // Resultat de if ou else 658 707 list<char>::iterator it; 659 for(it= testresult.begin(); it!=testresult.end(); it++) {708 for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) { 660 709 // Si on n'est pas ds le else et le if est faux 661 710 if ( !((*it)&2) && !((*it)&1) ) fgcont = false; … … 668 717 if ((!fgcont) && (kw != "if")) return(0); 669 718 719 720 // Les mots cles break et return peuvent de sortir de boucles/scripts/execfile 721 if (kw == "break") return(77766); 722 else if (kw == "return") return(77777); 670 723 671 724 // Nous ne sommes donc pas dans un bloc .... Substitution de variables … … 725 778 if ((kw == "foreach") || (kw == "for")) { 726 779 // cout << " *DBG* We got a foreach... " << endl; 727 PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);780 PIACmdBloc* bloc = new PIACmdBloc(this, CmdBlks.top(), kw, tokens); 728 781 if (!bloc->CheckOK()) { 729 782 cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl; … … 732 785 } 733 786 felevel++; 734 if ( curblk) curblk->AddBloc(bloc);787 if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc); 735 788 else mImgApp->GetConsole()->SetPrompt("for...> "); 736 curblk= bloc;789 CmdBlks.top() = bloc; 737 790 // cout << " *DBG* New Bloc created ... " << endl; 738 791 return(0); … … 746 799 } 747 800 char res_tst = (restst) ? 1 : 0; 748 testresult.push_back(res_tst);749 if ( testresult.size() == 1) tresit = testresult.begin();801 TestsStack.top().push_back(res_tst); 802 if (TestsStack.top().size() == 1) tresit = TestsStack.top().begin(); 750 803 else tresit++; 751 804 const char * npr = (restst) ? "if-T> " : "if-F> "; … … 768 821 NamedObjMgr& omg = *mObjMgr; 769 822 770 size_t p,q,q2,l; 823 int iarr = -1; // index d'element de tableau 824 size_t p,q,q2,q3,l; 771 825 772 826 s2=""; … … 805 859 vn = s.substr(q+2,q2-q-2); 806 860 q2++; 807 } 861 } 862 else if ( s[q+1] == '(' ) { // Variable in the form $(name) 863 q2 = s.find(')',q+1); 864 if (q2 >= l) { 865 cerr << " Syntax error - Unbalenced parenthesis () !!! " << endl; 866 return(3); 867 } 868 vn = s.substr(q+2,q2-q-2); 869 q2++; 870 } 808 871 else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname 809 872 q2 = s.find(']',q+1); … … 820 883 q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1); 821 884 if (q2 > l) q2 = l; 885 q3 = q2; 822 886 vn = s.substr(q+1, q2-q-1); 823 } 887 // Si variable de type $varname[index] : element de tableau 888 if ((q2 < l) && (s[q2] == '[') ) { 889 q3 = s.find_first_of("]",q2+1); 890 string sia = s.substr(q2+1, q3-q2-1); 891 int rcdia = ctoi(sia.c_str(), &iarr); 892 if (rcdia < 0) { 893 cerr << " Syntax error - in $varname[iarr] : $" 894 << vn << "[" << sia <<"]" << endl; 895 return(4); 896 } 897 } 898 } 824 899 if (!GetVar(vn, vv)) return(5); 825 s2 += (s.substr(p, q-p) + vv); 826 p = q2; 827 } 900 if (iarr < 0) { 901 s2 += (s.substr(p, q-p) + vv); 902 p = q2; 903 } 904 else { 905 vector<string> vs; 906 FillVStringFrString(vv, vs); 907 if (iarr >= vs.size()) { 908 cerr << " Substitution error - word index out of range in " 909 << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl; 910 return(4); 911 } 912 else s2 += (s.substr(p, q-p) + vs[iarr]); 913 p = q3+1; 914 } 915 } 828 916 if (p < l) s2 += s.substr(p); 829 917 … … 884 972 { 885 973 res = true; 886 if ((args.size() < 4) || (args[3] != "then")) return(1); 887 if (args[1] == "==") res = (args[0] == args[2]); 888 else if (args[1] == "!=") res = (args[0] != args[2]); 889 else if (args[1] == "<") 890 res = (atof(args[0].c_str()) < atof(args[2].c_str())); 891 else if (args[1] == ">") 892 res = (atof(args[0].c_str()) > atof(args[2].c_str())); 893 else if (args[1] == "<=") 894 res = (atof(args[0].c_str()) <= atof(args[2].c_str())); 895 else if (args[1] == ">=") 896 res = (atof(args[0].c_str()) >= atof(args[2].c_str())); 974 if ((args.size() != 6) || (args[5] != "then") || 975 (args[0] != "(") || (args[4] != ")") ) return(1); 976 if (args[2] == "==") res = (args[1] == args[3]); 977 else if (args[2] == "!=") res = (args[1] != args[3]); 978 else if (args[2] == "<") 979 res = (atof(args[1].c_str()) < atof(args[3].c_str())); 980 else if (args[2] == ">") 981 res = (atof(args[1].c_str()) > atof(args[3].c_str())); 982 else if (args[2] == "<=") 983 res = (atof(args[1].c_str()) <= atof(args[3].c_str())); 984 else if (args[2] == ">=") 985 res = (atof(args[1].c_str()) >= atof(args[3].c_str())); 897 986 else return(2); 898 987 return(0); 988 } 989 990 /* --Methode-- */ 991 void PIACmd::PushStack(vector<string>& args) 992 { 993 // We push the argument list (args) on the stack 994 ArgsStack.push(args); 995 // We push also PIACmdBloc and testresult on the stack 996 CmdBlks.push(NULL); 997 list<char> xtx; 998 TestsStack.push(xtx); 999 1000 } 1001 1002 /* --Methode-- */ 1003 void PIACmd::PopStack(bool psta) 1004 { 1005 // We remove the argument list (args) from the stack 1006 if (psta) ArgsStack.pop(); 1007 // And PIACmdBloc and TestResult from the corresponding stacks 1008 PIACmdBloc* curb = CmdBlks.top(); 1009 while (curb != NULL) { 1010 PIACmdBloc* parb = curb->Parent(); 1011 delete curb; curb = parb; 1012 } 1013 CmdBlks.pop(); 1014 TestsStack.pop(); 899 1015 } 900 1016 … … 1122 1238 // hist << "### Executing commands from " << file << endl; 1123 1239 1124 // We push the argument list (args) on the stack 1125 ArgsStack.push(args); 1126 1240 PushStack(args); 1127 1241 if (trace) { 1128 1242 mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta); … … 1149 1263 } 1150 1264 1151 // We remove the argument list (args) from the stack 1152 ArgsStack.pop(); 1265 PopStack(true); 1153 1266 1154 1267 return(0); -
trunk/SophyaPI/PIext/piacmd.h
r2214 r2215 102 102 103 103 protected: 104 virtual int 104 virtual int CheckHelpGrp(string& grp); 105 105 int ExecuteCommandLine(string & keyw, vector<string> & args, 106 106 string & toks); … … 110 110 string & line, bool & res); 111 111 bool GetVar(string & vn, string & vv); 112 void PushStack(vector<string> & args); 113 void PopStack(bool psta=true); 112 114 113 115 NamedObjMgr* mObjMgr; … … 151 153 stack< vector<string> > ArgsStack; 152 154 153 PIACmdBloc * curblk; // Bloc de commande courant (foreach, ...)154 int felevel; // foreahlevel155 list<char> testresult; // Resultat des test if155 stack< PIACmdBloc * > CmdBlks; // Bloc de commande courant (foreach, ...) 156 int felevel; // foreach-for level 157 stack< list<char> > TestsStack; // Stack des resultats de test 156 158 list<char>::iterator tresit; // Test courant 159 bool curtestresult; // Resultat courant des tests 160 157 161 bool mulinefg; // Bloc multi-lignes (ligne suite) 158 162 string mulinecmd; // Commande multi-lignes
Note:
See TracChangeset
for help on using the changeset viewer.