mirror of
https://github.com/RustyRaptor/compilers.git
synced 2026-12-03 15:15:17 -07:00
video 4
This commit is contained in:
1 parent
ef7bd73862
commit
8e1cccecf7
4 files changed
+96
-40
No files matched your search
+31
-18
@@ -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)
|
||||
|
||||
+3
-1
@@ -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
|
||||
};
|
||||
|
||||
|
||||
+58
-19
@@ -77,8 +77,8 @@ from LEX and how the operators are associated */
|
||||
/* %token <string> T_STRING_LIT
|
||||
%token <value> T_INT_LIT */
|
||||
|
||||
%type <astnode> Externs ExternDefn FullExternParmList ExternParmList
|
||||
%type <asttype> MethodType Type ExternType
|
||||
%type <astnode> Externs ExternDefn FullExternParmList ExternParmList FieldDecl FieldDecls
|
||||
%type <asttype> MethodType Type ExternType Constant
|
||||
/* %type <astnode> ExternParmList
|
||||
%type <astnode> FieldDecls
|
||||
%type <astnode> 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);
|
||||
}
|
||||
@@ -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
|
||||
Reference in new issue
Block a user