diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 1af464d..8bc718a 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -517,7 +517,7 @@ gen_expr(Ast_Expr *ast) { } For(node->exprs) { - if (is_struct(node->resolved_type)) + if (is_struct(node->resolved_type) || is_union(node->resolved_type)) gen(".%Q = ", it->resolved_name); else if (is_array(node->resolved_type)) gen("[%d] = ", (int)it->resolved_index); diff --git a/core_main.cpp b/core_main.cpp index 16f1313..2f30f5d 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -192,7 +192,7 @@ int main(int argument_count, char **arguments) { else { String program_name = string_from_cstring(arguments[1]); - compile_file(&arena, program_name, COMPILE_PRINT_STATS); + compile_file(&arena, program_name, COMPILE_PRINT_STATS | COMPILE_AND_RUN); } } printf("End of program\n"); diff --git a/core_typechecking.cpp b/core_typechecking.cpp index cf913ee..acacb50 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1047,6 +1047,9 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type) { CORE_Static void resolve_compound_struct(Ast_Call *node, Ast_Type *type) { + if (node->exprs.len != 1 && is_union(type)) + compiler_error(node->pos, "Too many union initializers. Only 1 named initializer required."); + S64 default_counter = 0; For(node->exprs) { if (is_flag_set(it->call_flags, CALL_INDEX)) @@ -1074,6 +1077,8 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type) { compiler_error(it->pos, "No member with that name"); } else { + if (is_union(type)) + compiler_error(it->pos, "Unions can only be initialized using named fields"); if (default_counter == -1) compiler_error(it->pos, "Cant mix named fields and normal fields"); if (default_counter >= type->agg.members.len) @@ -1463,7 +1468,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str if (is_array(type) || is_slice(type)) { resolve_compound_array(node, type); } - else if (is_struct(type)) { + else if (is_struct(type) || is_union(type)) { resolve_compound_struct(node, type); } else { diff --git a/examples/push_struct.core b/examples/push_struct.core index be30931..94daf95 100644 --- a/examples/push_struct.core +++ b/examples/push_struct.core @@ -12,15 +12,9 @@ PushStruct :: (a: *MA.Arena, type: Type): *void result := MA.PushSize(a, ti.size->U64) return result -U :: union - a: F64 - b: F32 - main :: (argc: int, argv: **char): int arena: MA.Arena a: *int = PushStruct(&arena, int) Assert(arena.len == SizeOf(int)) - memes: U - return 0 \ No newline at end of file diff --git a/examples/unions.core b/examples/unions.core new file mode 100644 index 0000000..d491a51 --- /dev/null +++ b/examples/unions.core @@ -0,0 +1,32 @@ + +U :: union + a: F64 + b: F32 + +C :: struct + a: int + b: int + +Test :: struct + a: int + +main :: (argc: int, argv: **char): int + memes: U + memes.b = 10 + Assert(memes.b == 10) + Assert(memes.a != 0) + + compound: U = {b = 10.0} + c2: C = {b = 10} + +/* @reproduction @todo +``` +examples/unions.core - Error! Couldn't infer type of compound expression + c = {10} +``` + + c: C + c = {10} +*/ + + return 0 \ No newline at end of file