diff options
author | Li Jin <dragon-fly@qq.com> | 2024-05-31 11:44:23 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-05-31 11:44:23 +0800 |
commit | f8cd1220147d606b7e96f88c12fd0f163fb4e1c5 (patch) | |
tree | ed7bd7cfe778a9a7d55c53cfca3ec6c6f869bdde | |
parent | 1890294226034d27165af016c8c93a484d978b49 (diff) | |
download | yuescript-f8cd1220147d606b7e96f88c12fd0f163fb4e1c5.tar.gz yuescript-f8cd1220147d606b7e96f88c12fd0f163fb4e1c5.tar.bz2 yuescript-f8cd1220147d606b7e96f88c12fd0f163fb4e1c5.zip |
fix spread exp list in up-value function issue.v0.23.8
-rw-r--r-- | spec/outputs/upvalue_func.lua | 4 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 51 |
2 files changed, 37 insertions, 18 deletions
diff --git a/spec/outputs/upvalue_func.lua b/spec/outputs/upvalue_func.lua index e66eefa..3181adf 100644 --- a/spec/outputs/upvalue_func.lua +++ b/spec/outputs/upvalue_func.lua | |||
@@ -264,7 +264,7 @@ local _anon_func_8 = function(itemB, listB) | |||
264 | end | 264 | end |
265 | return false | 265 | return false |
266 | end | 266 | end |
267 | local _anon_func_9 = function(listA, listB, pairs) | 267 | local _anon_func_9 = function(listA, listB) |
268 | local _tab_0 = { } | 268 | local _tab_0 = { } |
269 | local _idx_0 = #_tab_0 + 1 | 269 | local _idx_0 = #_tab_0 + 1 |
270 | for _index_0 = 1, #listA do | 270 | for _index_0 = 1, #listA do |
@@ -403,7 +403,7 @@ GameEngine:onUpdate(function(deltaTime) | |||
403 | if (itemB ~= nil) and _anon_func_8(itemB, listB) then | 403 | if (itemB ~= nil) and _anon_func_8(itemB, listB) then |
404 | print("item in list") | 404 | print("item in list") |
405 | end | 405 | end |
406 | func(_anon_func_9(listA, listB, pairs)) | 406 | func(_anon_func_9(listA, listB)) |
407 | func(_anon_func_10(), _anon_func_11(pairs, tb)) | 407 | func(_anon_func_10(), _anon_func_11(pairs, tb)) |
408 | func(_anon_func_12()) | 408 | func(_anon_func_12()) |
409 | func(_anon_func_13(pairs, tb)) | 409 | func(_anon_func_13(pairs, tb)) |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 04874d5..7b34d0e 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
75 | "close"s // Lua 5.4 | 75 | "close"s // Lua 5.4 |
76 | }; | 76 | }; |
77 | 77 | ||
78 | const std::string_view version = "0.23.7"sv; | 78 | const std::string_view version = "0.23.8"sv; |
79 | const std::string_view extension = "yue"sv; | 79 | const std::string_view extension = "yue"sv; |
80 | 80 | ||
81 | class CompileError : public std::logic_error { | 81 | class CompileError : public std::logic_error { |
@@ -2331,7 +2331,7 @@ private: | |||
2331 | if (hasSpreadExp(tableLit->values.objects())) { | 2331 | if (hasSpreadExp(tableLit->values.objects())) { |
2332 | auto expList = assignment->expList.get(); | 2332 | auto expList = assignment->expList.get(); |
2333 | std::string preDefine = getPreDefineLine(assignment); | 2333 | std::string preDefine = getPreDefineLine(assignment); |
2334 | transformSpreadTable(tableLit->values.objects(), out, ExpUsage::Assignment, expList); | 2334 | transformSpreadTable(tableLit->values.objects(), out, ExpUsage::Assignment, expList, false); |
2335 | out.back().insert(0, preDefine); | 2335 | out.back().insert(0, preDefine); |
2336 | return; | 2336 | return; |
2337 | } | 2337 | } |
@@ -2342,7 +2342,7 @@ private: | |||
2342 | if (hasSpreadExp(tableBlock->values.objects())) { | 2342 | if (hasSpreadExp(tableBlock->values.objects())) { |
2343 | auto expList = assignment->expList.get(); | 2343 | auto expList = assignment->expList.get(); |
2344 | std::string preDefine = getPreDefineLine(assignment); | 2344 | std::string preDefine = getPreDefineLine(assignment); |
2345 | transformSpreadTable(tableBlock->values.objects(), out, ExpUsage::Assignment, expList); | 2345 | transformSpreadTable(tableBlock->values.objects(), out, ExpUsage::Assignment, expList, false); |
2346 | out.back().insert(0, preDefine); | 2346 | out.back().insert(0, preDefine); |
2347 | return; | 2347 | return; |
2348 | } | 2348 | } |
@@ -5197,7 +5197,7 @@ private: | |||
5197 | case id<TableLit_t>(): { | 5197 | case id<TableLit_t>(): { |
5198 | auto tableLit = static_cast<TableLit_t*>(value); | 5198 | auto tableLit = static_cast<TableLit_t*>(value); |
5199 | if (hasSpreadExp(tableLit->values.objects())) { | 5199 | if (hasSpreadExp(tableLit->values.objects())) { |
5200 | transformSpreadTable(tableLit->values.objects(), out, ExpUsage::Return); | 5200 | transformSpreadTable(tableLit->values.objects(), out, ExpUsage::Return, nullptr, false); |
5201 | return; | 5201 | return; |
5202 | } | 5202 | } |
5203 | } | 5203 | } |
@@ -5219,7 +5219,7 @@ private: | |||
5219 | } else if (auto tableBlock = returnNode->valueList.as<TableBlock_t>()) { | 5219 | } else if (auto tableBlock = returnNode->valueList.as<TableBlock_t>()) { |
5220 | const auto& values = tableBlock->values.objects(); | 5220 | const auto& values = tableBlock->values.objects(); |
5221 | if (hasSpreadExp(values)) { | 5221 | if (hasSpreadExp(values)) { |
5222 | transformSpreadTable(values, out, ExpUsage::Return); | 5222 | transformSpreadTable(values, out, ExpUsage::Return, nullptr, false); |
5223 | } else { | 5223 | } else { |
5224 | transformTable(values, out); | 5224 | transformTable(values, out); |
5225 | out.back() = indent() + "return "s + out.back() + nlr(returnNode); | 5225 | out.back() = indent() + "return "s + out.back() + nlr(returnNode); |
@@ -6900,17 +6900,25 @@ private: | |||
6900 | return false; | 6900 | return false; |
6901 | } | 6901 | } |
6902 | 6902 | ||
6903 | void transformSpreadTable(const node_container& values, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { | 6903 | void transformSpreadTable(const node_container& values, str_list& out, ExpUsage usage, ExpList_t* assignList, bool isListTable) { |
6904 | auto x = values.front(); | 6904 | auto x = values.front(); |
6905 | bool extraScope = false; | 6905 | bool extraScope = false; |
6906 | switch (usage) { | 6906 | switch (usage) { |
6907 | case ExpUsage::Closure: { | 6907 | case ExpUsage::Closure: { |
6908 | auto tableLit = x->new_ptr<TableLit_t>(); | ||
6909 | for (ast_node* value : values) { | ||
6910 | tableLit->values.push_back(value); | ||
6911 | } | ||
6912 | auto simpleValue = x->new_ptr<SimpleValue_t>(); | 6908 | auto simpleValue = x->new_ptr<SimpleValue_t>(); |
6913 | simpleValue->value.set(tableLit); | 6909 | if (isListTable) { |
6910 | auto comp = x->new_ptr<Comprehension_t>(); | ||
6911 | for (ast_node* value : values) { | ||
6912 | comp->items.push_back(value); | ||
6913 | } | ||
6914 | simpleValue->value.set(comp); | ||
6915 | } else { | ||
6916 | auto tableLit = x->new_ptr<TableLit_t>(); | ||
6917 | for (ast_node* value : values) { | ||
6918 | tableLit->values.push_back(value); | ||
6919 | } | ||
6920 | simpleValue->value.set(tableLit); | ||
6921 | } | ||
6914 | if (transformAsUpValueFunc(newExp(simpleValue, x), out)) { | 6922 | if (transformAsUpValueFunc(newExp(simpleValue, x), out)) { |
6915 | return; | 6923 | return; |
6916 | } | 6924 | } |
@@ -7353,7 +7361,16 @@ private: | |||
7353 | void transformTableLit(TableLit_t* table, str_list& out) { | 7361 | void transformTableLit(TableLit_t* table, str_list& out) { |
7354 | const auto& values = table->values.objects(); | 7362 | const auto& values = table->values.objects(); |
7355 | if (hasSpreadExp(values)) { | 7363 | if (hasSpreadExp(values)) { |
7356 | transformSpreadTable(values, out, ExpUsage::Closure); | 7364 | transformSpreadTable(values, out, ExpUsage::Closure, nullptr, false); |
7365 | } else { | ||
7366 | transformTable(values, out); | ||
7367 | } | ||
7368 | } | ||
7369 | |||
7370 | void transformListTable(Comprehension_t* comp, str_list& out) { | ||
7371 | const auto& values = comp->items.objects(); | ||
7372 | if (hasSpreadExp(values)) { | ||
7373 | transformSpreadTable(values, out, ExpUsage::Closure, nullptr, true); | ||
7357 | } else { | 7374 | } else { |
7358 | transformTable(values, out); | 7375 | transformTable(values, out); |
7359 | } | 7376 | } |
@@ -7403,10 +7420,10 @@ private: | |||
7403 | void transformComprehension(Comprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { | 7420 | void transformComprehension(Comprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { |
7404 | auto x = comp; | 7421 | auto x = comp; |
7405 | if (comp->items.size() != 2 || !ast_is<CompInner_t>(comp->items.back())) { | 7422 | if (comp->items.size() != 2 || !ast_is<CompInner_t>(comp->items.back())) { |
7406 | auto tableLit = x->new_ptr<TableLit_t>(); | ||
7407 | tableLit->values.dup(comp->items); | ||
7408 | switch (usage) { | 7423 | switch (usage) { |
7409 | case ExpUsage::Assignment: { | 7424 | case ExpUsage::Assignment: { |
7425 | auto tableLit = x->new_ptr<TableLit_t>(); | ||
7426 | tableLit->values.dup(comp->items); | ||
7410 | auto simpleValue = x->new_ptr<SimpleValue_t>(); | 7427 | auto simpleValue = x->new_ptr<SimpleValue_t>(); |
7411 | simpleValue->value.set(tableLit); | 7428 | simpleValue->value.set(tableLit); |
7412 | auto exp = newExp(simpleValue, x); | 7429 | auto exp = newExp(simpleValue, x); |
@@ -7419,6 +7436,8 @@ private: | |||
7419 | break; | 7436 | break; |
7420 | } | 7437 | } |
7421 | case ExpUsage::Return: { | 7438 | case ExpUsage::Return: { |
7439 | auto tableLit = x->new_ptr<TableLit_t>(); | ||
7440 | tableLit->values.dup(comp->items); | ||
7422 | auto simpleValue = x->new_ptr<SimpleValue_t>(); | 7441 | auto simpleValue = x->new_ptr<SimpleValue_t>(); |
7423 | simpleValue->value.set(tableLit); | 7442 | simpleValue->value.set(tableLit); |
7424 | auto exp = newExp(simpleValue, x); | 7443 | auto exp = newExp(simpleValue, x); |
@@ -7430,7 +7449,7 @@ private: | |||
7430 | break; | 7449 | break; |
7431 | } | 7450 | } |
7432 | case ExpUsage::Closure: | 7451 | case ExpUsage::Closure: |
7433 | transformTableLit(tableLit, out); | 7452 | transformListTable(comp, out); |
7434 | break; | 7453 | break; |
7435 | default: | 7454 | default: |
7436 | YUEE("invalid comprehension usage", comp); | 7455 | YUEE("invalid comprehension usage", comp); |
@@ -9336,7 +9355,7 @@ private: | |||
9336 | void transformTableBlock(TableBlock_t* table, str_list& out) { | 9355 | void transformTableBlock(TableBlock_t* table, str_list& out) { |
9337 | const auto& values = table->values.objects(); | 9356 | const auto& values = table->values.objects(); |
9338 | if (hasSpreadExp(values)) { | 9357 | if (hasSpreadExp(values)) { |
9339 | transformSpreadTable(values, out, ExpUsage::Closure); | 9358 | transformSpreadTable(values, out, ExpUsage::Closure, nullptr, false); |
9340 | } else { | 9359 | } else { |
9341 | transformTable(values, out); | 9360 | transformTable(values, out); |
9342 | } | 9361 | } |