aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--enums.c7
-rw-r--r--structs.c9
-rw-r--r--test/structs.tm2
-rw-r--r--typecheck.c4
4 files changed, 14 insertions, 8 deletions
diff --git a/enums.c b/enums.c
index 684eb7f7..38e5444b 100644
--- a/enums.c
+++ b/enums.c
@@ -28,7 +28,7 @@ static CORD compile_str_method(env_t *env, ast_t *ast)
}
for (arg_ast_t *field = tag->fields; field; field = field->next) {
- type_t *field_t = parse_type_ast(env, field->type);
+ type_t *field_t = get_arg_ast_type(env, field);
CORD field_str = expr_as_texting(env, CORD_all("obj->", tag->name, ".", field->name), field_t, "use_color");
str_func = CORD_all(str_func, ", \"", field->name, "=\", ", field_str);
if (field->next) str_func = CORD_cat(str_func, ", \", \"");
@@ -113,7 +113,10 @@ void compile_enum_def(env_t *env, ast_t *ast)
// Constructor:
CORD arg_sig = CORD_EMPTY;
for (arg_ast_t *field = tag->fields; field; field = field->next) {
- arg_sig = CORD_all(arg_sig, compile_type_ast(field->type), " ", field->name);
+ type_t *field_t = get_arg_ast_type(env, field);
+ CORD type_code = compile_type(field_t);
+ arg_sig = CORD_all(arg_sig, type_code, " ", field->name);
+ if (CORD_cmp(type_code, "Bool_t") == 0) arg_sig = CORD_cat(arg_sig, ":1");
if (field->next) arg_sig = CORD_cat(arg_sig, ", ");
}
if (arg_sig == CORD_EMPTY) arg_sig = "void";
diff --git a/structs.c b/structs.c
index 970fc8ca..0ec08e5c 100644
--- a/structs.c
+++ b/structs.c
@@ -90,7 +90,7 @@ static CORD compile_equals_method(env_t *env, ast_t *ast)
"_t *y, const TypeInfo *info) {\n"
"(void)info;\n");
for (arg_ast_t *field = def->fields; field; field = field->next) {
- type_t *field_type = parse_type_ast(env, field->type);
+ type_t *field_type = get_arg_ast_type(env, field);
switch (field_type->tag) {
case BoolType: case IntType: case NumType: case PointerType: case FunctionType:
eq_func = CORD_all(eq_func, "if (x->", field->name, " != y->", field->name, ") return no;\n");
@@ -133,9 +133,10 @@ void compile_struct_def(env_t *env, ast_t *ast)
CORD_appendf(&env->code->typecode, "struct %s_s {\n", def->name);
for (arg_ast_t *field = def->fields; field; field = field->next) {
- CORD type = compile_type_ast(field->type);
- CORD_appendf(&env->code->typecode, "%r %s%s;\n", type, field->name,
- CORD_cmp(type, "Bool_t") ? "" : ":1");
+ type_t *field_t = get_arg_ast_type(env, field);
+ CORD type_code = compile_type(field_t);
+ CORD_appendf(&env->code->typecode, "%r %s%s;\n", type_code, field->name,
+ CORD_cmp(type_code, "Bool_t") ? "" : ":1");
}
CORD_appendf(&env->code->typecode, "};\n");
diff --git a/test/structs.tm b/test/structs.tm
index c86f9102..c46c87e4 100644
--- a/test/structs.tm
+++ b/test/structs.tm
@@ -50,3 +50,5 @@ func test_mixed()
= "missing"
test_mixed()
+struct LinkedList(x:Int, next=!LinkedList)
+>> @LinkedList(10, @LinkedList(20))
diff --git a/typecheck.c b/typecheck.c
index 98597330..165e74dd 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -121,7 +121,7 @@ void bind_statement(env_t *env, ast_t *statement)
type_t *type = Type(StructType, .name=def->name, .fields=fields, .opaque=true); // placeholder
Table_str_set(env->types, def->name, type);
for (arg_ast_t *field_ast = def->fields; field_ast; field_ast = field_ast->next) {
- type_t *field_t = parse_type_ast(env, field_ast->type);
+ type_t *field_t = get_arg_ast_type(env, field_ast);
if ((field_t->tag == StructType && Match(field_t, StructType)->opaque)
|| (field_t->tag == EnumType && Match(field_t, EnumType)->opaque))
code_err(field_ast->type, "This type is recursive and would create an infinitely sized struct. Try using a pointer.");
@@ -150,7 +150,7 @@ void bind_statement(env_t *env, ast_t *statement)
for (tag_ast_t *tag_ast = def->tags; tag_ast; tag_ast = tag_ast->next) {
arg_t *fields = NULL;
for (arg_ast_t *field_ast = tag_ast->fields; field_ast; field_ast = field_ast->next) {
- type_t *field_t = parse_type_ast(env, field_ast->type);
+ type_t *field_t = get_arg_ast_type(env, field_ast);
if ((field_t->tag == StructType && Match(field_t, StructType)->opaque)
|| (field_t->tag == EnumType && Match(field_t, EnumType)->opaque))
code_err(field_ast->type, "This type is recursive and would create an infinitely sized struct. Try using a pointer.");