From eb367126bf3a4f5b0e51ccef93b7c7136bea170e Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 22 Jul 2022 01:34:53 +0800 Subject: add -g option for yue cmd tool. --- src/yue.cpp | 16 +++++++++++++++- src/yuescript/yue_compiler.cpp | 10 ++++++++++ src/yuescript/yue_compiler.h | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/yue.cpp b/src/yue.cpp index 7b2ad0b..3e30c70 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -113,6 +113,7 @@ int main(int narg, const char** args) { " -s Use spaces in generated codes instead of tabs\n" " -p Write output to standard out\n" " -b Dump compile time (doesn't write output)\n" +" -g Dump global variables used in NAME LINE COLUMN\n" " -l Write line numbers from source codes\n" " -v Print version\n" #ifndef YUE_COMPILER_ONLY @@ -270,6 +271,7 @@ int main(int narg, const char** args) { config.useSpaceOverTab = false; bool writeToFile = true; bool dumpCompileTime = false; + bool lintGlobal = false; std::string targetPath; std::string resultFile; std::string workPath; @@ -380,6 +382,9 @@ int main(int narg, const char** args) { config.reserveLineNumber = true; } else if (arg == "-p"sv) { writeToFile = false; + } else if (arg == "-g"sv) { + writeToFile = false; + lintGlobal = true; } else if (arg == "-t"sv) { ++i; if (i < narg) { @@ -478,10 +483,19 @@ int main(int narg, const char** args) { return std::tuple{1, file.first, buf.str()}; } } + conf.lintGlobalVariable = lintGlobal; auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); if (result.error.empty()) { if (!writeToFile) { - return std::tuple{0, file.first, result.codes + '\n'}; + if (lintGlobal) { + std::ostringstream buf; + for (const auto& global : *result.globals) { + buf << global.name << ' ' << global.line << ' ' << global.col << '\n'; + } + return std::tuple{0, file.first, buf.str() + '\n'}; + } else { + return std::tuple{0, file.first, result.codes + '\n'}; + } } else { std::string targetExtension("lua"sv); if (result.options) { diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 85aa6d4..7bbf5e9 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -155,6 +155,16 @@ public: std::tie(line, col) = var.second; globals->push_back({var.first, line, col}); } + std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB) + { + if (varA.line < varB.line) { + return true; + } else if (varA.line == varB.line) { + return varA.col < varB.col; + } else { + return false; + } + }); } #ifndef YUE_NO_MACRO if (L) { diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index 903cb3d..3df5451 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -12,6 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include +#include #include #include #include @@ -40,7 +41,7 @@ struct GlobalVar { int col; }; -using GlobalVars = std::list; +using GlobalVars = std::vector; struct CompileInfo { std::string codes; -- cgit v1.2.3-55-g6feb