diff --git a/ps4/src.tar.gz b/ps4/src.tar.gz new file mode 100644 index 0000000..3ac6eda Binary files /dev/null and b/ps4/src.tar.gz differ diff --git a/ps4/src/symbols.c b/ps4/src/symbols.c index 289ee0b..51d6c11 100644 --- a/ps4/src/symbols.c +++ b/ps4/src/symbols.c @@ -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: diff --git a/ps4/vsl_programs/ps3-simplify/var-convert.vsl b/ps4/vsl_programs/ps3-simplify/var-convert.vsl index 7d29d26..3fc80cf 100644 --- a/ps4/vsl_programs/ps3-simplify/var-convert.vsl +++ b/ps4/vsl_programs/ps3-simplify/var-convert.vsl @@ -3,8 +3,8 @@ func main() { print(b) - var c - c = 10 + var d + d = 10 print(c) }