aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-06 10:41:30 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-06 10:41:30 +0800
commit32651cccb380bf4682fa8f6d3e70d74952171f7b (patch)
tree498a5d77886a0ff56b54eabed1177759bac9eba9
parent90bdaec058948f00d11ccfe955ce252ace045929 (diff)
downloadyuescript-32651cccb380bf4682fa8f6d3e70d74952171f7b.tar.gz
yuescript-32651cccb380bf4682fa8f6d3e70d74952171f7b.tar.bz2
yuescript-32651cccb380bf4682fa8f6d3e70d74952171f7b.zip
fix #38
-rw-r--r--spec/inputs/macro.mp42
-rw-r--r--src/MoonP/moon_compiler.cpp16
-rw-r--r--src/MoonP/moon_parser.h7
3 files changed, 47 insertions, 18 deletions
diff --git a/spec/inputs/macro.mp b/spec/inputs/macro.mp
index be88a3f..da696bd 100644
--- a/spec/inputs/macro.mp
+++ b/spec/inputs/macro.mp
@@ -154,16 +154,38 @@ $insertComment "a comment here"
154 154
155import 'underscore' as _ 155import 'underscore' as _
156 156
157macro expr chain = (...)-> $showMacro "chain",table.concat {...}, "\\" 157macro expr chain = (...)->
158 callable = nil
159 for item in *{...}
160 callable = callable? and "(#{callable})\\#{item}" or item
161 $showMacro "chain", callable
158 162
159a = $chain( 163a = $chain(
160 _({1, 2, 3, 4, -2, 3}) 164 _{1, 2, 3, 4, -2, 3}
161 chain! 165 chain!
162 map(=> @ * 2) 166 map => @ * 2
163 filter(=> @ > 3) 167 filter => @ > 3
164 value! 168 value!
165) 169)
166 170
171$chain(
172 _{1, 2, 3, 4, -2, 3}
173 chain!
174 map => @ * 2
175 filter => @ > 3
176 each => print @
177)
178
179result = $chain(
180 origin.transform.root.gameObject\Parents!
181 Descendants!
182 SelectEnable!
183 SelectVisible!
184 TagEqual "fx"
185 Where (x) -> x.name\EndsWith "(Clone)"
186 Destroy!
187)
188
167macro block chainB = (...)-> 189macro block chainB = (...)->
168 switch select "#", ... 190 switch select "#", ...
169 when 0 then return "" 191 when 0 then return ""
@@ -185,11 +207,13 @@ macro block chainB = (...)->
185 $showMacro "chainB", res 207 $showMacro "chainB", res
186 208
187$chainB( 209$chainB(
188 _{1, 2, 3, 4, -2, 3} 210 origin.transform.root.gameObject\Parents!
189 chain! 211 Descendants!
190 map => @ * 2 212 SelectEnable!
191 filter => @ > 3 213 SelectVisible!
192 each => print @ 214 TagEqual "fx"
215 Where (x) -> x.name\EndsWith "(Clone)"
216 Destroy!
193) 217)
194 218
195macro block implicitReturnblockMacroIsAllowed = -> "123" 219macro block implicitReturnblockMacroIsAllowed = -> "123"
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp
index fcdde59..4812cf4 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/MoonP/moon_compiler.cpp
@@ -53,7 +53,7 @@ inline std::string s(std::string_view sv) {
53 return std::string(sv); 53 return std::string(sv);
54} 54}
55 55
56const std::string_view version = "0.4.24"sv; 56const std::string_view version = "0.4.26"sv;
57const std::string_view extension = "mp"sv; 57const std::string_view extension = "mp"sv;
58 58
59class MoonCompilerImpl { 59class MoonCompilerImpl {
@@ -1555,7 +1555,10 @@ private:
1555 if (assign) { 1555 if (assign) {
1556 auto exp = ifCondPairs.front().first->condition.get(); 1556 auto exp = ifCondPairs.front().first->condition.get();
1557 auto x = exp; 1557 auto x = exp;
1558 bool lintGlobal = _config.lintGlobalVariable;
1559 _config.lintGlobalVariable = false;
1558 auto var = singleVariableFrom(exp); 1560 auto var = singleVariableFrom(exp);
1561 _config.lintGlobalVariable = lintGlobal;
1559 if (var.empty()) { 1562 if (var.empty()) {
1560 storingValue = true; 1563 storingValue = true;
1561 auto desVar = getUnusedName("_des_"sv); 1564 auto desVar = getUnusedName("_des_"sv);
@@ -3234,6 +3237,7 @@ private:
3234 str_list localVars; 3237 str_list localVars;
3235 std::tie(type, codes, localVars) = expandMacroStr(chainValue); 3238 std::tie(type, codes, localVars) = expandMacroStr(chainValue);
3236 std::string targetType(usage != ExpUsage::Common || chainList.size() > 2 ? "expr"sv : "block"sv); 3239 std::string targetType(usage != ExpUsage::Common || chainList.size() > 2 ? "expr"sv : "block"sv);
3240 ParseInfo info;
3237 if (type == "lua"sv) { 3241 if (type == "lua"sv) {
3238 if (!allowBlockMacroReturn && targetType != "block"sv) { 3242 if (!allowBlockMacroReturn && targetType != "block"sv) {
3239 throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x)); 3243 throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x));
@@ -3252,9 +3256,14 @@ private:
3252 } 3256 }
3253 return {nullptr, nullptr, std::move(codes), std::move(localVars)}; 3257 return {nullptr, nullptr, std::move(codes), std::move(localVars)};
3254 } else if (!allowBlockMacroReturn && type != targetType) { 3258 } else if (!allowBlockMacroReturn && type != targetType) {
3255 throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); 3259 if (!codes.empty() && targetType == "block") {
3260 info = _parser.parse<Block_t>(codes);
3261 }
3262 if (info.node) type = "block";
3263 else throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x));
3256 } 3264 }
3257 ParseInfo info; 3265 BLOCK_START
3266 BREAK_IF(info.node);
3258 if (usage == ExpUsage::Common) { 3267 if (usage == ExpUsage::Common) {
3259 if (codes.empty()) { 3268 if (codes.empty()) {
3260 return {x->new_ptr<Block_t>().get(), std::move(info.codes), Empty, std::move(localVars)}; 3269 return {x->new_ptr<Block_t>().get(), std::move(info.codes), Empty, std::move(localVars)};
@@ -3273,6 +3282,7 @@ private:
3273 } else { 3282 } else {
3274 info = _parser.parse<Exp_t>(codes); 3283 info = _parser.parse<Exp_t>(codes);
3275 } 3284 }
3285 BLOCK_END
3276 if (!info.node) { 3286 if (!info.node) {
3277 info.error = info.error.substr(info.error.find(':') + 2); 3287 info.error = info.error.substr(info.error.find(':') + 2);
3278 throw std::logic_error(_info.errorMessage("failed to parse expanded codes: " + info.error, x)); 3288 throw std::logic_error(_info.errorMessage("failed to parse expanded codes: " + info.error, x));
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h
index 832d39d..d965d87 100644
--- a/src/MoonP/moon_parser.h
+++ b/src/MoonP/moon_parser.h
@@ -48,12 +48,7 @@ public:
48 48
49 template<class AST> 49 template<class AST>
50 ParseInfo parse(std::string_view codes) { 50 ParseInfo parse(std::string_view codes) {
51 error_list errors; 51 return parse(codes, getRule<AST>());
52 auto res = parse(codes, getRule<AST>());
53 if (res.node.template is<AST>()) {
54 return res;
55 }
56 return res;
57 } 52 }
58 53
59 template <class AST> 54 template <class AST>