aboutsummaryrefslogtreecommitdiff
path: root/environment.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-17 16:56:19 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-17 16:56:19 -0500
commitde3eeacfa0151243e4ef52af3d6c2e2b731fc720 (patch)
tree4bf2ee295698a89a6378701f6e1e7aecbc552da2 /environment.c
parent60f3e91b80dee6fcd995066730058dd5bc29414d (diff)
Major cleanup
Diffstat (limited to 'environment.c')
-rw-r--r--environment.c52
1 files changed, 48 insertions, 4 deletions
diff --git a/environment.c b/environment.c
index 7c4e47c2..d6bb4c65 100644
--- a/environment.c
+++ b/environment.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
+#include <signal.h>
#include "environment.h"
#include "builtins/table.h"
@@ -21,9 +22,13 @@ static type_t *namespace_type(table_t *ns)
return Type(StructType, .fields=fields);
}
-env_t *new_environment(void)
+env_t *new_compilation_unit(void)
{
env_t *env = new(env_t);
+ env->code = new(compilation_unit_t);
+ env->types = new(table_t);
+ env->globals = new(table_t);
+ env->locals = new(table_t, .fallback=env->globals);
struct {
const char *name;
@@ -35,7 +40,7 @@ env_t *new_environment(void)
};
for (size_t i = 0; i < sizeof(global_vars)/sizeof(global_vars[0]); i++) {
- Table_str_set(&env->globals, global_vars[i].name, &global_vars[i].binding);
+ Table_str_set(env->globals, global_vars[i].name, &global_vars[i].binding);
}
struct {
@@ -53,11 +58,50 @@ env_t *new_environment(void)
};
for (size_t i = 0; i < sizeof(global_types)/sizeof(global_types[0]); i++) {
- Table_str_set(&env->globals, global_types[i].name, namespace_type(&global_types[i].namespace));
- Table_str_set(&env->types, global_types[i].name, global_types[i].type);
+ Table_str_set(env->globals, global_types[i].name, namespace_type(&global_types[i].namespace));
+ Table_str_set(env->types, global_types[i].name, global_types[i].type);
}
return env;
}
+env_t *fresh_scope(env_t *env)
+{
+ env_t *scope = new(env_t);
+ *scope = *env;
+ scope->locals = new(table_t, .fallback=env->locals);
+ return scope;
+}
+
+binding_t *get_binding(env_t *env, const char *name)
+{
+ return Table_str_get(env->locals, name);
+}
+
+void set_binding(env_t *env, const char *name, binding_t *binding)
+{
+ Table_str_set(env->locals, name, binding);
+}
+
+void compiler_err(file_t *f, const char *start, const char *end, const char *fmt, ...)
+{
+ if (isatty(STDERR_FILENO) && !getenv("NO_COLOR"))
+ fputs("\x1b[31;7;1m", stderr);
+ if (f && start && end)
+ fprintf(stderr, "%s:%ld.%ld: ", f->relative_filename, get_line_number(f, start),
+ get_line_column(f, start));
+ va_list args;
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ if (isatty(STDERR_FILENO) && !getenv("NO_COLOR"))
+ fputs(" \x1b[m", stderr);
+ fputs("\n\n", stderr);
+ if (f && start && end)
+ fprint_span(stderr, f, start, end, "\x1b[31;1m", 2, isatty(STDERR_FILENO) && !getenv("NO_COLOR"));
+
+ raise(SIGABRT);
+ exit(1);
+}
+
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0