aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-09-17 11:43:14 +0800
committerLi Jin <dragon-fly@qq.com>2020-09-17 11:43:14 +0800
commitbf39ea1dbbe6184c62cafc0249402f4082331de4 (patch)
treeaf0e139d2acb9d0fd49d0a9ffa84a471ce1f265c
parent02b6afeb2b92c4471cb954209c4ac9195150ea23 (diff)
downloadyuescript-bf39ea1dbbe6184c62cafc0249402f4082331de4.tar.gz
yuescript-bf39ea1dbbe6184c62cafc0249402f4082331de4.tar.bz2
yuescript-bf39ea1dbbe6184c62cafc0249402f4082331de4.zip
add a flag to build moonp without Lua.
-rw-r--r--src/moonp.cpp44
1 files changed, 33 insertions, 11 deletions
diff --git a/src/moonp.cpp b/src/moonp.cpp
index 94cba03..e9ea5c2 100644
--- a/src/moonp.cpp
+++ b/src/moonp.cpp
@@ -24,16 +24,16 @@ using namespace std::string_view_literals;
24#include "ghc/fs_std.hpp" 24#include "ghc/fs_std.hpp"
25#include "linenoise.hpp" 25#include "linenoise.hpp"
26 26
27#define MOONP_COMPILER_ONLY
28
29#ifndef MOONP_NO_MACRO
27#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) 30#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;})
28#define DEFER(code) _DEFER(code,__LINE__) 31#define DEFER(code) _DEFER(code,__LINE__)
29
30extern "C" { 32extern "C" {
31
32#include "lua.h" 33#include "lua.h"
33#include "lauxlib.h" 34#include "lauxlib.h"
34#include "lualib.h" 35#include "lualib.h"
35int luaopen_moonp(lua_State* L); 36int luaopen_moonp(lua_State* L);
36
37} // extern "C" 37} // extern "C"
38 38
39static void openlibs(void* state) { 39static void openlibs(void* state) {
@@ -74,6 +74,12 @@ void pushOptions(lua_State* L, int lineOffset) {
74 lua_rawset(L, -3); 74 lua_rawset(L, -3);
75} 75}
76 76
77#define MOONP_ARGS nullptr,openlibs
78#else
79#define MOONP_ARGS
80#endif // MOONP_NO_MACRO
81
82#ifndef MOONP_COMPILER_ONLY
77static const char luaminifyCodes[] = 83static const char luaminifyCodes[] =
78#include "LuaMinify.h" 84#include "LuaMinify.h"
79 85
@@ -86,24 +92,31 @@ static void pushLuaminify(lua_State* L) {
86 luaL_error(L, err.c_str()); 92 luaL_error(L, err.c_str());
87 } 93 }
88} 94}
95#endif // MOONP_COMPILER_ONLY
89 96
90int main(int narg, const char** args) { 97int main(int narg, const char** args) {
91 const char* help = 98 const char* help =
92"Usage: moonp [options|files|directories] ...\n\n" 99"Usage: moonp [options|files|directories] ...\n\n"
93" -h Print this message\n" 100" -h Print this message\n"
101#ifndef MOONP_COMPILER_ONLY
94" -e str Execute a file or raw codes\n" 102" -e str Execute a file or raw codes\n"
103" -m Generate minified codes\n"
104#endif // MOONP_COMPILER_ONLY
95" -t path Specify where to place compiled files\n" 105" -t path Specify where to place compiled files\n"
96" -o file Write output to file\n" 106" -o file Write output to file\n"
97" -s Use spaces in generated codes instead of tabs\n" 107" -s Use spaces in generated codes instead of tabs\n"
98" -m Generate minified codes\n"
99" -p Write output to standard out\n" 108" -p Write output to standard out\n"
100" -b Dump compile time (doesn't write output)\n" 109" -b Dump compile time (doesn't write output)\n"
101" -l Write line numbers from source codes\n" 110" -l Write line numbers from source codes\n"
102" -v Print version\n" 111" -v Print version\n"
112#ifndef MOONP_COMPILER_ONLY
103" -- Read from standard in, print to standard out\n" 113" -- Read from standard in, print to standard out\n"
104" (Must be first and only argument)\n\n" 114" (Must be first and only argument)\n\n"
105" Execute without options to enter REPL, type symbol '$'\n" 115" Execute without options to enter REPL, type symbol '$'\n"
106" in a single line to start/stop multi-line mode\n"; 116" in a single line to start/stop multi-line mode\n"
117#endif // MOONP_COMPILER_ONLY
118;
119#ifndef MOONP_COMPILER_ONLY
107 if (narg == 1) { 120 if (narg == 1) {
108 lua_State* L = luaL_newstate(); 121 lua_State* L = luaL_newstate();
109 openlibs(L); 122 openlibs(L);
@@ -237,6 +250,8 @@ int main(int narg, const char** args) {
237 std::cout << '\n'; 250 std::cout << '\n';
238 return 0; 251 return 0;
239 } 252 }
253 bool minify = false;
254#endif // MOONP_COMPILER_ONLY
240 MoonP::MoonConfig config; 255 MoonP::MoonConfig config;
241 config.implicitReturnRoot = true; 256 config.implicitReturnRoot = true;
242 config.lintGlobalVariable = false; 257 config.lintGlobalVariable = false;
@@ -244,7 +259,6 @@ int main(int narg, const char** args) {
244 config.useSpaceOverTab = false; 259 config.useSpaceOverTab = false;
245 bool writeToFile = true; 260 bool writeToFile = true;
246 bool dumpCompileTime = false; 261 bool dumpCompileTime = false;
247 bool minify = false;
248 std::string targetPath; 262 std::string targetPath;
249 std::string resultFile; 263 std::string resultFile;
250 std::list<std::pair<std::string,std::string>> files; 264 std::list<std::pair<std::string,std::string>> files;
@@ -265,7 +279,7 @@ int main(int narg, const char** args) {
265 conf.lintGlobalVariable = false; 279 conf.lintGlobalVariable = false;
266 conf.reserveLineNumber = false; 280 conf.reserveLineNumber = false;
267 conf.useSpaceOverTab = true; 281 conf.useSpaceOverTab = true;
268 auto result = MoonP::MoonCompiler{nullptr, openlibs}.compile(codes, conf); 282 auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(codes, conf);
269 if (std::get<1>(result).empty()) { 283 if (std::get<1>(result).empty()) {
270 std::cout << std::get<0>(result); 284 std::cout << std::get<0>(result);
271 return 0; 285 return 0;
@@ -274,6 +288,7 @@ int main(int narg, const char** args) {
274 std::cout << std::get<1>(result) << '\n'; 288 std::cout << std::get<1>(result) << '\n';
275 return 1; 289 return 1;
276 } 290 }
291#ifndef MOONP_COMPILER_ONLY
277 } else if (arg == "-e"sv) { 292 } else if (arg == "-e"sv) {
278 ++i; 293 ++i;
279 if (i < narg) { 294 if (i < narg) {
@@ -330,14 +345,15 @@ int main(int narg, const char** args) {
330 std::cout << help; 345 std::cout << help;
331 return 1; 346 return 1;
332 } 347 }
348 } else if (arg == "-m"sv) {
349 minify = true;
350#endif // MOONP_COMPILER_ONLY
333 } else if (arg == "-s"sv) { 351 } else if (arg == "-s"sv) {
334 config.useSpaceOverTab = true; 352 config.useSpaceOverTab = true;
335 } else if (arg == "-l"sv) { 353 } else if (arg == "-l"sv) {
336 config.reserveLineNumber = true; 354 config.reserveLineNumber = true;
337 } else if (arg == "-p"sv) { 355 } else if (arg == "-p"sv) {
338 writeToFile = false; 356 writeToFile = false;
339 } else if (arg == "-m"sv) {
340 minify = true;
341 } else if (arg == "-t"sv) { 357 } else if (arg == "-t"sv) {
342 ++i; 358 ++i;
343 if (i < narg) { 359 if (i < narg) {
@@ -396,7 +412,7 @@ int main(int narg, const char** args) {
396 std::istreambuf_iterator<char>()); 412 std::istreambuf_iterator<char>());
397 if (dumpCompileTime) { 413 if (dumpCompileTime) {
398 auto start = std::chrono::high_resolution_clock::now(); 414 auto start = std::chrono::high_resolution_clock::now();
399 auto result = MoonP::MoonCompiler{nullptr, openlibs}.compile(s, config); 415 auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config);
400 auto end = std::chrono::high_resolution_clock::now(); 416 auto end = std::chrono::high_resolution_clock::now();
401 if (!std::get<0>(result).empty()) { 417 if (!std::get<0>(result).empty()) {
402 std::chrono::duration<double> diff = end - start; 418 std::chrono::duration<double> diff = end - start;
@@ -416,7 +432,7 @@ int main(int narg, const char** args) {
416 return std::tuple{1, file.first, buf.str()}; 432 return std::tuple{1, file.first, buf.str()};
417 } 433 }
418 } 434 }
419 auto result = MoonP::MoonCompiler{nullptr, openlibs}.compile(s, config); 435 auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config);
420 if (std::get<1>(result).empty()) { 436 if (std::get<1>(result).empty()) {
421 if (!writeToFile) { 437 if (!writeToFile) {
422 return std::tuple{0, file.first, std::get<0>(result) + '\n'}; 438 return std::tuple{0, file.first, std::get<0>(result) + '\n'};
@@ -461,6 +477,7 @@ int main(int narg, const char** args) {
461 results.push_back(std::move(task)); 477 results.push_back(std::move(task));
462 } 478 }
463 int ret = 0; 479 int ret = 0;
480#ifndef MOONP_COMPILER_ONLY
464 lua_State* L = nullptr; 481 lua_State* L = nullptr;
465 DEFER({ 482 DEFER({
466 if (L) lua_close(L); 483 if (L) lua_close(L);
@@ -470,6 +487,7 @@ int main(int narg, const char** args) {
470 luaL_openlibs(L); 487 luaL_openlibs(L);
471 pushLuaminify(L); 488 pushLuaminify(L);
472 } 489 }
490#endif // MOONP_COMPILER_ONLY
473 std::list<std::string> errs; 491 std::list<std::string> errs;
474 for (auto& result : results) { 492 for (auto& result : results) {
475 int val = 0; 493 int val = 0;
@@ -480,6 +498,7 @@ int main(int narg, const char** args) {
480 ret = val; 498 ret = val;
481 errs.push_back(msg); 499 errs.push_back(msg);
482 } else { 500 } else {
501#ifndef MOONP_COMPILER_ONLY
483 if (minify) { 502 if (minify) {
484 std::ifstream input(file, std::ios::in); 503 std::ifstream input(file, std::ios::in);
485 if (input) { 504 if (input) {
@@ -519,6 +538,9 @@ int main(int narg, const char** args) {
519 } else { 538 } else {
520 std::cout << msg; 539 std::cout << msg;
521 } 540 }
541#else
542 std::cout << msg;
543#endif // MOONP_COMPILER_ONLY
522 } 544 }
523 } 545 }
524 for (const auto& err : errs) { 546 for (const auto& err : errs) {