diff --git a/lc.c b/lc.c index b643372..e4038c9 100644 --- a/lc.c +++ b/lc.c @@ -142,6 +142,14 @@ void print_ast(ast_node *node, int depth) { m = m->next; } break; + case NODE_UNION: + printf("Union: %.*s\n", (int)node->expr.structure.name_len, node->expr.structure.name); + m = node->expr.structure.members; + while (m) { + print_ast(m->type, depth + 1); + m = m->next; + } + break; case NODE_IF: printf("IfStmt (Fields missing in struct)\n"); break; diff --git a/parser.c b/parser.c index 660c86e..7303013 100644 --- a/parser.c +++ b/parser.c @@ -684,6 +684,10 @@ static member *parse_member(parser *p) if (match(p, TOKEN_STRUCT)) { type = parse_struct(p); } + if (match(p, TOKEN_UNION)) { + type = parse_struct(p); + type->type = NODE_UNION; + } if (!type) { type = parse_factor(p); if (!type) { @@ -899,6 +903,12 @@ static ast_node *parse_statement(parser *p) { return parse_struct(p); } + else if (match(p, TOKEN_UNION)) + { + ast_node *u = parse_struct(p); + u->type = NODE_UNION; + return u; + } else { ast_node *expr = parse_expression(p); diff --git a/test.c b/test.c index 544384a..4dd7f07 100644 --- a/test.c +++ b/test.c @@ -1,4 +1,4 @@ struct { - struct { u32 x, u32 y } a, + union { u32 x, u32 y } a, u32 b, }