summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarson Fleming <[email protected]>2026-03-26 20:27:19 -0700
committerCarson Fleming <[email protected]>2026-03-26 20:28:40 -0700
commit21f688c1eac5fb09ae68fd9b3cfcff687de36601 (patch)
tree66b819149532a58cec305cec73b916f9d3025f88
parent59a0cdabe9a67d5c3f296f85d29f43dcadace363 (diff)
downloadccc-21f688c1eac5fb09ae68fd9b3cfcff687de36601.tar.gz
scope inheritance
-rw-r--r--codegen.c2
-rw-r--r--scope.c22
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) {