From 5ad0f4daa8171460dc71332669c365bb8e07dab0 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 2 Nov 2023 12:27:27 +0800 Subject: fix table matching syntax in switch statement with list table. --- spec/inputs/switch.yue | 6 ++++++ spec/outputs/switch.lua | 35 +++++++++++++++++++++++++++++++++++ src/yuescript/yue_compiler.cpp | 13 ++++++++++--- 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 when {x: <>: mt} print mt +do + switch tb + when [ [item,],] + print item + when [a = 1, b = "abc"] + print a, b nil 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 end end end +do + do + local _exp_0 = tb + local _type_0 = type(_exp_0) + local _tab_0 = "table" == _type_0 or "userdata" == _type_0 + local _match_0 = false + if _tab_0 then + local item + do + local _obj_0 = _exp_0[1] + local _type_1 = type(_obj_0) + if "table" == _type_1 or "userdata" == _type_1 then + item = _obj_0[1] + end + end + if item ~= nil then + _match_0 = true + print(item) + end + end + if not _match_0 then + if _tab_0 then + local a = _exp_0[1] + local b = _exp_0[2] + if a == nil then + a = 1 + end + if b == nil then + b = "abc" + end + print(a, b) + end + end + end +end return 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 Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.20.3"sv; +const std::string_view version = "0.20.4"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -9155,8 +9155,15 @@ private: for (auto branch_ : branches) { auto branch = static_cast(branch_); auto valueList = static_cast(branch->condition.get()); - if (auto value = singleValueFrom(valueList); - value && (value->item.is() || value->get_by_path())) { + bool tableMatching = false; + if (auto value = singleValueFrom(valueList)) { + if (value->item.is()) { + tableMatching = true; + } else if (auto sVal = value->item.as()){ + tableMatching = ast_is(sVal->value); + } + } + if (tableMatching) { if (!firstBranch) { temp.push_back(indent() + "else"s + nll(branch)); pushScope(); -- cgit v1.2.3-55-g6feb