Changeset 1262 in Sophya


Ignore:
Timestamp:
Oct 27, 2000, 6:30:28 PM (25 years ago)
Author:
ercodmgr
Message:

Reorganisation classe PIACmd (interpreteur de piapp) - Utilisation des services de NameObjMgr pour les variables de l'interpreteur - Reza 27/10/2000

Location:
trunk/SophyaPI/PIext
Files:
5 edited

Legend:

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

    r1247 r1262  
    9696mpiac->RegisterCommand(kw, usage, this, hgrp);
    9797
    98 
    99 
    100 //// A VIRER quand variable de objmanager OK  --> Pour Reza
    101 kw = "c++setvar";
    102 usage = "c++setvar: Setting test variable \n";
    103 usage+= "Usage: c++setvar varname varcontent ";
    104 mpiac->RegisterCommand(kw, usage, this, hgrp);
    105 
    106 kw = "c++getvar";
    107 usage = "c++getvar: Getting test variable content\n";
    108 usage+= "Usage: c++getvar varname ";
    109 mpiac->RegisterCommand(kw, usage, this, hgrp);
    110 
    111 kw = "c++varlist";
    112 usage = "c++getvar: Printing test variable list\n";
    113 usage+= "Usage: c++varlist  ";
    114 mpiac->RegisterCommand(kw, usage, this, hgrp);
    115 //// A VIRER quand variable de objmanager OK  --> Pour Reza
    11698
    11799}
     
    222204
    223205/* --Methode-- */
     206int CxxExecutor::ExecuteCxx(string const & code)
     207{
     208  int rc;
     209
     210  mUserCodeFn = "";
     211  mUserCodeFn = "cxx_spiapp.h";
     212  ofstream os(mUserCodeFn.c_str(),ios::out);
     213  if(!os) {cout<<"CxxExecutor::ExecuteCxx(): unable to open "
     214               <<mUserCodeFn<<endl;  mUserCodeFn = ""; return 1;}
     215 
     216  os << code ;
     217  os << endl ;
     218
     219  cout<<"CxxExecutor: User code filled from standard input into "
     220      <<mUserCodeFn<<endl;
     221 
     222  rc = CrFile();   if(rc) return(1);
     223  rc = Compile();  if(rc) return(1);
     224  rc = Link();     if(rc) return(1);
     225  rc = Call();     if(rc) return(1);
     226  return(rc);
     227}
     228
     229/* --Methode-- */
    224230int CxxExecutor::CrFile(string cfilename,string func)
    225231{
     
    362368for(it=varlist.Begin(); it!=varlist.End(); it++) {
    363369  string key = (*it).first;
    364   os<<"___nomobj = \""<<key<<"\";"<<endl;
    365   os<<"MuTyV & $"<<key<<" = omg.GetVarList().Get(___nomobj);"<<endl;
     370  if (isalpha(key[0]) ) {
     371    os<<"___nomobj = \""<<key<<"\";"<<endl;
     372    os<<"MuTyV & $"<<key<<" = omg.GetVarList().Get(___nomobj);"<<endl;
     373  }
    366374}
    367375
  • trunk/SophyaPI/PIext/cxxexecutor.h

    r1237 r1262  
    2424
    2525  virtual int Execute(string& keyw,vector<string>& args);
     26
     27  int  ExecuteCxx(string const & code);
    2628
    2729  int  CrFile(string cfilename="cxx_spiapp.cc",string func="usercxx");
  • trunk/SophyaPI/PIext/cxxexecwin.cc

    r1251 r1262  
    9292      dap->SetBusy();
    9393      mBut[0]->SetUnSensitive();
    94       //  $CHECK$  REZA + CMV 23/10/2000 - Faire quelque chose de mieux ici
    95       vector<string> vs;
    96       int rc;
    97       vs.push_back(mText->GetText());
    98       rc = cxxex->FillUserCode(vs, 0);
    99       if (rc) break;
    100       rc = cxxex->CrFile();
    101       if (rc) break;
    102       rc = cxxex->Compile();
    103       if (rc) break;
    104       rc = cxxex->Link();
    105       if (rc) break;
    106       rc = cxxex->Call();
    107       if (rc) break;
    108       // End de $CHECK$  REZA + CMV 23/10/2000
     94      cxxex->ExecuteCxx(mText->GetText());
    10995      mBut[0]->SetSensitive();
    11096      dap->SetReady();
  • trunk/SophyaPI/PIext/piacmd.cc

    r1251 r1262  
    130130class PIACmdBloc {
    131131public:
    132                 PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args);
    133                 ~PIACmdBloc();
     132  enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
     133
     134  PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args);
     135  ~PIACmdBloc();
    134136  inline PIACmdBloc*   Parent() { return(parent); }
    135   inline bool   CheckOK() { return((typ >= 0) ? true : false); }
     137  inline bool   CheckOK() { return blkok; }
    136138  inline void   AddLine(string& line) 
    137139    { lines.push_back(line); bloclineid.push_back(lines.size()); }
     
    142144  PIACmd* piacmd;
    143145  PIACmdBloc* parent;
    144   int typ;           // 0 foreach , 1 integer loop, 2 float loop
     146  bool blkok;        // true -> block OK
     147  BType typ;         // foreach , integer loop, float loop, test
    145148  string varname;
    146149  vector<string> strlist;
     
    157160piacmd = piac;
    158161parent = par;
    159 typ = -1;
     162blkok = false;
     163typ = BT_None;
    160164i1 = 0;  i2 = -1;  di = 1;
    161165f1 = 0.; f2 = -1.; df = 1.;
    162166if ((args.size() < 2) ||  !isalpha((int)args[0][0]) )  return;
    163167varname = args[0];
    164 if (isalpha((int)args[1][0]) ) { // This is a foreach-integer bloc
     168if (isalpha((int)args[1][0]) ) { // This is a foreach bloc with string list
    165169  for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
    166   typ = 0;
     170  typ = BT_ForeachList;
    167171  }
    168172else { // This is an integer or float loop
     
    184188  else  a2 = aa;
    185189  if (fl) {
    186     typ = 2;
     190    typ = BT_ForeachFloat;
    187191    f1 = atof(a1.c_str());
    188192    f2 = atof(a2.c_str());
     
    191195    }
    192196  else {
    193     typ = 1;
     197    typ = BT_ForeachInt;
    194198    i1 = atoi(a1.c_str());
    195199    i2 = atoi(a2.c_str());
     
    216220int kk=0;
    217221char buff[32];
    218 if (typ == 0)   // foreach string loop
     222if (typ == BT_ForeachList)   // foreach string loop
    219223  for(k=0; k<strlist.size(); k++) {
    220224    cmd = "set " + varname + " " + strlist[k];
     
    226230      }
    227231  }
    228 else if (typ == 1)  // Integer loop
     232else if (typ == BT_ForeachInt)  // Integer loop
    229233  for(int i=i1; i<i2; i+=di) {
    230234    k++;
     
    242246      }
    243247  }
    244 else if (typ == 2)  // float loop
     248else if (typ == BT_ForeachFloat)  // float loop
    245249  for(float f=f1; f<f2; f+=df) {
    246250    k++;
     
    279283curblk = NULL;
    280284felevel = 0;
     285bloccxx = false;
     286bloctest = false;
    281287
    282288cmdhgrp["All"] = 0;
     
    449455
    450456
    451 //  Pour le decoupage des commandes en lignes
    452 typedef vector<string> cmdtok;
    453457
    454458/* Fonction */
     
    474478{
    475479int rc = 0;
    476 cmdtok tokens;
    477 CmdVarList::iterator it;
    478 
    479 // Removing leading blanks
    480 size_t p,q,q2,l;
     480NamedObjMgr omg;
     481
     482// On saute de commandes vides
     483size_t l;
    481484l = s.length();
    482485if (l < 1)  return(0);
     486
     487// On enregistre les commandes
     488if (histon) hist << s << endl;   
     489
    483490if (s[0] == '#') return(0); // si c'est un commentaire
     491
     492
     493// Removing leading blanks
     494size_t p,q,q2;
     495l = s.length();
     496if (l < 1)  return(0);
     497
    484498p=s.find_first_not_of(" \t");
    485499if (p < l) s = s.substr(p);
    486 // else return(0);
    487 
    488 // On enregistre les commandes
    489 if (histon) hist << s << endl;   
    490 
    491 
    492 string toks,kw;
    493500
    494501// >>>> Substitution d'alias (1er mot)
     502CmdVarList::iterator it;
    495503p = 0;
    496504q = s.find_first_of(" \t");
     
    508516
    509517// >>>> Separating keyword
     518string toks,kw;
    510519if (q < l)
    511520  {  kw = s.substr(p,q-p);  toks = s.substr(q, l-q); }
     
    533542  }
    534543
    535 // Nous ne sommes donc pas dans un bloc ....   
    536 
    537 // >>>> Variable substitution 
    538 string s2="";
     544
     545// Nous ne sommes donc pas dans un bloc ....  Substitution de variables
     546
     547// Execution de code C++
     548
     549if (s[0] == '@')   {
     550  CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
     551  if (cxxe == NULL) {
     552    cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
     553    return(99);
     554  }
     555  // Sans substitution des variables $
     556  if (s[1] == '@') return(cxxe->ExecuteCxx(s.substr(2)));
     557  else { // Avec substitution de variables $
     558    string s2;
     559    SubstituteVars(s, s2);
     560    return(cxxe->ExecuteCxx(s2.substr(1)));   
     561  }
     562}
     563
     564
     565string s2;
     566SubstituteVars(s, s2);
     567
     568// >>>> Separating keyword and tokens
     569vector<string> tokens;
     570
     571q = s2.find(' ');
     572l = s2.length();
     573if (q < l)
     574  {  kw = s2.substr(0,q);  toks = s2.substr(q, l-q); }
     575else { kw = s2;  toks = ""; }
     576
     577q = 0;
     578while (q < l)  {
     579  p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
     580  if (p>=l) break;
     581  q = toks.find_first_of(" \t",p); // la fin du token;
     582  string token = toks.substr(p,q-p);
     583  tokens.push_back(token);
     584  }
     585
     586
     587// Si c'est un foreach, on cree un nouveau bloc
     588if (kw == "foreach") {
     589  //     cout << " *DBG* We got a foreach... " << endl;
     590  PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens);
     591  if (!bloc->CheckOK()) {
     592    cerr << "foreach syntax Error ! " << endl;
     593    delete bloc;
     594    return(0);
     595    }
     596  felevel++;
     597  if (curblk)  curblk->AddBloc(bloc);
     598  else {
     599    char prompt[64];
     600    sprintf(prompt, "foreach-%d> ", felevel);
     601    mImgApp->GetConsole()->SetPrompt(prompt);
     602    }
     603  curblk = bloc;
     604  //  cout << " *DBG* New Bloc created ... " << endl;
     605  return(0);
     606  }
     607
     608else rc = ExecuteCommandLine(kw, tokens, toks);
     609return(rc);
     610
     611// cout << "PIACmd::Do() DBG  KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
     612//  for(int ii=0; ii<tokens.size(); ii++)
     613//  cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
     614
     615}
     616
     617/* --Methode-- */
     618int PIACmd::SubstituteVars(string & s, string & s2)
     619//  Variable substitution 
     620{
     621NamedObjMgr omg;
     622
     623size_t p,q,q2,l;
     624
     625s2="";
    539626p = 0;
    540627l = s.length();
     
    569656      }
    570657    vn = s.substr(q+2,q2-q-2);
    571     it =  mVars.find(vn); 
    572     if ( (vn.length() < 1) || (it == mVars.end()) ) {
     658    if ( (vn.length() < 1) || (!omg.HasVar(vn)) )  {
    573659      cerr << " Error: Undefined variable " << vn << " ! " << endl;
    574660      return(0);
    575661      }
    576     vn = mVars[vn];
     662    vn = omg.GetVar(vn);
    577663    q2++;
    578664    }
     
    582668    vn = s.substr(q+1, q2-q-1);
    583669    }
    584   it =  mVars.find(vn); 
    585   if ( (vn.length() < 1) || (it == mVars.end()) ) {
     670  if ( (vn.length() < 1) || (!omg.HasVar(vn)) )  {
    586671    cerr << " Error: Undefined variable " << vn << " ! " << endl;
    587672    return(0);
    588673    }
    589   s2 += (s.substr(p, q-p) + (*it).second);
     674  s2 += (s.substr(p, q-p) + omg.GetVar(vn));
    590675  p = q2;
    591676  }
     
    595680if (p < l) s2 = s2.substr(p);
    596681
    597 
    598 // >>>> Separating keyword and tokens
    599 q = s2.find(' ');
    600 l = s2.length();
    601 if (q < l)
    602   {  kw = s2.substr(0,q);  toks = s2.substr(q, l-q); }
    603 else { kw = s2;  toks = ""; }
    604 
    605 q = 0;
    606 while (q < l)  {
    607   p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
    608   if (p>=l) break;
    609   q = toks.find_first_of(" \t",p); // la fin du token;
    610   string token = toks.substr(p,q-p);
    611   tokens.push_back(token);
    612   }
    613 
    614 
    615 // Si c'est un foreach, on cree un nouveau bloc
    616 if (kw == "foreach") {
    617   //     cout << " *DBG* We got a foreach... " << endl;
    618   PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens);
    619   if (!bloc->CheckOK()) {
    620     cerr << "foreach syntax Error ! " << endl;
    621     delete bloc;
    622     return(0);
    623     }
    624   felevel++;
    625   if (curblk)  curblk->AddBloc(bloc);
    626   else {
    627     char prompt[64];
    628     sprintf(prompt, "foreach-%d> ", felevel);
    629     mImgApp->GetConsole()->SetPrompt(prompt);
    630     }
    631   curblk = bloc;
    632   //  cout << " *DBG* New Bloc created ... " << endl;
    633   return(0);
    634   }
    635 
    636 
    637 // cout << "PIACmd::Do() DBG  KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
    638 //  for(int ii=0; ii<tokens.size(); ii++)
    639 //  cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
     682return(0);
     683}
     684
     685/* --Methode-- */
     686int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
     687{
     688int rc = 0;
     689NamedObjMgr omg;
    640690
    641691// >>>>>>>>>>> Commande d'interpreteur
    642 else if (kw == "helpwindow") ShowHelpWindow();
     692if (kw == "helpwindow") ShowHelpWindow();
    643693else if (kw == "help") {
    644694  if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
     
    657707  string xx = tokens[1];
    658708  for (int kk=2; kk<tokens.size(); kk++)  xx += (' ' + tokens[kk] );
    659   mVars[tokens[0]] = xx;
     709  omg.SetVar(tokens[0], xx);
    660710  }
    661711
    662712else if (kw == "getvar") {
    663713  if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl;  return(0); }
    664   it = mVars.find(tokens[1]);
    665   if (it == mVars.end()) {
     714  if (!omg.HasVar(tokens[1])) {
    666715    cerr << "Error - No " << tokens[1] << " Variable " << endl;
    667716    return(0);
     
    671720    return(0);
    672721    }
    673   mVars[tokens[0]] = (*it).second;
     722  omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
    674723  }
    675724
     
    699748    for (int kk=1; kk<ol.size(); kk++)  vol += (' ' + ol[kk]);
    700749    }
    701   mVars[tokens[0]] = vol;
     750  omg.SetVar(tokens[0], vol);
    702751  }
    703752else if (kw == "unset") {
    704753  if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl;  return(0); }
    705   CmdVarList::iterator it = mVars.find(tokens[0]);
    706   if (it != mVars.end())  mVars.erase(it);
     754  if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
    707755  else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
    708756  }
     
    720768  mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
    721769  cout << tokens[0] << " ? " << endl;
    722   mVars[tokens[0]] = GetStringFrStdin(this);
     770  omg.SetVar(tokens[0], GetStringFrStdin(this) );
    723771  }
    724772
    725773else if (kw == "listvars") {
    726774  cout << "PIACmd::Interpret()  Variable List , VarName = Value \n";
    727   CmdVarList::iterator it;
    728   for(it = mVars.begin(); it != mVars.end(); it++) 
    729     cout << (*it).first << " = " <<  (*it).second << "\n";
     775  DVList& varlist = omg.GetVarList();
     776  DVList::ValList::const_iterator it;
     777  string value;
     778  for(it = varlist.Begin(); it != varlist.End(); it++) {
     779    value = (string)((*it).second.elval);
     780    cout << (*it).first << " = " <<  value << "\n";
     781    }
    730782  cout << endl;
    731783  }
     
    774826
    775827/* --Methode-- */
    776 int PIACmd::ExecuteCommandLine(string& line)
    777 {
    778 cmdtok tokens;
     828int PIACmd::ParseLineExecute(string& line)
     829{
     830vector<string> tokens;
     831
    779832if (line.length() < 1)  return(0);
    780833
     
    843896char buff[32];
    844897// First, we clear all previous values
     898NamedObjMgr omg;
    845899string vn="#";
    846 it = mVars.find(vn);
    847 if (it != mVars.end())  mVars.erase(it);
     900omg.DeleteVar(vn);
    848901for(k=0; k<99; k++) {   
    849902  sprintf(buff,"%d",k);
    850903  vn = buff;
    851   it = mVars.find(vn);
    852   if (it != mVars.end())  mVars.erase(it);
     904  omg.DeleteVar(vn);
    853905  }
    854906// We then set them
     907string vval;
    855908vn="#";
    856909sprintf(buff,"%d",(int)args.size());
    857 mVars[vn] = buff;
     910omg.SetVar(vn, buff);
    858911for(k=0; k<args.size(); k++) {   
    859912  sprintf(buff,"%d",k);
    860913  vn = buff;
    861   mVars[vn] = args[k];
     914  omg.SetVar(vn, args[k]);
    862915  }
    863916
  • trunk/SophyaPI/PIext/piacmd.h

    r1251 r1262  
    1212#include <string>
    1313#include <vector>
     14#include <list>
    1415#include <map>
    1516#include <functional>
     
    7879
    7980  virtual int           Interpret(string& line);
    80   virtual int           ExecuteCommandLine(string& line);
     81  virtual int           ParseLineExecute(string& line);
    8182  virtual int           ExecuteCommand(string& keyw, vector<string>& args);
    8283  virtual int           ExecFile(string& file, vector<string>& args);
     
    99100protected:
    100101  virtual int           CheckHelpGrp(string& grp);
     102  int           ExecuteCommandLine(string & keyw, vector<string> & args,
     103                                   string & toks);
     104
     105  int           SubstituteVars(string & s, string & s2); 
     106  bool          EvaluateTest(vector<string> & args);
    101107
    102108  NamedObjMgr* mObjMgr;
     
    128134//  Pour stocker les variables definies par l'interpreteur
    129135  typedef map<string, string, less<string> > CmdVarList;
    130   CmdVarList mVars;     // Liste des variables
     136  //  CmdVarList mVars;    Liste des variables
    131137  CmdVarList mAliases;  // Liste des alias
    132138
    133139  PIACmdBloc * curblk;  // Bloc de commande courant (foreach, ...)
    134140  int felevel;          // foreah level
     141  list<bool> testresult;    // Resultat des test if
     142  list<bool>::iterator tresit;      // Test courant
     143  bool bloctest;            // On est ds un bloc test
     144  bool bloccxx;             // On est ds un bloc code C++
     145  string codecxx;
    135146
    136147  ofstream hist;       //  History file
Note: See TracChangeset for help on using the changeset viewer.