diff options
author | Li Jin <dragon-fly@qq.com> | 2022-07-14 17:35:04 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-07-14 17:35:04 +0800 |
commit | f7b674213bd996016cb94c44ef22736a6cbdf6f3 (patch) | |
tree | c130d624f8445813d0b3dfbbbac0d698bd2b0f66 | |
parent | 9c1d7bca08a88254ac33d7df16fc0a48f34912f4 (diff) | |
download | yuescript-f7b674213bd996016cb94c44ef22736a6cbdf6f3.tar.gz yuescript-f7b674213bd996016cb94c44ef22736a6cbdf6f3.tar.bz2 yuescript-f7b674213bd996016cb94c44ef22736a6cbdf6f3.zip |
fix else clause issue in switch statement with table matching.
-rw-r--r-- | spec/inputs/switch.yue | 2 | ||||
-rw-r--r-- | spec/outputs/switch.lua | 10 | ||||
-rwxr-xr-x | 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 | |||
146 | 146 | ||
147 | do | 147 | do |
148 | switch y | 148 | switch y |
149 | when {x: #:mt} | 149 | when {x: #: mt} |
150 | print mt | 150 | print mt |
151 | 151 | ||
152 | nil | 152 | 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 | |||
213 | end | 213 | end |
214 | end | 214 | end |
215 | if not _match_2 then | 215 | if not _match_2 then |
216 | local _match_3 = false | ||
216 | if _tab_0 then | 217 | if _tab_0 then |
217 | local mt = getmetatable(item) | 218 | local mt = getmetatable(item) |
218 | if mt ~= nil then | 219 | if mt ~= nil then |
220 | _match_3 = true | ||
219 | print("A table with metatable") | 221 | print("A table with metatable") |
220 | end | 222 | end |
221 | else | 223 | end |
224 | if not _match_3 then | ||
222 | print("item not accepted!") | 225 | print("item not accepted!") |
223 | end | 226 | end |
224 | end | 227 | end |
@@ -316,13 +319,16 @@ do | |||
316 | if 1 == _exp_0 or 2 == _exp_0 or 3 == _exp_0 or 4 == _exp_0 or 5 == _exp_0 then | 319 | if 1 == _exp_0 or 2 == _exp_0 or 3 == _exp_0 or 4 == _exp_0 or 5 == _exp_0 then |
317 | return "number 1 - 5" | 320 | return "number 1 - 5" |
318 | else | 321 | else |
322 | local _match_1 = false | ||
319 | if _tab_0 then | 323 | if _tab_0 then |
320 | local matchAnyTable = _exp_0.matchAnyTable | 324 | local matchAnyTable = _exp_0.matchAnyTable |
321 | if matchAnyTable == nil then | 325 | if matchAnyTable == nil then |
322 | matchAnyTable = "fallback" | 326 | matchAnyTable = "fallback" |
323 | end | 327 | end |
328 | _match_1 = true | ||
324 | return matchAnyTable | 329 | return matchAnyTable |
325 | else | 330 | end |
331 | if not _match_1 then | ||
326 | return "should not reach here unless it is not a table" | 332 | return "should not reach here unless it is not a table" |
327 | end | 333 | end |
328 | end | 334 | 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; | |||
56 | 56 | ||
57 | typedef std::list<std::string> str_list; | 57 | typedef std::list<std::string> str_list; |
58 | 58 | ||
59 | const std::string_view version = "0.13.5"sv; | 59 | const std::string_view version = "0.13.6"sv; |
60 | const std::string_view extension = "yue"sv; | 60 | const std::string_view extension = "yue"sv; |
61 | 61 | ||
62 | class YueCompilerImpl { | 62 | class YueCompilerImpl { |
@@ -7169,7 +7169,7 @@ private: | |||
7169 | temp.push_back(indent() + "local "s + tabCheckVar + " = \"table\" == "s + globalVar("type", branch) + '(' + objVar + ')' + nll(branch)); | 7169 | temp.push_back(indent() + "local "s + tabCheckVar + " = \"table\" == "s + globalVar("type", branch) + '(' + objVar + ')' + nll(branch)); |
7170 | } | 7170 | } |
7171 | std::string matchVar; | 7171 | std::string matchVar; |
7172 | bool lastBranch = branches.back() == branch_; | 7172 | bool lastBranch = branches.back() == branch_ && !switchNode->lastBranch; |
7173 | if (!lastBranch) { | 7173 | if (!lastBranch) { |
7174 | matchVar = getUnusedName("_match_"); | 7174 | matchVar = getUnusedName("_match_"); |
7175 | forceAddToScope(matchVar); | 7175 | forceAddToScope(matchVar); |
@@ -7232,8 +7232,12 @@ private: | |||
7232 | } | 7232 | } |
7233 | } | 7233 | } |
7234 | if (switchNode->lastBranch) { | 7234 | if (switchNode->lastBranch) { |
7235 | temp.push_back(indent() + "else"s + nll(switchNode->lastBranch)); | 7235 | if (!firstBranch) { |
7236 | pushScope(); | 7236 | temp.push_back(indent() + "else"s + nll(switchNode->lastBranch)); |
7237 | pushScope(); | ||
7238 | } else { | ||
7239 | addScope--; | ||
7240 | } | ||
7237 | transform_plain_body(switchNode->lastBranch, temp, usage, assignList); | 7241 | transform_plain_body(switchNode->lastBranch, temp, usage, assignList); |
7238 | popScope(); | 7242 | popScope(); |
7239 | } | 7243 | } |