00001 /* -*- Mode: objc; c-basic-offset: 2; tab-width: 2 indent-tabs-mode: nil -*- */ 00002 /* vim: set filetype=objc ts=2 sw=2 expandtab: */ 00003 00004 /* ast.h 00005 * Copyright (C) 2007 Neil Dantam 00006 * 2007 Jeff Seibert 00007 * 00008 * This program is free software: you can redistribute it and/or modify it 00009 * under the terms of the GNU General Public License, verson 3 ONLY, as 00010 * published by the Free Software Foundation. 00011 * 00012 * This program is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00015 * more details. 00016 * 00017 * You should have received a copy of the GNU General Public License along with 00018 * this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00025 #ifndef _AST_H_ 00026 #define _AST_H_ 00027 00028 #include "token.h" 00029 #include "cons.h" 00030 #include "tipe.h" 00031 00034 @interface ASTNode: Object { 00035 Token *token; 00036 Tipe *tipe; 00037 BOOL checked; 00038 } 00039 /* Returns read only, borrowd */ 00040 -(char*) op; 00041 -(BOOL) isOp: (char*) s; 00042 -(Token*) token; 00043 -(void) token: (Token*) t; 00044 -(void) setToken: (Token*) t; 00045 00047 -(id) init: (id) t; 00048 -(void) setTipe: (Tipe*) t; 00049 -(Tipe*) tipe; 00050 -(void) tipe: (Tipe*) t; 00051 -(char*) op; 00052 -(BOOL) isOp: (char*) s; 00053 -(int) line; 00054 @end 00055 00056 00061 @interface Asm : ASTNode { 00062 Token *str; 00063 } 00067 -(id) init: (id) t text: (id) s; 00068 @end 00069 00070 00073 @interface Stmt: ASTNode { 00074 } 00075 @end 00076 00077 00078 @interface Spec: ASTNode{ 00079 } 00080 @end 00081 00084 @interface StoreSpec: Spec 00085 @end 00086 00087 00090 @interface Declarator: ASTNode { 00091 Cons *pointer; 00092 Asm *a; 00093 } 00095 -(void) setPointer: (Cons*) p; 00097 -(void) setAsm: (Asm*) p; 00098 @end 00099 00100 @interface AbstractDeclarator: Declarator 00101 @end 00102 00105 @interface Type: ASTNode { 00106 Cons* specs; 00107 00108 Declarator *abstract; 00109 } 00110 00115 -(Type*)init: (id) s : (id) a; 00116 @end 00117 00118 00123 @interface Ident: ASTNode { 00124 } 00128 @end 00129 00132 @interface Expr: ASTNode { 00133 } 00134 @end 00135 00136 00139 @interface OpExpr: Expr { 00140 } 00141 @end 00142 00147 @interface Literal: Expr { 00148 } 00150 -(char*) value; 00151 @end 00152 00155 @interface IntLiteral: Literal 00156 @end 00157 00160 @interface StringLiteral: Literal 00161 @end 00162 00163 00165 @interface SimpleDeclarator:Declarator { 00167 Ident *ident; 00168 } 00169 @end 00170 00172 @interface ArrayDeclarator: Declarator { 00173 Declarator *dec; 00174 00175 Expr *size; 00176 } 00182 -(id) init: (id) t decl: (id) d size: (id) i; 00183 @end 00184 00186 @interface FuncDeclarator: Declarator { 00187 Declarator *dec; 00188 Cons *formals; 00189 } 00195 -(id) init: (id) t decl: (id) d formals: (id) f; 00196 @end 00197 00199 @interface Initializer : Expr { 00200 Expr *exp; 00201 } 00204 -(id) init: (id) e; 00205 @end 00206 00208 @interface StructFieldInit : Initializer //{ 00209 @end 00210 00214 @interface NamedStructFieldInit : StructFieldInit { 00215 Ident *field; 00216 } 00222 -(id) init: (id) t ident: (id) i exp: (id) e; 00223 @end 00224 00226 @interface StructInitializer : Expr { 00227 Cons *inits; 00228 } 00233 -(id) init: (id) t inits: (id) i; 00234 @end 00235 00237 @interface DeclInit: ASTNode { 00238 Declarator *declr; 00239 Initializer *init; 00240 } 00241 -(DeclInit*) init: (id) d is: (id) e; 00242 @end 00243 00244 00245 00248 @interface VarDecList: Cons 00249 @end 00250 00253 @interface ExprStmt: Stmt { 00254 Expr *exp; 00255 } 00259 -(id) init: (id) t exp: (id) e; 00260 //-(id) init: (id) e; 00261 @end 00262 00265 @interface CastExpr: OpExpr { 00266 Type *type; 00267 Expr *exp; 00268 } 00269 -(CastExpr*) init: (Token*) tk type: (Type*) t exp: (Expr*) e; 00270 @end 00271 00274 @interface UnExpr: OpExpr { 00275 id exp; 00276 } 00281 -(id) init: (id) t exp: (id) e; 00282 @end 00283 00286 @interface PrefixExpr: UnExpr { 00287 } 00288 @end 00289 00292 @interface PostfixExpr: UnExpr { 00293 } 00294 @end 00295 00298 @interface SizeofExpr: UnExpr { 00299 } 00300 @end 00301 00304 @interface BinExpr: OpExpr { 00305 Expr *lhs; 00306 Expr *rhs; 00307 } 00308 -(BinExpr*) init: (Token*) tok lhs: (Expr*) e rhs: (Expr*) e; 00309 @end 00310 00313 @interface SubscriptExpr: BinExpr 00314 @end 00315 00318 @interface CondExpr: OpExpr { 00319 Expr *test; 00320 Expr *texp; 00321 Expr *fexp; 00322 } 00323 -(CondExpr*) init: (Token*) tk 00324 test: (Expr*) tst 00325 texp: (Expr*) t 00326 fexp: (Expr*) f; 00327 @end 00328 00331 @interface NameExpr: Expr { 00332 Ident *exp; 00333 00337 int parent_level; 00338 } 00339 -(char*) key; 00340 //-(NameExpr*) init: (Ident*) i; 00341 @end 00342 00343 00346 @interface CallExpr: Expr{ 00347 Expr *exp; 00348 Cons *args; 00349 } 00355 -(id) init: (id) t exp: (id) e args: (id)a; 00356 @end 00357 00360 @interface LabeledStmt : Stmt { 00361 Ident *ident; 00362 IntLiteral *constant; 00363 Stmt *stmt; 00364 } 00365 -(id) init: (id) i : (id) t : (id) c : (id) s; 00366 @end 00367 00370 @interface JumpStmt : Stmt { 00371 Ident *ident; 00372 Expr *expr; 00373 } 00374 -(id) init: (id) t : (id) i : (id) e; 00375 @end 00376 00379 @interface Block: Stmt { 00380 //Cons *decs; ///< Declarations at beginning (maybe unnesscary) 00381 Cons *stmts; 00382 } 00387 -(id) init: (id) t stmts: (id) s; 00388 @end 00389 00392 @interface SelectionStmt: Stmt { 00393 Expr *test; 00394 Stmt *thenStmt; 00395 Stmt *elseStmt; 00396 } 00397 00404 -(id) init: (id) t test: (id) e 00405 if_stmt: (id) ts else_stmt: (id) es; 00406 @end 00407 00410 @interface While: Stmt { 00411 Expr *test; 00412 Stmt *stmt; 00413 } 00419 -(id) init: (id) t test:(id) e stmt: (id) s; 00420 @end 00421 00424 @interface DoWhile: While { 00425 } 00426 @end 00427 00430 @interface For: Stmt { 00431 ExprStmt *dec; 00432 ExprStmt *test; 00433 Expr *step; 00434 Stmt *stmt; 00435 } 00443 -(id) init: (id) t dec: (id) d test: (id) ts step: (id) p stmt: (id) s; 00444 @end 00445 00447 @interface Enum: ASTNode { 00448 Ident* ident; 00449 Cons* expr; 00450 } 00451 -(Enum*) init: (id) t ident: (id) i list: (id) e; 00452 @end 00453 00455 @interface Enumerator : ASTNode { 00456 Ident* ident; 00457 IntLiteral* constant; 00458 } 00464 -(id) init: (id) t ident: (id) i value: (id) c; 00465 @end 00466 00467 00469 @interface StructOrUnion: ASTNode { 00470 Ident* ident; 00471 Cons* stmts; 00472 } 00473 -(StructOrUnion*) init: (id) t : (id) i : (id) d; 00474 @end 00475 00477 @interface StructDeclarator: ASTNode { 00478 Declarator* dec; 00479 IntLiteral* constant; 00480 } 00481 -(StructDeclarator*) init: (id) d : (id) c; 00482 @end 00483 00484 00486 @interface StructDeclaration: Stmt { 00487 Cons* type_specs; 00488 Cons* struct_decs; 00489 } 00490 -(StructDeclaration*) init: (id) t : (id) s; 00491 @end 00492 00493 00494 00495 00498 @interface TypeSpec: Spec { 00499 //Enum* enumerator; 00500 //StructOrUnion* su; 00501 } 00502 -(TypeSpec*) init: (id) t; 00503 //-(TypeSpec*) initEnum: (Enum*) e; 00504 //-(TypeSpec*) initStruct: (StructOrUnion*) s; 00505 @end 00506 00509 @interface PrimitiveTypeSpec : TypeSpec 00510 @end 00511 00514 @interface NameTypeSpec : TypeSpec 00515 @end 00516 00519 @interface PointerSpec : TypeSpec 00520 @end 00521 00524 @interface StructTypeSpec: TypeSpec { 00525 StructOrUnion *s; 00526 } 00527 -(id) init: (id) i; 00528 @end 00529 00532 @interface EnumTypeSpec: TypeSpec { 00533 Enum *e; 00534 } 00535 -(id) init: (id) i; 00536 @end 00537 00540 @interface DeclarationSpec: ASTNode{ 00541 Cons *specs; 00542 } 00544 -(void) addSpec: (Spec*) s; 00546 -(void) pushSpec: (Spec*) s; 00547 @end 00548 00551 @interface Declaration: Stmt{ 00552 DeclarationSpec *dspec; 00553 Cons *decl_inits; 00554 } 00555 -(void) addInit: (DeclInit*) d; 00556 -(void) addSpec: (DeclarationSpec*) s; 00557 @end 00558 00559 @interface TypeDefDeclaration : Declaration 00560 @end 00561 00562 @interface Formal: ASTNode 00563 @end 00564 00567 @interface NormFormal: Formal { 00568 DeclarationSpec *spec; 00569 Declarator* declr; 00570 } 00571 -(id) init: (id) s: (id) d; 00572 @end 00573 00574 00577 @interface VaFormal: Formal 00578 @end 00579 00582 @interface FuncDefinition : Stmt { 00583 DeclarationSpec *decl; 00584 Declarator *declr; 00585 Cons *decs; 00586 Block *stmts; 00587 } 00588 -(id) init: (id) l : (id) r : (id) c : (id) s; 00589 @end 00590 00593 @interface File : ASTNode { 00594 Cons *defs; 00595 } 00596 -(id) init: (id) d; 00597 @end 00598 00601 @interface InstanceVars : Stmt { 00602 Cons *vars; //Could be tokens, declarations, or other InstanceVars 00603 } 00608 -(id) init: (id) t vars: (id) d; 00609 @end 00610 00612 @interface KeyDeclarator : ASTNode { 00613 Ident *selector; 00614 Type *type1; 00615 Type *type2; 00616 Ident *ident; 00617 } 00618 -(id) init: (id) s type1: (id) t1 type2: (id) t2 ident: (id) i; 00619 00621 -(char*) key; 00622 -(Type*) type; 00623 -(Ident*) arg; 00624 @end 00625 00628 @interface Klass : Stmt { 00629 Ident *class; 00630 Ident *super_class; 00631 Cons *methods; 00632 } 00633 -(id) addMethods: (id) d; 00634 00637 -(char*) classname; 00638 @end 00639 00642 @interface Method : Stmt { 00643 Type *type; 00644 Cons *args; 00645 Klass *klass; 00646 } 00652 -(id) init: (id) t rtype: (id) r args: (id) a; 00653 -(char*) msg_name; 00654 @end 00655 00658 @interface MethodDec : Method 00659 @end 00660 00663 @interface ClassMethodDec : MethodDec 00664 @end 00665 00668 @interface InstanceMethodDec : MethodDec 00669 @end 00670 00673 @interface MethodDefinition : Method { 00674 Cons *decl; 00675 Block *block; 00676 } 00684 -(id) init: (id) t rtype: (id) r args: (id) a 00685 decl: (id) d block: (id) b; 00686 @end 00687 00690 @interface ClassMethodDefinition : MethodDefinition 00691 @end 00692 00695 @interface InstanceMethodDefinition : MethodDefinition 00696 @end 00697 00700 @interface Interface : Klass { 00701 InstanceVars *instance_vars; 00702 } 00703 -(id) addInstVars: (id) i; 00704 @end 00705 00708 @interface Implementation : Klass 00709 @end 00710 00711 00712 00713 @interface SelfExpr : Expr 00714 @end 00715 00718 @interface MessageExpr : Expr { 00719 Cons *args; 00720 } 00725 -(id) init: (id) t args: (id) a ; 00726 @end 00727 00728 00729 00732 @interface InstanceMessageExpr : MessageExpr { 00733 Expr *receiver; 00734 } 00740 -(id) init: (id) t recv: (id) r args: (id) a; 00741 @end 00742 00748 @interface ClassMessageExpr : MessageExpr { 00749 Ident *receiver; 00750 } 00756 -(id) init: (id) t recv: (id) r args: (id) a; 00757 @end 00758 00761 @interface KeywordArgument : ASTNode { 00762 Ident *selector; 00763 Expr *expr; 00764 } 00770 -(id) init: (id) t sel: (id) s exp: (id) e; 00772 -(char*) key; 00774 -(Expr*) val; 00775 @end 00776 00779 @interface AsmStmt : Stmt { 00780 Asm *a; 00781 } 00786 -(id) init: (id) t a: (id) aa; 00787 @end 00788 00791 @interface VisSpec : ASTNode 00792 @end 00793 00794 #endif 00795 00796