harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit b6f4872e033667c40a7e37f54eeaba9c4241dd33
parent 54c3867be78c112abbaf77e08dd561c3b9db48e6
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Fri, 25 Dec 2020 17:10:59 -0500

parse: implement tagged union types

Diffstat:
Msrc/parse.c | 33++++++++++++++++++++++++++++++++-
1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/parse.c b/src/parse.c @@ -462,6 +462,36 @@ parse_struct_union_type(struct parser *par) } static struct ast_type * +parse_tagged_union_type(struct parser *par) +{ + trenter(TR_PARSE, "tagged union"); + struct ast_type *type = xcalloc(sizeof(struct ast_type), 1); + type->storage = TYPE_STORAGE_TAGGED_UNION; + struct ast_tagged_union_type *next = &type->tagged_union; + next->type = parse_type(par); + struct token tok = {0}; + want(par, T_BOR, &tok); + while (tok.token != T_RPAREN) { + next->next = xcalloc(sizeof(struct ast_tagged_union_type), 1); + next = next->next; + next->type = parse_type(par); + switch (lex(par->lex, &tok)) { + case T_BOR: + if (lex(par->lex, &tok) != T_RPAREN) { + unlex(par->lex, &tok); + } + break; + case T_RPAREN: + break; + default: + synassert(false, &tok, T_BOR, T_RPAREN, T_EOF); + } + } + trleave(TR_PARSE, NULL); + return type; +} + +static struct ast_type * parse_type(struct parser *par) { trenter(TR_PARSE, "type"); @@ -523,7 +553,8 @@ parse_type(struct parser *par) type = parse_struct_union_type(par); break; case T_LPAREN: - assert(0); // TODO: Tagged unions + type = parse_tagged_union_type(par); + break; case T_LBRACKET: assert(0); // TODO: Slices/arrays case T_ATTR_NORETURN: