Changeset 2241 in Sophya for trunk/SophyaPI/PIext


Ignore:
Timestamp:
Oct 30, 2002, 6:02:26 PM (23 years ago)
Author:
ansari
Message:

Suite integration calculateur RPN ds PIACmd , Reza 30/10/2002

Location:
trunk/SophyaPI/PIext
Files:
2 edited

Legend:

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

    r2236 r2241  
    427427usage += "  > setol varname patt     # Fills varname with object list \n";
    428428usage += "  > unset varname          # clear variable definition \n";
     429usage += "  > rpneval varname RPNExpression # Reverse Polish Notation evaluation \n";
    429430usage += "  > echo string            # output string \n";
    430431usage += "  > alias name string      # define a command alias \n";
    431432usage += "  > readstdin varname      # reads a line from stdin into $varname \n";
    432 usage += "  > foreach varname string-list # Loop \n";
     433usage += "  > foreach varname ( string-list ) # Loop \n";
    433434usage += "  > for varname i1:i2[:di]      # Integer loop  \n";
    434435usage += "  > for varname f1:f2[:df]      # Float loop  \n";
    435436usage += "  > end                         # end loops \n";
    436 usage += "  > if test then  # Conditional test : a == != < > <= >= b \n";
     437usage += "  > if ( test ) then  # Conditional test : a == != < > <= >= b \n";
    437438usage += "  > else          # Conditional  \n";
    438439usage += "  > endif         # End of conditional if bloc \n";
     
    914915  mImgApp->GetConsole()->SetPrompt(npr);
    915916}
    916 else if (kw == "@") {  // Evaluation d'expression
    917   int rcev = EvalVarExpr(tokens, s);
     917else if ((tokens.size() > 0) && (tokens[0] == "=")) { 
     918  // x = RPNExpression (Evaluation d'expression RPN)
     919  tokens[0] = kw;
     920  int rcev = EvalRPNExpr(tokens, s);
    918921  if (rcev) {
    919     cerr << "PIACmd::Interpret() evaluation syntax Error ! " << endl;
     922    cerr << "PIACmd::Interpret() evaluation (RPN) syntax Error ! " << endl;
    920923    return(1);
    921924  }
     
    946949//  cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
    947950
     951return(0);
    948952}
    949953
     
    11881192}
    11891193
    1190 /* Verification la presence d'argument d'operation */
    1191 #define _Check_EvalVarArg(j_) if (j_ >= args.size()) { \
    1192   cerr << "PIACmd::EvalVarExpr: syntax error " << line << endl; \
    1193   return(1); }
     1194/* Operations sur le stack RPN */
     1195inline bool Check_myRPNStack_(stack<double>& s, double& x, string& line)
     1196{
     1197  if (s.empty()) {
     1198    cerr << "PIACmd::EvalRPNExpr: syntax error / empty RPN stack " << line << endl;
     1199    return true;
     1200  }
     1201  else x = s.top();
     1202  return false;
     1203}
     1204inline bool Check_myRPNStack_(stack<double>& s, double& x, double& y, string& line)
     1205{
     1206  if (s.size() < 2) {
     1207    cerr << "PIACmd::EvalRPNExpr: syntax error / RPN stack size < 2  " << line << endl;
     1208    return true;
     1209  } 
     1210 else {
     1211   x = s.top(); s.pop(); y = s.top();
     1212 }
     1213  return false;
     1214}
     1215
     1216inline void Print_myRPNStack_(stack<double> s)
     1217{
     1218  if (s.empty())
     1219    cout << "PIACmd::EvalRPNExpr/PrintStack: Empty stack " << endl;
     1220  else {
     1221    int k = 0;
     1222    cout << "PIACmd::EvalRPNExpr/PrintStack: Size()= " << s.size() << endl;
     1223    while( !s.empty() ) {
     1224      cout << "    " << k << ":  " << s.top() << "  ";
     1225      if (k == 0)  cout << " (x) " << endl;
     1226      else if (k == 1) cout << " (y) " << endl;
     1227      else if (k == 2) cout << " (z) " << endl;
     1228      else cout << endl;
     1229      s.pop(); k++;
     1230    }
     1231  }
    11941232 
    1195 /* --Methode-- */
    1196 int PIACmd::EvalVarExpr(vector<string> & args, string & line)
    1197 {
    1198   if ((args.size() < 3) || (args[1] != "=") || !isalpha(args[0][0]) )  {
    1199     cerr << " PIACmd::EvalVarExpr: syntax error " << line << endl;
     1233}
     1234
     1235 
     1236/* --Methode-- */
     1237int PIACmd::EvalRPNExpr(vector<string> & args, string & line)
     1238{
     1239  NamedObjMgr& omg = *mObjMgr;
     1240 
     1241  if (args.size() < 2) {
     1242    cerr << "PIACmd::EvalRPNExpr: syntax error / missing arguments " << line << endl;
    12001243    return(1);
    12011244  }
    1202 
    1203   double x = atof(args[2].c_str());
    1204   double y = 0.;
    1205   int k = 3;
    1206   while (k < args.size()) {
     1245  else if (args.size() == 2) {
     1246    omg.SetVar(args[0], args[1]);
     1247    return(0);
     1248  }
     1249
     1250  double x,y;
     1251  x = y = 0.;
     1252  stack<double> rpnstack;  // Stack des operations en RPN
     1253  for(int k=1; k<args.size(); k++) {
     1254    // Les 4 operations de base + - * /
    12071255    if (args[k] == "+") {
    1208       _Check_EvalVarArg(k+1);
    1209       y = atof(args[k+1].c_str());
    1210       x += y;  k += 2;
     1256      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1257      rpnstack.top() = y+x;
    12111258    }
    12121259    else if (args[k] == "-") {
    1213       _Check_EvalVarArg(k+1);
    1214       y = atof(args[k+1].c_str());
    1215       x -= y;  k += 2;
     1260      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1261      rpnstack.top() = y-x;
    12161262    }
    12171263    else if (args[k] == "*") {
    1218       _Check_EvalVarArg(k+1);
    1219       y = atof(args[k+1].c_str());
    1220       x *= y;  k += 2;
     1264      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1265      rpnstack.top() = y*x;
    12211266    }
    12221267    else if (args[k] == "/") {
    1223       _Check_EvalVarArg(k+1);
    1224       y = atof(args[k+1].c_str());
    1225       x /= y;  k += 2;
    1226     }
    1227     else if (args[k] == "//") {
    1228       _Check_EvalVarArg(k+1);
    1229       if (args[k+1] == "sin")  x = sin(x);
    1230       else if (args[k+1] == "cos")  x = cos(x);
    1231       else if (args[k+1] == "tan")  x = tan(x);
    1232       else if (args[k+1] == "asin")  x = asin(x);
    1233       else if (args[k+1] == "acos")  x = acos(x);
    1234       else if (args[k+1] == "atan")  x = atan(x);
    1235       else if (args[k+1] == "log")   x = log(x);
    1236       else if (args[k+1] == "log10") x = log10(x);
    1237       else if (args[k+1] == "exp")   x = exp(x);
    1238       k += 2;
    1239    }
    1240   }
    1241 
    1242   NamedObjMgr& omg = *mObjMgr;
     1268      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1269      rpnstack.top() = y/x;
     1270    }
     1271    // Les constantes : e , pi
     1272    else if (args[k] == "e") {
     1273      rpnstack.push(M_E);
     1274    }
     1275    else if (args[k] == "pi") {
     1276      rpnstack.push(M_PI);
     1277    }
     1278    // Les fonctions usuelles a 1 argument f(x)
     1279    else if (args[k] == "cos") {
     1280      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1281      rpnstack.top() = cos(x);
     1282    }
     1283    else if (args[k] == "sin") {
     1284      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1285      rpnstack.top() = sin(x);
     1286    }
     1287    else if (args[k] == "tan") {
     1288      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1289      rpnstack.top() = tan(x);
     1290    }
     1291    else if (args[k] == "acos") {
     1292      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1293      rpnstack.top() = acos(x);
     1294    }
     1295    else if (args[k] == "asin") {
     1296      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1297      rpnstack.top() = asin(x);
     1298    }
     1299    else if (args[k] == "atan") {
     1300      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1301      rpnstack.top() = atan(x);
     1302    }
     1303    else if (args[k] == "log") {
     1304      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1305      rpnstack.top() = log(x);
     1306    }
     1307    else if (args[k] == "log10") {
     1308      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1309      rpnstack.top() = log10(x);
     1310    }
     1311    else if (args[k] == "exp") {
     1312      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1313      rpnstack.top() = exp(x);
     1314    }
     1315    else if (args[k] == "deg2rad") {
     1316      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1317      rpnstack.top() = x*M_PI/180.;
     1318    }
     1319    else if (args[k] == "rad2deg") {
     1320      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
     1321      rpnstack.top() = x*180./M_PI;
     1322    }
     1323    // Les fonctions usuelles a 2 argument f(x,y)
     1324    else if (args[k] == "pow") {
     1325      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1326      rpnstack.top() = pow(y,x);
     1327    }
     1328    else if (args[k] == "atan2") {
     1329      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1330      rpnstack.top() = atan2(x,y);
     1331    }
     1332    // Fonctions de manipulation de stack
     1333    else if (args[k] == "print") {
     1334      Print_myRPNStack_(rpnstack);
     1335    }
     1336    else if (args[k] == "x<>y") {
     1337      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
     1338      rpnstack.top() = x;  rpnstack.push(y);
     1339    }
     1340    // On met un nombre sur le stack
     1341    else {
     1342      if (ctof(args[k].c_str(),&x) < 0) {
     1343        cerr << "PIACmd::EvalRPNExpr: syntax error near " << args[k]
     1344             << " in expression: \n" << line << endl;
     1345        return(2);
     1346      }
     1347      rpnstack.push(x);
     1348    }
     1349
     1350  }
     1351
     1352  if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    12431353  char buff[64];
    12441354  sprintf(buff, "%g", x);
    1245   string vv = buff;
    1246   omg.SetVar(args[0], vv);
     1355  string res = buff;
     1356  omg.SetVar(args[0], res);
    12471357  return(0);
    12481358}
     
    13471457  else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
    13481458  }
     1459 else if (kw == "rpneval") {  // Evaluation d'expression en notation polonaise inverse
     1460  return(EvalRPNExpr(tokens, toks));
     1461}
    13491462else if (kw == "echo") {
    13501463  for (int ii=0; ii<tokens.size(); ii++)
  • trunk/SophyaPI/PIext/piacmd.h

    r2236 r2241  
    112112  int           EvaluateTest(vector<string> & args,
    113113                             string & line, bool & res);
    114   int           EvalVarExpr(vector<string> & args, string & line);
     114  int           EvalRPNExpr(vector<string> & args, string & line);
    115115
    116116  bool          GetVar(string & vn, string & vv);
Note: See TracChangeset for help on using the changeset viewer.