diff options
author | Li Jin <dragon-fly@qq.com> | 2023-07-31 14:56:49 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-07-31 14:56:49 +0800 |
commit | 0380be2756cd2513a2b42e6e334eaa010489ae31 (patch) | |
tree | 1c8952a58418a7fc4728e41a865d9760d8177e9d /src | |
parent | a89680fe48dd1520843d7629e2006f732e313200 (diff) | |
download | yuescript-0380be2756cd2513a2b42e6e334eaa010489ae31.tar.gz yuescript-0380be2756cd2513a2b42e6e334eaa010489ae31.tar.bz2 yuescript-0380be2756cd2513a2b42e6e334eaa010489ae31.zip |
fix the way options table passing to compiler.
Diffstat (limited to 'src')
-rw-r--r-- | src/yue.cpp | 29 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 2 | ||||
-rw-r--r-- | src/yuescript/yuescript.cpp | 16 |
3 files changed, 42 insertions, 5 deletions
diff --git a/src/yue.cpp b/src/yue.cpp index dc42ff5..96bee0b 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
@@ -510,6 +510,7 @@ int main(int narg, const char** args) { | |||
510 | } | 510 | } |
511 | lua_setglobal(L, "arg"); | 511 | lua_setglobal(L, "arg"); |
512 | std::ifstream input(evalStr, std::ios::in); | 512 | std::ifstream input(evalStr, std::ios::in); |
513 | int argNum = 2; | ||
513 | if (input) { | 514 | if (input) { |
514 | auto ext = fs::path(evalStr).extension().string(); | 515 | auto ext = fs::path(evalStr).extension().string(); |
515 | for (auto& ch : ext) ch = std::tolower(ch); | 516 | for (auto& ch : ext) ch = std::tolower(ch); |
@@ -523,12 +524,38 @@ int main(int narg, const char** args) { | |||
523 | std::istreambuf_iterator<char>()); | 524 | std::istreambuf_iterator<char>()); |
524 | lua_pushlstring(L, s.c_str(), s.size()); | 525 | lua_pushlstring(L, s.c_str(), s.size()); |
525 | lua_pushlstring(L, evalStr.c_str(), evalStr.size()); | 526 | lua_pushlstring(L, evalStr.c_str(), evalStr.size()); |
527 | if (ext != ".lua") { | ||
528 | argNum = 3; | ||
529 | lua_newtable(L); | ||
530 | lua_newtable(L); | ||
531 | for (int j = i; j < narg; j++) { | ||
532 | std::string argItem = args[j]; | ||
533 | if (argItem.size() > 2 && argItem.substr(0, 2) == "--"sv && argItem.substr(2, 1) != "-"sv) { | ||
534 | auto argStr = argItem.substr(2); | ||
535 | yue::Utils::trim(argStr); | ||
536 | size_t idx = argStr.find('='); | ||
537 | if (idx != std::string::npos) { | ||
538 | auto key = argStr.substr(0, idx); | ||
539 | auto value = argStr.substr(idx + 1); | ||
540 | yue::Utils::trim(key); | ||
541 | yue::Utils::trim(value); | ||
542 | lua_pushlstring(L, key.c_str(), key.size()); | ||
543 | lua_pushlstring(L, value.c_str(), value.size()); | ||
544 | } else { | ||
545 | lua_pushlstring(L, argStr.c_str(), argStr.size()); | ||
546 | lua_pushliteral(L, ""); | ||
547 | } | ||
548 | lua_rawset(L, -3); | ||
549 | } | ||
550 | } | ||
551 | lua_setfield(L, -2, "options"); | ||
552 | } | ||
526 | } else { | 553 | } else { |
527 | pushYue(L, "loadstring"sv); | 554 | pushYue(L, "loadstring"sv); |
528 | lua_pushlstring(L, evalStr.c_str(), evalStr.size()); | 555 | lua_pushlstring(L, evalStr.c_str(), evalStr.size()); |
529 | lua_pushliteral(L, "=(eval str)"); | 556 | lua_pushliteral(L, "=(eval str)"); |
530 | } | 557 | } |
531 | if (lua_pcall(L, 2, 2, 0) != 0) { | 558 | if (lua_pcall(L, argNum, 2, 0) != 0) { |
532 | std::cout << lua_tostring(L, -1) << '\n'; | 559 | std::cout << lua_tostring(L, -1) << '\n'; |
533 | return 1; | 560 | return 1; |
534 | } | 561 | } |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index ed5e849..5b8b112 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -72,7 +72,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
72 | "close"s // Lua 5.4 | 72 | "close"s // Lua 5.4 |
73 | }; | 73 | }; |
74 | 74 | ||
75 | const std::string_view version = "0.17.13"sv; | 75 | const std::string_view version = "0.17.14"sv; |
76 | const std::string_view extension = "yue"sv; | 76 | const std::string_view extension = "yue"sv; |
77 | 77 | ||
78 | class CompileError : public std::logic_error { | 78 | class CompileError : public std::logic_error { |
diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 3954fb9..856d8f3 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp | |||
@@ -93,10 +93,20 @@ static void get_config(lua_State* L, yue::YueConfig& config) { | |||
93 | config.useSpaceOverTab = lua_toboolean(L, -1) != 0; | 93 | config.useSpaceOverTab = lua_toboolean(L, -1) != 0; |
94 | } | 94 | } |
95 | lua_pop(L, 1); | 95 | lua_pop(L, 1); |
96 | lua_pushliteral(L, "target"); | 96 | lua_pushliteral(L, "options"); |
97 | lua_gettable(L, -2); | 97 | lua_gettable(L, -2); |
98 | if (lua_isstring(L, -1) != 0) { | 98 | if (lua_istable(L, -1) != 0) { |
99 | config.options["target"] = lua_tostring(L, -1); | 99 | lua_pushnil(L); // options startKey |
100 | while (lua_next(L, -2) != 0) { // options key value | ||
101 | size_t len = 0; | ||
102 | auto pstr = lua_tolstring(L, -2, &len); | ||
103 | std::string key{pstr, len}; | ||
104 | pstr = lua_tolstring(L, -1, &len); | ||
105 | std::string value{pstr, len}; | ||
106 | config.options[key] = value; | ||
107 | lua_pop(L, 1); // options key | ||
108 | } | ||
109 | lua_pop(L, 1); // options | ||
100 | } | 110 | } |
101 | lua_pop(L, 1); | 111 | lua_pop(L, 1); |
102 | } | 112 | } |