aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-03 13:04:50 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-03 13:04:50 -0500
commitec7a9e5f107a68072b3a4dd84aa6a4b461657345 (patch)
tree2a03723c2d8060e4503c6b9ecb97baad9432cd3d
parent4dc70c84d4226174d44f1b53599d08de7a4e83aa (diff)
Initial pass at namespacing
-rw-r--r--builtins/integers.h1
-rw-r--r--compile.c19
-rw-r--r--enums.c9
-rw-r--r--environment.c21
-rw-r--r--environment.h2
-rw-r--r--structs.c9
-rw-r--r--typecheck.c73
-rw-r--r--typecheck.h1
-rw-r--r--types.c2
-rw-r--r--types.h5
10 files changed, 86 insertions, 56 deletions
diff --git a/builtins/integers.h b/builtins/integers.h
index 85b6e928..65a52a59 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -43,7 +43,6 @@ DEFINE_INT_TYPE(int8_t, Int8);
#define Int__hex Int64__hex
#define Int__octal Int64__octal
#define Int__random Int64__random
-#define Int_namespace_t Int64_namespace_t
#define Int Int64
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/compile.c b/compile.c
index d18445d0..4f221996 100644
--- a/compile.c
+++ b/compile.c
@@ -609,15 +609,21 @@ CORD compile(env_t *env, ast_t *ast)
if (ast->tag == FunctionCall) {
auto call = Match(ast, FunctionCall);
fn_t = get_type(env, call->fn);
- if (fn_t->tag != FunctionType)
+ if (fn_t->tag == TypeInfoType) {
+ type_t *t = Match(fn_t, TypeInfoType)->type;
+ if (!(t->tag == StructType))
+ code_err(call->fn, "This is not a type that has a constructor");
+ fn_t = Type(FunctionType, .args=Match(t, StructType)->fields, .ret=t);
+ } else if (fn_t->tag != FunctionType) {
code_err(call->fn, "This is not a function, it's a %T", fn_t);
+ }
args = call->args;
fn = compile(env, call->fn);
} else {
auto method = Match(ast, MethodCall);
fn_t = get_method_type(env, method->self, method->name);
args = new(arg_ast_t, .value=method->self, .next=method->args);
- binding_t *b = get_method_binding(env, method->self, method->name);
+ binding_t *b = get_namespace_binding(env, method->self, method->name);
if (!b) code_err(ast, "No such method");
fn = b->code;
}
@@ -902,6 +908,15 @@ CORD compile(env_t *env, ast_t *ast)
type_t *fielded_t = get_type(env, f->fielded);
type_t *value_t = value_type(fielded_t);
switch (value_t->tag) {
+ case TypeInfoType: {
+ auto info = Match(value_t, TypeInfoType);
+ table_t *namespace = Table_str_get(env->type_namespaces, info->name);
+ if (!namespace) code_err(f->fielded, "I couldn't find a namespace for this type");
+ binding_t *b = Table_str_get(namespace, f->field);
+ if (!b) code_err(ast, "I couldn't find the field '%s' on this type", f->field);
+ if (!b->code) code_err(ast, "I couldn't figure out how to compile this field");
+ return b->code;
+ }
case StructType: {
for (arg_t *field = Match(value_t, StructType)->fields; field; field = field->next) {
if (streq(field->name, f->field)) {
diff --git a/enums.c b/enums.c
index 890bf551..83663736 100644
--- a/enums.c
+++ b/enums.c
@@ -95,8 +95,7 @@ void compile_enum_def(env_t *env, ast_t *ast)
{
auto def = Match(ast, EnumDef);
CORD_appendf(&env->code->typedefs, "typedef struct %s_s %s_t;\n", def->name, def->name);
- CORD_appendf(&env->code->typedefs, "typedef struct { TypeInfo type; } %s_namespace_t;\n", def->name);
- CORD_appendf(&env->code->typedefs, "extern %s_namespace_t %s;\n", def->name, def->name);
+ CORD_appendf(&env->code->typedefs, "extern const TypeInfo %s;\n", def->name);
CORD enum_def = CORD_all("struct ", def->name, "_s {\n"
"\tenum {");
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
@@ -108,14 +107,14 @@ void compile_enum_def(env_t *env, ast_t *ast)
for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
compile_struct_def(env, WrapAST(ast, StructDef, .name=heap_strf("%s$%s", def->name, tag->name), .fields=tag->fields));
enum_def = CORD_all(enum_def, def->name, "$", tag->name, "_t ", tag->name, ";\n");
- CORD_appendf(&env->code->typedefs, "#define %s__%s(...) ((%s_t){$tag$%s$%s, .%s={__VA_ARGS__}})\n",
+ CORD_appendf(&env->code->typedefs, "#define %s$tagged$%s(...) ((%s_t){$tag$%s$%s, .%s={__VA_ARGS__}})\n",
def->name, tag->name, def->name, def->name, tag->name, tag->name);
}
enum_def = CORD_cat(enum_def, "};\n};\n");
env->code->typecode = CORD_cat(env->code->typecode, enum_def);
type_t *t = Table_str_get(env->types, def->name);
- CORD typeinfo = CORD_asprintf("public %s_namespace_t %s = {{%zu, %zu, {.tag=CustomInfo, .CustomInfo={",
+ CORD typeinfo = CORD_asprintf("public const TypeInfo %s = {%zu, %zu, {.tag=CustomInfo, .CustomInfo={",
def->name, def->name, type_size(t), type_align(t));
env->code->funcs = CORD_all(
@@ -129,7 +128,7 @@ void compile_enum_def(env_t *env, ast_t *ast)
".equal=(void*)", def->name, "__equal, "
".hash=(void*)", def->name, "__hash, "
".compare=(void*)", def->name, "__compare");
- typeinfo = CORD_cat(typeinfo, "}}}};\n");
+ typeinfo = CORD_cat(typeinfo, "}}};\n");
env->code->typeinfos = CORD_all(env->code->typeinfos, typeinfo);
}
diff --git a/environment.c b/environment.c
index d76118cc..a702129e 100644
--- a/environment.c
+++ b/environment.c
@@ -72,7 +72,7 @@ env_t *new_compilation_unit(void)
};
for (size_t i = 0; i < sizeof(global_types)/sizeof(global_types[0]); i++) {
- binding_t *binding = new(binding_t, .type=Type(TypeInfoType));
+ binding_t *binding = new(binding_t, .type=Type(TypeInfoType, .name=global_types[i].name, .type=global_types[i].type));
Table_str_set(env->globals, global_types[i].name, binding);
Table_str_set(env->types, global_types[i].name, global_types[i].type);
}
@@ -104,7 +104,7 @@ binding_t *get_binding(env_t *env, const char *name)
return Table_str_get(env->locals, name);
}
-binding_t *get_method_binding(env_t *env, ast_t *self, const char *name)
+binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name)
{
type_t *self_type = get_type(env, self);
if (!self_type)
@@ -121,11 +121,18 @@ binding_t *get_method_binding(env_t *env, ast_t *self, const char *name)
table_t *ns = Table_str_get(env->type_namespaces, "Str");
return Table_str_get(ns, name);
}
- case StructType: case EnumType: {
- errx(1, "Struct/enum methods not implemented");
- // const char *name = cls_type->tag == StructType ? Match(cls_type, StructType)->name : Match(cls_type, EnumType)->name;
- // table_t *namespace = Table_str_get(env->type_namespaces, name);
- // if (!name)
+ case TypeInfoType: case StructType: case EnumType: {
+ const char *name;
+ switch (cls_type->tag) {
+ case TypeInfoType: name = Match(cls_type, TypeInfoType)->name; break;
+ case StructType: name = Match(cls_type, StructType)->name; break;
+ case EnumType: name = Match(cls_type, EnumType)->name; break;
+ default: errx(1, "Unreachable");
+ }
+
+ table_t *namespace = Table_str_get(env->type_namespaces, name);
+ if (!namespace) return NULL;
+ return Table_str_get(namespace, name);
}
default: break;
}
diff --git a/environment.h b/environment.h
index cf7bc546..bbbdd01d 100644
--- a/environment.h
+++ b/environment.h
@@ -32,7 +32,7 @@ __attribute__((noreturn))
void compiler_err(file_t *f, const char *start, const char *end, const char *fmt, ...);
binding_t *get_binding(env_t *env, const char *name);
void set_binding(env_t *env, const char *name, binding_t *binding);
-binding_t *get_method_binding(env_t *env, ast_t *self, const char *name);
+binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name);
#define code_err(ast, ...) compiler_err((ast)->file, (ast)->start, (ast)->end, __VA_ARGS__)
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/structs.c b/structs.c
index 0869bd9b..5e297927 100644
--- a/structs.c
+++ b/structs.c
@@ -137,12 +137,11 @@ void compile_struct_def(env_t *env, ast_t *ast)
CORD_appendf(&env->code->typecode, "};\n");
// Typeinfo:
- CORD_appendf(&env->code->typedefs, "typedef struct { TypeInfo type; } %s_namespace_t;\n", def->name);
- CORD_appendf(&env->code->typedefs, "extern %s_namespace_t %s;\n", def->name, def->name);
+ CORD_appendf(&env->code->typedefs, "extern const TypeInfo %s;\n", def->name);
type_t *t = Table_str_get(env->types, def->name);
- CORD typeinfo = CORD_asprintf("public %s_namespace_t %s = {{%zu, %zu, {.tag=CustomInfo, .CustomInfo={",
- def->name, def->name, type_size(t), type_align(t));
+ CORD typeinfo = CORD_asprintf("public const TypeInfo %s = {%zu, %zu, {.tag=CustomInfo, .CustomInfo={",
+ def->name, type_size(t), type_align(t));
typeinfo = CORD_all(typeinfo, ".as_str=(void*)", def->name, "__as_str, ");
env->code->funcs = CORD_all(env->code->funcs, compile_str_method(env, ast));
@@ -156,7 +155,7 @@ void compile_struct_def(env_t *env, ast_t *ast)
".hash=(void*)", def->name, "__hash, "
".compare=(void*)", def->name, "__compare");
}
- typeinfo = CORD_cat(typeinfo, "}}}};\n");
+ typeinfo = CORD_cat(typeinfo, "}}};\n");
env->code->typeinfos = CORD_all(env->code->typeinfos, typeinfo);
}
diff --git a/typecheck.c b/typecheck.c
index 0cfa11cf..aba9ec2b 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -110,6 +110,10 @@ void bind_statement(env_t *env, ast_t *statement)
}
case StructDef: {
auto def = Match(statement, StructDef);
+
+ table_t *namespace = new(table_t);
+ Table_str_set(env->type_namespaces, def->name, namespace);
+
arg_t *fields = NULL;
type_t *type = Type(StructType, .name=def->name, .fields=fields); // placeholder
for (arg_ast_t *field_ast = def->fields; field_ast; field_ast = field_ast->next) {
@@ -119,15 +123,19 @@ void bind_statement(env_t *env, ast_t *statement)
REVERSE_LIST(fields);
type->__data.StructType.fields = fields; // populate placeholder
Table_str_set(env->types, def->name, type);
+
+ // TODO: bind body members
- if (!type) code_err(statement, "I couldn't get this type");
- type_t *constructor_t = Type(FunctionType, .args=Match(type, StructType)->fields, .ret=type);
- Table_str_set(env->globals, def->name, new(binding_t, .type=constructor_t));
+ type_t *typeinfo_type = Type(TypeInfoType, .name=def->name, .type=type);
+ Table_str_set(env->globals, def->name, new(binding_t, .type=typeinfo_type));
break;
}
case EnumDef: {
auto def = Match(statement, EnumDef);
+ table_t *namespace = new(table_t);
+ Table_str_set(env->type_namespaces, def->name, namespace);
+
tag_t *tags = NULL;
type_t *type = Type(EnumType, .name=def->name, .tags=tags); // placeholder
for (tag_ast_t *tag_ast = def->tags; tag_ast; tag_ast = tag_ast->next) {
@@ -144,12 +152,16 @@ void bind_statement(env_t *env, ast_t *statement)
type->__data.EnumType.tags = tags;
for (tag_t *tag = tags; tag; tag = tag->next) {
- const char *name = heap_strf("%s__%s", def->name, tag->name);
type_t *constructor_t = Type(FunctionType, .args=Match(tag->type, StructType)->fields, .ret=type);
- Table_str_set(env->globals, name, new(binding_t, .type=constructor_t));
+ Table_str_set(namespace, tag->name, new(binding_t, .type=constructor_t, .code=CORD_all(def->name, "$tagged$", tag->name)));
Table_str_set(env->types, heap_strf("%s$%s", def->name, tag->name), tag->type);
}
Table_str_set(env->types, def->name, type);
+
+ type_t *typeinfo_type = Type(TypeInfoType, .name=def->name, .type=type);
+ Table_str_set(env->globals, def->name, new(binding_t, .type=typeinfo_type));
+
+ // TODO: bind body members
break;
}
default: break;
@@ -179,7 +191,7 @@ type_t *get_method_type(env_t *env, ast_t *self, const char *name)
type_t *self_type = get_type(env, self);
if (!self_type)
code_err(self, "I couldn't get this type");
- binding_t *b = get_method_binding(env, self, name);
+ binding_t *b = get_namespace_binding(env, self, name);
if (!b || !b->type)
code_err(self, "No such method: %s", name);
return b->type;
@@ -314,6 +326,14 @@ type_t *get_type(env_t *env, ast_t *ast)
case FieldAccess: {
auto access = Match(ast, FieldAccess);
type_t *fielded_t = get_type(env, access->fielded);
+ if (fielded_t->tag == TypeInfoType) {
+ auto info = Match(fielded_t, TypeInfoType);
+ table_t *namespace = Table_str_get(env->type_namespaces, info->name);
+ if (!namespace) code_err(access->fielded, "I couldn't find a namespace for this type");
+ binding_t *b = Table_str_get(namespace, access->field);
+ if (!b) code_err(ast, "I couldn't find the field '%s' on this type", access->field);
+ return b->type;
+ }
type_t *field_t = get_field_type(fielded_t, access->field);
if (!field_t)
code_err(ast, "%T objects don't have a field called '%s'", fielded_t, access->field);
@@ -357,6 +377,13 @@ type_t *get_type(env_t *env, ast_t *ast)
type_t *fn_type_t = get_type(env, call->fn);
if (!fn_type_t)
code_err(call->fn, "I couldn't find this function");
+
+ if (fn_type_t->tag == TypeInfoType) {
+ type_t *t = Match(fn_type_t, TypeInfoType)->type;
+ if (t->tag == StructType)
+ return t; // Constructor
+ code_err(call->fn, "This is not a type that has a constructor");
+ }
if (fn_type_t->tag != FunctionType)
code_err(call->fn, "This isn't a function, it's a %T", fn_type_t);
auto fn_type = Match(fn_type_t, FunctionType);
@@ -676,24 +703,15 @@ bool is_discardable(env_t *env, ast_t *ast)
return (t->tag == VoidType || t->tag == AbortType);
}
-
-type_t *get_namespace_type(env_t *env, ast_t *namespace_ast, type_t *type)
+type_t *get_file_type(env_t *env, const char *path)
{
- arg_t *ns_fields = NULL;
- if (type) {
- ns_fields = new(arg_t, .name="type", .type=Type(TypeInfoType), .next=ns_fields);
- if (type->tag == EnumType) {
- // Add enum constructors:
- auto enum_ = Match(type, EnumType);
- for (tag_t *tag = enum_->tags; tag; tag = tag->next) {
- type_t *constructor_t = Type(FunctionType, .args=Match(tag->type, StructType)->fields,
- .ret=type);
- ns_fields = new(arg_t, .name=tag->name, .type=constructor_t, .next=ns_fields);
- }
- }
- }
+ // auto info = get_file_info(env, path);
+ file_t *f = load_file(path);
+ ast_t *ast = parse_file(f, NULL);
+ if (!ast) compiler_err(NULL, NULL, NULL, "Couldn't parse file: %s", path);
- for (ast_list_t *stmts = Match(namespace_ast, Block)->statements; stmts; stmts = stmts->next) {
+ arg_t *ns_fields = NULL;
+ for (ast_list_t *stmts = Match(ast, Block)->statements; stmts; stmts = stmts->next) {
ast_t *stmt = stmts->ast;
doctest_inner:
switch (stmt->tag) {
@@ -717,16 +735,7 @@ type_t *get_namespace_type(env_t *env, ast_t *namespace_ast, type_t *type)
default: break;
}
}
- return Type(StructType, .fields=ns_fields);
-}
-
-type_t *get_file_type(env_t *env, const char *path)
-{
- // auto info = get_file_info(env, path);
- file_t *f = load_file(path);
- ast_t *ast = parse_file(f, NULL);
- if (!ast) compiler_err(NULL, NULL, NULL, "Couldn't parse file: %s", path);
- return get_namespace_type(env, ast, NULL);
+ return Type(StructType, .name=path, .fields=ns_fields);
}
type_t *get_arg_ast_type(env_t *env, arg_ast_t *arg)
diff --git a/typecheck.h b/typecheck.h
index c99d4fbf..b2f27c38 100644
--- a/typecheck.h
+++ b/typecheck.h
@@ -12,7 +12,6 @@ type_t *get_type(env_t *env, ast_t *ast);
void bind_statement(env_t *env, ast_t *statement);
type_t *get_math_type(env_t *env, ast_t *ast, type_t *lhs_t, type_t *rhs_t);
bool is_discardable(env_t *env, ast_t *ast);
-type_t *get_namespace_type(env_t *env, ast_t *namespace_ast, type_t *type);
type_t *get_file_type(env_t *env, const char *path);
type_t *get_function_def_type(env_t *env, ast_t *ast);
type_t *get_arg_type(env_t *env, arg_t *arg);
diff --git a/types.c b/types.c
index 5e37536b..f03fe263 100644
--- a/types.c
+++ b/types.c
@@ -53,7 +53,7 @@ CORD type_to_cord(type_t *t) {
return tagged->name;
}
case TypeInfoType: {
- return "TypeInfo";
+ return CORD_all("TypeInfo(", Match(t, TypeInfoType)->name, ")");
}
default: {
raise(SIGABRT);
diff --git a/types.h b/types.h
index 4fcc391f..073bbe3b 100644
--- a/types.h
+++ b/types.h
@@ -85,7 +85,10 @@ struct type_s {
const char *name;
tag_t *tags;
} EnumType;
- struct {} TypeInfoType;
+ struct {
+ const char *name;
+ type_t *type;
+ } TypeInfoType;
} __data;
};