aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-01-30 17:01:40 +0800
committerLi Jin <dragon-fly@qq.com>2020-01-30 17:01:40 +0800
commitf6a9052be67134aeaad59d0e6f4058dfd09ce324 (patch)
treeb9b538ad07e466b10c0e5281bd1a5e0163f0fd43 /src
parentc62d9eb35a310e7663234526ce4b9fe3519ca7cf (diff)
downloadyuescript-f6a9052be67134aeaad59d0e6f4058dfd09ce324.tar.gz
yuescript-f6a9052be67134aeaad59d0e6f4058dfd09ce324.tar.bz2
yuescript-f6a9052be67134aeaad59d0e6f4058dfd09ce324.zip
fix
Diffstat (limited to 'src')
-rw-r--r--src/MoonP/moon_compiler.cpp32
1 files changed, 15 insertions, 17 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp
index ceaff96..e804965 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/MoonP/moon_compiler.cpp
@@ -34,30 +34,36 @@ const char* moonScriptVersion() {
34 34
35class MoonCompiler { 35class MoonCompiler {
36public: 36public:
37 std::pair<std::string,std::string> compile(const std::string& codes, const MoonConfig& config) { 37 std::tuple<std::string,std::string,GlobalVars> compile(const std::string& codes, const MoonConfig& config) {
38 _config = config; 38 _config = config;
39 _info = _parser.parse<File_t>(codes); 39 _info = _parser.parse<File_t>(codes);
40 GlobalVars globals;
40 if (_info.node) { 41 if (_info.node) {
41 try { 42 try {
42 str_list out; 43 str_list out;
43 pushScope(); 44 pushScope();
44 transformBlock(_info.node.to<File_t>()->block, out, config.implicitReturnRoot); 45 transformBlock(_info.node.to<File_t>()->block, out, config.implicitReturnRoot);
45 popScope(); 46 popScope();
46 return {std::move(out.back()), Empty}; 47 if (config.lintGlobalVariable) {
48 globals = std::make_unique<std::list<GlobalVar>>();
49 for (const auto& var : _globals) {
50 int line,col;
51 std::tie(line,col) = var.second;
52 globals->push_back({var.first, line, col});
53 }
54 }
55 clear();
56 return {std::move(out.back()), Empty, std::move(globals)};
47 } catch (const std::logic_error& error) { 57 } catch (const std::logic_error& error) {
48 clear(); 58 clear();
49 return {Empty, error.what()}; 59 return {Empty, error.what(), std::move(globals)};
50 } 60 }
51 } else { 61 } else {
52 clear(); 62 clear();
53 return {Empty, _info.error}; 63 return {Empty, _info.error, std::move(globals)};
54 } 64 }
55 } 65 }
56 66
57 const std::unordered_map<std::string,std::pair<int,int>>& getGlobals() const {
58 return _globals;
59 }
60
61 void clear() { 67 void clear() {
62 _indentOffset = 0; 68 _indentOffset = 0;
63 _scopes.clear(); 69 _scopes.clear();
@@ -4293,15 +4299,7 @@ private:
4293const std::string MoonCompiler::Empty; 4299const std::string MoonCompiler::Empty;
4294 4300
4295std::tuple<std::string,std::string,GlobalVars> moonCompile(const std::string& codes, const MoonConfig& config) { 4301std::tuple<std::string,std::string,GlobalVars> moonCompile(const std::string& codes, const MoonConfig& config) {
4296 auto compiler = MoonCompiler{}; 4302 return MoonCompiler{}.compile(codes, config);
4297 auto result = compiler.compile(codes, config);
4298 auto globals = std::make_unique<std::list<GlobalVar>>();
4299 for (const auto& var : compiler.getGlobals()) {
4300 int line,col;
4301 std::tie(line,col) = var.second;
4302 globals->push_back({var.first, line, col});
4303 }
4304 return std::make_tuple(std::move(result.first),std::move(result.second),std::move(globals));
4305} 4303}
4306 4304
4307} // namespace MoonP 4305} // namespace MoonP