diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-15 17:07:11 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-15 17:07:11 +0800 |
| commit | fced0c4f4101ad7c8d81432a0e8c45d38b72616c (patch) | |
| tree | ed673461c7ef3c614cb5d56905c437f6a6b27454 /spec/outputs | |
| parent | 4177d237e3ed642b2bba5bec13127a44d2b0524d (diff) | |
| download | yuescript-fced0c4f4101ad7c8d81432a0e8c45d38b72616c.tar.gz yuescript-fced0c4f4101ad7c8d81432a0e8c45d38b72616c.tar.bz2 yuescript-fced0c4f4101ad7c8d81432a0e8c45d38b72616c.zip | |
Added `import global` syntax.
Diffstat (limited to 'spec/outputs')
| -rw-r--r-- | spec/outputs/codes_from_doc.lua | 142 | ||||
| -rw-r--r-- | spec/outputs/codes_from_doc_zh.lua | 142 | ||||
| -rw-r--r-- | spec/outputs/import_global.lua | 120 |
3 files changed, 284 insertions, 120 deletions
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua index de5abdd..02f36d1 100644 --- a/spec/outputs/codes_from_doc.lua +++ b/spec/outputs/codes_from_doc.lua | |||
| @@ -409,6 +409,17 @@ local tb = { | |||
| 409 | } | 409 | } |
| 410 | } | 410 | } |
| 411 | do | 411 | do |
| 412 | local math = math | ||
| 413 | local print = print | ||
| 414 | print("hello") | ||
| 415 | math.random(3) | ||
| 416 | end | ||
| 417 | do | ||
| 418 | local print = print | ||
| 419 | print(FLAG) | ||
| 420 | FLAG = 123 | ||
| 421 | end | ||
| 422 | do | ||
| 412 | local insert, concat = table.insert, table.concat | 423 | local insert, concat = table.insert, table.concat |
| 413 | local C, Ct, Cmt | 424 | local C, Ct, Cmt |
| 414 | do | 425 | do |
| @@ -737,36 +748,6 @@ end | |||
| 737 | local first = select(1, ...) | 748 | local first = select(1, ...) |
| 738 | return print(ok, count, first) | 749 | return print(ok, count, first) |
| 739 | end)(fn(true)) | 750 | end)(fn(true)) |
| 740 | local f | ||
| 741 | f = function(...) | ||
| 742 | local t = { | ||
| 743 | n = select("#", ...), | ||
| 744 | ... | ||
| 745 | } | ||
| 746 | print("argument count:", t.n) | ||
| 747 | print("table length:", #t) | ||
| 748 | for i = 1, t.n do | ||
| 749 | print(t[i]) | ||
| 750 | end | ||
| 751 | end | ||
| 752 | f(1, 2, 3) | ||
| 753 | f("a", "b", "c", "d") | ||
| 754 | f() | ||
| 755 | local process | ||
| 756 | process = function(...) | ||
| 757 | local args = { | ||
| 758 | n = select("#", ...), | ||
| 759 | ... | ||
| 760 | } | ||
| 761 | local sum = 0 | ||
| 762 | for i = 1, args.n do | ||
| 763 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 764 | sum = sum + args[i] | ||
| 765 | end | ||
| 766 | end | ||
| 767 | return sum | ||
| 768 | end | ||
| 769 | process(1, nil, 3, nil, 5) | ||
| 770 | Rx.Observable.fromRange(1, 8):filter(function(x) | 751 | Rx.Observable.fromRange(1, 8):filter(function(x) |
| 771 | return x % 2 == 0 | 752 | return x % 2 == 0 |
| 772 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) | 753 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) |
| @@ -1000,6 +981,36 @@ local arg1 = { | |||
| 1000 | a = 0 | 981 | a = 0 |
| 1001 | } | 982 | } |
| 1002 | f2(arg1, arg2) | 983 | f2(arg1, arg2) |
| 984 | local f | ||
| 985 | f = function(...) | ||
| 986 | local t = { | ||
| 987 | n = select("#", ...), | ||
| 988 | ... | ||
| 989 | } | ||
| 990 | print("argument count:", t.n) | ||
| 991 | print("table length:", #t) | ||
| 992 | for i = 1, t.n do | ||
| 993 | print(t[i]) | ||
| 994 | end | ||
| 995 | end | ||
| 996 | f(1, 2, 3) | ||
| 997 | f("a", "b", "c", "d") | ||
| 998 | f() | ||
| 999 | local process | ||
| 1000 | process = function(...) | ||
| 1001 | local args = { | ||
| 1002 | n = select("#", ...), | ||
| 1003 | ... | ||
| 1004 | } | ||
| 1005 | local sum = 0 | ||
| 1006 | for i = 1, args.n do | ||
| 1007 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 1008 | sum = sum + args[i] | ||
| 1009 | end | ||
| 1010 | end | ||
| 1011 | return sum | ||
| 1012 | end | ||
| 1013 | process(1, nil, 3, nil, 5) | ||
| 1003 | f(function() | 1014 | f(function() |
| 1004 | return print("hello") | 1015 | return print("hello") |
| 1005 | end) | 1016 | end) |
| @@ -2917,6 +2928,17 @@ local tb = { | |||
| 2917 | } | 2928 | } |
| 2918 | } | 2929 | } |
| 2919 | do | 2930 | do |
| 2931 | local math = math | ||
| 2932 | local print = print | ||
| 2933 | print("hello") | ||
| 2934 | math.random(3) | ||
| 2935 | end | ||
| 2936 | do | ||
| 2937 | local print = print | ||
| 2938 | print(FLAG) | ||
| 2939 | FLAG = 123 | ||
| 2940 | end | ||
| 2941 | do | ||
| 2920 | local insert, concat = table.insert, table.concat | 2942 | local insert, concat = table.insert, table.concat |
| 2921 | local C, Ct, Cmt | 2943 | local C, Ct, Cmt |
| 2922 | do | 2944 | do |
| @@ -3245,36 +3267,6 @@ end | |||
| 3245 | local first = select(1, ...) | 3267 | local first = select(1, ...) |
| 3246 | return print(ok, count, first) | 3268 | return print(ok, count, first) |
| 3247 | end)(fn(true)) | 3269 | end)(fn(true)) |
| 3248 | local f | ||
| 3249 | f = function(...) | ||
| 3250 | local t = { | ||
| 3251 | n = select("#", ...), | ||
| 3252 | ... | ||
| 3253 | } | ||
| 3254 | print("argument count:", t.n) | ||
| 3255 | print("table length:", #t) | ||
| 3256 | for i = 1, t.n do | ||
| 3257 | print(t[i]) | ||
| 3258 | end | ||
| 3259 | end | ||
| 3260 | f(1, 2, 3) | ||
| 3261 | f("a", "b", "c", "d") | ||
| 3262 | f() | ||
| 3263 | local process | ||
| 3264 | process = function(...) | ||
| 3265 | local args = { | ||
| 3266 | n = select("#", ...), | ||
| 3267 | ... | ||
| 3268 | } | ||
| 3269 | local sum = 0 | ||
| 3270 | for i = 1, args.n do | ||
| 3271 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 3272 | sum = sum + args[i] | ||
| 3273 | end | ||
| 3274 | end | ||
| 3275 | return sum | ||
| 3276 | end | ||
| 3277 | process(1, nil, 3, nil, 5) | ||
| 3278 | Rx.Observable.fromRange(1, 8):filter(function(x) | 3270 | Rx.Observable.fromRange(1, 8):filter(function(x) |
| 3279 | return x % 2 == 0 | 3271 | return x % 2 == 0 |
| 3280 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) | 3272 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) |
| @@ -3538,6 +3530,36 @@ findFirstEven = function(list) | |||
| 3538 | end | 3530 | end |
| 3539 | return nil | 3531 | return nil |
| 3540 | end | 3532 | end |
| 3533 | local f | ||
| 3534 | f = function(...) | ||
| 3535 | local t = { | ||
| 3536 | n = select("#", ...), | ||
| 3537 | ... | ||
| 3538 | } | ||
| 3539 | print("argument count:", t.n) | ||
| 3540 | print("table length:", #t) | ||
| 3541 | for i = 1, t.n do | ||
| 3542 | print(t[i]) | ||
| 3543 | end | ||
| 3544 | end | ||
| 3545 | f(1, 2, 3) | ||
| 3546 | f("a", "b", "c", "d") | ||
| 3547 | f() | ||
| 3548 | local process | ||
| 3549 | process = function(...) | ||
| 3550 | local args = { | ||
| 3551 | n = select("#", ...), | ||
| 3552 | ... | ||
| 3553 | } | ||
| 3554 | local sum = 0 | ||
| 3555 | for i = 1, args.n do | ||
| 3556 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 3557 | sum = sum + args[i] | ||
| 3558 | end | ||
| 3559 | end | ||
| 3560 | return sum | ||
| 3561 | end | ||
| 3562 | process(1, nil, 3, nil, 5) | ||
| 3541 | f(function() | 3563 | f(function() |
| 3542 | return print("hello") | 3564 | return print("hello") |
| 3543 | end) | 3565 | end) |
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index 6a6c38c..87944e0 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua | |||
| @@ -409,6 +409,17 @@ local tb = { | |||
| 409 | } | 409 | } |
| 410 | } | 410 | } |
| 411 | do | 411 | do |
| 412 | local math = math | ||
| 413 | local print = print | ||
| 414 | print("hello") | ||
| 415 | math.random(3) | ||
| 416 | end | ||
| 417 | do | ||
| 418 | local print = print | ||
| 419 | print(FLAG) | ||
| 420 | FLAG = 123 | ||
| 421 | end | ||
| 422 | do | ||
| 412 | local insert, concat = table.insert, table.concat | 423 | local insert, concat = table.insert, table.concat |
| 413 | local C, Ct, Cmt | 424 | local C, Ct, Cmt |
| 414 | do | 425 | do |
| @@ -737,36 +748,6 @@ end | |||
| 737 | local first = select(1, ...) | 748 | local first = select(1, ...) |
| 738 | return print(ok, count, first) | 749 | return print(ok, count, first) |
| 739 | end)(fn(true)) | 750 | end)(fn(true)) |
| 740 | local f | ||
| 741 | f = function(...) | ||
| 742 | local t = { | ||
| 743 | n = select("#", ...), | ||
| 744 | ... | ||
| 745 | } | ||
| 746 | print("参数个数:", t.n) | ||
| 747 | print("表长度:", #t) | ||
| 748 | for i = 1, t.n do | ||
| 749 | print(t[i]) | ||
| 750 | end | ||
| 751 | end | ||
| 752 | f(1, 2, 3) | ||
| 753 | f("a", "b", "c", "d") | ||
| 754 | f() | ||
| 755 | local process | ||
| 756 | process = function(...) | ||
| 757 | local args = { | ||
| 758 | n = select("#", ...), | ||
| 759 | ... | ||
| 760 | } | ||
| 761 | local sum = 0 | ||
| 762 | for i = 1, args.n do | ||
| 763 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 764 | sum = sum + args[i] | ||
| 765 | end | ||
| 766 | end | ||
| 767 | return sum | ||
| 768 | end | ||
| 769 | process(1, nil, 3, nil, 5) | ||
| 770 | Rx.Observable.fromRange(1, 8):filter(function(x) | 751 | Rx.Observable.fromRange(1, 8):filter(function(x) |
| 771 | return x % 2 == 0 | 752 | return x % 2 == 0 |
| 772 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) | 753 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) |
| @@ -994,6 +975,36 @@ local arg1 = { | |||
| 994 | a = 0 | 975 | a = 0 |
| 995 | } | 976 | } |
| 996 | f2(arg1, arg2) | 977 | f2(arg1, arg2) |
| 978 | local f | ||
| 979 | f = function(...) | ||
| 980 | local t = { | ||
| 981 | n = select("#", ...), | ||
| 982 | ... | ||
| 983 | } | ||
| 984 | print("参数个数:", t.n) | ||
| 985 | print("表长度:", #t) | ||
| 986 | for i = 1, t.n do | ||
| 987 | print(t[i]) | ||
| 988 | end | ||
| 989 | end | ||
| 990 | f(1, 2, 3) | ||
| 991 | f("a", "b", "c", "d") | ||
| 992 | f() | ||
| 993 | local process | ||
| 994 | process = function(...) | ||
| 995 | local args = { | ||
| 996 | n = select("#", ...), | ||
| 997 | ... | ||
| 998 | } | ||
| 999 | local sum = 0 | ||
| 1000 | for i = 1, args.n do | ||
| 1001 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 1002 | sum = sum + args[i] | ||
| 1003 | end | ||
| 1004 | end | ||
| 1005 | return sum | ||
| 1006 | end | ||
| 1007 | process(1, nil, 3, nil, 5) | ||
| 997 | f(function() | 1008 | f(function() |
| 998 | return print("hello") | 1009 | return print("hello") |
| 999 | end) | 1010 | end) |
| @@ -2911,6 +2922,17 @@ local tb = { | |||
| 2911 | } | 2922 | } |
| 2912 | } | 2923 | } |
| 2913 | do | 2924 | do |
| 2925 | local math = math | ||
| 2926 | local print = print | ||
| 2927 | print("hello") | ||
| 2928 | math.random(3) | ||
| 2929 | end | ||
| 2930 | do | ||
| 2931 | local print = print | ||
| 2932 | print(FLAG) | ||
| 2933 | FLAG = 123 | ||
| 2934 | end | ||
| 2935 | do | ||
| 2914 | local insert, concat = table.insert, table.concat | 2936 | local insert, concat = table.insert, table.concat |
| 2915 | local C, Ct, Cmt | 2937 | local C, Ct, Cmt |
| 2916 | do | 2938 | do |
| @@ -3239,36 +3261,6 @@ end | |||
| 3239 | local first = select(1, ...) | 3261 | local first = select(1, ...) |
| 3240 | return print(ok, count, first) | 3262 | return print(ok, count, first) |
| 3241 | end)(fn(true)) | 3263 | end)(fn(true)) |
| 3242 | local f | ||
| 3243 | f = function(...) | ||
| 3244 | local t = { | ||
| 3245 | n = select("#", ...), | ||
| 3246 | ... | ||
| 3247 | } | ||
| 3248 | print("参数个数:", t.n) | ||
| 3249 | print("表长度:", #t) | ||
| 3250 | for i = 1, t.n do | ||
| 3251 | print(t[i]) | ||
| 3252 | end | ||
| 3253 | end | ||
| 3254 | f(1, 2, 3) | ||
| 3255 | f("a", "b", "c", "d") | ||
| 3256 | f() | ||
| 3257 | local process | ||
| 3258 | process = function(...) | ||
| 3259 | local args = { | ||
| 3260 | n = select("#", ...), | ||
| 3261 | ... | ||
| 3262 | } | ||
| 3263 | local sum = 0 | ||
| 3264 | for i = 1, args.n do | ||
| 3265 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 3266 | sum = sum + args[i] | ||
| 3267 | end | ||
| 3268 | end | ||
| 3269 | return sum | ||
| 3270 | end | ||
| 3271 | process(1, nil, 3, nil, 5) | ||
| 3272 | Rx.Observable.fromRange(1, 8):filter(function(x) | 3264 | Rx.Observable.fromRange(1, 8):filter(function(x) |
| 3273 | return x % 2 == 0 | 3265 | return x % 2 == 0 |
| 3274 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) | 3266 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) |
| @@ -3526,6 +3518,36 @@ findFirstEven = function(list) | |||
| 3526 | end | 3518 | end |
| 3527 | return nil | 3519 | return nil |
| 3528 | end | 3520 | end |
| 3521 | local f | ||
| 3522 | f = function(...) | ||
| 3523 | local t = { | ||
| 3524 | n = select("#", ...), | ||
| 3525 | ... | ||
| 3526 | } | ||
| 3527 | print("参数个数:", t.n) | ||
| 3528 | print("表长度:", #t) | ||
| 3529 | for i = 1, t.n do | ||
| 3530 | print(t[i]) | ||
| 3531 | end | ||
| 3532 | end | ||
| 3533 | f(1, 2, 3) | ||
| 3534 | f("a", "b", "c", "d") | ||
| 3535 | f() | ||
| 3536 | local process | ||
| 3537 | process = function(...) | ||
| 3538 | local args = { | ||
| 3539 | n = select("#", ...), | ||
| 3540 | ... | ||
| 3541 | } | ||
| 3542 | local sum = 0 | ||
| 3543 | for i = 1, args.n do | ||
| 3544 | if args[i] ~= nil and type(args[i]) == "number" then | ||
| 3545 | sum = sum + args[i] | ||
| 3546 | end | ||
| 3547 | end | ||
| 3548 | return sum | ||
| 3549 | end | ||
| 3550 | process(1, nil, 3, nil, 5) | ||
| 3529 | f(function() | 3551 | f(function() |
| 3530 | return print("hello") | 3552 | return print("hello") |
| 3531 | end) | 3553 | end) |
diff --git a/spec/outputs/import_global.lua b/spec/outputs/import_global.lua new file mode 100644 index 0000000..08a90e2 --- /dev/null +++ b/spec/outputs/import_global.lua | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | do | ||
| 2 | local math = math | ||
| 3 | local print = print | ||
| 4 | print("hello") | ||
| 5 | math.random(10) | ||
| 6 | end | ||
| 7 | do | ||
| 8 | local print = print | ||
| 9 | local value = 1 | ||
| 10 | value = value + 2 | ||
| 11 | print(value) | ||
| 12 | end | ||
| 13 | do | ||
| 14 | local print | ||
| 15 | print = function(msg) | ||
| 16 | return msg | ||
| 17 | end | ||
| 18 | do | ||
| 19 | local math = math | ||
| 20 | print("local") | ||
| 21 | math.random(1) | ||
| 22 | end | ||
| 23 | end | ||
| 24 | do | ||
| 25 | local print = print | ||
| 26 | local tostring | ||
| 27 | tostring = function(v) | ||
| 28 | return "local" | ||
| 29 | end | ||
| 30 | tostring("value") | ||
| 31 | print(tostring(123)) | ||
| 32 | end | ||
| 33 | do | ||
| 34 | local func | ||
| 35 | func = function(x, y) | ||
| 36 | local tostring = tostring | ||
| 37 | local print = print | ||
| 38 | local type = type | ||
| 39 | return type(x, tostring(y, print)) | ||
| 40 | end | ||
| 41 | func(1, 2) | ||
| 42 | end | ||
| 43 | do | ||
| 44 | local print = print | ||
| 45 | local tostring = tostring | ||
| 46 | local func = func | ||
| 47 | local world = world | ||
| 48 | local xpcall = xpcall | ||
| 49 | xpcall(function() | ||
| 50 | return func("hello " .. tostring(world)) | ||
| 51 | end, function(err) | ||
| 52 | return print(err) | ||
| 53 | end) | ||
| 54 | end | ||
| 55 | do | ||
| 56 | local print = print | ||
| 57 | print(FLAG) | ||
| 58 | FLAG = 123 | ||
| 59 | end | ||
| 60 | do | ||
| 61 | local print = print | ||
| 62 | Foo = 10 | ||
| 63 | print(Foo) | ||
| 64 | Foo = Foo + 2 | ||
| 65 | end | ||
| 66 | do | ||
| 67 | local print = print | ||
| 68 | Bar = 1 | ||
| 69 | Baz = 2 | ||
| 70 | print(Bar, Baz) | ||
| 71 | end | ||
| 72 | do | ||
| 73 | local y = y | ||
| 74 | x = 3434 | ||
| 75 | if y then | ||
| 76 | x = 10 | ||
| 77 | end | ||
| 78 | end | ||
| 79 | do | ||
| 80 | local tostring = tostring | ||
| 81 | local Uppercase = Uppercase | ||
| 82 | local lowercase = lowercase | ||
| 83 | local foobar = "all " .. tostring(lowercase) | ||
| 84 | FooBar = "pascal case" | ||
| 85 | FOOBAR = "all " .. tostring(Uppercase) | ||
| 86 | end | ||
| 87 | do | ||
| 88 | local print = print | ||
| 89 | local setmetatable = setmetatable | ||
| 90 | do | ||
| 91 | local _class_0 | ||
| 92 | local _base_0 = { } | ||
| 93 | if _base_0.__index == nil then | ||
| 94 | _base_0.__index = _base_0 | ||
| 95 | end | ||
| 96 | _class_0 = setmetatable({ | ||
| 97 | __init = function() end, | ||
| 98 | __base = _base_0, | ||
| 99 | __name = "A" | ||
| 100 | }, { | ||
| 101 | __index = _base_0, | ||
| 102 | __call = function(cls, ...) | ||
| 103 | local _self_0 = setmetatable({ }, _base_0) | ||
| 104 | cls.__init(_self_0, ...) | ||
| 105 | return _self_0 | ||
| 106 | end | ||
| 107 | }) | ||
| 108 | _base_0.__class = _class_0 | ||
| 109 | A = _class_0 | ||
| 110 | end | ||
| 111 | Flag = 1 | ||
| 112 | const, x, y = "const", 1, 2 | ||
| 113 | print(math, table) | ||
| 114 | end | ||
| 115 | do | ||
| 116 | local X = X | ||
| 117 | X:func(1, 2, 3) | ||
| 118 | X.tag = "abc" | ||
| 119 | return X | ||
| 120 | end | ||
