diff options
author | Li Jin <dragon-fly@qq.com> | 2022-07-13 14:46:55 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-07-13 14:46:55 +0800 |
commit | a1d341085eed96d567329a30f2cf57c95fe6f071 (patch) | |
tree | 37d359a02b99cf94fa52cfc43eb9c5c80ecfeb89 | |
parent | aa194667c7cb7bb70f2a58477555d1419ae33eb0 (diff) | |
download | yuescript-a1d341085eed96d567329a30f2cf57c95fe6f071.tar.gz yuescript-a1d341085eed96d567329a30f2cf57c95fe6f071.tar.bz2 yuescript-a1d341085eed96d567329a30f2cf57c95fe6f071.zip |
auto add search path for cmd tools.
-rw-r--r-- | makefile | 4 | ||||
-rw-r--r-- | spec/inputs/macro-export.yue | 2 | ||||
-rw-r--r-- | spec/inputs/macro-teal.yue | 2 | ||||
-rw-r--r-- | spec/inputs/macro.yue | 3 | ||||
-rw-r--r-- | spec/inputs/teal-lang.yue | 5 | ||||
-rw-r--r-- | spec/outputs/macro.lua | 2 | ||||
-rw-r--r-- | src/yue.cpp | 20 | ||||
-rwxr-xr-x | src/yuescript/yue_compiler.cpp | 18 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.h | 2 | ||||
-rw-r--r-- | src/yuescript/yuescript.h | 18 |
10 files changed, 44 insertions, 32 deletions
@@ -266,7 +266,7 @@ test: release | |||
266 | @mkdir -p $(TEST_OUTPUT) | 266 | @mkdir -p $(TEST_OUTPUT) |
267 | @echo "Compiling Yuescript codes..." | 267 | @echo "Compiling Yuescript codes..." |
268 | @$(START_TIME) | 268 | @$(START_TIME) |
269 | @./$(BIN_NAME) $(TEST_INPUT) -t $(TEST_OUTPUT) -tl_enabled=true | 269 | @./$(BIN_NAME) $(TEST_INPUT) -t $(TEST_OUTPUT) -tl_enabled |
270 | @./$(BIN_NAME) $(TEST_INPUT)/teal-lang.yue -o $(TEST_OUTPUT)/teal-lang.lua | 270 | @./$(BIN_NAME) $(TEST_INPUT)/teal-lang.yue -o $(TEST_OUTPUT)/teal-lang.lua |
271 | @echo -en "Compile time: " | 271 | @echo -en "Compile time: " |
272 | @$(END_TIME) | 272 | @$(END_TIME) |
@@ -279,7 +279,7 @@ test: release | |||
279 | gen: release | 279 | gen: release |
280 | @echo "Compiling Yuescript codes..." | 280 | @echo "Compiling Yuescript codes..." |
281 | @$(START_TIME) | 281 | @$(START_TIME) |
282 | @./$(BIN_NAME) $(TEST_INPUT) -t $(GEN_OUTPUT) -tl_enabled=true | 282 | @./$(BIN_NAME) $(TEST_INPUT) -t $(GEN_OUTPUT) -tl_enabled |
283 | @./$(BIN_NAME) $(TEST_INPUT)/teal-lang.yue -o $(GEN_OUTPUT)/teal-lang.lua | 283 | @./$(BIN_NAME) $(TEST_INPUT)/teal-lang.yue -o $(GEN_OUTPUT)/teal-lang.lua |
284 | @echo -en "Compile time: " | 284 | @echo -en "Compile time: " |
285 | @$(END_TIME) | 285 | @$(END_TIME) |
diff --git a/spec/inputs/macro-export.yue b/spec/inputs/macro-export.yue index adc71da..ff0d273 100644 --- a/spec/inputs/macro-export.yue +++ b/spec/inputs/macro-export.yue | |||
@@ -1,5 +1,3 @@ | |||
1 | $ -> package.path = "?.lua;./spec/inputs/?.lua" | ||
2 | |||
3 | import "macro-todo" as $ | 1 | import "macro-todo" as $ |
4 | 2 | ||
5 | import "macro-todo" as {$, :$todo} | 3 | import "macro-todo" as {$, :$todo} |
diff --git a/spec/inputs/macro-teal.yue b/spec/inputs/macro-teal.yue index 951e882..a443614 100644 --- a/spec/inputs/macro-teal.yue +++ b/spec/inputs/macro-teal.yue | |||
@@ -2,7 +2,7 @@ $ -> | |||
2 | import "yue" as {:options} | 2 | import "yue" as {:options} |
3 | if options.tl_enabled | 3 | if options.tl_enabled |
4 | options.target_extension = "tl" | 4 | options.target_extension = "tl" |
5 | package.path ..= "?.lua;./spec/lib/?.lua" | 5 | package.path ..= ";./spec/lib/?.lua" |
6 | 6 | ||
7 | macro to_lua = (code)-> | 7 | macro to_lua = (code)-> |
8 | "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" | 8 | "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" |
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue index e4b0fc8..7bbb06d 100644 --- a/spec/inputs/macro.yue +++ b/spec/inputs/macro.yue | |||
@@ -1,6 +1,3 @@ | |||
1 | $ -> | ||
2 | package.path = "?.lua;./spec/inputs/?.lua" | ||
3 | |||
4 | import "macro-export" as { | 1 | import "macro-export" as { |
5 | $, -- import all macros | 2 | $, -- import all macros |
6 | $config: $myconfig, -- rename macro $config to $myconfig | 3 | $config: $myconfig, -- rename macro $config to $myconfig |
diff --git a/spec/inputs/teal-lang.yue b/spec/inputs/teal-lang.yue index 68b2dd1..b28c915 100644 --- a/spec/inputs/teal-lang.yue +++ b/spec/inputs/teal-lang.yue | |||
@@ -1,7 +1,4 @@ | |||
1 | $ -> | 1 | import "macro-teal" as $ |
2 | package.path = "?.lua;./spec/inputs/?.lua" | ||
3 | |||
4 | import "macro-teal" as {$} | ||
5 | 2 | ||
6 | $local "a:{string:number}", {value:123} | 3 | $local "a:{string:number}", {value:123} |
7 | $local "b:number", a.value | 4 | $local "b:number", a.value |
diff --git a/spec/outputs/macro.lua b/spec/outputs/macro.lua index 5917f43..c19b2d7 100644 --- a/spec/outputs/macro.lua +++ b/spec/outputs/macro.lua | |||
@@ -277,7 +277,7 @@ print((setmetatable({ | |||
277 | return 998 | 277 | return 998 |
278 | end | 278 | end |
279 | })) | 279 | })) |
280 | print("current line: " .. tostring(271)); | 280 | print("current line: " .. tostring(268)); |
281 | -- TODO | 281 | -- TODO |
282 | do | 282 | do |
283 | print(1) | 283 | print(1) |
diff --git a/src/yue.cpp b/src/yue.cpp index 96649c4..7b2ad0b 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
@@ -272,6 +272,7 @@ int main(int narg, const char** args) { | |||
272 | bool dumpCompileTime = false; | 272 | bool dumpCompileTime = false; |
273 | std::string targetPath; | 273 | std::string targetPath; |
274 | std::string resultFile; | 274 | std::string resultFile; |
275 | std::string workPath; | ||
275 | std::list<std::pair<std::string,std::string>> files; | 276 | std::list<std::pair<std::string,std::string>> files; |
276 | for (int i = 1; i < narg; ++i) { | 277 | for (int i = 1; i < narg; ++i) { |
277 | std::string arg = args[i]; | 278 | std::string arg = args[i]; |
@@ -414,10 +415,11 @@ int main(int narg, const char** args) { | |||
414 | yue::Utils::trim(value); | 415 | yue::Utils::trim(value); |
415 | config.options[key] = value; | 416 | config.options[key] = value; |
416 | } else { | 417 | } else { |
417 | config.options[argStr] = '1'; | 418 | config.options[argStr] = ""; |
418 | } | 419 | } |
419 | } else { | 420 | } else { |
420 | if (fs::is_directory(arg)) { | 421 | if (fs::is_directory(arg)) { |
422 | workPath = arg; | ||
421 | for (auto item : fs::recursive_directory_iterator(arg)) { | 423 | for (auto item : fs::recursive_directory_iterator(arg)) { |
422 | if (!item.is_directory()) { | 424 | if (!item.is_directory()) { |
423 | auto ext = item.path().extension().string(); | 425 | auto ext = item.path().extension().string(); |
@@ -428,6 +430,7 @@ int main(int narg, const char** args) { | |||
428 | } | 430 | } |
429 | } | 431 | } |
430 | } else { | 432 | } else { |
433 | workPath = fs::path(arg).parent_path().string(); | ||
431 | files.emplace_back(arg, arg); | 434 | files.emplace_back(arg, arg); |
432 | } | 435 | } |
433 | } | 436 | } |
@@ -450,6 +453,9 @@ int main(int narg, const char** args) { | |||
450 | std::istreambuf_iterator<char>()); | 453 | std::istreambuf_iterator<char>()); |
451 | auto conf = config; | 454 | auto conf = config; |
452 | conf.module = file.first; | 455 | conf.module = file.first; |
456 | if (!workPath.empty()) { | ||
457 | conf.options["path"] = (fs::path(workPath) / "?.lua").string(); | ||
458 | } | ||
453 | if (dumpCompileTime) { | 459 | if (dumpCompileTime) { |
454 | auto start = std::chrono::high_resolution_clock::now(); | 460 | auto start = std::chrono::high_resolution_clock::now(); |
455 | auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); | 461 | auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); |
@@ -467,7 +473,7 @@ int main(int narg, const char** args) { | |||
467 | return std::tuple{0, file.first, buf.str()}; | 473 | return std::tuple{0, file.first, buf.str()}; |
468 | } else { | 474 | } else { |
469 | std::ostringstream buf; | 475 | std::ostringstream buf; |
470 | buf << "Fail to compile: "sv << file.first << ".\n"sv; | 476 | buf << "Failed to compile: "sv << file.first << ".\n"sv; |
471 | buf << result.error << '\n'; | 477 | buf << result.error << '\n'; |
472 | return std::tuple{1, file.first, buf.str()}; | 478 | return std::tuple{1, file.first, buf.str()}; |
473 | } | 479 | } |
@@ -511,17 +517,17 @@ int main(int narg, const char** args) { | |||
511 | output.write(codes.c_str(), codes.size()); | 517 | output.write(codes.c_str(), codes.size()); |
512 | return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; | 518 | return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; |
513 | } else { | 519 | } else { |
514 | return std::tuple{1, std::string(), std::string("Fail to write file: "sv) + targetFile.string() + '\n'}; | 520 | return std::tuple{1, std::string(), std::string("Failed to write file: "sv) + targetFile.string() + '\n'}; |
515 | } | 521 | } |
516 | } | 522 | } |
517 | } else { | 523 | } else { |
518 | std::ostringstream buf; | 524 | std::ostringstream buf; |
519 | buf << "Fail to compile: "sv << file.first << ".\n"; | 525 | buf << "Failed to compile: "sv << file.first << ".\n"; |
520 | buf << result.error << '\n'; | 526 | buf << result.error << '\n'; |
521 | return std::tuple{1, std::string(), buf.str()}; | 527 | return std::tuple{1, std::string(), buf.str()}; |
522 | } | 528 | } |
523 | } else { | 529 | } else { |
524 | return std::tuple{1, std::string(), std::string("Fail to read file: "sv) + file.first + ".\n"}; | 530 | return std::tuple{1, std::string(), std::string("Failed to read file: "sv) + file.first + ".\n"}; |
525 | } | 531 | } |
526 | }); | 532 | }); |
527 | results.push_back(std::move(task)); | 533 | results.push_back(std::move(task)); |
@@ -568,7 +574,7 @@ int main(int narg, const char** args) { | |||
568 | if (lua_pcall(L, 1, 1, 0) != 0) { | 574 | if (lua_pcall(L, 1, 1, 0) != 0) { |
569 | ret = 2; | 575 | ret = 2; |
570 | std::string err = lua_tostring(L, -1); | 576 | std::string err = lua_tostring(L, -1); |
571 | errs.push_back(std::string("Fail to minify: "sv) + file + '\n' + err + '\n'); | 577 | errs.push_back(std::string("Failed to minify: "sv) + file + '\n' + err + '\n'); |
572 | } else { | 578 | } else { |
573 | size_t size = 0; | 579 | size_t size = 0; |
574 | const char* minifiedCodes = lua_tolstring(L, -1, &size); | 580 | const char* minifiedCodes = lua_tolstring(L, -1, &size); |
@@ -583,7 +589,7 @@ int main(int narg, const char** args) { | |||
583 | } | 589 | } |
584 | } else { | 590 | } else { |
585 | ret = 2; | 591 | ret = 2; |
586 | errs.push_back(std::string("Fail to minify: "sv) + file + '\n'); | 592 | errs.push_back(std::string("Failed to minify: "sv) + file + '\n'); |
587 | } | 593 | } |
588 | } else { | 594 | } else { |
589 | std::cout << msg; | 595 | std::cout << msg; |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c7ceeb4..c68746c 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -56,7 +56,7 @@ using namespace parserlib; | |||
56 | 56 | ||
57 | typedef std::list<std::string> str_list; | 57 | typedef std::list<std::string> str_list; |
58 | 58 | ||
59 | const std::string_view version = "0.13.2"sv; | 59 | const std::string_view version = "0.13.3"sv; |
60 | const std::string_view extension = "yue"sv; | 60 | const std::string_view extension = "yue"sv; |
61 | 61 | ||
62 | class YueCompilerImpl { | 62 | class YueCompilerImpl { |
@@ -6887,13 +6887,23 @@ private: | |||
6887 | DEFER(lua_settop(L, top)); | 6887 | DEFER(lua_settop(L, top)); |
6888 | pushYue("find_modulepath"sv); // cur find_modulepath | 6888 | pushYue("find_modulepath"sv); // cur find_modulepath |
6889 | lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName | 6889 | lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName |
6890 | if (lua_pcall(L, 1, 1, 0) != 0) { | 6890 | if (lua_pcall(L, 1, 2, 0) != 0) { |
6891 | std::string err = lua_tostring(L, -1); | 6891 | std::string err = lua_tostring(L, -1); |
6892 | throw std::logic_error(_info.errorMessage("failed to resolve module path\n"s + err, x)); | 6892 | throw std::logic_error(_info.errorMessage("failed to resolve module path\n"s + err, x)); |
6893 | } | 6893 | } |
6894 | if (lua_isnil(L, -1) != 0) { | 6894 | if (lua_isnil(L, -2) != 0) { |
6895 | throw std::logic_error(_info.errorMessage("failed to find module '"s + moduleName + '\'', x)); | 6895 | str_list files; |
6896 | if (lua_istable(L, -1) != 0) { | ||
6897 | int size = static_cast<int>(lua_objlen(L, -1)); | ||
6898 | for (int i = 0; i < size; i++) { | ||
6899 | lua_rawgeti(L, -1, i + 1); | ||
6900 | files.push_back("no file \""s + lua_tostring(L, -1) + "\""s); | ||
6901 | lua_pop(L, 1); | ||
6902 | } | ||
6903 | } | ||
6904 | throw std::logic_error(_info.errorMessage("module '"s + moduleName + "\' not found:\n\t"s + join(files, "\n\t"sv), x)); | ||
6896 | } | 6905 | } |
6906 | lua_pop(L, 1); | ||
6897 | std::string moduleFullName = lua_tostring(L, -1); | 6907 | std::string moduleFullName = lua_tostring(L, -1); |
6898 | lua_pop(L, 1); // cur | 6908 | lua_pop(L, 1); // cur |
6899 | if (!isModuleLoaded(moduleFullName)) { | 6909 | if (!isModuleLoaded(moduleFullName)) { |
diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index 6f5170f..903cb3d 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h | |||
@@ -21,7 +21,7 @@ namespace yue { | |||
21 | extern const std::string_view version; | 21 | extern const std::string_view version; |
22 | extern const std::string_view extension; | 22 | extern const std::string_view extension; |
23 | 23 | ||
24 | using Options = std::unordered_map<std::string,std::string>; | 24 | using Options = std::unordered_map<std::string, std::string>; |
25 | 25 | ||
26 | struct YueConfig { | 26 | struct YueConfig { |
27 | bool lintGlobalVariable = false; | 27 | bool lintGlobalVariable = false; |
diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index 1430758..2d2d660 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h | |||
@@ -59,13 +59,17 @@ local function find_modulepath(name) | |||
59 | local name_path = name:match("[\\/]") and name or name:gsub("%.", dirsep) | 59 | local name_path = name:match("[\\/]") and name or name:gsub("%.", dirsep) |
60 | local file_exist, file_path | 60 | local file_exist, file_path |
61 | local tried = {} | 61 | local tried = {} |
62 | for path in package.path:gmatch("[^;]+") do | 62 | local paths = {package.path, yue.options.path} |
63 | file_path = path:gsub("?", name_path):gsub("%.lua$", suffix) | 63 | for i = 1, #paths do |
64 | file_exist = yue.file_exist(file_path) | 64 | local yue_path = paths[i] |
65 | if file_exist then | 65 | for path in yue_path:gmatch("[^;]+") do |
66 | break | 66 | file_path = path:gsub("?", name_path):gsub("%.lua$", suffix) |
67 | else | 67 | file_exist = yue.file_exist(file_path) |
68 | tried[#tried + 1] = file_path | 68 | if file_exist then |
69 | break | ||
70 | else | ||
71 | tried[#tried + 1] = file_path | ||
72 | end | ||
69 | end | 73 | end |
70 | end | 74 | end |
71 | if file_exist then | 75 | if file_exist then |