aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-03-18 01:48:35 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-03-18 01:48:35 -0400
commitdd4ff777f4948402f4a38a221c15b6f3813f1bd4 (patch)
tree16df7deabcf9cdfc6580fb0b8b25eaa2000c1bf4
parentf6ae78b2cccba4d09dd6280bee65c37073e25541 (diff)
Rename/rework global env functions
-rw-r--r--environment.c18
-rw-r--r--environment.h3
-rw-r--r--repl.c2
-rw-r--r--tomo.c6
4 files changed, 11 insertions, 18 deletions
diff --git a/environment.c b/environment.c
index f43a9ca2..3ba36a31 100644
--- a/environment.c
+++ b/environment.c
@@ -50,8 +50,11 @@ static type_t *bind_type(env_t *env, const char *name, type_t *type)
return type;
}
-env_t *new_compilation_unit(CORD libname)
+env_t *global_env(void)
{
+ static env_t *_global_env = NULL;
+ if (_global_env != NULL) return _global_env;
+
env_t *env = new(env_t);
env->code = new(compilation_unit_t);
env->types = new(Table_t);
@@ -585,9 +588,8 @@ env_t *new_compilation_unit(CORD libname)
Table$str_set(env->globals, global_vars[i].name, new(binding_t, .type=type, .code=global_vars[i].code));
}
- env_t *lib_env = fresh_scope(env);
- lib_env->libname = libname;
- return lib_env;
+ _global_env = env;
+ return env;
}
CORD namespace_prefix(env_t *env, namespace_t *ns)
@@ -622,14 +624,6 @@ env_t *load_module_env(env_t *env, ast_t *ast)
return module_env;
}
-env_t *global_scope(env_t *env)
-{
- env_t *scope = new(env_t);
- *scope = *env;
- scope->locals = new(Table_t, .fallback=env->globals);
- return scope;
-}
-
env_t *namespace_scope(env_t *env)
{
env_t *scope = new(env_t);
diff --git a/environment.h b/environment.h
index b86fba20..aa820892 100644
--- a/environment.h
+++ b/environment.h
@@ -55,11 +55,10 @@ typedef struct {
CORD code;
} binding_t;
-env_t *new_compilation_unit(CORD libname);
+env_t *global_env(void);
env_t *load_module_env(env_t *env, ast_t *ast);
CORD namespace_prefix(env_t *env, namespace_t *ns);
env_t *get_namespace_by_type(env_t *env, type_t *t);
-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);
diff --git a/repl.c b/repl.c
index 474532ec..25242f5a 100644
--- a/repl.c
+++ b/repl.c
@@ -41,7 +41,7 @@ static PUREFUNC repl_binding_t *get_repl_binding(env_t *env, const char *name)
void repl(void)
{
- env_t *env = new_compilation_unit(NULL);
+ env_t *env = global_env();
void *dl = dlopen("libtomo.so", RTLD_LAZY);
if (!dl) errx(1, "I couldn't find libtomo.so in your library paths");
diff --git a/tomo.c b/tomo.c
index 15dcad86..b8adab8c 100644
--- a/tomo.c
+++ b/tomo.c
@@ -181,7 +181,7 @@ int main(int argc, char *argv[])
else if (files.length != 1)
errx(1, "Too many files specified!");
Path_t path = *(Path_t*)files.data;
- env_t *env = new_compilation_unit(NULL);
+ env_t *env = global_env();
Array_t object_files = {},
extra_ldlibs = {};
compile_files(env, files, false, &object_files, &extra_ldlibs);
@@ -194,7 +194,7 @@ int main(int argc, char *argv[])
execv(prog_args[0], prog_args);
errx(1, "Failed to run compiled program");
} else {
- env_t *env = new_compilation_unit(NULL);
+ env_t *env = global_env();
Array_t object_files = {},
extra_ldlibs = {};
compile_files(env, files, stop_at_obj_compilation, &object_files, &extra_ldlibs);
@@ -318,7 +318,7 @@ static void _compile_file_header_for_library(env_t *env, Path_t path, Table_t *v
void build_library(Text_t lib_dir_name)
{
Array_t tm_files = Path$glob(Path("./[!._0-9]*.tm"));
- env_t *env = new_compilation_unit(NULL);
+ env_t *env = fresh_scope(global_env());
Array_t object_files = {},
extra_ldlibs = {};
compile_files(env, tm_files, false, &object_files, &extra_ldlibs);