00001 #ifndef AST_H
00002 #define AST_H
00003 #include <string>
00004 #include <vector>
00005 #include <map>
00006 #include "attributeproxy.h"
00007
00008
00009
00010 using namespace std;
00011 namespace AST {
00017 class Node {
00018 protected:
00019
00024 enum {
00025 NODE_BRANCH,
00026 NODE_LIST,
00027 NODE_LEAF,
00028 NODE_ID,
00029 NODE_CONSTANT,
00030 } nodeType;
00031 public:
00033 Node(string new_name):
00034 name(new_name),
00035 nodeType(NODE_BRANCH)
00036 {
00037 }
00038 public:
00039 virtual void to3ACFile(FILE* f) = 0;
00040 void toASTFile(FILE* f);
00041 void set_attribute(string attributeName, Node* child);
00042 void set_attribute(string attributeName, string value);
00043 void set_attribute(string attributeName, vector<Node*> vec_nodes);
00044 AttributeProxy operator[](string attributeName);
00045 const char* get_name(){ return this->name.c_str(); }
00046
00047 protected:
00048 AttributeProxy addAttribute(const char* attributeName);
00049 protected:
00050
00051 map<string, Node*> attributes;
00052 string name;
00053 private:
00054 vector<string> orderedAttributes;
00055 };
00056
00057
00058 Node* fromASTFile(FILE* f);
00059
00063 class List : public Node {
00064 typedef Node super;
00065 public:
00066 List():
00067 super("List")
00068 {
00069 this->nodeType = NODE_LIST;
00070 }
00071 List(vector<Node*> vec):
00072 super("List")
00073 {
00074 this->nodeType = NODE_LIST;
00075 for( int i = 0; i < vec.size(); i ++){
00076 this->addChild(vec[i]);
00077 }
00078 }
00079 void addChild(Node* child){
00080 list.push_back(child);
00081 }
00082 virtual void to3ACFile(FILE* f){}
00083 vector<Node*> list;
00084 protected:
00085 };
00086
00090 class Leaf : public Node {
00091 typedef Node super;
00092 public:
00093 Leaf(string new_value):
00094 super("Leaf"),
00095 value(new_value)
00096 {
00097 this->nodeType = NODE_LEAF;
00098 }
00099 string value;
00100 virtual void to3ACFile(FILE* f){}
00101 };
00102
00104 class Program : public Node {
00105 typedef Node super;
00106 public:
00107 Program():
00108 super("Program"),
00109 statements(this->addAttribute("statements"))
00110 {
00111 }
00112 virtual void to3ACFile(FILE* f){}
00113 AttributeProxy statements;
00114 };
00115
00117 class Def_Function : public Node {
00118 typedef Node super;
00119 public:
00120 Def_Function():
00121 super("Def_Function"),
00122 handle (this->addAttribute("handle")),
00123 arguments (this->addAttribute("arguments")),
00124 statements(this->addAttribute("statements"))
00125 {
00126 }
00127 virtual void to3ACFile(FILE* f){}
00128 AttributeProxy handle;
00129 AttributeProxy arguments;
00130 AttributeProxy statements;
00131 };
00132
00134 class Def_Struct : public Node {
00135 typedef Node super;
00136 public:
00137 Def_Struct():
00138 super("Def_Struct"),
00139 list_variables (this->addAttribute("handle"))
00140 {
00141 }
00142 virtual void to3ACFile(FILE* f){}
00143 AttributeProxy list_variables;
00144 };
00145
00147 class If : public Node {
00148 typedef Node super;
00149 public:
00150 If():
00151 super("If"),
00152 condition(this->addAttribute("condition")),
00153 statements_then(this->addAttribute("statements_then")),
00154 statements_else(this->addAttribute("statements_else"))
00155 {
00156 }
00157 virtual void to3ACFile(FILE* f){}
00158 AttributeProxy condition;
00159 AttributeProxy statements_then;
00160 AttributeProxy statements_else;
00161 };
00162
00164 class Jump : public Node {
00165 typedef Node super;
00166 public:
00167 Jump():
00168 super("Jump"),
00169 type(this->addAttribute("type"))
00170 {
00171 }
00172 virtual void to3ACFile(FILE* f){}
00174 AttributeProxy type;
00175 };
00176
00178 class Loop : public Node {
00179 typedef Node super;
00180 public:
00181 Loop():
00182 super("Conditional"),
00183 type (this->addAttribute("type")),
00184 declaration(this->addAttribute("declaration")),
00185 condition (this->addAttribute("condition")),
00186 increment (this->addAttribute("increment")),
00187 statements (this->addAttribute("statements"))
00188 {
00189 }
00190 virtual void to3ACFile(FILE* f){}
00192 AttributeProxy type;
00194 AttributeProxy declaration;
00196 AttributeProxy condition;
00198 AttributeProxy increment;
00200 AttributeProxy statements;
00201 };
00202
00204 class Return : public Node {
00205 typedef Node super;
00206 public:
00207 Return():
00208 super("Return"),
00209 value(this->addAttribute("value"))
00210 {
00211 }
00212 virtual void to3ACFile(FILE* f){}
00214 AttributeProxy value;
00215 };
00216
00218 class Op_Unary : public Node {
00219 typedef Node super;
00220 public:
00221 Op_Unary():
00222 super("Op_Unary"),
00223 id(this->addAttribute("id")),
00224 op(this->addAttribute("op"))
00225 {
00226 }
00227 virtual void to3ACFile(FILE* f){}
00228 AttributeProxy id;
00229 AttributeProxy op;
00230 };
00231
00233 class Op_Binary : public Node {
00234 typedef Node super;
00235 public:
00236 Op_Binary():
00237 super("Op_Binary"),
00238 lhs(this->addAttribute("lhs")),
00239 op (this->addAttribute("op")),
00240 rhs(this->addAttribute("rhs"))
00241 {
00242 }
00243 virtual void to3ACFile(FILE* f){}
00244 AttributeProxy lhs;
00245 AttributeProxy op;
00246 AttributeProxy rhs;
00247 };
00248
00250 class Op_Ternary : public Node {
00251 typedef Node super;
00252 public:
00253 Op_Ternary():
00254 super("Op_Ternary"),
00255 condition (this->addAttribute("conditon")),
00256 trueValue (this->addAttribute("trueValue")),
00257 falseValue(this->addAttribute("falseValue"))
00258 {
00259 }
00260 virtual void to3ACFile(FILE* f){}
00261 AttributeProxy condition;
00262 AttributeProxy trueValue;
00263 AttributeProxy falseValue;
00264 };
00265
00267 class Assignment : public Node {
00268 typedef Node super;
00269 public:
00270 Assignment():
00271 super("Assignment"),
00272 lhs(this->addAttribute("lhs")),
00273 op (this->addAttribute("op")),
00274 rhs(this->addAttribute("rhs"))
00275 {
00276 }
00277 virtual void to3ACFile(FILE* f){}
00278 AttributeProxy lhs;
00279 AttributeProxy op;
00280 AttributeProxy rhs;
00281 };
00282
00284 class Constant : public Node {
00285 typedef Node super;
00286 public:
00287 Constant():
00288 super("Constant"),
00289 type (this->addAttribute("type")),
00290 value(this->addAttribute("value"))
00291 {
00292 this->nodeType = NODE_CONSTANT;
00293 }
00294 virtual void to3ACFile(FILE* f){}
00295 AttributeProxy type;
00296 AttributeProxy value;
00297 };
00298
00300 class FunctionCall : public Node {
00301 typedef Node super;
00302 public:
00303 FunctionCall():
00304 super("FunctionCall"),
00305 handle(this->addAttribute("handle")),
00306 arguments(this->addAttribute("arguments"))
00307 {
00308 }
00309 virtual void to3ACFile(FILE* f){}
00310 AttributeProxy handle;
00311 AttributeProxy arguments;
00312 };
00313
00315 class Identifier : public Node {
00316 typedef Node super;
00317 public:
00318 Identifier():
00319 super("Identifier"),
00320 handle(this->addAttribute("handle"))
00321 {
00322 this->nodeType = NODE_ID;
00323 }
00324 virtual void to3ACFile(FILE* f){}
00325 AttributeProxy handle;
00326 };
00327
00328
00330 class Op_Bracket : public Node {
00331 typedef Node super;
00332 public:
00333 Op_Bracket():
00334 super("Op_Bracket"),
00335 handle (this->addAttribute("handle")),
00336 list_index(this->addAttribute("list_index"))
00337 {
00338 }
00339 virtual void to3ACFile(FILE* f){}
00340 AttributeProxy handle;
00341 AttributeProxy list_index;
00342 };
00343
00345 class Op_Dot : public Node {
00346 typedef Node super;
00347 public:
00348 Op_Dot():
00349 super("Op_Dot"),
00350 handle (this->addAttribute("handle")),
00351 variable(this->addAttribute("variable"))
00352 {
00353 }
00354 virtual void to3ACFile(FILE* f){}
00355 AttributeProxy handle;
00356 AttributeProxy variable;
00357 };
00358
00360 class Def_Var : public Node {
00361 typedef Node super;
00362 public:
00363 Def_Var():
00364 super("Def_Var"),
00365 type (this->addAttribute("handle")),
00366 handles(this->addAttribute("variable"))
00367 {
00368 }
00369 virtual void to3ACFile(FILE* f){}
00370 AttributeProxy type;
00371 AttributeProxy handles;
00372 };
00373
00375 class Def_Array : public Node {
00376 typedef Node super;
00377 public:
00378 Def_Array():
00379 super("Def_Array"),
00380 type (this->addAttribute("handle")),
00381 handle (this->addAttribute("variable")),
00382 list_dimension(this->addAttribute("list_dimension"))
00383 {
00384 }
00385 virtual void to3ACFile(FILE* f){}
00386 AttributeProxy type;
00387 AttributeProxy handle;
00388 AttributeProxy list_dimension;
00389 };
00390
00391 };
00392 #endif