Changeset 2241 in Sophya for trunk/SophyaPI/PIext
- Timestamp:
- Oct 30, 2002, 6:02:26 PM (23 years ago)
- Location:
- trunk/SophyaPI/PIext
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaPI/PIext/piacmd.cc
r2236 r2241 427 427 usage += " > setol varname patt # Fills varname with object list \n"; 428 428 usage += " > unset varname # clear variable definition \n"; 429 usage += " > rpneval varname RPNExpression # Reverse Polish Notation evaluation \n"; 429 430 usage += " > echo string # output string \n"; 430 431 usage += " > alias name string # define a command alias \n"; 431 432 usage += " > readstdin varname # reads a line from stdin into $varname \n"; 432 usage += " > foreach varname string-list# Loop \n";433 usage += " > foreach varname ( string-list ) # Loop \n"; 433 434 usage += " > for varname i1:i2[:di] # Integer loop \n"; 434 435 usage += " > for varname f1:f2[:df] # Float loop \n"; 435 436 usage += " > end # end loops \n"; 436 usage += " > if testthen # Conditional test : a == != < > <= >= b \n";437 usage += " > if ( test ) then # Conditional test : a == != < > <= >= b \n"; 437 438 usage += " > else # Conditional \n"; 438 439 usage += " > endif # End of conditional if bloc \n"; … … 914 915 mImgApp->GetConsole()->SetPrompt(npr); 915 916 } 916 else if (kw == "@") { // Evaluation d'expression 917 int rcev = EvalVarExpr(tokens, s); 917 else if ((tokens.size() > 0) && (tokens[0] == "=")) { 918 // x = RPNExpression (Evaluation d'expression RPN) 919 tokens[0] = kw; 920 int rcev = EvalRPNExpr(tokens, s); 918 921 if (rcev) { 919 cerr << "PIACmd::Interpret() evaluation syntax Error ! " << endl;922 cerr << "PIACmd::Interpret() evaluation (RPN) syntax Error ! " << endl; 920 923 return(1); 921 924 } … … 946 949 // cout << "arg[ " << ii << " ] : " << tokens[ii] << endl; 947 950 951 return(0); 948 952 } 949 953 … … 1188 1192 } 1189 1193 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 */ 1195 inline 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 } 1204 inline 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 1216 inline 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 } 1194 1232 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-- */ 1237 int 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; 1200 1243 return(1); 1201 1244 } 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 + - * / 1207 1255 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; 1211 1258 } 1212 1259 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; 1216 1262 } 1217 1263 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; 1221 1266 } 1222 1267 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); 1243 1353 char buff[64]; 1244 1354 sprintf(buff, "%g", x); 1245 string vv= buff;1246 omg.SetVar(args[0], vv);1355 string res = buff; 1356 omg.SetVar(args[0], res); 1247 1357 return(0); 1248 1358 } … … 1347 1457 else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl; 1348 1458 } 1459 else if (kw == "rpneval") { // Evaluation d'expression en notation polonaise inverse 1460 return(EvalRPNExpr(tokens, toks)); 1461 } 1349 1462 else if (kw == "echo") { 1350 1463 for (int ii=0; ii<tokens.size(); ii++) -
trunk/SophyaPI/PIext/piacmd.h
r2236 r2241 112 112 int EvaluateTest(vector<string> & args, 113 113 string & line, bool & res); 114 int Eval VarExpr(vector<string> & args, string & line);114 int EvalRPNExpr(vector<string> & args, string & line); 115 115 116 116 bool GetVar(string & vn, string & vv);
Note:
See TracChangeset
for help on using the changeset viewer.