From e549ca891fc42b78d97df3c46fb83f28bb74e01b Mon Sep 17 00:00:00 2001 From: Li Jin <dragon-fly@qq.com> Date: Fri, 11 Feb 2022 11:14:00 +0800 Subject: add a missed local variable check. --- spec/outputs/syntax.lua | 11 ++++++----- src/yuescript/yue_compiler.cpp | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 4596a29..457f1e1 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -195,9 +195,10 @@ do local _obj_2 = _with_0.a _obj_2.c = _obj_2.c + 1 end -tb[#tb + 1] = 10 -local _obj_2 = a.b.c -_obj_2[#_obj_2 + 1] = 1 +local _obj_2 = tb +_obj_2[#_obj_2 + 1] = 10 +local _obj_3 = a.b.c +_obj_3[#_obj_3 + 1] = 1 if v then x[#x + 1] = 1 else @@ -205,12 +206,12 @@ else end do local _with_0 = tb - local _obj_3 = _with_0.b.c + local _obj_4 = _with_0.b.c do local _with_1 = vec _with_1.x = 1 _with_1.y = 2 - _obj_3[#_obj_3 + 1] = _with_1 + _obj_4[#_obj_4 + 1] = _with_1 end end x = 0 diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 33d4dcb..6617ec5 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -2023,17 +2023,9 @@ private: throw std::logic_error(_info.errorMessage("can not apply update to a metatable"sv, leftExp)); } BLOCK_START - BREAK_IF(chain->items.size() < 2); - if (chain->items.size() == 2) { - if (auto callable = ast_cast<Callable_t>(chain->items.front())) { - ast_node* var = callable->item.as<Variable_t>(); - if (auto self = callable->item.as<SelfName_t>()) { - var = self->name.as<self_t>(); - } - BREAK_IF(var); - } - } if (op == "#"sv) { + auto varName = singleVariableFrom(chain); + BREAK_IF(!varName.empty() && isLocal(varName)); auto objVar = getUnusedName("_obj_"sv); auto newAssignment = x->new_ptr<ExpListAssign_t>(); newAssignment->expList.set(toAst<ExpList_t>(objVar, x)); @@ -2044,6 +2036,16 @@ private: chain->items.clear(); chain->items.push_back(toAst<Callable_t>(objVar, x)); } else { + BREAK_IF(chain->items.size() < 2); + if (chain->items.size() == 2) { + if (auto callable = ast_cast<Callable_t>(chain->items.front())) { + ast_node* var = callable->item.as<Variable_t>(); + if (auto self = callable->item.as<SelfName_t>()) { + var = self->name.as<self_t>(); + } + BREAK_IF(var && isLocal(_parser.toString(var))); + } + } auto tmpChain = x->new_ptr<ChainValue_t>(); ast_ptr<false, ast_node> ptr(chain->items.back()); for (auto item : chain->items.objects()) { -- cgit v1.2.3-55-g6feb