aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-03-23 17:25:28 +0800
committerLi Jin <dragon-fly@qq.com>2025-03-23 17:25:28 +0800
commit08b68269ff6d6f868242dcefce75e1db26e4de61 (patch)
tree3c63e851edc5baa49ad0d180021c2ddecd77f013 /src
parent28bae6517f43c384a828df62b727517e26b3af9b (diff)
downloadyuescript-08b68269ff6d6f868242dcefce75e1db26e4de61.tar.gz
yuescript-08b68269ff6d6f868242dcefce75e1db26e4de61.tar.bz2
yuescript-08b68269ff6d6f868242dcefce75e1db26e4de61.zip
Fixed issue #198.
Diffstat (limited to 'src')
-rw-r--r--src/yuescript/yue_compiler.cpp15
1 files changed, 3 insertions, 12 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 3d98fbc..e3c9d31 100644
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = {
75 "close"s // Lua 5.4 75 "close"s // Lua 5.4
76}; 76};
77 77
78const std::string_view version = "0.27.1"sv; 78const std::string_view version = "0.27.2"sv;
79const std::string_view extension = "yue"sv; 79const std::string_view extension = "yue"sv;
80 80
81class CompileError : public std::logic_error { 81class CompileError : public std::logic_error {
@@ -437,7 +437,6 @@ private:
437#endif 437#endif
438 std::unique_ptr<std::unordered_map<std::string, VarType>> vars; 438 std::unique_ptr<std::unordered_map<std::string, VarType>> vars;
439 std::unique_ptr<std::unordered_set<std::string>> allows; 439 std::unique_ptr<std::unordered_set<std::string>> allows;
440 std::unique_ptr<std::unordered_set<std::string>> globals;
441 }; 440 };
442 std::list<Scope> _scopes; 441 std::list<Scope> _scopes;
443 static const std::string Empty; 442 static const std::string Empty;
@@ -516,10 +515,8 @@ private:
516 int mode = int(std::isupper(name[0]) ? GlobalMode::Capital : GlobalMode::Any); 515 int mode = int(std::isupper(name[0]) ? GlobalMode::Capital : GlobalMode::Any);
517 const auto& current = _scopes.back(); 516 const auto& current = _scopes.back();
518 if (int(current.mode) >= mode) { 517 if (int(current.mode) >= mode) {
519 if (!current.globals) { 518 isDefined = true;
520 isDefined = true; 519 current.vars->insert_or_assign(name, VarType::Global);
521 current.vars->insert_or_assign(name, VarType::Global);
522 }
523 } 520 }
524 decltype(_scopes.back().allows.get()) allows = nullptr; 521 decltype(_scopes.back().allows.get()) allows = nullptr;
525 for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { 522 for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) {
@@ -892,10 +889,6 @@ private:
892 void addGlobalVar(const std::string& name, ast_node* x) { 889 void addGlobalVar(const std::string& name, ast_node* x) {
893 if (isLocal(name)) throw CompileError("can not declare a local variable to be global"sv, x); 890 if (isLocal(name)) throw CompileError("can not declare a local variable to be global"sv, x);
894 auto& scope = _scopes.back(); 891 auto& scope = _scopes.back();
895 if (!scope.globals) {
896 scope.globals = std::make_unique<std::unordered_set<std::string>>();
897 }
898 scope.globals->insert(name);
899 scope.vars->insert_or_assign(name, VarType::Global); 892 scope.vars->insert_or_assign(name, VarType::Global);
900 } 893 }
901 894
@@ -9401,7 +9394,6 @@ private:
9401 auto classDecl = static_cast<ClassDecl_t*>(item); 9394 auto classDecl = static_cast<ClassDecl_t*>(item);
9402 if (classDecl->name) { 9395 if (classDecl->name) {
9403 if (auto var = classDecl->name->item.as<Variable_t>()) { 9396 if (auto var = classDecl->name->item.as<Variable_t>()) {
9404 markVarsGlobal(GlobalMode::Any);
9405 addGlobalVar(variableToString(var), classDecl->name->item); 9397 addGlobalVar(variableToString(var), classDecl->name->item);
9406 } 9398 }
9407 } 9399 }
@@ -9416,7 +9408,6 @@ private:
9416 } 9408 }
9417 break; 9409 break;
9418 case id<GlobalValues_t>(): { 9410 case id<GlobalValues_t>(): {
9419 markVarsGlobal(GlobalMode::Any);
9420 auto values = global->item.to<GlobalValues_t>(); 9411 auto values = global->item.to<GlobalValues_t>();
9421 if (values->valueList) { 9412 if (values->valueList) {
9422 auto expList = x->new_ptr<ExpList_t>(); 9413 auto expList = x->new_ptr<ExpList_t>();