From 7f18f8cce64f50fb0fb8ba46078860d93b883f21 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 23 Feb 2020 10:24:01 +0800 Subject: add placeholder support for backcall statement. --- spec/inputs/backcall.moon | 10 ++++++++++ src/MoonP/moon_compiler.cpp | 30 ++++++++++++++++++++++++++---- 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 @@ -40,6 +40,16 @@ do (x)<- map {1,2,3} x * 2 +do + (x)<- map _,{1,2,3} + x * 2 + +do + (x)<- filter _, do + (x)<- map _,{1,2,3,4} + x * 2 + x > 2 + do (data)<- http?.get "ajaxtest" body[".result"]\html 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) { } const char* moonScriptVersion() { - return "0.5.0-r0.1.3"; + return "0.5.0-r0.1.4"; } class MoonCompilerImpl { @@ -1516,7 +1516,10 @@ private: } bool findPlaceHolder = false; for (auto a : args->objects()) { + bool lintGlobal = _config.lintGlobalVariable; + _config.lintGlobalVariable = false; auto name = singleVariableFrom(a); + _config.lintGlobalVariable = lintGlobal; if (name == "_"sv) { if (!findPlaceHolder) { args->swap(a, arg); @@ -1747,10 +1750,29 @@ private: } if (isChainValueCall(backcall->value)) { auto last = backcall->value->items.back(); - if (auto invoke = ast_cast(last)) { - invoke->args.push_back(arg); + _ast_list* args = nullptr; + if (auto invoke = ast_cast(last)) { + args = &invoke->args; } else { - ast_to(last)->args.push_back(arg); + args = &(ast_to(last)->args); + } + bool findPlaceHolder = false; + for (auto a : args->objects()) { + bool lintGlobal = _config.lintGlobalVariable; + _config.lintGlobalVariable = false; + auto name = singleVariableFrom(a); + _config.lintGlobalVariable = lintGlobal; + if (name == "_"sv) { + if (!findPlaceHolder) { + args->swap(a, arg); + findPlaceHolder = true; + } else { + throw std::logic_error(_info.errorMessage("backcall placeholder can be used only in one place."sv, a)); + } + } + } + if (!findPlaceHolder) { + args->push_back(arg); } } else { auto invoke = x->new_ptr(); -- cgit v1.2.3-55-g6feb