diff --git a/src/symbol_table_parse.c b/src/symbol_table_parse.c index 5c05ce8da40936adfd080f63ca2ec08e4dd07fd4..38e8dbe9e0670c84cc983ef8ad5b0df33a63d073 100644 --- a/src/symbol_table_parse.c +++ b/src/symbol_table_parse.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <assert.h> #include "mcc/ast.h" +#include "mcc/ast_print.h" #include "mcc/symbol_table_parse.h" #include "utils/unused.h" @@ -137,7 +138,7 @@ int mcc_symbol_table_parse_statement( struct mcc_symbol_table *table = symbol_table; if (create_new) { - table = mcc_symbol_table_create_inner_table(symbol_table,symbol_table->parent->sym_table_name); + table = mcc_symbol_table_create_inner_table(symbol_table,"COMPOUND_STMT"); } switch(statement->type) { @@ -340,6 +341,7 @@ struct mcc_symbol_table *mcc_symbol_table_build_program(struct mcc_ast_program * assert(program); struct mcc_symbol_table *st = mcc_symbol_table_new_table(NULL); + st->sym_table_name = "functions"; mcc_symbol_table_add_builtins(st); @@ -361,7 +363,7 @@ struct mcc_symbol_table *mcc_symbol_table_build_function(struct mcc_ast_function assert(function); struct mcc_symbol_table *st = mcc_symbol_table_new_table(NULL); - + st->sym_table_name = "functions"; mcc_symbol_table_add_builtins(st); int parse_function = mcc_symbol_table_add_function_declaration(function, st, ec); diff --git a/src/symbol_table_print.c b/src/symbol_table_print.c index b9d406514e635bb02a112f5a9b0909536df7c130..07b2f01d43daeeaf9aeaf58a90aa52e02a6ef8f3 100644 --- a/src/symbol_table_print.c +++ b/src/symbol_table_print.c @@ -5,6 +5,7 @@ #include "mcc/symbol_table.h" #include "mcc/symbol_table_print.h" + static char *type_to_string(enum mcc_ast_data_type type){ switch (type) { @@ -29,12 +30,16 @@ static char *symbol_type_to_string(enum mcc_symbol_type type){ } void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out){ + printf("----------------------------------\n"); + printf("Table: %s // Child of: %s\n", symbol_table ? symbol_table->sym_table_name : "no name", + symbol_table->parent ? symbol_table->parent->sym_table_name : "no parent"); + printf("----------------------------------\n"); for(int i = 0; i < symbol_table->symbols->size; i++){ struct mcc_symbol *sym = (struct mcc_symbol *) symbol_table->symbols->arr[i]; if(sym->symbol_type != MCC_SYMBOL_TYPE_FUNCTION){ fprintf(out,"\t"); - } + } fprintf(out,"%*s | ", 15, symbol_type_to_string(sym->symbol_type)); fprintf(out,"%*s | ", 6, type_to_string(sym->data_type)); @@ -43,7 +48,7 @@ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out){ case MCC_SYMBOL_TYPE_BUILTIN_FUNCTION: case MCC_SYMBOL_TYPE_VARIABLE: fprintf(out,"%s", sym->variable_name); - break; + break; case MCC_SYMBOL_TYPE_ARRAY: fprintf(out,"%s[%ld]", sym->variable_name, sym->array_size); break; @@ -63,21 +68,19 @@ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out){ }else{ fprintf(out,"%s()", sym->variable_name); } - fprintf(out,"\n"); - for(int j = 0; j < symbol_table-> inner_tables -> size; j++){ - struct mcc_symbol_table *it= (struct mcc_symbol_table *) symbol_table -> inner_tables -> arr[j]; - if(it->sym_table_name == sym->variable_name){ - mcc_symbol_table_print(it,out); - } - - } + fprintf(out,"\n"); break; } + fprintf(out,"\n"); } + for (int i = 0; i < symbol_table->inner_tables->size; i++) { + mcc_symbol_table_print(symbol_table->inner_tables->arr[i],out); + } } + void mcc_symbol_table_print_error(struct mcc_symbol_table_error_collector *ec, FILE *out) { // for now only one error in collector struct mcc_semantic_error *error = ec -> errors -> arr[0]; diff --git a/test/unit/symbol_table_test.c b/test/unit/symbol_table_test.c index f383bde8adae92631d3c468d3e5c12f7cb9c5a48..621f65ccf5585567292614d2d7136672567d7592 100644 --- a/test/unit/symbol_table_test.c +++ b/test/unit/symbol_table_test.c @@ -39,7 +39,7 @@ void clean_pointers( struct mcc_ast_program *p, struct mcc_symbol_table_error_collector *ec) { mcc_symbol_table_delete_error_collector(ec); - // mcc_ast_delete_program(p); + mcc_ast_delete_program(p); } @@ -406,11 +406,11 @@ void InvalidReturnType(CuTest *ct) { TEST(BinaryOpHandsideNumberType) \ TEST(BinaryOpHandsideDivisionByZero) \ TEST(ConditionBoolExpected) \ - // The following tests are currently not working due to unknown reasons + /*The following tests are currently not working due to unknown reasons // TEST(WrongArgumentType) \ // TEST(NoReturnInNonVoidFunction) \ // TEST(InvalidReturnType) \ - // TEST(WrongNumOfArguments) \ + // TEST(WrongNumOfArguments) \ */ #include "main_stub.inc"