diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-24 23:53:08 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-24 23:53:08 +0000 |
| commit | 7813a12ed59274989ce954c931bf02ebea2a0104 (patch) | |
| tree | 24737d8f7e6708b68da62bae17ada6b99c941362 | |
| parent | 5ab67fe0f9e9a61b1edab343a5d0819895fab74f (diff) | |
Fix for #115
| -rw-r--r-- | src/codegen/codegen.c | 15 | ||||
| -rw-r--r-- | tests/features/test_unions.zc | 24 |
2 files changed, 37 insertions, 2 deletions
diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c index 038f3cb..f239bb6 100644 --- a/src/codegen/codegen.c +++ b/src/codegen/codegen.c @@ -12,7 +12,6 @@ #include "zprep_plugin.h" // Emit literal expression (int, float, string, char) -// Emit literal expression (int, float, string, char) static void codegen_literal_expr(ASTNode *node, FILE *out) { if (node->literal.type_kind == LITERAL_STRING) @@ -1034,6 +1033,7 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) } int is_zen_struct = 0; + int is_union = 0; StructRef *sr = ctx->parsed_structs_list; while (sr) { @@ -1041,6 +1041,10 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) strcmp(sr->node->strct.name, struct_name) == 0) { is_zen_struct = 1; + if (sr->node->strct.is_union) + { + is_union = 1; + } break; } sr = sr->next; @@ -1048,7 +1052,14 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) if (is_zen_struct) { - fprintf(out, "(struct %s){", struct_name); + if (is_union) + { + fprintf(out, "(union %s){", struct_name); + } + else + { + fprintf(out, "(struct %s){", struct_name); + } } else { diff --git a/tests/features/test_unions.zc b/tests/features/test_unions.zc new file mode 100644 index 0000000..ee9848b --- /dev/null +++ b/tests/features/test_unions.zc @@ -0,0 +1,24 @@ + +struct Circle { + radius: f32; +} + +struct Rect { + width: f32; + height: f32; +} + +union Shape { + circle: Circle; + rect: Rect; +} + +test "union_init" { + var c = Circle{ radius: 10.0 }; + var s = Shape{ circle: c }; + assert(s.circle.radius == 10.0, "s.circle.radius != 10.0"); + + var s2 = Shape{}; + s2.rect = Rect{ width: 5.0, height: 5.0 }; + assert(s2.rect.width == 5.0, "s2.rect.width != 5.0"); +} |
