diff --git a/ccodegen.cpp b/ccodegen.cpp index 9d10b8f..293a594 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -285,7 +285,7 @@ gen_ast(Ast *ast){ else if(sym->type == type_type){ if(sym->type_val->kind == TYPE_STRUCT){ Ast_Struct *agg = const_get_struct(sym->type_val->sym->ast); - if(node->value->kind == AST_STRUCT){ + if(agg){ gen("struct %s{", node->name.str); global_indent++; For(agg->members){ diff --git a/main.cpp b/main.cpp index 11af4a5..4200680 100644 --- a/main.cpp +++ b/main.cpp @@ -47,8 +47,8 @@ int main(){ test_intern_table(); lex_test(); - String result = compile_file("globals.kl"_s); - // String result = compile_file("structs.kl"_s); + // String result = compile_file("globals.kl"_s); + String result = compile_file("structs.kl"_s); // String result = compile_file("order_independent_globals.kl"_s); printf("%s", result.str); diff --git a/new_ast.cpp b/new_ast.cpp index 5626f55..744402d 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -211,9 +211,11 @@ struct Ast_Array: Ast_Expr{ Ast_Expr *expr; }; +struct Ast_Resolved_Type; struct Ast_Struct: Ast_Expr{ // Required to be Ast_Struct or Ast_Var or Ast_Const Array members; + Ast_Resolved_Type *type; }; struct Ast_Named:Ast{ @@ -469,11 +471,20 @@ ast_package(Token *pos, String name, Array decls){ // Utillities //----------------------------------------------------------------------------- function Ast_Struct * -const_get_struct(Ast *ast){ +const_try_getting_struct(Ast *ast){ assert(ast->kind == AST_CONST); Ast_Const *constant = (Ast_Const *)ast; - assert(constant->value->kind == AST_STRUCT); - return (Ast_Struct *)constant->value; + if(constant->value->kind == AST_STRUCT){ + return (Ast_Struct *)constant->value; + } + return 0; +} + +function Ast_Struct * +const_get_struct(Ast *ast){ + auto result = const_try_getting_struct(ast); + assert(result); + return result; } function Intern_String @@ -482,3 +493,13 @@ ast_get_name(Ast *ast){ auto constant = (Ast_Named *)ast; return constant->name; } + +function B32 +ast_is_struct(Ast *ast){ + if(ast->kind == AST_CONST){ + auto a = (Ast_Const *)ast; + B32 result = a->agg->kind == AST_STRUCT; + return result; + } + return false; +} diff --git a/new_resolve.cpp b/new_resolve.cpp index e3e1ed2..4142790 100644 --- a/new_resolve.cpp +++ b/new_resolve.cpp @@ -269,6 +269,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){ result.is_const = true; } else if(sym->kind == SYM_CONST || sym->kind == SYM_VAR){ + type_complete(sym->type); result.type = sym->type; result.is_const = sym->kind == SYM_CONST ? true : false; result.value = sym->value; @@ -543,6 +544,9 @@ function void resolve_package(Ast_Package *package){ For(package->decls){ resolve_name(it[0]->pos, it[0]->name); + if(ast_is_struct(it[0])){ + type_complete(const_get_struct(it[0])->type); + } } } @@ -567,10 +571,12 @@ parse_file(){ Sym *sym = sym_new(SYM_VAR, decl->name, decl); if(decl->kind == AST_CONST) { sym->kind = SYM_CONST; - auto constant = (Ast_Const *)decl; - if(constant->value->kind == AST_STRUCT){ - sym->type = type_incomplete(sym); - sym->state = SYM_RESOLVED; + Ast_Struct *s = const_try_getting_struct(decl); + if(s){ + s->type = type_incomplete(sym); + sym->type_val = s->type; + sym->type = type_type; + sym->state = SYM_RESOLVED; } } else assert(decl->kind == AST_VAR); diff --git a/new_type.cpp b/new_type.cpp index 93ce750..6ff6ab7 100644 --- a/new_type.cpp +++ b/new_type.cpp @@ -185,6 +185,7 @@ type_complete(Ast_Resolved_Type *type){ // @note: complete struct type->kind = TYPE_STRUCT; // @todo: compute size, alignement, offset + pctx->resolving_package->ordered.add((Ast_Named *)node->parent); } function Ast_Resolved_Type * diff --git a/structs.kl b/structs.kl index e3ec23d..4c56227 100644 --- a/structs.kl +++ b/structs.kl @@ -1,7 +1,7 @@ arena_pointer: *Arena = null Arena :: struct -// next: *Arena + next: *Arena data: *int len : int cap : int