From 7bfa002f3d4dcb62e6a301141be6d17889a94f55 Mon Sep 17 00:00:00 2001 From: Li Jin <dragon-fly@qq.com> Date: Fri, 27 Oct 2023 16:58:43 +0800 Subject: fix a missing case for list destructuring. --- spec/inputs/destructure.yue | 8 ++++++++ spec/outputs/destructure.lua | 24 ++++++++++++++++++++++++ src/yuescript/LICENSE.md | 21 +++++++++++++++++++++ src/yuescript/yue_ast.h | 2 +- src/yuescript/yue_compiler.cpp | 9 ++++++++- src/yuescript/yue_parser.cpp | 2 +- 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/yuescript/LICENSE.md diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue index d161fd8..73e4d33 100644 --- a/spec/inputs/destructure.yue +++ b/spec/inputs/destructure.yue @@ -80,6 +80,8 @@ do for {x,y} in *thing print x,y + for [x,y] in *thing + print x,y -- @@ -178,9 +180,15 @@ do for {left = "null", right = false} in *tuples print left, right + for [left = "null", right = false] in *tuples + print left, right + do {_, a, _, b} = tb -- list placeholder +do + [a, _, b, _] = tb -- list placeholder + do {x: a.b = 1, y: a.c = 2} = x.x.x diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua index 25e0529..38f21ff 100644 --- a/spec/outputs/destructure.lua +++ b/spec/outputs/destructure.lua @@ -124,6 +124,11 @@ do local x, y = _des_0[1], _des_0[2] print(x, y) end + for _index_0 = 1, #thing do + local _des_0 = thing[_index_0] + local x, y = _des_0[1], _des_0[2] + print(x, y) + end end do do @@ -370,6 +375,18 @@ do end print(left, right) end + local _list_1 = tuples + for _index_0 = 1, #_list_1 do + local _des_0 = _list_1[_index_0] + local left, right = _des_0[1], _des_0[2] + if left == nil then + left = "null" + end + if right == nil then + right = false + end + print(left, right) + end end do local a, b @@ -378,6 +395,13 @@ do a, b = _obj_0[2], _obj_0[4] end end +do + local a, b + do + local _obj_0 = tb + a, b = _obj_0[1], _obj_0[3] + end +end do do local _obj_0 = x.x.x diff --git a/src/yuescript/LICENSE.md b/src/yuescript/LICENSE.md new file mode 100644 index 0000000..b1a5fee --- /dev/null +++ b/src/yuescript/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Li Jin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 2136849..fa47559 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -787,7 +787,7 @@ AST_NODE(Macro) AST_END(Macro, "macro"sv) AST_NODE(NameOrDestructure) - ast_sel<true, Variable_t, TableLit_t> item; + ast_sel<true, Variable_t, TableLit_t, Comprehension_t> item; AST_MEMBER(NameOrDestructure, &item) AST_END(NameOrDestructure, "name_or_des"sv) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 0265e07..648dab2 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.20.2"sv; +const std::string_view version = "0.20.3"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -6842,6 +6842,13 @@ private: varAfter.push_back(desVar); break; } + case id<Comprehension_t>(): { + auto desVar = getUnusedName("_des_"sv); + destructPairs.emplace_back(item, toAst<Exp_t>(desVar, x)); + vars.push_back(desVar); + varAfter.push_back(desVar); + break; + } default: YUEE("AST node mismatch", item); break; } } diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 8ceee9a..11c7627 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -835,7 +835,7 @@ YueParser::YueParser() { MacroInPlace = '$' >> space >> "->" >> space >> Body; NameList = Seperator >> Variable >> *(space >> ',' >> space >> Variable); - NameOrDestructure = Variable | TableLit; + NameOrDestructure = Variable | TableLit | Comprehension; AssignableNameList = Seperator >> NameOrDestructure >> *(space >> ',' >> space >> NameOrDestructure); FnArrowBack = '<' >> set("-="); -- cgit v1.2.3-55-g6feb