Skip to content

Commit

Permalink
Berry var allowed in with walrus operator := (#19018)
Browse files Browse the repository at this point in the history
* Berry `var` allowed in with walrus operator `:=`

* fix regression
  • Loading branch information
s-hadinger authored Jul 3, 2023
1 parent fc9065d commit 8f06552
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Matter add option to disable bridge mode (#18992)
- Support for SGP41 TVOC/NOx Sensor (#18880)
- Command ``BrRestart`` to restart the Berry VM (experimental)
- Berry `var` allowed in with walrus operator `:=`

### Breaking Changed
- Berry `bool( [] )` and `bool( {} )` now evaluate as `false` (#18986)
Expand Down
Binary file modified lib/libesp32/berry/berry.exe
Binary file not shown.
19 changes: 16 additions & 3 deletions lib/libesp32/berry/src/be_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,8 +1135,22 @@ static void sub_expr(bparser *parser, bexpdesc *e, int prio)
static void walrus_expr(bparser *parser, bexpdesc *e)
{
int line = parser->lexer.linenumber;
sub_expr(parser, e, ASSIGN_OP_PRIO); /* left expression */
btokentype op = next_type(parser);
if (op == KeyVar) {
/* 'var' ID ':=' expr */
scan_next_token(parser); /* skip 'var' */
bstring *name;
name = next_token(parser).u.s;
match_token(parser, TokenId); /* match and skip ID */
new_var(parser, name, e); /* new variable */
op = next_type(parser);
if (op != OptWalrus) {
parser_error(parser, "'var' in expr must be followed by ':='");
}
} else {
sub_expr(parser, e, ASSIGN_OP_PRIO); /* left expression */
op = next_type(parser);
}
if (op == OptWalrus) {
check_symbol(parser, e);
bexpdesc e1 = *e; /* copy var to e1, e will get the result of expression */
Expand All @@ -1149,8 +1163,7 @@ static void walrus_expr(bparser *parser, bexpdesc *e)
}
if (be_code_setvar(parser->finfo, &e1, e, btrue /* do not release register */ )) {
parser->lexer.linenumber = line;
parser_error(parser,
"try to assign constant expressions.");
parser_error(parser, "try to assign constant expressions.");
}
}
}
Expand Down

0 comments on commit 8f06552

Please sign in to comment.