harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 0776ad2e4eed263119947c3db9a092bb88b71b6c
parent 0f9f57f9dbe0db242a2ccaef28d4fe0d227cebf1
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Sat, 21 May 2022 03:03:03 +0200

prohibit enum definitions outside type declarations

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Msrc/parse.c | 12++++++++----
Mtests/15-enums.ha | 8++++++++
2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/parse.c b/src/parse.c @@ -671,9 +671,6 @@ parse_type(struct lexer *lexer) unlex(lexer, &tok); type = parse_primitive_type(lexer); break; - case T_ENUM: - type = parse_enum_type(lexer); - break; case T_NULLABLE: nullable = true; want(lexer, T_TIMES, NULL); @@ -2439,7 +2436,14 @@ parse_type_decl(struct lexer *lexer, struct ast_type_decl *decl) while (more) { parse_identifier(lexer, &i->ident, false); want(lexer, T_EQUAL, NULL); - i->type = parse_type(lexer); + switch (lex(lexer, &tok)) { + case T_ENUM: + i->type = parse_enum_type(lexer); + break; + default: + unlex(lexer, &tok); + i->type = parse_type(lexer); + } switch (lex(lexer, &tok)) { case T_COMMA: if (lex(lexer, &tok) == T_NAME) { diff --git a/tests/15-enums.ha b/tests/15-enums.ha @@ -1,3 +1,5 @@ +use rt; + type implicit_values = enum { ZERO, ONE, @@ -56,8 +58,14 @@ fn storage() void = { assert(rune_storage::FOO == '0' && rune_storage::BAR == '1'); }; +fn reject() void = { + assert(rt::compile("export let a: enum { A, B } = 0;") != 0); + assert(rt::compile("export let a: int = 0: enum{A, B}: int;") != 0); +}; + export fn main() void = { implicit(); explicit(); storage(); + reject(); };