From f6a9052be67134aeaad59d0e6f4058dfd09ce324 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 30 Jan 2020 17:01:40 +0800 Subject: fix --- src/MoonP/moon_compiler.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'src') 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() { class MoonCompiler { public: - std::pair compile(const std::string& codes, const MoonConfig& config) { + std::tuple compile(const std::string& codes, const MoonConfig& config) { _config = config; _info = _parser.parse(codes); + GlobalVars globals; if (_info.node) { try { str_list out; pushScope(); transformBlock(_info.node.to()->block, out, config.implicitReturnRoot); popScope(); - return {std::move(out.back()), Empty}; + if (config.lintGlobalVariable) { + globals = std::make_unique>(); + for (const auto& var : _globals) { + int line,col; + std::tie(line,col) = var.second; + globals->push_back({var.first, line, col}); + } + } + clear(); + return {std::move(out.back()), Empty, std::move(globals)}; } catch (const std::logic_error& error) { clear(); - return {Empty, error.what()}; + return {Empty, error.what(), std::move(globals)}; } } else { clear(); - return {Empty, _info.error}; + return {Empty, _info.error, std::move(globals)}; } } - const std::unordered_map>& getGlobals() const { - return _globals; - } - void clear() { _indentOffset = 0; _scopes.clear(); @@ -4293,15 +4299,7 @@ private: const std::string MoonCompiler::Empty; std::tuple moonCompile(const std::string& codes, const MoonConfig& config) { - auto compiler = MoonCompiler{}; - auto result = compiler.compile(codes, config); - auto globals = std::make_unique>(); - for (const auto& var : compiler.getGlobals()) { - int line,col; - std::tie(line,col) = var.second; - globals->push_back({var.first, line, col}); - } - return std::make_tuple(std::move(result.first),std::move(result.second),std::move(globals)); + return MoonCompiler{}.compile(codes, config); } } // namespace MoonP -- cgit v1.2.3-55-g6feb