Skip to content

Commit 8a335ae

Browse files
feat(compiler): Allow arrays to do binary operation assignment (#1928)
Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
1 parent f65002d commit 8a335ae

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

compiler/src/parsing/parser.messages

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5376,6 +5376,15 @@ program: MODULE UIDENT EOL LIDENT INFIX_ASSIGNMENT_10 WHEN
53765376
## The known suffix of the stack is as follows:
53775377
## id_expr assign_binop_op
53785378
##
5379+
program: MODULE UIDENT EOL UIDENT LBRACK UIDENT RBRACK INFIX_ASSIGNMENT_10 YIELD
5380+
##
5381+
## Ends in an error in state: 478.
5382+
##
5383+
## array_set -> left_accessor_expr lbrack expr rbrack assign_binop_op . expr [ THICKARROW STAR SLASH SEMI RPAREN RCARET RBRACK RBRACE PIPE LCARET INFIX_90 INFIX_80 INFIX_70 INFIX_60 INFIX_50 INFIX_40 INFIX_30 INFIX_120 INFIX_110 INFIX_100 EOL EOF ELSE DASH COMMA COLON AND ]
5384+
##
5385+
## The known suffix of the stack is as follows:
5386+
## left_accessor_expr lbrack expr rbrack assign_binop_op
5387+
##
53795388
program: MODULE UIDENT EOL LIDENT EQUAL WHEN
53805389
##
53815390
## Ends in an error in state: 224.

compiler/src/parsing/parser.mly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ array_get:
634634

635635
array_set:
636636
| left_accessor_expr lbrack expr rbrack equal expr { Expression.array_set ~loc:(to_loc $loc) $1 $3 $6 }
637+
| left_accessor_expr lbrack expr rbrack assign_binop_op expr { Expression.array_set ~loc:(to_loc $loc) $1 $3 (Expression.apply ~loc:(to_loc $loc) (mkid_expr $loc($5) [$5]) [{paa_label=Unlabeled; paa_expr=Expression.array_get ~loc:(to_loc $loc) $1 $3; paa_loc=(to_loc $loc($6))}; {paa_label=Unlabeled; paa_expr=$6; paa_loc=(to_loc $loc($6))}]) }
637638

638639
record_get:
639640
| left_accessor_expr dot lid { Expression.record_get ~loc:(to_loc $loc) $1 $3 }

compiler/test/suites/arrays.re

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe("arrays", ({test, testSkip}) => {
7171
"let x = [> 1, 2, 3]; x[-2] = 4; print(x)",
7272
"[> 1, 4, 3]\n",
7373
);
74+
assertRun(
75+
"array_set3",
76+
"let x = [> 1, 2, 3]; x[0] += 1; print(x)",
77+
"[> 2, 2, 3]\n",
78+
);
7479
assertCompileError(
7580
"array_set_err",
7681
"let x = [> 1, 2, 3]; x[-2] = false",

0 commit comments

Comments
 (0)