Added strings, and cleaned up

main
Øyvind Skaaden 2022-03-17 22:00:38 +01:00
parent d80961ec56
commit e839190fbf
1 changed files with 44 additions and 15 deletions

View File

@ -116,6 +116,17 @@ print_global_tree(symbol_t* global)
free(locals_list); 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 void
print_symbol_table ( void ) print_symbol_table ( void )
@ -133,6 +144,8 @@ print_symbol_table ( void )
print_global_tree(global_list[g]); print_global_tree(global_list[g]);
} }
free(global_list); free(global_list);
print_string_list();
} }
void void
@ -283,7 +296,6 @@ find_globals ( void )
break; break;
} }
} }
} }
void void
@ -318,35 +330,29 @@ void
insert_local_to_scope(symbol_t *local) insert_local_to_scope(symbol_t *local)
{ {
insert_symbol(scopes[cur_scope_depth - 1], 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 void
insert_local_to_func(symbol_t *function, symbol_t *local) insert_local_to_func(symbol_t *function, symbol_t *root)
{ {
tlhash_insert( tlhash_insert(
function->locals, //! Insert local to the function var table function->locals, //! Insert local to the function var table
&local->seq, //! The key is a number, unique, strictly growing &root->seq, //! The key is a number, unique, strictly growing
sizeof(local->seq), //! Size of key sizeof(root->seq), //! Size of key
local //! The local symbol root //! The local symbol
); );
} }
void 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); size_t sequence = tlhash_size(function->locals);
symbol_t *variable = malloc(sizeof(symbol_t)); symbol_t *variable = malloc(sizeof(symbol_t));
*variable = (symbol_t){ *variable = (symbol_t){
.type = SYM_LOCAL_VAR, .type = SYM_LOCAL_VAR,
.name = local->data, .name = root->data,
.node = local, .node = root,
.seq = sequence, //! Use sequence as name in var list of function, strictly growing .seq = sequence, //! Use sequence as name in var list of function, strictly growing
.nparms = 0, .nparms = 0,
.locals = NULL .locals = NULL
@ -355,10 +361,32 @@ insert_local_var(symbol_t *function, node_t *local)
insert_local_to_func(function, variable); 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 void
bind_names ( symbol_t *function, node_t *root ) bind_names ( symbol_t *function, node_t *root )
{ {
/* TODO: Bind names and string literals in local symbol table */
if (!function) if (!function)
return; return;
if (!root) if (!root)
@ -393,6 +421,7 @@ bind_names ( symbol_t *function, node_t *root )
break; break;
case STRING_DATA: case STRING_DATA:
collect_string(root);
break; break;
default: default: