Skip to content

Commit 6fd1603

Browse files
authored
feat(compiler): Allow non-block bodies with loops (#2064)
1 parent 5d601c0 commit 6fd1603

File tree

8 files changed

+458
-176
lines changed

8 files changed

+458
-176
lines changed

compiler/src/parsing/parser.messages

Lines changed: 442 additions & 171 deletions
Large diffs are not rendered by default.

compiler/src/parsing/parser.mly

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,6 @@ braced_expr:
532532
| lbrace block_body rbrace { Expression.block ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $2 }
533533
| lbrace record_exprs rbrace { Expression.record_fields ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $2 }
534534

535-
block:
536-
| lbrace block_body rbrace { Expression.block ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $2 }
537-
538535
arg_default:
539536
| EQUAL non_stmt_expr { $2 }
540537

@@ -573,14 +570,14 @@ if_expr:
573570
| IF lparen expr rparen opt_eols expr ioption(else_expr) %prec _if { Expression.if_ ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $3 $6 $7 }
574571

575572
while_expr:
576-
| WHILE lparen expr rparen block { Expression.while_ ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $3 $5 }
573+
| WHILE lparen expr rparen opt_eols expr { Expression.while_ ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $3 $6 }
577574

578575
for_inner_expr:
579576
| %prec EOL { None }
580577
| expr { Some $1 }
581578

582579
for_expr:
583-
| FOR lparen block_body_expr? opt_eols SEMI opt_eols for_inner_expr opt_eols SEMI opt_eols for_inner_expr rparen block { Expression.for_ ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $3 $7 $11 $13 }
580+
| FOR lparen block_body_expr? opt_eols SEMI opt_eols for_inner_expr opt_eols SEMI opt_eols for_inner_expr rparen opt_eols expr { Expression.for_ ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $3 $7 $11 $14 }
584581

585582
when_guard:
586583
| opt_eols WHEN expr { $3 }

compiler/test/grainfmt/comments.expected.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ while (true /* true */) {
490490
while (true /* true */) {
491491
void
492492
}
493+
while (true /* true */) void
493494

494495
for (/* for */
495496
/* init */
@@ -531,6 +532,7 @@ for (/* for */
531532
for (;; /* for */ /* init */ /*cond*/ /*inc*/ /* more */ /* and more */) {
532533
void
533534
}
535+
for (;; /* for */ /* init */ /*cond*/ /*inc*/ /* more */ /* and more */) void
534536

535537
match (/* match */ /*foo*/ foo /* post foo */ /* post foo 2 */) { /* mr branch */
536538
_ => /* void */ void, /* post void */

compiler/test/grainfmt/comments.input.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,14 @@ while /* true */ (true) {void}
406406
while (/* true */ true) {void}
407407
while (true /* true */) {void}
408408
while (true) /* true */ {void}
409+
while (true) /* true */ void
409410

410411
for /* for */ (/* init */ let mut i = 1; /*cond*/i < 1; /*inc*/ i += 1 /* more */ )/* and more */ {void}
411412
for /* for */ (/* init */ ; /*cond*/i < 1; /*inc*/ i += 1 /* more */ )/* and more */ {void}
412413
for /* for */ (/* init */ let mut i = 1; /*cond*/; /*inc*/ i += 1 /* more */ )/* and more */ {void}
413414
for /* for */ (/* init */ let mut i = 1; /*cond*/i < 1; /*inc*/ /* more */ )/* and more */ {void}
414415
for /* for */ (/* init */ ; /*cond*/; /*inc*/ /* more */ )/* and more */ {void}
416+
for /* for */ (/* init */ ; /*cond*/; /*inc*/ /* more */ )/* and more */ void
415417

416418
match /* match */ (/*foo*/ foo /* post foo */) /* post foo 2 */ {/* mr branch */_=> /* void */void/* post void */}
417419

compiler/test/grainfmt/for_loops.expected.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ for (
1616
let strLength = 0
1717
print("Loop")
1818
}
19+
20+
for (let mut i = 0; i < size; i += 1) print("Loop")

compiler/test/grainfmt/for_loops.input.gr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ let verylongvariablenameforcingalinebreakhereplease = 2
1212
let strLength = 0
1313
print("Loop")
1414
}
15+
16+
for (let mut i = 0; i < size; i += 1)
17+
print("Loop")

compiler/test/grainfmt/while_loops.expected.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ let size = 10
66
while (i < size) {
77
i += 1
88
}
9+
10+
while (i < size) i += 1

compiler/test/grainfmt/while_loops.input.gr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ module WhileLoops
66
while (i < size) {
77
i = i + 1
88
}
9+
10+
while (i < size)
11+
i = i + 1

0 commit comments

Comments
 (0)