From c44cab60b7b6cf0a36e1dd011c2fe483e8d87b38 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sat, 20 Feb 2021 17:34:52 +0800 Subject: fix issue in destructure syntax. --- CHANGELOG.md | 91 ++++++++++++++++++++++++++++++++++++------ spec/inputs/destructure.yue | 6 +++ src/yuescript/yue_compiler.cpp | 17 ++++++-- 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e1be05..347f6e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,33 +3,98 @@ The implementation for original Moonscript language 0.5.0 can be found in the `0.5.0` branch of Yuescript. The Moonscript with fixes and new features is in the main branch of Yuescript. Here are the changelogs for each Yuescript version. -## v0.6.6 +## v0.6.7 ### Fixed Issues -* Simplify macro syntax. Macro function can either return a code string or a config table. +* Simplify macro syntax. Macro function can either return a Yuescript string or a config table containing Lua codes. ```moonscript -macro local = (var)-> "global *" +macro localFunc = (var)-> "local #{var} = ->" +$localFunc f1 +f1 = -> "another function" -$local x -x = 1 -y = 2 -z = 3 +-- or -macro local = (var)-> +macro localFunc = (var)-> { - codes: "local #{var}" + codes: "local function #{var}() end" type: "lua" locals: {var} } - -$local y -y = 1 +$localFunc f2 +f2 = -> "another function" +``` +Compiles to: +```Lua +local f1 +f1 = function() end +f1 = function() + return "another function" +end +local function f2() end +f2 = function() + return "another function" +end ``` * Change Yuescript file extension to '.yue' because some of the Moonscript syntax are no longer supported and some codes written in Yuescript syntax won't be accepted by Moonscript compiler. -* +* Disable the use of local and global statement with wildcard operators. +* Change backcall operator syntax, extra parentheses for multiline chains are no longer needed. +```moonscript +readFile "example.txt" + |> extract language, {} + |> parse language + |> emit + |> render + |> print +``` +Compiles to: +```Lua +return print(render(emit(parse(extract(readFile("example.txt"), language, { }), language)))) +``` + + +### Added Features + +* Supporting multiline chaining function call syntax. +```Moonscript +result = origin + .transform.root + .gameObject + \Parents! + \Descendants! + \SelectEnable! + \SelectVisible! + \TagEqual "fx" + \Where (x)-> + if x\IsTargeted! + return false + x.name\EndsWith "(Clone)" + \Destroy! + +origin.transform.root.gameObject + \Parents!\Descendants! + \SelectEnable! + \SelectVisible! + \TagEqual "fx" + \Where (x)-> x.name\EndsWith "(Clone)" + \Destroy! +``` +Compiles to: +```Lua +local result = origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x) + if x:IsTargeted() then + return false + end + return x.name:EndsWith("(Clone)") +end):Destroy() +return origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x) + return x.name:EndsWith("(Clone)") +end):Destroy() +``` + + ## v0.4.16 diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue index 49e6393..6b52441 100644 --- a/spec/inputs/destructure.yue +++ b/spec/inputs/destructure.yue @@ -22,6 +22,12 @@ do {a, :b, c, :d, e, :f, g} = tbl + do :a, :b, c = tbl + + do :a, b, :c = tbl + + do a, :b, :c = tbl + --- do diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 7be0c84..ebc2b3f 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -58,7 +58,7 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const std::string_view version = "0.6.7"sv; +const std::string_view version = "0.6.8"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -1237,7 +1237,7 @@ private: if (pair.isVariable && !isDefined(pair.name)) { _buf << s("local "sv); } - _buf << pair.name << " = "sv << info.first.front().value << pair.structure << nll(assignment); + _buf << pair.name << " = "sv << destruct.value << pair.structure << nll(assignment); addToScope(pair.name); temp.push_back(clearBuf()); } else if (_parser.match(destruct.value)) { @@ -1451,6 +1451,13 @@ private: auto value = singleValueFrom(expr); ast_node* destructNode = value->getByPath(); if (destructNode || (destructNode = value->item.as())) { + if (*j != nullNode) { + if (auto ssVal = simpleSingleValueFrom(*j)) { + if (ssVal->value.is()) { + throw std::logic_error(_info.errorMessage("can not destruct a const value"sv, ssVal->value)); + } + } + } destructPairs.push_back({i,j}); auto& destruct = destructs.emplace_back(); if (!varDefOnly) { @@ -1462,7 +1469,11 @@ private: } auto pairs = destructFromExp(expr); destruct.items = std::move(pairs); - if (destruct.items.size() == 1 && !singleValueFrom(*j)) { + if (*j == nullNode) { + for (auto& item : destruct.items) { + item.structure.clear(); + } + } else if (destruct.items.size() == 1 && !singleValueFrom(*j)) { destruct.value.insert(0, "("sv); destruct.value.append(")"sv); } -- cgit v1.2.3-55-g6feb