aboutsummaryrefslogtreecommitdiff
path: root/src/moonp.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-02-07 17:29:34 +0800
committerLi Jin <dragon-fly@qq.com>2020-02-07 17:29:34 +0800
commitc241ea241e8e9c152f6eb14f163b2ae39749f7bf (patch)
tree2fd05ca6866ea60ca778fb6ff31c7ec429e138c4 /src/moonp.cpp
parent2e50c15bfe67d4709880a0377d37fca191be2f3e (diff)
downloadyuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.tar.gz
yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.tar.bz2
yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.zip
releasing moonplus as a lib.
Diffstat (limited to '')
-rw-r--r--src/moonp.cpp (renamed from src/moonc.cpp)128
1 files changed, 121 insertions, 7 deletions
diff --git a/src/moonc.cpp b/src/moonp.cpp
index d04ce47..a6c99df 100644
--- a/src/moonc.cpp
+++ b/src/moonp.cpp
@@ -13,9 +13,11 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
13#include "MoonP/moon_compiler.h" 13#include "MoonP/moon_compiler.h"
14#include "MoonP/moon_parser.h" 14#include "MoonP/moon_parser.h"
15 15
16#ifndef LIBMOONP
17
16int main(int narg, const char** args) { 18int main(int narg, const char** args) {
17 const char* help = 19 const char* help =
18"Usage: moonc [options|files] ...\n\n" 20"Usage: moonp [options|files] ...\n\n"
19" -h Print this message\n" 21" -h Print this message\n"
20" -t path Specify where to place compiled files\n" 22" -t path Specify where to place compiled files\n"
21" -o file Write output to file\n" 23" -o file Write output to file\n"
@@ -82,7 +84,7 @@ int main(int narg, const char** args) {
82 std::cout << "Error: -o can not be used with multiple input files.\n"; 84 std::cout << "Error: -o can not be used with multiple input files.\n";
83 std::cout << help; 85 std::cout << help;
84 } 86 }
85 if (targetPath.back() != '/' && targetPath.back() != '\\') { 87 if (!targetPath.empty() && targetPath.back() != '/' && targetPath.back() != '\\') {
86 targetPath.append("/"); 88 targetPath.append("/");
87 } 89 }
88 std::list<std::future<std::result_of_t<std::decay_t<int()>()>>> results; 90 std::list<std::future<std::result_of_t<std::decay_t<int()>()>>> results;
@@ -121,14 +123,9 @@ int main(int narg, const char** args) {
121 } else { 123 } else {
122 std::string targetFile; 124 std::string targetFile;
123 if (resultFile.empty()) { 125 if (resultFile.empty()) {
124 std::string ext;
125 targetFile = file; 126 targetFile = file;
126 size_t pos = file.rfind('.'); 127 size_t pos = file.rfind('.');
127 if (pos != std::string::npos) { 128 if (pos != std::string::npos) {
128 ext = file.substr(pos + 1);
129 for (size_t i = 0; i < ext.length(); i++) {
130 ext[i] = static_cast<char>(tolower(ext[i]));
131 }
132 targetFile = file.substr(0, pos) + ".lua"; 129 targetFile = file.substr(0, pos) + ".lua";
133 } 130 }
134 if (!targetPath.empty()) { 131 if (!targetPath.empty()) {
@@ -177,3 +174,120 @@ int main(int narg, const char** args) {
177 return ret; 174 return ret;
178} 175}
179 176
177#else
178
179extern "C" {
180
181#include "lua.h"
182#include "lauxlib.h"
183
184static const char moonplusCodes[] =
185#include "MoonPlus.h"
186
187static int init_moonplus(lua_State* L) {
188 MoonP::MoonConfig config;
189 std::string s(moonplusCodes, sizeof(moonplusCodes) / sizeof(moonplusCodes[0]) - 1);
190 std::string codes, err;
191 MoonP::GlobalVars globals;
192 std::tie(codes, err, globals) = MoonP::moonCompile(s, config);
193 if (codes.empty()) {
194 luaL_error(L, "fail to compile moonplus init codes.\n%s", err.c_str());
195 }
196 int top = lua_gettop(L);
197 if (luaL_loadbuffer(L, codes.c_str(), codes.size(), "=(moonplus)") != 0) {
198 luaL_error(L, "fail to init moonplus module.");
199 } else {
200 lua_call(L, 0, 0);
201 }
202 lua_settop(L, top);
203 return 0;
204}
205
206static const char stpCodes[] =
207#include "StackTracePlus.h"
208
209static int init_stacktraceplus(lua_State* L) {
210 if (luaL_loadbuffer(L, stpCodes, sizeof(stpCodes) / sizeof(stpCodes[0]) - 1, "=(stacktraceplus)") != 0) {
211 luaL_error(L, "fail to init stacktraceplus module.");
212 } else {
213 lua_call(L, 0, 1);
214 }
215 return 1;
216}
217
218static int moontolua(lua_State* L) {
219 size_t size = 0;
220 const char* input = luaL_checklstring(L, 1, &size);
221 MoonP::MoonConfig config;
222 if (lua_gettop(L) == 2) {
223 lua_pushstring(L, "lint_global");
224 lua_gettable(L, -2);
225 if (!lua_isnil(L, -1)) {
226 config.lintGlobalVariable = lua_toboolean(L, -1) != 0;
227 }
228 lua_pop(L, 1);
229 lua_pushstring(L, "implicit_return_root");
230 lua_gettable(L, -2);
231 if (!lua_isnil(L, -1)) {
232 config.implicitReturnRoot = lua_toboolean(L, -1) != 0;
233 }
234 lua_pop(L, 1);
235 lua_pushstring(L, "reserve_line_number");
236 lua_gettable(L, -2);
237 if (!lua_isnil(L, -1)) {
238 config.reserveLineNumber = lua_toboolean(L, -1) != 0;
239 }
240 lua_pop(L, 1);
241 }
242 std::string s(input, size);
243 std::string codes, err;
244 MoonP::GlobalVars globals;
245 std::tie(codes, err, globals) = MoonP::moonCompile(s, config);
246 if (codes.empty()) {
247 lua_pushnil(L);
248 } else {
249 lua_pushlstring(L, codes.c_str(), codes.size());
250 }
251 if (err.empty()) {
252 lua_pushnil(L);
253 } else {
254 lua_pushlstring(L, err.c_str(), err.size());
255 }
256 if (globals && !globals->empty()) {
257 lua_createtable(L, static_cast<int>(globals->size()), 0);
258 int i = 1;
259 for (const auto& var : *globals) {
260 lua_createtable(L, 3, 0);
261 lua_pushlstring(L, var.name.c_str(), var.name.size());
262 lua_rawseti(L, -2, 1);
263 lua_pushinteger(L, var.line);
264 lua_rawseti(L, -2, 2);
265 lua_pushinteger(L, var.col);
266 lua_rawseti(L, -2, 3);
267 lua_rawseti(L, -2, i);
268 i++;
269 }
270 } else {
271 lua_pushnil(L);
272 }
273 return 3;
274}
275
276int luaopen_moonp(lua_State* L) {
277 lua_getglobal(L, "package");
278 lua_getfield(L, -1, "loaded");
279 lua_createtable(L, 0, 0);
280 lua_pushcfunction(L, moontolua);
281 lua_setfield(L, -2, "to_lua");
282 lua_pushcfunction(L, init_stacktraceplus);
283 lua_setfield(L, -2, "load_stacktraceplus");
284 lua_setfield(L, -2, "moonp");
285 lua_pop(L, 2);
286 init_moonplus(L);
287 return 0;
288}
289
290} // extern "C"
291
292#endif // LIBMOONP
293