Changeset 1562 in Sophya for trunk/SophyaPI/PIext


Ignore:
Timestamp:
Jul 4, 2001, 1:10:28 AM (24 years ago)
Author:
ansari
Message:

Amelioration PIACmd (for/foreach , debut if ...) - Reza 4/7/2001

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaPI/PIext/piacmd.cc

    r1539 r1562  
    132132  enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
    133133
    134   PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args);
     134  PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
    135135  ~PIACmdBloc();
    136136  inline PIACmdBloc*   Parent() { return(parent); }
     
    156156
    157157/* --Methode-- */
    158 PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args)
     158PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
    159159{
    160160piacmd = piac;
     
    165165f1 = 0.; f2 = -1.; df = 1.;
    166166if ((args.size() < 2) ||  !isalpha((int)args[0][0]) )  return;
    167 varname = args[0];
    168 if (isalpha((int)args[1][0]) ) { // This is a foreach bloc with string list
     167if ((kw != "foreach") && (kw != "for"))  return;
     168varname = args[0];  // $CHECK$ Variable name should be checked
     169//if (isalpha((int)args[1][0]) ) {  This is a foreach bloc with string list
     170if (kw == "foreach" ) { // This is a foreach bloc with string list
    169171  for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
    170172  typ = BT_ForeachList;
     
    309311usage += "  > readstdin varname      # reads a line from stdin into $varname \n";
    310312usage += "  > foreach varname string-list # Loop \n";
    311 usage += "  > foreach varname i1:i2[:di]  # Integer loop  \n";
    312 usage += "  > foreach varname f1:f2[:df]  # Float loop  \n";
     313usage += "  > for varname i1:i2[:di]      # Integer loop  \n";
     314usage += "  > for varname f1:f2[:df]      # Float loop  \n";
    313315usage += "  > end                         # end loops \n";
    314316usage += "  > listvars      # List of variable names and values \n";
     
    509511  mulinecmd = "";
    510512  mulinefg = false;
    511   const char * rprompt = (curblk == NULL) ? "Cmd> " : "foreach? ";
     513  const char * rprompt = (curblk == NULL) ? "Cmd> " : "for...? ";
    512514  mImgApp->GetConsole()->SetPrompt(rprompt);
    513515}
     
    541543else { kw = s.substr(p,l-p);  toks = ""; }
    542544
    543 // On verifie si nous sommes dans un bloc
    544 if ( (curblk != NULL) && (kw != "foreach") ) { // On est dans un bloc
    545   if (kw != "end")   { curblk->AddLine(s);  return(0); }
    546   else {
     545// les mot-cle end else endif doivent etre le seul mot de la ligne
     546if ( (kw == "end") || (kw == "else") || (kw == "endif") ) {
     547  size_t ltk = toks.length();
     548  if (toks.find_first_not_of(" \t") < ltk) {
     549    cerr << "PIACmd::Interpret()/syntax error near end else endif \n"
     550         << "line: " << s << endl;
     551    return(1);
     552  }
     553}
     554
     555// On verifie si nous sommes dans un bloc (for , foreach)
     556if (curblk != NULL)  { // On est dans un bloc
     557  if ( (kw == "for") || (kw == "foreach")) felevel++;
     558  else if (kw == "end") felevel--;
     559  if (felevel == 0) { // Il faut executer le bloc
    547560    PIACmdBloc* curb = curblk;
    548561    curblk = curb->Parent();
    549     felevel--;
    550     if (curblk  == NULL) {
    551        mImgApp->GetConsole()->SetPrompt("Cmd> ");
     562    mImgApp->GetConsole()->SetPrompt("Cmd> ");
    552563       //       cout << " *DBG* Executing bloc " << endl;
    553        curb->Execute();
    554        }
    555     else  {
    556       //      char prompt[64];
    557       //      sprintf(prompt, "foreach-%d? ", felevel);
    558       mImgApp->GetConsole()->SetPrompt("foreach? ");
    559       }
     564    bool ohv = histon;
     565    histon = false;
     566    curb->Execute();
     567    histon = ohv;
     568  }
     569  else curblk->AddLine(s);
     570  return(0);
     571}
     572
     573// Sommes-nous dans un bloc de test if then else
     574if (testresult.size() > 0) {  // Nous sommes ds un bloc if
     575  if (kw == "endif") {
     576    list<bool>::iterator dbit = tresit;
     577    tresit--;
     578    testresult.erase(dbit);
    560579    return(0);
    561     }
    562   }
    563 
     580  }
     581}
     582if ((testresult.size() > 0) && !(*tresit)) return(0);
    564583
    565584// Nous ne sommes donc pas dans un bloc ....  Substitution de variables
     
    616635
    617636
    618 // Si c'est un foreach, on cree un nouveau bloc
    619 if (kw == "foreach") {
     637// Si c'est un for/foreach, on cree un nouveau bloc
     638if ((kw == "foreach") || (kw == "for")) {
    620639  //     cout << " *DBG* We got a foreach... " << endl;
    621   PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens);
     640  PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
    622641  if (!bloc->CheckOK()) {
    623     cerr << "foreach syntax Error ! " << endl;
     642    cerr << "for/foreach syntax Error ! " << endl;
    624643    delete bloc;
    625644    return(0);
     
    627646  felevel++;
    628647  if (curblk)  curblk->AddBloc(bloc);
    629   else {
    630     char prompt[64];
    631     sprintf(prompt, "foreach-%d> ", felevel);
    632     mImgApp->GetConsole()->SetPrompt(prompt);
    633     }
     648  else  mImgApp->GetConsole()->SetPrompt("for...> ");
    634649  curblk = bloc;
    635650  //  cout << " *DBG* New Bloc created ... " << endl;
    636651  return(0);
    637652  }
    638 
     653else if (kw == "if") {  // Un test
     654  bool res_tst = EvaluateTest(tokens);
     655  testresult.push_back(res_tst);
     656  if (testresult.size() == 1) tresit = testresult.begin();
     657  else tresit++;
     658}
    639659else rc = ExecuteCommandLine(kw, tokens, toks);
    640660return(rc);
     
    705725    }
    706726  else {
    707     q2 = s.find_first_of(" .:/,]()$\"'",q+1);
     727    q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
    708728    if (q2 > l) q2 = l;
    709729    vn = s.substr(q+1, q2-q-1);
     
    722742
    723743return(0);
     744}
     745
     746/* --Methode-- */
     747bool PIACmd::EvaluateTest(vector<string> & args)
     748{
     749  if (args.size() < 2) {
     750    cerr << " PIACmd::EvaluateTest()/Warning - args.size() < 2 " << endl;
     751    return true;
     752  }
     753  else return(args[0] == args[1]);
    724754}
    725755
     
    9701000  }
    9711001
     1002bool ohv = histon;
    9721003histon = false;
    9731004while (fgets(line_buff,511,fip) != NULL)
     
    9781009  Interpret(line);
    9791010  }
    980 histon = true;
     1011histon = ohv;
    9811012
    9821013// hist << "### End of Exec( " << file << " ) " << endl;
Note: See TracChangeset for help on using the changeset viewer.