aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-15 17:20:24 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-15 17:20:24 +0800
commit079f563e63d2d12db92db9226e248d5d5f064e3d (patch)
tree495e2de278356768e7d460fe89426195984acb0e /src
parentfced0c4f4101ad7c8d81432a0e8c45d38b72616c (diff)
downloadyuescript-079f563e63d2d12db92db9226e248d5d5f064e3d.tar.gz
yuescript-079f563e63d2d12db92db9226e248d5d5f064e3d.tar.bz2
yuescript-079f563e63d2d12db92db9226e248d5d5f064e3d.zip
Fixed globals importing order.
Diffstat (limited to 'src')
-rw-r--r--src/yuescript/yue_compiler.cpp29
-rw-r--r--src/yuescript/yue_parser.cpp2
2 files changed, 15 insertions, 16 deletions
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 |