From 07a64933f8d956a8bba401db2200e6f7d8244dc9 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 9 Feb 2022 10:14:25 +0800 Subject: fix the way update assignment is implemented. --- src/yuescript/yue_compiler.cpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 17822c6..852a0f4 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -2015,9 +2015,42 @@ private: auto leftValue = singleValueFrom(leftExp); if (!leftValue) throw std::logic_error(_info.errorMessage("left hand expression is not assignable"sv, leftExp)); if (auto chain = leftValue->item.as()) { - if (specialChainValue(chain) == ChainType::Metatable) { + auto dot = ast_cast(chain->items.back()); + if (dot && dot->name.is()) { 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(chain->items.front())) { + ast_node* var = callable->item.as(); + if (auto self = callable->item.as()) { + var = self->name.as(); + } + BREAK_IF(var); + } + } + auto tmpChain = x->new_ptr(); + ast_ptr ptr(chain->items.back()); + for (auto item : chain->items.objects()) { + if (item != ptr) { + tmpChain->items.push_back(item); + } + } + auto value = x->new_ptr(); + value->item.set(tmpChain); + auto exp = newExp(value, x); + auto objVar = getUnusedName("_obj_"sv); + auto newAssignment = x->new_ptr(); + newAssignment->expList.set(toAst(objVar, x)); + auto assign = x->new_ptr(); + assign->values.push_back(exp); + newAssignment->action.set(assign); + transformAssignment(newAssignment, temp); + chain->items.clear(); + chain->items.push_back(toAst(objVar, x)); + chain->items.push_back(ptr); + BLOCK_END auto tmpChain = x->new_ptr(); for (auto item : chain->items.objects()) { bool itemAdded = false; -- cgit v1.2.3-55-g6feb