From f4e2286da2b0212b0a7ac76821203bd398cfac5b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Tue, 15 Feb 2022 09:55:18 +0800 Subject: fix a global variable check case. --- src/yuescript/yue_compiler.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 6bc9fe8..6c8e167 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.9.10"sv; +const std::string_view version = "0.9.11"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -3539,7 +3539,19 @@ private: } BLOCK_END auto objVar = singleVariableFrom(partOne); - if (objVar.empty()) { + bool isScoped = false; + if (objVar.empty() || !isLocal(objVar)) { + switch (usage) { + case ExpUsage::Common: + case ExpUsage::Assignment: + isScoped = true; + break; + default: break; + } + if (isScoped) { + temp.push_back(indent() + "do"s + nll(x)); + pushScope(); + } objVar = getUnusedName("_obj_"sv); if (auto colonItem = ast_cast(partOne->items.back())) { auto chainValue = x->new_ptr(); @@ -3549,7 +3561,7 @@ private: if (_withVars.empty()) { throw std::logic_error(_info.errorMessage("short dot/colon syntax must be called within a with block"sv, x)); } - chainValue->items.push_back(toAst(_withVars.top(), x)); + chainValue->items.push_back(toAst(_withVars.top(), x)); } auto newObj = singleVariableFrom(chainValue); if (!newObj.empty()) { @@ -3650,6 +3662,10 @@ private: default: break; } + if (isScoped) { + popScope(); + temp.push_back(indent() + "end"s + nlr(x)); + } out.push_back(join(temp)); return true; } -- cgit v1.2.3-55-g6feb