Skip to content
Snippets Groups Projects
Commit 157e76cf authored by FlorianKrull's avatar FlorianKrull
Browse files

Fixed symbol table printing

parent b6e62f10
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "mcc/ast.h" #include "mcc/ast.h"
#include "mcc/ast_print.h"
#include "mcc/symbol_table_parse.h" #include "mcc/symbol_table_parse.h"
#include "utils/unused.h" #include "utils/unused.h"
...@@ -137,7 +138,7 @@ int mcc_symbol_table_parse_statement( ...@@ -137,7 +138,7 @@ int mcc_symbol_table_parse_statement(
struct mcc_symbol_table *table = symbol_table; struct mcc_symbol_table *table = symbol_table;
if (create_new) { 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) { switch(statement->type) {
...@@ -340,6 +341,7 @@ struct mcc_symbol_table *mcc_symbol_table_build_program(struct mcc_ast_program * ...@@ -340,6 +341,7 @@ struct mcc_symbol_table *mcc_symbol_table_build_program(struct mcc_ast_program *
assert(program); assert(program);
struct mcc_symbol_table *st = mcc_symbol_table_new_table(NULL); struct mcc_symbol_table *st = mcc_symbol_table_new_table(NULL);
st->sym_table_name = "functions";
mcc_symbol_table_add_builtins(st); mcc_symbol_table_add_builtins(st);
...@@ -361,7 +363,7 @@ struct mcc_symbol_table *mcc_symbol_table_build_function(struct mcc_ast_function ...@@ -361,7 +363,7 @@ struct mcc_symbol_table *mcc_symbol_table_build_function(struct mcc_ast_function
assert(function); assert(function);
struct mcc_symbol_table *st = mcc_symbol_table_new_table(NULL); struct mcc_symbol_table *st = mcc_symbol_table_new_table(NULL);
st->sym_table_name = "functions";
mcc_symbol_table_add_builtins(st); mcc_symbol_table_add_builtins(st);
int parse_function = mcc_symbol_table_add_function_declaration(function, st, ec); int parse_function = mcc_symbol_table_add_function_declaration(function, st, ec);
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "mcc/symbol_table.h" #include "mcc/symbol_table.h"
#include "mcc/symbol_table_print.h" #include "mcc/symbol_table_print.h"
int global_level;
static char *type_to_string(enum mcc_ast_data_type type){ static char *type_to_string(enum mcc_ast_data_type type){
switch (type) switch (type)
{ {
...@@ -29,13 +31,20 @@ static char *symbol_type_to_string(enum mcc_symbol_type type){ ...@@ -29,13 +31,20 @@ static char *symbol_type_to_string(enum mcc_symbol_type type){
} }
void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out, int level){ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out, int level){
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++){ for(int i = 0; i < symbol_table->symbols->size; i++){
struct mcc_symbol *sym = (struct mcc_symbol *) symbol_table->symbols->arr[i]; struct mcc_symbol *sym = (struct mcc_symbol *) symbol_table->symbols->arr[i];
if(sym->symbol_type != MCC_SYMBOL_TYPE_FUNCTION){ if(sym->symbol_type != MCC_SYMBOL_TYPE_FUNCTION){
fprintf(out,"\t"); fprintf(out,"\t");
} }
for(int j = 1;j < global_level;j++){
fprintf(out,"\t");
}
fprintf(out,"%*s | ", 15, symbol_type_to_string(sym->symbol_type)); fprintf(out,"%*s | ", 15, symbol_type_to_string(sym->symbol_type));
fprintf(out,"%*s | ", 6, type_to_string(sym->data_type)); fprintf(out,"%*s | ", 6, type_to_string(sym->data_type));
...@@ -43,7 +52,7 @@ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out, in ...@@ -43,7 +52,7 @@ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out, in
case MCC_SYMBOL_TYPE_BUILTIN_FUNCTION: case MCC_SYMBOL_TYPE_BUILTIN_FUNCTION:
case MCC_SYMBOL_TYPE_VARIABLE: case MCC_SYMBOL_TYPE_VARIABLE:
fprintf(out,"%s", sym->variable_name); fprintf(out,"%s", sym->variable_name);
break; break;
case MCC_SYMBOL_TYPE_ARRAY: case MCC_SYMBOL_TYPE_ARRAY:
fprintf(out,"%s[%ld]", sym->variable_name, sym->array_size); fprintf(out,"%s[%ld]", sym->variable_name, sym->array_size);
break; break;
...@@ -63,15 +72,14 @@ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out, in ...@@ -63,15 +72,14 @@ void mcc_symbol_table_print(struct mcc_symbol_table *symbol_table, FILE *out, in
}else{ }else{
fprintf(out,"%s()", sym->variable_name); fprintf(out,"%s()", sym->variable_name);
} }
fprintf(out,"\n"); fprintf(out,"\n");
break; break;
} }
fprintf(out,"\n"); fprintf(out,"\n");
} }
for (int i = 0; i < symbol_table->inner_tables->size; i++) { for (int i = 0; i < symbol_table->inner_tables->size; i++) {
mcc_symbol_table_print(symbol_table->inner_tables->arr[i],out,i); mcc_symbol_table_print(symbol_table->inner_tables->arr[i],out,i);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment