aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2022-07-22 01:34:53 +0800
committerLi Jin <dragon-fly@qq.com>2022-07-22 01:34:53 +0800
commiteb367126bf3a4f5b0e51ccef93b7c7136bea170e (patch)
treeb89733b7ad07c1ea46a5bb3fab263cf820b24d86
parent557b92b99839f3f0abe96eeea8123cf926ffc3be (diff)
downloadyuescript-eb367126bf3a4f5b0e51ccef93b7c7136bea170e.tar.gz
yuescript-eb367126bf3a4f5b0e51ccef93b7c7136bea170e.tar.bz2
yuescript-eb367126bf3a4f5b0e51ccef93b7c7136bea170e.zip
add -g option for yue cmd tool.
-rw-r--r--src/yue.cpp16
-rwxr-xr-xsrc/yuescript/yue_compiler.cpp10
-rw-r--r--src/yuescript/yue_compiler.h3
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) {
113" -s Use spaces in generated codes instead of tabs\n" 113" -s Use spaces in generated codes instead of tabs\n"
114" -p Write output to standard out\n" 114" -p Write output to standard out\n"
115" -b Dump compile time (doesn't write output)\n" 115" -b Dump compile time (doesn't write output)\n"
116" -g Dump global variables used in NAME LINE COLUMN\n"
116" -l Write line numbers from source codes\n" 117" -l Write line numbers from source codes\n"
117" -v Print version\n" 118" -v Print version\n"
118#ifndef YUE_COMPILER_ONLY 119#ifndef YUE_COMPILER_ONLY
@@ -270,6 +271,7 @@ int main(int narg, const char** args) {
270 config.useSpaceOverTab = false; 271 config.useSpaceOverTab = false;
271 bool writeToFile = true; 272 bool writeToFile = true;
272 bool dumpCompileTime = false; 273 bool dumpCompileTime = false;
274 bool lintGlobal = false;
273 std::string targetPath; 275 std::string targetPath;
274 std::string resultFile; 276 std::string resultFile;
275 std::string workPath; 277 std::string workPath;
@@ -380,6 +382,9 @@ int main(int narg, const char** args) {
380 config.reserveLineNumber = true; 382 config.reserveLineNumber = true;
381 } else if (arg == "-p"sv) { 383 } else if (arg == "-p"sv) {
382 writeToFile = false; 384 writeToFile = false;
385 } else if (arg == "-g"sv) {
386 writeToFile = false;
387 lintGlobal = true;
383 } else if (arg == "-t"sv) { 388 } else if (arg == "-t"sv) {
384 ++i; 389 ++i;
385 if (i < narg) { 390 if (i < narg) {
@@ -478,10 +483,19 @@ int main(int narg, const char** args) {
478 return std::tuple{1, file.first, buf.str()}; 483 return std::tuple{1, file.first, buf.str()};
479 } 484 }
480 } 485 }
486 conf.lintGlobalVariable = lintGlobal;
481 auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); 487 auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf);
482 if (result.error.empty()) { 488 if (result.error.empty()) {
483 if (!writeToFile) { 489 if (!writeToFile) {
484 return std::tuple{0, file.first, result.codes + '\n'}; 490 if (lintGlobal) {
491 std::ostringstream buf;
492 for (const auto& global : *result.globals) {
493 buf << global.name << ' ' << global.line << ' ' << global.col << '\n';
494 }
495 return std::tuple{0, file.first, buf.str() + '\n'};
496 } else {
497 return std::tuple{0, file.first, result.codes + '\n'};
498 }
485 } else { 499 } else {
486 std::string targetExtension("lua"sv); 500 std::string targetExtension("lua"sv);
487 if (result.options) { 501 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:
155 std::tie(line, col) = var.second; 155 std::tie(line, col) = var.second;
156 globals->push_back({var.first, line, col}); 156 globals->push_back({var.first, line, col});
157 } 157 }
158 std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB)
159 {
160 if (varA.line < varB.line) {
161 return true;
162 } else if (varA.line == varB.line) {
163 return varA.col < varB.col;
164 } else {
165 return false;
166 }
167 });
158 } 168 }
159#ifndef YUE_NO_MACRO 169#ifndef YUE_NO_MACRO
160 if (L) { 170 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
12#include <string_view> 12#include <string_view>
13#include <tuple> 13#include <tuple>
14#include <list> 14#include <list>
15#include <vector>
15#include <memory> 16#include <memory>
16#include <unordered_map> 17#include <unordered_map>
17#include <functional> 18#include <functional>
@@ -40,7 +41,7 @@ struct GlobalVar {
40 int col; 41 int col;
41}; 42};
42 43
43using GlobalVars = std::list<GlobalVar>; 44using GlobalVars = std::vector<GlobalVar>;
44 45
45struct CompileInfo { 46struct CompileInfo {
46 std::string codes; 47 std::string codes;