aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spec/outputs/codes_from_doc.lua4
-rw-r--r--spec/outputs/codes_from_doc_zh.lua4
-rw-r--r--spec/outputs/import_global.lua14
-rw-r--r--src/yuescript/yue_compiler.cpp29
-rw-r--r--src/yuescript/yue_parser.cpp2
5 files changed, 26 insertions, 27 deletions
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index 02f36d1..84dfc4e 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -409,8 +409,8 @@ local tb = {
409 } 409 }
410} 410}
411do 411do
412 local math = math
413 local print = print 412 local print = print
413 local math = math
414 print("hello") 414 print("hello")
415 math.random(3) 415 math.random(3)
416end 416end
@@ -2928,8 +2928,8 @@ local tb = {
2928 } 2928 }
2929} 2929}
2930do 2930do
2931 local math = math
2932 local print = print 2931 local print = print
2932 local math = math
2933 print("hello") 2933 print("hello")
2934 math.random(3) 2934 math.random(3)
2935end 2935end
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index 87944e0..d4e8b6a 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -409,8 +409,8 @@ local tb = {
409 } 409 }
410} 410}
411do 411do
412 local math = math
413 local print = print 412 local print = print
413 local math = math
414 print("hello") 414 print("hello")
415 math.random(3) 415 math.random(3)
416end 416end
@@ -2922,8 +2922,8 @@ local tb = {
2922 } 2922 }
2923} 2923}
2924do 2924do
2925 local math = math
2926 local print = print 2925 local print = print
2926 local math = math
2927 print("hello") 2927 print("hello")
2928 math.random(3) 2928 math.random(3)
2929end 2929end
diff --git a/spec/outputs/import_global.lua b/spec/outputs/import_global.lua
index 08a90e2..c748c78 100644
--- a/spec/outputs/import_global.lua
+++ b/spec/outputs/import_global.lua
@@ -1,6 +1,6 @@
1do 1do
2 local math = math
3 local print = print 2 local print = print
3 local math = math
4 print("hello") 4 print("hello")
5 math.random(10) 5 math.random(10)
6end 6end
@@ -33,19 +33,19 @@ end
33do 33do
34 local func 34 local func
35 func = function(x, y) 35 func = function(x, y)
36 local type = type
36 local tostring = tostring 37 local tostring = tostring
37 local print = print 38 local print = print
38 local type = type
39 return type(x, tostring(y, print)) 39 return type(x, tostring(y, print))
40 end 40 end
41 func(1, 2) 41 func(1, 2)
42end 42end
43do 43do
44 local print = print 44 local xpcall = xpcall
45 local tostring = tostring
46 local func = func 45 local func = func
47 local world = world 46 local world = world
48 local xpcall = xpcall 47 local tostring = tostring
48 local print = print
49 xpcall(function() 49 xpcall(function()
50 return func("hello " .. tostring(world)) 50 return func("hello " .. tostring(world))
51 end, function(err) 51 end, function(err)
@@ -77,16 +77,16 @@ do
77 end 77 end
78end 78end
79do 79do
80 local lowercase = lowercase
80 local tostring = tostring 81 local tostring = tostring
81 local Uppercase = Uppercase 82 local Uppercase = Uppercase
82 local lowercase = lowercase
83 local foobar = "all " .. tostring(lowercase) 83 local foobar = "all " .. tostring(lowercase)
84 FooBar = "pascal case" 84 FooBar = "pascal case"
85 FOOBAR = "all " .. tostring(Uppercase) 85 FOOBAR = "all " .. tostring(Uppercase)
86end 86end
87do 87do
88 local print = print
89 local setmetatable = setmetatable 88 local setmetatable = setmetatable
89 local print = print
90 do 90 do
91 local _class_0 91 local _class_0
92 local _base_0 = { } 92 local _base_0 = { }
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 10f8719..e79b857 100644
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -440,7 +440,8 @@ private:
440 Scope* importingScope = nullptr; 440 Scope* importingScope = nullptr;
441 std::string indent; 441 std::string indent;
442 std::string nl; 442 std::string nl;
443 std::unordered_set<std::string> globals; 443 std::unordered_set<std::string_view> globals;
444 str_list globalList;
444 }; 445 };
445 struct Scope { 446 struct Scope {
446 GlobalMode mode = GlobalMode::None; 447 GlobalMode mode = GlobalMode::None;
@@ -1620,13 +1621,18 @@ private:
1620 return !_varArgs.empty() && _varArgs.top().usedVar ? "end)(...)"s : "end)()"s; 1621 return !_varArgs.empty() && _varArgs.top().usedVar ? "end)(...)"s : "end)()"s;
1621 } 1622 }
1622 1623
1624 void markGlobalImported(const std::string& name) {
1625 if (_importedGlobal->globals.find(name) == _importedGlobal->globals.end() && !isSolidDefined(name)) {
1626 const auto& global = _importedGlobal->globalList.emplace_back(name);
1627 _importedGlobal->globals.insert(global);
1628 _importedGlobal->importingScope->vars->insert_or_assign(name, VarType::LocalConst);
1629 }
1630 }
1631
1623 std::string globalVar(std::string_view var, ast_node* x, AccessType accessType) { 1632 std::string globalVar(std::string_view var, ast_node* x, AccessType accessType) {
1624 std::string str(var); 1633 std::string str(var);
1625 if (_importedGlobal) { 1634 if (_importedGlobal) {
1626 if (_importedGlobal->globals.find(str) == _importedGlobal->globals.end() && !isSolidDefined(str)) { 1635 markGlobalImported(str);
1627 _importedGlobal->globals.insert(str);
1628 _importedGlobal->importingScope->vars->insert_or_assign(str, VarType::LocalConst);
1629 }
1630 } else if (_config.lintGlobalVariable) { 1636 } else if (_config.lintGlobalVariable) {
1631 if (!isLocal(str)) { 1637 if (!isLocal(str)) {
1632 auto key = str + ':' + std::to_string(x->m_begin.m_line) + ':' + std::to_string(x->m_begin.m_col); 1638 auto key = str + ':' + std::to_string(x->m_begin.m_line) + ':' + std::to_string(x->m_begin.m_col);
@@ -4634,11 +4640,7 @@ private:
4634 transformVariable(static_cast<Variable_t*>(item), out); 4640 transformVariable(static_cast<Variable_t*>(item), out);
4635 if (accessType != AccessType::None) { 4641 if (accessType != AccessType::None) {
4636 if (_importedGlobal) { 4642 if (_importedGlobal) {
4637 const auto& str = out.back(); 4643 markGlobalImported(out.back());
4638 if (_importedGlobal->globals.find(str) == _importedGlobal->globals.end() && !isSolidDefined(str)) {
4639 _importedGlobal->globals.insert(str);
4640 _importedGlobal->importingScope->vars->insert_or_assign(str, VarType::LocalConst);
4641 }
4642 } else if (_config.lintGlobalVariable && !isLocal(out.back())) { 4644 } else if (_config.lintGlobalVariable && !isLocal(out.back())) {
4643 auto key = out.back() + ':' + std::to_string(item->m_begin.m_line) + ':' + std::to_string(item->m_begin.m_col); 4645 auto key = out.back() + ':' + std::to_string(item->m_begin.m_line) + ':' + std::to_string(item->m_begin.m_col);
4644 if (_globals.find(key) == _globals.end()) { 4646 if (_globals.find(key) == _globals.end()) {
@@ -5379,7 +5381,7 @@ private:
5379 } 5381 }
5380 if (auto importedGlobal = currentScope().importedGlobal.get()) { 5382 if (auto importedGlobal = currentScope().importedGlobal.get()) {
5381 str_list globalCodes; 5383 str_list globalCodes;
5382 for (const auto& global : importedGlobal->globals) { 5384 for (const auto& global : importedGlobal->globalList) {
5383 globalCodes.emplace_back(importedGlobal->indent + "local "s + global + " = "s + global + importedGlobal->nl); 5385 globalCodes.emplace_back(importedGlobal->indent + "local "s + global + " = "s + global + importedGlobal->nl);
5384 } 5386 }
5385 *importedGlobal->globalCodeLine = join(globalCodes); 5387 *importedGlobal->globalCodeLine = join(globalCodes);
@@ -9361,10 +9363,7 @@ private:
9361 out.push_back(name + " = "s + name); 9363 out.push_back(name + " = "s + name);
9362 } 9364 }
9363 if (_importedGlobal) { 9365 if (_importedGlobal) {
9364 if (_importedGlobal->globals.find(name) == _importedGlobal->globals.end() && !isSolidDefined(name)) { 9366 markGlobalImported(name);
9365 _importedGlobal->globals.insert(name);
9366 _importedGlobal->importingScope->vars->insert_or_assign(name, VarType::LocalConst);
9367 }
9368 } else if (_config.lintGlobalVariable && !isLocal(name)) { 9367 } else if (_config.lintGlobalVariable && !isLocal(name)) {
9369 auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col); 9368 auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col);
9370 if (_globals.find(key) == _globals.end()) { 9369 if (_globals.find(key) == _globals.end()) {
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp
index 6990e31..ab1ba7a 100644
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -1128,7 +1128,7 @@ YueParser::YueParser() {
1128 Statement = 1128 Statement =
1129 ( 1129 (
1130 Import | Export | Global | Macro | MacroInPlace | Label 1130 Import | Export | Global | Macro | MacroInPlace | Label
1131 ) | ( 1131 ) >> space | (
1132 Local | While | Repeat | For | Return | 1132 Local | While | Repeat | For | Return |
1133 BreakLoop | Goto | ShortTabAppending | 1133 BreakLoop | Goto | ShortTabAppending |
1134 LocalAttrib | Backcall | PipeBody | ExpListAssign | ChainAssign | 1134 LocalAttrib | Backcall | PipeBody | ExpListAssign | ChainAssign |