From a4fff5cb49b25aca4c889ec312ddf4c6c530c69b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 8 Sep 2024 21:55:15 -0400 Subject: [PATCH] Bugfix for default arguments not supporting enclosing types --- compile.c | 3 ++- environment.c | 10 ++++++++++ environment.h | 3 ++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/compile.c b/compile.c index b2c59bb..6b6db1d 100644 --- a/compile.c +++ b/compile.c @@ -1399,7 +1399,7 @@ CORD compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t { Table_t used_args = {}; CORD code = CORD_EMPTY; - env_t *default_scope = global_scope(env); + env_t *default_scope = namespace_scope(env); for (arg_t *spec_arg = spec_args; spec_arg; spec_arg = spec_arg->next) { int64_t i = 1; // Find keyword: @@ -1935,6 +1935,7 @@ CORD compile(env_t *env, ast_t *ast) CORD code = "({\n"; deferral_t *prev_deferred = env->deferred; env = fresh_scope(env); + printf("Prebinding block\n"); for (ast_list_t *stmt = stmts; stmt; stmt = stmt->next) prebind_statement(env, stmt->ast); for (ast_list_t *stmt = stmts; stmt; stmt = stmt->next) { diff --git a/environment.c b/environment.c index be0db69..3cb47b9 100644 --- a/environment.c +++ b/environment.c @@ -347,6 +347,7 @@ env_t *load_module_env(env_t *env, ast_t *ast) env_t *module_env = fresh_scope(env); module_env->code = new(compilation_unit_t); module_env->namespace = new(namespace_t, .name=file_base_name(name)); + module_env->namespace_bindings = module_env->locals; Table$str_set(module_env->imports, name, module_env); for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) @@ -366,6 +367,14 @@ env_t *global_scope(env_t *env) return scope; } +env_t *namespace_scope(env_t *env) +{ + env_t *scope = new(env_t); + *scope = *env; + scope->locals = new(Table_t, .fallback=env->namespace_bindings ? env->namespace_bindings : env->globals); + return scope; +} + env_t *fresh_scope(env_t *env) { env_t *scope = new(env_t); @@ -491,6 +500,7 @@ env_t *namespace_env(env_t *env, const char *namespace_name) *ns_env = *env; ns_env->locals = new(Table_t, .fallback=env->locals); ns_env->namespace = new(namespace_t, .name=namespace_name, .parent=env->namespace); + ns_env->namespace_bindings = ns_env->locals; return ns_env; } diff --git a/environment.h b/environment.h index f54eccc..1e13d5a 100644 --- a/environment.h +++ b/environment.h @@ -42,7 +42,7 @@ typedef struct namespace_s { } namespace_t; typedef struct env_s { - Table_t *types, *globals, *locals; + Table_t *types, *globals, *namespace_bindings, *locals; // Lookup table for env_t* where the key is: // - Resolved path for local imports (so that `use ./foo.tm` is the same as `use ./baz/../foo.tm`) // - Raw 'use' string for module imports @@ -68,6 +68,7 @@ env_t *new_compilation_unit(CORD *libname); env_t *load_module_env(env_t *env, ast_t *ast); CORD namespace_prefix(CORD *libname, namespace_t *ns); env_t *global_scope(env_t *env); +env_t *namespace_scope(env_t *env); env_t *fresh_scope(env_t *env); env_t *for_scope(env_t *env, ast_t *ast); env_t *namespace_env(env_t *env, const char *namespace_name);