diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-25 10:46:09 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-25 10:46:09 +0200 |
commit | f39149eaac8a7e6400aca1edea4bf47e3941a3fc (patch) | |
tree | ac51b3d0b7158c836129589c6b86ca9d072f7425 | |
parent | bcc40682a0d699e8c36df62975c12cdea491d167 (diff) | |
download | bdl-f39149eaac8a7e6400aca1edea4bf47e3941a3fc.tar.gz bdl-f39149eaac8a7e6400aca1edea4bf47e3941a3fc.zip |
Remove parenthesis requirement from match, while, if
-rw-r--r-- | src/parser.c | 12 | ||||
-rw-r--r-- | tests/conditionals.bad | 14 | ||||
-rw-r--r-- | tests/loops.bad | 8 |
3 files changed, 11 insertions, 23 deletions
diff --git a/src/parser.c b/src/parser.c index b08c82a..a6d9f83 100644 --- a/src/parser.c +++ b/src/parser.c | |||
@@ -587,12 +587,8 @@ parse_keyword(Parser *parser) { | |||
587 | case TOK_IF: { | 587 | case TOK_IF: { |
588 | node = node_alloc(parser, NODE_IF, prev); | 588 | node = node_alloc(parser, NODE_IF, prev); |
589 | if (!node) return; | 589 | if (!node) return; |
590 | parse_consume(parser, TOK_LPAREN, | ||
591 | cstr("expected '(' on if expression")); | ||
592 | parse_expr(parser, PREC_LOW); | 590 | parse_expr(parser, PREC_LOW); |
593 | node->cond_if = array_pop(parser->nodes); | 591 | node->cond_if = array_pop(parser->nodes); |
594 | parse_consume(parser, TOK_RPAREN, | ||
595 | cstr("expected ')' on if expression")); | ||
596 | parse_expr(parser, PREC_LOW); | 592 | parse_expr(parser, PREC_LOW); |
597 | node->cond_expr = array_pop(parser->nodes); | 593 | node->cond_expr = array_pop(parser->nodes); |
598 | if (parse_match(parser, TOK_ELSE)) { | 594 | if (parse_match(parser, TOK_ELSE)) { |
@@ -603,12 +599,8 @@ parse_keyword(Parser *parser) { | |||
603 | case TOK_MATCH: { | 599 | case TOK_MATCH: { |
604 | node = node_alloc(parser, NODE_MATCH, prev); | 600 | node = node_alloc(parser, NODE_MATCH, prev); |
605 | if (!node) return; | 601 | if (!node) return; |
606 | parse_consume(parser, TOK_LPAREN, | ||
607 | cstr("expected '(' on if expression")); | ||
608 | parse_expr(parser, PREC_LOW); | 602 | parse_expr(parser, PREC_LOW); |
609 | node->match_expr = array_pop(parser->nodes); | 603 | node->match_expr = array_pop(parser->nodes); |
610 | parse_consume(parser, TOK_RPAREN, | ||
611 | cstr("expected ')' on if expression")); | ||
612 | parse_consume(parser, TOK_LCURLY, | 604 | parse_consume(parser, TOK_LCURLY, |
613 | cstr("expected block of match cases")); | 605 | cstr("expected block of match cases")); |
614 | while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { | 606 | while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { |
@@ -698,12 +690,8 @@ parse_keyword(Parser *parser) { | |||
698 | case TOK_WHILE: { | 690 | case TOK_WHILE: { |
699 | node = node_alloc(parser, NODE_WHILE, prev); | 691 | node = node_alloc(parser, NODE_WHILE, prev); |
700 | if (!node) return; | 692 | if (!node) return; |
701 | parse_consume(parser, TOK_LPAREN, | ||
702 | cstr("expected '(' on while expression")); | ||
703 | parse_expr(parser, PREC_LOW); | 693 | parse_expr(parser, PREC_LOW); |
704 | node->while_cond = array_pop(parser->nodes); | 694 | node->while_cond = array_pop(parser->nodes); |
705 | parse_consume(parser, TOK_RPAREN, | ||
706 | cstr("expected ')' on while expression")); | ||
707 | parse_expr(parser, PREC_LOW); | 695 | parse_expr(parser, PREC_LOW); |
708 | node->while_expr = array_pop(parser->nodes); | 696 | node->while_expr = array_pop(parser->nodes); |
709 | } break; | 697 | } break; |
diff --git a/tests/conditionals.bad b/tests/conditionals.bad index 50d0cfc..772bd07 100644 --- a/tests/conditionals.bad +++ b/tests/conditionals.bad | |||
@@ -1,5 +1,5 @@ | |||
1 | ; Basic if expressions. | 1 | ; Basic if expressions. |
2 | if (true) "hello" | 2 | if true "hello" |
3 | 3 | ||
4 | ; These can produce values and the result bound to a name. | 4 | ; These can produce values and the result bound to a name. |
5 | let a = if (2 + 2 >= 4) 42 | 5 | let a = if (2 + 2 >= 4) 42 |
@@ -8,19 +8,19 @@ let a = if (2 + 2 >= 4) 42 | |||
8 | let b = if (0xff == 0x32) "hello" else "world" | 8 | let b = if (0xff == 0x32) "hello" else "world" |
9 | 9 | ||
10 | ; ... but these can compound on each other | 10 | ; ... but these can compound on each other |
11 | if (1 < 2) 6 | 11 | if 1 < 2 6 |
12 | else if (1 > 2) 7 | 12 | else if 1 > 2 7 |
13 | else 8 | 13 | else 8 |
14 | 14 | ||
15 | ; A block is an expression, and if raise the scope level regardless if a block | 15 | ; A block is an expression, and if raise the scope level regardless if a block |
16 | ; is used or not. | 16 | ; is used or not. |
17 | if (true != false) { | 17 | if true != false { |
18 | let a = "yo" | 18 | let a = "yo" |
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. They should generally convert to a lookup table. | 22 | ; isn't allowed. They should generally convert to a lookup table. |
23 | match (a) { | 23 | match a { |
24 | case 8 = "hello" | 24 | case 8 = "hello" |
25 | case 9 = "world" | 25 | case 9 = "world" |
26 | else = "what" | 26 | else = "what" |
@@ -42,7 +42,7 @@ let f:flags = flags.node_add | |||
42 | ; case node_add = "subbing something..." | 42 | ; case node_add = "subbing something..." |
43 | ; else = "default case" | 43 | ; else = "default case" |
44 | ; } | 44 | ; } |
45 | match (a) { | 45 | match a { |
46 | case flags.node_add = "adding something..." | 46 | case flags.node_add = "adding something..." |
47 | case flags.node_add = "subbing something..." | 47 | case flags.node_add = "subbing something..." |
48 | else = "default case" | 48 | else = "default case" |
diff --git a/tests/loops.bad b/tests/loops.bad index d42716f..a068ed0 100644 --- a/tests/loops.bad +++ b/tests/loops.bad | |||
@@ -1,18 +1,18 @@ | |||
1 | ; The most basic loop is the while loop. | 1 | ; The most basic loop is the while loop. |
2 | while (true) "heelo" | 2 | while true "heelo" |
3 | 3 | ||
4 | while (1 + 2 == 3) { | 4 | while 1 + 2 == 3 { |
5 | "hello" | 5 | "hello" |
6 | } | 6 | } |
7 | 7 | ||
8 | let a = true | 8 | let a = true |
9 | while (a) { | 9 | while a { |
10 | "hello" | 10 | "hello" |
11 | } | 11 | } |
12 | 12 | ||
13 | ; We could use some sugar for this. | 13 | ; We could use some sugar for this. |
14 | let i = 0 | 14 | let i = 0 |
15 | while (i < 10) { | 15 | while i < 10 { |
16 | "hello" | 16 | "hello" |
17 | ; --i | 17 | ; --i |
18 | } | 18 | } |