From 21f688c1eac5fb09ae68fd9b3cfcff687de36601 Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Thu, 26 Mar 2026 20:27:19 -0700 Subject: scope inheritance --- codegen.c | 2 ++ scope.c | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/codegen.c b/codegen.c index cfb4cca..d6f212a 100644 --- a/codegen.c +++ b/codegen.c @@ -184,6 +184,8 @@ void emit_code(const struct root_node* ast, const char* path) { fprintf(outfile, "section .text\n"); scope_push(&scope); + /* TODO: register all basic types */ + /* output all non-static function declarations as globals */ const struct root_node* node = ast; while (node != NULL) { diff --git a/scope.c b/scope.c index 2fe62e1..251aa51 100644 --- a/scope.c +++ b/scope.c @@ -84,10 +84,13 @@ bool scope_get_type( struct type_def* p_entry, const char* name ) { - struct type_def* type_def = *type_cell(scope, name); - if (type_def == NULL) return false; - *p_entry = *type_def; - return true; + for (; scope != NULL; scope = scope->next_out) { + struct type_def* type_def = *type_cell(scope, name); + if (type_def == NULL) continue; + *p_entry = *type_def; + return true; + } + return false; } void scope_define_type(struct scope* scope, struct type_def type) { @@ -109,10 +112,13 @@ bool scope_get_var( struct var_def* p_entry, const char* name ) { - struct var_def* var_def = *var_cell(scope, name); - if (var_def == NULL) return false; - *p_entry = *var_def; - return true; + for (; scope != NULL; scope = scope->next_out) { + struct var_def* var_def = *var_cell(scope, name); + if (var_def == NULL) continue; + *p_entry = *var_def; + return true; + } + return false; } void scope_define_var(struct scope* scope, struct var_def var) { -- cgit v1.2.3