From afb046316a15a2c3702b1f056456b58e9356e693 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 15 Jan 2026 17:54:59 +0800 Subject: Added const attributes. --- makefile | 2 + spec/outputs/5.1/import_global.lua | 120 +++++++++++++++++++++++++++++++++++++ spec/outputs/codes_from_doc.lua | 60 ++++++++++++------- spec/outputs/codes_from_doc_zh.lua | 60 ++++++++++++------- spec/outputs/import_global.lua | 46 +++++++------- src/yuescript/yue_compiler.cpp | 4 +- 6 files changed, 224 insertions(+), 68 deletions(-) create mode 100644 spec/outputs/5.1/import_global.lua diff --git a/makefile b/makefile index e0cdd91..047718a 100644 --- a/makefile +++ b/makefile @@ -435,6 +435,7 @@ test: debug @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(TEST_OUTPUT)/5.1/loops.lua --target=5.1 @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(TEST_OUTPUT)/5.1/try_catch.lua --target=5.1 @./$(BIN_NAME) $(TEST_INPUT)/attrib.yue -o $(TEST_OUTPUT)/5.1/attrib.lua --target=5.1 + @./$(BIN_NAME) $(TEST_INPUT)/import_global.yue -o $(TEST_OUTPUT)/5.1/import_global.lua --target=5.1 @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(TEST_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 @./$(BIN_NAME) -e spec/inputs/compile_doc.yue $(TEST_OUTPUT) @echo -en "Compile time: " @@ -454,6 +455,7 @@ gen: release @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(GEN_OUTPUT)/5.1/loops.lua --target=5.1 @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(GEN_OUTPUT)/5.1/try_catch.lua --target=5.1 @./$(BIN_NAME) $(TEST_INPUT)/attrib.yue -o $(GEN_OUTPUT)/5.1/attrib.lua --target=5.1 + @./$(BIN_NAME) $(TEST_INPUT)/import_global.yue -o $(GEN_OUTPUT)/5.1/import_global.lua --target=5.1 @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(GEN_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 @./$(BIN_NAME) -e spec/inputs/compile_doc.yue $(GEN_OUTPUT) @echo -en "Compile time: " diff --git a/spec/outputs/5.1/import_global.lua b/spec/outputs/5.1/import_global.lua new file mode 100644 index 0000000..c748c78 --- /dev/null +++ b/spec/outputs/5.1/import_global.lua @@ -0,0 +1,120 @@ +do + local print = print + local math = math + print("hello") + math.random(10) +end +do + local print = print + local value = 1 + value = value + 2 + print(value) +end +do + local print + print = function(msg) + return msg + end + do + local math = math + print("local") + math.random(1) + end +end +do + local print = print + local tostring + tostring = function(v) + return "local" + end + tostring("value") + print(tostring(123)) +end +do + local func + func = function(x, y) + local type = type + local tostring = tostring + local print = print + return type(x, tostring(y, print)) + end + func(1, 2) +end +do + local xpcall = xpcall + local func = func + local world = world + local tostring = tostring + local print = print + xpcall(function() + return func("hello " .. tostring(world)) + end, function(err) + return print(err) + end) +end +do + local print = print + print(FLAG) + FLAG = 123 +end +do + local print = print + Foo = 10 + print(Foo) + Foo = Foo + 2 +end +do + local print = print + Bar = 1 + Baz = 2 + print(Bar, Baz) +end +do + local y = y + x = 3434 + if y then + x = 10 + end +end +do + local lowercase = lowercase + local tostring = tostring + local Uppercase = Uppercase + local foobar = "all " .. tostring(lowercase) + FooBar = "pascal case" + FOOBAR = "all " .. tostring(Uppercase) +end +do + local setmetatable = setmetatable + local print = print + do + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "A" + }, { + __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 + A = _class_0 + end + Flag = 1 + const, x, y = "const", 1, 2 + print(math, table) +end +do + local X = X + X:func(1, 2, 3) + X.tag = "abc" + return X +end diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua index 84dfc4e..1df7cef 100644 --- a/spec/outputs/codes_from_doc.lua +++ b/spec/outputs/codes_from_doc.lua @@ -408,17 +408,6 @@ local tb = { } } } -do - local print = print - local math = math - print("hello") - math.random(3) -end -do - local print = print - print(FLAG) - FLAG = 123 -end do local insert, concat = table.insert, table.concat local C, Ct, Cmt @@ -452,6 +441,25 @@ do local _obj_0 = require("export") one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1] end +do + local tostring = tostring + local concat = table.concat + print(concat({ + "a", + tostring(1) + })) +end +do + local print = print + local math = math + print("hello") + math.random(3) +end +do + local print = print + print(FLAG) + FLAG = 123 +end local _module_0 = { } local a, b, c = 1, 2, 3 _module_0["a"], _module_0["b"], _module_0["c"] = a, b, c @@ -2927,17 +2935,6 @@ local tb = { } } } -do - local print = print - local math = math - print("hello") - math.random(3) -end -do - local print = print - print(FLAG) - FLAG = 123 -end do local insert, concat = table.insert, table.concat local C, Ct, Cmt @@ -2971,6 +2968,25 @@ do local _obj_0 = require("export") one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1] end +do + local tostring = tostring + local concat = table.concat + print(concat({ + "a", + tostring(1) + })) +end +do + local print = print + local math = math + print("hello") + math.random(3) +end +do + local print = print + print(FLAG) + FLAG = 123 +end local _module_0 = { } local a, b, c = 1, 2, 3 _module_0["a"], _module_0["b"], _module_0["c"] = a, b, c diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index d4e8b6a..b407895 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua @@ -408,17 +408,6 @@ local tb = { } } } -do - local print = print - local math = math - print("hello") - math.random(3) -end -do - local print = print - print(FLAG) - FLAG = 123 -end do local insert, concat = table.insert, table.concat local C, Ct, Cmt @@ -452,6 +441,25 @@ do local _obj_0 = require("export") one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1] end +do + local tostring = tostring + local concat = table.concat + print(concat({ + "a", + tostring(1) + })) +end +do + local print = print + local math = math + print("hello") + math.random(3) +end +do + local print = print + print(FLAG) + FLAG = 123 +end local _module_0 = { } local a, b, c = 1, 2, 3 _module_0["a"], _module_0["b"], _module_0["c"] = a, b, c @@ -2921,17 +2929,6 @@ local tb = { } } } -do - local print = print - local math = math - print("hello") - math.random(3) -end -do - local print = print - print(FLAG) - FLAG = 123 -end do local insert, concat = table.insert, table.concat local C, Ct, Cmt @@ -2965,6 +2962,25 @@ do local _obj_0 = require("export") one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1] end +do + local tostring = tostring + local concat = table.concat + print(concat({ + "a", + tostring(1) + })) +end +do + local print = print + local math = math + print("hello") + math.random(3) +end +do + local print = print + print(FLAG) + FLAG = 123 +end local _module_0 = { } local a, b, c = 1, 2, 3 _module_0["a"], _module_0["b"], _module_0["c"] = a, b, c diff --git a/spec/outputs/import_global.lua b/spec/outputs/import_global.lua index c748c78..f76b4fe 100644 --- a/spec/outputs/import_global.lua +++ b/spec/outputs/import_global.lua @@ -1,11 +1,11 @@ do - local print = print - local math = math + local print = print + local math = math print("hello") math.random(10) end do - local print = print + local print = print local value = 1 value = value + 2 print(value) @@ -16,13 +16,13 @@ do return msg end do - local math = math + local math = math print("local") math.random(1) end end do - local print = print + local print = print local tostring tostring = function(v) return "local" @@ -33,19 +33,19 @@ end do local func func = function(x, y) - local type = type - local tostring = tostring - local print = print + local type = type + local tostring = tostring + local print = print return type(x, tostring(y, print)) end func(1, 2) end do - local xpcall = xpcall - local func = func - local world = world - local tostring = tostring - local print = print + local xpcall = xpcall + local func = func + local world = world + local tostring = tostring + local print = print xpcall(function() return func("hello " .. tostring(world)) end, function(err) @@ -53,40 +53,40 @@ do end) end do - local print = print + local print = print print(FLAG) FLAG = 123 end do - local print = print + local print = print Foo = 10 print(Foo) Foo = Foo + 2 end do - local print = print + local print = print Bar = 1 Baz = 2 print(Bar, Baz) end do - local y = y + local y = y x = 3434 if y then x = 10 end end do - local lowercase = lowercase - local tostring = tostring - local Uppercase = Uppercase + local lowercase = lowercase + local tostring = tostring + local Uppercase = Uppercase local foobar = "all " .. tostring(lowercase) FooBar = "pascal case" FOOBAR = "all " .. tostring(Uppercase) end do - local setmetatable = setmetatable - local print = print + local setmetatable = setmetatable + local print = print do local _class_0 local _base_0 = { } @@ -113,7 +113,7 @@ do print(math, table) end do - local X = X + local X = X X:func(1, 2, 3) X.tag = "abc" return X diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index e79b857..09d0bd1 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -5380,9 +5380,11 @@ private: } } if (auto importedGlobal = currentScope().importedGlobal.get()) { + int target = getLuaTarget(block); + auto attrib = target >= 504 ? " "s : Empty; str_list globalCodes; for (const auto& global : importedGlobal->globalList) { - globalCodes.emplace_back(importedGlobal->indent + "local "s + global + " = "s + global + importedGlobal->nl); + globalCodes.emplace_back(importedGlobal->indent + "local "s + global + attrib + " = "s + global + importedGlobal->nl); } *importedGlobal->globalCodeLine = join(globalCodes); _importedGlobal = nullptr; -- cgit v1.2.3-55-g6feb