aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-03 21:13:05 -0500
committerMustafa Quraish <[email protected]>2022-02-03 21:13:05 -0500
commit7d673646150ec10707cfe28e147d03a2ab32b775 (patch)
tree68bc84a8e44d2b162086d3d693aedb0c52fd1610 /src/parser.c
parentAdd support for basic structs (diff)
downloadcup-7d673646150ec10707cfe28e147d03a2ab32b775.tar.xz
cup-7d673646150ec10707cfe28e147d03a2ab32b775.zip
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.
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c19
1 files changed, 10 insertions, 9 deletions
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)