aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/MoonP/moon_ast.h1
-rw-r--r--src/MoonP/moon_compiler.cpp27
-rw-r--r--src/MoonP/moon_compiler.h2
-rw-r--r--src/MoonP/moonplus.h6
-rw-r--r--src/MoonP/stacktraceplus.h13
5 files changed, 32 insertions, 17 deletions
diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h
index 0743014..44a55a4 100644
--- a/src/MoonP/moon_ast.h
+++ b/src/MoonP/moon_ast.h
@@ -184,6 +184,7 @@ AST_NODE(ExpList)
184AST_END(ExpList) 184AST_END(ExpList)
185 185
186AST_NODE(Return) 186AST_NODE(Return)
187 bool allowBlockMacroReturn = false;
187 ast_ptr<false, ExpListLow_t> valueList; 188 ast_ptr<false, ExpListLow_t> valueList;
188 AST_MEMBER(Return, &valueList) 189 AST_MEMBER(Return, &valueList)
189AST_END(Return) 190AST_END(Return)
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;
diff --git a/src/MoonP/moon_compiler.h b/src/MoonP/moon_compiler.h
index 540f9e7..37a79bf 100644
--- a/src/MoonP/moon_compiler.h
+++ b/src/MoonP/moon_compiler.h
@@ -17,7 +17,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
17 17
18namespace MoonP { 18namespace MoonP {
19 19
20const char* version(); 20const std::string_view version();
21 21
22struct MoonConfig { 22struct MoonConfig {
23 bool lintGlobalVariable = false; 23 bool lintGlobalVariable = false;
diff --git a/src/MoonP/moonplus.h b/src/MoonP/moonplus.h
index 7059286..83d4c75 100644
--- a/src/MoonP/moonplus.h
+++ b/src/MoonP/moonplus.h
@@ -27,8 +27,8 @@ local lua = {
27 loadstring = loadstring, 27 loadstring = loadstring,
28 load = load 28 load = load
29} 29}
30local dirsep, split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath 30local split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath
31dirsep = "/" 31moonp.dirsep = "/"
32moonp.moon_compiled = { } 32moonp.moon_compiled = { }
33moonp.file_exist = function(fname) 33moonp.file_exist = function(fname)
34 local file = io.open(fname) 34 local file = io.open(fname)
@@ -103,7 +103,7 @@ find_modulepath = function(name)
103 if not package.moonpath then 103 if not package.moonpath then
104 package.moonpath = create_moonpath(package.path) 104 package.moonpath = create_moonpath(package.path)
105 end 105 end
106 local name_path = name:gsub("%.", dirsep) 106 local name_path = name:gsub("%.", moonp.dirsep)
107 local file_exist, file_path 107 local file_exist, file_path
108 for path in package.moonpath:gmatch("[^;]+") do 108 for path in package.moonpath:gmatch("[^;]+") do
109 file_path = path:gsub("?", name_path) 109 file_path = path:gsub("?", name_path)
diff --git a/src/MoonP/stacktraceplus.h b/src/MoonP/stacktraceplus.h
index ea53885..3d32322 100644
--- a/src/MoonP/stacktraceplus.h
+++ b/src/MoonP/stacktraceplus.h
@@ -324,8 +324,17 @@ local function getMoonLineNumber(fname, line)
324 source = moonCompiled["@="..fname] 324 source = moonCompiled["@="..fname]
325 end 325 end
326 if not source then 326 if not source then
327 if moonp.file_exist(fname) then 327 local name_path = fname:gsub("%.", moonp.dirsep)
328 local codes = moonp.read_file(fname) 328 local file_exist, file_path
329 for path in package.path:gmatch("[^;]+") do
330 file_path = path:gsub("?", name_path)
331 file_exist = moonp.file_exist(file_path)
332 if file_exist then
333 break
334 end
335 end
336 if file_exist then
337 local codes = moonp.read_file(file_path)
329 local moonFile = codes:match("^%s*--%s*%[moon%]:%s*([^\n]*)") 338 local moonFile = codes:match("^%s*--%s*%[moon%]:%s*([^\n]*)")
330 if moonFile then 339 if moonFile then
331 fname = moonFile:gsub("^%s*(.-)%s*$", "%1") 340 fname = moonFile:gsub("^%s*(.-)%s*$", "%1")