Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): Allow arrays to do binary operation assignment #1928

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/src/parsing/parser.messages
Original file line number Diff line number Diff line change
Expand Up @@ -5376,6 +5376,15 @@ program: MODULE UIDENT EOL LIDENT INFIX_ASSIGNMENT_10 WHEN
## The known suffix of the stack is as follows:
## id_expr assign_binop_op
##
program: MODULE UIDENT EOL UIDENT LBRACK UIDENT RBRACK INFIX_ASSIGNMENT_10 YIELD
##
## Ends in an error in state: 478.
##
## 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 ]
##
## The known suffix of the stack is as follows:
## left_accessor_expr lbrack expr rbrack assign_binop_op
##
program: MODULE UIDENT EOL LIDENT EQUAL WHEN
##
## Ends in an error in state: 224.
Expand Down
1 change: 1 addition & 0 deletions compiler/src/parsing/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ array_get:

array_set:
| left_accessor_expr lbrack expr rbrack equal expr { Expression.array_set ~loc:(to_loc $loc) $1 $3 $6 }
| 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))}]) }

record_get:
| left_accessor_expr dot lid { Expression.record_get ~loc:(to_loc $loc) $1 $3 }
Expand Down
5 changes: 5 additions & 0 deletions compiler/test/suites/arrays.re
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ describe("arrays", ({test, testSkip}) => {
"let x = [> 1, 2, 3]; x[-2] = 4; print(x)",
"[> 1, 4, 3]\n",
);
assertRun(
"array_set3",
"let x = [> 1, 2, 3]; x[0] += 1; print(x)",
"[> 2, 2, 3]\n",
);
assertCompileError(
"array_set_err",
"let x = [> 1, 2, 3]; x[-2] = false",
Expand Down
Loading