|
| 1 | +#ifndef __AST_H__ |
| 2 | +#define __AST_H__ |
| 3 | + |
| 4 | +#include <fstream> |
| 5 | +#include "Operand.h" |
| 6 | + |
| 7 | +class SymbolEntry; |
| 8 | +class Unit; |
| 9 | +class Function; |
| 10 | +class BasicBlock; |
| 11 | +class Instruction; |
| 12 | +class IRBuilder; |
| 13 | + |
| 14 | +class Node |
| 15 | +{ |
| 16 | +private: |
| 17 | + static int counter; |
| 18 | + int seq; |
| 19 | +protected: |
| 20 | + std::vector<Instruction*> true_list; |
| 21 | + std::vector<Instruction*> false_list; |
| 22 | + static IRBuilder *builder; |
| 23 | + void backPatch(std::vector<Instruction*> &list, BasicBlock*bb); |
| 24 | + std::vector<Instruction*> merge(std::vector<Instruction*> &list1, std::vector<Instruction*> &list2); |
| 25 | + |
| 26 | +public: |
| 27 | + Node(); |
| 28 | + int getSeq() const {return seq;}; |
| 29 | + static void setIRBuilder(IRBuilder*ib) {builder = ib;}; |
| 30 | + virtual void output(int level) = 0; |
| 31 | + virtual void typeCheck() = 0; |
| 32 | + virtual void genCode() = 0; |
| 33 | + std::vector<Instruction*>& trueList() {return true_list;} |
| 34 | + std::vector<Instruction*>& falseList() {return false_list;} |
| 35 | +}; |
| 36 | + |
| 37 | +class ExprNode : public Node |
| 38 | +{ |
| 39 | +protected: |
| 40 | + SymbolEntry *symbolEntry; |
| 41 | + Operand *dst; // The result of the subtree is stored into dst. |
| 42 | +public: |
| 43 | + ExprNode(SymbolEntry *symbolEntry) : symbolEntry(symbolEntry){}; |
| 44 | + Operand* getOperand() {return dst;}; |
| 45 | + SymbolEntry* getSymPtr() {return symbolEntry;}; |
| 46 | +}; |
| 47 | + |
| 48 | +class BinaryExpr : public ExprNode |
| 49 | +{ |
| 50 | +private: |
| 51 | + int op; |
| 52 | + ExprNode *expr1, *expr2; |
| 53 | +public: |
| 54 | + enum {ADD, SUB, AND, OR, LESS, GREATER}; |
| 55 | + BinaryExpr(SymbolEntry *se, int op, ExprNode*expr1, ExprNode*expr2) : ExprNode(se), op(op), expr1(expr1), expr2(expr2){dst = new Operand(se);}; |
| 56 | + void output(int level); |
| 57 | + void typeCheck(); |
| 58 | + void genCode(); |
| 59 | +}; |
| 60 | + |
| 61 | +class Constant : public ExprNode |
| 62 | +{ |
| 63 | +public: |
| 64 | + Constant(SymbolEntry *se) : ExprNode(se){dst = new Operand(se);}; |
| 65 | + void output(int level); |
| 66 | + void typeCheck(); |
| 67 | + void genCode(); |
| 68 | +}; |
| 69 | + |
| 70 | +class Id : public ExprNode |
| 71 | +{ |
| 72 | +public: |
| 73 | + Id(SymbolEntry *se) : ExprNode(se){SymbolEntry *temp = new TemporarySymbolEntry(se->getType(), SymbolTable::getLabel()); dst = new Operand(temp);}; |
| 74 | + void output(int level); |
| 75 | + void typeCheck(); |
| 76 | + void genCode(); |
| 77 | +}; |
| 78 | + |
| 79 | +class StmtNode : public Node |
| 80 | +{}; |
| 81 | + |
| 82 | +class CompoundStmt : public StmtNode |
| 83 | +{ |
| 84 | +private: |
| 85 | + StmtNode *stmt; |
| 86 | +public: |
| 87 | + CompoundStmt(StmtNode *stmt) : stmt(stmt) {}; |
| 88 | + void output(int level); |
| 89 | + void typeCheck(); |
| 90 | + void genCode(); |
| 91 | +}; |
| 92 | + |
| 93 | +class SeqNode : public StmtNode |
| 94 | +{ |
| 95 | +private: |
| 96 | + StmtNode *stmt1, *stmt2; |
| 97 | +public: |
| 98 | + SeqNode(StmtNode *stmt1, StmtNode *stmt2) : stmt1(stmt1), stmt2(stmt2){}; |
| 99 | + void output(int level); |
| 100 | + void typeCheck(); |
| 101 | + void genCode(); |
| 102 | +}; |
| 103 | + |
| 104 | +class DeclStmt : public StmtNode |
| 105 | +{ |
| 106 | +private: |
| 107 | + Id *id; |
| 108 | +public: |
| 109 | + DeclStmt(Id *id) : id(id){}; |
| 110 | + void output(int level); |
| 111 | + void typeCheck(); |
| 112 | + void genCode(); |
| 113 | +}; |
| 114 | + |
| 115 | +class IfStmt : public StmtNode |
| 116 | +{ |
| 117 | +private: |
| 118 | + ExprNode *cond; |
| 119 | + StmtNode *thenStmt; |
| 120 | +public: |
| 121 | + IfStmt(ExprNode *cond, StmtNode *thenStmt) : cond(cond), thenStmt(thenStmt){}; |
| 122 | + void output(int level); |
| 123 | + void typeCheck(); |
| 124 | + void genCode(); |
| 125 | +}; |
| 126 | + |
| 127 | +class IfElseStmt : public StmtNode |
| 128 | +{ |
| 129 | +private: |
| 130 | + ExprNode *cond; |
| 131 | + StmtNode *thenStmt; |
| 132 | + StmtNode *elseStmt; |
| 133 | +public: |
| 134 | + IfElseStmt(ExprNode *cond, StmtNode *thenStmt, StmtNode *elseStmt) : cond(cond), thenStmt(thenStmt), elseStmt(elseStmt) {}; |
| 135 | + void output(int level); |
| 136 | + void typeCheck(); |
| 137 | + void genCode(); |
| 138 | +}; |
| 139 | + |
| 140 | +class ReturnStmt : public StmtNode |
| 141 | +{ |
| 142 | +private: |
| 143 | + ExprNode *retValue; |
| 144 | +public: |
| 145 | + ReturnStmt(ExprNode*retValue) : retValue(retValue) {}; |
| 146 | + void output(int level); |
| 147 | + void typeCheck(); |
| 148 | + void genCode(); |
| 149 | +}; |
| 150 | + |
| 151 | +class AssignStmt : public StmtNode |
| 152 | +{ |
| 153 | +private: |
| 154 | + ExprNode *lval; |
| 155 | + ExprNode *expr; |
| 156 | +public: |
| 157 | + AssignStmt(ExprNode *lval, ExprNode *expr) : lval(lval), expr(expr) {}; |
| 158 | + void output(int level); |
| 159 | + void typeCheck(); |
| 160 | + void genCode(); |
| 161 | +}; |
| 162 | + |
| 163 | +class FunctionDef : public StmtNode |
| 164 | +{ |
| 165 | +private: |
| 166 | + SymbolEntry *se; |
| 167 | + StmtNode *stmt; |
| 168 | +public: |
| 169 | + FunctionDef(SymbolEntry *se, StmtNode *stmt) : se(se), stmt(stmt){}; |
| 170 | + void output(int level); |
| 171 | + void typeCheck(); |
| 172 | + void genCode(); |
| 173 | +}; |
| 174 | + |
| 175 | +class Ast |
| 176 | +{ |
| 177 | +private: |
| 178 | + Node* root; |
| 179 | +public: |
| 180 | + Ast() {root = nullptr;} |
| 181 | + void setRoot(Node*n) {root = n;} |
| 182 | + void output(); |
| 183 | + void typeCheck(); |
| 184 | + void genCode(Unit *unit); |
| 185 | +}; |
| 186 | + |
| 187 | +#endif |
0 commit comments