ps4: fix 2

This commit is contained in:
2026-03-26 21:17:27 +01:00
parent f0689ae844
commit 0c15e23a12
3 changed files with 26 additions and 26 deletions

BIN
ps4/src.tar.gz Normal file

Binary file not shown.

View File

@@ -26,7 +26,7 @@ void create_tables(void)
symbol_t *symbol = global_symbols->symbols[i];
if (symbol->type == SYMBOL_FUNCTION) {
node_t *block = symbol->node->children[2];
bind_names(symbol->function_symtable, block->children[0]);
bind_names(symbol->function_symtable, block);
}
}
}
@@ -146,38 +146,38 @@ static void bind_names(symbol_table_t* local_symbols, node_t* node)
local_symbols->hashmap = inner->backup;
symbol_hashmap_destroy(inner);
return;
case LOCAL_VARIABLE_DECLARATION:
node_t *local_variables = node->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, SYMBOL_LOCAL_VAR, var, NULL);
if (symbol_table_insert(local_symbols, sym) != INSERT_OK) {
fprintf(stderr, "bind_names: failed to insert local variable");
exit(EXIT_FAILURE);
}
case LOCAL_VARIABLE:
symbol_t *s = create_symbol(node->children[0]->data.identifier, SYMBOL_LOCAL_VAR, node, NULL);
if (symbol_table_insert(local_symbols, s) != INSERT_OK) {
fprintf(stderr, "bind_names: failed to insert local variable\n");
exit(EXIT_FAILURE);
}
for (int k = 1; k < node->n_children; k++)
bind_names(local_symbols, node->children[k]);
break;
case PRINTLN_STATEMENT:
case PRINT_STATEMENT:
node_t *print_list = node->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, 0);
new->data.string_list_index = pos;
free(print_item->children);
free(print_item);
print_list->children[j] = new;
if (print_item->type == STRING_LITERAL) {
size_t pos = add_string(print_item->data.string_literal);
node_t *new = node_create(STRING_LIST_REFERENCE, 0);
new->data.string_list_index = pos;
free(print_item->children);
free(print_item);
print_list->children[j] = new;
} else {
bind_names(local_symbols, print_item);
}
}
break;
case PRINTLN_STATEMENT:
fprintf(stderr, "println encountered: this should've been handled last time...");
exit(EXIT_FAILURE);
break;
case IDENTIFIER:
symbol_t *sym = symbol_hashmap_lookup(local_symbols->hashmap, node->data.identifier);
if (!sym) sym = symbol_hashmap_lookup(global_symbols->hashmap, node->data.identifier);
symbol_t *sym = NULL;
for (symbol_hashmap_t *scope = local_symbols->hashmap; scope && !sym; scope = scope->backup)
sym = symbol_hashmap_lookup(scope, node->data.identifier);
if (!sym)
sym = symbol_hashmap_lookup(global_symbols->hashmap, node->data.identifier);
node->symbol = sym;
return;
default:

View File

@@ -3,8 +3,8 @@ func main() {
print(b)
var c
c = 10
var d
d = 10
print(c)
}