From 3af701f4f06d09e45eb65ff4eb9a591bc7ca4a3a Mon Sep 17 00:00:00 2001 From: Li Jin Date: Tue, 21 Apr 2020 15:26:48 +0800 Subject: allow implicit return block macro, fix compiled Lua codes searching issue. --- src/MoonP/moon_ast.h | 1 + src/MoonP/moon_compiler.cpp | 27 ++++++++++++++++----------- src/MoonP/moon_compiler.h | 2 +- src/MoonP/moonplus.h | 6 +++--- src/MoonP/stacktraceplus.h | 13 +++++++++++-- 5 files changed, 32 insertions(+), 17 deletions(-) (limited to 'src') 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) AST_END(ExpList) AST_NODE(Return) + bool allowBlockMacroReturn = false; ast_ptr valueList; AST_MEMBER(Return, &valueList) AST_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) { return std::string(sv); } -const char* version() { - return "0.3.8"; +const std::string_view version() { + return "0.3.9"sv; } // name of table stored in lua registry @@ -2013,6 +2013,7 @@ private: expListLow->exprs.dup(expList->exprs); auto returnNode = x->new_ptr(); returnNode->valueList.set(expListLow); + returnNode->allowBlockMacroReturn = true; last->content.set(returnNode); BLOCK_END break; @@ -2274,10 +2275,9 @@ private: transformUnless(static_cast(value), out, ExpUsage::Return); return; } - } - if (auto chainValue = singleValue->item.as()) { + } else if (auto chainValue = singleValue->item.as()) { if (specialChainValue(chainValue) != ChainType::Common) { - transformChainValue(chainValue, out, ExpUsage::Return); + transformChainValue(chainValue, out, ExpUsage::Return, nullptr, returnNode->allowBlockMacroReturn); return; } } @@ -3015,7 +3015,7 @@ private: return {type, codes}; } - std::tuple, std::unique_ptr, std::string> expandMacro(ChainValue_t* chainValue, ExpUsage usage) { + std::tuple, std::unique_ptr, std::string> expandMacro(ChainValue_t* chainValue, ExpUsage usage, bool allowBlockMacroReturn) { auto x = ast_to(chainValue->items.front())->item.to(); const auto& chainList = chainValue->items.objects(); std::string type, codes; @@ -3033,8 +3033,7 @@ private: throw std::logic_error(_info.errorMessage(err, x)); } return {nullptr, nullptr, std::move(codes)}; - } - if (type != targetType) { + } else if (!allowBlockMacroReturn && type != targetType) { throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); } ParseInfo info; @@ -3047,6 +3046,12 @@ private: } else { info = _parser.parse(codes); } + } else if (allowBlockMacroReturn) { + if (type == "expr"sv) { + info = _parser.parse(codes); + } else { + info = _parser.parse(codes); + } } else { info = _parser.parse(codes); } @@ -3100,12 +3105,12 @@ private: return {info.node, std::move(info.codes), Empty}; } - void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { + void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) { if (isMacroChain(chainValue)) { ast_ptr node; std::unique_ptr codes; std::string luaCodes; - std::tie(node, codes, luaCodes) = expandMacro(chainValue, usage); + std::tie(node, codes, luaCodes) = expandMacro(chainValue, usage, allowBlockMacroReturn); Utils::replace(luaCodes, "\r\n"sv, "\n"sv); Utils::trim(luaCodes); if (!node && !codes) { @@ -3118,7 +3123,7 @@ private: out.push_back(luaCodes); return; } - if (usage == ExpUsage::Common) { + if (usage == ExpUsage::Common || (usage == ExpUsage::Return && node.is())) { transformBlock(node.to(), out, usage, assignList); } else { 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 namespace MoonP { -const char* version(); +const std::string_view version(); struct MoonConfig { 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 = { loadstring = loadstring, load = load } -local dirsep, split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath -dirsep = "/" +local split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath +moonp.dirsep = "/" moonp.moon_compiled = { } moonp.file_exist = function(fname) local file = io.open(fname) @@ -103,7 +103,7 @@ find_modulepath = function(name) if not package.moonpath then package.moonpath = create_moonpath(package.path) end - local name_path = name:gsub("%.", dirsep) + local name_path = name:gsub("%.", moonp.dirsep) local file_exist, file_path for path in package.moonpath:gmatch("[^;]+") do 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) source = moonCompiled["@="..fname] end if not source then - if moonp.file_exist(fname) then - local codes = moonp.read_file(fname) + local name_path = fname:gsub("%.", moonp.dirsep) + local file_exist, file_path + for path in package.path:gmatch("[^;]+") do + file_path = path:gsub("?", name_path) + file_exist = moonp.file_exist(file_path) + if file_exist then + break + end + end + if file_exist then + local codes = moonp.read_file(file_path) local moonFile = codes:match("^%s*--%s*%[moon%]:%s*([^\n]*)") if moonFile then fname = moonFile:gsub("^%s*(.-)%s*$", "%1") -- cgit v1.2.3-55-g6feb