Changeset 1565 in Sophya for trunk/SophyaPI/PIext/piacmd.cc


Ignore:
Timestamp:
Jul 4, 2001, 6:07:47 PM (24 years ago)
Author:
ansari
Message:

suite implementation test if ... then ... else ... endif et debug for/foreach/if ds PIACmd - Reza 4/7/2001

File:
1 edited

Legend:

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

    r1562 r1565  
    227227if (typ == BT_ForeachList)   // foreach string loop
    228228  for(k=0; k<strlist.size(); k++) {
    229     cmd = "set " + varname + " " + strlist[k];
     229    cmd = "set " + varname + " '" + strlist[k] + "'";
    230230    piacmd->Interpret(cmd);
    231231    for(kj=0; kj<bloclineid.size(); kj++) {
     
    288288curblk = NULL;
    289289felevel = 0;
    290 bloccxx = false;
    291 bloctest = false;
    292290mulinecmd = "";
    293291mulinefg = false;
     
    314312usage += "  > for varname f1:f2[:df]      # Float loop  \n";
    315313usage += "  > end                         # end loops \n";
     314usage += "  > if test then  # Conditional test : a == != < > <= >= b \n";
     315usage += "  > else          # Conditional  \n";
     316usage += "  > endif         # End of conditional if bloc \n";
     317usage += "  > break         # Delete (clears) all test and loop blocs \n";
     318usage += "  > return        # Stops command execution from a file \n";
    316319usage += "  > listvars      # List of variable names and values \n";
    317320usage += "  > listalias     # List of alias names and values \n";
     
    553556}
    554557
     558if (kw == "break") {
     559  PIACmdBloc* curb = curblk;
     560  while (curb) {
     561    PIACmdBloc* dblk = curb;
     562    curb = curb->Parent();
     563    delete dblk;
     564  }
     565  testresult.clear();
     566  return(0);
     567}
     568else if (kw == "return")  return(77777);
     569
    555570// On verifie si nous sommes dans un bloc (for , foreach)
    556571if (curblk != NULL)  { // On est dans un bloc
     
    570585  return(0);
    571586}
     587else if (kw == "end") {
     588  cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
     589       << "line: " << s << endl;
     590  return(1); 
     591}
    572592
    573593// Sommes-nous dans un bloc de test if then else
    574594if (testresult.size() > 0) {  // Nous sommes ds un bloc if
    575   if (kw == "endif") {
    576     list<bool>::iterator dbit = tresit;
     595  if (kw == "else") {
     596    if ((*tresit) & 2) {
     597      cerr << "PIACmd::Interpret()/syntax error - multiple else in if bloc \n"
     598           << "line: " << s << endl;
     599      return(1);
     600    }
     601    else {
     602      char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
     603      mImgApp->GetConsole()->SetPrompt(npr);
     604      (*tresit) |= 2;
     605      return(0);
     606    }
     607  }
     608  else if (kw == "endif") {
     609    list<char>::iterator dbit = tresit;
    577610    tresit--;
    578611    testresult.erase(dbit);
     612    char * npr = "Cmd> ";
     613    if (testresult.size() > 1) {
     614      if (!((*tresit)&2))
     615        npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
     616      else
     617        npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
     618    }
     619    mImgApp->GetConsole()->SetPrompt(npr);
    579620    return(0);
    580621  }
    581 }
    582 if ((testresult.size() > 0) && !(*tresit)) return(0);
     622}
     623else if ((kw == "else") || (kw == "endif")) {
     624  cerr << "PIACmd::Interpret()/syntax error - else,endif outside if bloc \n"
     625       << "line: " << s << endl;
     626  return(1); 
     627}
     628
     629bool fgcont = true;
     630if (testresult.size() > 0) { // Resultat de if ou else
     631  list<char>::iterator it;
     632  for(it=testresult.begin(); it!=testresult.end(); it++) {
     633    // Si on n'est pas ds le else et le if est faux
     634    if ( !((*it)&2) && !((*it)&1) )  fgcont = false;
     635    // Si on est ds else et le if etait vrai !
     636    if ( ((*it)&2) && ((*it)&1) )  fgcont = false;
     637    if (!fgcont)  break;
     638  }
     639}
     640
     641if ((!fgcont) && (kw != "if"))  return(0);
     642
    583643
    584644// Nous ne sommes donc pas dans un bloc ....  Substitution de variables
     
    622682    q = toks.find(toks[p],p+1);
    623683    if (q>=l)  {
    624       cerr << "Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
     684      cerr << "PIACmd::Interpret()/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
    625685      return(2);
    626686    }
     
    640700  PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
    641701  if (!bloc->CheckOK()) {
    642     cerr << "for/foreach syntax Error ! " << endl;
     702    cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
    643703    delete bloc;
    644     return(0);
     704    return(1);
    645705    }
    646706  felevel++;
     
    651711  return(0);
    652712  }
    653 else if (kw == "if") {  // Un test
    654   bool res_tst = EvaluateTest(tokens);
     713else if (kw == "if") {  // Un test if
     714  bool restst = true;
     715  int rct = EvaluateTest(tokens, s, restst);
     716  if (rct) {
     717    cerr << "PIACmd::Interpret() if syntax Error ! " << endl;
     718    return(1);
     719  }
     720  char res_tst = (restst) ? 1 : 0;
    655721  testresult.push_back(res_tst);
    656722  if (testresult.size() == 1) tresit = testresult.begin();
    657723  else tresit++;
    658 }
     724  char * npr = (restst) ? "if-T> " : "if-F> ";
     725  mImgApp->GetConsole()->SetPrompt(npr);
     726}
     727//  Execution de commandes
    659728else rc = ExecuteCommandLine(kw, tokens, toks);
    660729return(rc);
     
    745814
    746815/* --Methode-- */
    747 bool 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]);
     816int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
     817{
     818  res = true;
     819  if ((args.size() < 4) || (args[3] != "then")) return(1);
     820  if (args[1] == "==") res = (args[0] == args[2]);
     821  else if (args[1] == "!=") res = (args[0] != args[2]);
     822  else if (args[1] == "<")
     823    res = (atof(args[0].c_str()) < atof(args[2].c_str()));
     824  else if (args[1] == ">")
     825    res = (atof(args[0].c_str()) > atof(args[2].c_str()));
     826  else if (args[1] == "<=")
     827    res = (atof(args[0].c_str()) <= atof(args[2].c_str()));
     828  else if (args[1] == ">=")
     829    res = (atof(args[0].c_str()) >= atof(args[2].c_str()));
     830  else return(2);
     831  return(0);
    754832}
    755833
     
    10071085  line_buff[strlen(line_buff)-1] = '\0';   /*  LF/CR de la fin */
    10081086  string line(line_buff);
    1009   Interpret(line);
     1087  if (Interpret(line) == 77777) break;
    10101088  }
    10111089histon = ohv;
Note: See TracChangeset for help on using the changeset viewer.