From 2dc2aa85990c3e334765138e76e11b94d9ed73d1 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 14 Nov 2025 23:31:36 +0800 Subject: Fixed code generating. --- src/yuescript/yue_compiler.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 8c28e61..d6db3fc 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -8317,9 +8317,12 @@ private: auto compInner = static_cast(comp->items.back()); str_list temp; std::string accumVar = getUnusedName("_accum_"sv); - std::string lenVar = getUnusedName("_len_"sv); addToScope(accumVar); - addToScope(lenVar); + std::string lenVar; + if (!isSpread) { + lenVar = getUnusedName("_len_"sv); + addToScope(lenVar); + } for (auto item : compInner->items.objects()) { switch (item->get_id()) { case id(): @@ -8337,7 +8340,7 @@ private: } } { - auto assignLeft = toAst(accumVar + '[' + lenVar + (isSpread ? "][]"s : "]"s), x); + auto assignLeft = toAst(accumVar + '[' + (isSpread ? "]"s : lenVar + ']'), x); auto assign = x->new_ptr(); assign->values.push_back(value); auto assignment = x->new_ptr(); @@ -8351,10 +8354,15 @@ private: popScope(); } _buf << indent() << "local "sv << accumVar << " = { }"sv << nll(comp); - _buf << indent() << "local "sv << lenVar << " = 1"sv << nll(comp); - _buf << join(temp); - _buf << assignStr; - _buf << indent(int(temp.size())) << lenVar << " = "sv << lenVar << " + 1"sv << nll(comp); + if (isSpread) { + _buf << join(temp); + _buf << assignStr; + } else { + _buf << indent() << "local "sv << lenVar << " = 1"sv << nll(comp); + _buf << join(temp); + _buf << assignStr; + _buf << indent(int(temp.size())) << lenVar << " = "sv << lenVar << " + 1"sv << nll(comp); + } for (int ind = int(temp.size()) - 1; ind > -1; --ind) { _buf << indent(ind) << "end"sv << nll(comp); } -- cgit v1.2.3-55-g6feb