From e839190fbf103a7d02d033ac83bf6fe7f5ee1312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind?= Date: Thu, 17 Mar 2022 22:00:38 +0100 Subject: [PATCH] Added strings, and cleaned up --- exercises/04/vslc/src/ir.c | 59 ++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/exercises/04/vslc/src/ir.c b/exercises/04/vslc/src/ir.c index 4b6690b..f79546c 100644 --- a/exercises/04/vslc/src/ir.c +++ b/exercises/04/vslc/src/ir.c @@ -116,6 +116,17 @@ print_global_tree(symbol_t* global) free(locals_list); } +void +print_string_list(void) +{ + printf("─STRINGS [%ld]\n", stringc); + for (uint64_t i = 0; i < stringc; i++) + printf(" %s─[%ld]: %s\n", + (i < (stringc - 1)) ? "├" : "└", + i, string_list[i] + ); +} + void print_symbol_table ( void ) @@ -133,6 +144,8 @@ print_symbol_table ( void ) print_global_tree(global_list[g]); } free(global_list); + + print_string_list(); } void @@ -283,7 +296,6 @@ find_globals ( void ) break; } } - } void @@ -318,35 +330,29 @@ void insert_local_to_scope(symbol_t *local) { insert_symbol(scopes[cur_scope_depth - 1], local); - /*tlhash_insert( - scopes[cur_scope_depth - 1], //! Insert local to topmost scope - local->name, //! Key is name, this is used to do lookup - strlen(local->name), //! Length of key - local //! The local symbol - );*/ } void -insert_local_to_func(symbol_t *function, symbol_t *local) +insert_local_to_func(symbol_t *function, symbol_t *root) { tlhash_insert( function->locals, //! Insert local to the function var table - &local->seq, //! The key is a number, unique, strictly growing - sizeof(local->seq), //! Size of key - local //! The local symbol + &root->seq, //! The key is a number, unique, strictly growing + sizeof(root->seq), //! Size of key + root //! The local symbol ); } void -insert_local_var(symbol_t *function, node_t *local) +insert_local_var(symbol_t *function, node_t *root) { size_t sequence = tlhash_size(function->locals); symbol_t *variable = malloc(sizeof(symbol_t)); *variable = (symbol_t){ .type = SYM_LOCAL_VAR, - .name = local->data, - .node = local, + .name = root->data, + .node = root, .seq = sequence, //! Use sequence as name in var list of function, strictly growing .nparms = 0, .locals = NULL @@ -355,10 +361,32 @@ insert_local_var(symbol_t *function, node_t *local) insert_local_to_func(function, variable); } +void +collect_string(node_t *root) +{ + if (!root->data) + return; + + string_list[stringc] = root->data; + root->data = malloc(sizeof(size_t)); + *((size_t*)root->data) = stringc++; + + if (stringc >= n_string_list) + { + n_string_list *= 2; + char **new_string_list = realloc(string_list, n_string_list * sizeof(char*)); + if (!new_string_list) + { + fprintf(stderr, "[ERROR] Could not realloc string list!\n"); + exit(EXIT_FAILURE); + } + string_list = new_string_list; + } +} + void bind_names ( symbol_t *function, node_t *root ) { - /* TODO: Bind names and string literals in local symbol table */ if (!function) return; if (!root) @@ -393,6 +421,7 @@ bind_names ( symbol_t *function, node_t *root ) break; case STRING_DATA: + collect_string(root); break; default: