From 08b68269ff6d6f868242dcefce75e1db26e4de61 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 23 Mar 2025 17:25:28 +0800 Subject: Fixed issue #198. --- spec/outputs/class.lua | 44 ++++++++++++++++++-------------------- spec/outputs/codes_from_doc.lua | 10 ++++----- spec/outputs/codes_from_doc_zh.lua | 10 ++++----- spec/outputs/cond.lua | 14 ++++++------ spec/outputs/unicode/cond.lua | 16 ++++++-------- spec/outputs/unicode/with.lua | 20 ++++++++--------- spec/outputs/with.lua | 20 ++++++++--------- src/yuescript/yue_compiler.cpp | 15 +++---------- 8 files changed, 63 insertions(+), 86 deletions(-) diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index aaea33c..07efde4 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -1231,30 +1231,28 @@ do end do local CX - do - local _class_0 - local _base_0 = { } - if _base_0.__index == nil then - _base_0.__index = _base_0 - end - _class_0 = setmetatable({ - __init = function(self) end, - __base = _base_0, - __name = "CX" - }, { - __index = _base_0, - __call = function(cls, ...) - local _self_0 = setmetatable({ }, _base_0) - cls.__init(_self_0, ...) - return _self_0 - end - }) - _base_0.__class = _class_0 - local self = _class_0; - xa = 1 - xb = 1 - CX = _class_0 + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 end + _class_0 = setmetatable({ + __init = function(self) end, + __base = _base_0, + __name = "CX" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0; + xa = 1 + xb = 1 + CX = _class_0 end do local CY diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua index a37b459..27f8de5 100644 --- a/spec/outputs/codes_from_doc.lua +++ b/spec/outputs/codes_from_doc.lua @@ -486,17 +486,16 @@ end do a = 1 print("declare all variables as globals") - local x x = function() return 1 + y + z end - local y, z = 2, 3 + y, z = 2, 3 end do X = 1 print("only declare upper case variables as globals") local a = 1 - local B = 2 + B = 2 local Temp = "a local value" end local thing = { @@ -2494,17 +2493,16 @@ end do a = 1 print("declare all variables as globals") - local x x = function() return 1 + y + z end - local y, z = 2, 3 + y, z = 2, 3 end do X = 1 print("only declare upper case variables as globals") local a = 1 - local B = 2 + B = 2 local Temp = "a local value" end local thing = { diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index a23044d..ffa0483 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua @@ -486,17 +486,16 @@ end do a = 1 print("预先声明所有变量为全局变量") - local x x = function() return 1 + y + z end - local y, z = 2, 3 + y, z = 2, 3 end do x = 1 print("只预先声明大写的变量为全局变量") local a = 1 - local B = 2 + B = 2 local Temp = "一个局部值" end local thing = { @@ -2488,17 +2487,16 @@ end do a = 1 print("预先声明所有变量为全局变量") - local x x = function() return 1 + y + z end - local y, z = 2, 3 + y, z = 2, 3 end do x = 1 print("只预先声明大写的变量为全局变量") local a = 1 - local B = 2 + B = 2 local Temp = "一个局部值" end local thing = { diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index 083419a..e194909 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua @@ -313,14 +313,12 @@ do end end do - do - local _des_0 = { } - if _des_0 then - _M = _des_0 - local Thing = _M.Thing - local _obj_0 = _M - a, b = _obj_0.a, _obj_0.b - end + local _des_0 = { } + if _des_0 then + _M = _des_0 + local Thing = _M.Thing + local _obj_0 = _M + a, b = _obj_0.a, _obj_0.b end end do diff --git a/spec/outputs/unicode/cond.lua b/spec/outputs/unicode/cond.lua index 7229001..5e86228 100644 --- a/spec/outputs/unicode/cond.lua +++ b/spec/outputs/unicode/cond.lua @@ -314,15 +314,13 @@ do end end do - do - local _des_0 = { } - if _des_0 then - __u6a21_u5757 = _des_0 - local _u4e1c_u897f = __u6a21_u5757["东西"] - local a_u529f_u80fd, b_u529f_u80fd - local _obj_0 = __u6a21_u5757 - a_u529f_u80fd, b_u529f_u80fd = _obj_0["a功能"], _obj_0["b功能"] - end + local _des_0 = { } + if _des_0 then + __u6a21_u5757 = _des_0 + local _u4e1c_u897f = __u6a21_u5757["东西"] + local a_u529f_u80fd, b_u529f_u80fd + local _obj_0 = __u6a21_u5757 + a_u529f_u80fd, b_u529f_u80fd = _obj_0["a功能"], _obj_0["b功能"] end end do diff --git a/spec/outputs/unicode/with.lua b/spec/outputs/unicode/with.lua index 7a5ba00..3a32a27 100644 --- a/spec/outputs/unicode/with.lua +++ b/spec/outputs/unicode/with.lua @@ -161,17 +161,15 @@ do } end do - do - local _with_0 = _u5b9e_u5fc3_u77e9_u5f62({ - ["宽"] = w, - ["高"] = h, - ["颜色"] = 0x66000000 - }) - _u63a9_u7801 = _with_0 - if _with_0 ~= nil then - _with_0["触摸启用"] = true - _with_0["吞噬触摸"] = true - end + local _with_0 = _u5b9e_u5fc3_u77e9_u5f62({ + ["宽"] = w, + ["高"] = h, + ["颜色"] = 0x66000000 + }) + _u63a9_u7801 = _with_0 + if _with_0 ~= nil then + _with_0["触摸启用"] = true + _with_0["吞噬触摸"] = true end end do diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua index 5d33bdb..1a795c1 100644 --- a/spec/outputs/with.lua +++ b/spec/outputs/with.lua @@ -158,17 +158,15 @@ do } end do - do - local _with_0 = SolidRect({ - width = w, - height = h, - color = 0x66000000 - }) - mask = _with_0 - if _with_0 ~= nil then - _with_0.touchEnabled = true - _with_0.swallowTouches = true - end + local _with_0 = SolidRect({ + width = w, + height = h, + color = 0x66000000 + }) + mask = _with_0 + if _with_0 ~= nil then + _with_0.touchEnabled = true + _with_0.swallowTouches = true end end do 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 Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.27.1"sv; +const std::string_view version = "0.27.2"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -437,7 +437,6 @@ private: #endif std::unique_ptr> vars; std::unique_ptr> allows; - std::unique_ptr> globals; }; std::list _scopes; static const std::string Empty; @@ -516,10 +515,8 @@ private: int mode = int(std::isupper(name[0]) ? GlobalMode::Capital : GlobalMode::Any); const auto& current = _scopes.back(); if (int(current.mode) >= mode) { - if (!current.globals) { - isDefined = true; - current.vars->insert_or_assign(name, VarType::Global); - } + isDefined = true; + current.vars->insert_or_assign(name, VarType::Global); } decltype(_scopes.back().allows.get()) allows = nullptr; for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { @@ -892,10 +889,6 @@ private: void addGlobalVar(const std::string& name, ast_node* x) { if (isLocal(name)) throw CompileError("can not declare a local variable to be global"sv, x); auto& scope = _scopes.back(); - if (!scope.globals) { - scope.globals = std::make_unique>(); - } - scope.globals->insert(name); scope.vars->insert_or_assign(name, VarType::Global); } @@ -9401,7 +9394,6 @@ private: auto classDecl = static_cast(item); if (classDecl->name) { if (auto var = classDecl->name->item.as()) { - markVarsGlobal(GlobalMode::Any); addGlobalVar(variableToString(var), classDecl->name->item); } } @@ -9416,7 +9408,6 @@ private: } break; case id(): { - markVarsGlobal(GlobalMode::Any); auto values = global->item.to(); if (values->valueList) { auto expList = x->new_ptr(); -- cgit v1.2.3-55-g6feb