From 0380be2756cd2513a2b42e6e334eaa010489ae31 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 31 Jul 2023 14:56:49 +0800 Subject: fix the way options table passing to compiler. --- src/yue.cpp | 29 ++++++++++++++++++++++++++++- src/yuescript/yue_compiler.cpp | 2 +- src/yuescript/yuescript.cpp | 16 +++++++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) (limited to 'src') 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) { } lua_setglobal(L, "arg"); std::ifstream input(evalStr, std::ios::in); + int argNum = 2; if (input) { auto ext = fs::path(evalStr).extension().string(); for (auto& ch : ext) ch = std::tolower(ch); @@ -523,12 +524,38 @@ int main(int narg, const char** args) { std::istreambuf_iterator()); lua_pushlstring(L, s.c_str(), s.size()); lua_pushlstring(L, evalStr.c_str(), evalStr.size()); + if (ext != ".lua") { + argNum = 3; + lua_newtable(L); + lua_newtable(L); + for (int j = i; j < narg; j++) { + std::string argItem = args[j]; + if (argItem.size() > 2 && argItem.substr(0, 2) == "--"sv && argItem.substr(2, 1) != "-"sv) { + auto argStr = argItem.substr(2); + yue::Utils::trim(argStr); + size_t idx = argStr.find('='); + if (idx != std::string::npos) { + auto key = argStr.substr(0, idx); + auto value = argStr.substr(idx + 1); + yue::Utils::trim(key); + yue::Utils::trim(value); + lua_pushlstring(L, key.c_str(), key.size()); + lua_pushlstring(L, value.c_str(), value.size()); + } else { + lua_pushlstring(L, argStr.c_str(), argStr.size()); + lua_pushliteral(L, ""); + } + lua_rawset(L, -3); + } + } + lua_setfield(L, -2, "options"); + } } else { pushYue(L, "loadstring"sv); lua_pushlstring(L, evalStr.c_str(), evalStr.size()); lua_pushliteral(L, "=(eval str)"); } - if (lua_pcall(L, 2, 2, 0) != 0) { + if (lua_pcall(L, argNum, 2, 0) != 0) { std::cout << lua_tostring(L, -1) << '\n'; return 1; } 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 Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.17.13"sv; +const std::string_view version = "0.17.14"sv; const std::string_view extension = "yue"sv; 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) { config.useSpaceOverTab = lua_toboolean(L, -1) != 0; } lua_pop(L, 1); - lua_pushliteral(L, "target"); + lua_pushliteral(L, "options"); lua_gettable(L, -2); - if (lua_isstring(L, -1) != 0) { - config.options["target"] = lua_tostring(L, -1); + if (lua_istable(L, -1) != 0) { + lua_pushnil(L); // options startKey + while (lua_next(L, -2) != 0) { // options key value + size_t len = 0; + auto pstr = lua_tolstring(L, -2, &len); + std::string key{pstr, len}; + pstr = lua_tolstring(L, -1, &len); + std::string value{pstr, len}; + config.options[key] = value; + lua_pop(L, 1); // options key + } + lua_pop(L, 1); // options } lua_pop(L, 1); } -- cgit v1.2.3-55-g6feb