diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-10-21 23:44:50 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-10-21 23:44:50 +0800 |
| commit | b6725202f4a8cac5f829dac9a72a81f3ff73e787 (patch) | |
| tree | c173accb869b60cba14babc7685284864bc80426 /src/moonp.cpp | |
| parent | 0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c (diff) | |
| download | yuescript-b6725202f4a8cac5f829dac9a72a81f3ff73e787.tar.gz yuescript-b6725202f4a8cac5f829dac9a72a81f3ff73e787.tar.bz2 yuescript-b6725202f4a8cac5f829dac9a72a81f3ff73e787.zip | |
extend macro feature to support compiling Moonscript to other Lua dialect like teal.
add examples for how to write MoonPlus codes that compile to teal.
fix C++ macro to build without MoonPlus macro feature or built-in Lua.
add support for passing arguments from command line to compiler that can be accessed or altered by "require('moonp').options".
Diffstat (limited to 'src/moonp.cpp')
| -rw-r--r-- | src/moonp.cpp | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/moonp.cpp b/src/moonp.cpp index 25be015..1e4302e 100644 --- a/src/moonp.cpp +++ b/src/moonp.cpp | |||
| @@ -24,7 +24,7 @@ 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 | #ifndef MOONP_NO_MACRO | 27 | #if not (defined MOONP_NO_MACRO && defined MOONP_COMPILER_ONLY) |
| 28 | #define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) | 28 | #define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) |
| 29 | #define DEFER(code) _DEFER(code,__LINE__) | 29 | #define DEFER(code) _DEFER(code,__LINE__) |
| 30 | extern "C" { | 30 | extern "C" { |
| @@ -71,7 +71,9 @@ void pushOptions(lua_State* L, int lineOffset) { | |||
| 71 | lua_pushinteger(L, lineOffset); | 71 | lua_pushinteger(L, lineOffset); |
| 72 | lua_rawset(L, -3); | 72 | lua_rawset(L, -3); |
| 73 | } | 73 | } |
| 74 | #endif // not (defined MOONP_NO_MACRO && defined MOONP_COMPILER_ONLY) | ||
| 74 | 75 | ||
| 76 | #ifndef MOONP_NO_MACRO | ||
| 75 | #define MOONP_ARGS nullptr,openlibs | 77 | #define MOONP_ARGS nullptr,openlibs |
| 76 | #else | 78 | #else |
| 77 | #define MOONP_ARGS | 79 | #define MOONP_ARGS |
| @@ -278,12 +280,12 @@ int main(int narg, const char** args) { | |||
| 278 | conf.reserveLineNumber = false; | 280 | conf.reserveLineNumber = false; |
| 279 | conf.useSpaceOverTab = true; | 281 | conf.useSpaceOverTab = true; |
| 280 | auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(codes, conf); | 282 | auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(codes, conf); |
| 281 | if (std::get<1>(result).empty()) { | 283 | if (result.error.empty()) { |
| 282 | std::cout << std::get<0>(result); | 284 | std::cout << result.codes; |
| 283 | return 0; | 285 | return 0; |
| 284 | } else { | 286 | } else { |
| 285 | std::ostringstream buf; | 287 | std::ostringstream buf; |
| 286 | std::cout << std::get<1>(result) << '\n'; | 288 | std::cout << result.error << '\n'; |
| 287 | return 1; | 289 | return 1; |
| 288 | } | 290 | } |
| 289 | #ifndef MOONP_COMPILER_ONLY | 291 | #ifndef MOONP_COMPILER_ONLY |
| @@ -376,6 +378,12 @@ int main(int narg, const char** args) { | |||
| 376 | std::cout << help; | 378 | std::cout << help; |
| 377 | return 1; | 379 | return 1; |
| 378 | } | 380 | } |
| 381 | } else if (arg.substr(0, 1) == "-"sv && arg.find('=') != std::string::npos) { | ||
| 382 | auto argStr = arg.substr(1); | ||
| 383 | size_t idx = argStr.find('='); | ||
| 384 | auto key = argStr.substr(0, idx); | ||
| 385 | auto value = argStr.substr(idx + 1); | ||
| 386 | config.options[key] = value; | ||
| 379 | } else { | 387 | } else { |
| 380 | if (fs::is_directory(arg)) { | 388 | if (fs::is_directory(arg)) { |
| 381 | for (auto item : fs::recursive_directory_iterator(arg)) { | 389 | for (auto item : fs::recursive_directory_iterator(arg)) { |
| @@ -412,7 +420,7 @@ int main(int narg, const char** args) { | |||
| 412 | auto start = std::chrono::high_resolution_clock::now(); | 420 | auto start = std::chrono::high_resolution_clock::now(); |
| 413 | auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config); | 421 | auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config); |
| 414 | auto end = std::chrono::high_resolution_clock::now(); | 422 | auto end = std::chrono::high_resolution_clock::now(); |
| 415 | if (!std::get<0>(result).empty()) { | 423 | if (!result.codes.empty()) { |
| 416 | std::chrono::duration<double> diff = end - start; | 424 | std::chrono::duration<double> diff = end - start; |
| 417 | start = std::chrono::high_resolution_clock::now(); | 425 | start = std::chrono::high_resolution_clock::now(); |
| 418 | MoonP::MoonParser{}.parse<MoonP::File_t>(s); | 426 | MoonP::MoonParser{}.parse<MoonP::File_t>(s); |
| @@ -426,15 +434,22 @@ int main(int narg, const char** args) { | |||
| 426 | } else { | 434 | } else { |
| 427 | std::ostringstream buf; | 435 | std::ostringstream buf; |
| 428 | buf << "Fail to compile: "sv << file.first << ".\n"sv; | 436 | buf << "Fail to compile: "sv << file.first << ".\n"sv; |
| 429 | buf << std::get<1>(result) << '\n'; | 437 | buf << result.error << '\n'; |
| 430 | return std::tuple{1, file.first, buf.str()}; | 438 | return std::tuple{1, file.first, buf.str()}; |
| 431 | } | 439 | } |
| 432 | } | 440 | } |
| 433 | auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config); | 441 | auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config); |
| 434 | if (std::get<1>(result).empty()) { | 442 | if (result.error.empty()) { |
| 435 | if (!writeToFile) { | 443 | if (!writeToFile) { |
| 436 | return std::tuple{0, file.first, std::get<0>(result) + '\n'}; | 444 | return std::tuple{0, file.first, result.codes + '\n'}; |
| 437 | } else { | 445 | } else { |
| 446 | std::string targetExtension("lua"sv); | ||
| 447 | if (result.options) { | ||
| 448 | auto it = result.options->find("target_extension"); | ||
| 449 | if (it != result.options->end()) { | ||
| 450 | targetExtension = it->second; | ||
| 451 | } | ||
| 452 | } | ||
| 438 | fs::path targetFile; | 453 | fs::path targetFile; |
| 439 | if (!resultFile.empty()) { | 454 | if (!resultFile.empty()) { |
| 440 | targetFile = resultFile; | 455 | targetFile = resultFile; |
| @@ -444,14 +459,14 @@ int main(int narg, const char** args) { | |||
| 444 | } else { | 459 | } else { |
| 445 | targetFile = file.first; | 460 | targetFile = file.first; |
| 446 | } | 461 | } |
| 447 | targetFile.replace_extension(".lua"sv); | 462 | targetFile.replace_extension('.' + targetExtension); |
| 448 | } | 463 | } |
| 449 | if (!targetPath.empty()) { | 464 | if (!targetPath.empty()) { |
| 450 | fs::create_directories(targetFile.parent_path()); | 465 | fs::create_directories(targetFile.parent_path()); |
| 451 | } | 466 | } |
| 452 | std::ofstream output(targetFile, std::ios::trunc | std::ios::out); | 467 | std::ofstream output(targetFile, std::ios::trunc | std::ios::out); |
| 453 | if (output) { | 468 | if (output) { |
| 454 | const auto& codes = std::get<0>(result); | 469 | const auto& codes = result.codes; |
| 455 | if (config.reserveLineNumber) { | 470 | if (config.reserveLineNumber) { |
| 456 | auto head = std::string("-- [moonp]: "sv) + file.first + '\n'; | 471 | auto head = std::string("-- [moonp]: "sv) + file.first + '\n'; |
| 457 | output.write(head.c_str(), head.size()); | 472 | output.write(head.c_str(), head.size()); |
| @@ -465,7 +480,7 @@ int main(int narg, const char** args) { | |||
| 465 | } else { | 480 | } else { |
| 466 | std::ostringstream buf; | 481 | std::ostringstream buf; |
| 467 | buf << "Fail to compile: "sv << file.first << ".\n"; | 482 | buf << "Fail to compile: "sv << file.first << ".\n"; |
| 468 | buf << std::get<1>(result) << '\n'; | 483 | buf << result.error << '\n'; |
| 469 | return std::tuple{1, std::string(), buf.str()}; | 484 | return std::tuple{1, std::string(), buf.str()}; |
| 470 | } | 485 | } |
| 471 | } else { | 486 | } else { |
