From 7d673646150ec10707cfe28e147d03a2ab32b775 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 3 Feb 2022 21:13:05 -0500 Subject: Modify implementation of structs to support unions This was simple enough, we just needed to change the part where we were computing the offset for each field, and the total size of the compound structure. --- src/parser.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index 7c01c75..a9c49f9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -813,14 +813,15 @@ Lexer *remove_lexer() return lexer_stack[lexer_stack_count - 1]; } -Type *parse_struct_declaration(Lexer *lexer, bool is_global) { +Type *parse_struct_union_declaration(Lexer *lexer, bool is_global) { i64 prev_struct_count = defined_structs_count; - assert_token(Lexer_next(lexer), TOKEN_STRUCT); - - Type *struct_type = type_new(TYPE_STRUCT); + Type *struct_type; + Token token = Lexer_next(lexer); + assert(token.type == TOKEN_STRUCT || token.type == TOKEN_UNION); + struct_type = type_new(token.type == TOKEN_STRUCT ? TYPE_STRUCT : TYPE_UNION); - Token token = Lexer_peek(lexer); + token = Lexer_peek(lexer); // For nested temporary structs we don't need a name if (token.type != TOKEN_IDENTIFIER && is_global) die_location(token.loc, "You need to specify a name for the struct defined globally."); @@ -845,8 +846,8 @@ Type *parse_struct_declaration(Lexer *lexer, bool is_global) { // We want to allow nested temporary structs. Type *type; Token next = Lexer_peek(lexer); - if (next.type == TOKEN_STRUCT) { - type = parse_struct_declaration(lexer, false); + if (next.type == TOKEN_STRUCT || next.type == TOKEN_UNION) { + type = parse_struct_union_declaration(lexer, false); } else { type = parse_type(lexer); } @@ -879,8 +880,8 @@ Node *parse_program(Lexer *lexer) } else if (token.type == TOKEN_LET) { Node *var_decl = parse_var_declaration(lexer); Node_add_child(program, var_decl); - } else if (token.type == TOKEN_STRUCT) { - parse_struct_declaration(lexer, true); + } else if (token.type == TOKEN_STRUCT || token.type == TOKEN_UNION) { + parse_struct_union_declaration(lexer, true); } else if (token.type == TOKEN_IMPORT) { // TODO: Handle circular imports // TODO: Handle complex import graphs (#pragma once) -- cgit v1.2.3