Parsing union

This commit is contained in:
Krzosa Karol
2023-03-29 09:34:33 +02:00
parent fa26e9d218
commit b572f4ef7c
4 changed files with 18 additions and 10 deletions

View File

@@ -214,8 +214,10 @@ finalize_stmt_scope(Ast_Scope *scope) {
} }
CORE_Static Ast_Decl * 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); AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE);
result->kind = kind;
result->scope = scope; result->scope = scope;
return result; return result;
} }

View File

@@ -692,7 +692,7 @@ parse_assign_expr() {
} }
CORE_Static Ast_Decl * CORE_Static Ast_Decl *
parse_struct(Token *pos) { parse_struct(Token *pos, Ast_Kind kind) {
Scratch_Scope scratch(pctx->scratch); Scratch_Scope scratch(pctx->scratch);
token_match(OPEN_SCOPE); token_match(OPEN_SCOPE);
@@ -711,7 +711,7 @@ parse_struct(Token *pos) {
token_expect(CLOSE_SCOPE); token_expect(CLOSE_SCOPE);
finalize_decl_scope(scope); finalize_decl_scope(scope);
Ast_Decl *result = ast_struct(pos, scope); Ast_Decl *result = ast_struct(pos, scope, kind);
return result; return result;
} }
@@ -853,9 +853,12 @@ parse_decl(B32 is_global) {
set_flag(flags, AST_STRICT); set_flag(flags, AST_STRICT);
} }
// @note parse struct binding
if (token_match_keyword(pctx->keyword_struct)) { 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)) { else if (token_match_keyword(pctx->keyword_enum)) {

View File

@@ -12,6 +12,10 @@ PushStruct :: (a: *MA.Arena, type: Type): *void
result := MA.PushSize(a, ti.size->U64) result := MA.PushSize(a, ti.size->U64)
return result return result
U :: union
a: F64
b: F32
main :: (argc: int, argv: **char): int main :: (argc: int, argv: **char): int
arena: MA.Arena arena: MA.Arena
a: *int = PushStruct(&arena, int) a: *int = PushStruct(&arena, int)

View File

@@ -135,11 +135,11 @@ value_struct_content = """
Ast_Type *type; Ast_Type *type;
Ast_Decl *resolved_decl; Ast_Decl *resolved_decl;
union { union {
bool bool_val; bool bool_val;
double f64_val; double f64_val;
Intern_String intern_val; Intern_String intern_val;
BigInt big_int_val; BigInt big_int_val;
Ast_Type *type_val; Ast_Type *type_val;
}; };
""".strip() """.strip()
@@ -152,4 +152,3 @@ union {{
}}; }};
}}; }};
""".strip()) """.strip())