1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
| #include "default-defs.h" #include <list> #include <ostream> #include <iostream> #include <sstream> #include <map>
#ifndef YYTOKENTYPE #include "decafast.tab.h" #endif
using namespace std;
class decafAST { public: virtual ~decafAST() {} virtual string str() { return string(""); } };
string getString(decafAST *d) { if (d != NULL) { return d->str(); } else { return string("None"); } }
template <class T> string commaList(list<T> vec) { string s(""); for (typename list<T>::iterator i = vec.begin(); i != vec.end(); i++) { s = s + (s.empty() ? string("") : string(",")) + (*i)->str(); } if (s.empty()) { s = string("None"); } return s; }
class decafAllexp : public decafAST { string Kind; string Name; public: string get_name(){return this->Name; } decafAllexp(string name,string kind) : Name(name),Kind(kind){ map<string,int> tab = { {"'\\t'",'\t'}, {"'\\r'",'\r'}, {"'\\n'",'\n'}, {"'\\a'",'\a'}, {"'\\v'",'\v'}, {"'\\b'",'\b'}, {"'\\f'",'\f'}, {"'\\\\'",'\\'}, {"'\\\''",'\''}, {"'\\\"'",'\"'}, }; if( kind == "NumberExpr"){ if(tab.count(this->Name)){ this->Name = to_string(tab[this->Name]); } else if(this->Name.size() == 1){ this->Name = to_string(Name[0]-48); } else if(this->Name.size() == 3 && ( this->Name[0]=='\'' || this->Name[0]=='\"' )){ this->Name = to_string(Name[1]); } } } string str() { return Kind + "(" + Name + ")"; } };
class decafArrexp : public decafAST { string Name; decafAllexp * Exp; public: decafArrexp(string Name, decafAllexp *Exp) : Name(Name), Exp(Exp) {} string str() { return string("ArrayLocExpr") + "(" + Name + "," + getString(Exp) + ")"; } };
class decafBinexp : public decafAST { string Option; decafAllexp * Exp1; decafAllexp * Exp2; public: decafBinexp(string op, decafAllexp *exp1, decafAllexp *exp2) : Option(op), Exp1(exp1), Exp2(exp2) {} string str() { return string("BinaryExpr") + "(" + Option + "," + getString(Exp1) + "," + getString(Exp2) + ")"; } };
class decafUnaryexp : public decafAST { string Option; decafAllexp * Exp1; public: decafUnaryexp(string op, decafAllexp *exp1) : Option(op), Exp1(exp1) {} string str() { return string("UnaryExpr") + "(" + Option + "," + getString(Exp1) + ")"; } };
class decafType : public decafAST { string Type; string Name; public: string get_type(){return this->Type; } void put_name(string Name){ this->Name = Name;} decafType(string Type){this->Type = Type;} string str() { if(Name != ""){ return "VarDef("+Name+","+Type+")"; } else{ return "VarDef("+Type+")"; } } };
class decafVar : public decafAST { string Type; string Name; string Kind; decafAllexp* Exp; decafAllexp* Arr; public: decafVar(string Name) {this->Name = Name;} string get_type(){return this->Type;} string get_name(){return this->Name;} decafAllexp* get_arr(){return this->Arr;} decafAllexp* get_exp(){return this->Exp;} void put_type(string Type){this->Type = Type;} void put_name(string Name){this->Name = Name;} void put_kind(string Kind){ if(this->Kind == "") this->Kind = Kind; } void put_arr(decafAllexp* Arr){this->Arr = Arr;} void put_exp(decafAllexp* Exp){this->Exp = Exp;} string str() { if(Exp != NULL){ return "AssignGlobalVar("+Name+","+Type+","+getString(Exp)+")"; } else if(Kind != "" && Name != ""){ return "FieldDecl("+Name+","+Type+","+Kind+")"; } else if(Name != ""){ return "VarDef("+Name+","+Type+")"; } else{ return "VarDef("+Type+")"; } } };
class decafVarList : public decafAST { list<decafVar*> List; public: list<decafVar*> get_list(){return this->List; } int size() { return List.size(); } void push_front(decafVar *e) { List.push_front(e); } void push_back(decafVar *e) { List.push_back(e); } void cat_front(decafVarList* List) { list<decafVar*> l = List->get_list(); for(auto e:l){ this->List.push_front(e); } } void put_types(string Type){ for(auto e:this->List){ e->put_type(Type); } } void put_kinds(string Kind){ for(auto e:this->List){ e->put_kind(Kind); } } string str() {return commaList<class decafVar *>(List);} };
class decafStmtList : public decafAST { list<decafAST *> stmts; public: decafStmtList() {} ~decafStmtList() { for (list<decafAST *>::iterator i = stmts.begin(); i != stmts.end(); i++) { delete *i; } } int size() { return stmts.size(); } void push_front(decafAST *e) { stmts.push_front(e); } void push_back(decafAST *e) { stmts.push_back(e); } list<decafAST *> get_para(){return stmts; } string str() { return commaList<class decafAST *>(stmts); } };
class decafAssign : public decafAST { string Var; bool key; decafAllexp* Arr; decafAllexp* Exp; decafAllexp* Exp2; public: void put_arr(decafAllexp* Arr){this->Arr = Arr;} void put_key(bool key){this->key = key;} decafAssign(string Var,decafAllexp* Exp,decafAllexp* Exp2):Var(Var),Exp(Exp),Exp2(Exp2){} string str() { if(Arr == NULL) return string("AssignVar") + "(" + Var + "," + getString(Exp) + ")"; else return string("AssignArrayLoc") + "(" + Var + "," + getString(Arr)+","+ getString(Exp) + ")"; } };
class decafAssignList : public decafAST { list<decafAssign *>List; public: int size() { return List.size(); } void push_front(decafAssign *e) { List.push_front(e); } void push_back(decafAssign *e) { List.push_back(e); } void put_keys(bool key){ for(auto ass:List){ ass->put_key(key); } } string str() { return commaList<class decafAssign *>(List); } };
class decafFunCall : public decafAST { string Name; list<decafAST *> Para; public: void put_para(list<decafAST *> Para){ this->Para = Para; } void put_name(string *Name){ this->Name = *Name; } string str() { return string("MethodCall") + "(" + Name + "," + commaList<class decafAST *>(Para) + ")"; } };
class decafOutput : public decafAST { string Data; public: decafOutput(string Data){this->Data = Data;} string str() { return Data; } };
class decafPara : public decafAST { list<decafType *> Para; public: int size() { return Para.size(); } void push_front(decafType *e) { Para.push_front(e); } void push_back(decafType *e) { Para.push_back(e); } list<decafType *> get_para(){return Para; } string str() { return commaList<class decafType *>(Para); } };
class PackageAST : public decafAST { string Name; decafVarList *FieldDeclList; decafStmtList *MethodDeclList; public: PackageAST(string name, decafVarList *fieldlist, decafStmtList *methodlist) : Name(name), FieldDeclList(fieldlist), MethodDeclList(methodlist) {} ~PackageAST() { if (FieldDeclList != NULL) { delete FieldDeclList; } if (MethodDeclList != NULL) { delete MethodDeclList; } } string str() { return string("Package") + "(" + Name + "," + getString(FieldDeclList) + "," + getString(MethodDeclList) + ")"; } };
class decafFuncDef : public decafAST { string Name; string Type; decafPara * Para; decafStmtList * Block; public: void put_para(decafPara * Para){ this->Para = Para;} void put_name(string *Name){ this->Name = *Name; } void put_type(string Type){ this->Type = Type; } void put_block(decafStmtList *Block){ this->Block = Block; } string str() { return string("Method") + "(" + Name + "," + Type + "," + getString(Para) + "," + getString(Block) + ")"; } };
class decafEXFuncDef : public decafAST { string Name; string Type; decafPara* Para; public: void put_para(decafPara* Para){ this->Para = Para;} void put_name(string *Name){ this->Name = *Name; } void put_type(string Type){ this->Type = Type; } string str() { return string("ExternFunction") + "(" + Name + "," + Type + "," + getString(Para) + ")"; } };
class decafBlock : public decafAST { string Kind; decafStmtList *FieldDeclList; decafStmtList *StateDeclList; public: decafBlock(string kind,decafStmtList *fieldlist, decafStmtList *methodlist) : Kind(kind), FieldDeclList(fieldlist), StateDeclList(methodlist) {} ~decafBlock() { if (FieldDeclList != NULL) { delete FieldDeclList; } if (StateDeclList != NULL) { delete StateDeclList; } } string str() { return Kind + "(" + getString(FieldDeclList) + "," + getString(StateDeclList) + ")"; } };
class decafIF : public decafAST { decafAllexp * Exp; decafBlock * Block; decafBlock * Block2; public: decafIF(decafAllexp * Exp,decafBlock * Block,decafBlock * Block2): Exp(Exp),Block(Block),Block2(Block2){} string str() { return string("IfStmt") + "(" + getString(Exp) +"," + getString(Block) + "," + getString(Block2) + ")"; } };
class decafWhile : public decafAST { decafAllexp * Exp; decafBlock * Block; public: decafWhile(decafAllexp * Exp,decafBlock * Block): Exp(Exp),Block(Block){} string str() { return string("WhileStmt") + "(" + getString(Exp) +"," + getString(Block) + ")"; } };
class decafFor : public decafAST { decafAllexp * Exp; decafBlock * Block; decafAssignList *List; decafAssignList *List2; public: decafFor(decafAllexp * Exp,decafBlock * Block,decafAssignList *List,decafAssignList *List2): Exp(Exp),Block(Block),List(List),List2(List2){} string str() { return string("ForStmt") + "(" + getString(List)+","+getString(Exp) +","+getString(List2)+","+getString(Block) + ")"; } };
class decafReturn : public decafAST { decafAllexp * Exp; public: decafReturn(decafAllexp * Exp){ this->Exp = Exp; } string str() { return string("ReturnStmt") + "(" + getString(Exp) + ")"; } };
class ProgramAST : public decafAST { decafStmtList *ExternList; PackageAST *PackageDef; public: ProgramAST(decafStmtList *externs, PackageAST *c) : ExternList(externs), PackageDef(c) {} ~ProgramAST() { if (ExternList != NULL) { delete ExternList; } if (PackageDef != NULL) { delete PackageDef; } } string str() { return string("Program") + "(" + getString(ExternList) + "," + getString(PackageDef) + ")"; } };
|