Advertisement
m4v1

AST.h

Nov 14th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #ifndef _AST_H_
  2. #define _AST_H_
  3.  
  4. #include <string>
  5. #include <list>
  6. #include <map>
  7.  
  8. using namespace std;
  9.  
  10. enum DataType {DT_Int, DT_Double};
  11.  
  12. struct VValue {
  13.     double doubleValue() {
  14.     if (type == DT_Int)
  15.         return (double)u.ivalue;
  16.     else
  17.         return u.dvalue;
  18.     }
  19.    
  20.     int intValue() {
  21.     if (type == DT_Int)
  22.         return u.ivalue;
  23.     else
  24.         return (int)u.dvalue;
  25.     }
  26.    
  27.     DataType type;
  28.    
  29.     union {
  30.         int ivalue;
  31.         double dvalue;
  32.     } u;
  33. };
  34.  
  35. extern map<string, VValue> vars;
  36.  
  37. class Declaration;
  38.  
  39. typedef list<string> StringList;
  40. typedef list<Declaration*> DeclarationList;
  41.  
  42. class Declaration {
  43. public:
  44.     Declaration(DataType type, StringList &idList) {
  45.         this->type = type;
  46.         this->idList = idList;
  47.     }
  48.    
  49.     DataType type;
  50.     StringList idList;  
  51. };
  52.  
  53.  
  54. class Expr {
  55. public:
  56.     virtual VValue evaluate() = 0;
  57. };
  58.  
  59. class BinaryExpr: public Expr {
  60. public:
  61.     BinaryExpr(Expr *expr1, Expr *expr2) {
  62.         this->expr1 = expr1;
  63.         this->expr2 = expr2;
  64.     }
  65.    
  66.     Expr *expr1;
  67.     Expr *expr2;
  68. };
  69.  
  70. class LTExpr: public BinaryExpr {
  71. public:
  72.     LTExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  73.    
  74.     VValue evaluate();
  75. };
  76.  
  77. class GTExpr: public BinaryExpr {
  78. public:
  79.     GTExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  80.    
  81.     VValue evaluate();
  82. };
  83.  
  84. class LTEExpr: public BinaryExpr {
  85. public:
  86.     LTEExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  87.    
  88.     VValue evaluate();
  89. };
  90.  
  91. class GTEExpr: public BinaryExpr {
  92. public:
  93.     GTEExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  94.    
  95.     VValue evaluate();
  96. };
  97.  
  98. class NEExpr: public BinaryExpr {
  99. public:
  100.     NEExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  101.    
  102.     VValue evaluate();
  103. };
  104.  
  105. class EQExpr: public BinaryExpr {
  106. public:
  107.     EQExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  108.    
  109.     VValue evaluate();
  110. };
  111.  
  112. class AddExpr: public BinaryExpr {
  113. public:
  114.     AddExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  115.    
  116.     VValue evaluate();
  117. };
  118.  
  119. class SubExpr: public BinaryExpr {
  120. public:
  121.     SubExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  122.    
  123.     VValue evaluate();
  124. };
  125.  
  126. class MultExpr: public BinaryExpr {
  127. public:
  128.     MultExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  129.    
  130.     VValue evaluate();
  131. };
  132.  
  133. class DivExpr: public BinaryExpr {
  134. public:
  135.     DivExpr(Expr *expr1, Expr *expr2): BinaryExpr(expr1, expr2) {}
  136.    
  137.     VValue evaluate();
  138. };
  139.  
  140. class NumExpr: public Expr {
  141. public:
  142.     NumExpr(int value) {
  143.         this->value.type = DT_Int;
  144.         this->value.u.ivalue = value;
  145.     }
  146.     NumExpr(double value) {
  147.         this->value.type = DT_Double;
  148.         this->value.u.dvalue = value;
  149.     }
  150.     VValue evaluate() { return value; }
  151.    
  152.     VValue value;
  153. };
  154.  
  155. class IdExpr: public Expr {
  156. public:
  157.     IdExpr(string id) { this->id = id; }
  158.     VValue evaluate();
  159.    
  160.     string id;
  161. };
  162.  
  163. class IncExpr: public Expr {
  164. public:
  165.     IncExpr(string id) { this->id = id; }
  166.     VValue evaluate();
  167.    
  168.     string id;
  169. };
  170.  
  171. class DecExpr: public Expr {
  172. public:
  173.     DecExpr(string id) { this->id = id; }
  174.     VValue evaluate();
  175.    
  176.     string id;
  177. };
  178.  
  179. enum StatementKind {
  180.     BLOCK_STATEMENT,
  181.     PRINT_STATEMENT,
  182.     ASSIGN_STATEMENT,
  183.     IF_STATEMENT,
  184.     WHILE_STATEMENT,
  185.     FOR_STATEMENT,
  186.     EXPR_STATEMENT
  187. };
  188.  
  189. class Statement {
  190. public:
  191.     virtual void execute() = 0;
  192.     virtual StatementKind getKind() = 0;
  193. };
  194.  
  195. class BlockStatement: public Statement {
  196. public:
  197.     BlockStatement(list<Statement *> stList) { this->stList = stList; }
  198.     void execute();
  199.     StatementKind getKind() { return BLOCK_STATEMENT; }
  200.    
  201.     list<Statement *> stList;
  202. };
  203.  
  204. class PrintStatement: public Statement {
  205. public:
  206.     PrintStatement(Expr *expr) { this->expr = expr; }
  207.     void execute();
  208.     StatementKind getKind() { return PRINT_STATEMENT; }
  209.    
  210.     Expr *expr;
  211. };
  212.  
  213. class AssignStatement: public Statement {
  214. public:
  215.     AssignStatement(string id, Expr *expr) {
  216.         this->id = id;
  217.         this->expr = expr;
  218.     }
  219.     void execute();
  220.     StatementKind getKind() { return ASSIGN_STATEMENT; }
  221.    
  222.     string id;
  223.     Expr *expr;
  224. };
  225.  
  226. class IfStatement: public Statement {
  227. public:
  228.     IfStatement(Expr *cond, Statement *trueBlock, Statement *falseBlock) {
  229.         this->cond = cond;
  230.         this->trueBlock = trueBlock;
  231.         this->falseBlock = falseBlock;
  232.     }
  233.     void execute();
  234.     StatementKind getKind() { return IF_STATEMENT; }
  235.    
  236.     Expr *cond;
  237.     Statement *trueBlock;
  238.     Statement *falseBlock;
  239. };
  240.  
  241. class WhileStatement: public Statement {
  242. public:
  243.     WhileStatement(Expr *cond, Statement *loopBody) {
  244.         this->cond = cond;
  245.         this->loopBody = loopBody;
  246.     }
  247.     void execute();
  248.     StatementKind getKind() { return WHILE_STATEMENT; }
  249.    
  250.     Expr *cond;
  251.     Statement *loopBody;
  252. };
  253.  
  254. class ForStatement: public Statement {
  255. public:
  256.     ForStatement(Statement *initAssign, Expr *cond, Statement *incAssign, Statement *loopBody) {
  257.         this->initAssign = initAssign;
  258.         this->cond = cond;
  259.         this->incAssign = incAssign;
  260.         this->loopBody = loopBody;
  261.     }
  262.     void execute();
  263.     StatementKind getKind() { return FOR_STATEMENT; }
  264.    
  265.     Statement *initAssign;
  266.     Statement *incAssign;
  267.     Expr *cond;
  268.     Statement *loopBody;
  269. };
  270.  
  271. class ExprStatement: public Statement {
  272. public:
  273.     ExprStatement(Expr *expr) {
  274.         this->expr = expr;
  275.     }
  276.     void execute();
  277.     StatementKind getKind() { return EXPR_STATEMENT; }
  278.    
  279.     Expr *expr;
  280. };
  281.  
  282.  
  283. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement