From b572f4ef7c45f61dff0ccc141bea6e678ba2856c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 29 Mar 2023 09:34:33 +0200 Subject: [PATCH] Parsing union --- core_ast.cpp | 4 +++- core_parsing.cpp | 11 +++++++---- examples/push_struct.core | 4 ++++ meta.py | 9 ++++----- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/core_ast.cpp b/core_ast.cpp index 259239e..0369402 100644 --- a/core_ast.cpp +++ b/core_ast.cpp @@ -214,8 +214,10 @@ finalize_stmt_scope(Ast_Scope *scope) { } CORE_Static Ast_Decl * -ast_struct(Token *pos, Ast_Scope *scope) { +ast_struct(Token *pos, Ast_Scope *scope, Ast_Kind kind = AST_STRUCT) { + assert(kind == AST_STRUCT || kind == AST_UNION); AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE); + result->kind = kind; result->scope = scope; return result; } diff --git a/core_parsing.cpp b/core_parsing.cpp index 8dbaffb..2ed93ca 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -692,7 +692,7 @@ parse_assign_expr() { } CORE_Static Ast_Decl * -parse_struct(Token *pos) { +parse_struct(Token *pos, Ast_Kind kind) { Scratch_Scope scratch(pctx->scratch); token_match(OPEN_SCOPE); @@ -711,7 +711,7 @@ parse_struct(Token *pos) { token_expect(CLOSE_SCOPE); finalize_decl_scope(scope); - Ast_Decl *result = ast_struct(pos, scope); + Ast_Decl *result = ast_struct(pos, scope, kind); return result; } @@ -853,9 +853,12 @@ parse_decl(B32 is_global) { set_flag(flags, AST_STRICT); } - // @note parse struct binding if (token_match_keyword(pctx->keyword_struct)) { - result = parse_struct(tname); + result = parse_struct(tname, AST_STRUCT); + } + + else if (token_match_keyword(pctx->keyword_union)) { + result = parse_struct(tname, AST_UNION); } else if (token_match_keyword(pctx->keyword_enum)) { diff --git a/examples/push_struct.core b/examples/push_struct.core index 94daf95..7ae2ff4 100644 --- a/examples/push_struct.core +++ b/examples/push_struct.core @@ -12,6 +12,10 @@ 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) diff --git a/meta.py b/meta.py index 075b5f9..6789cf1 100644 --- a/meta.py +++ b/meta.py @@ -135,11 +135,11 @@ value_struct_content = """ Ast_Type *type; Ast_Decl *resolved_decl; union { - bool bool_val; - double f64_val; - Intern_String intern_val; + bool bool_val; + double f64_val; + Intern_String intern_val; BigInt big_int_val; - Ast_Type *type_val; + Ast_Type *type_val; }; """.strip() @@ -152,4 +152,3 @@ union {{ }}; }}; """.strip()) - \ No newline at end of file