From 1e745807e447c4e9ac101558ed33c6ed19698099 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sun, 18 Jun 2023 20:21:08 +0200 Subject: [PATCH] Berry fixed parser error with upvals in closures (#18902) --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_parser.c | 3 ++- lib/libesp32/berry/tests/closure.be | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0c9b7641..9b0f33915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file. - Fixed HASPmota event when value is non-integer (fixes #18229) - Matter fix local Occupancy sensor - Zigbee fixed regression with SetOption101 +- Berry fixed parser error with upvals in closures ### Removed diff --git a/lib/libesp32/berry/src/be_parser.c b/lib/libesp32/berry/src/be_parser.c index 5e58df6cc..107ae5522 100644 --- a/lib/libesp32/berry/src/be_parser.c +++ b/lib/libesp32/berry/src/be_parser.c @@ -964,9 +964,10 @@ static void suffix_alloc_reg(bparser *parser, bexpdesc *l) bbool is_suffix = l->type == ETINDEX || l->type == ETMEMBER; /* is suffix */ bbool is_suffix_reg = l->v.ss.tt == ETREG || l->v.ss.tt == ETLOCAL || l->v.ss.tt == ETGLOBAL || l->v.ss.tt == ETNGLOBAL; /* if suffix, does it need a register */ bbool is_global = l->type == ETGLOBAL || l->type == ETNGLOBAL; + bbool is_upval = l->type == ETUPVAL; /* in the suffix expression, if the object is a temporary * variable (l->v.ss.tt == ETREG), it needs to be cached. */ - if (is_global || (is_suffix && is_suffix_reg)) { + if (is_global || is_upval || (is_suffix && is_suffix_reg)) { be_code_allocregs(finfo, 1); } } diff --git a/lib/libesp32/berry/tests/closure.be b/lib/libesp32/berry/tests/closure.be index 2474fede9..757c2a944 100644 --- a/lib/libesp32/berry/tests/closure.be +++ b/lib/libesp32/berry/tests/closure.be @@ -11,3 +11,6 @@ tick() assert(l[0]() == [1, 100]) assert(l[1]() == [2, 100]) assert(l[2]() == [3, 100]) + +# the following failed to compile #344 +def test() var nv = 1 var f = def() nv += 2*1 print(nv) end end