diff --git a/LAB6/ast.c b/LAB6/ast.c index a383835..8a634e8 100644 --- a/LAB6/ast.c +++ b/LAB6/ast.c @@ -41,15 +41,15 @@ void AST_Print_Type(enum AST_Decaf_Types t) case A_Decaf_INT: printf(" INT "); break; - case A_Decaf_BOOL: - printf(" BOOL "); - break; - case A_Decaf_STRING: - printf(" STRING "); - break; - case A_Decaf_VOID: - printf(" VOID "); - break; + case A_Decaf_BOOL: + printf(" BOOL "); + break; + case A_Decaf_STRING: + printf(" STRING "); + break; + case A_Decaf_VOID: + printf(" VOID "); + break; default: fprintf(stderr, "Unknown AST DECAF TYPE !!!\n"); } // of switch @@ -65,19 +65,32 @@ void ASTprint(int level, ASTnode *p) else { PT(level); /*indent */ switch (p->type) { + case A_PROGRAM: + printf("PROGRAM \n"); + ASTprint(level + 1, p->S1); + ASTprint(level + 1, p->S2); + break; case A_EXTERN: printf("EXTERN FUNC %s ", p->name); - printf(" ( "); - ASTprint(level, p->S1); - printf(" ) "); - AST_Print_Type(p->A_Declared_Type); - printf("\n"); + printf(" ( "); + ASTprint(level + 1, p->S1); + printf(" ) "); + AST_Print_Type(p->A_Declared_Type); + printf("\n"); break; - case A_ExternType: - printf("ExternType "); - AST_Print_Type(p->A_Declared_Type); - break; + case A_PACKAGE: + printf("PACKAGE %s", p->name); + printf("{\n"); + ASTprint(level + 1, p->S1); + ASTprint(level + 1, p->S2); + printf("}\n"); + break; + case A_ExternType: + printf("ExternType "); + AST_Print_Type(p->A_Declared_Type); + + break; case A_VARDEC: printf("Variable "); if ((p->operator) == A_Decaf_VOID) diff --git a/LAB6/ast.h b/LAB6/ast.h index 968f907..e504534 100644 --- a/LAB6/ast.h +++ b/LAB6/ast.h @@ -17,6 +17,7 @@ static int mydebug; sort of production rule we came across */ enum AST_Tree_Element_Type { + A_PROGRAM, A_EXTERN, A_PACKAGE, A_METHODDEC, @@ -43,7 +44,8 @@ enum AST_Decaf_Types { A_Decaf_INT, A_Decaf_BOOL, A_Decaf_VOID, - A_Decaf_STRING + A_Decaf_STRING, + A_Decaf_CHAR // missing }; diff --git a/LAB6/lab6.y b/LAB6/lab6.y index 8a842b5..1b546f8 100644 --- a/LAB6/lab6.y +++ b/LAB6/lab6.y @@ -77,8 +77,8 @@ from LEX and how the operators are associated */ /* %token T_STRING_LIT %token T_INT_LIT */ -%type Externs ExternDefn FullExternParmList ExternParmList -%type MethodType Type ExternType +%type Externs ExternDefn FullExternParmList ExternParmList FieldDecl FieldDecls +%type MethodType Type ExternType Constant /* %type ExternParmList %type FieldDecls %type MethodDecls @@ -126,7 +126,15 @@ that allows for one or more of the token. %% /* end specs, begin rules */ Program : Externs T_PACKAGE T_ID '{' FieldDecls MethodDecls '}' - { PROGRAM = $1; } + { + PROGRAM = ASTCreateNode(A_PROGRAM); + PROGRAM->S1 = $1; + PROGRAM->S2 = ASTCreateNode(A_PACKAGE); + PROGRAM->S2->name = $3; + PROGRAM->S2->S1 = $5; // TODO: Field Decls $5 + PROGRAM->S2->S2 = NULL; // TODO: Method Decls $6 + + } ; Externs : /* empty */ { $$ = NULL; } @@ -159,18 +167,40 @@ FullExternParmList: ExternType $$->A_Declared_Type = $1; $$->next = $3; } - ; -FieldDecls : /* empty */ - | FieldDecl FieldDecls - - ; +FieldDecls : /* empty */ {$$ = NULL;} + | FieldDecl FieldDecls + { + $$ = $1; + $$->next = $2; + } + ; -FieldDecl : T_VAR T_ID Type ';' +FieldDecl : T_VAR T_ID Type ';' + { + $$ = ASTCreateNode(A_VARDEC); + $$->name = $2; + $$->A_Declared_Type = $3; + + } | T_VAR T_ID ArrayType ';' + { + $$ = ASTCreateNode(A_VARDEC); + $$->name = $2; + // $$->A_Declared_Type = $3; + // TODO: Implement Arrays + + } | T_VAR T_ID Type T_ASSIGN Constant ';' - ; + { + $$ = ASTCreateNode(A_VARDEC); + $$->name = $2; + $$->A_Declared_Type = $3; + $$->value = $5; + + } + ; MethodDecls : /* empty */ | MethodDecl MethodDecls @@ -321,19 +351,28 @@ MethodType : T_VOID { $$ = A_Decaf_VOID; } ; BoolConstant : T_TRUE - | T_FALSE + | T_FALSE - ; + ; ArrayType : '[' T_INTCONSTANT ']' Type ; /* added String and Char as constants */ -Constant : T_INTCONSTANT - | T_STRINGCONSTANT - - | T_CHARCONSTANT - - | BoolConstant +Constant : T_INTCONSTANT { + $$ = A_Decaf_INT; + } + + | T_STRINGCONSTANT { + $$ = A_Decaf_STRING; + } + + | T_CHARCONSTANT { + $$ = A_Decaf_CHAR; + } + + | BoolConstant { + $$ = A_Decaf_BOOL; + } ; %% /* end of rules, start of program */ @@ -343,5 +382,5 @@ int main(){ yyparse(); printf("parsing done\n"); // We know PROGRAM points to our AST - ASTprint(20, PROGRAM); + ASTprint(1, PROGRAM); } diff --git a/LAB6/testinput.decaf b/LAB6/testinput.decaf index 1a7aa53..5ed943f 100644 --- a/LAB6/testinput.decaf +++ b/LAB6/testinput.decaf @@ -1,10 +1,12 @@ -extern func print_int (int, bool) int; +extern func print_int (int, bool) void; extern func read_int (int, string) int; - // extern func read_str (string) int; // extern func read_int (string) string; package foo { + var x int; + var y_me int = 10; + var A[100] bool; } // end of foo