aboutsummaryrefslogtreecommitdiff
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
parent28bae6517f43c384a828df62b727517e26b3af9b (diff)
downloadyuescript-08b68269ff6d6f868242dcefce75e1db26e4de61.tar.gz
yuescript-08b68269ff6d6f868242dcefce75e1db26e4de61.tar.bz2
yuescript-08b68269ff6d6f868242dcefce75e1db26e4de61.zip
Fixed issue #198.
-rw-r--r--spec/outputs/class.lua44
-rw-r--r--spec/outputs/codes_from_doc.lua10
-rw-r--r--spec/outputs/codes_from_doc_zh.lua10
-rw-r--r--spec/outputs/cond.lua14
-rw-r--r--spec/outputs/unicode/cond.lua16
-rw-r--r--spec/outputs/unicode/with.lua20
-rw-r--r--spec/outputs/with.lua20
-rw-r--r--src/yuescript/yue_compiler.cpp15
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
1231end 1231end
1232do 1232do
1233 local CX 1233 local CX
1234 do 1234 local _class_0
1235 local _class_0 1235 local _base_0 = { }
1236 local _base_0 = { } 1236 if _base_0.__index == nil then
1237 if _base_0.__index == nil then 1237 _base_0.__index = _base_0
1238 _base_0.__index = _base_0
1239 end
1240 _class_0 = setmetatable({
1241 __init = function(self) end,
1242 __base = _base_0,
1243 __name = "CX"
1244 }, {
1245 __index = _base_0,
1246 __call = function(cls, ...)
1247 local _self_0 = setmetatable({ }, _base_0)
1248 cls.__init(_self_0, ...)
1249 return _self_0
1250 end
1251 })
1252 _base_0.__class = _class_0
1253 local self = _class_0;
1254 xa = 1
1255 xb = 1
1256 CX = _class_0
1257 end 1238 end
1239 _class_0 = setmetatable({
1240 __init = function(self) end,
1241 __base = _base_0,
1242 __name = "CX"
1243 }, {
1244 __index = _base_0,
1245 __call = function(cls, ...)
1246 local _self_0 = setmetatable({ }, _base_0)
1247 cls.__init(_self_0, ...)
1248 return _self_0
1249 end
1250 })
1251 _base_0.__class = _class_0
1252 local self = _class_0;
1253 xa = 1
1254 xb = 1
1255 CX = _class_0
1258end 1256end
1259do 1257do
1260 local CY 1258 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
486do 486do
487 a = 1 487 a = 1
488 print("declare all variables as globals") 488 print("declare all variables as globals")
489 local x
490 x = function() 489 x = function()
491 return 1 + y + z 490 return 1 + y + z
492 end 491 end
493 local y, z = 2, 3 492 y, z = 2, 3
494end 493end
495do 494do
496 X = 1 495 X = 1
497 print("only declare upper case variables as globals") 496 print("only declare upper case variables as globals")
498 local a = 1 497 local a = 1
499 local B = 2 498 B = 2
500 local Temp = "a local value" 499 local Temp = "a local value"
501end 500end
502local thing = { 501local thing = {
@@ -2494,17 +2493,16 @@ end
2494do 2493do
2495 a = 1 2494 a = 1
2496 print("declare all variables as globals") 2495 print("declare all variables as globals")
2497 local x
2498 x = function() 2496 x = function()
2499 return 1 + y + z 2497 return 1 + y + z
2500 end 2498 end
2501 local y, z = 2, 3 2499 y, z = 2, 3
2502end 2500end
2503do 2501do
2504 X = 1 2502 X = 1
2505 print("only declare upper case variables as globals") 2503 print("only declare upper case variables as globals")
2506 local a = 1 2504 local a = 1
2507 local B = 2 2505 B = 2
2508 local Temp = "a local value" 2506 local Temp = "a local value"
2509end 2507end
2510local thing = { 2508local 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
486do 486do
487 a = 1 487 a = 1
488 print("预先声明所有变量为全局变量") 488 print("预先声明所有变量为全局变量")
489 local x
490 x = function() 489 x = function()
491 return 1 + y + z 490 return 1 + y + z
492 end 491 end
493 local y, z = 2, 3 492 y, z = 2, 3
494end 493end
495do 494do
496 x = 1 495 x = 1
497 print("只预先声明大写的变量为全局变量") 496 print("只预先声明大写的变量为全局变量")
498 local a = 1 497 local a = 1
499 local B = 2 498 B = 2
500 local Temp = "一个局部值" 499 local Temp = "一个局部值"
501end 500end
502local thing = { 501local thing = {
@@ -2488,17 +2487,16 @@ end
2488do 2487do
2489 a = 1 2488 a = 1
2490 print("预先声明所有变量为全局变量") 2489 print("预先声明所有变量为全局变量")
2491 local x
2492 x = function() 2490 x = function()
2493 return 1 + y + z 2491 return 1 + y + z
2494 end 2492 end
2495 local y, z = 2, 3 2493 y, z = 2, 3
2496end 2494end
2497do 2495do
2498 x = 1 2496 x = 1
2499 print("只预先声明大写的变量为全局变量") 2497 print("只预先声明大写的变量为全局变量")
2500 local a = 1 2498 local a = 1
2501 local B = 2 2499 B = 2
2502 local Temp = "一个局部值" 2500 local Temp = "一个局部值"
2503end 2501end
2504local thing = { 2502local 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
313 end 313 end
314end 314end
315do 315do
316 do 316 local _des_0 = { }
317 local _des_0 = { } 317 if _des_0 then
318 if _des_0 then 318 _M = _des_0
319 _M = _des_0 319 local Thing = _M.Thing
320 local Thing = _M.Thing 320 local _obj_0 = _M
321 local _obj_0 = _M 321 a, b = _obj_0.a, _obj_0.b
322 a, b = _obj_0.a, _obj_0.b
323 end
324 end 322 end
325end 323end
326do 324do
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
314 end 314 end
315end 315end
316do 316do
317 do 317 local _des_0 = { }
318 local _des_0 = { } 318 if _des_0 then
319 if _des_0 then 319 __u6a21_u5757 = _des_0
320 __u6a21_u5757 = _des_0 320 local _u4e1c_u897f = __u6a21_u5757["东西"]
321 local _u4e1c_u897f = __u6a21_u5757["东西"] 321 local a_u529f_u80fd, b_u529f_u80fd
322 local a_u529f_u80fd, b_u529f_u80fd 322 local _obj_0 = __u6a21_u5757
323 local _obj_0 = __u6a21_u5757 323 a_u529f_u80fd, b_u529f_u80fd = _obj_0["a功能"], _obj_0["b功能"]
324 a_u529f_u80fd, b_u529f_u80fd = _obj_0["a功能"], _obj_0["b功能"]
325 end
326 end 324 end
327end 325end
328do 326do
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
161 } 161 }
162end 162end
163do 163do
164 do 164 local _with_0 = _u5b9e_u5fc3_u77e9_u5f62({
165 local _with_0 = _u5b9e_u5fc3_u77e9_u5f62({ 165 ["宽"] = w,
166 ["宽"] = w, 166 ["高"] = h,
167 ["高"] = h, 167 ["颜色"] = 0x66000000
168 ["颜色"] = 0x66000000 168 })
169 }) 169 _u63a9_u7801 = _with_0
170 _u63a9_u7801 = _with_0 170 if _with_0 ~= nil then
171 if _with_0 ~= nil then 171 _with_0["触摸启用"] = true
172 _with_0["触摸启用"] = true 172 _with_0["吞噬触摸"] = true
173 _with_0["吞噬触摸"] = true
174 end
175 end 173 end
176end 174end
177do 175do
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
158 } 158 }
159end 159end
160do 160do
161 do 161 local _with_0 = SolidRect({
162 local _with_0 = SolidRect({ 162 width = w,
163 width = w, 163 height = h,
164 height = h, 164 color = 0x66000000
165 color = 0x66000000 165 })
166 }) 166 mask = _with_0
167 mask = _with_0 167 if _with_0 ~= nil then
168 if _with_0 ~= nil then 168 _with_0.touchEnabled = true
169 _with_0.touchEnabled = true 169 _with_0.swallowTouches = true
170 _with_0.swallowTouches = true
171 end
172 end 170 end
173end 171end
174do 172do
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>();