Index: trunk/SophyaLib/SysTools/cexpre.h
===================================================================
--- trunk/SophyaLib/SysTools/cexpre.h	(revision 2511)
+++ trunk/SophyaLib/SysTools/cexpre.h	(revision 2512)
@@ -15,5 +15,5 @@
 
 //! Exception class used by  CExpressionEvaluator
-class CExprException : PException {
+class CExprException : public PException {
   public:
   explicit CExprException(const char * m) : PException(m) {}
Index: trunk/SophyaLib/SysTools/commander.cc
===================================================================
--- trunk/SophyaLib/SysTools/commander.cc	(revision 2511)
+++ trunk/SophyaLib/SysTools/commander.cc	(revision 2512)
@@ -7,4 +7,6 @@
 #include "strutil.h"
 #include "strutilxx.h"
+#include "cexpre.h"
+#include "rpneval.h"
 #include "srandgen.h"
 
@@ -831,13 +833,19 @@
 }
 else if ((tokens.size() > 0) && (tokens[0] == "=")) {  
-  // x = RPNExpression (Evaluation d'expression RPN)
-  tokens[0] = kw;
-  int rcev = EvalRPNExpr(tokens, s);
-  if (rcev) {
-    cerr << "Commander::Interpret() evaluation (RPN) syntax Error ! " << "line: " << s << endl;
+  // x = Expression 
+  try {
+    CExpressionEvaluator cex(tokens[1]);
+    double res = cex.Value();
+    char cbuff[64];
+    sprintf(cbuff,"%g",res);
+    string vv = cbuff;
+    SetVariable(kw, vv);
+  }
+  catch (CExprException& cexerr) {
+    cerr << "Commander::Interpret() evaluation Error : \n " << "line: " << s 
+	 << " \n Msg=" << cexerr.Msg()  << endl;
     _xstatus = 98;
     return(98);
   }
-  return(0);
 }
 else if (kw == "defscript") {  // definition de script
@@ -1236,6 +1244,6 @@
   else { 
     if (idx >= (*it).second.size())
-      for(int j=(*it).second.size(); j<idx; j++)  (*it).second.push_back("");
-    (*it).second.push_back(val);
+      for(int j=(*it).second.size(); j<=idx; j++)  (*it).second.push_back("");
+    (*it).second[idx] = val;
     fg = true;
   }
@@ -1404,267 +1412,9 @@
 }
 
-/* Operations sur le stack RPN */
-inline bool Check_myRPNStack_(stack<double>& s, double& x, string& line) 
-{
-  if (s.empty()) { 
-    cerr << "Commander::EvalRPNExpr: syntax error / empty RPN stack " << line << endl; 
-    return true; 
-  } 
-  else x = s.top();
-  return false;
-}
-inline bool Check_myRPNStack_(stack<double>& s, double& x, double& y, string& line) 
-{
-  if (s.size() < 2) { 
-    cerr << "Commander::EvalRPNExpr: syntax error / RPN stack size < 2  " << line << endl; 
-    return true; 
-  }  
- else { 
-   x = s.top(); s.pop(); y = s.top(); 
- }
-  return false;
-}
-
-inline void Print_myRPNStack_(stack<double> s)
-{
-  if (s.empty()) 
-    cout << "Commander::EvalRPNExpr/PrintStack: Empty stack " << endl;
-  else {
-    int k = 0;
-    cout << "Commander::EvalRPNExpr/PrintStack: Size()= " << s.size() << endl;
-    while( !s.empty() ) {
-      cout << "    " << k << ":  " << s.top() << "  ";
-      if (k == 0)  cout << " (x) " << endl;
-      else if (k == 1) cout << " (y) " << endl;
-      else if (k == 2) cout << " (z) " << endl;
-      else cout << endl;
-      s.pop(); k++;
-    }
-  }
   
-} 
-
-int Sum_RPNStack_(stack<double>& s, double& sx, double& sx2, string& line) 
-{
-  sx = sx2 = 0.;
-  int nn = 0;
-  double x = 0.;
-  while( !s.empty() ) {
-    x = s.top(); s.pop();
-    sx += x; sx2 += x*x;
-    nn++;
-  } 
-  return(nn);  
-}
-
-int Product_RPNStack_(stack<double>& s, double& px, string& line) 
-{
-  px = 1.;
-  int nn = 0;
-  double x = 0.;
-  while( !s.empty() ) {
-    x = s.top(); s.pop();
-    px *= x;  nn++;
-  } 
-  return(nn);  
-}
-  
 /* --Methode-- */
 int Commander::EvalRPNExpr(vector<string> & args, string & line)
 {
-  
-  if (args.size() < 2) { 
-    cerr << "Commander::EvalRPNExpr: syntax error / missing arguments " << line << endl; 
-    return(1);
-  }
-  else if (args.size() == 2) { 
-    SetVariable(args[0], args[1]);
-    return(0); 
-  }
-
-  double x,y;
-  x = y = 0.;
-  stack<double> rpnstack;  // Stack des operations en RPN 
-  for(int k=1; k<args.size(); k++) {
-    // Les 4 operations de base + - * / 
-    if (args[k] == "+") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = y+x;
-    }
-    else if (args[k] == "-") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = y-x;
-    }
-    else if (args[k] == "*") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = y*x;
-    }
-    else if (args[k] == "/") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = y/x;
-    }
-    else if (args[k] == "%") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = (int)y % (int)x;
-    }
-    // Les constantes : e , pi
-    else if (args[k] == "e") {
-      rpnstack.push(M_E);
-    }
-    else if (args[k] == "pi") {
-      rpnstack.push(M_PI);
-    }
-    // Les fonctions usuelles a 1 argument f(x)
-    else if (args[k] == "cos") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = cos(x);
-    }
-    else if (args[k] == "sin") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = sin(x);
-    }
-    else if (args[k] == "tan") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = tan(x);
-    }
-    else if (args[k] == "acos") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = acos(x);
-    }
-    else if (args[k] == "asin") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = asin(x);
-    }
-    else if (args[k] == "atan") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = atan(x);
-    }
-    else if (args[k] == "chs") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = -x;
-    }
-    else if (args[k] == "sqrt") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = sqrt(x);
-    }
-    else if (args[k] == "sq") {  // x^2 
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = x*x;
-    }
-    else if (args[k] == "log") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = log(x);
-    }
-    else if (args[k] == "log10") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = log10(x);
-    }
-    else if (args[k] == "exp") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = exp(x);
-    }
-    else if (args[k] == "fabs") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = fabs(x);
-    }
-    else if (args[k] == "floor") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = floor(x);
-    }
-    else if (args[k] == "ceil") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = ceil(x);
-    }
-    // trunc et nint vire - ca ne compile pas sous linux - Reza 01/2003
-    else if (args[k] == "deg2rad") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = x*M_PI/180.;
-    }
-    else if (args[k] == "rad2deg") {
-      if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-      rpnstack.top() = x*180./M_PI;
-    }
-    // Les fonctions usuelles a 2 argument f(x,y)
-    else if (args[k] == "pow") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = pow(y,x);
-    }
-    else if (args[k] == "atan2") { 
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = atan2(x,y);
-    }
-    // generateur aleatoire
-    else if (args[k] == "rand") { 
-      double rnd = drand01();
-      rpnstack.push(rnd);
-    }
-    else if (args[k] == "norand") { 
-      double rnd = GauRnd(0., 1.);
-      rpnstack.push(rnd);
-    }
-    // Fonction a N arguments  - Somme, produit, etc ...
-    else if ((args[k] == "sum") || (args[k] == "mean") || (args[k] == "sigmean") || 
-	     (args[k] == "sigma") || (args[k] == "sigma2") ) {
-      double sx, sx2;
-      int nn = Sum_RPNStack_(rpnstack, sx, sx2, line);
-      if (args[k] == "sum") rpnstack.push(sx);
-      else { 
-	if (nn == 0) {
-	  cerr << "Commander::EvalRPNExpr: mean/sigma error- RPN stack empty: " 
-	       << line << endl; 
-	  return(1);
-	}
-	double fnn = nn;
-	if ((args[k] == "sigma") || (args[k] == "sigmean")) 
-	  rpnstack.push(sqrt(sx2/fnn-(x*x/(fnn*fnn))));
-	else if ((args[k] == "mean") || (args[k] == "sigmean"))  rpnstack.push(sx/fnn);
-	else rpnstack.push(sx2/fnn-(x*x/(fnn*fnn)));
-      }
-    }
-    else if (args[k] == "product") { 
-      double px;
-      int nn = Product_RPNStack_(rpnstack, px, line);
-      if (nn == 0) {
-	cerr << "Commander::EvalRPNExpr: product error- RPN stack empty: " 
-	     << line << endl; 
-	return(1);
-      }
-      rpnstack.push(px);
-    }
-    // Fonctions de manipulation de stack
-    else if (args[k] == "print") { 
-      Print_myRPNStack_(rpnstack);
-    }
-    else if (args[k] == "x<>y") {
-      if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
-      rpnstack.top() = x;  rpnstack.push(y);
-    }
-    else if (args[k] == "pop") {
-      rpnstack.pop();
-    }
-    else if (args[k] == "push") {
-      if (rpnstack.empty()) rpnstack.push(0.);
-      else rpnstack.push(rpnstack.top());
-    }
-    // On met un nombre sur le stack 
-    else {
-      char * esptr;
-      x = strtod(args[k].c_str(), &esptr);
-      //      if (ctof(args[k].c_str(),&x) < 0) {
-      if (esptr == args[k].c_str()) {
-	cerr << "Commander::EvalRPNExpr: syntax error near " << args[k] 
-	     << " in expression: \n" << line << endl; 
-	return(2);
-      }
-      rpnstack.push(x);
-    }
-
-  }
-
-  if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
-  char buff[64];
-  sprintf(buff, "%g", x);
-  string res = buff;
-  SetVariable(args[0], res);
+  // A virer - Reza 15/03/2004 
   return(0);
 }
@@ -1755,6 +1505,19 @@
 }
 // Evaluation d'expression en notation polonaise inverse
-else if (kw == "rpneval") {  
-  return(EvalRPNExpr(tokens, toks));
+else if (kw == "rpneval") {
+  try {
+    RPNExpressionEvaluator rpn(tokens, 1);
+    double res = rpn.Value();
+    char cbuff[64];
+    sprintf(cbuff,"%g",res);
+    string vv = cbuff;
+    SetVariable(tokens[0],vv);
+    return 0;
+  }
+  catch (RPNExprException& rpnerr) {
+    cerr << " rpneval: Syntax error - Msg=" << rpnerr.Msg() 
+	 << " \n Line=" << toks << endl;
+    return 98;
+  }
 }
 else if (kw == "echo") {
Index: trunk/SophyaLib/SysTools/rpneval.cc
===================================================================
--- trunk/SophyaLib/SysTools/rpneval.cc	(revision 2511)
+++ trunk/SophyaLib/SysTools/rpneval.cc	(revision 2512)
@@ -22,5 +22,12 @@
   FillVStringFrString(sex, exe, ' ');
   int rc = EvalRPNExpr(exe, 0);
-  if (rc != 0) throw RPNExprException("RPNExpressionEvaluator() - syntax error");  
+  if (rc < exe.size()) { 
+    string msg = "RPNExpressionEvaluator() - syntax error near ";
+    msg += exe[rc];
+    char buff[32];
+    sprintf(buff," (word %d)",rc);
+    msg += buff;
+    throw RPNExprException(msg);
+  }
 }
 
@@ -28,5 +35,12 @@
 {
   int rc = EvalRPNExpr(exe, off);
-  if (rc != 0) throw RPNExprException("RPNExpressionEvaluator() - syntax error");
+  if (rc < exe.size()) { 
+    string msg = "RPNExpressionEvaluator() - syntax error near ";
+    msg += exe[rc];
+    char buff[32];
+    sprintf(buff," (word %d)",rc);
+    msg += buff;
+    throw RPNExprException(msg);
+  }
 }
 
@@ -49,28 +63,28 @@
 {
   
-  if (args.size() <= off)  return 0;
+  if (args.size() <= off)  return 1;
   double x,y;
   x = y = 0.;
-  stack<double> rpnstack_;  // Stack des operations en RPN 
+
   for(int k=off; k<args.size(); k++) {
     // Les 4 operations de base + - * / 
     if (args[k] == "+") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = y+x;
     }
     else if (args[k] == "-") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = y-x;
     }
     else if (args[k] == "*") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = y*x;
     }
     else if (args[k] == "/") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = y/x;
     }
     else if (args[k] == "%") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = (int)y % (int)x;
     }
@@ -84,79 +98,79 @@
     // Les fonctions usuelles a 1 argument f(x)
     else if (args[k] == "cos") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = cos(x);
     }
     else if (args[k] == "sin") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = sin(x);
     }
     else if (args[k] == "tan") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = tan(x);
     }
     else if (args[k] == "acos") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = acos(x);
     }
     else if (args[k] == "asin") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = asin(x);
     }
     else if (args[k] == "atan") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = atan(x);
     }
     else if (args[k] == "chs") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = -x;
     }
     else if (args[k] == "sqrt") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = sqrt(x);
     }
     else if (args[k] == "sq") {  // x^2 
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = x*x;
     }
     else if (args[k] == "log") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = log(x);
     }
     else if (args[k] == "log10") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = log10(x);
     }
     else if (args[k] == "exp") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = exp(x);
     }
     else if (args[k] == "fabs") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = fabs(x);
     }
     else if (args[k] == "floor") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = floor(x);
     }
     else if (args[k] == "ceil") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = ceil(x);
     }
     // trunc et nint vire - ca ne compile pas sous linux - Reza 01/2003
     else if (args[k] == "deg2rad") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = x*M_PI/180.;
     }
     else if (args[k] == "rad2deg") {
-      if ( CheckStack( x) ) return(1);
+      if ( CheckStack( x) ) return k;
       rpnstack_.top() = x*180./M_PI;
     }
     // Les fonctions usuelles a 2 argument f(x,y)
     else if (args[k] == "pow") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = pow(y,x);
     }
     else if (args[k] == "atan2") { 
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = atan2(x,y);
     }
@@ -188,5 +202,5 @@
       double px;
       int nn = ProductStack( px);
-      if (nn == 0) return(1);
+      if (nn == 0) return k;
       rpnstack_.push(px);
     }
@@ -196,5 +210,5 @@
     }
     else if (args[k] == "x<>y") {
-      if ( CheckStack( x, y) ) return(1);
+      if ( CheckStack( x, y) ) return k;
       rpnstack_.top() = x;  rpnstack_.push(y);
     }
@@ -211,10 +225,10 @@
       x = strtod(args[k].c_str(), &esptr);
       //      if (ctof(args[k].c_str(),&x) < 0) {
-      if (esptr == args[k].c_str()) return 2; 
+      if (esptr == args[k].c_str()) return k; 
       rpnstack_.push(x);
     }
 
   }
-  return(0);
+  return(args.size()+1);
 }
 
