From c9db27abbbfa80a79515e26efaae9012d3275404 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 18 Jun 2024 19:29:14 +0200 Subject: Fix some syntax issues with match cases --- src/main.c | 12 +++++++----- tests/conditionals.bad | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 1bb5ff6..ccd4877 100644 --- a/src/main.c +++ b/src/main.c @@ -579,20 +579,22 @@ parse_keyword(Parser *parser) { if (!node) return; parse_expr(parser, PREC_LOW); node->match_expr = array_pop(parser->nodes); + parse_consume(parser, TOK_ASSIGN, + cstr("malformed match statement")); parse_consume(parser, TOK_LCURLY, cstr("expected block of match cases")); while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { - parse_consume(parser, TOK_CASE, - cstr("expected case statement")); Node *tmp = node_alloc(parser, NODE_CASE, parser->previous); if (!tmp) return; // Are we on the default case. - if (!parse_match(parser, TOK_ASSIGN)) { + if (!parse_match(parser, TOK_ELSE)) { + parse_consume(parser, TOK_CASE, + cstr("expected case statement")); parse_expr(parser, PREC_LOW); tmp->case_value = array_pop(parser->nodes); - parse_consume(parser, TOK_ASSIGN, - cstr("malformed case statement")); } + parse_consume(parser, TOK_ASSIGN, + cstr("malformed case statement")); parse_expr(parser, PREC_LOW); tmp->case_expr = array_pop(parser->nodes); array_push(node->match_cases, tmp, parser->storage); diff --git a/tests/conditionals.bad b/tests/conditionals.bad index 71d49c3..b1c6a89 100644 --- a/tests/conditionals.bad +++ b/tests/conditionals.bad @@ -19,9 +19,10 @@ if true != false { } ; Match cases should only apply to literal values, for example `case 1 + 2` -; isn't allowed. -match 4 * 2 { +; isn't allowed. They should generally convert to a lookup table. +match a = { case 8 = "hello" case 9 = "world" - case = "what" + else = "what" } + -- cgit v1.2.1