aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2024-06-18 19:29:14 +0200
committerBad Diode <bd@badd10de.dev>2024-06-18 19:29:14 +0200
commitc9db27abbbfa80a79515e26efaae9012d3275404 (patch)
treeff0dc2888b9e3f6e98cb76bfd07a5e3fafdd82dc
parenta0068318895ff8dea6d3c3f0db381fbca83e3f40 (diff)
downloadbdl-c9db27abbbfa80a79515e26efaae9012d3275404.tar.gz
bdl-c9db27abbbfa80a79515e26efaae9012d3275404.zip
Fix some syntax issues with match cases
-rw-r--r--src/main.c12
-rw-r--r--tests/conditionals.bad7
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) {
579 if (!node) return; 579 if (!node) return;
580 parse_expr(parser, PREC_LOW); 580 parse_expr(parser, PREC_LOW);
581 node->match_expr = array_pop(parser->nodes); 581 node->match_expr = array_pop(parser->nodes);
582 parse_consume(parser, TOK_ASSIGN,
583 cstr("malformed match statement"));
582 parse_consume(parser, TOK_LCURLY, 584 parse_consume(parser, TOK_LCURLY,
583 cstr("expected block of match cases")); 585 cstr("expected block of match cases"));
584 while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { 586 while (!parse_match(parser, TOK_RCURLY) && !parser->panic) {
585 parse_consume(parser, TOK_CASE,
586 cstr("expected case statement"));
587 Node *tmp = node_alloc(parser, NODE_CASE, parser->previous); 587 Node *tmp = node_alloc(parser, NODE_CASE, parser->previous);
588 if (!tmp) return; 588 if (!tmp) return;
589 // Are we on the default case. 589 // Are we on the default case.
590 if (!parse_match(parser, TOK_ASSIGN)) { 590 if (!parse_match(parser, TOK_ELSE)) {
591 parse_consume(parser, TOK_CASE,
592 cstr("expected case statement"));
591 parse_expr(parser, PREC_LOW); 593 parse_expr(parser, PREC_LOW);
592 tmp->case_value = array_pop(parser->nodes); 594 tmp->case_value = array_pop(parser->nodes);
593 parse_consume(parser, TOK_ASSIGN,
594 cstr("malformed case statement"));
595 } 595 }
596 parse_consume(parser, TOK_ASSIGN,
597 cstr("malformed case statement"));
596 parse_expr(parser, PREC_LOW); 598 parse_expr(parser, PREC_LOW);
597 tmp->case_expr = array_pop(parser->nodes); 599 tmp->case_expr = array_pop(parser->nodes);
598 array_push(node->match_cases, tmp, parser->storage); 600 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 {
19} 19}
20 20
21; Match cases should only apply to literal values, for example `case 1 + 2` 21; Match cases should only apply to literal values, for example `case 1 + 2`
22; isn't allowed. 22; isn't allowed. They should generally convert to a lookup table.
23match 4 * 2 { 23match a = {
24 case 8 = "hello" 24 case 8 = "hello"
25 case 9 = "world" 25 case 9 = "world"
26 case = "what" 26 else = "what"
27} 27}
28