aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-02-23 10:24:01 +0800
committerLi Jin <dragon-fly@qq.com>2020-02-23 10:24:01 +0800
commit7f18f8cce64f50fb0fb8ba46078860d93b883f21 (patch)
treeec66fa92a0688a7e7c79c67ec6df79055a551d8e
parent39457b75c0923cf287c9145fd9c9a6ba4a86767b (diff)
downloadyuescript-7f18f8cce64f50fb0fb8ba46078860d93b883f21.tar.gz
yuescript-7f18f8cce64f50fb0fb8ba46078860d93b883f21.tar.bz2
yuescript-7f18f8cce64f50fb0fb8ba46078860d93b883f21.zip
add placeholder support for backcall statement.
-rw-r--r--spec/inputs/backcall.moon10
-rw-r--r--src/MoonP/moon_compiler.cpp30
2 files changed, 36 insertions, 4 deletions
diff --git a/spec/inputs/backcall.moon b/spec/inputs/backcall.moon
index f5d0046..3aedba9 100644
--- a/spec/inputs/backcall.moon
+++ b/spec/inputs/backcall.moon
@@ -41,6 +41,16 @@ do
41 x * 2 41 x * 2
42 42
43do 43do
44 (x)<- map _,{1,2,3}
45 x * 2
46
47do
48 (x)<- filter _, do
49 (x)<- map _,{1,2,3,4}
50 x * 2
51 x > 2
52
53do
44 (data)<- http?.get "ajaxtest" 54 (data)<- http?.get "ajaxtest"
45 body[".result"]\html data 55 body[".result"]\html data
46 (processed)<- http.post "ajaxprocess", data 56 (processed)<- http.post "ajaxprocess", data
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp
index 64152cf..a39bfd0 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/MoonP/moon_compiler.cpp
@@ -32,7 +32,7 @@ inline std::string s(std::string_view sv) {
32} 32}
33 33
34const char* moonScriptVersion() { 34const char* moonScriptVersion() {
35 return "0.5.0-r0.1.3"; 35 return "0.5.0-r0.1.4";
36} 36}
37 37
38class MoonCompilerImpl { 38class MoonCompilerImpl {
@@ -1516,7 +1516,10 @@ private:
1516 } 1516 }
1517 bool findPlaceHolder = false; 1517 bool findPlaceHolder = false;
1518 for (auto a : args->objects()) { 1518 for (auto a : args->objects()) {
1519 bool lintGlobal = _config.lintGlobalVariable;
1520 _config.lintGlobalVariable = false;
1519 auto name = singleVariableFrom(a); 1521 auto name = singleVariableFrom(a);
1522 _config.lintGlobalVariable = lintGlobal;
1520 if (name == "_"sv) { 1523 if (name == "_"sv) {
1521 if (!findPlaceHolder) { 1524 if (!findPlaceHolder) {
1522 args->swap(a, arg); 1525 args->swap(a, arg);
@@ -1747,10 +1750,29 @@ private:
1747 } 1750 }
1748 if (isChainValueCall(backcall->value)) { 1751 if (isChainValueCall(backcall->value)) {
1749 auto last = backcall->value->items.back(); 1752 auto last = backcall->value->items.back();
1750 if (auto invoke = ast_cast<Invoke_t>(last)) { 1753 _ast_list* args = nullptr;
1751 invoke->args.push_back(arg); 1754 if (auto invoke = ast_cast<InvokeArgs_t>(last)) {
1755 args = &invoke->args;
1752 } else { 1756 } else {
1753 ast_to<InvokeArgs_t>(last)->args.push_back(arg); 1757 args = &(ast_to<Invoke_t>(last)->args);
1758 }
1759 bool findPlaceHolder = false;
1760 for (auto a : args->objects()) {
1761 bool lintGlobal = _config.lintGlobalVariable;
1762 _config.lintGlobalVariable = false;
1763 auto name = singleVariableFrom(a);
1764 _config.lintGlobalVariable = lintGlobal;
1765 if (name == "_"sv) {
1766 if (!findPlaceHolder) {
1767 args->swap(a, arg);
1768 findPlaceHolder = true;
1769 } else {
1770 throw std::logic_error(_info.errorMessage("backcall placeholder can be used only in one place."sv, a));
1771 }
1772 }
1773 }
1774 if (!findPlaceHolder) {
1775 args->push_back(arg);
1754 } 1776 }
1755 } else { 1777 } else {
1756 auto invoke = x->new_ptr<Invoke_t>(); 1778 auto invoke = x->new_ptr<Invoke_t>();