aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moon_compiler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/MoonP/moon_compiler.cpp')
-rw-r--r--src/MoonP/moon_compiler.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp
index 0ac7a60..80ab5a9 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/MoonP/moon_compiler.cpp
@@ -42,8 +42,8 @@ inline std::string s(std::string_view sv) {
42 return std::string(sv); 42 return std::string(sv);
43} 43}
44 44
45const char* version() { 45const std::string_view version() {
46 return "0.3.8"; 46 return "0.3.9"sv;
47} 47}
48 48
49// name of table stored in lua registry 49// name of table stored in lua registry
@@ -2013,6 +2013,7 @@ private:
2013 expListLow->exprs.dup(expList->exprs); 2013 expListLow->exprs.dup(expList->exprs);
2014 auto returnNode = x->new_ptr<Return_t>(); 2014 auto returnNode = x->new_ptr<Return_t>();
2015 returnNode->valueList.set(expListLow); 2015 returnNode->valueList.set(expListLow);
2016 returnNode->allowBlockMacroReturn = true;
2016 last->content.set(returnNode); 2017 last->content.set(returnNode);
2017 BLOCK_END 2018 BLOCK_END
2018 break; 2019 break;
@@ -2274,10 +2275,9 @@ private:
2274 transformUnless(static_cast<Unless_t*>(value), out, ExpUsage::Return); 2275 transformUnless(static_cast<Unless_t*>(value), out, ExpUsage::Return);
2275 return; 2276 return;
2276 } 2277 }
2277 } 2278 } else if (auto chainValue = singleValue->item.as<ChainValue_t>()) {
2278 if (auto chainValue = singleValue->item.as<ChainValue_t>()) {
2279 if (specialChainValue(chainValue) != ChainType::Common) { 2279 if (specialChainValue(chainValue) != ChainType::Common) {
2280 transformChainValue(chainValue, out, ExpUsage::Return); 2280 transformChainValue(chainValue, out, ExpUsage::Return, nullptr, returnNode->allowBlockMacroReturn);
2281 return; 2281 return;
2282 } 2282 }
2283 } 2283 }
@@ -3015,7 +3015,7 @@ private:
3015 return {type, codes}; 3015 return {type, codes};
3016 } 3016 }
3017 3017
3018 std::tuple<ast_ptr<false,ast_node>, std::unique_ptr<input>, std::string> expandMacro(ChainValue_t* chainValue, ExpUsage usage) { 3018 std::tuple<ast_ptr<false,ast_node>, std::unique_ptr<input>, std::string> expandMacro(ChainValue_t* chainValue, ExpUsage usage, bool allowBlockMacroReturn) {
3019 auto x = ast_to<Callable_t>(chainValue->items.front())->item.to<MacroName_t>(); 3019 auto x = ast_to<Callable_t>(chainValue->items.front())->item.to<MacroName_t>();
3020 const auto& chainList = chainValue->items.objects(); 3020 const auto& chainList = chainValue->items.objects();
3021 std::string type, codes; 3021 std::string type, codes;
@@ -3033,8 +3033,7 @@ private:
3033 throw std::logic_error(_info.errorMessage(err, x)); 3033 throw std::logic_error(_info.errorMessage(err, x));
3034 } 3034 }
3035 return {nullptr, nullptr, std::move(codes)}; 3035 return {nullptr, nullptr, std::move(codes)};
3036 } 3036 } else if (!allowBlockMacroReturn && type != targetType) {
3037 if (type != targetType) {
3038 throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); 3037 throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x));
3039 } 3038 }
3040 ParseInfo info; 3039 ParseInfo info;
@@ -3047,6 +3046,12 @@ private:
3047 } else { 3046 } else {
3048 info = _parser.parse<Block_t>(codes); 3047 info = _parser.parse<Block_t>(codes);
3049 } 3048 }
3049 } else if (allowBlockMacroReturn) {
3050 if (type == "expr"sv) {
3051 info = _parser.parse<Exp_t>(codes);
3052 } else {
3053 info = _parser.parse<Block_t>(codes);
3054 }
3050 } else { 3055 } else {
3051 info = _parser.parse<Exp_t>(codes); 3056 info = _parser.parse<Exp_t>(codes);
3052 } 3057 }
@@ -3100,12 +3105,12 @@ private:
3100 return {info.node, std::move(info.codes), Empty}; 3105 return {info.node, std::move(info.codes), Empty};
3101 } 3106 }
3102 3107
3103 void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { 3108 void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) {
3104 if (isMacroChain(chainValue)) { 3109 if (isMacroChain(chainValue)) {
3105 ast_ptr<false,ast_node> node; 3110 ast_ptr<false,ast_node> node;
3106 std::unique_ptr<input> codes; 3111 std::unique_ptr<input> codes;
3107 std::string luaCodes; 3112 std::string luaCodes;
3108 std::tie(node, codes, luaCodes) = expandMacro(chainValue, usage); 3113 std::tie(node, codes, luaCodes) = expandMacro(chainValue, usage, allowBlockMacroReturn);
3109 Utils::replace(luaCodes, "\r\n"sv, "\n"sv); 3114 Utils::replace(luaCodes, "\r\n"sv, "\n"sv);
3110 Utils::trim(luaCodes); 3115 Utils::trim(luaCodes);
3111 if (!node && !codes) { 3116 if (!node && !codes) {
@@ -3118,7 +3123,7 @@ private:
3118 out.push_back(luaCodes); 3123 out.push_back(luaCodes);
3119 return; 3124 return;
3120 } 3125 }
3121 if (usage == ExpUsage::Common) { 3126 if (usage == ExpUsage::Common || (usage == ExpUsage::Return && node.is<Block_t>())) {
3122 transformBlock(node.to<Block_t>(), out, usage, assignList); 3127 transformBlock(node.to<Block_t>(), out, usage, assignList);
3123 } else { 3128 } else {
3124 auto x = chainValue; 3129 auto x = chainValue;