This commit is contained in:
2026-03-16 22:14:31 +01:00
parent 682bfff8b6
commit be4ad427f0
+41 -3
View File
@@ -25,7 +25,9 @@ void create_tables(void)
for (int i = 0; i < global_symbols->n_symbols; i++) {
symbol_t *symbol = global_symbols->symbols[i];
if (symbol->type == SYMBOL_FUNCTION) {
bind_names(symbol->function_symtable, symbol->node);
node_t *block = symbol->node->children[2];
assert(block->type == BLOCK);
bind_names(symbol->function_symtable, block->children[0]);
}
}
}
@@ -51,7 +53,7 @@ void destroy_tables(void)
/* Internal matters */
symbol_t *create_symbol(char *name, symtype_t type, node_t *node, symbol_table_t *function_symtable) {
static symbol_t *create_symbol(char *name, symtype_t type, node_t *node, symbol_table_t *function_symtable) {
symbol_t *new_symbol = malloc(sizeof(symbol_t));
if (!new_symbol) {
fprintf(stderr, "create_symbol: failed to allocate");
@@ -145,7 +147,43 @@ static void bind_names(symbol_table_t* local_symbols, node_t* node)
// a parent scope, or in the global symbol table, that is an error.
// Feel free to print a nice error message and abort.
// We will not test your compiler on incorrect VSL.
symbol_hashmap_t *hashmap = symbol_hashmap_init();
assert(node->type == LIST);
for (int i = 0; i < node->n_children; i++) {
node_t statement_or_declaration = node->children[i];
switch (statement_or_declaration->type) {
case LOCAL_VARIABLE_DECLARATION:
node_t *local_variables = statement_or_declaration->children[0];
for (int j = 0; j < local_variables->n_children; j++) {
node_t *var = local_variables->children[j];
assert(var->type == LOCAL_VARIABLE);
symbol_t *sym = create_symbol(var->children[0]->data.identifier, var->type, var, NULL);
if (symbol_table_insert(local_symbols, sym) != INSERT_OK) {
fprintf(stderr, "bind_names: failed to insert local variable");
exit(EXIT_FAILURE);
}
}
break;
case BLOCK:
break;
case PRINT_STATEMENT:
node_t *print_list = statement_or_declaration->children[0];
for (int j = 0; j < print_list->n_children; j++) {
node_t *print_item = print_list->children[j];
if (print_item->type != STRING_LITERAL) continue;
size_t pos = add_string(print_item->data.string_literal);
node_t *new = node_create(STRING_LIST_REFERENCE);
new->data.string_list_index = pos;
node_finalize(print_item);
print_list->children[j] = new;
}
break;
case PRINTLN_STATEMENT:
fprintf(stderr, "println encountered: this should've been handled last time...");
exit(EXIT_FAILURE);
break;
}
}
}
// Prints the given symbol table, with sequence number, symbol names and types.