diff options
-rw-r--r-- | spec/inputs/switch.yue | 6 | ||||
-rw-r--r-- | spec/outputs/switch.lua | 35 | ||||
-rw-r--r-- | 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 | |||
159 | when {x: <>: mt} | 159 | when {x: <>: mt} |
160 | print mt | 160 | print mt |
161 | 161 | ||
162 | do | ||
163 | switch tb | ||
164 | when [ [item,],] | ||
165 | print item | ||
166 | when [a = 1, b = "abc"] | ||
167 | print a, b | ||
162 | nil | 168 | nil |
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 |
400 | end | 400 | end |
401 | do | ||
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 | ||
435 | end | ||
401 | return nil | 436 | 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<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.20.3"sv; | 78 | const std::string_view version = "0.20.4"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 { |
@@ -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(); |