From 02b6afeb2b92c4471cb954209c4ac9195150ea23 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 17 Sep 2020 11:17:49 +0800 Subject: add a compiler flag to disable macro feature and support linking without Lua. --- src/MoonP/moon_compiler.cpp | 51 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 112cae7..510b47e 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -17,20 +17,25 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include "MoonP/moon_parser.h" #include "MoonP/moon_compiler.h" -extern "C" { +#ifndef MOONP_NO_MACRO +extern "C" { #include "lua.h" #include "lauxlib.h" #include "lualib.h" - } // extern "C" +// name of table stored in lua registry +#define MOONP_MODULE "__moon_modules__" + #if LUA_VERSION_NUM > 501 #ifndef LUA_COMPAT_5_1 #define lua_objlen lua_rawlen #endif // LUA_COMPAT_5_1 #endif // LUA_VERSION_NUM +#endif // MOONP_NO_MACRO + namespace MoonP { using namespace std::string_view_literals; using namespace parserlib; @@ -52,11 +57,9 @@ const std::string_view version() { return "0.4.14"sv; } -// name of table stored in lua registry -#define MOONP_MODULE "__moon_modules__" - class MoonCompilerImpl { public: +#ifndef MOONP_NO_MACRO MoonCompilerImpl(lua_State* sharedState, const std::function& luaOpen, bool sameModule, @@ -85,6 +88,7 @@ public: L = nullptr; } } +#endif // MOONP_NO_MACRO std::tuple compile(std::string_view codes, const MoonConfig& config) { _config = config; @@ -131,6 +135,7 @@ public: _withVars = {}; _continueVars = {}; _enableReturn = {}; +#ifndef MOONP_NO_MACRO if (_useModule) { _useModule = false; if (!_sameModule) { @@ -143,12 +148,16 @@ public: lua_rawseti(L, -2, idx); // tb[idx] = nil, tb } } +#endif // MOONP_NO_MACRO } private: +#ifndef MOONP_NO_MACRO bool _stateOwner = false; bool _useModule = false; bool _sameModule = false; lua_State* L = nullptr; + std::function _luaOpen; +#endif // MOONP_NO_MACRO MoonConfig _config; MoonParser _parser; ParseInfo _info; @@ -162,7 +171,6 @@ private: std::ostringstream _buf; std::ostringstream _joinBuf; const std::string _newLine = "\n"; - std::function _luaOpen; std::string _moduleName; enum class LocalMode { @@ -2142,6 +2150,7 @@ private: } } +#ifndef MOONP_NO_MACRO void pushCurrentModule() { if (_useModule) { lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE @@ -2305,6 +2314,11 @@ private: lua_rawset(L, -3); // cur[name] = macro, cur out.push_back(Empty); } +#else + void transformMacro(Macro_t* macro, str_list& out, bool exporting) { + throw std::logic_error(_info.errorMessage("macro feature not supported"sv, macro)); + } +#endif // MOONP_NO_MACRO void transformReturn(Return_t* returnNode, str_list& out) { if (!_enableReturn.top()) { @@ -3009,6 +3023,7 @@ private: } } +#ifndef MOONP_NO_MACRO std::pair expandMacroStr(ChainValue_t* chainValue) { const auto& chainList = chainValue->items.objects(); auto x = ast_to(chainList.front())->item.to(); @@ -3183,9 +3198,11 @@ private: } return {info.node, std::move(info.codes), Empty}; } +#endif // MOONP_NO_MACRO void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) { if (isMacroChain(chainValue)) { +#ifndef MOONP_NO_MACRO ast_ptr node; std::unique_ptr codes; std::string luaCodes; @@ -3230,6 +3247,9 @@ private: } } return; +#else + throw std::logic_error(_info.errorMessage("macro feature not supported"sv, chainValue)); +#endif // MOONP_NO_MACRO } const auto& chainList = chainValue->items.objects(); if (transformChainEndWithEOP(chainList, out, usage, assignList)) { @@ -4894,6 +4914,7 @@ private: } if (auto tableLit = import->target.as()) { auto newTab = x->new_ptr(); +#ifndef MOONP_NO_MACRO std::list> macroPairs; for (auto item : tableLit->values.objects()) { switch (item->getId()) { @@ -4963,6 +4984,20 @@ private: lua_setfield(L, -3, pair.second.c_str()); } } +#else // MOONP_NO_MACRO + for (auto item : tableLit->values.objects()) { + switch (item->getId()) { + case id(): + case id(): { + throw std::logic_error(_info.errorMessage("macro feature not supported"sv, item)); + break; + } + default: + newTab->values.push_back(item); + break; + } + } +#endif // MOONP_NO_MACRO if (newTab->values.empty()) { out.push_back(Empty); return; @@ -5261,7 +5296,11 @@ const std::string MoonCompilerImpl::Empty; MoonCompiler::MoonCompiler(void* sharedState, const std::function& luaOpen, bool sameModule): +#ifndef MOONP_NO_MACRO _compiler(std::make_unique(static_cast(sharedState), luaOpen, sameModule)) {} +#else +_compiler(std::make_unique()) {} +#endif // MOONP_NO_MACRO MoonCompiler::~MoonCompiler() {} -- cgit v1.2.3-55-g6feb