aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2023-11-02 12:27:27 +0800
committerLi Jin <dragon-fly@qq.com>2023-11-02 12:27:27 +0800
commit5ad0f4daa8171460dc71332669c365bb8e07dab0 (patch)
treeda57ecdabf5fa54c324c023ac22c53041aaff143
parent2263cca3687bb5c6b9c51f42d59d2249c18941a0 (diff)
downloadyuescript-0.20.4.tar.gz
yuescript-0.20.4.tar.bz2
yuescript-0.20.4.zip
fix table matching syntax in switch statement with list table.v0.20.4
-rw-r--r--spec/inputs/switch.yue6
-rw-r--r--spec/outputs/switch.lua35
-rw-r--r--src/yuescript/yue_compiler.cpp13
3 files changed, 51 insertions, 3 deletions
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue
index cb1eb31..49d47f3 100644
--- a/spec/inputs/switch.yue
+++ b/spec/inputs/switch.yue
@@ -159,5 +159,11 @@ do
159 when {x: <>: mt} 159 when {x: <>: mt}
160 print mt 160 print mt
161 161
162do
163 switch tb
164 when [ [item,],]
165 print item
166 when [a = 1, b = "abc"]
167 print a, b
162nil 168nil
163 169
diff --git a/spec/outputs/switch.lua b/spec/outputs/switch.lua
index 1ace1e3..bbdf794 100644
--- a/spec/outputs/switch.lua
+++ b/spec/outputs/switch.lua
@@ -398,4 +398,39 @@ do
398 end 398 end
399 end 399 end
400end 400end
401do
402 do
403 local _exp_0 = tb
404 local _type_0 = type(_exp_0)
405 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
406 local _match_0 = false
407 if _tab_0 then
408 local item
409 do
410 local _obj_0 = _exp_0[1]
411 local _type_1 = type(_obj_0)
412 if "table" == _type_1 or "userdata" == _type_1 then
413 item = _obj_0[1]
414 end
415 end
416 if item ~= nil then
417 _match_0 = true
418 print(item)
419 end
420 end
421 if not _match_0 then
422 if _tab_0 then
423 local a = _exp_0[1]
424 local b = _exp_0[2]
425 if a == nil then
426 a = 1
427 end
428 if b == nil then
429 b = "abc"
430 end
431 print(a, b)
432 end
433 end
434 end
435end
401return nil 436return nil
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 648dab2..1733967 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
78const std::string_view version = "0.20.3"sv; 78const std::string_view version = "0.20.4"sv;
79const std::string_view extension = "yue"sv; 79const std::string_view extension = "yue"sv;
80 80
81class CompileError : public std::logic_error { 81class CompileError : public std::logic_error {
@@ -9155,8 +9155,15 @@ private:
9155 for (auto branch_ : branches) { 9155 for (auto branch_ : branches) {
9156 auto branch = static_cast<SwitchCase_t*>(branch_); 9156 auto branch = static_cast<SwitchCase_t*>(branch_);
9157 auto valueList = static_cast<SwitchList_t*>(branch->condition.get()); 9157 auto valueList = static_cast<SwitchList_t*>(branch->condition.get());
9158 if (auto value = singleValueFrom(valueList); 9158 bool tableMatching = false;
9159 value && (value->item.is<SimpleTable_t>() || value->get_by_path<SimpleValue_t, TableLit_t>())) { 9159 if (auto value = singleValueFrom(valueList)) {
9160 if (value->item.is<SimpleTable_t>()) {
9161 tableMatching = true;
9162 } else if (auto sVal = value->item.as<SimpleValue_t>()){
9163 tableMatching = ast_is<TableLit_t, Comprehension_t>(sVal->value);
9164 }
9165 }
9166 if (tableMatching) {
9160 if (!firstBranch) { 9167 if (!firstBranch) {
9161 temp.push_back(indent() + "else"s + nll(branch)); 9168 temp.push_back(indent() + "else"s + nll(branch));
9162 pushScope(); 9169 pushScope();