find_globals: fix name assignment

This commit is contained in:
2026-03-15 13:08:37 +01:00
parent 1b1cc336c5
commit 682bfff8b6

View File

@@ -51,10 +51,14 @@ void destroy_tables(void)
/* Internal matters */
symbol_t *create_symbol(symtype_t type, node_t *node, symbol_table_t *function_symtable) {
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");
exit(EXIT_FAILURE);
}
new_symbol->type = type;
new_symbol->name = SYMBOL_TYPE_NAMES[type];
new_symbol->name = name;
new_symbol->node = node;
new_symbol->function_symtable = function_symtable;
return new_symbol;
@@ -77,14 +81,14 @@ static void find_globals(void)
node_t *global = list->children[j];
switch (global->type) {
case IDENTIFIER:
symbol_t *identifier = create_symbol(SYMBOL_GLOBAL_VAR, global, NULL);
symbol_t *identifier = create_symbol(global->data.identifier, SYMBOL_GLOBAL_VAR, global, NULL);
if (symbol_table_insert(global_symbols, identifier) != INSERT_OK) {
fprintf(stderr, "find_globals: insertion collision (identifier)");
exit(EXIT_FAILURE);
}
break;
case ARRAY_INDEXING:
symbol_t *array = create_symbol(SYMBOL_GLOBAL_ARRAY, global, NULL);
symbol_t *array = create_symbol(global->children[0]->data.identifier, SYMBOL_GLOBAL_ARRAY, global, NULL);
if (symbol_table_insert(global_symbols, array) != INSERT_OK) {
fprintf(stderr, "find_globals: insertion collision (array)");
exit(EXIT_FAILURE);
@@ -101,13 +105,14 @@ static void find_globals(void)
node_t *parameters = top_level_node->children[1]->children[0];
assert(parameters->type == LIST);
for (int j = 0; j < parameters->n_children; j++) {
symbol_t *param = create_symbol(SYMBOL_PARAMETER, parameters->children[j], NULL);
node_t *p = parameters->children[j];
symbol_t *param = create_symbol(p->data.identifier, SYMBOL_PARAMETER, p, NULL);
if (symbol_table_insert(function_symtable, param) != INSERT_OK) {
fprintf(stderr, "find_globals: insertion collision (parameter)");
exit(EXIT_FAILURE);
}
}
symbol_t *new_symbol = create_symbol(SYMBOL_FUNCTION, top_level_node, function_symtable);
symbol_t *new_symbol = create_symbol(top_level_node->children[0]->data.identifier, SYMBOL_FUNCTION, top_level_node, function_symtable);
if (symbol_table_insert(global_symbols, new_symbol) != INSERT_OK) {
fprintf(stderr, "find_globals: insertion collision (function)");
exit(EXIT_FAILURE);
@@ -140,6 +145,7 @@ 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.
}
// Prints the given symbol table, with sequence number, symbol names and types.