ffead.server.doc
CppInterpreter.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * CppInterpreter.cpp
18  *
19  * Created on: Aug 22, 2009
20  * Author: sumeet
21  */
22 
23 #include "CppInterpreter.h"
24 
25 CppInterpreter::CppInterpreter() {
26  logger = Logger::getLogger("CppInterpreter");
27 }
28 
29 CppInterpreter::~CppInterpreter() {
30  // TODO Auto-generated destructor stub
31 }
32 
33 mapVars CppInterpreter::getLocalVariables() const
34 {
35  return localVariables;
36 }
37 
38 void CppInterpreter::setLocalVariables(mapVars localVariables)
39 {
40  this->localVariables = localVariables;
41 }
42 
43 mapVars CppInterpreter::getBoundVariables() const
44 {
45  return boundVariables;
46 }
47 
48 void CppInterpreter::setBoundVariables(mapVars boundVariables)
49 {
50  this->boundVariables = boundVariables;
51 }
52 
53 bool CppInterpreter::isInBuiltType(string type)
54 {
55  return (type=="int" || type=="float" || type=="double" || type=="string" || type=="std::string");
56 }
57 /*
58 vector<string> splitPattern(string str)
59 {
60  string comm;
61  vector<string> commands;//stack of command structure
62  for(unsigned int l=0;l<str.length();l++)
63  {
64  if(str.substr(l,1)=="+")
65  {
66  if(comm=="")
67  commands.push_back("(");
68  else
69  {
70  comm = comm.substr(comm.find_first_not_of(" "));
71  commands.push_back(comm);
72  commands.push_back("(");
73  }
74  comm = "";
75  }
76  else if(str.substr(l,1)==")")
77  {
78  if(comm=="")
79  commands.push_back(")");
80  else
81  {
82  comm = comm.substr(comm.find_first_not_of(" "));
83  commands.push_back(comm);
84  commands.push_back(")");
85  }
86  comm = "";
87  }
88  else if(str.substr(l,1)=="{")
89  {
90  if(comm=="")
91  commands.push_back("{");
92  else
93  {
94  comm = comm.substr(comm.find_first_not_of(" "));
95  commands.push_back(comm);
96  commands.push_back("{");
97  }
98  comm = "";
99  }
100  else if(str.substr(l,1)=="}")
101  {
102  if(comm=="")
103  commands.push_back("}");
104  else
105  {
106  comm = comm.substr(comm.find_first_not_of(" "));
107  commands.push_back(comm);
108  commands.push_back("}");
109  }
110  comm = "";
111  }
112  else if(str.substr(l,1)==";")
113  {
114  if(comm=="")
115  commands.push_back(";");
116  else
117  {
118  comm = comm.substr(comm.find_first_not_of(" "));
119  commands.push_back(comm);
120  commands.push_back(";");
121  }
122  comm = "";
123  }
124  else
125  comm += str.substr(l,1);
126  }
127  if(comm!="")
128  {
129  comm = comm.substr(comm.find_first_not_of(" "));
130  commands.push_back(comm);
131  }
132  return commands;
133 }
134 */
135 void CppInterpreter::storeInbuilt(string type,string name)
136 {
137  if(type=="int")
138  {
139  int *_temp = new int;
140  Object o;
141  o << *_temp;
142  localVariables[name] = o;
143  }
144  else if(type=="float")
145  {
146  float *_temp = new float;
147  Object o;
148  o << *_temp;
149  localVariables[name] = o;
150  }
151  else if(type=="double")
152  {
153  double *_temp = new double;
154  Object o;
155  o << *_temp;
156  localVariables[name] = o;
157  }
158  else if(type=="string")
159  {
160  string *_temp = new string;
161  Object o;
162  o << *_temp;
163  localVariables[name] = o;
164  }
165 }
166 
167 void CppInterpreter::storeCustom(string type,string name)
168 {
169  Reflector ref;
170  ClassInfo clas = ref.getClassInfo(type);
171  args argus;
172  Constructor ctor = clas.getConstructor(argus);
173  void *_temp = ref.newInstanceGVP(ctor);
174  Object o;
175  o << _temp;
176  o.setTypeName(type);
177  localVariables[name] = o;
178 }
179 
180 bool CppInterpreter::containsChar(string varname)
181 {
182  string allchars = "abcdefghijklmnopqrstuvwxyz_$";
183  for(unsigned int l=0;l<varname.length();l++)
184  {
185  for(unsigned int l1=0;l1<allchars.length();l1++)
186  {
187  if(allchars.substr(l1,1)==varname.substr(l,1))
188  {
189  return true;
190  }
191  }
192  }
193  return false;
194 }
195 
196 
197 bool CppInterpreter::isCommand(string test)
198 {
199  return (test=="if" || test=="else" || test=="elseif" || test=="while" || test=="for");
200 }
201 
202 bool CppInterpreter::retState(string type,Object source,Object target)
203 {
204  if(isInBuiltType(source.getTypeName()))
205  {
206  if(source.getTypeName()=="int")
207  {
208  if(type=="==")
209  return (source.getValue<int>()==target.getValue<int>());
210  else if(type=="<=")
211  return (source.getValue<int>()<=target.getValue<int>());
212  else if(type==">=")
213  return (source.getValue<int>()>=target.getValue<int>());
214  else if(type=="!=")
215  return (source.getValue<int>()!=target.getValue<int>());
216  else if(type==">")
217  return (source.getValue<int>()>target.getValue<int>());
218  else if(type=="<")
219  return (source.getValue<int>()<target.getValue<int>());
220  }
221  else if(source.getTypeName()=="float")
222  {
223  if(type=="==")
224  return (source.getValue<float>()==target.getValue<float>());
225  else if(type=="<=")
226  return (source.getValue<float>()<=target.getValue<float>());
227  else if(type==">=")
228  return (source.getValue<float>()>=target.getValue<float>());
229  else if(type=="!=")
230  return (source.getValue<float>()!=target.getValue<float>());
231  else if(type==">")
232  return (source.getValue<float>()>target.getValue<float>());
233  else if(type=="<")
234  return (source.getValue<float>()<target.getValue<float>());
235  }
236  else if(source.getTypeName()=="double")
237  {
238  if(type=="==")
239  return (source.getValue<double>()==target.getValue<double>());
240  else if(type=="<=")
241  return (source.getValue<double>()<=target.getValue<double>());
242  else if(type==">=")
243  return (source.getValue<double>()>=target.getValue<double>());
244  else if(type=="!=")
245  return (source.getValue<double>()!=target.getValue<double>());
246  else if(type==">")
247  return (source.getValue<double>()>target.getValue<double>());
248  else if(type=="<")
249  return (source.getValue<double>()<target.getValue<double>());
250  }
251  else if(source.getTypeName()=="string")
252  {
253  if(type=="==")
254  return (source.getValue<string>()==target.getValue<string>());
255  else if(type=="<=")
256  return (source.getValue<string>()<=target.getValue<string>());
257  else if(type==">=")
258  return (source.getValue<string>()>=target.getValue<string>());
259  else if(type=="!=")
260  return (source.getValue<string>()!=target.getValue<string>());
261  else if(type==">")
262  return (source.getValue<string>()>target.getValue<string>());
263  else if(type=="<")
264  return (source.getValue<string>()<target.getValue<string>());
265  }
266  }
267  return false;
268 }
269 
270 bool CppInterpreter::retState(string type,Object source,string target)
271 {
272  if(isInBuiltType(source.getTypeName()))
273  {
274  if(source.getTypeName()=="int")
275  {
276  if(type=="==")
277  return (source.getValue<int>()==CastUtil::lexical_cast<int>(target));
278  else if(type=="<=")
279  return (source.getValue<int>()<=CastUtil::lexical_cast<int>(target));
280  else if(type==">=")
281  return (source.getValue<int>()>=CastUtil::lexical_cast<int>(target));
282  else if(type=="!=")
283  return (source.getValue<int>()!=CastUtil::lexical_cast<int>(target));
284  else if(type==">")
285  return (source.getValue<int>()>CastUtil::lexical_cast<int>(target));
286  else if(type=="<")
287  return (source.getValue<int>()<CastUtil::lexical_cast<int>(target));
288  }
289  else if(source.getTypeName()=="float")
290  {
291  if(type=="==")
292  return (source.getValue<float>()==CastUtil::lexical_cast<float>(target));
293  else if(type=="<=")
294  return (source.getValue<float>()<=CastUtil::lexical_cast<float>(target));
295  else if(type==">=")
296  return (source.getValue<float>()>=CastUtil::lexical_cast<float>(target));
297  else if(type=="!=")
298  return (source.getValue<float>()!=CastUtil::lexical_cast<float>(target));
299  else if(type==">")
300  return (source.getValue<float>()>CastUtil::lexical_cast<float>(target));
301  else if(type=="<")
302  return (source.getValue<float>()<CastUtil::lexical_cast<float>(target));
303  }
304  else if(source.getTypeName()=="double")
305  {
306  if(type=="==")
307  return (source.getValue<double>()==CastUtil::lexical_cast<double>(target));
308  else if(type=="<=")
309  return (source.getValue<double>()<=CastUtil::lexical_cast<double>(target));
310  else if(type==">=")
311  return (source.getValue<double>()>=CastUtil::lexical_cast<double>(target));
312  else if(type=="!=")
313  return (source.getValue<double>()!=CastUtil::lexical_cast<double>(target));
314  else if(type==">")
315  return (source.getValue<double>()>CastUtil::lexical_cast<double>(target));
316  else if(type=="<")
317  return (source.getValue<double>()<CastUtil::lexical_cast<double>(target));
318  }
319  else if(source.getTypeName()=="string")
320  {
321  if(type=="==")
322  return (source.getValue<string>()==target);
323  else if(type=="<=")
324  return (source.getValue<string>()<=target);
325  else if(type==">=")
326  return (source.getValue<string>()>=target);
327  else if(type=="!=")
328  return (source.getValue<string>()!=target);
329  else if(type==">")
330  return (source.getValue<string>()>target);
331  else if(type=="<")
332  return (source.getValue<string>()<target);
333  }
334  }
335  return false;
336 }
337 
338 bool CppInterpreter::evaluateCondition(string condition)
339 {
340  bool state = false;
341  string token;
342  if(condition.find("==")!=string::npos)
343  {
344  token = "==";
345  }
346  else if(condition.find("!=")!=string::npos)
347  {
348  token = "!=";
349  }
350  else if(condition.find("<=")!=string::npos || condition.find("=<")!=string::npos)
351  {
352  token = "<=";
353  }
354  else if(condition.find(">=")!=string::npos || condition.find("=>")!=string::npos)
355  {
356  token = ">=";
357  }
358  else if(condition.find(">")!=string::npos)
359  {
360  token = ">";
361  }
362  else if(condition.find("<")!=string::npos)
363  {
364  token = "<";
365  }
366  if(token!="")
367  {
368  vector<string> bs;
369  StringUtil::split(bs, condition, (token));
370  if(bs.size()==2 && bs.at(0)!="" && bs.at(1)!="")
371  {
372  Object source,target;
373  if(localVariables.find(bs.at(0))!=localVariables.end())
374  {
375  source = localVariables[bs.at(0)];
376  }
377  else if(boundVariables.find(bs.at(0))!=boundVariables.end())
378  {
379  source = boundVariables[bs.at(0)];
380  }
381  if(containsChar(bs.at(1)))
382  {
383  if(localVariables.find(bs.at(1))!=localVariables.end())
384  {
385  target = localVariables[bs.at(1)];
386  }
387  else if(boundVariables.find(bs.at(1))!=boundVariables.end())
388  {
389  target = boundVariables[bs.at(1)];
390  }
391  state = retState(token,source,target);
392  }
393  else
394  {
395  state = retState(token,source,bs.at(1));
396  }
397  }
398  }
399  return state;
400 }
401 
402 void CppInterpreter::skipStatement(vector<string>::iterator &itr)
403 {
404  string nextToken = *(itr);
405  while(nextToken!=";")
406  {
407  nextToken = *(++itr);
408  }
409  //++itr;
410 }
411 
412 void CppInterpreter::skipCommand(vector<string>::iterator &itr)
413 {
414  int crcnt = 0;
415  int cucnt = 0;
416  string nextToken = *(itr);
417  while(1)
418  {
419  if(nextToken=="(")
420  {
421  crcnt++;
422  }
423  else if(nextToken==")")
424  {
425  crcnt--;
426  if(crcnt==0)
427  break;
428  }
429  nextToken = *(itr++);
430  }
431  if(nextToken=="{")
432  {
433  while(1)
434  {
435  if(nextToken=="{")
436  cucnt++;
437  else if(nextToken=="}")
438  {
439  cucnt--;
440  if(cucnt==0)
441  {
442  //++itr;
443  break;
444  }
445  }
446  nextToken = *(++itr);
447  }
448  }
449  else
450  {
451  if(isCommand(nextToken))
452  skipCommand(itr);
453  else
454  skipStatement(itr);
455  }
456 }
457 
458 void CppInterpreter::evaluateUpdateCustom(string sep,string type,string name,vector<string> opr,bool local)
459 {
460  Object o;
461  if(local)
462  o = localVariables[name];
463  else
464  o = boundVariables[name];
465  Object i;
466  if(localVariables.find(opr.at(0))!=localVariables.end())
467  {
468  i = localVariables[opr.at(0)];
469  }
470  else if(boundVariables.find(opr.at(0))!=boundVariables.end())
471  {
472  i = boundVariables[opr.at(0)];
473  }
474  if(i.getTypeName()!="" && o.getTypeName()!="")
475  {
476  Reflector reflector;
477  ClassInfo clas = reflector.getClassInfo(type);
478  string name = "invokeReflectionCIAssignMethodFor"+i.getTypeName();
479  args argus;
480  argus.push_back("void*");
481  argus.push_back("void*");
482  Method meth = clas.getMethod(name,argus);
483  vals valus;
484  valus.push_back(o.getVoidPointer());
485  reflector.invokeMethod<void*>(i.getVoidPointer(),meth,valus);
486  }
487 }
488 
489 void CppInterpreter::evaluateUpdateInbuilt(string sep,string type,string name,vector<string> opr,bool local)
490 {
491  if(type=="int")
492  {
493  Object o;
494  if(local)
495  o = localVariables[name];
496  else
497  o = boundVariables[name];
498  int *_temp = (int*)o.getVoidPointer();
499  vector<string> curr,going,temp;
500  //int scnt = 0,ecnt = 0,bcnt = 0;
501  //bool gost= false;
502  if(sep=="++" || sep=="--")
503  opr.clear();
504  /*for(unsigned int i=0;i<opr.size();i++)
505  {
506  if(opr.at(i)=="(")
507  {
508  //ecnt++;
509  scnt++;
510  gost = true;
511  //ecnt = scnt+1;
512  if(ecnt)
513  {
514  going.clear();
515  gost = true;
516  }
517  }
518  else if(opr.at(i)==")")
519  {
520  ecnt++;
521  if(scnt==ecnt)
522  {
523  curr.push_back(handleAssignment<int>(going));
524  //curr = going;
525  going.clear();
526  }
527  else
528  curr.push_back(handleAssignment<int>(going));
529  going.clear();
530  gost = false;
531  }
532  else if(gost)
533  going.push_back(opr.at(i));
534  else
535  curr.push_back(opr.at(i));
536 
537  }*/
538  vector<string>::iterator itre,endre;
539  itre = opr.begin();
540  endre = opr.end();
541  string res = evalBrackets<int>(itre,endre);
542  /*opr.clear();
543  for(unsigned int i=0;i<curr.size();i++)
544  {
545  if(containsChar(curr.at(i)))
546  {
547  Object o = localVariables[curr.at(i)];
548  opr.push_back(CastUtil::lexical_cast<string>(o.getValue<int>()));
549  }
550  else
551  opr.push_back(curr.at(i));
552  }
553  if(sep=="=")
554  *_temp = CastUtil::lexical_cast<int>(handleAssignment<int>(opr));
555  else if(sep=="+=")
556  *_temp = *_temp + CastUtil::lexical_cast<int>(handleAssignment<int>(opr));
557  else if(sep=="-=")
558  *_temp = *_temp - CastUtil::lexical_cast<int>(handleAssignment<int>(opr));
559  else if(sep=="++")
560  *_temp = (*_temp+1);
561  else if(sep=="--")
562  *_temp = (*_temp-1);*/
563  if(sep=="=")
564  *_temp = CastUtil::lexical_cast<int>(res);
565  else if(sep=="+=")
566  *_temp = *_temp + CastUtil::lexical_cast<int>(res);
567  else if(sep=="-=")
568  *_temp = *_temp - CastUtil::lexical_cast<int>(res);
569  else if(sep=="++")
570  *_temp = (*_temp+1);
571  else if(sep=="--")
572  *_temp = (*_temp-1);
573  }
574  /*else if(type=="float")
575  {
576  float *_temp = new float;
577  Object o;
578  o << *_temp;
579  localVariables[name] = o;
580  }
581  else if(type=="double")
582  {
583  double *_temp = new double;
584  Object o;
585  o << *_temp;
586  localVariables[name] = o;
587  }
588  else if(type=="string" || type=="std::string")
589  {
590  string *_temp = new string;
591  Object o;
592  o << *_temp;
593  localVariables[name] = o;
594  }*/
595 }
596 
597 void CppInterpreter::executeStatement(string sep,vector<string> lhs,vector<string> rhs)
598 {
599  if(sep!="")
600  {
601  if(lhs.size()>0)//probable declaration
602  {
603  vector<string> bs;
604  StringUtil::split(bs, lhs.at(0), (" "));
605  if(bs.size()==1)
606  {
607  Object source;
608  bool local = false;
609  if(localVariables.find(bs.at(0))!=localVariables.end())
610  {
611  source = localVariables[bs.at(0)];
612  local = true;
613  }
614  else if(boundVariables.find(bs.at(0))!=boundVariables.end())
615  {
616  source = boundVariables[bs.at(0)];
617  }
618  if(isInBuiltType(source.getTypeName()))
619  {
620  evaluateUpdateInbuilt(sep,source.getTypeName(),bs.at(0),rhs,local);
621  }
622  else
623  {
624  evaluateUpdateCustom(sep,bs.at(0),bs.at(1),rhs,local);
625  }
626  }
627  else
628  {
629  if(isInBuiltType(bs.at(0)))
630  {
631  storeInbuilt(bs.at(0),bs.at(1));
632  evaluateUpdateInbuilt(sep,bs.at(0),bs.at(1),rhs,true);
633  }
634  else
635  {
636  storeCustom(bs.at(0),bs.at(1));
637  evaluateUpdateCustom(sep,bs.at(0),bs.at(1),rhs,true);
638  }
639  }
640  }
641  }
642  else
643  {
644  vector<string> bs;
645  StringUtil::split(bs, lhs.at(0), (" "));
646  if(isInBuiltType(bs.at(0)))
647  {
648  storeInbuilt(bs.at(0),bs.at(1));
649  //evaluateUpdateInbuilt(sep,bs.at(0),bs.at(1),rhs,true);
650  }
651  else
652  {
653  storeCustom(bs.at(0),bs.at(1));
654  //evaluateUpdateCustom(sep,bs.at(0),bs.at(1),rhs,true);
655  }
656  }
657 }
658 
659 Obj CppInterpreter::handleObjectMethodInvocation(string objn,string methn,vector<string>::iterator &itr)
660 {
661  string token = *(++itr);
662  int crcnt = 1;
663  string statement;
664  vals valus;
665  args argus;
666  Obj obj;
667  string objname,methName;
668  bool objmem = false;
669  while(1)
670  {
671  if(token=="(")
672  {
673  crcnt++;
674  if(objmem)
675  {
676  methName = statement;
677  obj = handleObjectMethodInvocation(objname,methName,itr);
678  statement = "";
679  }
680  }
681  else if(token==")")
682  {
683  crcnt--;
684  if(crcnt==0)
685  {
686  if(obj.getType()!="")
687  {
688  valus.push_back(obj.getPointer());
689  argus.push_back(obj.getType());
690  obj.setPointer(NULL);
691  obj.setType("");
692  }
693  else if(statement!="")
694  {
695  Object src;
696  if(localVariables.find(statement)!=localVariables.end())
697  {
698  src = localVariables[statement];
699  }
700  else if(boundVariables.find(statement)!=boundVariables.end())
701  {
702  src = boundVariables[statement];
703  }
704  if(src.getTypeName()!="")
705  {
706  argus.push_back(src.getTypeName());
707  valus.push_back(src.getVoidPointer());
708  }
709  }
710  break;
711  }
712  }
713  else if((token=="." || token=="->"))
714  {
715  objmem = true;
716  objname = statement;
717  }
718  else if(token==",")
719  {
720  if(obj.getType()!="")
721  {
722  valus.push_back(obj.getPointer());
723  argus.push_back(obj.getType());
724  obj.setPointer(NULL);
725  obj.setType("");
726  }
727  else if(statement!="")
728  {
729  Object src;
730  if(localVariables.find(statement)!=localVariables.end())
731  {
732  src = localVariables[statement];
733  }
734  else if(boundVariables.find(statement)!=boundVariables.end())
735  {
736  src = boundVariables[statement];
737  }
738  if(src.getTypeName()!="")
739  {
740  argus.push_back(src.getTypeName());
741  valus.push_back(obj.getPointer());
742  }
743  }
744  }
745  statement += token;
746  token = *(++itr);
747  }
748 
749  Object source;
750  if(localVariables.find(objn)!=localVariables.end())
751  {
752  source = localVariables[objn];
753  }
754  else if(boundVariables.find(objn)!=boundVariables.end())
755  {
756  source = boundVariables[objn];
757  }
758  if(source.getTypeName()!="")
759  {
760  Reflector reflector;
761  ClassInfo clas = reflector.getClassInfo(source.getTypeName());
762  Method meth = clas.getMethod(methn,argus);
763  void *po = reflector.invokeMethodGVP(source.getVoidPointer(),meth,valus);
764  obj.setPointer(po);
765  obj.setType(meth.getReturnType());
766  }
767  return obj;
768 }
769 
770 void CppInterpreter::handleStatement(vector<string>::iterator &itr)
771 {
772  string token = *itr;
773  string statement;
774  string sep;
775  vector<string> lhs,rhs;
776  string objname,methName;
777  bool rhsa = false,objmem = false;
778  int argn = 1;
779  bool objectex = false;
780  while(token!=";")
781  {
782  if(token=="=")
783  {
784  //rhs.push_back(statement);
785  statement = "";
786  sep = "=";
787  //rhs.push_back("=");
788  }
789  else if(token=="-=")
790  {
791  //rhs.push_back(statement);
792  statement = "";
793  sep = "-=";
794  //rhs.push_back("=");
795  }
796  else if(token=="+=")
797  {
798  //rhs.push_back(statement);
799  statement = "";
800  sep = "+=";
801  //rhs.push_back("=");
802  }
803  else if(token=="+")
804  {
805  rhsa = true;
806  if(statement!="")rhs.push_back(statement);
807  statement = "";
808  rhs.push_back("+");
809  }
810  else if(token=="-")
811  {
812  rhsa = true;
813  if(statement!="")rhs.push_back(statement);
814  statement = "";
815  rhs.push_back("-");
816  }
817  else if(token=="*")
818  {
819  rhsa = true;
820  if(statement!="")rhs.push_back(statement);
821  statement = "";
822  rhs.push_back("*");
823  }
824  else if(token=="/")
825  {
826  rhsa = true;
827  if(statement!="")rhs.push_back(statement);
828  statement = "";
829  rhs.push_back("/");
830  }
831  else if(token=="(")
832  {
833  if(objmem)
834  {
835  Obj obj = handleObjectMethodInvocation(objname,methName,itr);
836  if(obj.getType()!="" && sep!="")
837  {
838  Object o;
839  o << obj.getPointer();
840  o.setTypeName(obj.getType());
841  string argname = "_argno"+CastUtil::lexical_cast<string>(argn++);
842  localVariables[argname] = o;
843  //rhs.push_back(argname);
844  token = *(itr);
845  statement = argname;
846  rhsa = true;
847  objmem = false;
848  }
849  else
850  objectex = true;
851  }
852  else
853  {
854  rhsa = true;
855  if(statement!="")rhs.push_back(statement);
856  statement = "";
857  rhs.push_back("(");
858  }
859  }
860  else if(token==")")
861  {
862  rhsa = true;
863  if(statement!="")rhs.push_back(statement);
864  statement = "";
865  rhs.push_back(")");
866  }
867  else if(token=="++")
868  {
869  //rhs.push_back(statement);
870  statement = "";
871  sep = "++";
872  //rhs.push_back("=");
873  }
874  else if(token=="--")
875  {
876  //rhs.push_back(statement);
877  statement = "";
878  sep = "--";
879  //rhs.push_back("=");
880  }
881  else if(token=="--")
882  {
883  //rhs.push_back(statement);
884  statement = "";
885  sep = "--";
886  //rhs.push_back("=");
887  }
888  else if(token.find(".")!=string::npos)
889  {
890  objmem = true;
891  vector<string> bs;
892  StringUtil::split(bs,token, ("."));
893  objname = bs.at(0);
894  methName = bs.at(1);
895  }
896  if(sep=="")
897  {
898  lhs.push_back(token);
899  }
900  else if(sep!=token && !rhsa)
901  {
902  statement += token;
903  }
904  rhsa = false;
905  token = *(++itr);
906  }
907  if(sep!=token && statement!="")
908  rhs.push_back(statement);
909  if(!objectex)
910  executeStatement(sep,lhs,rhs);
911  return;
912 }
913 
914 bool CppInterpreter::evalCond(vector<string> str)
915 {
916  vector<string>::iterator iter;
917  iter = str.begin();
918  bool state = true;
919  string st = *(iter);
920  string prev,cond;
921  int crcnt = 0;
922  while(1)
923  {
924  if(st=="(")
925  {
926  crcnt++;
927  cond = "";
928  }
929  else if(st==")")
930  {
931  crcnt--;
932  if(prev=="&")
933  {
934  state = state && evaluateCondition(cond);
935  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
936  }
937  else if(prev=="|")
938  {
939  state = state || evaluateCondition(cond);
940  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
941  }
942  else if(cond!="")
943  {
944  state = evaluateCondition(cond);
945  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
946  }
947  prev = "";
948  cond = "";
949  if(crcnt==0)
950  break;
951  }
952  else if(st=="||")
953  {
954  if(prev=="&")
955  {
956  state = state && evaluateCondition(cond);
957  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
958  }
959  else if(prev=="|")
960  {
961  state = state || evaluateCondition(cond);
962  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
963  }
964  else if(cond!="")
965  {
966  state = evaluateCondition(cond);
967  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
968  }
969  prev = "|";
970  cond = "";
971  }
972  else if(st=="&&")
973  {
974  if(prev=="&")
975  {
976  state = state && evaluateCondition(cond);
977  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
978  }
979  else if(prev=="|")
980  {
981  state = state || evaluateCondition(cond);
982  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
983  }
984  else if(cond!="")
985  {
986  state = evaluateCondition(cond);
987  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
988  }
989  prev = "&";
990  cond = "";
991  }
992  else
993  cond += st;
994  ++iter;
995  if(iter<str.end())
996  st = *(iter);
997  else
998  break;
999  }
1000  if(cond!="" && cond!=" ")
1001  {
1002  logger << "state=" << state << "\n" << flush;
1003  state = evaluateCondition(cond);
1004  }
1005  return state;
1006 }
1007 
1008 void CppInterpreter::handleELSE(vector<string>::iterator &iter)
1009 {
1010  string nextToken = *(++iter);
1011  int cucnt = 0;
1012  if(nextToken=="{")
1013  {
1014  while(1)
1015  {
1016  if(nextToken=="{")
1017  cucnt++;
1018  else if(nextToken=="}")
1019  {
1020  cucnt--;
1021  if(cucnt==0)
1022  break;
1023  }
1024  else if(isCommand(nextToken))
1025  hanldeCommand(iter);
1026  else
1027  handleStatement(iter);
1028  nextToken = *(++iter);
1029  }
1030  }
1031  else
1032  {
1033  bool gone = false;
1034  if(isCommand(nextToken))
1035  {
1036  hanldeCommand(iter);
1037  gone = true;
1038  }
1039  else
1040  {
1041  handleStatement(iter);
1042  gone = true;
1043  }
1044  if(gone)
1045  nextToken = *(++iter);
1046  }
1047 }
1048 
1049 
1050 void CppInterpreter::hanldeIF(vector<string>::iterator &iter)
1051 {
1052  bool state = true;
1053  string st = *(++iter);
1054  string prev,cond;
1055  int crcnt = 0;
1056  while(1)
1057  {
1058  if(st=="(")
1059  {
1060  crcnt++;
1061  cond = "";
1062  }
1063  else if(st==")")
1064  {
1065  crcnt--;
1066  if(prev=="&")
1067  {
1068  state = state && evaluateCondition(cond);
1069  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1070  }
1071  else if(prev=="|")
1072  {
1073  state = state || evaluateCondition(cond);
1074  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1075  }
1076  else if(cond!="")
1077  {
1078  state = evaluateCondition(cond);
1079  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1080  }
1081  prev = "";
1082  cond = "";
1083  if(crcnt==0)
1084  break;
1085  }
1086  else if(st=="||")
1087  {
1088  if(prev=="&")
1089  {
1090  state = state && evaluateCondition(cond);
1091  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1092  }
1093  else if(prev=="|")
1094  {
1095  state = state || evaluateCondition(cond);
1096  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1097  }
1098  else if(cond!="")
1099  {
1100  state = evaluateCondition(cond);
1101  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1102  }
1103  prev = "|";
1104  cond = "";
1105  }
1106  else if(st=="&&")
1107  {
1108  if(prev=="&")
1109  {
1110  state = state && evaluateCondition(cond);
1111  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1112  }
1113  else if(prev=="|")
1114  {
1115  state = state || evaluateCondition(cond);
1116  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1117  }
1118  else if(cond!="")
1119  {
1120  state = evaluateCondition(cond);
1121  logger << "prev=" << prev << " cond=" << cond << " state=" << state << "\n" << flush;
1122  }
1123  prev = "&";
1124  cond = "";
1125  }
1126  else
1127  cond += st;
1128  st = *(++iter);
1129  }
1130  string nextToken = *(++iter);
1131  if(state)
1132  {
1133  int cucnt = 0;
1134  if(nextToken=="{")
1135  {
1136  while(1)
1137  {
1138  if(nextToken=="{")
1139  cucnt++;
1140  else if(nextToken=="}")
1141  {
1142  cucnt--;
1143  if(cucnt==0)
1144  break;
1145  }
1146  else if(isCommand(nextToken))
1147  hanldeCommand(iter);
1148  else
1149  handleStatement(iter);
1150  nextToken = *(++iter);
1151  }
1152  }
1153  else
1154  {
1155  bool gone = false;
1156  if(isCommand(nextToken))
1157  {
1158  hanldeCommand(iter);
1159  gone = true;
1160  }
1161  else
1162  {
1163  handleStatement(iter);
1164  gone = true;
1165  }
1166  if(gone)
1167  nextToken = *(++iter);
1168  }
1169  }
1170  else
1171  {
1172  if(nextToken=="{")
1173  {
1174  int cucnt = 0;
1175  while(1)
1176  {
1177  if(nextToken=="{")
1178  cucnt++;
1179  else if(nextToken=="}")
1180  {
1181  cucnt--;
1182  if(cucnt==0)
1183  {
1184  //nextToken = *(++iter);
1185  break;
1186  }
1187  }
1188  nextToken = *(++iter);
1189  }
1190  }
1191  else
1192  {
1193  bool gone = false;
1194  if(isCommand(nextToken))
1195  {
1196  skipCommand(iter);
1197  gone = true;
1198  }
1199  else
1200  {
1201  skipStatement(iter);
1202  gone = true;
1203  }
1204  if(gone)
1205  nextToken = *(++iter);
1206  }
1207  }
1208  if(nextToken=="else")
1209  {
1210  nextToken = *(++iter);
1211  if(nextToken=="if")
1212  {
1213  hanldeIF(iter);
1214  }
1215  else if(!state)
1216  {
1217  --iter;
1218  handleELSE(iter);
1219  }
1220  }
1221 }
1222 
1223 void CppInterpreter::hanldeWHILE(vector<string>::iterator &itr)
1224 {
1225  string st = *(++itr);
1226  string prev,cond;
1227  int crcnt = 0;
1228  vector<string> whlstr;
1229  while(1)
1230  {
1231  whlstr.push_back(st);
1232  if(st=="(")
1233  {
1234  crcnt++;
1235  }
1236  else if(st==")")
1237  {
1238  crcnt--;
1239  if(crcnt==0)
1240  break;
1241  }
1242  st = *(++itr);
1243  }
1244  string nextToken = *(++itr);
1245  vector<string> stmt;
1246  bool blk = false;
1247  //bool whd = false;
1248  if(nextToken=="{")
1249  {
1250  int cucnt = 0;
1251  while(1)
1252  {
1253  stmt.push_back(nextToken);
1254  if(nextToken=="{")
1255  cucnt++;
1256  else if(nextToken=="}")
1257  {
1258  cucnt--;
1259  if(cucnt==0)
1260  {
1261  break;
1262  }
1263  }
1264  nextToken = *(++itr);
1265  }
1266  blk = true;
1267  }
1268  else
1269  {
1270  while(nextToken!=";")
1271  {
1272  stmt.push_back(nextToken);
1273  nextToken = *(++itr);
1274  }
1275  stmt.push_back(nextToken);
1276  }
1277  while(evalCond(whlstr))
1278  {
1279  //whd = true;
1280  vector<string>::iterator tok = stmt.begin();
1281  string stok = *(tok);
1282  int cucnt = 0;
1283  while(tok<stmt.end())
1284  {
1285  if(blk)
1286  {
1287  while(1)
1288  {
1289  if(stok=="{")
1290  cucnt++;
1291  else if(stok=="}")
1292  {
1293  cucnt--;
1294  if(cucnt==0)
1295  break;
1296  }
1297  else if(isCommand(stok))
1298  hanldeCommand(tok);
1299  else
1300  handleStatement(tok);
1301  stok = *(++tok);
1302  }
1303  ++tok;
1304  }
1305  else
1306  {
1307  bool gone = false;
1308  if(isCommand(stok))
1309  {
1310  hanldeCommand(tok);
1311  gone = true;
1312  }
1313  else
1314  {
1315  handleStatement(tok);
1316  gone = true;
1317  }
1318  if(gone)
1319  stok = *(++tok);
1320  }
1321  }
1322  }
1323 }
1324 
1325 void CppInterpreter::hanldeFOR(vector<string>::iterator &itr)
1326 {
1327  string st = *(++itr);
1328  string prev,cond;
1329  int crcnt = 0;
1330  bool condit=false,inc=false;
1331  vector<string> whlstr,incr;
1332  while(1)
1333  {
1334  if(st!=";" && condit)
1335  whlstr.push_back(st);
1336  if(st=="(")
1337  {
1338  crcnt++;
1339  ++itr;
1340  handleStatement(itr);
1341  condit = true;
1342  }
1343  else if(st==";")
1344  {
1345  condit = false;
1346  inc = true;
1347  }
1348  else if(st==")")
1349  {
1350  crcnt--;
1351  if(crcnt==0)
1352  {
1353  incr.push_back(";");
1354  break;
1355  }
1356  }
1357  if(st!=";" && inc)
1358  incr.push_back(st);
1359  st = *(++itr);
1360  }
1361  string nextToken = *(++itr);
1362  vector<string> stmt;
1363  bool blk = false;
1364  //bool whd = false;
1365  if(nextToken=="{")
1366  {
1367  int cucnt = 0;
1368  while(1)
1369  {
1370  stmt.push_back(nextToken);
1371  if(nextToken=="{")
1372  cucnt++;
1373  else if(nextToken=="}")
1374  {
1375  cucnt--;
1376  if(cucnt==0)
1377  {
1378  stmt.erase(stmt.begin()+stmt.size()-1);
1379  if(incr.at(0)=="++" || incr.at(0)=="--")
1380  {
1381  for(unsigned int l=0;l<stmt.size();l++)
1382  {
1383  incr.push_back(stmt.at(l));
1384  }
1385  stmt = incr;
1386  }
1387  else
1388  {
1389  for(unsigned int l=0;l<incr.size();l++)
1390  {
1391  stmt.push_back(incr.at(l));
1392  }
1393  }
1394  stmt.push_back(nextToken);
1395  break;
1396  }
1397  }
1398  nextToken = *(++itr);
1399  }
1400  blk = true;
1401  }
1402  else
1403  {
1404  while(nextToken!=";")
1405  {
1406  stmt.push_back(nextToken);
1407  nextToken = *(++itr);
1408  }
1409  stmt.push_back(nextToken);
1410  }
1411  while(evalCond(whlstr))
1412  {
1413  //whd = true;
1414  vector<string>::iterator tok = stmt.begin();
1415  string stok = *(tok);
1416  int cucnt = 0;
1417  while(tok<stmt.end())
1418  {
1419  if(blk)
1420  {
1421  while(1)
1422  {
1423  if(stok=="{")
1424  cucnt++;
1425  else if(stok=="}")
1426  {
1427  cucnt--;
1428  if(cucnt==0)
1429  break;
1430  }
1431  else if(isCommand(stok))
1432  hanldeCommand(tok);
1433  else
1434  handleStatement(tok);
1435  stok = *(++tok);
1436  }
1437  ++tok;
1438  }
1439  else
1440  {
1441  bool gone = false;
1442  if(isCommand(stok))
1443  {
1444  hanldeCommand(tok);
1445  gone = true;
1446  }
1447  else
1448  {
1449  handleStatement(tok);
1450  gone = true;
1451  }
1452  if(gone)
1453  stok = *(++tok);
1454  }
1455  }
1456  }
1457 }
1458 
1459 void CppInterpreter::hanldeCommand(vector<string>::iterator &itr)
1460 {
1461  if(*itr=="if" || *itr=="else if")
1462  {
1463  hanldeIF(itr);
1464  }
1465  else if(*itr=="for")
1466  {
1467  hanldeFOR(itr);
1468  }
1469  else if(*itr=="while")
1470  {
1471  hanldeWHILE(itr);
1472  }
1473 }
1474 
1475 
1476 void CppInterpreter::eval(string str)
1477 {
1478  logger << str << flush;
1479  string temp = str;
1480  int st = 1;
1481  while(temp.find("\"")!=string::npos)
1482  {
1483  size_t s = temp.find_first_of("\"")+1;
1484  string temp1 = temp.substr(s);
1485  size_t e = temp1.find_first_of("\"");
1486  string litval;
1487  litval = temp.substr(s,e);
1488  temp = temp.substr(e+s+1);
1489  string varn = ("_"+ CastUtil::lexical_cast<string>(st++));
1490  literals[varn] = litval;
1491  string ini = str.substr(0,s-1);
1492  str = (ini + varn + temp);
1493  temp = str;
1494  logger << "\n" << flush;
1495  logger << litval << flush;
1496  logger << "\n" << flush;
1497  logger << s << e << flush;
1498  logger << "\n" << flush;
1499  logger << temp << flush;
1500  logger << "\n" << flush;
1501  }
1502  logger << str << flush;
1503  RegexUtil::replace(str, "[\t]+", " ");
1504  RegexUtil::replace(str, "[ ]+", " ");
1505  logger << "\n" << flush;
1506  logger << str << flush;
1507  logger << "\n" << flush;
1508 
1509  string comm;
1510  vector<string> commands;//stack of command structure
1511  bool eqop = false;
1512  string command;
1513  for(unsigned int l=0;l<str.length();l++)
1514  {
1515  if(str.substr(l,1)=="{" || str.substr(l,1)=="}" || str.substr(l,1)==";" || str.substr(l,1)==")"
1516  || str.substr(l,1)=="(" || str.substr(l,1)=="+" || str.substr(l,1)=="-" || str.substr(l,1)=="/"
1517  || str.substr(l,1)=="*" || str.substr(l,1)=="=" || str.substr(l,1)=="!" || str.substr(l,1)=="|"
1518  || str.substr(l,1)=="&" || str.substr(l,1)=="<" || str.substr(l,1)==">")// || str.substr(l,1)==".")
1519  {
1520  if(comm=="")
1521  {
1522  if(eqop)
1523  {
1524  eqop = false;
1525  }
1526  else if(!eqop)
1527  {
1528  commands.push_back(str.substr(l,1));
1529  }
1530  }
1531  else
1532  {
1533  if(comm.find_first_not_of(" ")!=string::npos && comm.find_last_not_of(" ")!=string::npos)
1534  {
1535  comm = comm.substr(comm.find_first_not_of(" "),comm.find_last_not_of(" ")+1);
1536  commands.push_back(comm);
1537  }
1538  if(str.substr(l,2)=="==" || str.substr(l,2)=="||" || str.substr(l,2)=="&&" || str.substr(l,2)=="!="
1539  || str.substr(l,2)==">=" || str.substr(l,2)=="<=" || str.substr(l,2)=="=>" || str.substr(l,2)=="=<"
1540  || str.substr(l,2)=="<<" || str.substr(l,2)==">>" || str.substr(l,2)=="++" || str.substr(l,2)=="--"
1541  || str.substr(l,2)=="+=" || str.substr(l,2)=="-=")
1542  {
1543  eqop=true;
1544  commands.push_back(str.substr(l,2));
1545  }
1546  else if(!eqop)
1547  commands.push_back(str.substr(l,1));
1548  }
1549  comm = "";
1550  }
1551  else
1552  {
1553  comm += str.substr(l,1);
1554  if(comm=="else ")
1555  {
1556  commands.push_back("else");
1557  comm = "";
1558  }
1559  }
1560  }
1561  if(comm!="")
1562  {
1563  comm = comm.substr(comm.find_first_not_of(" "));
1564  commands.push_back(comm);
1565  }
1566  vector<string>::iterator iter;
1567  iter = commands.begin();
1568  string nextToken = *(iter);
1569  while(iter<commands.end())
1570  {
1571  int cucnt = 0;
1572  if(nextToken=="{")
1573  {
1574  while(1)
1575  {
1576  if(nextToken=="{")
1577  cucnt++;
1578  else if(nextToken=="}")
1579  {
1580  cucnt--;
1581  if(cucnt==0)
1582  break;
1583  }
1584  else if(isCommand(nextToken))
1585  hanldeCommand(iter);
1586  else
1587  handleStatement(iter);
1588  nextToken = *(++iter);
1589  }
1590  }
1591  else
1592  {
1593  if(isCommand(nextToken))
1594  hanldeCommand(iter);
1595  else
1596  handleStatement(iter);
1597  }
1598  ++iter;
1599  if(iter<commands.end())
1600  nextToken = *(iter);
1601  }
1602  //while(iter!=commands.end())
1603  //{
1604  //logger << commands.at(k) << "\n" << flush;
1605  /*string curr = *(iter++);
1606  int crcnt = 0;
1607  vector<string> state;
1608  string cond;
1609  string prev;
1610  while(curr=="if")
1611  {
1612  string st = *(iter++);
1613  if(st=="(")
1614  {
1615  crcnt++;
1616  cond = "";
1617  }
1618  else if(st==")")
1619  {
1620  crcnt--;
1621  state.push_back(evaluate(cond,"IF"));
1622  logger << cond << flush;
1623  if(crcnt==0)
1624  break;
1625  }
1626  else if(st=="||")
1627  {
1628  state.push_back(evaluate(cond,"IF"));
1629  state.push_back("OR");
1630  logger << cond << flush;
1631  logger << " OR " << flush;
1632  cond = "";
1633  prev = "OR";
1634  }
1635  else if(st=="&&")
1636  {
1637  state.push_back(evaluate(cond,"IF"));
1638  state.push_back("AND");
1639  logger << cond << flush;
1640  logger << " AND " << flush;
1641  cond = "";
1642  prev = "AND";
1643  }
1644  else
1645  cond += st;
1646  }*/
1647  /*if(commands.at(k).find("=")!=string::npos)//inititialization operation
1648  {
1649  vector<string> bs,lhs;
1650  StringUtil::split(bs, commands.at(k), ("="));
1651  StringUtil::split(lhs, bs.at(0), (" "));
1652  if(lhs.size()==2)
1653  {
1654  if(isInBuiltType(lhs.at(0)))
1655  {
1656  storeInbuilt(lhs.at(0),lhs.at(1));
1657  evaluateUpdateInbuilt(lhs.at(0),lhs.at(1),bs.at(1),true);
1658  }
1659  else
1660  {
1661  storeCustom(lhs.at(0),lhs.at(1));
1662  }
1663  }
1664  else
1665  {
1666  bool local = true;
1667  Object target = localVariables[lhs.at(0)];
1668  if(target.getTypeName()=="")
1669  {
1670  target = boundVariables[lhs.at(0)];
1671  mapVars::iterator it;
1672  it=localVariables.find(lhs.at(0));
1673  localVariables.erase (it);
1674  local = false;
1675  }
1676  if(isInBuiltType(target.getTypeName()))
1677  evaluateUpdateInbuilt(target.getTypeName(),lhs.at(0),bs.at(1),local);
1678  }
1679  }*/
1680  /*else if(tokens.size()==4 && tokens.at(2)=="=")//assignment and initialization operations
1681  {
1682  if(isInBuiltType(tokens.at(0)))
1683  {
1684  storeInbuilt(tokens.at(0),tokens.at(1));
1685  }
1686  else
1687  {
1688  storeCustom(tokens.at(0),tokens.at(1));
1689  }
1690  Object target = localVariables[tokens.at(1)];
1691  if(target.getType()=="")
1692  target = boundVariables[tokens.at(1)];
1693  Object source = localVariables[tokens.at(1)];
1694  if(source.getType()=="")
1695  source = boundVariables[tokens.at(1)];
1696  if(target.getType()!="" && source.getType()!="" && target.getType()==source.getType())
1697  target.setPointer(source.getPointer());
1698  else
1699  logger << "run time exception" << flush;
1700  }
1701  else if(tokens.size()==3 && tokens.at(1)=="=")//assignment operation
1702  {
1703  Object target = localVariables[tokens.at(0)];
1704  if(target.getType()=="")
1705  target = boundVariables[tokens.at(0)];
1706  Object source = localVariables[tokens.at(2)];
1707  if(source.getType()=="")
1708  source = boundVariables[tokens.at(2)];
1709  if(target.getType()!="" && source.getType()!="" && target.getType()==source.getType())
1710  target.setPointer(source.getPointer());
1711  else
1712  logger << "run time exception" << flush;
1713  }
1714  else if(tokens.size()>=5 && tokens.at(1)=="=")//multiple opeartions and final addition on this line
1715  {
1716 
1717  }*/
1718  //}
1719 }