summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-20 17:34:52 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-20 17:34:52 +0800
commitc44cab60b7b6cf0a36e1dd011c2fe483e8d87b38 (patch)
tree9fcf8a06e63991a27b8f73b4e5040788338d115f
parent3ea3f9b1c42eae103c370a986f539a0f1550db9f (diff)
downloadyuescript-c44cab60b7b6cf0a36e1dd011c2fe483e8d87b38.tar.gz
yuescript-c44cab60b7b6cf0a36e1dd011c2fe483e8d87b38.tar.bz2
yuescript-c44cab60b7b6cf0a36e1dd011c2fe483e8d87b38.zip
fix issue in destructure syntax.
-rw-r--r--CHANGELOG.md91
-rw-r--r--spec/inputs/destructure.yue6
-rw-r--r--src/yuescript/yue_compiler.cpp17
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 @@
3The 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. 3The 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.
4 4
5 5
6## v0.6.6 6## v0.6.7
7 7
8### Fixed Issues 8### Fixed Issues
9 9
10* Simplify macro syntax. Macro function can either return a code string or a config table. 10* Simplify macro syntax. Macro function can either return a Yuescript string or a config table containing Lua codes.
11 11
12```moonscript 12```moonscript
13macro local = (var)-> "global *" 13macro localFunc = (var)-> "local #{var} = ->"
14$localFunc f1
15f1 = -> "another function"
14 16
15$local x 17-- or
16x = 1
17y = 2
18z = 3
19 18
20macro local = (var)-> 19macro localFunc = (var)->
21 { 20 {
22 codes: "local #{var}" 21 codes: "local function #{var}() end"
23 type: "lua" 22 type: "lua"
24 locals: {var} 23 locals: {var}
25 } 24 }
26 25$localFunc f2
27$local y 26f2 = -> "another function"
28y = 1 27```
28Compiles to:
29```Lua
30local f1
31f1 = function() end
32f1 = function()
33 return "another function"
34end
35local function f2() end
36f2 = function()
37 return "another function"
38end
29``` 39```
30 40
31* 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. 41* 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.
32* 42* Disable the use of local and global statement with wildcard operators.
43* Change backcall operator syntax, extra parentheses for multiline chains are no longer needed.
44```moonscript
45readFile "example.txt"
46 |> extract language, {}
47 |> parse language
48 |> emit
49 |> render
50 |> print
51```
52Compiles to:
53```Lua
54return print(render(emit(parse(extract(readFile("example.txt"), language, { }), language))))
55```
56
57
58### Added Features
59
60* Supporting multiline chaining function call syntax.
61```Moonscript
62result = origin
63 .transform.root
64 .gameObject
65 \Parents!
66 \Descendants!
67 \SelectEnable!
68 \SelectVisible!
69 \TagEqual "fx"
70 \Where (x)->
71 if x\IsTargeted!
72 return false
73 x.name\EndsWith "(Clone)"
74 \Destroy!
75
76origin.transform.root.gameObject
77 \Parents!\Descendants!
78 \SelectEnable!
79 \SelectVisible!
80 \TagEqual "fx"
81 \Where (x)-> x.name\EndsWith "(Clone)"
82 \Destroy!
83```
84Compiles to:
85```Lua
86local result = origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x)
87 if x:IsTargeted() then
88 return false
89 end
90 return x.name:EndsWith("(Clone)")
91end):Destroy()
92return origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x)
93 return x.name:EndsWith("(Clone)")
94end):Destroy()
95```
96
97
33 98
34## v0.4.16 99## v0.4.16
35 100
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
22 22
23 {a, :b, c, :d, e, :f, g} = tbl 23 {a, :b, c, :d, e, :f, g} = tbl
24 24
25 do :a, :b, c = tbl
26
27 do :a, b, :c = tbl
28
29 do a, :b, :c = tbl
30
25--- 31---
26 32
27do 33do
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) {
58 return std::string(sv); 58 return std::string(sv);
59} 59}
60 60
61const std::string_view version = "0.6.7"sv; 61const std::string_view version = "0.6.8"sv;
62const std::string_view extension = "yue"sv; 62const std::string_view extension = "yue"sv;
63 63
64class YueCompilerImpl { 64class YueCompilerImpl {
@@ -1237,7 +1237,7 @@ private:
1237 if (pair.isVariable && !isDefined(pair.name)) { 1237 if (pair.isVariable && !isDefined(pair.name)) {
1238 _buf << s("local "sv); 1238 _buf << s("local "sv);
1239 } 1239 }
1240 _buf << pair.name << " = "sv << info.first.front().value << pair.structure << nll(assignment); 1240 _buf << pair.name << " = "sv << destruct.value << pair.structure << nll(assignment);
1241 addToScope(pair.name); 1241 addToScope(pair.name);
1242 temp.push_back(clearBuf()); 1242 temp.push_back(clearBuf());
1243 } else if (_parser.match<Name_t>(destruct.value)) { 1243 } else if (_parser.match<Name_t>(destruct.value)) {
@@ -1451,6 +1451,13 @@ private:
1451 auto value = singleValueFrom(expr); 1451 auto value = singleValueFrom(expr);
1452 ast_node* destructNode = value->getByPath<SimpleValue_t, TableLit_t>(); 1452 ast_node* destructNode = value->getByPath<SimpleValue_t, TableLit_t>();
1453 if (destructNode || (destructNode = value->item.as<simple_table_t>())) { 1453 if (destructNode || (destructNode = value->item.as<simple_table_t>())) {
1454 if (*j != nullNode) {
1455 if (auto ssVal = simpleSingleValueFrom(*j)) {
1456 if (ssVal->value.is<const_value_t>()) {
1457 throw std::logic_error(_info.errorMessage("can not destruct a const value"sv, ssVal->value));
1458 }
1459 }
1460 }
1454 destructPairs.push_back({i,j}); 1461 destructPairs.push_back({i,j});
1455 auto& destruct = destructs.emplace_back(); 1462 auto& destruct = destructs.emplace_back();
1456 if (!varDefOnly) { 1463 if (!varDefOnly) {
@@ -1462,7 +1469,11 @@ private:
1462 } 1469 }
1463 auto pairs = destructFromExp(expr); 1470 auto pairs = destructFromExp(expr);
1464 destruct.items = std::move(pairs); 1471 destruct.items = std::move(pairs);
1465 if (destruct.items.size() == 1 && !singleValueFrom(*j)) { 1472 if (*j == nullNode) {
1473 for (auto& item : destruct.items) {
1474 item.structure.clear();
1475 }
1476 } else if (destruct.items.size() == 1 && !singleValueFrom(*j)) {
1466 destruct.value.insert(0, "("sv); 1477 destruct.value.insert(0, "("sv);
1467 destruct.value.append(")"sv); 1478 destruct.value.append(")"sv);
1468 } 1479 }