From f7b674213bd996016cb94c44ef22736a6cbdf6f3 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 14 Jul 2022 17:35:04 +0800 Subject: fix else clause issue in switch statement with table matching. --- spec/inputs/switch.yue | 2 +- spec/outputs/switch.lua | 10 ++++++++-- src/yuescript/yue_compiler.cpp | 12 ++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue index 9e7ecfd..08330bf 100644 --- a/spec/inputs/switch.yue +++ b/spec/inputs/switch.yue @@ -146,7 +146,7 @@ do do switch y - when {x: #:mt} + when {x: #: mt} print mt nil diff --git a/spec/outputs/switch.lua b/spec/outputs/switch.lua index a341e99..c4cde26 100644 --- a/spec/outputs/switch.lua +++ b/spec/outputs/switch.lua @@ -213,12 +213,15 @@ do end end if not _match_2 then + local _match_3 = false if _tab_0 then local mt = getmetatable(item) if mt ~= nil then + _match_3 = true print("A table with metatable") end - else + end + if not _match_3 then print("item not accepted!") end end @@ -316,13 +319,16 @@ do if 1 == _exp_0 or 2 == _exp_0 or 3 == _exp_0 or 4 == _exp_0 or 5 == _exp_0 then return "number 1 - 5" else + local _match_1 = false if _tab_0 then local matchAnyTable = _exp_0.matchAnyTable if matchAnyTable == nil then matchAnyTable = "fallback" end + _match_1 = true return matchAnyTable - else + end + if not _match_1 then return "should not reach here unless it is not a table" end end diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 21764fe..85aa6d4 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -56,7 +56,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.13.5"sv; +const std::string_view version = "0.13.6"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -7169,7 +7169,7 @@ private: temp.push_back(indent() + "local "s + tabCheckVar + " = \"table\" == "s + globalVar("type", branch) + '(' + objVar + ')' + nll(branch)); } std::string matchVar; - bool lastBranch = branches.back() == branch_; + bool lastBranch = branches.back() == branch_ && !switchNode->lastBranch; if (!lastBranch) { matchVar = getUnusedName("_match_"); forceAddToScope(matchVar); @@ -7232,8 +7232,12 @@ private: } } if (switchNode->lastBranch) { - temp.push_back(indent() + "else"s + nll(switchNode->lastBranch)); - pushScope(); + if (!firstBranch) { + temp.push_back(indent() + "else"s + nll(switchNode->lastBranch)); + pushScope(); + } else { + addScope--; + } transform_plain_body(switchNode->lastBranch, temp, usage, assignList); popScope(); } -- cgit v1.2.3-55-g6feb