Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1. /*
  2. * parsetree.h
  3. */
  4.  
  5. #ifndef PARSETREE_H_
  6. #define PARSETREE_H_
  7.  
  8. #include "value.h"
  9. #include <vector>
  10. #include <map>
  11. #include <string>
  12. using std::vector;
  13. using std::map;
  14.  
  15. // NodeType represents all possible types
  16. enum NodeType { ERRTYPE, INTTYPE, STRTYPE, BOOLTYPE, IDENTTYPE};
  17.  
  18. // a "forward declaration" for a class to hold values
  19. class Value;
  20.  
  21. class ParseTree {
  22. public:
  23. int linenum;
  24. ParseTree *left;
  25. ParseTree *right;
  26.  
  27. public:
  28. ParseTree(int linenum, ParseTree *l = 0, ParseTree *r = 0)
  29. : linenum(linenum), left(l), right(r) {}
  30.  
  31. virtual ~ParseTree() {
  32. delete left;
  33. delete right;
  34. }
  35.  
  36. int GetLinenum() const { return linenum; }
  37. virtual string GetID() const { return "NO WORKING"; }
  38. virtual NodeType GetType() const { return ERRTYPE; }
  39.  
  40. int LeafCount() const {
  41. int lc = 0;
  42. if( left ) lc += left->LeafCount();
  43. if( right ) lc += right->LeafCount();
  44. if( left == 0 && right == 0 )
  45. lc++;
  46. return lc;
  47. }
  48.  
  49. virtual bool IsIdent() const { return false; }
  50. virtual bool IsString() const { return false; }
  51.  
  52. virtual string GetId() const { return ""; }
  53.  
  54. virtual Value Eval(map<string, Value> *symbolMap) {
  55. return Value();
  56. }
  57.  
  58. int IdentCount() const {
  59. int cnt = 0;
  60. if( left ) cnt += left->IdentCount();
  61. if( right ) cnt += right->IdentCount();
  62. if( IsIdent() )
  63. cnt++;
  64. return cnt;
  65. }
  66.  
  67. int StringCount() const {
  68. int cnt = 0;
  69. if( left ) cnt += left->StringCount();
  70. if( right ) cnt += right->StringCount();
  71. if( IsString() )
  72. cnt++;
  73. return cnt;
  74. }
  75.  
  76. void GetVars(map<string,int>& var) {
  77. if( left ) left->GetVars(var);
  78. if( right ) right->GetVars(var);
  79. if( IsIdent() )
  80. var[ this->GetId() ]++;
  81. }
  82. };
  83.  
  84. class StmtList : public ParseTree {
  85.  
  86. public:
  87. StmtList(ParseTree *l, ParseTree *r) : ParseTree(0, l, r) {}
  88.  
  89. Value Eval(map<string, Value> *symbolMap) {
  90. Value leftVal = left->Eval(symbolMap);
  91. if(leftVal.hasMessage())
  92. {
  93. cout<<std::to_string(GetLinenum())+": RUNTIME ERROR " + leftVal.getMessage()<<endl;
  94.  
  95. }
  96. else if(right!=0)
  97. {
  98.  
  99. Value rightVal = right->Eval(symbolMap);
  100. if(rightVal.hasMessage()){
  101. cout<<std::to_string(GetLinenum())+": RUNTIME ERROR " + rightVal.getMessage()<<endl;
  102. }
  103. }
  104. return Value();
  105. }
  106.  
  107. };
  108.  
  109. class IfStatement : public ParseTree {
  110. public:
  111. IfStatement(int line, ParseTree *ex, ParseTree *stmt) : ParseTree(line, ex, stmt) {}
  112.  
  113. Value Eval(map<string, Value> *symbolMap) {
  114. Value expr = left->Eval(symbolMap);
  115. if(expr.isBoolType())
  116. {
  117. if(expr.getBoolean()){
  118. Value stmt = right->Eval(symbolMap);
  119. return stmt;
  120. }
  121.  
  122. return Value();
  123. }
  124. int line = GetLinenum();
  125. string err = ": RUNTIME ERROR if does not have boolean value";
  126. return Value(std::to_string(line) + err, true);
  127. }
  128. };
  129.  
  130. class Assignment : public ParseTree {
  131. public:
  132. Assignment(int line, ParseTree *lhs, ParseTree *rhs) : ParseTree(line, lhs, rhs) {}
  133.  
  134. Value Eval(map<string, Value> *symbolMap) {
  135. Value assignValue = right->Eval(symbolMap);
  136. if(left->GetType()==IDENTTYPE)
  137. {
  138.  
  139. (*symbolMap)[left->GetID()]=assignValue;
  140. return Value();
  141. }
  142. else{
  143. int line = GetLinenum();
  144. string err = ": RUNTIME ERROR only identifiers can be assigned a value";
  145. return Value(std::to_string(line) + err, true);
  146. }
  147. }
  148. };
  149.  
  150. class PrintStatement : public ParseTree {
  151. public:
  152. PrintStatement(int line, ParseTree *e) : ParseTree(line, e) {}
  153.  
  154. Value Eval(map<string, Value> *symbolMap) {
  155. Value printMsg = left->Eval(symbolMap);
  156.  
  157. if(left->GetType()==IDENTTYPE){
  158. map<string, Value>::iterator it = symbolMap->find(left->GetID());
  159. if(it == symbolMap->end()){
  160. int line = GetLinenum();
  161. string err = ": RUNTIME ERROR cannot print an unassigned identifier";
  162. return Value(std::to_string(line) + err, true);
  163. }
  164. }
  165. if(printMsg.isError()){
  166. return Value(printMsg.getMessage(), true);
  167. }
  168. cout<<printMsg<<endl;
  169. return Value();
  170. }
  171. };
  172.  
  173. class PlusExpr : public ParseTree {
  174. public:
  175. PlusExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  176.  
  177. Value Eval(map<string, Value> *symbolMap) {
  178. return left->Eval(symbolMap)+right->Eval(symbolMap);
  179. }
  180. };
  181.  
  182. class MinusExpr : public ParseTree {
  183. public:
  184. MinusExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  185.  
  186. Value Eval(map<string, Value> *symbolMap) {
  187.  
  188. return left->Eval(symbolMap)-right->Eval(symbolMap);
  189. }
  190. };
  191.  
  192. class TimesExpr : public ParseTree {
  193. public:
  194. TimesExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  195.  
  196. Value Eval(map<string, Value> *symbolMap) {
  197.  
  198. return left->Eval(symbolMap)*right->Eval(symbolMap);
  199. }
  200. };
  201.  
  202. class DivideExpr : public ParseTree {
  203. public:
  204. DivideExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  205.  
  206. Value Eval(map<string, Value> *symbolMap) {
  207.  
  208. return left->Eval(symbolMap) / right->Eval(symbolMap);
  209. }
  210. };
  211.  
  212. class LogicAndExpr : public ParseTree {
  213. public:
  214. LogicAndExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  215.  
  216. Value Eval(map<string, Value> *symbolMap) {
  217.  
  218. return left->Eval(symbolMap) && right->Eval(symbolMap);
  219. }
  220.  
  221. NodeType GetType() const { return BOOLTYPE; }
  222. };
  223.  
  224. class LogicOrExpr : public ParseTree {
  225. public:
  226. LogicOrExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  227.  
  228. Value Eval(map<string, Value> *symbolMap) {
  229.  
  230. return left->Eval(symbolMap) || right->Eval(symbolMap);
  231.  
  232. }
  233.  
  234. NodeType GetType() const { return BOOLTYPE; }
  235. };
  236.  
  237. class EqExpr : public ParseTree {
  238. public:
  239. EqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  240.  
  241. Value Eval(map<string, Value> *symbolMap) {
  242.  
  243. return left->Eval(symbolMap) == right->Eval(symbolMap);
  244. }
  245.  
  246. NodeType GetType() const { return BOOLTYPE; }
  247. };
  248.  
  249. class NEqExpr : public ParseTree {
  250. public:
  251. NEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  252.  
  253. Value Eval(map<string, Value> *symbolMap) {
  254.  
  255. return left->Eval(symbolMap) != right->Eval(symbolMap);
  256. }
  257.  
  258. NodeType GetType() const { return BOOLTYPE; }
  259. };
  260.  
  261. class LtExpr : public ParseTree {
  262. public:
  263. LtExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  264.  
  265. Value Eval(map<string, Value> *symbolMap) {
  266.  
  267. return left->Eval(symbolMap) < right->Eval(symbolMap);
  268. }
  269.  
  270. NodeType GetType() const { return BOOLTYPE; }
  271. };
  272.  
  273. class LEqExpr : public ParseTree {
  274. public:
  275. LEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  276.  
  277. Value Eval(map<string, Value> *symbolMap) {
  278.  
  279. return left->Eval(symbolMap) <= right->Eval(symbolMap);
  280. }
  281.  
  282. NodeType GetType() const { return BOOLTYPE; }
  283. };
  284.  
  285. class GtExpr : public ParseTree {
  286. public:
  287. GtExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  288.  
  289. Value Eval(map<string, Value> *symbolMap) {
  290.  
  291. return left->Eval(symbolMap) > right->Eval(symbolMap);
  292. }
  293.  
  294. NodeType GetType() const { return BOOLTYPE; }
  295. };
  296.  
  297. class GEqExpr : public ParseTree {
  298. public:
  299. GEqExpr(int line, ParseTree *l, ParseTree *r) : ParseTree(line,l,r) {}
  300.  
  301. Value Eval(map<string, Value> *symbolMap) {
  302.  
  303. return left->Eval(symbolMap) >= right->Eval(symbolMap);
  304. }
  305.  
  306. NodeType GetType() const { return BOOLTYPE; }
  307. };
  308.  
  309. class IConst : public ParseTree {
  310. int val;
  311.  
  312. public:
  313. IConst(int l, int i) : ParseTree(l), val(i) {}
  314. IConst(Token& t) : ParseTree(t.GetLinenum()) {
  315. val = stoi(t.GetLexeme());
  316. }
  317.  
  318. Value Eval(map<string, Value> *symbolMap) {
  319. return Value(val);
  320. }
  321.  
  322. NodeType GetType() const { return INTTYPE; }
  323. };
  324.  
  325. class BoolConst : public ParseTree {
  326. bool val;
  327.  
  328. public:
  329. BoolConst(Token& t, bool val) : ParseTree(t.GetLinenum()), val(val) {}
  330.  
  331. Value Eval(map<string, Value> *symbolMap) {
  332.  
  333. return Value(val);
  334. }
  335.  
  336. NodeType GetType() const { return BOOLTYPE; }
  337. };
  338.  
  339. class SConst : public ParseTree {
  340. string val;
  341.  
  342. public:
  343. SConst(Token& t) : ParseTree(t.GetLinenum()) {
  344. val = t.GetLexeme();
  345. }
  346.  
  347. NodeType GetType() const { return STRTYPE; }
  348. bool IsString() const { return true; }
  349.  
  350. Value Eval(map<string, Value> *symbolMap) {
  351. return Value(val);
  352. }
  353. };
  354.  
  355. class Ident : public ParseTree {
  356. string id;
  357.  
  358. public:
  359. Ident(Token& t) : ParseTree(t.GetLinenum()), id(t.GetLexeme()) {}
  360.  
  361. NodeType GetType() const { return IDENTTYPE; }
  362.  
  363. Value Eval(map<string, Value> *symbolMap) {
  364. if(symbolMap->count(id))
  365. return symbolMap->at(id);
  366. else
  367. return Value("An unassigned identifier cant be used", true);
  368. }
  369.  
  370. bool IsIdent() const { return true; }
  371. string GetID() const { return id; }
  372. };
  373.  
  374. #endif /* PARSETREE_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement