diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-29 13:28:39 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-29 13:28:39 -0500 |
| commit | 8171a38b7130849e3049a4ea15a4fd06c154d9b0 (patch) | |
| tree | 7ec4b204e6600ac3dc553d9caadfc722d04d0c8c /environment.c | |
| parent | 764d9fe73b0c9da918d2ecf2c4031219a1fac5be (diff) | |
Add type namespaces
Diffstat (limited to 'environment.c')
| -rw-r--r-- | environment.c | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/environment.c b/environment.c index 0f0e18d2..87fe8a1e 100644 --- a/environment.c +++ b/environment.c @@ -5,6 +5,7 @@ #include "environment.h" #include "builtins/table.h" #include "builtins/string.h" +#include "typecheck.h" #include "util.h" typedef struct { @@ -12,22 +13,23 @@ typedef struct { binding_t binding; } ns_entry_t; -static type_t *namespace_type(const char *name, table_t *ns) -{ - arg_t *fields = NULL; - for (int64_t i = Table_length(ns); i >= 1; i--) { - struct {const char *name; binding_t *binding; } *entry = Table_entry(ns, i); - fields = new(arg_t, .next=fields, .name=entry->name, .type=entry->binding->type); - } - name = heap_strf("%s_namespace", name); - return Type(StructType, .name=name, .fields=fields); -} +// static type_t *namespace_type(const char *name, table_t *ns) +// { +// arg_t *fields = NULL; +// for (int64_t i = Table_length(ns); i >= 1; i--) { +// struct {const char *name; binding_t *binding; } *entry = Table_entry(ns, i); +// fields = new(arg_t, .next=fields, .name=entry->name, .type=entry->binding->type); +// } +// name = heap_strf("%s_namespace", name); +// return Type(StructType, .name=name, .fields=fields); +// } env_t *new_compilation_unit(void) { env_t *env = new(env_t); env->code = new(compilation_unit_t); env->types = new(table_t); + env->type_namespaces = new(table_t); env->globals = new(table_t); env->locals = new(table_t, .fallback=env->globals); @@ -46,31 +48,46 @@ env_t *new_compilation_unit(void) Table_str_set(env->globals, global_vars[i].name, b); } + typedef struct { + const char *name, *code, *type_str; + } ns_entry_t; + struct { const char *name; type_t *type; CORD typename; CORD struct_val; - table_t namespace; + array_t namespace; } global_types[] = { {"Bool", Type(BoolType), "Bool_t", "Bool", {}}, {"Int", Type(IntType, .bits=64), "Int_t", "Int", {}}, - {"Int32", Type(IntType, .bits=32), "Int32_t", "Int", {}}, - {"Int16", Type(IntType, .bits=16), "Int16_t", "Int", {}}, - {"Int8", Type(IntType, .bits=8), "Int8_t", "Int", {}}, + {"Int32", Type(IntType, .bits=32), "Int32_t", "Int32", {}}, + {"Int16", Type(IntType, .bits=16), "Int16_t", "Int16", {}}, + {"Int8", Type(IntType, .bits=8), "Int8_t", "Int8", {}}, {"Num", Type(NumType, .bits=64), "Num_t", "Num", {}}, {"Num32", Type(NumType, .bits=32), "Num32_t", "Num32", {}}, - {"Str", Type(StringType), "Str_t", "Str", {}}, + {"Str", Type(StringType), "Str_t", "Str", $TypedArray(ns_entry_t, + {"quoted", "Str__quoted", "func(s:Str, color=no)->Str"} + )}, }; for (size_t i = 0; i < sizeof(global_types)/sizeof(global_types[0]); i++) { - table_t *ns = new(table_t); - *ns = global_types[i].namespace; - binding_t *binding = new(binding_t, .type=namespace_type(global_types[i].name, ns)); + binding_t *binding = new(binding_t, .type=Type(TypeInfoType)); Table_str_set(env->globals, global_types[i].name, binding); Table_str_set(env->types, global_types[i].name, global_types[i].type); } + for (size_t i = 0; i < sizeof(global_types)/sizeof(global_types[0]); i++) { + table_t *namespace = new(table_t); + $ARRAY_FOREACH(global_types[i].namespace, j, ns_entry_t, entry, { + type_t *type = parse_type_string(env, entry.type_str); + binding_t *b = new(binding_t, .code=entry.code, .type=type); + Table_str_set(namespace, entry.name, b); + // printf("Bound %s:%s -> %T\n", global_types[i].name, entry.name, b->type); + }, {}) + Table_str_set(env->type_namespaces, global_types[i].name, namespace); + } + return env; } |
