23 #ifndef CPPINTERPRETER_H_
24 #define CPPINTERPRETER_H_
28 #include "Reflector.h"
31 #include "StringUtil.h"
32 #include "RegexUtil.h"
35 typedef map<string,Object> mapVars;
36 typedef map<string,string> mapStrs;
46 string getType()
const
51 void setType(
string type)
56 void *getPointer()
const
61 void setPointer(
void *pointer)
63 this->pointer = pointer;
69 mapVars localVariables;
70 mapVars boundVariables;
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);
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)
99 string token = *(itr);
100 vector<string> curr,going;
108 curr.push_back(evalBrackets<T>(itr,enditr));
113 curr.push_back(handleAssignment<T>(going));
119 if(containsChar(token))
121 Object o = localVariables[token];
122 token = CastUtil::lexical_cast<
string>(o.getValue<
int>());
125 going.push_back(token);
127 curr.push_back(token);
131 return handleAssignment<T>(curr);
133 template<
class T>
string handleAssignment(vector<string> opr)
138 bool continu =
false;
143 for(
int i=0;i<(int)opr.size();i++)
145 if(opr.at(i)==
"/" && !continu)
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++)
151 temp.push_back(opr.at(k));
153 temp.push_back(CastUtil::lexical_cast<string>(f));
154 for(
int k=i+2;k<(int)opr.size();k++)
156 temp.push_back(opr.at(k));
171 if(temp.size()>0)opr = temp;
180 for(
int i=0;i<(int)opr.size();i++)
182 if(opr.at(i)==
"*" && !continu)
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++)
188 temp.push_back(opr.at(k));
190 temp.push_back(CastUtil::lexical_cast<string>(f));
191 for(
int k=i+2;k<(int)opr.size();k++)
193 temp.push_back(opr.at(k));
208 if(temp.size()>0)opr = temp;
250 for(
int i=0;i<(int)opr.size();i++)
252 if(opr.at(i)==
"+" && !continu)
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++)
258 temp.push_back(opr.at(k));
260 temp.push_back(CastUtil::lexical_cast<string>(f));
261 for(
int k=i+2;k<(int)opr.size();k++)
263 temp.push_back(opr.at(k));
268 else if(opr.at(i)==
"-" && !continu)
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++)
274 temp.push_back(opr.at(k));
276 temp.push_back(CastUtil::lexical_cast<string>(f));
277 for(
int k=i+2;k<(int)opr.size();k++)
279 temp.push_back(opr.at(k));
294 if(temp.size()>0)opr = temp;
307 template <
class T>
void bind(
string name,T &t)
311 boundVariables[name] = o;
313 template <
class T>
void bind(
string name,T *t)
317 boundVariables[name] = o;