diff options
author | Li Jin <dragon-fly@qq.com> | 2020-09-17 11:17:49 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-09-17 11:17:49 +0800 |
commit | 02b6afeb2b92c4471cb954209c4ac9195150ea23 (patch) | |
tree | 1962e5b8f8c8b143587ecbfbcd62568588cf15f8 | |
parent | fef716c4c4ff28822409f8ce05a05488da15fa76 (diff) | |
download | yuescript-02b6afeb2b92c4471cb954209c4ac9195150ea23.tar.gz yuescript-02b6afeb2b92c4471cb954209c4ac9195150ea23.tar.bz2 yuescript-02b6afeb2b92c4471cb954209c4ac9195150ea23.zip |
add a compiler flag to disable macro feature and support linking without Lua.
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 51 |
1 files changed, 45 insertions, 6 deletions
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 | |||
17 | #include "MoonP/moon_parser.h" | 17 | #include "MoonP/moon_parser.h" |
18 | #include "MoonP/moon_compiler.h" | 18 | #include "MoonP/moon_compiler.h" |
19 | 19 | ||
20 | extern "C" { | 20 | #ifndef MOONP_NO_MACRO |
21 | 21 | ||
22 | extern "C" { | ||
22 | #include "lua.h" | 23 | #include "lua.h" |
23 | #include "lauxlib.h" | 24 | #include "lauxlib.h" |
24 | #include "lualib.h" | 25 | #include "lualib.h" |
25 | |||
26 | } // extern "C" | 26 | } // extern "C" |
27 | 27 | ||
28 | // name of table stored in lua registry | ||
29 | #define MOONP_MODULE "__moon_modules__" | ||
30 | |||
28 | #if LUA_VERSION_NUM > 501 | 31 | #if LUA_VERSION_NUM > 501 |
29 | #ifndef LUA_COMPAT_5_1 | 32 | #ifndef LUA_COMPAT_5_1 |
30 | #define lua_objlen lua_rawlen | 33 | #define lua_objlen lua_rawlen |
31 | #endif // LUA_COMPAT_5_1 | 34 | #endif // LUA_COMPAT_5_1 |
32 | #endif // LUA_VERSION_NUM | 35 | #endif // LUA_VERSION_NUM |
33 | 36 | ||
37 | #endif // MOONP_NO_MACRO | ||
38 | |||
34 | namespace MoonP { | 39 | namespace MoonP { |
35 | using namespace std::string_view_literals; | 40 | using namespace std::string_view_literals; |
36 | using namespace parserlib; | 41 | using namespace parserlib; |
@@ -52,11 +57,9 @@ const std::string_view version() { | |||
52 | return "0.4.14"sv; | 57 | return "0.4.14"sv; |
53 | } | 58 | } |
54 | 59 | ||
55 | // name of table stored in lua registry | ||
56 | #define MOONP_MODULE "__moon_modules__" | ||
57 | |||
58 | class MoonCompilerImpl { | 60 | class MoonCompilerImpl { |
59 | public: | 61 | public: |
62 | #ifndef MOONP_NO_MACRO | ||
60 | MoonCompilerImpl(lua_State* sharedState, | 63 | MoonCompilerImpl(lua_State* sharedState, |
61 | const std::function<void(void*)>& luaOpen, | 64 | const std::function<void(void*)>& luaOpen, |
62 | bool sameModule, | 65 | bool sameModule, |
@@ -85,6 +88,7 @@ public: | |||
85 | L = nullptr; | 88 | L = nullptr; |
86 | } | 89 | } |
87 | } | 90 | } |
91 | #endif // MOONP_NO_MACRO | ||
88 | 92 | ||
89 | std::tuple<std::string,std::string,GlobalVars> compile(std::string_view codes, const MoonConfig& config) { | 93 | std::tuple<std::string,std::string,GlobalVars> compile(std::string_view codes, const MoonConfig& config) { |
90 | _config = config; | 94 | _config = config; |
@@ -131,6 +135,7 @@ public: | |||
131 | _withVars = {}; | 135 | _withVars = {}; |
132 | _continueVars = {}; | 136 | _continueVars = {}; |
133 | _enableReturn = {}; | 137 | _enableReturn = {}; |
138 | #ifndef MOONP_NO_MACRO | ||
134 | if (_useModule) { | 139 | if (_useModule) { |
135 | _useModule = false; | 140 | _useModule = false; |
136 | if (!_sameModule) { | 141 | if (!_sameModule) { |
@@ -143,12 +148,16 @@ public: | |||
143 | lua_rawseti(L, -2, idx); // tb[idx] = nil, tb | 148 | lua_rawseti(L, -2, idx); // tb[idx] = nil, tb |
144 | } | 149 | } |
145 | } | 150 | } |
151 | #endif // MOONP_NO_MACRO | ||
146 | } | 152 | } |
147 | private: | 153 | private: |
154 | #ifndef MOONP_NO_MACRO | ||
148 | bool _stateOwner = false; | 155 | bool _stateOwner = false; |
149 | bool _useModule = false; | 156 | bool _useModule = false; |
150 | bool _sameModule = false; | 157 | bool _sameModule = false; |
151 | lua_State* L = nullptr; | 158 | lua_State* L = nullptr; |
159 | std::function<void(void*)> _luaOpen; | ||
160 | #endif // MOONP_NO_MACRO | ||
152 | MoonConfig _config; | 161 | MoonConfig _config; |
153 | MoonParser _parser; | 162 | MoonParser _parser; |
154 | ParseInfo _info; | 163 | ParseInfo _info; |
@@ -162,7 +171,6 @@ private: | |||
162 | std::ostringstream _buf; | 171 | std::ostringstream _buf; |
163 | std::ostringstream _joinBuf; | 172 | std::ostringstream _joinBuf; |
164 | const std::string _newLine = "\n"; | 173 | const std::string _newLine = "\n"; |
165 | std::function<void(void*)> _luaOpen; | ||
166 | std::string _moduleName; | 174 | std::string _moduleName; |
167 | 175 | ||
168 | enum class LocalMode { | 176 | enum class LocalMode { |
@@ -2142,6 +2150,7 @@ private: | |||
2142 | } | 2150 | } |
2143 | } | 2151 | } |
2144 | 2152 | ||
2153 | #ifndef MOONP_NO_MACRO | ||
2145 | void pushCurrentModule() { | 2154 | void pushCurrentModule() { |
2146 | if (_useModule) { | 2155 | if (_useModule) { |
2147 | lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE | 2156 | lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE |
@@ -2305,6 +2314,11 @@ private: | |||
2305 | lua_rawset(L, -3); // cur[name] = macro, cur | 2314 | lua_rawset(L, -3); // cur[name] = macro, cur |
2306 | out.push_back(Empty); | 2315 | out.push_back(Empty); |
2307 | } | 2316 | } |
2317 | #else | ||
2318 | void transformMacro(Macro_t* macro, str_list& out, bool exporting) { | ||
2319 | throw std::logic_error(_info.errorMessage("macro feature not supported"sv, macro)); | ||
2320 | } | ||
2321 | #endif // MOONP_NO_MACRO | ||
2308 | 2322 | ||
2309 | void transformReturn(Return_t* returnNode, str_list& out) { | 2323 | void transformReturn(Return_t* returnNode, str_list& out) { |
2310 | if (!_enableReturn.top()) { | 2324 | if (!_enableReturn.top()) { |
@@ -3009,6 +3023,7 @@ private: | |||
3009 | } | 3023 | } |
3010 | } | 3024 | } |
3011 | 3025 | ||
3026 | #ifndef MOONP_NO_MACRO | ||
3012 | std::pair<std::string,std::string> expandMacroStr(ChainValue_t* chainValue) { | 3027 | std::pair<std::string,std::string> expandMacroStr(ChainValue_t* chainValue) { |
3013 | const auto& chainList = chainValue->items.objects(); | 3028 | const auto& chainList = chainValue->items.objects(); |
3014 | auto x = ast_to<Callable_t>(chainList.front())->item.to<MacroName_t>(); | 3029 | auto x = ast_to<Callable_t>(chainList.front())->item.to<MacroName_t>(); |
@@ -3183,9 +3198,11 @@ private: | |||
3183 | } | 3198 | } |
3184 | return {info.node, std::move(info.codes), Empty}; | 3199 | return {info.node, std::move(info.codes), Empty}; |
3185 | } | 3200 | } |
3201 | #endif // MOONP_NO_MACRO | ||
3186 | 3202 | ||
3187 | void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) { | 3203 | void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) { |
3188 | if (isMacroChain(chainValue)) { | 3204 | if (isMacroChain(chainValue)) { |
3205 | #ifndef MOONP_NO_MACRO | ||
3189 | ast_ptr<false,ast_node> node; | 3206 | ast_ptr<false,ast_node> node; |
3190 | std::unique_ptr<input> codes; | 3207 | std::unique_ptr<input> codes; |
3191 | std::string luaCodes; | 3208 | std::string luaCodes; |
@@ -3230,6 +3247,9 @@ private: | |||
3230 | } | 3247 | } |
3231 | } | 3248 | } |
3232 | return; | 3249 | return; |
3250 | #else | ||
3251 | throw std::logic_error(_info.errorMessage("macro feature not supported"sv, chainValue)); | ||
3252 | #endif // MOONP_NO_MACRO | ||
3233 | } | 3253 | } |
3234 | const auto& chainList = chainValue->items.objects(); | 3254 | const auto& chainList = chainValue->items.objects(); |
3235 | if (transformChainEndWithEOP(chainList, out, usage, assignList)) { | 3255 | if (transformChainEndWithEOP(chainList, out, usage, assignList)) { |
@@ -4894,6 +4914,7 @@ private: | |||
4894 | } | 4914 | } |
4895 | if (auto tableLit = import->target.as<TableLit_t>()) { | 4915 | if (auto tableLit = import->target.as<TableLit_t>()) { |
4896 | auto newTab = x->new_ptr<TableLit_t>(); | 4916 | auto newTab = x->new_ptr<TableLit_t>(); |
4917 | #ifndef MOONP_NO_MACRO | ||
4897 | std::list<std::pair<std::string,std::string>> macroPairs; | 4918 | std::list<std::pair<std::string,std::string>> macroPairs; |
4898 | for (auto item : tableLit->values.objects()) { | 4919 | for (auto item : tableLit->values.objects()) { |
4899 | switch (item->getId()) { | 4920 | switch (item->getId()) { |
@@ -4963,6 +4984,20 @@ private: | |||
4963 | lua_setfield(L, -3, pair.second.c_str()); | 4984 | lua_setfield(L, -3, pair.second.c_str()); |
4964 | } | 4985 | } |
4965 | } | 4986 | } |
4987 | #else // MOONP_NO_MACRO | ||
4988 | for (auto item : tableLit->values.objects()) { | ||
4989 | switch (item->getId()) { | ||
4990 | case id<MacroName_t>(): | ||
4991 | case id<macro_name_pair_t>(): { | ||
4992 | throw std::logic_error(_info.errorMessage("macro feature not supported"sv, item)); | ||
4993 | break; | ||
4994 | } | ||
4995 | default: | ||
4996 | newTab->values.push_back(item); | ||
4997 | break; | ||
4998 | } | ||
4999 | } | ||
5000 | #endif // MOONP_NO_MACRO | ||
4966 | if (newTab->values.empty()) { | 5001 | if (newTab->values.empty()) { |
4967 | out.push_back(Empty); | 5002 | out.push_back(Empty); |
4968 | return; | 5003 | return; |
@@ -5261,7 +5296,11 @@ const std::string MoonCompilerImpl::Empty; | |||
5261 | MoonCompiler::MoonCompiler(void* sharedState, | 5296 | MoonCompiler::MoonCompiler(void* sharedState, |
5262 | const std::function<void(void*)>& luaOpen, | 5297 | const std::function<void(void*)>& luaOpen, |
5263 | bool sameModule): | 5298 | bool sameModule): |
5299 | #ifndef MOONP_NO_MACRO | ||
5264 | _compiler(std::make_unique<MoonCompilerImpl>(static_cast<lua_State*>(sharedState), luaOpen, sameModule)) {} | 5300 | _compiler(std::make_unique<MoonCompilerImpl>(static_cast<lua_State*>(sharedState), luaOpen, sameModule)) {} |
5301 | #else | ||
5302 | _compiler(std::make_unique<MoonCompilerImpl>()) {} | ||
5303 | #endif // MOONP_NO_MACRO | ||
5265 | 5304 | ||
5266 | MoonCompiler::~MoonCompiler() {} | 5305 | MoonCompiler::~MoonCompiler() {} |
5267 | 5306 | ||