Bugfix for default arguments not supporting enclosing types

This commit is contained in:
Bruce Hill 2024-09-08 21:55:15 -04:00
parent eb47f61450
commit a4fff5cb49
3 changed files with 14 additions and 2 deletions

View File

@ -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) {

View File

@ -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;
}

View File

@ -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);