From b7845f60ab5847a6c660d2116b4c182c53058664 Mon Sep 17 00:00:00 2001 From: Max Base Date: Mon, 3 Aug 2020 19:42:24 +0430 Subject: [PATCH 1/5] Update ast.h --- ast.h | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/ast.h b/ast.h index 2db916b..0f135c3 100755 --- a/ast.h +++ b/ast.h @@ -2,17 +2,28 @@ #define AST_H enum { - BE_LT, BE_LTE, - BE_GT, BE_GTE, - BE_EQ, BE_NEQ, - BE_ADD, BE_SUB, - BE_MUL, BE_DIV, - K_NUM, K_CALL, - E_ASSIGN, VAR_NAME, - ARRAY_ACCESS, RETURN, - WHILE, IF_THEN, - COMPOUND, PARAM, - FUNCTION, VAR_DECL + TOKEN_BE_LT, + TOKEN_BE_LTE, + TOKEN_BE_GT, + TOKEN_BE_GTE, + TOKEN_BE_EQ, + TOKEN_BE_NEQ, + TOKEN_BE_ADD, + TOKEN_BE_SUB, + TOKEN_BE_MUL, + TOKEN_BE_DIV, + TOKEN_K_NUM, + TOKEN_K_CALL, + TOKEN_E_ASSIGN, + TOKEN_VAR_NAME, + TOKEN_ARRAY_ACCESS, + TOKEN_RETURN, + TOKEN_WHILE, + TOKEN_IF_THEN, + TOKEN_COMPOUND, + TOKEN_PARAM, + TOKEN_FUNCTION, + TOKEN_VAR_DECL } kind; struct list; // forward declaration From f6a7bdb9b081d454fefb934c5cb7134f6d230f2d Mon Sep 17 00:00:00 2001 From: Max Base Date: Mon, 3 Aug 2020 19:42:31 +0430 Subject: [PATCH 2/5] Update pretty_print.c --- pretty_print.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/pretty_print.c b/pretty_print.c index 50b8452..2f15d06 100755 --- a/pretty_print.c +++ b/pretty_print.c @@ -31,73 +31,73 @@ static void pprint_(AST* ast, int indent) { switch(ast->kind_) { - case K_NUM: indentation(indent); + case TOKEN_K_NUM: indentation(indent); printf("%d\n",ast->d.val_); break; - case BE_LT: print_binexpr(indent, "<", ast); + case TOKEN_BE_LT: print_binexpr(indent, "<", ast); break; - case BE_LTE: print_binexpr(indent, "<=", ast); + case TOKEN_BE_LTE: print_binexpr(indent, "<=", ast); break; - case BE_GT: print_binexpr(indent, ">", ast); + case TOKEN_BE_GT: print_binexpr(indent, ">", ast); break; - case BE_GTE: print_binexpr(indent, ">=", ast); + case TOKEN_BE_GTE: print_binexpr(indent, ">=", ast); break; - case BE_EQ: print_binexpr(indent, "==", ast); + case TOKEN_BE_EQ: print_binexpr(indent, "==", ast); break; - case BE_NEQ: print_binexpr(indent, "!=", ast); + case TOKEN_BE_NEQ: print_binexpr(indent, "!=", ast); break; - case BE_ADD: print_binexpr(indent, "+", ast); + case TOKEN_BE_ADD: print_binexpr(indent, "+", ast); break; - case BE_SUB: print_binexpr(indent, "-", ast); + case TOKEN_BE_SUB: print_binexpr(indent, "-", ast); break; - case BE_MUL: print_binexpr(indent, "*", ast); + case TOKEN_BE_MUL: print_binexpr(indent, "*", ast); break; - case BE_DIV: print_binexpr(indent, "/", ast); + case TOKEN_BE_DIV: print_binexpr(indent, "/", ast); break; - case K_CALL: indentation(indent); + case TOKEN_K_CALL: indentation(indent); printf("callee: %s\n", ast->d.call.name_); for (struct node* tmp = ast->d.call.args_->head; tmp != NULL; tmp = tmp->next) pprint(tmp->data, indent+2); break; - case E_ASSIGN: indentation(indent); + case TOKEN_E_ASSIGN: indentation(indent); printf("=\n"); pprint(ast->d.assign.to_, indent+2); pprint(ast->d.assign.expr_, indent+2); break; - case ARRAY_ACCESS: indentation(indent); + case TOKEN_ARRAY_ACCESS: indentation(indent); printf("Array name: %s\n",ast->d.access.name_); pprint(ast->d.access.expr_, indent+2); break; - case VAR_NAME: indentation(indent); + case TOKEN_VAR_NAME: indentation(indent); printf("variable name: %s\n",ast->d.variable.name_); break; - case RETURN: indentation(indent); + case TOKEN_RETURN: indentation(indent); printf("Return statement\n"); pprint(ast->d.ret.expr_, indent+2); break; - case WHILE: indentation(indent); + case TOKEN_WHILE: indentation(indent); printf("While statement\n"); pprint(ast->d.while_loop.expr_, indent+2); pprint(ast->d.while_loop.stmt_, indent+2); break; - case IF_THEN: indentation(indent); + case TOKEN_IF_THEN: indentation(indent); printf("If then statement\n"); pprint(ast->d.if_then.expr_, indent+2); pprint(ast->d.if_then.if_stmt_, indent+2); @@ -109,7 +109,7 @@ static void pprint_(AST* ast, int indent) } break; - case COMPOUND: indentation(indent); + case TOKEN_COMPOUND: indentation(indent); printf("Declarations\n"); for (struct node* tmp = ast->d.compound.decls_->head; tmp != NULL; tmp = tmp->next) pprint(tmp->data, indent+2); @@ -120,14 +120,14 @@ static void pprint_(AST* ast, int indent) pprint(tmp->data, indent+2); break; - case PARAM: indentation(indent); + case TOKEN_PARAM: indentation(indent); printf("%s %s%s\n", ast->d.param.type_spec_ == TYPE_INT ? "int" : "void", ast->d.param.name_, ast->d.param.ref_ == 0 ? "" : " []"); break; - case FUNCTION: indentation(indent); + case TOKEN_FUNCTION: indentation(indent); printf("%s %s\n", ast->d.function.ret_type_ == TYPE_INT ? "int" : "void", ast->d.function.name_); indentation(indent+2); @@ -139,7 +139,7 @@ static void pprint_(AST* ast, int indent) pprint(ast->d.function.body_,indent+2); break; - case VAR_DECL: indentation(indent); + case TOKEN_VAR_DECL: indentation(indent); printf("%s %s %d %s\n", ast->d.var_decl.type_spec_ == TYPE_INT ? "int" : "void", ast->d.var_decl.name_, From 78ce6b34988f7293f90b4a13d8b5a152659aa0f4 Mon Sep 17 00:00:00 2001 From: Max Base Date: Mon, 3 Aug 2020 19:42:43 +0430 Subject: [PATCH 3/5] Update ast_cons.c --- ast_cons.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/ast_cons.c b/ast_cons.c index 07fe1cc..907d855 100755 --- a/ast_cons.c +++ b/ast_cons.c @@ -23,57 +23,57 @@ static AST* make_binexpr(int lineno, int type, AST* lhs, AST* rhs) AST* lt(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_LT, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_LT, lhs, rhs); } AST* lte(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_LTE, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_LTE, lhs, rhs); } AST* gt(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_GT, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_GT, lhs, rhs); } AST* gte(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_GTE, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_GTE, lhs, rhs); } AST* eq(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_EQ, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_EQ, lhs, rhs); } AST* neq(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_NEQ, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_NEQ, lhs, rhs); } AST* add(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_ADD, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_ADD, lhs, rhs); } AST* sub(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_SUB, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_SUB, lhs, rhs); } AST* mul(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_MUL, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_MUL, lhs, rhs); } AST* divide(AST* lhs, AST* rhs) { - return make_binexpr(yylineno, BE_DIV, lhs, rhs); + return make_binexpr(yylineno, TOKEN_BE_DIV, lhs, rhs); } AST* call(char* name, list* args) { - AST* ast = make_ast(yylineno, K_CALL); + AST* ast = make_ast(yylineno, TOKEN_K_CALL); //printf(name); ast->d.call.name_ = name; ast->d.call.args_ = args; @@ -82,7 +82,7 @@ AST* call(char* name, list* args) AST* assign(AST* to, AST* expr) { - AST* temp = make_ast(yylineno, E_ASSIGN); + AST* temp = make_ast(yylineno, TOKEN_E_ASSIGN); temp->d.assign.to_ = to; temp->d.assign.expr_ = expr; return temp; @@ -90,21 +90,21 @@ AST* assign(AST* to, AST* expr) AST* variable(char* name) { - AST* temp = make_ast(yylineno, VAR_NAME); + AST* temp = make_ast(yylineno, TOKEN_VAR_NAME); temp->d.variable.name_ = name; return temp; } AST* num(int val) { - AST* ast = make_ast(yylineno, K_NUM); + AST* ast = make_ast(yylineno, TOKEN_K_NUM); ast->d.val_ = val; return ast; } AST* access(char* name, AST* expr) { - AST* temp = make_ast(yylineno, ARRAY_ACCESS); + AST* temp = make_ast(yylineno, TOKEN_ARRAY_ACCESS); temp->d.access.name_ = name; temp->d.access.expr_ = expr; return temp; @@ -112,14 +112,14 @@ AST* access(char* name, AST* expr) AST* ret(AST *expr) { - AST* temp = make_ast(yylineno, RETURN); + AST* temp = make_ast(yylineno, TOKEN_RETURN); temp->d.ret.expr_ = expr; return temp; } AST* while_loop(AST* expr, AST* stmt) { - AST* temp = make_ast(yylineno, WHILE); + AST* temp = make_ast(yylineno, TOKEN_WHILE); temp->d.while_loop.expr_ = expr; temp->d.while_loop.stmt_ = stmt; return temp; @@ -127,7 +127,7 @@ AST* while_loop(AST* expr, AST* stmt) AST* if_then(AST* expr, AST* if_stmt, AST* else_stmt) { - AST* temp = make_ast(yylineno, IF_THEN); + AST* temp = make_ast(yylineno, TOKEN_IF_THEN); temp->d.if_then.expr_ = expr; temp->d.if_then.if_stmt_ = if_stmt; temp->d.if_then.else_stmt_ = else_stmt; @@ -136,7 +136,7 @@ AST* if_then(AST* expr, AST* if_stmt, AST* else_stmt) AST* compound_stmt(list* decls, list* stmts) { - AST* temp = make_ast(yylineno, COMPOUND); + AST* temp = make_ast(yylineno, TOKEN_COMPOUND); temp->d.compound.decls_ = decls; temp->d.compound.stmts_ = stmts; return temp; @@ -144,7 +144,7 @@ AST* compound_stmt(list* decls, list* stmts) AST* param(int type_spec, char* name, int ref) { - AST* temp = make_ast(yylineno, PARAM); + AST* temp = make_ast(yylineno, TOKEN_PARAM); temp->d.param.type_spec_ = type_spec; temp->d.param.name_ = name; temp->d.param.ref_ = ref; @@ -153,7 +153,7 @@ AST* param(int type_spec, char* name, int ref) AST* function(int ret_type, char* name, list* params, AST* body) { - AST* temp = make_ast(yylineno, FUNCTION); + AST* temp = make_ast(yylineno, TOKEN_FUNCTION); temp->d.function.ret_type_ = ret_type; temp->d.function.name_ = name; temp->d.function.params_ = params; @@ -163,7 +163,7 @@ AST* function(int ret_type, char* name, list* params, AST* body) AST* var_decl(int type_spec, char* name, int size, int ref) { - AST* temp = make_ast(yylineno, VAR_DECL); + AST* temp = make_ast(yylineno, TOKEN_VAR_DECL); temp->d.var_decl.type_spec_ = type_spec; temp->d.var_decl.name_ = name; temp->d.var_decl.size_ = size; From aa4da6e675a790237ffc103ae45365a749655a53 Mon Sep 17 00:00:00 2001 From: Max Base Date: Mon, 3 Aug 2020 19:43:31 +0430 Subject: [PATCH 4/5] Update semant.c --- semant.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/semant.c b/semant.c index 8b86d4d..ee67c53 100755 --- a/semant.c +++ b/semant.c @@ -230,93 +230,93 @@ static void semant_(AST* node, struct symtab* M, struct symtab* V) { switch (node->kind_) { - case K_NUM: + case TOKEN_K_NUM: set_type(node, TYPE_INT); set_symtab(node, M, V); break; - case BE_LT: + case TOKEN_BE_LT: semant_bexpr(node, "<", M, V); break; - case BE_LTE: + case TOKEN_BE_LTE: semant_bexpr(node, "<=", M, V); break; - case BE_GT: + case TOKEN_BE_GT: semant_bexpr(node, ">", M, V); break; - case BE_GTE: + case TOKEN_BE_GTE: semant_bexpr(node, ">=", M, V); break; - case BE_EQ: + case TOKEN_BE_EQ: semant_bexpr(node, "==", M, V); break; - case BE_NEQ: + case TOKEN_BE_NEQ: semant_bexpr(node, "!=", M, V); break; - case BE_ADD: + case TOKEN_BE_ADD: semant_bexpr(node, "+", M, V); break; - case BE_SUB: + case TOKEN_BE_SUB: semant_bexpr(node, "-", M, V); break; - case BE_MUL: + case TOKEN_BE_MUL: semant_bexpr(node, "*", M, V); break; - case BE_DIV: + case TOKEN_BE_DIV: semant_bexpr(node, "/", M, V); break; - case K_CALL: + case TOKEN_K_CALL: set_symtab(node, M, V); semant(node->d.call.args_, M, V); break; - case E_ASSIGN: + case TOKEN_E_ASSIGN: semant_assign(node, M, V); break; - case ARRAY_ACCESS: + case TOKEN_ARRAY_ACCESS: semant_array(node, M, V); break; - case VAR_NAME: + case TOKEN_VAR_NAME: semant_variable(node, M, V); break; - case RETURN: + case TOKEN_RETURN: semant_ret(node, M, V); break; - case WHILE: + case TOKEN_WHILE: semant_while(node, M, V); break; - case IF_THEN: + case TOKEN_IF_THEN: semant_if_then(node, M, V); break; - case COMPOUND: + case TOKEN_COMPOUND: semant_compound(node, M, V); break; - case PARAM: + case TOKEN_PARAM: semant_param(node, M, V); break; - case FUNCTION: + case TOKEN_FUNCTION: semant_function(node, M, V); break; - case VAR_DECL: + case TOKEN_VAR_DECL: semant_var_decl(node, M, V); break; From 33fa50b9ea05ac9e3ff93c2e482cba82ff633fbb Mon Sep 17 00:00:00 2001 From: Max Base Date: Mon, 3 Aug 2020 19:43:53 +0430 Subject: [PATCH 5/5] Update cgen.c --- cgen.c | 64 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/cgen.c b/cgen.c index ed16dfc..28c62f3 100755 --- a/cgen.c +++ b/cgen.c @@ -28,66 +28,66 @@ static void codegen_(AST* node) { switch (node->kind_) { - case K_NUM: + case TOKEN_K_NUM: { char buf[15]; sprintf(buf,"%d", node->d.val_); emit3("mov", "eax", buf); } break; // FALL THROUGH - case BE_LT: - case BE_LTE: - case BE_GT: - case BE_GTE: - case BE_EQ: - case BE_NEQ: - case BE_ADD: - case BE_SUB: - case BE_MUL: - case BE_DIV: + case TOKEN_BE_LT: + case TOKEN_BE_LTE: + case TOKEN_BE_GT: + case TOKEN_BE_GTE: + case TOKEN_BE_EQ: + case TOKEN_BE_NEQ: + case TOKEN_BE_ADD: + case TOKEN_BE_SUB: + case TOKEN_BE_MUL: + case TOKEN_BE_DIV: codegen_bexpr(node); break; - case K_CALL: + case TOKEN_K_CALL: codegen(node->d.call.args_); break; - case E_ASSIGN: + case TOKEN_E_ASSIGN: codegen_assign(node); break; - case ARRAY_ACCESS: + case TOKEN_ARRAY_ACCESS: codegen_array_access(node); break; - case VAR_NAME: + case TOKEN_VAR_NAME: codegen_variable(node); break; - case RETURN: + case TOKEN_RETURN: codegen_ret(node); break; - case WHILE: + case TOKEN_WHILE: codegen_while(node); break; - case IF_THEN: + case TOKEN_IF_THEN: codegen_if_then(node); break; - case COMPOUND: + case TOKEN_COMPOUND: codegen_compound(node); break; - case PARAM: + case TOKEN_PARAM: codegen_param(node); break; - case FUNCTION: + case TOKEN_FUNCTION: codegen_function(node); break; - case VAR_DECL: + case TOKEN_VAR_DECL: codegen_var_decl(node); break; @@ -108,40 +108,40 @@ static void codegen_bexpr(AST* node) switch(node->kind_) { - case BE_LT: + case TOKEN_BE_LT: emit3("cmp","eax","ebx"); emit2("setl","esi"); break; - case BE_LTE: + case TOKEN_BE_LTE: emit3("cmp","eax","ebx"); emit2("setle","esi"); break; - case BE_GT: + case TOKEN_BE_GT: emit3("cmp","eax","ebx"); emit2("setg","esi"); break; - case BE_GTE: + case TOKEN_BE_GTE: emit3("cmp","eax","ebx"); emit2("setge","esi"); break; - case BE_EQ: + case TOKEN_BE_EQ: emit3("cmp","eax","ebx"); emit2("sete","esi"); break; - case BE_NEQ: + case TOKEN_BE_NEQ: emit3("cmp","eax","ebx"); emit2("setne","esi"); break; - case BE_ADD: + case TOKEN_BE_ADD: emit3("add","eax","ebx"); break; - case BE_SUB: + case TOKEN_BE_SUB: emit3("sub","eax","ebx"); break; - case BE_MUL: + case TOKEN_BE_MUL: emit3("imul","eax","ebx"); break; - case BE_DIV: + case TOKEN_BE_DIV: emit3("idiv","eax","ebx"); break; default: