diff --git a/include/mcc/symbol_table.h b/include/mcc/symbol_table.h index 1afdd6190698b557515700fd43d3a7a977cf2595..1603c47bb37b14ce5687193f0dbce33ac3c26901 100644 --- a/include/mcc/symbol_table.h +++ b/include/mcc/symbol_table.h @@ -56,6 +56,8 @@ struct mcc_symbol_table { struct mcc_symbol_table *mcc_symbol_table_new_table(struct mcc_symbol_table *parent); +struct mcc_symbol_table *mcc_symbol_table_get_inner_table_by_name(struct mcc_symbol_table *table, char *name); + void mcc_symbol_table_delete_table(void *parent); struct mcc_symbol_table* mcc_symbol_table_create_inner_table(struct mcc_symbol_table *parent, char *name); diff --git a/include/mcc/tac.h b/include/mcc/tac.h index 002ccd2fb54a9d7d2726d5a55ebc102222c0b42a..57c953f21302edad646008df71f3da9d511a0828 100644 --- a/include/mcc/tac.h +++ b/include/mcc/tac.h @@ -2,12 +2,14 @@ #define MCC_TAC_H #include "ast.h" +#include "symbol_table.h" #include "dynamic_array.h" #define MCC_TAC_ENTRY_SIZE 10 #define MCC_TAC_ERROR_SIZE 10 -enum mcc_tac_operation{ +enum mcc_tac_operation { + MCC_TAC_BOOL_LITERAL, MCC_TAC_INT_LITERAL, MCC_TAC_STRING_LITERAL, @@ -81,6 +83,12 @@ struct mcc_tac { struct mcc_tac *next; struct mcc_tac *prev; + + + // symbol tables + int + struct mcc_symbol_table root_table; + struct mcc_symbol_table current_symbol_table; }; struct mcc_tac_error { diff --git a/include/mcc/tac_build.h b/include/mcc/tac_build.h index b23ca4cea3986e15c56293e87a2997da5ec14b29..ff522dcd4007d712ab1af552303138c412e241c6 100644 --- a/include/mcc/tac_build.h +++ b/include/mcc/tac_build.h @@ -8,7 +8,7 @@ // builds tac by parsing program struct mcc_tac *mcc_build_tac(struct mcc_ast_program *program, struct mcc_symbol_table *st); -struct mcc_tac *mcc_tac_parse_statement(struct mcc_ast_statement *statement, struct mcc_tac *tac); +struct mcc_tac *mcc_tac_parse_statement(struct mcc_ast_statement *statement, struct mcc_tac *tac, struct mcc_symbol_table *st); void mcc_tac_parse_statement_list(struct mcc_ast_statement_list *stl, struct mcc_tac *tac); diff --git a/src/symbol_table.c b/src/symbol_table.c index c50136d7980d1c6e3576bacab0a36769f38ede34..16a9cf2ba645858a275879a067a9092f29f75719 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -90,6 +90,28 @@ struct mcc_symbol_table *mcc_symbol_table_new_table( return table; } +struct mcc_symbol_table *mcc_symbol_table_get_inner_table_by_name(struct mcc_symbol_table *table, char *name) { + struct mcc_symbol_table *result = NULL; + + if (strcmp(table -> sym_table_name, name) == 0) { + return table; + } + + + if (table -> inner_tables -> size < 1) { + return NULL; + } + + for (int i = 0; i < table -> inner_tables -> size; i++) { + struct mcc_symbol_table *t = (struct mcc_symbol_table *) table -> inner_tables -> arr[i]; + result = mcc_symbol_table_get_inner_table_by_name(t, name); + + if (result != NULL) break; + } + + return result; +} + void mcc_symbol_table_delete_table(void *table) { // Delete symbols assert(table); diff --git a/src/symbol_table_parse.c b/src/symbol_table_parse.c index 38e8dbe9e0670c84cc983ef8ad5b0df33a63d073..9b765584692ac8981b44dd5697417cb62e2cf8fe 100644 --- a/src/symbol_table_parse.c +++ b/src/symbol_table_parse.c @@ -161,22 +161,30 @@ int mcc_symbol_table_parse_statement( } else { return 1; } - case MCC_AST_STATEMENT_TYPE_IF: + case MCC_AST_STATEMENT_TYPE_IF: { if(mcc_symbol_table_validate_expression( statement->if_condition, table, ec) == 0) { - if (mcc_symbol_table_validate_condition_to_type_bool( - statement -> if_condition, + if(mcc_symbol_table_validate_condition_to_type_bool( + statement->if_condition, table, ec ) == 0) { - return mcc_symbol_table_parse_statement( + int parsed_if_statement = mcc_symbol_table_parse_statement( statement->if_stmt, table, ec, 1); + + if (parsed_if_statement == 0 && statement -> else_stmt != NULL) { + return mcc_symbol_table_parse_statement( + statement->else_stmt, table, ec, 1); + } else { + return parsed_if_statement; + } } else { return 1; } } else { return 1; } + } case MCC_AST_STATEMENT_TYPE_DECL: if (statement->declaration->arr_literal != NULL) { // array declaration diff --git a/src/tac_build.c b/src/tac_build.c index 8666c7e66f59d7d32cbaf98e544000561599108d..a0ef5597bac6e47af43706c7c1cbcfbc52b2d122 100644 --- a/src/tac_build.c +++ b/src/tac_build.c @@ -4,33 +4,36 @@ #include <assert.h> #include "tac_build.h" +#include "symbol_table.h" // -------------------------------- parse expression - -static void parse_expression_identifier(struct mcc_ast_identifier *identifier, struct mcc_tac *tac) { - - - // Get expression type from symbol table - enum mCc_ast_type expression_type = - mCc_symbol_table_check_get_expression_type( - builder->root_st, builder->current_st, expression); - - // Get tac copy operation type - enum mCc_tac_operation_type type = get_copy_type(expression_type); - - // Create arg1 from identifier - char *arg1 = id->name; - create_and_add_line_new_temp(builder, type, arg1, NULL); -} +// +//static void parse_expression_identifier(struct mcc_ast_identifier *identifier, struct mcc_tac *tac) { +// +// +// // Get expression type from symbol table +// enum mCc_ast_type expression_type = +// mCc_symbol_table_check_get_expression_type( +// builder->root_st, builder->current_st, expression); +// +// // Get tac copy operation type +// enum mCc_tac_operation_type type = get_copy_type(expression_type); +// +// // Create arg1 from identifier +// char *arg1 = id->name; +// create_and_add_line_new_temp(builder, type, arg1, NULL); +//} void mcc_tac_parse_expression(struct mcc_ast_expression *expression, struct mcc_tac *tac) { assert(expression); assert(tac); switch (expression -> type) { - case MCC_AST_EXPRESSION_TYPE_IDENTIFIER: - char *temp = + case MCC_AST_EXPRESSION_TYPE_IDENTIFIER: { + char *temp = "s"; + break; + } default: return; } @@ -51,7 +54,7 @@ struct mcc_tac *mcc_tac_parse_statement_if(struct mcc_ast_statement *if_st, stru // false condition } -void mcc_tac_parse_statement_list(struct mcc_ast_statement_list *stl, struct mcc_tac *tac) { +void mcc_tac_parse_statement_list(struct mcc_ast_statement_list *stl, struct mcc_tac *tac, struct mcc_symbol_table *st) { assert(stl); assert(tac); @@ -62,10 +65,11 @@ void mcc_tac_parse_statement_list(struct mcc_ast_statement_list *stl, struct mcc } } -struct mcc_tac *mcc_tac_parse_statement(struct mcc_ast_statement *statement, struct mcc_tac *tac) { +struct mcc_tac *mcc_tac_parse_statement(struct mcc_ast_statement *statement, struct mcc_tac *tac, struct mcc_symbol_table *st) { + switch (statement -> type) { case MCC_AST_STATEMENT_TYPE_COMPOUND: - mcc_tac_parse_statement_list(statement -> statement_list, tac); + mcc_tac_parse_statement_list(statement -> statement_list, tac, st); case MCC_AST_STATEMENT_TYPE_IF: return NULL; } @@ -73,14 +77,14 @@ struct mcc_tac *mcc_tac_parse_statement(struct mcc_ast_statement *statement, str // -------------------------------- parse function -int mcc_tac_parse_function(struct mcc_ast_function *f, struct mcc_tac *tac) { +int mcc_tac_parse_function(struct mcc_ast_function *f, struct mcc_tac *tac, struct mcc_symbol_table *st) { assert(f); assert(tac); // TODO: do something with params if (f->statement != NULL) { - mcc_tac_parse_statement(f -> statement, tac); + mcc_tac_parse_statement(f -> statement, tac, st); } } @@ -95,7 +99,11 @@ struct mcc_tac *mcc_tac_build(struct mcc_ast_program *program, struct mcc_symbol f = program -> function_def -> arr[i]; if (strcmp(f -> identifier -> i_value, "main") == 0) { - mcc_tac_parse_function(f, tac); + struct mcc_symbol_table *t = mcc_symbol_table_get_inner_table_by_name(st, "main"); + + if (t != NULL) { + mcc_tac_parse_function(f, tac, t); + } } }