ffead.server.doc
CppInterpreter.h
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.h
18  *
19  * Created on: Aug 22, 2009
20  * Author: sumeet
21  */
22 
23 #ifndef CPPINTERPRETER_H_
24 #define CPPINTERPRETER_H_
25 #include "string"
26 #include "map"
27 #include "Object.h"
28 #include "Reflector.h"
29 #include "iostream"
30 #include "CastUtil.h"
31 #include "StringUtil.h"
32 #include "RegexUtil.h"
33 #include "bitset"
34 #include "Logger.h"
35 typedef map<string,Object> mapVars;
36 typedef map<string,string> mapStrs;
37 using namespace std;
38 
39 class Obj
40 {
41  string type;
42  void* pointer;
43 public:
44  Obj(){}
45  ~Obj(){}
46  string getType() const
47  {
48  return type;
49  }
50 
51  void setType(string type)
52  {
53  this->type = type;
54  }
55 
56  void *getPointer() const
57  {
58  return pointer;
59  }
60 
61  void setPointer(void *pointer)
62  {
63  this->pointer = pointer;
64  }
65 
66 };
68  Logger logger;
69  mapVars localVariables;
70  mapVars boundVariables;
71  mapStrs literals;
72  mapVars getLocalVariables() const;
73  void setLocalVariables(mapVars);
74  mapVars getBoundVariables() const;
75  void setBoundVariables(mapVars);
76  bool isInBuiltType(string);
77  void storeInbuilt(string,string);
78  void storeCustom(string,string);
79  void evaluateUpdateInbuilt(string,string,string,vector<string>,bool);
80  void evaluateUpdateCustom(string,string,string,vector<string>,bool);
81  void executeStatement(string sep,vector<string> lhs,vector<string> rhs);
82  bool evaluateCondition(string condition);
83  bool evalCond(vector<string> str);
84  bool isCommand(string test);
85  bool containsChar(string varname);
86  bool retState(string type,Object source,Object target);
87  bool retState(string type,Object source,string target);
88  void hanldeIF(vector<string>::iterator &iter);
89  void handleELSE(vector<string>::iterator &iter);
90  void hanldeFOR(vector<string>::iterator &iter);
91  void hanldeWHILE(vector<string>::iterator &iter);
92  void hanldeCommand(vector<string>::iterator &itr);
93  void handleStatement(vector<string>::iterator &itr);
94  void skipStatement(vector<string>::iterator &itr);
95  void skipCommand(vector<string>::iterator &itr);
96  Obj handleObjectMethodInvocation(string,string,vector<string>::iterator &itr);
97  template<class T> string evalBrackets(vector<string>::iterator &itr,vector<string>::iterator enditr)
98  {
99  string token = *(itr);
100  vector<string> curr,going;
101  bool st = false;
102  while(itr<enditr)
103  {
104  token = *(itr);
105  if(token=="(")
106  {
107  if(st)
108  curr.push_back(evalBrackets<T>(itr,enditr));
109  st = true;
110  }
111  else if(token==")")
112  {
113  curr.push_back(handleAssignment<T>(going));
114  st = false;
115  going.clear();
116  }
117  else
118  {
119  if(containsChar(token))
120  {
121  Object o = localVariables[token];
122  token = CastUtil::lexical_cast<string>(o.getValue<int>());
123  }
124  if(st)
125  going.push_back(token);
126  else
127  curr.push_back(token);
128  }
129  ++itr;
130  }
131  return handleAssignment<T>(curr);
132  }
133  template<class T> string handleAssignment(vector<string> opr)
134  {
135  vector<string> temp;
136  while(opr.size()>1)
137  {
138  bool continu = false;
139  temp.clear();
140  while(1)
141  {
142  //unsigned int found = -1;
143  for(int i=0;i<(int)opr.size();i++)
144  {
145  if(opr.at(i)=="/" && !continu)
146  {
147  T f;
148  f = CastUtil::lexical_cast<T>(opr.at(i-1))/CastUtil::lexical_cast<T>(opr.at(i+1));
149  for(int k=0;k<(i-1);k++)
150  {
151  temp.push_back(opr.at(k));
152  }
153  temp.push_back(CastUtil::lexical_cast<string>(f));
154  for(int k=i+2;k<(int)opr.size();k++)
155  {
156  temp.push_back(opr.at(k));
157  }
158  continu = true;
159  //found = i;
160  }
161  /*else if((opr.size()>(i+2) && opr.at(i+2)!='/') && i!=found && i!=found+1 && i!=found-1)
162  {
163  temp.push_back(opr.at(i));
164  }*/
165  }
166  if(!continu)
167  break;
168  else
169  {
170  continu = false;
171  if(temp.size()>0)opr = temp;
172  temp.clear();
173  }
174  }
175  continu = false;
176  temp.clear();
177  while(1)
178  {
179  //unsigned int found = -1;
180  for(int i=0;i<(int)opr.size();i++)
181  {
182  if(opr.at(i)=="*" && !continu)
183  {
184  T f;
185  f = CastUtil::lexical_cast<T>(opr.at(i-1))*CastUtil::lexical_cast<T>(opr.at(i+1));
186  for(int k=0;k<i-1;k++)
187  {
188  temp.push_back(opr.at(k));
189  }
190  temp.push_back(CastUtil::lexical_cast<string>(f));
191  for(int k=i+2;k<(int)opr.size();k++)
192  {
193  temp.push_back(opr.at(k));
194  }
195  continu = true;
196  //found = i;
197  }
198  /*else if(i!=found && i!=found+1 && i!=found-1)
199  {
200  temp.push_back(opr.at(i));
201  }*/
202  }
203  if(!continu)
204  break;
205  else
206  {
207  continu = false;
208  if(temp.size()>0)opr = temp;
209  temp.clear();
210  }
211  }
212  continu = false;
213  temp.clear();
214  /*while(1)
215  {
216  unsigned int found = -1;
217  for(int i=0;i<(int)opr.size();i++)
218  {
219  if(opr.at(i)=="-" && !continu)
220  {
221  T f;
222  f = CastUtil::lexical_cast<T>(opr.at(i-1))-CastUtil::lexical_cast<T>(opr.at(i+1));
223  for(int k=0;k<i-1;k++)
224  {
225  temp.push_back(opr.at(k));
226  }
227  temp.push_back(CastUtil::lexical_cast<string>(f));
228  for(int k=i+2;k<(int)opr.size();k++)
229  {
230  temp.push_back(opr.at(k));
231  }
232  continu = true;
233  found = i;
234  }
235  }
236  if(!continu)
237  break;
238  else
239  {
240  continu = false;
241  if(temp.size()>0)opr = temp;
242  temp.clear();
243  }
244  }
245  continu = false;
246  temp.clear();*/
247  while(1)
248  {
249  //unsigned int found = -1;
250  for(int i=0;i<(int)opr.size();i++)
251  {
252  if(opr.at(i)=="+" && !continu)
253  {
254  T f;
255  f = CastUtil::lexical_cast<T>(opr.at(i-1))+CastUtil::lexical_cast<T>(opr.at(i+1));
256  for(int k=0;k<i-1;k++)
257  {
258  temp.push_back(opr.at(k));
259  }
260  temp.push_back(CastUtil::lexical_cast<string>(f));
261  for(int k=i+2;k<(int)opr.size();k++)
262  {
263  temp.push_back(opr.at(k));
264  }
265  continu = true;
266  //found = i;
267  }
268  else if(opr.at(i)=="-" && !continu)
269  {
270  T f;
271  f = CastUtil::lexical_cast<T>(opr.at(i-1))+CastUtil::lexical_cast<T>(opr.at(i)+opr.at(i+1));
272  for(int k=0;k<i-1;k++)
273  {
274  temp.push_back(opr.at(k));
275  }
276  temp.push_back(CastUtil::lexical_cast<string>(f));
277  for(int k=i+2;k<(int)opr.size();k++)
278  {
279  temp.push_back(opr.at(k));
280  }
281  continu = true;
282  //found = i;
283  }
284  /*else if(i!=found && i!=found+1 && i!=found-1)
285  {
286  temp.push_back(opr.at(i));
287  }*/
288  }
289  if(!continu)
290  break;
291  else
292  {
293  continu = false;
294  if(temp.size()>0)opr = temp;
295  temp.clear();
296  }
297  }
298  }
299  //if(opr.size()>0)
300  return opr.at(0);
301  //return "";
302  }
303 public:
304  CppInterpreter();
305  virtual ~CppInterpreter();
306  void eval(string);
307  template <class T> void bind(string name,T &t)
308  {
309  Object o;
310  o << t;
311  boundVariables[name] = o;
312  }
313  template <class T> void bind(string name,T *t)
314  {
315  Object o;
316  o << t;
317  boundVariables[name] = o;
318  }
319  /*template <class T> void bind(map<string,T> mapT)
320  {
321  map<string,string>::iterator it;
322  for(it=mapT.begin();it!=mapT.end();++it)
323  {
324  Object o;
325  o << it->second;
326  boundVariables[it->first] = o;
327  }
328  }
329  template <class T> T getVariable(string name)
330  {
331  Object o = localVariables[name];
332  return o.getValue<T>();
333  }
334  template <class T> T getCollectionVariable(string name,string type,string index="")
335  {
336  if(type=="vector")
337  {
338 
339  }
340  else if(type=="deque")
341  {
342 
343  }
344  else if(type=="list")
345  {
346 
347  }
348  else if(type=="stack")
349  {
350 
351  }
352  else if(type=="queue")
353  {
354 
355  }
356  else if(type=="priority_queue")
357  {
358 
359  }
360  else if(type=="set")
361  {
362 
363  }
364  else if(type=="multiset")
365  {
366 
367  }
368  else if(type=="map")
369  {
370 
371  }
372  else if(type=="bitset")
373  {
374 
375  }
376  }*/
377 };
378 
379 #endif /* CPPINTERPRETER_H_ */