aboutsummaryrefslogtreecommitdiff
path: root/src/yue.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-07-24 22:13:08 +0800
committerLi Jin <dragon-fly@qq.com>2022-07-24 22:13:08 +0800
commit303834e1b1e6cd9cae64b66c2ae44dcd7185238f (patch)
tree519f6a1debcc5791d35e06dc5cbb1ce22f9cfd31 /src/yue.cpp
parenteb367126bf3a4f5b0e51ccef93b7c7136bea170e (diff)
downloadyuescript-303834e1b1e6cd9cae64b66c2ae44dcd7185238f.tar.gz
yuescript-303834e1b1e6cd9cae64b66c2ae44dcd7185238f.tar.bz2
yuescript-303834e1b1e6cd9cae64b66c2ae44dcd7185238f.zip
add option --target=5.1 to generate Lua 5.1 compatible codes. add const destructure. make import item const by default.
Diffstat (limited to 'src/yue.cpp')
-rw-r--r--src/yue.cpp39
1 files changed, 23 insertions, 16 deletions
diff --git a/src/yue.cpp b/src/yue.cpp
index 3e30c70..cb27db8 100644
--- a/src/yue.cpp
+++ b/src/yue.cpp
@@ -21,6 +21,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
21#include <string_view> 21#include <string_view>
22#include <memory> 22#include <memory>
23using namespace std::string_view_literals; 23using namespace std::string_view_literals;
24using namespace std::string_literals;
24#include "ghc/fs_std.hpp" 25#include "ghc/fs_std.hpp"
25#include "linenoise.hpp" 26#include "linenoise.hpp"
26 27
@@ -91,10 +92,10 @@ static const char luaminifyCodes[] =
91 92
92static void pushLuaminify(lua_State* L) { 93static void pushLuaminify(lua_State* L) {
93 if (luaL_loadbuffer(L, luaminifyCodes, sizeof(luaminifyCodes) / sizeof(luaminifyCodes[0]) - 1, "=(luaminify)") != 0) { 94 if (luaL_loadbuffer(L, luaminifyCodes, sizeof(luaminifyCodes) / sizeof(luaminifyCodes[0]) - 1, "=(luaminify)") != 0) {
94 std::string err = std::string("failed to load luaminify module.\n") + lua_tostring(L, -1); 95 std::string err = "failed to load luaminify module.\n"s + lua_tostring(L, -1);
95 luaL_error(L, err.c_str()); 96 luaL_error(L, err.c_str());
96 } else if (lua_pcall(L, 0, 1, 0) != 0) { 97 } else if (lua_pcall(L, 0, 1, 0) != 0) {
97 std::string err = std::string("failed to init luaminify module.\n") + lua_tostring(L, -1); 98 std::string err = "failed to init luaminify module.\n"s + lua_tostring(L, -1);
98 luaL_error(L, err.c_str()); 99 luaL_error(L, err.c_str());
99 } 100 }
100} 101}
@@ -209,7 +210,7 @@ int main(int narg, const char** args) {
209 DEFER(lua_settop(L, top)); 210 DEFER(lua_settop(L, top));
210 pushYue(L, "loadstring"sv); 211 pushYue(L, "loadstring"sv);
211 lua_pushlstring(L, codes.c_str(), codes.size()); 212 lua_pushlstring(L, codes.c_str(), codes.size());
212 lua_pushstring(L, (std::string("=(repl ") + std::to_string(count) + ')').c_str()); 213 lua_pushstring(L, ("=(repl "s + std::to_string(count) + ')').c_str());
213 pushOptions(L, -1); 214 pushOptions(L, -1);
214 const std::string_view Err = "\033[35m"sv, Val = "\033[33m"sv, Stop = "\033[0m\n"sv; 215 const std::string_view Err = "\033[35m"sv, Val = "\033[33m"sv, Stop = "\033[0m\n"sv;
215 if (lua_pcall(L, 3, 2, 0) != 0) { 216 if (lua_pcall(L, 3, 2, 0) != 0) {
@@ -218,7 +219,7 @@ int main(int narg, const char** args) {
218 } 219 }
219 if (lua_isnil(L, -2) != 0) { 220 if (lua_isnil(L, -2) != 0) {
220 std::string err = lua_tostring(L, -1); 221 std::string err = lua_tostring(L, -1);
221 auto modName = std::string("(repl "sv) + std::to_string(count) + "):"; 222 auto modName = "(repl "s + std::to_string(count) + "):"s;
222 if (err.substr(0, modName.size()) == modName) { 223 if (err.substr(0, modName.size()) == modName) {
223 err = err.substr(modName.size()); 224 err = err.substr(modName.size());
224 } 225 }
@@ -409,8 +410,8 @@ int main(int narg, const char** args) {
409 std::cout << help; 410 std::cout << help;
410 return 1; 411 return 1;
411 } 412 }
412 } else if (arg.size() > 1 && arg.substr(0, 1) == "-"sv && arg.substr(1, 1) != "-"sv) { 413 } else if (arg.size() > 2 && arg.substr(0, 2) == "--"sv && arg.substr(2, 1) != "-"sv) {
413 auto argStr = arg.substr(1); 414 auto argStr = arg.substr(2);
414 yue::Utils::trim(argStr); 415 yue::Utils::trim(argStr);
415 size_t idx = argStr.find('='); 416 size_t idx = argStr.find('=');
416 if (idx != std::string::npos) { 417 if (idx != std::string::npos) {
@@ -420,7 +421,7 @@ int main(int narg, const char** args) {
420 yue::Utils::trim(value); 421 yue::Utils::trim(value);
421 config.options[key] = value; 422 config.options[key] = value;
422 } else { 423 } else {
423 config.options[argStr] = ""; 424 config.options[argStr] = std::string();
424 } 425 }
425 } else { 426 } else {
426 if (fs::is_directory(arg)) { 427 if (fs::is_directory(arg)) {
@@ -459,7 +460,13 @@ int main(int narg, const char** args) {
459 auto conf = config; 460 auto conf = config;
460 conf.module = file.first; 461 conf.module = file.first;
461 if (!workPath.empty()) { 462 if (!workPath.empty()) {
462 conf.options["path"] = (fs::path(workPath) / "?.lua").string(); 463 auto it = conf.options.find("path");
464 if (it != conf.options.end()) {
465 it->second += ';';
466 it->second += (fs::path(workPath) / "?.lua"sv).string();
467 } else {
468 conf.options["path"] = (fs::path(workPath) / "?.lua"sv).string();
469 }
463 } 470 }
464 if (dumpCompileTime) { 471 if (dumpCompileTime) {
465 auto start = std::chrono::high_resolution_clock::now(); 472 auto start = std::chrono::high_resolution_clock::now();
@@ -499,7 +506,7 @@ int main(int narg, const char** args) {
499 } else { 506 } else {
500 std::string targetExtension("lua"sv); 507 std::string targetExtension("lua"sv);
501 if (result.options) { 508 if (result.options) {
502 auto it = result.options->find("target_extension"); 509 auto it = result.options->find("target_extension"s);
503 if (it != result.options->end()) { 510 if (it != result.options->end()) {
504 targetExtension = it->second; 511 targetExtension = it->second;
505 } 512 }
@@ -519,19 +526,19 @@ int main(int narg, const char** args) {
519 fs::create_directories(targetFile.parent_path()); 526 fs::create_directories(targetFile.parent_path());
520 } 527 }
521 if (result.codes.empty()) { 528 if (result.codes.empty()) {
522 return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; 529 return std::tuple{0, targetFile.string(), "Built "s + file.first + '\n'};
523 } 530 }
524 std::ofstream output(targetFile, std::ios::trunc | std::ios::out); 531 std::ofstream output(targetFile, std::ios::trunc | std::ios::out);
525 if (output) { 532 if (output) {
526 const auto& codes = result.codes; 533 const auto& codes = result.codes;
527 if (config.reserveLineNumber) { 534 if (config.reserveLineNumber) {
528 auto head = std::string("-- [yue]: "sv) + file.first + '\n'; 535 auto head = "-- [yue]: "s + file.first + '\n';
529 output.write(head.c_str(), head.size()); 536 output.write(head.c_str(), head.size());
530 } 537 }
531 output.write(codes.c_str(), codes.size()); 538 output.write(codes.c_str(), codes.size());
532 return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; 539 return std::tuple{0, targetFile.string(), "Built "s + file.first + '\n'};
533 } else { 540 } else {
534 return std::tuple{1, std::string(), std::string("Failed to write file: "sv) + targetFile.string() + '\n'}; 541 return std::tuple{1, std::string(), "Failed to write file: "s + targetFile.string() + '\n'};
535 } 542 }
536 } 543 }
537 } else { 544 } else {
@@ -541,7 +548,7 @@ int main(int narg, const char** args) {
541 return std::tuple{1, std::string(), buf.str()}; 548 return std::tuple{1, std::string(), buf.str()};
542 } 549 }
543 } else { 550 } else {
544 return std::tuple{1, std::string(), std::string("Failed to read file: "sv) + file.first + ".\n"}; 551 return std::tuple{1, std::string(), "Failed to read file: "s + file.first + ".\n"};
545 } 552 }
546 }); 553 });
547 results.push_back(std::move(task)); 554 results.push_back(std::move(task));
@@ -588,7 +595,7 @@ int main(int narg, const char** args) {
588 if (lua_pcall(L, 1, 1, 0) != 0) { 595 if (lua_pcall(L, 1, 1, 0) != 0) {
589 ret = 2; 596 ret = 2;
590 std::string err = lua_tostring(L, -1); 597 std::string err = lua_tostring(L, -1);
591 errs.push_back(std::string("Failed to minify: "sv) + file + '\n' + err + '\n'); 598 errs.push_back("Failed to minify: "s + file + '\n' + err + '\n');
592 } else { 599 } else {
593 size_t size = 0; 600 size_t size = 0;
594 const char* minifiedCodes = lua_tolstring(L, -1, &size); 601 const char* minifiedCodes = lua_tolstring(L, -1, &size);
@@ -603,7 +610,7 @@ int main(int narg, const char** args) {
603 } 610 }
604 } else { 611 } else {
605 ret = 2; 612 ret = 2;
606 errs.push_back(std::string("Failed to minify: "sv) + file + '\n'); 613 errs.push_back("Failed to minify: "s + file + '\n');
607 } 614 }
608 } else { 615 } else {
609 std::cout << msg; 616 std::cout << msg;