aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs
diff options
context:
space:
mode:
Diffstat (limited to 'spec/outputs')
-rw-r--r--spec/outputs/codes_from_doc.lua3916
-rw-r--r--spec/outputs/codes_from_doc_zh.lua3916
-rw-r--r--spec/outputs/compile_doc.lua135
3 files changed, 4048 insertions, 3919 deletions
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index b8985e7..889105a 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -76,6 +76,123 @@ end
76local _u1f31b = "月之脚本" 76local _u1f31b = "月之脚本"
77_module_0["🌛"] = _u1f31b 77_module_0["🌛"] = _u1f31b
78return _module_0 78return _module_0
79local _module_0 = { }
80local p, to_lua
81do
82 local _obj_0 = require("yue")
83 p, to_lua = _obj_0.p, _obj_0.to_lua
84end
85local inventory = {
86 equipment = {
87 "sword",
88 "shield"
89 },
90 items = {
91 {
92 name = "potion",
93 count = 10
94 },
95 {
96 name = "bread",
97 count = 3
98 }
99 }
100}
101local map
102map = function(arr, action)
103 local _accum_0 = { }
104 local _len_0 = 1
105 for _index_0 = 1, #arr do
106 local item = arr[_index_0]
107 _accum_0[_len_0] = action(item)
108 _len_0 = _len_0 + 1
109 end
110 return _accum_0
111end
112local filter
113filter = function(arr, cond)
114 local _accum_0 = { }
115 local _len_0 = 1
116 for _index_0 = 1, #arr do
117 local item = arr[_index_0]
118 if cond(item) then
119 _accum_0[_len_0] = item
120 _len_0 = _len_0 + 1
121 end
122 end
123 return _accum_0
124end
125local reduce
126reduce = function(arr, init, action)
127 for _index_0 = 1, #arr do
128 local item = arr[_index_0]
129 init = action(init, item)
130 end
131 return init
132end
133print(reduce(filter(map({
134 1,
135 2,
136 3
137}, function(x)
138 return x * 2
139end), function(x)
140 return x > 4
141end), 0, function(a, b)
142 return a + b
143end))
144local apple = setmetatable({
145 size = 15,
146}, {
147 __index = {
148 color = 0x00ffff
149 }
150})
151if (getmetatable(apple) ~= nil) then
152 p(apple.size, apple.color, getmetatable(apple).__index)
153end
154local _u1f31b = "月之脚本"
155_module_0["🌛"] = _u1f31b
156return _module_0
157local area = 6.2831853071796 * 5
158print('hello world')
159do
160 assert(item ~= nil)
161end
162local value = item
163if (f1() and f2() and f3()) then
164 print("OK")
165end
166do
167 local funcA
168 funcA = function() end
169end
170local funcA
171funcA = function()
172 return "fail to assign to the Yue macro defined variable"
173end
174do
175local function funcB() end
176end
177local funcB
178funcB = function()
179 return "fail to assign to the Lua macro defined variable"
180end
181do
182-- raw Lua codes insertion
183if cond then
184 print("output")
185end
186end
187print("yuescript")
188print(2)
189print("Valid enum type:", "Static")
190do
191 print(123, "hello")
192end
193do
194 print(123, "hello")
195end
79local area = 6.2831853071796 * 5 196local area = 6.2831853071796 * 5
80print('hello world') 197print('hello world')
81do 198do
@@ -408,6 +525,430 @@ local tb = {
408 } 525 }
409 } 526 }
410} 527}
528if tb ~= nil then
529 tb:func()
530end
531if tb ~= nil then
532 tb:func()
533end
534print(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5)
535local a = 5
536print(1 <= a and a <= 10)
537local v
538v = function(x)
539 print(x)
540 return x
541end
542print((function()
543 local _cond_0 = v(2)
544 if not (v(1) < _cond_0) then
545 return false
546 else
547 return _cond_0 <= v(3)
548 end
549end)())
550print((function()
551 local _cond_0 = v(2)
552 if not (v(1) > _cond_0) then
553 return false
554 else
555 return _cond_0 <= v(3)
556 end
557end)())
558local tab = { }
559tab[#tab + 1] = "Value"
560local tbA = {
561 1,
562 2,
563 3
564}
565local tbB = {
566 4,
567 5,
568 6
569}
570local _len_0 = #tbA + 1
571for _index_0 = 1, #tbB do
572 local _elm_0 = tbB[_index_0]
573 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
574end
575local parts = {
576 "shoulders",
577 "knees"
578}
579local lyrics
580do
581 local _tab_0 = {
582 "head"
583 }
584 local _idx_0 = 1
585 for _key_0, _value_0 in pairs(parts) do
586 if _idx_0 == _key_0 then
587 _tab_0[#_tab_0 + 1] = _value_0
588 _idx_0 = _idx_0 + 1
589 else
590 _tab_0[_key_0] = _value_0
591 end
592 end
593 _tab_0[#_tab_0 + 1] = "and"
594 _tab_0[#_tab_0 + 1] = "toes"
595 lyrics = _tab_0
596end
597local copy
598do
599 local _tab_0 = { }
600 local _idx_0 = 1
601 for _key_0, _value_0 in pairs(other) do
602 if _idx_0 == _key_0 then
603 _tab_0[#_tab_0 + 1] = _value_0
604 _idx_0 = _idx_0 + 1
605 else
606 _tab_0[_key_0] = _value_0
607 end
608 end
609 copy = _tab_0
610end
611local a = {
612 1,
613 2,
614 3,
615 x = 1
616}
617local b = {
618 4,
619 5,
620 y = 1
621}
622local merge
623local _tab_0 = { }
624local _idx_0 = 1
625for _key_0, _value_0 in pairs(a) do
626 if _idx_0 == _key_0 then
627 _tab_0[#_tab_0 + 1] = _value_0
628 _idx_0 = _idx_0 + 1
629 else
630 _tab_0[_key_0] = _value_0
631 end
632end
633local _idx_1 = 1
634for _key_0, _value_0 in pairs(b) do
635 if _idx_1 == _key_0 then
636 _tab_0[#_tab_0 + 1] = _value_0
637 _idx_1 = _idx_1 + 1
638 else
639 _tab_0[_key_0] = _value_0
640 end
641end
642merge = _tab_0
643local last
644do
645 local _item_0 = data.items
646 last = _item_0[#_item_0]
647end
648local second_last
649do
650 local _item_0 = data.items
651 second_last = _item_0[#_item_0 - 1]
652end
653local _obj_0 = data.items
654_obj_0[#_obj_0] = 1
655local mt = { }
656local add
657add = function(self, right)
658 return setmetatable({
659 value = self.value + right.value
660 }, mt)
661end
662mt.__add = add
663local a = setmetatable({
664 value = 1
665}, mt)
666local b = setmetatable({
667 value = 2
668}, {
669 __add = add
670})
671local c = setmetatable({
672 value = 3
673}, {
674 __add = mt.__add
675})
676local d = a + b + c
677print(d.value)
678local _ <close> = setmetatable({ }, {
679 __close = function()
680 return print("out of scope")
681 end
682})
683local tb = setmetatable({ }, {
684 ["value"] = 123
685})
686getmetatable(tb).__index = getmetatable(tb)
687print(tb.value)
688setmetatable(tb, {
689 __index = {
690 item = "hello"
691 }
692})
693print(tb.item)
694local item, new, close, getter
695do
696 local _obj_0 = tb
697 item, new = _obj_0[1], _obj_0.new
698 do
699 local _obj_1 = getmetatable(_obj_0)
700 close, getter = _obj_1.__close, _obj_1.__index
701 end
702end
703print(item, new, close, getter)
704do
705 local _obj_0 = func
706 if _obj_0 ~= nil then
707 _obj_0()
708 end
709end
710print((function()
711 local _obj_0 = abc
712 if _obj_0 ~= nil then
713 local _obj_1 = _obj_0["hello world"]
714 if _obj_1 ~= nil then
715 return _obj_1.xyz
716 end
717 return nil
718 end
719 return nil
720end)())
721local x
722do
723 local _obj_0 = tab
724 if _obj_0 ~= nil then
725 x = _obj_0.value
726 end
727end
728local len = (function()
729 local _obj_0 = utf8
730 if _obj_0 ~= nil then
731 return _obj_0.len
732 end
733 return nil
734end)() or (function()
735 local _obj_0 = string
736 if _obj_0 ~= nil then
737 return _obj_0.len
738 end
739 return nil
740end)() or function(o)
741 return #o
742end
743if print and (x ~= nil) then
744 print(x)
745end
746local _with_0 = io.open("test.txt", "w")
747if _with_0 ~= nil then
748 _with_0:write("hello")
749 _with_0:close()
750end
751print("hello")
752print(1, 2)
753print(1, 2, 3)
754print(render(emit(parse(extract(readFile("example.txt"), language, { }), language))))
755local a, b, c, d
756if b ~= nil then
757 a = b
758else
759 if c ~= nil then
760 a = c
761 else
762 a = d
763 end
764end
765func((function()
766 if a ~= nil then
767 return a
768 else
769 return { }
770 end
771end)())
772if a == nil then
773 a = false
774end
775local list = {
776 1,
777 2,
778 3
779}
780func({
781 1,
782 2,
783 3
784})
785local f
786f = function()
787 return {
788 1,
789 2,
790 3
791 }
792end
793local tb = {
794 name = "abc",
795 values = {
796 "a",
797 "b",
798 "c"
799 },
800 objects = {
801 {
802 name = "a",
803 value = 1,
804 func = function(self)
805 return self.value + 1
806 end,
807 tb = {
808 fieldA = 1
809 }
810 },
811 {
812 name = "b",
813 value = 2,
814 func = function(self)
815 return self.value + 2
816 end,
817 tb = { }
818 }
819 }
820}
821do
822 local insert, concat = table.insert, table.concat
823 local C, Ct, Cmt
824 do
825 local _obj_0 = require("lpeg")
826 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
827 end
828 local x, y, z
829 do
830 local _obj_0 = require('mymodule')
831 x, y, z = _obj_0.x, _obj_0.y, _obj_0.z
832 end
833 local a, b, c
834 local _obj_0 = require('module')
835 a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
836end
837do
838 local module = require('module')
839 local module_x = require('module_x')
840 local d_a_s_h_e_s = require("d-a-s-h-e-s")
841 local part = require("module.part")
842end
843do
844 local PlayerModule = require("player")
845 local C, Ct, Cmt
846 do
847 local _obj_0 = require("lpeg")
848 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
849 end
850 local one, two, ch
851 local _obj_0 = require("export")
852 one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1]
853end
854do
855 local tostring <const> = tostring
856 local concat <const> = table.concat
857 print(concat({
858 "a",
859 tostring(1)
860 }))
861end
862do
863 local print <const> = print
864 local math <const> = math
865 print("hello")
866 math.random(3)
867end
868do
869 local print <const> = print
870 print(FLAG)
871 FLAG = 123
872end
873local _module_0 = { }
874local a, b, c = 1, 2, 3
875_module_0["a"], _module_0["b"], _module_0["c"] = a, b, c
876local cool = "cat"
877_module_0["cool"] = cool
878local What
879if this then
880 What = "abc"
881else
882 What = "def"
883end
884_module_0["What"] = What
885local y
886y = function()
887 local hallo = 3434
888end
889_module_0["y"] = y
890local Something
891local _class_0
892local _base_0 = {
893 umm = "cool"
894}
895if _base_0.__index == nil then
896 _base_0.__index = _base_0
897end
898_class_0 = setmetatable({
899 __init = function() end,
900 __base = _base_0,
901 __name = "Something"
902}, {
903 __index = _base_0,
904 __call = function(cls, ...)
905 local _self_0 = setmetatable({ }, _base_0)
906 cls.__init(_self_0, ...)
907 return _self_0
908 end
909})
910_base_0.__class = _class_0
911Something = _class_0
912_module_0["Something"] = Something
913return _module_0
914local _module_0 = { }
915local loadstring, tolua
916do
917 local _obj_0 = yue
918 loadstring, tolua = _obj_0.loadstring, _obj_0.to_lua
919end
920_module_0["loadstring"], _module_0["tolua"] = loadstring, tolua
921local fieldA = tb.itemA.fieldA
922if fieldA == nil then
923 fieldA = 'default'
924end
925_module_0["fieldA"] = fieldA
926return _module_0
927local _module_0 = setmetatable({ }, { })
928_module_0.itemA = tb
929getmetatable(_module_0).__index = items
930_module_0["a-b-c"] = 123
931return _module_0
932local _module_0 = { }
933local d, e, f = 3, 2, 1
934_module_0[#_module_0 + 1] = d
935_module_0[#_module_0 + 1] = e
936_module_0[#_module_0 + 1] = f
937if this then
938 _module_0[#_module_0 + 1] = 123
939else
940 _module_0[#_module_0 + 1] = 456
941end
942local _with_0 = tmp
943local j = 2000
944_module_0[#_module_0 + 1] = _with_0
945return _module_0
946local _module_0 = nil
947_module_0 = function()
948 print("hello")
949 return 123
950end
951return _module_0
411do 952do
412 local insert, concat = table.insert, table.concat 953 local insert, concat = table.insert, table.concat
413 local C, Ct, Cmt 954 local C, Ct, Cmt
@@ -590,6 +1131,57 @@ do
590 B = 2 1131 B = 2
591 local Temp = "a local value" 1132 local Temp = "a local value"
592end 1133end
1134local hello = "world"
1135local a, b, c = 1, 2, 3
1136hello = 123
1137local x = 1
1138x = x + 1
1139x = x - 1
1140x = x * 10
1141x = x / 10
1142x = x % 10
1143local s = s .. "world"
1144local arg = arg or "default value"
1145local a = 0
1146local b = 0
1147local c = 0
1148local d = 0
1149local e = 0
1150local x = f()
1151local y = x
1152local z = x
1153do
1154 local a = 1
1155 local x, y, z
1156 print("forward declare all variables as locals")
1157 x = function()
1158 return 1 + y + z
1159 end
1160 y, z = 2, 3
1161 instance = Item:new()
1162end
1163do
1164 local X = 1
1165 local B
1166 print("only forward declare upper case variables")
1167 local a = 1
1168 B = 2
1169end
1170do
1171 a = 1
1172 print("declare all variables as globals")
1173 x = function()
1174 return 1 + y + z
1175 end
1176 y, z = 2, 3
1177end
1178do
1179 X = 1
1180 print("only declare upper case variables as globals")
1181 local a = 1
1182 B = 2
1183 local Temp = "a local value"
1184end
593local thing = { 1185local thing = {
594 1, 1186 1,
595 2 1187 2
@@ -707,6 +1299,155 @@ for _index_0 = 1, #tuples do
707 local left, right = _des_0[1], _des_0[2] 1299 local left, right = _des_0[1], _des_0[2]
708 print(left, right) 1300 print(left, right)
709end 1301end
1302local thing = {
1303 1,
1304 2
1305}
1306local a, b = thing[1], thing[2]
1307print(a, b)
1308local obj = {
1309 hello = "world",
1310 day = "tuesday",
1311 length = 20
1312}
1313local hello, the_day = obj.hello, obj.day
1314print(hello, the_day)
1315local day = obj.day
1316local obj2 = {
1317 numbers = {
1318 1,
1319 2,
1320 3,
1321 4
1322 },
1323 properties = {
1324 color = "green",
1325 height = 13.5
1326 }
1327}
1328local first, second = obj2.numbers[1], obj2.numbers[2]
1329print(first, second, color)
1330local first, second, color
1331local _obj_0 = obj2
1332first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color
1333local concat, insert
1334local _obj_0 = table
1335concat, insert = _obj_0.concat, _obj_0.insert
1336local mix, max, rand
1337local _obj_0 = math
1338mix, max, rand = _obj_0.mix, _obj_0.max, _obj_0.random
1339local name, job
1340local _obj_0 = person
1341name, job = _obj_0.name, _obj_0.job
1342if name == nil then
1343 name = "nameless"
1344end
1345if job == nil then
1346 job = "jobless"
1347end
1348local two, four
1349local _obj_0 = items
1350two, four = _obj_0[2], _obj_0[4]
1351local orders = {
1352 "first",
1353 "second",
1354 "third",
1355 "fourth",
1356 "last"
1357}
1358local first, bulk, last = orders[1], (function()
1359 local _accum_0 = { }
1360 local _len_0 = 1
1361 local _max_0 = #orders + -2 + 1
1362 for _index_0 = 2, _max_0 do
1363 local _item_0 = orders[_index_0]
1364 _accum_0[_len_0] = _item_0
1365 _len_0 = _len_0 + 1
1366 end
1367 return _accum_0
1368end)(), orders[#orders]
1369print(first)
1370print(bulk)
1371print(last)
1372local first, rest
1373do
1374 local _obj_0 = orders
1375 first, rest = _obj_0[1], (function()
1376 local _accum_0 = { }
1377 local _len_0 = 1
1378 local _max_0 = #_obj_0
1379 for _index_0 = 2, _max_0 do
1380 local _item_0 = _obj_0[_index_0]
1381 _accum_0[_len_0] = _item_0
1382 _len_0 = _len_0 + 1
1383 end
1384 return _accum_0
1385 end)()
1386end
1387local start, last
1388do
1389 local _obj_0 = orders
1390 start, last = (function()
1391 local _accum_0 = { }
1392 local _len_0 = 1
1393 local _max_0 = #_obj_0 + -2 + 1
1394 for _index_0 = 1, _max_0 do
1395 local _item_0 = _obj_0[_index_0]
1396 _accum_0[_len_0] = _item_0
1397 _len_0 = _len_0 + 1
1398 end
1399 return _accum_0
1400 end)(), _obj_0[#_obj_0]
1401end
1402local _obj_0 = orders
1403first, last = _obj_0[1], _obj_0[#_obj_0]
1404local tuples = {
1405 {
1406 "hello",
1407 "world"
1408 },
1409 {
1410 "egg",
1411 "head"
1412 }
1413}
1414for _index_0 = 1, #tuples do
1415 local _des_0 = tuples[_index_0]
1416 local left, right = _des_0[1], _des_0[2]
1417 print(left, right)
1418end
1419local user = database.find_user("moon")
1420if user then
1421 print(user.name)
1422end
1423local hello = os.getenv("hello")
1424if hello then
1425 print("You have hello", hello)
1426else
1427 local world = os.getenv("world")
1428 if world then
1429 print("you have world", world)
1430 else
1431 print("nothing :(")
1432 end
1433end
1434do
1435 local success, result = pcall(function()
1436 return "get result without problems"
1437 end)
1438 if success then
1439 print(result)
1440 end
1441end
1442print("OK")
1443repeat
1444 local byte = stream:read_one()
1445 if byte then
1446 print(byte)
1447 else
1448 break
1449 end
1450until false
710local user = database.find_user("moon") 1451local user = database.find_user("moon")
711if user then 1452if user then
712 print(user.name) 1453 print(user.name)
@@ -756,6 +1497,31 @@ end
756 local first = select(1, ...) 1497 local first = select(1, ...)
757 return print(ok, count, first) 1498 return print(ok, count, first)
758end)(fn(true)) 1499end)(fn(true))
1500local list = {
1501 1,
1502 2,
1503 3,
1504 4,
1505 5
1506}
1507local fn
1508fn = function(ok)
1509 return ok, table.unpack(list)
1510end
1511(function(_arg_0, ...)
1512 local ok = _arg_0
1513 local count = select('#', ...)
1514 local first = select(1, ...)
1515 return print(ok, count, first)
1516end)(fn(true))
1517local a = 1
1518local b = 2
1519print(a + b)
1520Rx.Observable.fromRange(1, 8):filter(function(x)
1521 return x % 2 == 0
1522end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
1523 return value .. '!'
1524end):subscribe(print)
759local a = 1 1525local a = 1
760local b = 2 1526local b = 2
761print(a + b) 1527print(a + b)
@@ -766,6 +1532,88 @@ end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
766end):subscribe(print) 1532end):subscribe(print)
767local str = strA .. strB .. strC 1533local str = strA .. strB .. strC
768func(3000, "192.168.1.1") 1534func(3000, "192.168.1.1")
1535local str = strA .. strB .. strC
1536func(3000, "192.168.1.1")
1537xpcall(function()
1538 return func(1, 2, 3)
1539end, function(err)
1540 return print(yue.traceback(err))
1541end)
1542local success, result = xpcall(function()
1543 return func(1, 2, 3)
1544end, function(err)
1545 return yue.traceback(err)
1546end)
1547xpcall(function()
1548 return func(1, 2, 3)
1549end, function(err)
1550 return print(yue.traceback(err))
1551end)
1552success, result = pcall(function()
1553 return func(1, 2, 3)
1554end)
1555pcall(function()
1556 print("trying")
1557 return func(1, 2, 3)
1558end)
1559success, result = xpcall(function()
1560 return func(1, 2, 3)
1561end, function(err)
1562 return print(yue.traceback(err))
1563end)
1564if success then
1565 print(result)
1566end
1567local a, b, c
1568do
1569 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
1570 return func()
1571 end)
1572 if _ok_0 then
1573 a, b, c = _ret_0, _ret_1, _ret_2
1574 end
1575end
1576do
1577 local _exp_0 = ((function()
1578 return (function(_arg_0, ...)
1579 local _ok_0 = _arg_0
1580 if _ok_0 then
1581 return ...
1582 end
1583 end)(pcall(function()
1584 return func()
1585 end))
1586 end)())
1587 if _exp_0 ~= nil then
1588 a = _exp_0
1589 else
1590 a = "default"
1591 end
1592end
1593f((function()
1594 return (function(_arg_0, ...)
1595 local _ok_0 = _arg_0
1596 if _ok_0 then
1597 return ...
1598 end
1599 end)(pcall(function()
1600 return func()
1601 end))
1602end)())
1603f((function()
1604 return (function(_arg_0, ...)
1605 local _ok_0 = _arg_0
1606 if _ok_0 then
1607 return ...
1608 end
1609 end)(xpcall(function()
1610 print(123)
1611 return func()
1612 end, function(e)
1613 print(e)
1614 return e
1615 end))
1616end)())
769xpcall(function() 1617xpcall(function()
770 return func(1, 2, 3) 1618 return func(1, 2, 3)
771end, function(err) 1619end, function(err)
@@ -856,6 +1704,28 @@ local a, b, c, d
856local _obj_0 = tb 1704local _obj_0 = tb
857a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2] 1705a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2]
858Constant = 123 1706Constant = 123
1707local a <const> = 123
1708local _ <close> = setmetatable({ }, {
1709 __close = function()
1710 return print("Out of scope.")
1711 end
1712})
1713local a, b, c, d
1714local _obj_0 = tb
1715a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2]
1716Constant = 123
1717local some_string = "Here is a string\n that has a line break in it."
1718print("I am " .. tostring(math.random() * 100) .. "% sure.")
1719local integer = 1000000
1720local hex = 0xEFBBBF
1721local binary = 19
1722local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
1723local fn
1724fn = function()
1725 local str = "foo:\n bar: baz"
1726 return str
1727end
1728local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
859local some_string = "Here is a string\n that has a line break in it." 1729local some_string = "Here is a string\n that has a line break in it."
860print("I am " .. tostring(math.random() * 100) .. "% sure.") 1730print("I am " .. tostring(math.random() * 100) .. "% sure.")
861local integer = 1000000 1731local integer = 1000000
@@ -1052,8 +1922,192 @@ process = function(...)
1052 return sum 1922 return sum
1053end 1923end
1054process(1, nil, 3, nil, 5) 1924process(1, nil, 3, nil, 5)
1055f(function() 1925local my_function
1056 return print("hello") 1926my_function = function() end
1927my_function()
1928local func_a
1929func_a = function()
1930 return print("hello world")
1931end
1932local func_b
1933func_b = function()
1934 local value = 100
1935 return print("The value:", value)
1936end
1937func_a()
1938func_b()
1939local sum
1940sum = function(x, y)
1941 return print("sum", x + y)
1942end
1943sum(10, 20)
1944print(sum(10, 20))
1945a(b(c("a", "b", "c")))
1946print("x:", sum(10, 20), "y:", sum(30, 40))
1947local sum
1948sum = function(x, y)
1949 return x + y
1950end
1951print("The sum is ", sum(10, 20))
1952local sum
1953sum = function(x, y)
1954 return x + y
1955end
1956local mystery
1957mystery = function(x, y)
1958 return x + y, x - y
1959end
1960local a, b = mystery(10, 20)
1961local func
1962func = function(self, num)
1963 return self.value + num
1964end
1965local my_function
1966my_function = function(name, height)
1967 if name == nil then
1968 name = "something"
1969 end
1970 if height == nil then
1971 height = 100
1972 end
1973 print("Hello I am", name)
1974 return print("My height is", height)
1975end
1976local some_args
1977some_args = function(x, y)
1978 if x == nil then
1979 x = 100
1980 end
1981 if y == nil then
1982 y = x + 1000
1983 end
1984 return print(x + y)
1985end
1986local a = x - 10
1987local b = x - 10
1988local c = x(-y)
1989local d = x - z
1990local x = func("hello") + 100
1991local y = func("hello" + 100)
1992my_func(5, 4, 3, 8, 9, 10)
1993cool_func(1, 2, 3, 4, 5, 6, 7, 8)
1994my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4)
1995local x = {
1996 1,
1997 2,
1998 3,
1999 4,
2000 a_func(4, 5, 5, 6),
2001 8,
2002 9,
2003 10
2004}
2005local y = {
2006 my_func(1, 2, 3, 4, 5),
2007 5,
2008 6,
2009 7
2010}
2011if func(1, 2, 3, "hello", "world") then
2012 print("hello")
2013 print("I am inside if")
2014end
2015if func(1, 2, 3, "hello", "world") then
2016 print("hello")
2017 print("I am inside if")
2018end
2019local f1
2020f1 = function(_arg_0)
2021 local a, b, c
2022 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
2023 return print(a, b, c)
2024end
2025f1({
2026 a = 1,
2027 b = "2",
2028 c = { }
2029})
2030local f2
2031f2 = function(_arg_0, c)
2032 local a1, b
2033 a1, b = _arg_0.a, _arg_0.b
2034 if a1 == nil then
2035 a1 = 123
2036 end
2037 if b == nil then
2038 b = 'abc'
2039 end
2040 if c == nil then
2041 c = { }
2042 end
2043end
2044print(a1, b, c)
2045local arg1 = {
2046 a = 0
2047}
2048f2(arg1, arg2)
2049local findFirstEven
2050findFirstEven = function(list)
2051 for _index_0 = 1, #list do
2052 local item = list[_index_0]
2053 if type(item) == "table" then
2054 for _index_1 = 1, #item do
2055 local sub = item[_index_1]
2056 if sub % 2 == 0 then
2057 return sub
2058 end
2059 end
2060 end
2061 end
2062 return nil
2063end
2064local findFirstEven
2065findFirstEven = function(list)
2066 for _index_0 = 1, #list do
2067 local item = list[_index_0]
2068 if type(item) == "table" then
2069 for _index_1 = 1, #item do
2070 local sub = item[_index_1]
2071 if sub % 2 == 0 then
2072 return sub
2073 end
2074 end
2075 end
2076 end
2077 return nil
2078end
2079local f
2080f = function(...)
2081 local t = {
2082 n = select("#", ...),
2083 ...
2084 }
2085 print("argument count:", t.n)
2086 print("table length:", #t)
2087 for i = 1, t.n do
2088 print(t[i])
2089 end
2090end
2091f(1, 2, 3)
2092f("a", "b", "c", "d")
2093f()
2094local process
2095process = function(...)
2096 local args = {
2097 n = select("#", ...),
2098 ...
2099 }
2100 local sum = 0
2101 for i = 1, args.n do
2102 if args[i] ~= nil and type(args[i]) == "number" then
2103 sum = sum + args[i]
2104 end
2105 end
2106 return sum
2107end
2108process(1, nil, 3, nil, 5)
2109f(function(x)
2110 return print("hello" .. x)
1057end) 2111end)
1058f(function(self) 2112f(function(self)
1059 return print(self.value) 2113 return print(self.value)
@@ -1075,6 +2129,97 @@ do
1075 end) 2129 end)
1076end 2130end
1077print(result, msg) 2131print(result, msg)
2132f(function(x)
2133 return print("hello" .. x)
2134end)
2135f(function(self)
2136 return print(self.value)
2137end)
2138map(function(x)
2139 return x * 2
2140end, {
2141 1,
2142 2,
2143 3
2144})
2145local result, msg
2146do
2147 result, msg = readAsync("filename.txt", function(data)
2148 print(data)
2149 return processAsync(data, function(info)
2150 return check(info)
2151 end)
2152 end)
2153end
2154print(result, msg)
2155local some_values = {
2156 1,
2157 2,
2158 3,
2159 4
2160}
2161local some_values = {
2162 name = "Bill",
2163 age = 200,
2164 ["favorite food"] = "rice"
2165}
2166local profile = {
2167 height = "4 feet",
2168 shoe_size = 13,
2169 favorite_foods = {
2170 "ice cream",
2171 "donuts"
2172 }
2173}
2174local values = {
2175 1,
2176 2,
2177 3,
2178 4,
2179 5,
2180 6,
2181 7,
2182 8,
2183 name = "superman",
2184 occupation = "crime fighting"
2185}
2186my_function({
2187 dance = "Tango",
2188 partner = "none"
2189})
2190local y = {
2191 type = "dog",
2192 legs = 4,
2193 tails = 1
2194}
2195local tbl = {
2196 ["do"] = "something",
2197 ["end"] = "hunger"
2198}
2199local hair = "golden"
2200local height = 200
2201local person = {
2202 hair = hair,
2203 height = height,
2204 shoe_size = 40
2205}
2206print_table({
2207 hair = hair,
2208 height = height
2209})
2210local t = {
2211 [1 + 2] = "hello",
2212 ["hello world"] = true
2213}
2214local some_values = {
2215 1,
2216 2,
2217 3,
2218 4
2219}
2220local list_with_one_element = {
2221 1
2222}
1078local some_values = { 2223local some_values = {
1079 1, 2224 1,
1080 2, 2225 2,
@@ -1341,15 +2486,250 @@ for _index_0 = _min_0, 1, -1 do
1341end 2486end
1342reverse_slice = _accum_0 2487reverse_slice = _accum_0
1343local sub_list 2488local sub_list
2489do
2490 local _accum_0 = { }
2491 local _len_0 = 1
2492 local _list_0 = items
2493 for _index_0 = 2, 4 do
2494 local _item_0 = _list_0[_index_0]
2495 _accum_0[_len_0] = _item_0
2496 _len_0 = _len_0 + 1
2497 end
2498 sub_list = _accum_0
2499end
2500local last_four_items
1344local _accum_0 = { } 2501local _accum_0 = { }
1345local _len_0 = 1 2502local _len_0 = 1
1346local _list_0 = items 2503local _list_0 = items
1347for _index_0 = 2, 4 do 2504local _min_0 = #_list_0 + -4 + 1
2505local _max_0 = #_list_0 + -1 + 1
2506for _index_0 = _min_0, _max_0 do
1348 local _item_0 = _list_0[_index_0] 2507 local _item_0 = _list_0[_index_0]
1349 _accum_0[_len_0] = _item_0 2508 _accum_0[_len_0] = _item_0
1350 _len_0 = _len_0 + 1 2509 _len_0 = _len_0 + 1
1351end 2510end
1352sub_list = _accum_0 2511last_four_items = _accum_0
2512local items = {
2513 1,
2514 2,
2515 3,
2516 4
2517}
2518local doubled
2519local _accum_0 = { }
2520local _len_0 = 1
2521for i, item in ipairs(items) do
2522 _accum_0[_len_0] = item * 2
2523 _len_0 = _len_0 + 1
2524end
2525doubled = _accum_0
2526local slice
2527local _accum_0 = { }
2528local _len_0 = 1
2529for i, item in ipairs(items) do
2530 if i > 1 and i < 3 then
2531 _accum_0[_len_0] = item
2532 _len_0 = _len_0 + 1
2533 end
2534end
2535slice = _accum_0
2536local doubled
2537local _accum_0 = { }
2538local _len_0 = 1
2539local _list_0 = items
2540for _index_0 = 1, #_list_0 do
2541 local item = _list_0[_index_0]
2542 _accum_0[_len_0] = item * 2
2543 _len_0 = _len_0 + 1
2544end
2545doubled = _accum_0
2546local data = {
2547 a = {
2548 1,
2549 2,
2550 3
2551 },
2552 b = {
2553 4,
2554 5,
2555 6
2556 }
2557}
2558local flat
2559local _accum_0 = { }
2560for k, v in pairs(data) do
2561 local _len_0 = #_accum_0 + 1
2562 for _index_0 = 1, #v do
2563 local _elm_0 = v[_index_0]
2564 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
2565 end
2566end
2567flat = _accum_0
2568local x_coords = {
2569 4,
2570 5,
2571 6,
2572 7
2573}
2574local y_coords = {
2575 9,
2576 2,
2577 3
2578}
2579local points
2580local _accum_0 = { }
2581local _len_0 = 1
2582for _index_0 = 1, #x_coords do
2583 local x = x_coords[_index_0]
2584 for _index_1 = 1, #y_coords do
2585 local y = y_coords[_index_1]
2586 _accum_0[_len_0] = {
2587 x,
2588 y
2589 }
2590 _len_0 = _len_0 + 1
2591 end
2592end
2593points = _accum_0
2594local evens
2595local _accum_0 = { }
2596local _len_0 = 1
2597for i = 1, 100 do
2598 if i % 2 == 0 then
2599 _accum_0[_len_0] = i
2600 _len_0 = _len_0 + 1
2601 end
2602end
2603evens = _accum_0
2604local thing = {
2605 color = "red",
2606 name = "fast",
2607 width = 123
2608}
2609local thing_copy
2610local _tbl_0 = { }
2611for k, v in pairs(thing) do
2612 _tbl_0[k] = v
2613end
2614thing_copy = _tbl_0
2615local no_color
2616local _tbl_0 = { }
2617for k, v in pairs(thing) do
2618 if k ~= "color" then
2619 _tbl_0[k] = v
2620 end
2621end
2622no_color = _tbl_0
2623local numbers = {
2624 1,
2625 2,
2626 3,
2627 4
2628}
2629local sqrts
2630local _tbl_0 = { }
2631for _index_0 = 1, #numbers do
2632 local i = numbers[_index_0]
2633 _tbl_0[i] = math.sqrt(i)
2634end
2635sqrts = _tbl_0
2636local tuples = {
2637 {
2638 "hello",
2639 "world"
2640 },
2641 {
2642 "foo",
2643 "bar"
2644 }
2645}
2646local tbl
2647local _tbl_0 = { }
2648for _index_0 = 1, #tuples do
2649 local tuple = tuples[_index_0]
2650 local _key_0, _val_0 = unpack(tuple)
2651 _tbl_0[_key_0] = _val_0
2652end
2653tbl = _tbl_0
2654local slice
2655local _accum_0 = { }
2656local _len_0 = 1
2657local _list_0 = items
2658for _index_0 = 1, 5 do
2659 local item = _list_0[_index_0]
2660 _accum_0[_len_0] = item
2661 _len_0 = _len_0 + 1
2662end
2663slice = _accum_0
2664local slice
2665local _accum_0 = { }
2666local _len_0 = 1
2667local _list_0 = items
2668local _max_0 = #_list_0
2669for _index_0 = 2, _max_0 do
2670 local item = _list_0[_index_0]
2671 _accum_0[_len_0] = item
2672 _len_0 = _len_0 + 1
2673end
2674slice = _accum_0
2675local slice
2676local _accum_0 = { }
2677local _len_0 = 1
2678local _list_0 = items
2679local _max_0 = #_list_0
2680for _index_0 = 1, _max_0, 2 do
2681 local item = _list_0[_index_0]
2682 _accum_0[_len_0] = item
2683 _len_0 = _len_0 + 1
2684end
2685slice = _accum_0
2686local slice
2687local _accum_0 = { }
2688local _len_0 = 1
2689local _list_0 = items
2690local _min_0 = #_list_0 + -4 + 1
2691local _max_0 = #_list_0 + -1 + 1
2692for _index_0 = _min_0, _max_0 do
2693 local item = _list_0[_index_0]
2694 _accum_0[_len_0] = item
2695 _len_0 = _len_0 + 1
2696end
2697slice = _accum_0
2698local reverse_slice
2699local _accum_0 = { }
2700local _len_0 = 1
2701local _list_0 = items
2702local _min_0 = #_list_0 + -1 + 1
2703for _index_0 = _min_0, 1, -1 do
2704 local item = _list_0[_index_0]
2705 _accum_0[_len_0] = item
2706 _len_0 = _len_0 + 1
2707end
2708reverse_slice = _accum_0
2709local sub_list
2710do
2711 local _accum_0 = { }
2712 local _len_0 = 1
2713 local _list_0 = items
2714 for _index_0 = 2, 4 do
2715 local _item_0 = _list_0[_index_0]
2716 _accum_0[_len_0] = _item_0
2717 _len_0 = _len_0 + 1
2718 end
2719 sub_list = _accum_0
2720end
2721local last_four_items
2722local _accum_0 = { }
2723local _len_0 = 1
2724local _list_0 = items
2725local _min_0 = #_list_0 + -4 + 1
2726local _max_0 = #_list_0 + -1 + 1
2727for _index_0 = _min_0, _max_0 do
2728 local _item_0 = _list_0[_index_0]
2729 _accum_0[_len_0] = _item_0
2730 _len_0 = _len_0 + 1
2731end
2732last_four_items = _accum_0
1353for i = 10, 20 do 2733for i = 10, 20 do
1354 print(i) 2734 print(i)
1355end 2735end
@@ -1414,6 +2794,86 @@ func_b = function()
1414end 2794end
1415print(func_a()) 2795print(func_a())
1416print(func_b()) 2796print(func_b())
2797for i = 10, 20 do
2798 print(i)
2799end
2800for k = 1, 15, 2 do
2801 print(k)
2802end
2803for key, value in pairs(object) do
2804 print(key, value)
2805end
2806local _list_0 = items
2807for _index_0 = 2, 4 do
2808 local item = _list_0[_index_0]
2809 print(item)
2810end
2811local _list_0 = items
2812for _index_0 = 1, #_list_0 do
2813 local item = _list_0[_index_0]
2814 print(item)
2815end
2816for j = 1, 10, 3 do
2817 print(j)
2818end
2819local doubled_evens
2820local _accum_0 = { }
2821local _len_0 = 1
2822for i = 1, 20 do
2823 if i % 2 == 0 then
2824 _accum_0[_len_0] = i * 2
2825 _len_0 = _len_0 + 1
2826 else
2827 _accum_0[_len_0] = i
2828 _len_0 = _len_0 + 1
2829 end
2830end
2831doubled_evens = _accum_0
2832local first_large
2833local _accum_0
2834local _list_0 = numbers
2835for _index_0 = 1, #_list_0 do
2836 local n = _list_0[_index_0]
2837 if n > 10 then
2838 _accum_0 = n
2839 break
2840 end
2841end
2842first_large = _accum_0
2843local func_a
2844func_a = function()
2845 for i = 1, 10 do
2846 print(i)
2847 end
2848end
2849local func_b
2850func_b = function()
2851 local _accum_0 = { }
2852 local _len_0 = 1
2853 for i = 1, 10 do
2854 _accum_0[_len_0] = i
2855 _len_0 = _len_0 + 1
2856 end
2857 return _accum_0
2858end
2859print(func_a())
2860print(func_b())
2861local i = 10
2862while i > 0 do
2863 print(i)
2864 i = i - 1
2865end
2866while running == true do
2867 my_function()
2868end
2869local i = 10
2870while not (i == 0) do
2871 print(i)
2872 i = i - 1
2873end
2874while not (running == false) do
2875 my_function()
2876end
1417local i = 10 2877local i = 10
1418repeat 2878repeat
1419 print(i) 2879 print(i)
@@ -1435,6 +2895,41 @@ end
1435while not (running == false) do 2895while not (running == false) do
1436 my_function() 2896 my_function()
1437end 2897end
2898local i = 10
2899repeat
2900 print(i)
2901 i = i - 1
2902until i == 0
2903local i = 0
2904while i < 10 do
2905 i = i + 1
2906 if i % 2 == 0 then
2907 goto _continue_0
2908 end
2909 print(i)
2910 ::_continue_0::
2911end
2912local my_numbers = {
2913 1,
2914 2,
2915 3,
2916 4,
2917 5,
2918 6
2919}
2920local odds
2921local _accum_0 = { }
2922local _len_0 = 1
2923for _index_0 = 1, #my_numbers do
2924 local x = my_numbers[_index_0]
2925 if x % 2 == 1 then
2926 goto _continue_0
2927 end
2928 _accum_0[_len_0] = x
2929 _len_0 = _len_0 + 1
2930 ::_continue_0::
2931end
2932odds = _accum_0
1438local i = 0 2933local i = 0
1439while i < 10 do 2934while i < 10 do
1440 i = i + 1 2935 i = i + 1
@@ -1524,6 +3019,79 @@ end
1524if not (math.random() > 0.1) then 3019if not (math.random() > 0.1) then
1525 print("You're lucky!") 3020 print("You're lucky!")
1526end 3021end
3022local have_coins = false
3023if have_coins then
3024 print("Got coins")
3025else
3026 print("No coins")
3027end
3028local have_coins = false
3029if have_coins then
3030 print("Got coins")
3031else
3032 print("No coins")
3033end
3034local have_coins = false
3035print((function()
3036 if have_coins then
3037 return "Got coins"
3038 else
3039 return "No coins"
3040 end
3041end)())
3042local is_tall
3043is_tall = function(name)
3044 if name == "Rob" then
3045 return true
3046 else
3047 return false
3048 end
3049end
3050local message
3051if is_tall("Rob") then
3052 message = "I am very tall"
3053else
3054 message = "I am not so tall"
3055end
3056print(message)
3057if not (os.date("%A") == "Monday") then
3058 print("it is not Monday!")
3059end
3060if not (math.random() > 0.1) then
3061 print("You're lucky!")
3062end
3063local a = 5
3064if (1 == a or 3 == a or 5 == a or 7 == a) then
3065 print("checking equality with discrete values")
3066end
3067if (function()
3068 local _check_0 = list
3069 for _index_0 = 1, #_check_0 do
3070 if _check_0[_index_0] == a then
3071 return true
3072 end
3073 end
3074 return false
3075end)() then
3076 print("checking if `a` is in a list")
3077end
3078if not (math.random() > 0.1) then
3079 print("You're lucky!")
3080end
3081if name == "Rob" then
3082 print("hello world")
3083end
3084local _list_0 = items
3085for _index_0 = 1, #_list_0 do
3086 local item = _list_0[_index_0]
3087 print("item: ", item)
3088end
3089while game:isRunning() do
3090 game:update()
3091end
3092while not reader:eof() do
3093 reader:parse_line()
3094end
1527if name == "Rob" then 3095if name == "Rob" then
1528 print("hello world") 3096 print("hello world")
1529end 3097end
@@ -1830,6 +3398,298 @@ if _tab_0 then
1830 print("Action:", action) 3398 print("Action:", action)
1831 end 3399 end
1832end 3400end
3401local name = "Dan"
3402if "Robert" == name then
3403 print("You are Robert")
3404elseif "Dan" == name or "Daniel" == name then
3405 print("Your name, it's Dan")
3406else
3407 print("I don't know about you with name " .. tostring(name))
3408end
3409local b = 1
3410local next_number
3411if 1 == b then
3412 next_number = 2
3413elseif 2 == b then
3414 next_number = 3
3415else
3416 next_number = error("can't count that high!")
3417end
3418local msg
3419local _exp_0 = math.random(1, 5)
3420if 1 == _exp_0 then
3421 msg = "you are lucky"
3422elseif 2 == _exp_0 then
3423 msg = "you are almost lucky"
3424else
3425 msg = "not so lucky"
3426end
3427do
3428 local _exp_0 = math.random(1, 5)
3429 if 1 == _exp_0 then
3430 print("you are lucky")
3431 else
3432 print("not so lucky")
3433 end
3434end
3435local _exp_0 = math.random(1, 5)
3436if 1 == _exp_0 then
3437 print("you are lucky")
3438else
3439 print("not so lucky")
3440end
3441local items = {
3442 {
3443 x = 100,
3444 y = 200
3445 },
3446 {
3447 width = 300,
3448 height = 400
3449 }
3450}
3451for _index_0 = 1, #items do
3452 local item = items[_index_0]
3453 local _type_0 = type(item)
3454 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3455 local _match_0 = false
3456 if _tab_0 then
3457 local x = item.x
3458 local y = item.y
3459 if x ~= nil and y ~= nil then
3460 _match_0 = true
3461 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
3462 end
3463 end
3464 if not _match_0 then
3465 if _tab_0 then
3466 local width = item.width
3467 local height = item.height
3468 if width ~= nil and height ~= nil then
3469 print("size " .. tostring(width) .. ", " .. tostring(height))
3470 end
3471 end
3472 end
3473end
3474local item = { }
3475local x, y = item.pos.x, item.pos.y
3476if x == nil then
3477 x = 50
3478end
3479if y == nil then
3480 y = 200
3481end
3482local _type_0 = type(item)
3483local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3484if _tab_0 then
3485 do
3486 local _obj_0 = item.pos
3487 local _type_1 = type(_obj_0)
3488 if "table" == _type_1 or "userdata" == _type_1 then
3489 x = _obj_0.x
3490 end
3491 end
3492 do
3493 local _obj_0 = item.pos
3494 local _type_1 = type(_obj_0)
3495 if "table" == _type_1 or "userdata" == _type_1 then
3496 y = _obj_0.y
3497 end
3498 end
3499 if x == nil then
3500 x = 50
3501 end
3502 if y == nil then
3503 y = 200
3504 end
3505 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
3506end
3507local _exp_0 = tb
3508local _type_0 = type(_exp_0)
3509local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3510local _match_0 = false
3511if _tab_0 then
3512 if 1 == _exp_0[1] and 2 == _exp_0[2] and 3 == _exp_0[3] then
3513 _match_0 = true
3514 print("1, 2, 3")
3515 end
3516end
3517if not _match_0 then
3518 local _match_1 = false
3519 if _tab_0 then
3520 local b = _exp_0[2]
3521 if 1 == _exp_0[1] and b ~= nil and 3 == _exp_0[3] then
3522 _match_1 = true
3523 print("1, " .. tostring(b) .. ", 3")
3524 end
3525 end
3526 if not _match_1 then
3527 if _tab_0 then
3528 local b = _exp_0[3]
3529 if b == nil then
3530 b = 3
3531 end
3532 if 1 == _exp_0[1] and 2 == _exp_0[2] then
3533 print("1, 2, " .. tostring(b))
3534 end
3535 end
3536 end
3537end
3538local _exp_0 = tb
3539local _type_0 = type(_exp_0)
3540local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3541local _match_0 = false
3542if _tab_0 then
3543 local result = _exp_0.result
3544 if true == _exp_0.success and result ~= nil then
3545 _match_0 = true
3546 print("success", result)
3547 end
3548end
3549if not _match_0 then
3550 local _match_1 = false
3551 if _tab_0 then
3552 if false == _exp_0.success then
3553 _match_1 = true
3554 print("failed", result)
3555 end
3556 end
3557 if not _match_1 then
3558 print("invalid")
3559 end
3560end
3561local _exp_0 = tb
3562local _type_0 = type(_exp_0)
3563local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3564local _match_0 = false
3565if _tab_0 then
3566 local content
3567 do
3568 local _obj_0 = _exp_0.data
3569 local _type_1 = type(_obj_0)
3570 if "table" == _type_1 or "userdata" == _type_1 then
3571 content = _obj_0.content
3572 end
3573 end
3574 local _val_0
3575 do
3576 local _obj_0 = _exp_0.data
3577 if _obj_0 ~= nil then
3578 _val_0 = _obj_0.type
3579 end
3580 end
3581 if "success" == _val_0 and content ~= nil then
3582 _match_0 = true
3583 print("success", content)
3584 end
3585end
3586if not _match_0 then
3587 local _match_1 = false
3588 if _tab_0 then
3589 local content
3590 do
3591 local _obj_0 = _exp_0.data
3592 local _type_1 = type(_obj_0)
3593 if "table" == _type_1 or "userdata" == _type_1 then
3594 content = _obj_0.content
3595 end
3596 end
3597 local _val_0
3598 do
3599 local _obj_0 = _exp_0.data
3600 if _obj_0 ~= nil then
3601 _val_0 = _obj_0.type
3602 end
3603 end
3604 if "error" == _val_0 and content ~= nil then
3605 _match_1 = true
3606 print("failed", content)
3607 end
3608 end
3609 if not _match_1 then
3610 print("invalid")
3611 end
3612end
3613local _exp_0 = tb
3614local _type_0 = type(_exp_0)
3615local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3616if _tab_0 then
3617 local fourth = _exp_0[4]
3618 local _val_0
3619 do
3620 local _obj_0 = _exp_0[1]
3621 if _obj_0 ~= nil then
3622 _val_0 = _obj_0.a
3623 end
3624 end
3625 local _val_1
3626 do
3627 local _obj_0 = _exp_0[1]
3628 if _obj_0 ~= nil then
3629 _val_1 = _obj_0.b
3630 end
3631 end
3632 local _val_2
3633 do
3634 local _obj_0 = _exp_0[2]
3635 if _obj_0 ~= nil then
3636 _val_2 = _obj_0.a
3637 end
3638 end
3639 local _val_3
3640 do
3641 local _obj_0 = _exp_0[2]
3642 if _obj_0 ~= nil then
3643 _val_3 = _obj_0.b
3644 end
3645 end
3646 local _val_4
3647 do
3648 local _obj_0 = _exp_0[3]
3649 if _obj_0 ~= nil then
3650 _val_4 = _obj_0.a
3651 end
3652 end
3653 local _val_5
3654 do
3655 local _obj_0 = _exp_0[3]
3656 if _obj_0 ~= nil then
3657 _val_5 = _obj_0.b
3658 end
3659 end
3660 if 1 == _val_0 and 2 == _val_1 and 3 == _val_2 and 4 == _val_3 and 5 == _val_4 and 6 == _val_5 and fourth ~= nil then
3661 print("matched", fourth)
3662 end
3663end
3664local segments = {
3665 "admin",
3666 "users",
3667 "logs",
3668 "view"
3669}
3670local _type_0 = type(segments)
3671local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3672if _tab_0 then
3673 local groups
3674 do
3675 local _accum_0 = { }
3676 local _len_0 = 1
3677 local _max_0 = #segments + -3 + 1
3678 for _index_0 = 1, _max_0 do
3679 local _item_0 = segments[_index_0]
3680 _accum_0[_len_0] = _item_0
3681 _len_0 = _len_0 + 1
3682 end
3683 groups = _accum_0
3684 end
3685 local resource = segments[#segments - 1]
3686 local action = segments[#segments]
3687 if resource ~= nil and action ~= nil then
3688 print("Group:", groups)
3689 print("Resource:", resource)
3690 print("Action:", action)
3691 end
3692end
1833local Inventory 3693local Inventory
1834local _class_0 3694local _class_0
1835local _base_0 = { 3695local _base_0 = {
@@ -2450,1946 +4310,6 @@ end
2450local y = Y() 4310local y = Y()
2451y:func() 4311y:func()
2452assert(y.__class.__parent ~= X) 4312assert(y.__class.__parent ~= X)
2453local _with_0 = Person()
2454_with_0.name = "Oswald"
2455_with_0:add_relative(my_dad)
2456_with_0:save()
2457print(_with_0.name)
2458local file
2459local _with_0 = File("favorite_foods.txt")
2460_with_0:set_encoding("utf8")
2461file = _with_0
2462local create_person
2463create_person = function(name, relatives)
2464 local _with_0 = Person()
2465 _with_0.name = name
2466 for _index_0 = 1, #relatives do
2467 local relative = relatives[_index_0]
2468 _with_0:add_relative(relative)
2469 end
2470 return _with_0
2471end
2472local me = create_person("Leaf", {
2473 dad,
2474 mother,
2475 sister
2476})
2477local str = "Hello"
2478print("original:", str)
2479print("upper:", str:upper())
2480local _with_0 = tb
2481_with_0[1] = 1
2482print(_with_0[2])
2483do
2484 local _with_1 = _with_0[abc]
2485 _with_1[3] = _with_1[2]:func()
2486 _with_1["key-name"] = value
2487end
2488_with_0[#_with_0 + 1] = "abc"
2489local _with_0 = obj
2490if _with_0 ~= nil then
2491 print(obj.name)
2492end
2493do
2494 local var = "hello"
2495 print(var)
2496end
2497print(var)
2498local counter
2499do
2500 local i = 0
2501 counter = function()
2502 i = i + 1
2503 return i
2504 end
2505end
2506print(counter())
2507print(counter())
2508local tbl = {
2509 key = (function()
2510 print("assigning key!")
2511 return 1234
2512 end)()
2513}
2514local my_object = {
2515 value = 1000,
2516 write = function(self)
2517 return print("the value:", self.value)
2518 end
2519}
2520local run_callback
2521run_callback = function(func)
2522 print("running callback...")
2523 return func()
2524end
2525run_callback(my_object.write)
2526run_callback((function()
2527 local _base_0 = my_object
2528 local _fn_0 = _base_0.write
2529 return _fn_0 and function(...)
2530 return _fn_0(_base_0, ...)
2531 end
2532end)())
2533local i = 100
2534local my_func
2535my_func = function()
2536 i = 10
2537 while i > 0 do
2538 print(i)
2539 i = i - 1
2540 end
2541end
2542my_func()
2543print(i)
2544local i = 100
2545local my_func
2546my_func = function()
2547 local i = "hello"
2548end
2549my_func()
2550print(i)
2551local tmp = 1213
2552local i, k = 100, 50
2553local my_func
2554my_func = function(add)
2555 local tmp = tmp + add
2556 i = i + tmp
2557 k = k + tmp
2558end
2559my_func(22)
2560print(i, k)
2561local _module_0 = { }
2562local p, to_lua
2563do
2564 local _obj_0 = require("yue")
2565 p, to_lua = _obj_0.p, _obj_0.to_lua
2566end
2567local inventory = {
2568 equipment = {
2569 "sword",
2570 "shield"
2571 },
2572 items = {
2573 {
2574 name = "potion",
2575 count = 10
2576 },
2577 {
2578 name = "bread",
2579 count = 3
2580 }
2581 }
2582}
2583local map
2584map = function(arr, action)
2585 local _accum_0 = { }
2586 local _len_0 = 1
2587 for _index_0 = 1, #arr do
2588 local item = arr[_index_0]
2589 _accum_0[_len_0] = action(item)
2590 _len_0 = _len_0 + 1
2591 end
2592 return _accum_0
2593end
2594local filter
2595filter = function(arr, cond)
2596 local _accum_0 = { }
2597 local _len_0 = 1
2598 for _index_0 = 1, #arr do
2599 local item = arr[_index_0]
2600 if cond(item) then
2601 _accum_0[_len_0] = item
2602 _len_0 = _len_0 + 1
2603 end
2604 end
2605 return _accum_0
2606end
2607local reduce
2608reduce = function(arr, init, action)
2609 for _index_0 = 1, #arr do
2610 local item = arr[_index_0]
2611 init = action(init, item)
2612 end
2613 return init
2614end
2615print(reduce(filter(map({
2616 1,
2617 2,
2618 3
2619}, function(x)
2620 return x * 2
2621end), function(x)
2622 return x > 4
2623end), 0, function(a, b)
2624 return a + b
2625end))
2626local apple = setmetatable({
2627 size = 15,
2628}, {
2629 __index = {
2630 color = 0x00ffff
2631 }
2632})
2633if (getmetatable(apple) ~= nil) then
2634 p(apple.size, apple.color, getmetatable(apple).__index)
2635end
2636local _u1f31b = "月之脚本"
2637_module_0["🌛"] = _u1f31b
2638return _module_0
2639local area = 6.2831853071796 * 5
2640print('hello world')
2641do
2642 assert(item ~= nil)
2643end
2644local value = item
2645if (f1() and f2() and f3()) then
2646 print("OK")
2647end
2648do
2649 local funcA
2650 funcA = function() end
2651end
2652local funcA
2653funcA = function()
2654 return "fail to assign to the Yue macro defined variable"
2655end
2656do
2657local function funcB() end
2658end
2659local funcB
2660funcB = function()
2661 return "fail to assign to the Lua macro defined variable"
2662end
2663do
2664-- raw Lua codes insertion
2665if cond then
2666 print("output")
2667end
2668end
2669print("yuescript")
2670print(2)
2671print("Valid enum type:", "Static")
2672do
2673 print(123, "hello")
2674end
2675do
2676 print(123, "hello")
2677end
2678if tb ~= nil then
2679 tb:func()
2680end
2681if tb ~= nil then
2682 tb:func()
2683end
2684print(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5)
2685local a = 5
2686print(1 <= a and a <= 10)
2687local v
2688v = function(x)
2689 print(x)
2690 return x
2691end
2692print((function()
2693 local _cond_0 = v(2)
2694 if not (v(1) < _cond_0) then
2695 return false
2696 else
2697 return _cond_0 <= v(3)
2698 end
2699end)())
2700print((function()
2701 local _cond_0 = v(2)
2702 if not (v(1) > _cond_0) then
2703 return false
2704 else
2705 return _cond_0 <= v(3)
2706 end
2707end)())
2708local tab = { }
2709tab[#tab + 1] = "Value"
2710local tbA = {
2711 1,
2712 2,
2713 3
2714}
2715local tbB = {
2716 4,
2717 5,
2718 6
2719}
2720local _len_0 = #tbA + 1
2721for _index_0 = 1, #tbB do
2722 local _elm_0 = tbB[_index_0]
2723 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
2724end
2725local parts = {
2726 "shoulders",
2727 "knees"
2728}
2729local lyrics
2730do
2731 local _tab_0 = {
2732 "head"
2733 }
2734 local _idx_0 = 1
2735 for _key_0, _value_0 in pairs(parts) do
2736 if _idx_0 == _key_0 then
2737 _tab_0[#_tab_0 + 1] = _value_0
2738 _idx_0 = _idx_0 + 1
2739 else
2740 _tab_0[_key_0] = _value_0
2741 end
2742 end
2743 _tab_0[#_tab_0 + 1] = "and"
2744 _tab_0[#_tab_0 + 1] = "toes"
2745 lyrics = _tab_0
2746end
2747local copy
2748do
2749 local _tab_0 = { }
2750 local _idx_0 = 1
2751 for _key_0, _value_0 in pairs(other) do
2752 if _idx_0 == _key_0 then
2753 _tab_0[#_tab_0 + 1] = _value_0
2754 _idx_0 = _idx_0 + 1
2755 else
2756 _tab_0[_key_0] = _value_0
2757 end
2758 end
2759 copy = _tab_0
2760end
2761local a = {
2762 1,
2763 2,
2764 3,
2765 x = 1
2766}
2767local b = {
2768 4,
2769 5,
2770 y = 1
2771}
2772local merge
2773local _tab_0 = { }
2774local _idx_0 = 1
2775for _key_0, _value_0 in pairs(a) do
2776 if _idx_0 == _key_0 then
2777 _tab_0[#_tab_0 + 1] = _value_0
2778 _idx_0 = _idx_0 + 1
2779 else
2780 _tab_0[_key_0] = _value_0
2781 end
2782end
2783local _idx_1 = 1
2784for _key_0, _value_0 in pairs(b) do
2785 if _idx_1 == _key_0 then
2786 _tab_0[#_tab_0 + 1] = _value_0
2787 _idx_1 = _idx_1 + 1
2788 else
2789 _tab_0[_key_0] = _value_0
2790 end
2791end
2792merge = _tab_0
2793local last
2794do
2795 local _item_0 = data.items
2796 last = _item_0[#_item_0]
2797end
2798local second_last
2799do
2800 local _item_0 = data.items
2801 second_last = _item_0[#_item_0 - 1]
2802end
2803local _obj_0 = data.items
2804_obj_0[#_obj_0] = 1
2805local mt = { }
2806local add
2807add = function(self, right)
2808 return setmetatable({
2809 value = self.value + right.value
2810 }, mt)
2811end
2812mt.__add = add
2813local a = setmetatable({
2814 value = 1
2815}, mt)
2816local b = setmetatable({
2817 value = 2
2818}, {
2819 __add = add
2820})
2821local c = setmetatable({
2822 value = 3
2823}, {
2824 __add = mt.__add
2825})
2826local d = a + b + c
2827print(d.value)
2828local _ <close> = setmetatable({ }, {
2829 __close = function()
2830 return print("out of scope")
2831 end
2832})
2833local tb = setmetatable({ }, {
2834 ["value"] = 123
2835})
2836getmetatable(tb).__index = getmetatable(tb)
2837print(tb.value)
2838setmetatable(tb, {
2839 __index = {
2840 item = "hello"
2841 }
2842})
2843print(tb.item)
2844local item, new, close, getter
2845do
2846 local _obj_0 = tb
2847 item, new = _obj_0[1], _obj_0.new
2848 do
2849 local _obj_1 = getmetatable(_obj_0)
2850 close, getter = _obj_1.__close, _obj_1.__index
2851 end
2852end
2853print(item, new, close, getter)
2854do
2855 local _obj_0 = func
2856 if _obj_0 ~= nil then
2857 _obj_0()
2858 end
2859end
2860print((function()
2861 local _obj_0 = abc
2862 if _obj_0 ~= nil then
2863 local _obj_1 = _obj_0["hello world"]
2864 if _obj_1 ~= nil then
2865 return _obj_1.xyz
2866 end
2867 return nil
2868 end
2869 return nil
2870end)())
2871local x
2872do
2873 local _obj_0 = tab
2874 if _obj_0 ~= nil then
2875 x = _obj_0.value
2876 end
2877end
2878local len = (function()
2879 local _obj_0 = utf8
2880 if _obj_0 ~= nil then
2881 return _obj_0.len
2882 end
2883 return nil
2884end)() or (function()
2885 local _obj_0 = string
2886 if _obj_0 ~= nil then
2887 return _obj_0.len
2888 end
2889 return nil
2890end)() or function(o)
2891 return #o
2892end
2893if print and (x ~= nil) then
2894 print(x)
2895end
2896local _with_0 = io.open("test.txt", "w")
2897if _with_0 ~= nil then
2898 _with_0:write("hello")
2899 _with_0:close()
2900end
2901print("hello")
2902print(1, 2)
2903print(1, 2, 3)
2904print(render(emit(parse(extract(readFile("example.txt"), language, { }), language))))
2905local a, b, c, d
2906if b ~= nil then
2907 a = b
2908else
2909 if c ~= nil then
2910 a = c
2911 else
2912 a = d
2913 end
2914end
2915func((function()
2916 if a ~= nil then
2917 return a
2918 else
2919 return { }
2920 end
2921end)())
2922if a == nil then
2923 a = false
2924end
2925local list = {
2926 1,
2927 2,
2928 3
2929}
2930func({
2931 1,
2932 2,
2933 3
2934})
2935local f
2936f = function()
2937 return {
2938 1,
2939 2,
2940 3
2941 }
2942end
2943local tb = {
2944 name = "abc",
2945 values = {
2946 "a",
2947 "b",
2948 "c"
2949 },
2950 objects = {
2951 {
2952 name = "a",
2953 value = 1,
2954 func = function(self)
2955 return self.value + 1
2956 end,
2957 tb = {
2958 fieldA = 1
2959 }
2960 },
2961 {
2962 name = "b",
2963 value = 2,
2964 func = function(self)
2965 return self.value + 2
2966 end,
2967 tb = { }
2968 }
2969 }
2970}
2971do
2972 local insert, concat = table.insert, table.concat
2973 local C, Ct, Cmt
2974 do
2975 local _obj_0 = require("lpeg")
2976 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
2977 end
2978 local x, y, z
2979 do
2980 local _obj_0 = require('mymodule')
2981 x, y, z = _obj_0.x, _obj_0.y, _obj_0.z
2982 end
2983 local a, b, c
2984 local _obj_0 = require('module')
2985 a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
2986end
2987do
2988 local module = require('module')
2989 local module_x = require('module_x')
2990 local d_a_s_h_e_s = require("d-a-s-h-e-s")
2991 local part = require("module.part")
2992end
2993do
2994 local PlayerModule = require("player")
2995 local C, Ct, Cmt
2996 do
2997 local _obj_0 = require("lpeg")
2998 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
2999 end
3000 local one, two, ch
3001 local _obj_0 = require("export")
3002 one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1]
3003end
3004do
3005 local tostring <const> = tostring
3006 local concat <const> = table.concat
3007 print(concat({
3008 "a",
3009 tostring(1)
3010 }))
3011end
3012do
3013 local print <const> = print
3014 local math <const> = math
3015 print("hello")
3016 math.random(3)
3017end
3018do
3019 local print <const> = print
3020 print(FLAG)
3021 FLAG = 123
3022end
3023local _module_0 = { }
3024local a, b, c = 1, 2, 3
3025_module_0["a"], _module_0["b"], _module_0["c"] = a, b, c
3026local cool = "cat"
3027_module_0["cool"] = cool
3028local What
3029if this then
3030 What = "abc"
3031else
3032 What = "def"
3033end
3034_module_0["What"] = What
3035local y
3036y = function()
3037 local hallo = 3434
3038end
3039_module_0["y"] = y
3040local Something
3041local _class_0
3042local _base_0 = {
3043 umm = "cool"
3044}
3045if _base_0.__index == nil then
3046 _base_0.__index = _base_0
3047end
3048_class_0 = setmetatable({
3049 __init = function() end,
3050 __base = _base_0,
3051 __name = "Something"
3052}, {
3053 __index = _base_0,
3054 __call = function(cls, ...)
3055 local _self_0 = setmetatable({ }, _base_0)
3056 cls.__init(_self_0, ...)
3057 return _self_0
3058 end
3059})
3060_base_0.__class = _class_0
3061Something = _class_0
3062_module_0["Something"] = Something
3063return _module_0
3064local _module_0 = { }
3065local loadstring, tolua
3066do
3067 local _obj_0 = yue
3068 loadstring, tolua = _obj_0.loadstring, _obj_0.to_lua
3069end
3070_module_0["loadstring"], _module_0["tolua"] = loadstring, tolua
3071local fieldA = tb.itemA.fieldA
3072if fieldA == nil then
3073 fieldA = 'default'
3074end
3075_module_0["fieldA"] = fieldA
3076return _module_0
3077local _module_0 = setmetatable({ }, { })
3078_module_0.itemA = tb
3079getmetatable(_module_0).__index = items
3080_module_0["a-b-c"] = 123
3081return _module_0
3082local _module_0 = { }
3083local d, e, f = 3, 2, 1
3084_module_0[#_module_0 + 1] = d
3085_module_0[#_module_0 + 1] = e
3086_module_0[#_module_0 + 1] = f
3087if this then
3088 _module_0[#_module_0 + 1] = 123
3089else
3090 _module_0[#_module_0 + 1] = 456
3091end
3092local _with_0 = tmp
3093local j = 2000
3094_module_0[#_module_0 + 1] = _with_0
3095return _module_0
3096local _module_0 = nil
3097_module_0 = function()
3098 print("hello")
3099 return 123
3100end
3101return _module_0
3102local hello = "world"
3103local a, b, c = 1, 2, 3
3104hello = 123
3105local x = 1
3106x = x + 1
3107x = x - 1
3108x = x * 10
3109x = x / 10
3110x = x % 10
3111local s = s .. "world"
3112local arg = arg or "default value"
3113local a = 0
3114local b = 0
3115local c = 0
3116local d = 0
3117local e = 0
3118local x = f()
3119local y = x
3120local z = x
3121do
3122 local a = 1
3123 local x, y, z
3124 print("forward declare all variables as locals")
3125 x = function()
3126 return 1 + y + z
3127 end
3128 y, z = 2, 3
3129 instance = Item:new()
3130end
3131do
3132 local X = 1
3133 local B
3134 print("only forward declare upper case variables")
3135 local a = 1
3136 B = 2
3137end
3138do
3139 a = 1
3140 print("declare all variables as globals")
3141 x = function()
3142 return 1 + y + z
3143 end
3144 y, z = 2, 3
3145end
3146do
3147 X = 1
3148 print("only declare upper case variables as globals")
3149 local a = 1
3150 B = 2
3151 local Temp = "a local value"
3152end
3153local thing = {
3154 1,
3155 2
3156}
3157local a, b = thing[1], thing[2]
3158print(a, b)
3159local obj = {
3160 hello = "world",
3161 day = "tuesday",
3162 length = 20
3163}
3164local hello, the_day = obj.hello, obj.day
3165print(hello, the_day)
3166local day = obj.day
3167local obj2 = {
3168 numbers = {
3169 1,
3170 2,
3171 3,
3172 4
3173 },
3174 properties = {
3175 color = "green",
3176 height = 13.5
3177 }
3178}
3179local first, second = obj2.numbers[1], obj2.numbers[2]
3180print(first, second, color)
3181local first, second, color
3182local _obj_0 = obj2
3183first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color
3184local concat, insert
3185local _obj_0 = table
3186concat, insert = _obj_0.concat, _obj_0.insert
3187local mix, max, rand
3188local _obj_0 = math
3189mix, max, rand = _obj_0.mix, _obj_0.max, _obj_0.random
3190local name, job
3191local _obj_0 = person
3192name, job = _obj_0.name, _obj_0.job
3193if name == nil then
3194 name = "nameless"
3195end
3196if job == nil then
3197 job = "jobless"
3198end
3199local two, four
3200local _obj_0 = items
3201two, four = _obj_0[2], _obj_0[4]
3202local orders = {
3203 "first",
3204 "second",
3205 "third",
3206 "fourth",
3207 "last"
3208}
3209local first, bulk, last = orders[1], (function()
3210 local _accum_0 = { }
3211 local _len_0 = 1
3212 local _max_0 = #orders + -2 + 1
3213 for _index_0 = 2, _max_0 do
3214 local _item_0 = orders[_index_0]
3215 _accum_0[_len_0] = _item_0
3216 _len_0 = _len_0 + 1
3217 end
3218 return _accum_0
3219end)(), orders[#orders]
3220print(first)
3221print(bulk)
3222print(last)
3223local first, rest
3224do
3225 local _obj_0 = orders
3226 first, rest = _obj_0[1], (function()
3227 local _accum_0 = { }
3228 local _len_0 = 1
3229 local _max_0 = #_obj_0
3230 for _index_0 = 2, _max_0 do
3231 local _item_0 = _obj_0[_index_0]
3232 _accum_0[_len_0] = _item_0
3233 _len_0 = _len_0 + 1
3234 end
3235 return _accum_0
3236 end)()
3237end
3238local start, last
3239do
3240 local _obj_0 = orders
3241 start, last = (function()
3242 local _accum_0 = { }
3243 local _len_0 = 1
3244 local _max_0 = #_obj_0 + -2 + 1
3245 for _index_0 = 1, _max_0 do
3246 local _item_0 = _obj_0[_index_0]
3247 _accum_0[_len_0] = _item_0
3248 _len_0 = _len_0 + 1
3249 end
3250 return _accum_0
3251 end)(), _obj_0[#_obj_0]
3252end
3253local _obj_0 = orders
3254first, last = _obj_0[1], _obj_0[#_obj_0]
3255local tuples = {
3256 {
3257 "hello",
3258 "world"
3259 },
3260 {
3261 "egg",
3262 "head"
3263 }
3264}
3265for _index_0 = 1, #tuples do
3266 local _des_0 = tuples[_index_0]
3267 local left, right = _des_0[1], _des_0[2]
3268 print(left, right)
3269end
3270local user = database.find_user("moon")
3271if user then
3272 print(user.name)
3273end
3274local hello = os.getenv("hello")
3275if hello then
3276 print("You have hello", hello)
3277else
3278 local world = os.getenv("world")
3279 if world then
3280 print("you have world", world)
3281 else
3282 print("nothing :(")
3283 end
3284end
3285do
3286 local success, result = pcall(function()
3287 return "get result without problems"
3288 end)
3289 if success then
3290 print(result)
3291 end
3292end
3293print("OK")
3294repeat
3295 local byte = stream:read_one()
3296 if byte then
3297 print(byte)
3298 else
3299 break
3300 end
3301until false
3302local list = {
3303 1,
3304 2,
3305 3,
3306 4,
3307 5
3308}
3309local fn
3310fn = function(ok)
3311 return ok, table.unpack(list)
3312end
3313(function(_arg_0, ...)
3314 local ok = _arg_0
3315 local count = select('#', ...)
3316 local first = select(1, ...)
3317 return print(ok, count, first)
3318end)(fn(true))
3319local a = 1
3320local b = 2
3321print(a + b)
3322Rx.Observable.fromRange(1, 8):filter(function(x)
3323 return x % 2 == 0
3324end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
3325 return value .. '!'
3326end):subscribe(print)
3327local str = strA .. strB .. strC
3328func(3000, "192.168.1.1")
3329xpcall(function()
3330 return func(1, 2, 3)
3331end, function(err)
3332 return print(yue.traceback(err))
3333end)
3334local success, result = xpcall(function()
3335 return func(1, 2, 3)
3336end, function(err)
3337 return yue.traceback(err)
3338end)
3339xpcall(function()
3340 return func(1, 2, 3)
3341end, function(err)
3342 return print(yue.traceback(err))
3343end)
3344success, result = pcall(function()
3345 return func(1, 2, 3)
3346end)
3347pcall(function()
3348 print("trying")
3349 return func(1, 2, 3)
3350end)
3351success, result = xpcall(function()
3352 return func(1, 2, 3)
3353end, function(err)
3354 return print(yue.traceback(err))
3355end)
3356if success then
3357 print(result)
3358end
3359local a, b, c
3360do
3361 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
3362 return func()
3363 end)
3364 if _ok_0 then
3365 a, b, c = _ret_0, _ret_1, _ret_2
3366 end
3367end
3368do
3369 local _exp_0 = ((function()
3370 return (function(_arg_0, ...)
3371 local _ok_0 = _arg_0
3372 if _ok_0 then
3373 return ...
3374 end
3375 end)(pcall(function()
3376 return func()
3377 end))
3378 end)())
3379 if _exp_0 ~= nil then
3380 a = _exp_0
3381 else
3382 a = "default"
3383 end
3384end
3385f((function()
3386 return (function(_arg_0, ...)
3387 local _ok_0 = _arg_0
3388 if _ok_0 then
3389 return ...
3390 end
3391 end)(pcall(function()
3392 return func()
3393 end))
3394end)())
3395f((function()
3396 return (function(_arg_0, ...)
3397 local _ok_0 = _arg_0
3398 if _ok_0 then
3399 return ...
3400 end
3401 end)(xpcall(function()
3402 print(123)
3403 return func()
3404 end, function(e)
3405 print(e)
3406 return e
3407 end))
3408end)())
3409local a <const> = 123
3410local _ <close> = setmetatable({ }, {
3411 __close = function()
3412 return print("Out of scope.")
3413 end
3414})
3415local a, b, c, d
3416local _obj_0 = tb
3417a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2]
3418Constant = 123
3419local some_string = "Here is a string\n that has a line break in it."
3420print("I am " .. tostring(math.random() * 100) .. "% sure.")
3421local integer = 1000000
3422local hex = 0xEFBBBF
3423local binary = 19
3424local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
3425local fn
3426fn = function()
3427 local str = "foo:\n bar: baz"
3428 return str
3429end
3430local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
3431local my_function
3432my_function = function() end
3433my_function()
3434local func_a
3435func_a = function()
3436 return print("hello world")
3437end
3438local func_b
3439func_b = function()
3440 local value = 100
3441 return print("The value:", value)
3442end
3443func_a()
3444func_b()
3445local sum
3446sum = function(x, y)
3447 return print("sum", x + y)
3448end
3449sum(10, 20)
3450print(sum(10, 20))
3451a(b(c("a", "b", "c")))
3452print("x:", sum(10, 20), "y:", sum(30, 40))
3453local sum
3454sum = function(x, y)
3455 return x + y
3456end
3457print("The sum is ", sum(10, 20))
3458local sum
3459sum = function(x, y)
3460 return x + y
3461end
3462local mystery
3463mystery = function(x, y)
3464 return x + y, x - y
3465end
3466local a, b = mystery(10, 20)
3467local func
3468func = function(self, num)
3469 return self.value + num
3470end
3471local my_function
3472my_function = function(name, height)
3473 if name == nil then
3474 name = "something"
3475 end
3476 if height == nil then
3477 height = 100
3478 end
3479 print("Hello I am", name)
3480 return print("My height is", height)
3481end
3482local some_args
3483some_args = function(x, y)
3484 if x == nil then
3485 x = 100
3486 end
3487 if y == nil then
3488 y = x + 1000
3489 end
3490 return print(x + y)
3491end
3492local a = x - 10
3493local b = x - 10
3494local c = x(-y)
3495local d = x - z
3496local x = func("hello") + 100
3497local y = func("hello" + 100)
3498my_func(5, 4, 3, 8, 9, 10)
3499cool_func(1, 2, 3, 4, 5, 6, 7, 8)
3500my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4)
3501local x = {
3502 1,
3503 2,
3504 3,
3505 4,
3506 a_func(4, 5, 5, 6),
3507 8,
3508 9,
3509 10
3510}
3511local y = {
3512 my_func(1, 2, 3, 4, 5),
3513 5,
3514 6,
3515 7
3516}
3517if func(1, 2, 3, "hello", "world") then
3518 print("hello")
3519 print("I am inside if")
3520end
3521if func(1, 2, 3, "hello", "world") then
3522 print("hello")
3523 print("I am inside if")
3524end
3525local f1
3526f1 = function(_arg_0)
3527 local a, b, c
3528 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
3529 return print(a, b, c)
3530end
3531f1({
3532 a = 1,
3533 b = "2",
3534 c = { }
3535})
3536local f2
3537f2 = function(_arg_0, c)
3538 local a1, b
3539 a1, b = _arg_0.a, _arg_0.b
3540 if a1 == nil then
3541 a1 = 123
3542 end
3543 if b == nil then
3544 b = 'abc'
3545 end
3546 if c == nil then
3547 c = { }
3548 end
3549end
3550print(a1, b, c)
3551local arg1 = {
3552 a = 0
3553}
3554f2(arg1, arg2)
3555local findFirstEven
3556findFirstEven = function(list)
3557 for _index_0 = 1, #list do
3558 local item = list[_index_0]
3559 if type(item) == "table" then
3560 for _index_1 = 1, #item do
3561 local sub = item[_index_1]
3562 if sub % 2 == 0 then
3563 return sub
3564 end
3565 end
3566 end
3567 end
3568 return nil
3569end
3570local findFirstEven
3571findFirstEven = function(list)
3572 for _index_0 = 1, #list do
3573 local item = list[_index_0]
3574 if type(item) == "table" then
3575 for _index_1 = 1, #item do
3576 local sub = item[_index_1]
3577 if sub % 2 == 0 then
3578 return sub
3579 end
3580 end
3581 end
3582 end
3583 return nil
3584end
3585local f
3586f = function(...)
3587 local t = {
3588 n = select("#", ...),
3589 ...
3590 }
3591 print("argument count:", t.n)
3592 print("table length:", #t)
3593 for i = 1, t.n do
3594 print(t[i])
3595 end
3596end
3597f(1, 2, 3)
3598f("a", "b", "c", "d")
3599f()
3600local process
3601process = function(...)
3602 local args = {
3603 n = select("#", ...),
3604 ...
3605 }
3606 local sum = 0
3607 for i = 1, args.n do
3608 if args[i] ~= nil and type(args[i]) == "number" then
3609 sum = sum + args[i]
3610 end
3611 end
3612 return sum
3613end
3614process(1, nil, 3, nil, 5)
3615f(function()
3616 return print("hello")
3617end)
3618f(function(self)
3619 return print(self.value)
3620end)
3621map(function(x)
3622 return x * 2
3623end, {
3624 1,
3625 2,
3626 3
3627})
3628local result, msg
3629do
3630 result, msg = readAsync("filename.txt", function(data)
3631 print(data)
3632 return processAsync(data, function(info)
3633 return check(info)
3634 end)
3635 end)
3636end
3637print(result, msg)
3638local some_values = {
3639 1,
3640 2,
3641 3,
3642 4
3643}
3644local some_values = {
3645 name = "Bill",
3646 age = 200,
3647 ["favorite food"] = "rice"
3648}
3649local profile = {
3650 height = "4 feet",
3651 shoe_size = 13,
3652 favorite_foods = {
3653 "ice cream",
3654 "donuts"
3655 }
3656}
3657local values = {
3658 1,
3659 2,
3660 3,
3661 4,
3662 5,
3663 6,
3664 7,
3665 8,
3666 name = "superman",
3667 occupation = "crime fighting"
3668}
3669my_function({
3670 dance = "Tango",
3671 partner = "none"
3672})
3673local y = {
3674 type = "dog",
3675 legs = 4,
3676 tails = 1
3677}
3678local tbl = {
3679 ["do"] = "something",
3680 ["end"] = "hunger"
3681}
3682local hair = "golden"
3683local height = 200
3684local person = {
3685 hair = hair,
3686 height = height,
3687 shoe_size = 40
3688}
3689print_table({
3690 hair = hair,
3691 height = height
3692})
3693local t = {
3694 [1 + 2] = "hello",
3695 ["hello world"] = true
3696}
3697local some_values = {
3698 1,
3699 2,
3700 3,
3701 4
3702}
3703local list_with_one_element = {
3704 1
3705}
3706local items = {
3707 1,
3708 2,
3709 3,
3710 4
3711}
3712local doubled
3713local _accum_0 = { }
3714local _len_0 = 1
3715for i, item in ipairs(items) do
3716 _accum_0[_len_0] = item * 2
3717 _len_0 = _len_0 + 1
3718end
3719doubled = _accum_0
3720local slice
3721local _accum_0 = { }
3722local _len_0 = 1
3723for i, item in ipairs(items) do
3724 if i > 1 and i < 3 then
3725 _accum_0[_len_0] = item
3726 _len_0 = _len_0 + 1
3727 end
3728end
3729slice = _accum_0
3730local doubled
3731local _accum_0 = { }
3732local _len_0 = 1
3733local _list_0 = items
3734for _index_0 = 1, #_list_0 do
3735 local item = _list_0[_index_0]
3736 _accum_0[_len_0] = item * 2
3737 _len_0 = _len_0 + 1
3738end
3739doubled = _accum_0
3740local data = {
3741 a = {
3742 1,
3743 2,
3744 3
3745 },
3746 b = {
3747 4,
3748 5,
3749 6
3750 }
3751}
3752local flat
3753local _accum_0 = { }
3754for k, v in pairs(data) do
3755 local _len_0 = #_accum_0 + 1
3756 for _index_0 = 1, #v do
3757 local _elm_0 = v[_index_0]
3758 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
3759 end
3760end
3761flat = _accum_0
3762local x_coords = {
3763 4,
3764 5,
3765 6,
3766 7
3767}
3768local y_coords = {
3769 9,
3770 2,
3771 3
3772}
3773local points
3774local _accum_0 = { }
3775local _len_0 = 1
3776for _index_0 = 1, #x_coords do
3777 local x = x_coords[_index_0]
3778 for _index_1 = 1, #y_coords do
3779 local y = y_coords[_index_1]
3780 _accum_0[_len_0] = {
3781 x,
3782 y
3783 }
3784 _len_0 = _len_0 + 1
3785 end
3786end
3787points = _accum_0
3788local evens
3789local _accum_0 = { }
3790local _len_0 = 1
3791for i = 1, 100 do
3792 if i % 2 == 0 then
3793 _accum_0[_len_0] = i
3794 _len_0 = _len_0 + 1
3795 end
3796end
3797evens = _accum_0
3798local thing = {
3799 color = "red",
3800 name = "fast",
3801 width = 123
3802}
3803local thing_copy
3804local _tbl_0 = { }
3805for k, v in pairs(thing) do
3806 _tbl_0[k] = v
3807end
3808thing_copy = _tbl_0
3809local no_color
3810local _tbl_0 = { }
3811for k, v in pairs(thing) do
3812 if k ~= "color" then
3813 _tbl_0[k] = v
3814 end
3815end
3816no_color = _tbl_0
3817local numbers = {
3818 1,
3819 2,
3820 3,
3821 4
3822}
3823local sqrts
3824local _tbl_0 = { }
3825for _index_0 = 1, #numbers do
3826 local i = numbers[_index_0]
3827 _tbl_0[i] = math.sqrt(i)
3828end
3829sqrts = _tbl_0
3830local tuples = {
3831 {
3832 "hello",
3833 "world"
3834 },
3835 {
3836 "foo",
3837 "bar"
3838 }
3839}
3840local tbl
3841local _tbl_0 = { }
3842for _index_0 = 1, #tuples do
3843 local tuple = tuples[_index_0]
3844 local _key_0, _val_0 = unpack(tuple)
3845 _tbl_0[_key_0] = _val_0
3846end
3847tbl = _tbl_0
3848local slice
3849local _accum_0 = { }
3850local _len_0 = 1
3851local _list_0 = items
3852for _index_0 = 1, 5 do
3853 local item = _list_0[_index_0]
3854 _accum_0[_len_0] = item
3855 _len_0 = _len_0 + 1
3856end
3857slice = _accum_0
3858local slice
3859local _accum_0 = { }
3860local _len_0 = 1
3861local _list_0 = items
3862local _max_0 = #_list_0
3863for _index_0 = 2, _max_0 do
3864 local item = _list_0[_index_0]
3865 _accum_0[_len_0] = item
3866 _len_0 = _len_0 + 1
3867end
3868slice = _accum_0
3869local slice
3870local _accum_0 = { }
3871local _len_0 = 1
3872local _list_0 = items
3873local _max_0 = #_list_0
3874for _index_0 = 1, _max_0, 2 do
3875 local item = _list_0[_index_0]
3876 _accum_0[_len_0] = item
3877 _len_0 = _len_0 + 1
3878end
3879slice = _accum_0
3880local slice
3881local _accum_0 = { }
3882local _len_0 = 1
3883local _list_0 = items
3884local _min_0 = #_list_0 + -4 + 1
3885local _max_0 = #_list_0 + -1 + 1
3886for _index_0 = _min_0, _max_0 do
3887 local item = _list_0[_index_0]
3888 _accum_0[_len_0] = item
3889 _len_0 = _len_0 + 1
3890end
3891slice = _accum_0
3892local reverse_slice
3893local _accum_0 = { }
3894local _len_0 = 1
3895local _list_0 = items
3896local _min_0 = #_list_0 + -1 + 1
3897for _index_0 = _min_0, 1, -1 do
3898 local item = _list_0[_index_0]
3899 _accum_0[_len_0] = item
3900 _len_0 = _len_0 + 1
3901end
3902reverse_slice = _accum_0
3903local sub_list
3904local _accum_0 = { }
3905local _len_0 = 1
3906local _list_0 = items
3907for _index_0 = 2, 4 do
3908 local _item_0 = _list_0[_index_0]
3909 _accum_0[_len_0] = _item_0
3910 _len_0 = _len_0 + 1
3911end
3912sub_list = _accum_0
3913for i = 10, 20 do
3914 print(i)
3915end
3916for k = 1, 15, 2 do
3917 print(k)
3918end
3919for key, value in pairs(object) do
3920 print(key, value)
3921end
3922local _list_0 = items
3923for _index_0 = 2, 4 do
3924 local item = _list_0[_index_0]
3925 print(item)
3926end
3927local _list_0 = items
3928for _index_0 = 1, #_list_0 do
3929 local item = _list_0[_index_0]
3930 print(item)
3931end
3932for j = 1, 10, 3 do
3933 print(j)
3934end
3935local doubled_evens
3936local _accum_0 = { }
3937local _len_0 = 1
3938for i = 1, 20 do
3939 if i % 2 == 0 then
3940 _accum_0[_len_0] = i * 2
3941 _len_0 = _len_0 + 1
3942 else
3943 _accum_0[_len_0] = i
3944 _len_0 = _len_0 + 1
3945 end
3946end
3947doubled_evens = _accum_0
3948local first_large
3949local _accum_0
3950local _list_0 = numbers
3951for _index_0 = 1, #_list_0 do
3952 local n = _list_0[_index_0]
3953 if n > 10 then
3954 _accum_0 = n
3955 break
3956 end
3957end
3958first_large = _accum_0
3959local func_a
3960func_a = function()
3961 for i = 1, 10 do
3962 print(i)
3963 end
3964end
3965local func_b
3966func_b = function()
3967 local _accum_0 = { }
3968 local _len_0 = 1
3969 for i = 1, 10 do
3970 _accum_0[_len_0] = i
3971 _len_0 = _len_0 + 1
3972 end
3973 return _accum_0
3974end
3975print(func_a())
3976print(func_b())
3977local i = 10
3978repeat
3979 print(i)
3980 i = i - 1
3981until i == 0
3982local i = 10
3983while i > 0 do
3984 print(i)
3985 i = i - 1
3986end
3987while running == true do
3988 my_function()
3989end
3990local i = 10
3991while not (i == 0) do
3992 print(i)
3993 i = i - 1
3994end
3995while not (running == false) do
3996 my_function()
3997end
3998local i = 0
3999while i < 10 do
4000 i = i + 1
4001 if i % 2 == 0 then
4002 goto _continue_0
4003 end
4004 print(i)
4005 ::_continue_0::
4006end
4007local my_numbers = {
4008 1,
4009 2,
4010 3,
4011 4,
4012 5,
4013 6
4014}
4015local odds
4016local _accum_0 = { }
4017local _len_0 = 1
4018for _index_0 = 1, #my_numbers do
4019 local x = my_numbers[_index_0]
4020 if x % 2 == 1 then
4021 goto _continue_0
4022 end
4023 _accum_0[_len_0] = x
4024 _len_0 = _len_0 + 1
4025 ::_continue_0::
4026end
4027odds = _accum_0
4028local have_coins = false
4029if have_coins then
4030 print("Got coins")
4031else
4032 print("No coins")
4033end
4034local have_coins = false
4035if have_coins then
4036 print("Got coins")
4037else
4038 print("No coins")
4039end
4040local have_coins = false
4041print((function()
4042 if have_coins then
4043 return "Got coins"
4044 else
4045 return "No coins"
4046 end
4047end)())
4048local is_tall
4049is_tall = function(name)
4050 if name == "Rob" then
4051 return true
4052 else
4053 return false
4054 end
4055end
4056local message
4057if is_tall("Rob") then
4058 message = "I am very tall"
4059else
4060 message = "I am not so tall"
4061end
4062print(message)
4063if not (os.date("%A") == "Monday") then
4064 print("it is not Monday!")
4065end
4066if not (math.random() > 0.1) then
4067 print("You're lucky!")
4068end
4069local a = 5
4070if (1 == a or 3 == a or 5 == a or 7 == a) then
4071 print("checking equality with discrete values")
4072end
4073if (function()
4074 local _check_0 = list
4075 for _index_0 = 1, #_check_0 do
4076 if _check_0[_index_0] == a then
4077 return true
4078 end
4079 end
4080 return false
4081end)() then
4082 print("checking if `a` is in a list")
4083end
4084if not (math.random() > 0.1) then
4085 print("You're lucky!")
4086end
4087if name == "Rob" then
4088 print("hello world")
4089end
4090local _list_0 = items
4091for _index_0 = 1, #_list_0 do
4092 local item = _list_0[_index_0]
4093 print("item: ", item)
4094end
4095while game:isRunning() do
4096 game:update()
4097end
4098while not reader:eof() do
4099 reader:parse_line()
4100end
4101local name = "Dan"
4102if "Robert" == name then
4103 print("You are Robert")
4104elseif "Dan" == name or "Daniel" == name then
4105 print("Your name, it's Dan")
4106else
4107 print("I don't know about you with name " .. tostring(name))
4108end
4109local b = 1
4110local next_number
4111if 1 == b then
4112 next_number = 2
4113elseif 2 == b then
4114 next_number = 3
4115else
4116 next_number = error("can't count that high!")
4117end
4118local msg
4119local _exp_0 = math.random(1, 5)
4120if 1 == _exp_0 then
4121 msg = "you are lucky"
4122elseif 2 == _exp_0 then
4123 msg = "you are almost lucky"
4124else
4125 msg = "not so lucky"
4126end
4127do
4128 local _exp_0 = math.random(1, 5)
4129 if 1 == _exp_0 then
4130 print("you are lucky")
4131 else
4132 print("not so lucky")
4133 end
4134end
4135local _exp_0 = math.random(1, 5)
4136if 1 == _exp_0 then
4137 print("you are lucky")
4138else
4139 print("not so lucky")
4140end
4141local items = {
4142 {
4143 x = 100,
4144 y = 200
4145 },
4146 {
4147 width = 300,
4148 height = 400
4149 }
4150}
4151for _index_0 = 1, #items do
4152 local item = items[_index_0]
4153 local _type_0 = type(item)
4154 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4155 local _match_0 = false
4156 if _tab_0 then
4157 local x = item.x
4158 local y = item.y
4159 if x ~= nil and y ~= nil then
4160 _match_0 = true
4161 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
4162 end
4163 end
4164 if not _match_0 then
4165 if _tab_0 then
4166 local width = item.width
4167 local height = item.height
4168 if width ~= nil and height ~= nil then
4169 print("size " .. tostring(width) .. ", " .. tostring(height))
4170 end
4171 end
4172 end
4173end
4174local item = { }
4175local x, y = item.pos.x, item.pos.y
4176if x == nil then
4177 x = 50
4178end
4179if y == nil then
4180 y = 200
4181end
4182local _type_0 = type(item)
4183local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4184if _tab_0 then
4185 do
4186 local _obj_0 = item.pos
4187 local _type_1 = type(_obj_0)
4188 if "table" == _type_1 or "userdata" == _type_1 then
4189 x = _obj_0.x
4190 end
4191 end
4192 do
4193 local _obj_0 = item.pos
4194 local _type_1 = type(_obj_0)
4195 if "table" == _type_1 or "userdata" == _type_1 then
4196 y = _obj_0.y
4197 end
4198 end
4199 if x == nil then
4200 x = 50
4201 end
4202 if y == nil then
4203 y = 200
4204 end
4205 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
4206end
4207local _exp_0 = tb
4208local _type_0 = type(_exp_0)
4209local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4210local _match_0 = false
4211if _tab_0 then
4212 if 1 == _exp_0[1] and 2 == _exp_0[2] and 3 == _exp_0[3] then
4213 _match_0 = true
4214 print("1, 2, 3")
4215 end
4216end
4217if not _match_0 then
4218 local _match_1 = false
4219 if _tab_0 then
4220 local b = _exp_0[2]
4221 if 1 == _exp_0[1] and b ~= nil and 3 == _exp_0[3] then
4222 _match_1 = true
4223 print("1, " .. tostring(b) .. ", 3")
4224 end
4225 end
4226 if not _match_1 then
4227 if _tab_0 then
4228 local b = _exp_0[3]
4229 if b == nil then
4230 b = 3
4231 end
4232 if 1 == _exp_0[1] and 2 == _exp_0[2] then
4233 print("1, 2, " .. tostring(b))
4234 end
4235 end
4236 end
4237end
4238local _exp_0 = tb
4239local _type_0 = type(_exp_0)
4240local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4241local _match_0 = false
4242if _tab_0 then
4243 local result = _exp_0.result
4244 if true == _exp_0.success and result ~= nil then
4245 _match_0 = true
4246 print("success", result)
4247 end
4248end
4249if not _match_0 then
4250 local _match_1 = false
4251 if _tab_0 then
4252 if false == _exp_0.success then
4253 _match_1 = true
4254 print("failed", result)
4255 end
4256 end
4257 if not _match_1 then
4258 print("invalid")
4259 end
4260end
4261local _exp_0 = tb
4262local _type_0 = type(_exp_0)
4263local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4264local _match_0 = false
4265if _tab_0 then
4266 local content
4267 do
4268 local _obj_0 = _exp_0.data
4269 local _type_1 = type(_obj_0)
4270 if "table" == _type_1 or "userdata" == _type_1 then
4271 content = _obj_0.content
4272 end
4273 end
4274 local _val_0
4275 do
4276 local _obj_0 = _exp_0.data
4277 if _obj_0 ~= nil then
4278 _val_0 = _obj_0.type
4279 end
4280 end
4281 if "success" == _val_0 and content ~= nil then
4282 _match_0 = true
4283 print("success", content)
4284 end
4285end
4286if not _match_0 then
4287 local _match_1 = false
4288 if _tab_0 then
4289 local content
4290 do
4291 local _obj_0 = _exp_0.data
4292 local _type_1 = type(_obj_0)
4293 if "table" == _type_1 or "userdata" == _type_1 then
4294 content = _obj_0.content
4295 end
4296 end
4297 local _val_0
4298 do
4299 local _obj_0 = _exp_0.data
4300 if _obj_0 ~= nil then
4301 _val_0 = _obj_0.type
4302 end
4303 end
4304 if "error" == _val_0 and content ~= nil then
4305 _match_1 = true
4306 print("failed", content)
4307 end
4308 end
4309 if not _match_1 then
4310 print("invalid")
4311 end
4312end
4313local _exp_0 = tb
4314local _type_0 = type(_exp_0)
4315local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4316if _tab_0 then
4317 local fourth = _exp_0[4]
4318 local _val_0
4319 do
4320 local _obj_0 = _exp_0[1]
4321 if _obj_0 ~= nil then
4322 _val_0 = _obj_0.a
4323 end
4324 end
4325 local _val_1
4326 do
4327 local _obj_0 = _exp_0[1]
4328 if _obj_0 ~= nil then
4329 _val_1 = _obj_0.b
4330 end
4331 end
4332 local _val_2
4333 do
4334 local _obj_0 = _exp_0[2]
4335 if _obj_0 ~= nil then
4336 _val_2 = _obj_0.a
4337 end
4338 end
4339 local _val_3
4340 do
4341 local _obj_0 = _exp_0[2]
4342 if _obj_0 ~= nil then
4343 _val_3 = _obj_0.b
4344 end
4345 end
4346 local _val_4
4347 do
4348 local _obj_0 = _exp_0[3]
4349 if _obj_0 ~= nil then
4350 _val_4 = _obj_0.a
4351 end
4352 end
4353 local _val_5
4354 do
4355 local _obj_0 = _exp_0[3]
4356 if _obj_0 ~= nil then
4357 _val_5 = _obj_0.b
4358 end
4359 end
4360 if 1 == _val_0 and 2 == _val_1 and 3 == _val_2 and 4 == _val_3 and 5 == _val_4 and 6 == _val_5 and fourth ~= nil then
4361 print("matched", fourth)
4362 end
4363end
4364local segments = {
4365 "admin",
4366 "users",
4367 "logs",
4368 "view"
4369}
4370local _type_0 = type(segments)
4371local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4372if _tab_0 then
4373 local groups
4374 do
4375 local _accum_0 = { }
4376 local _len_0 = 1
4377 local _max_0 = #segments + -3 + 1
4378 for _index_0 = 1, _max_0 do
4379 local _item_0 = segments[_index_0]
4380 _accum_0[_len_0] = _item_0
4381 _len_0 = _len_0 + 1
4382 end
4383 groups = _accum_0
4384 end
4385 local resource = segments[#segments - 1]
4386 local action = segments[#segments]
4387 if resource ~= nil and action ~= nil then
4388 print("Group:", groups)
4389 print("Resource:", resource)
4390 print("Action:", action)
4391 end
4392end
4393local Inventory 4313local Inventory
4394local _class_0 4314local _class_0
4395local _base_0 = { 4315local _base_0 = {
@@ -5050,6 +4970,46 @@ local _with_0 = obj
5050if _with_0 ~= nil then 4970if _with_0 ~= nil then
5051 print(obj.name) 4971 print(obj.name)
5052end 4972end
4973local _with_0 = Person()
4974_with_0.name = "Oswald"
4975_with_0:add_relative(my_dad)
4976_with_0:save()
4977print(_with_0.name)
4978local file
4979local _with_0 = File("favorite_foods.txt")
4980_with_0:set_encoding("utf8")
4981file = _with_0
4982local create_person
4983create_person = function(name, relatives)
4984 local _with_0 = Person()
4985 _with_0.name = name
4986 for _index_0 = 1, #relatives do
4987 local relative = relatives[_index_0]
4988 _with_0:add_relative(relative)
4989 end
4990 return _with_0
4991end
4992local me = create_person("Leaf", {
4993 dad,
4994 mother,
4995 sister
4996})
4997local str = "Hello"
4998print("original:", str)
4999print("upper:", str:upper())
5000local _with_0 = tb
5001_with_0[1] = 1
5002print(_with_0[2])
5003do
5004 local _with_1 = _with_0[abc]
5005 _with_1[3] = _with_1[2]:func()
5006 _with_1["key-name"] = value
5007end
5008_with_0[#_with_0 + 1] = "abc"
5009local _with_0 = obj
5010if _with_0 ~= nil then
5011 print(obj.name)
5012end
5053do 5013do
5054 local var = "hello" 5014 local var = "hello"
5055 print(var) 5015 print(var)
@@ -5071,6 +5031,46 @@ local tbl = {
5071 return 1234 5031 return 1234
5072 end)() 5032 end)()
5073} 5033}
5034do
5035 local var = "hello"
5036 print(var)
5037end
5038print(var)
5039local counter
5040do
5041 local i = 0
5042 counter = function()
5043 i = i + 1
5044 return i
5045 end
5046end
5047print(counter())
5048print(counter())
5049local tbl = {
5050 key = (function()
5051 print("assigning key!")
5052 return 1234
5053 end)()
5054}
5055local my_object = {
5056 value = 1000,
5057 write = function(self)
5058 return print("the value:", self.value)
5059 end
5060}
5061local run_callback
5062run_callback = function(func)
5063 print("running callback...")
5064 return func()
5065end
5066run_callback(my_object.write)
5067run_callback((function()
5068 local _base_0 = my_object
5069 local _fn_0 = _base_0.write
5070 return _fn_0 and function(...)
5071 return _fn_0(_base_0, ...)
5072 end
5073end)())
5074local my_object = { 5074local my_object = {
5075 value = 1000, 5075 value = 1000,
5076 write = function(self) 5076 write = function(self)
@@ -5118,3 +5118,31 @@ my_func = function(add)
5118end 5118end
5119my_func(22) 5119my_func(22)
5120print(i, k) 5120print(i, k)
5121local i = 100
5122local my_func
5123my_func = function()
5124 i = 10
5125 while i > 0 do
5126 print(i)
5127 i = i - 1
5128 end
5129end
5130my_func()
5131print(i)
5132local i = 100
5133local my_func
5134my_func = function()
5135 local i = "hello"
5136end
5137my_func()
5138print(i)
5139local tmp = 1213
5140local i, k = 100, 50
5141local my_func
5142my_func = function(add)
5143 local tmp = tmp + add
5144 i = i + tmp
5145 k = k + tmp
5146end
5147my_func(22)
5148print(i, k)
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index 2aa354e..f4ef2c7 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -76,6 +76,84 @@ end
76local _u1f31b = "月之脚本" 76local _u1f31b = "月之脚本"
77_module_0["🌛"] = _u1f31b 77_module_0["🌛"] = _u1f31b
78return _module_0 78return _module_0
79local _module_0 = { }
80local p, to_lua
81do
82 local _obj_0 = require("yue")
83 p, to_lua = _obj_0.p, _obj_0.to_lua
84end
85local inventory = {
86 equipment = {
87 "sword",
88 "shield"
89 },
90 items = {
91 {
92 name = "potion",
93 count = 10
94 },
95 {
96 name = "bread",
97 count = 3
98 }
99 }
100}
101local map
102map = function(arr, action)
103 local _accum_0 = { }
104 local _len_0 = 1
105 for _index_0 = 1, #arr do
106 local item = arr[_index_0]
107 _accum_0[_len_0] = action(item)
108 _len_0 = _len_0 + 1
109 end
110 return _accum_0
111end
112local filter
113filter = function(arr, cond)
114 local _accum_0 = { }
115 local _len_0 = 1
116 for _index_0 = 1, #arr do
117 local item = arr[_index_0]
118 if cond(item) then
119 _accum_0[_len_0] = item
120 _len_0 = _len_0 + 1
121 end
122 end
123 return _accum_0
124end
125local reduce
126reduce = function(arr, init, action)
127 for _index_0 = 1, #arr do
128 local item = arr[_index_0]
129 init = action(init, item)
130 end
131 return init
132end
133print(reduce(filter(map({
134 1,
135 2,
136 3
137}, function(x)
138 return x * 2
139end), function(x)
140 return x > 4
141end), 0, function(a, b)
142 return a + b
143end))
144local apple = setmetatable({
145 size = 15,
146}, {
147 __index = {
148 color = 0x00ffff
149 }
150})
151if (getmetatable(apple) ~= nil) then
152 p(apple.size, apple.color, getmetatable(apple).__index)
153end
154local _u1f31b = "月之脚本"
155_module_0["🌛"] = _u1f31b
156return _module_0
79local area = 6.2831853071796 * 5 157local area = 6.2831853071796 * 5
80print('你好 世界') 158print('你好 世界')
81do 159do
@@ -115,6 +193,338 @@ end
115do 193do
116 print(123, "hello") 194 print(123, "hello")
117end 195end
196local area = 6.2831853071796 * 5
197print('你好 世界')
198do
199 assert(item ~= nil)
200end
201local value = item
202if (f1() and f2() and f3()) then
203 print("OK")
204end
205do
206 local funcA
207 funcA = function() end
208end
209local funcA
210funcA = function()
211 return "无法访问宏生成月之脚本里定义的变量"
212end
213do
214local function funcB() end
215end
216local funcB
217funcB = function()
218 return "无法访问宏生成 Lua 代码里定义的变量"
219end
220do
221-- 插入原始Lua代码
222if cond then
223 print("输出")
224end
225end
226print("yuescript")
227print(2)
228print("有效的枚举类型:", "Static")
229do
230 print(123, "hello")
231end
232do
233 print(123, "hello")
234end
235if tb ~= nil then
236 tb:func()
237end
238if tb ~= nil then
239 tb:func()
240end
241print(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5)
242local a = 5
243print(1 <= a and a <= 10)
244local v
245v = function(x)
246 print(x)
247 return x
248end
249print((function()
250 local _cond_0 = v(2)
251 if not (v(1) < _cond_0) then
252 return false
253 else
254 return _cond_0 <= v(3)
255 end
256end)())
257print((function()
258 local _cond_0 = v(2)
259 if not (v(1) > _cond_0) then
260 return false
261 else
262 return _cond_0 <= v(3)
263 end
264end)())
265local tab = { }
266tab[#tab + 1] = "Value"
267local tbA = {
268 1,
269 2,
270 3
271}
272local tbB = {
273 4,
274 5,
275 6
276}
277local _len_0 = #tbA + 1
278for _index_0 = 1, #tbB do
279 local _elm_0 = tbB[_index_0]
280 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
281end
282local parts = {
283 "shoulders",
284 "knees"
285}
286local lyrics
287do
288 local _tab_0 = {
289 "head"
290 }
291 local _idx_0 = 1
292 for _key_0, _value_0 in pairs(parts) do
293 if _idx_0 == _key_0 then
294 _tab_0[#_tab_0 + 1] = _value_0
295 _idx_0 = _idx_0 + 1
296 else
297 _tab_0[_key_0] = _value_0
298 end
299 end
300 _tab_0[#_tab_0 + 1] = "and"
301 _tab_0[#_tab_0 + 1] = "toes"
302 lyrics = _tab_0
303end
304local copy
305do
306 local _tab_0 = { }
307 local _idx_0 = 1
308 for _key_0, _value_0 in pairs(other) do
309 if _idx_0 == _key_0 then
310 _tab_0[#_tab_0 + 1] = _value_0
311 _idx_0 = _idx_0 + 1
312 else
313 _tab_0[_key_0] = _value_0
314 end
315 end
316 copy = _tab_0
317end
318local a = {
319 1,
320 2,
321 3,
322 x = 1
323}
324local b = {
325 4,
326 5,
327 y = 1
328}
329local merge
330local _tab_0 = { }
331local _idx_0 = 1
332for _key_0, _value_0 in pairs(a) do
333 if _idx_0 == _key_0 then
334 _tab_0[#_tab_0 + 1] = _value_0
335 _idx_0 = _idx_0 + 1
336 else
337 _tab_0[_key_0] = _value_0
338 end
339end
340local _idx_1 = 1
341for _key_0, _value_0 in pairs(b) do
342 if _idx_1 == _key_0 then
343 _tab_0[#_tab_0 + 1] = _value_0
344 _idx_1 = _idx_1 + 1
345 else
346 _tab_0[_key_0] = _value_0
347 end
348end
349merge = _tab_0
350local last
351do
352 local _item_0 = data.items
353 last = _item_0[#_item_0]
354end
355local second_last
356do
357 local _item_0 = data.items
358 second_last = _item_0[#_item_0 - 1]
359end
360local _obj_0 = data.items
361_obj_0[#_obj_0] = 1
362local mt = { }
363local add
364add = function(self, right)
365 return setmetatable({
366 value = self.value + right.value
367 }, mt)
368end
369mt.__add = add
370local a = setmetatable({
371 value = 1
372}, mt)
373local b = setmetatable({
374 value = 2
375}, {
376 __add = add
377})
378local c = setmetatable({
379 value = 3
380}, {
381 __add = mt.__add
382})
383local d = a + b + c
384print(d.value)
385local _ <close> = setmetatable({ }, {
386 __close = function()
387 return print("超出范围")
388 end
389})
390local tb = setmetatable({ }, {
391 ["value"] = 123
392})
393getmetatable(tb).__index = getmetatable(tb)
394print(tb.value)
395setmetatable(tb, {
396 __index = {
397 item = "hello"
398 }
399})
400print(tb.item)
401local item, new, close, getter
402do
403 local _obj_0 = tb
404 item, new = _obj_0[1], _obj_0.new
405 do
406 local _obj_1 = getmetatable(_obj_0)
407 close, getter = _obj_1.__close, _obj_1.__index
408 end
409end
410print(item, new, close, getter)
411do
412 local _obj_0 = func
413 if _obj_0 ~= nil then
414 _obj_0()
415 end
416end
417print((function()
418 local _obj_0 = abc
419 if _obj_0 ~= nil then
420 local _obj_1 = _obj_0["你好 世界"]
421 if _obj_1 ~= nil then
422 return _obj_1.xyz
423 end
424 return nil
425 end
426 return nil
427end)())
428local x
429do
430 local _obj_0 = tab
431 if _obj_0 ~= nil then
432 x = _obj_0.value
433 end
434end
435local len = (function()
436 local _obj_0 = utf8
437 if _obj_0 ~= nil then
438 return _obj_0.len
439 end
440 return nil
441end)() or (function()
442 local _obj_0 = string
443 if _obj_0 ~= nil then
444 return _obj_0.len
445 end
446 return nil
447end)() or function(o)
448 return #o
449end
450if print and (x ~= nil) then
451 print(x)
452end
453local _with_0 = io.open("test.txt", "w")
454if _with_0 ~= nil then
455 _with_0:write("你好")
456 _with_0:close()
457end
458print("你好")
459print(1, 2)
460print(1, 2, 3)
461print(render(emit(parse(extract(readFile("example.txt"), language, { }), language))))
462local a, b, c, d
463if b ~= nil then
464 a = b
465else
466 if c ~= nil then
467 a = c
468 else
469 a = d
470 end
471end
472func((function()
473 if a ~= nil then
474 return a
475 else
476 return { }
477 end
478end)())
479if a == nil then
480 a = false
481end
482local list = {
483 1,
484 2,
485 3
486}
487func({
488 1,
489 2,
490 3
491})
492local f
493f = function()
494 return {
495 1,
496 2,
497 3
498 }
499end
500local tb = {
501 name = "abc",
502 values = {
503 "a",
504 "b",
505 "c"
506 },
507 objects = {
508 {
509 name = "a",
510 value = 1,
511 func = function(self)
512 return self.value + 1
513 end,
514 tb = {
515 fieldA = 1
516 }
517 },
518 {
519 name = "b",
520 value = 2,
521 func = function(self)
522 return self.value + 2
523 end,
524 tb = { }
525 }
526 }
527}
118if tb ~= nil then 528if tb ~= nil then
119 tb:func() 529 tb:func()
120end 530end
@@ -539,6 +949,188 @@ _module_0 = function()
539 return 123 949 return 123
540end 950end
541return _module_0 951return _module_0
952do
953 local insert, concat = table.insert, table.concat
954 local C, Ct, Cmt
955 do
956 local _obj_0 = require("lpeg")
957 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
958 end
959 local x, y, z
960 do
961 local _obj_0 = require('mymodule')
962 x, y, z = _obj_0.x, _obj_0.y, _obj_0.z
963 end
964 local a, b, c
965 local _obj_0 = require('module')
966 a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
967end
968do
969 local module = require('module')
970 local module_x = require('module_x')
971 local d_a_s_h_e_s = require("d-a-s-h-e-s")
972 local part = require("module.part")
973end
974do
975 local PlayerModule = require("player")
976 local C, Ct, Cmt
977 do
978 local _obj_0 = require("lpeg")
979 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
980 end
981 local one, two, ch
982 local _obj_0 = require("export")
983 one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1]
984end
985do
986 local tostring <const> = tostring
987 local concat <const> = table.concat
988 print(concat({
989 "a",
990 tostring(1)
991 }))
992end
993do
994 local print <const> = print
995 local math <const> = math
996 print("hello")
997 math.random(3)
998end
999do
1000 local print <const> = print
1001 print(FLAG)
1002 FLAG = 123
1003end
1004local _module_0 = { }
1005local a, b, c = 1, 2, 3
1006_module_0["a"], _module_0["b"], _module_0["c"] = a, b, c
1007local cool = "cat"
1008_module_0["cool"] = cool
1009local What
1010if this then
1011 What = "abc"
1012else
1013 What = "def"
1014end
1015_module_0["What"] = What
1016local y
1017y = function()
1018 local hallo = 3434
1019end
1020_module_0["y"] = y
1021local Something
1022local _class_0
1023local _base_0 = {
1024 umm = "cool"
1025}
1026if _base_0.__index == nil then
1027 _base_0.__index = _base_0
1028end
1029_class_0 = setmetatable({
1030 __init = function() end,
1031 __base = _base_0,
1032 __name = "Something"
1033}, {
1034 __index = _base_0,
1035 __call = function(cls, ...)
1036 local _self_0 = setmetatable({ }, _base_0)
1037 cls.__init(_self_0, ...)
1038 return _self_0
1039 end
1040})
1041_base_0.__class = _class_0
1042Something = _class_0
1043_module_0["Something"] = Something
1044return _module_0
1045local _module_0 = { }
1046local loadstring, tolua
1047do
1048 local _obj_0 = yue
1049 loadstring, tolua = _obj_0.loadstring, _obj_0.to_lua
1050end
1051_module_0["loadstring"], _module_0["tolua"] = loadstring, tolua
1052local fieldA = tb.itemA.fieldA
1053if fieldA == nil then
1054 fieldA = '默认值'
1055end
1056_module_0["fieldA"] = fieldA
1057return _module_0
1058local _module_0 = setmetatable({ }, { })
1059_module_0.itemA = tb
1060getmetatable(_module_0).__index = items
1061_module_0["a-b-c"] = 123
1062return _module_0
1063local _module_0 = { }
1064local d, e, f = 3, 2, 1
1065_module_0[#_module_0 + 1] = d
1066_module_0[#_module_0 + 1] = e
1067_module_0[#_module_0 + 1] = f
1068if this then
1069 _module_0[#_module_0 + 1] = 123
1070else
1071 _module_0[#_module_0 + 1] = 456
1072end
1073local _with_0 = tmp
1074local j = 2000
1075_module_0[#_module_0 + 1] = _with_0
1076return _module_0
1077local _module_0 = nil
1078_module_0 = function()
1079 print("你好")
1080 return 123
1081end
1082return _module_0
1083local hello = "world"
1084local a, b, c = 1, 2, 3
1085hello = 123
1086local x = 1
1087x = x + 1
1088x = x - 1
1089x = x * 10
1090x = x / 10
1091x = x % 10
1092local s = s .. "world"
1093local arg = arg or "默认值"
1094local a = 0
1095local b = 0
1096local c = 0
1097local d = 0
1098local e = 0
1099local x = f()
1100local y = x
1101local z = x
1102do
1103 local a = 1
1104 local x, y, z
1105 print("预先声明后续所有变量为局部变量")
1106 x = function()
1107 return 1 + y + z
1108 end
1109 y, z = 2, 3
1110 instance = Item:new()
1111end
1112do
1113 local X = 1
1114 local B
1115 print("只预先声明后续大写的变量为局部变量")
1116 local a = 1
1117 B = 2
1118end
1119do
1120 a = 1
1121 print("预先声明所有变量为全局变量")
1122 x = function()
1123 return 1 + y + z
1124 end
1125 y, z = 2, 3
1126end
1127do
1128 x = 1
1129 print("只预先声明大写的变量为全局变量")
1130 local a = 1
1131 B = 2
1132 local Temp = "一个局部值"
1133end
542local hello = "world" 1134local hello = "world"
543local a, b, c = 1, 2, 3 1135local a, b, c = 1, 2, 3
544hello = 123 1136hello = 123
@@ -707,6 +1299,155 @@ for _index_0 = 1, #tuples do
707 local left, right = _des_0[1], _des_0[2] 1299 local left, right = _des_0[1], _des_0[2]
708 print(left, right) 1300 print(left, right)
709end 1301end
1302local thing = {
1303 1,
1304 2
1305}
1306local a, b = thing[1], thing[2]
1307print(a, b)
1308local obj = {
1309 hello = "world",
1310 day = "tuesday",
1311 length = 20
1312}
1313local hello, the_day = obj.hello, obj.day
1314print(hello, the_day)
1315local day = obj.day
1316local obj2 = {
1317 numbers = {
1318 1,
1319 2,
1320 3,
1321 4
1322 },
1323 properties = {
1324 color = "green",
1325 height = 13.5
1326 }
1327}
1328local first, second = obj2.numbers[1], obj2.numbers[2]
1329print(first, second, color)
1330local first, second, color
1331local _obj_0 = obj2
1332first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color
1333local concat, insert
1334local _obj_0 = table
1335concat, insert = _obj_0.concat, _obj_0.insert
1336local mix, max, rand
1337local _obj_0 = math
1338mix, max, rand = _obj_0.mix, _obj_0.max, _obj_0.random
1339local name, job
1340local _obj_0 = person
1341name, job = _obj_0.name, _obj_0.job
1342if name == nil then
1343 name = "nameless"
1344end
1345if job == nil then
1346 job = "jobless"
1347end
1348local two, four
1349local _obj_0 = items
1350two, four = _obj_0[2], _obj_0[4]
1351local orders = {
1352 "first",
1353 "second",
1354 "third",
1355 "fourth",
1356 "last"
1357}
1358local first, bulk, last = orders[1], (function()
1359 local _accum_0 = { }
1360 local _len_0 = 1
1361 local _max_0 = #orders + -2 + 1
1362 for _index_0 = 2, _max_0 do
1363 local _item_0 = orders[_index_0]
1364 _accum_0[_len_0] = _item_0
1365 _len_0 = _len_0 + 1
1366 end
1367 return _accum_0
1368end)(), orders[#orders]
1369print(first)
1370print(bulk)
1371print(last)
1372local first, rest
1373do
1374 local _obj_0 = orders
1375 first, rest = _obj_0[1], (function()
1376 local _accum_0 = { }
1377 local _len_0 = 1
1378 local _max_0 = #_obj_0
1379 for _index_0 = 2, _max_0 do
1380 local _item_0 = _obj_0[_index_0]
1381 _accum_0[_len_0] = _item_0
1382 _len_0 = _len_0 + 1
1383 end
1384 return _accum_0
1385 end)()
1386end
1387local start, last
1388do
1389 local _obj_0 = orders
1390 start, last = (function()
1391 local _accum_0 = { }
1392 local _len_0 = 1
1393 local _max_0 = #_obj_0 + -2 + 1
1394 for _index_0 = 1, _max_0 do
1395 local _item_0 = _obj_0[_index_0]
1396 _accum_0[_len_0] = _item_0
1397 _len_0 = _len_0 + 1
1398 end
1399 return _accum_0
1400 end)(), _obj_0[#_obj_0]
1401end
1402local _obj_0 = orders
1403first, last = _obj_0[1], _obj_0[#_obj_0]
1404local tuples = {
1405 {
1406 "hello",
1407 "world"
1408 },
1409 {
1410 "egg",
1411 "head"
1412 }
1413}
1414for _index_0 = 1, #tuples do
1415 local _des_0 = tuples[_index_0]
1416 local left, right = _des_0[1], _des_0[2]
1417 print(left, right)
1418end
1419local user = database.find_user("moon")
1420if user then
1421 print(user.name)
1422end
1423local hello = os.getenv("hello")
1424if hello then
1425 print("你有 hello", hello)
1426else
1427 local world = os.getenv("world")
1428 if world then
1429 print("你有 world", world)
1430 else
1431 print("什么都没有 :(")
1432 end
1433end
1434do
1435 local success, result = pcall(function()
1436 return "无报错地获取结果"
1437 end)
1438 if success then
1439 print(result)
1440 end
1441end
1442print("好的")
1443repeat
1444 local byte = stream:read_one()
1445 if byte then
1446 print(byte)
1447 else
1448 break
1449 end
1450until false
710local user = database.find_user("moon") 1451local user = database.find_user("moon")
711if user then 1452if user then
712 print(user.name) 1453 print(user.name)
@@ -756,6 +1497,23 @@ end
756 local first = select(1, ...) 1497 local first = select(1, ...)
757 return print(ok, count, first) 1498 return print(ok, count, first)
758end)(fn(true)) 1499end)(fn(true))
1500local list = {
1501 1,
1502 2,
1503 3,
1504 4,
1505 5
1506}
1507local fn
1508fn = function(ok)
1509 return ok, table.unpack(list)
1510end
1511(function(_arg_0, ...)
1512 local ok = _arg_0
1513 local count = select('#', ...)
1514 local first = select(1, ...)
1515 return print(ok, count, first)
1516end)(fn(true))
759local a = 1 1517local a = 1
760local b = 2 1518local b = 2
761print(a + b) 1519print(a + b)
@@ -764,6 +1522,16 @@ Rx.Observable.fromRange(1, 8):filter(function(x)
764end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) 1522end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
765 return value .. '!' 1523 return value .. '!'
766end):subscribe(print) 1524end):subscribe(print)
1525local a = 1
1526local b = 2
1527print(a + b)
1528Rx.Observable.fromRange(1, 8):filter(function(x)
1529 return x % 2 == 0
1530end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
1531 return value .. '!'
1532end):subscribe(print)
1533local str = strA .. strB .. strC
1534func(3000, "192.168.1.1")
767local str = strA .. strB .. strC 1535local str = strA .. strB .. strC
768func(3000, "192.168.1.1") 1536func(3000, "192.168.1.1")
769xpcall(function() 1537xpcall(function()
@@ -846,6 +1614,96 @@ f((function()
846 return e 1614 return e
847 end)) 1615 end))
848end)()) 1616end)())
1617xpcall(function()
1618 return func(1, 2, 3)
1619end, function(err)
1620 return print(yue.traceback(err))
1621end)
1622local success, result = xpcall(function()
1623 return func(1, 2, 3)
1624end, function(err)
1625 return yue.traceback(err)
1626end)
1627xpcall(function()
1628 return func(1, 2, 3)
1629end, function(err)
1630 return print(yue.traceback(err))
1631end)
1632success, result = pcall(function()
1633 return func(1, 2, 3)
1634end)
1635pcall(function()
1636 print("尝试中")
1637 return func(1, 2, 3)
1638end)
1639success, result = xpcall(function()
1640 return func(1, 2, 3)
1641end, function(err)
1642 return print(yue.traceback(err))
1643end)
1644if success then
1645 print(result)
1646end
1647local a, b, c
1648do
1649 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
1650 return func()
1651 end)
1652 if _ok_0 then
1653 a, b, c = _ret_0, _ret_1, _ret_2
1654 end
1655end
1656do
1657 local _exp_0 = ((function()
1658 return (function(_arg_0, ...)
1659 local _ok_0 = _arg_0
1660 if _ok_0 then
1661 return ...
1662 end
1663 end)(pcall(function()
1664 return func()
1665 end))
1666 end)())
1667 if _exp_0 ~= nil then
1668 a = _exp_0
1669 else
1670 a = "default"
1671 end
1672end
1673f((function()
1674 return (function(_arg_0, ...)
1675 local _ok_0 = _arg_0
1676 if _ok_0 then
1677 return ...
1678 end
1679 end)(pcall(function()
1680 return func()
1681 end))
1682end)())
1683f((function()
1684 return (function(_arg_0, ...)
1685 local _ok_0 = _arg_0
1686 if _ok_0 then
1687 return ...
1688 end
1689 end)(xpcall(function()
1690 print(123)
1691 return func()
1692 end, function(e)
1693 print(e)
1694 return e
1695 end))
1696end)())
1697local a <const> = 123
1698local _ <close> = setmetatable({ }, {
1699 __close = function()
1700 return print("超出范围。")
1701 end
1702})
1703local a, b, c, d
1704local _obj_0 = tb
1705a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2]
1706Constant = 123
849local a <const> = 123 1707local a <const> = 123
850local _ <close> = setmetatable({ }, { 1708local _ <close> = setmetatable({ }, {
851 __close = function() 1709 __close = function()
@@ -868,6 +1726,18 @@ fn = function()
868 return str 1726 return str
869end 1727end
870local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'" 1728local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
1729local some_string = "这是一个字符串\n 并包括一个换行。"
1730print("我有" .. tostring(math.random() * 100) .. "%的把握。")
1731local integer = 1000000
1732local hex = 0xEFBBBF
1733local binary = 19
1734local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
1735local fn
1736fn = function()
1737 local str = "foo:\n bar: baz"
1738 return str
1739end
1740local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
871local my_function 1741local my_function
872my_function = function() end 1742my_function = function() end
873my_function() 1743my_function()
@@ -929,6 +1799,12 @@ some_args = function(x, y)
929 end 1799 end
930 return print(x + y) 1800 return print(x + y)
931end 1801end
1802local a = x - 10
1803local b = x - 10
1804local c = x(-y)
1805local d = x - z
1806local x = func("hello") + 100
1807local y = func("hello" + 100)
932my_func(5, 4, 3, 8, 9, 10) 1808my_func(5, 4, 3, 8, 9, 10)
933cool_func(1, 2, 3, 4, 5, 6, 7, 8) 1809cool_func(1, 2, 3, 4, 5, 6, 7, 8)
934my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4) 1810my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4)
@@ -1046,8 +1922,215 @@ process = function(...)
1046 return sum 1922 return sum
1047end 1923end
1048process(1, nil, 3, nil, 5) 1924process(1, nil, 3, nil, 5)
1049f(function() 1925local my_function
1050 return print("hello") 1926my_function = function() end
1927my_function()
1928local func_a
1929func_a = function()
1930 return print("你好,世界")
1931end
1932local func_b
1933func_b = function()
1934 local value = 100
1935 return print("这个值是:", value)
1936end
1937func_a()
1938func_b()
1939local sum
1940sum = function(x, y)
1941 return print("数字的和", x + y)
1942end
1943sum(10, 20)
1944print(sum(10, 20))
1945a(b(c("a", "b", "c")))
1946print("x:", sum(10, 20), "y:", sum(30, 40))
1947local sum
1948sum = function(x, y)
1949 return x + y
1950end
1951print("数字的和是", sum(10, 20))
1952local sum
1953sum = function(x, y)
1954 return x + y
1955end
1956local mystery
1957mystery = function(x, y)
1958 return x + y, x - y
1959end
1960local a, b = mystery(10, 20)
1961local func
1962func = function(self, num)
1963 return self.value + num
1964end
1965local my_function
1966my_function = function(name, height)
1967 if name == nil then
1968 name = "某物"
1969 end
1970 if height == nil then
1971 height = 100
1972 end
1973 print("你好,我是", name)
1974 return print("我的高度是", height)
1975end
1976local some_args
1977some_args = function(x, y)
1978 if x == nil then
1979 x = 100
1980 end
1981 if y == nil then
1982 y = x + 1000
1983 end
1984 return print(x + y)
1985end
1986local a = x - 10
1987local b = x - 10
1988local c = x(-y)
1989local d = x - z
1990local x = func("hello") + 100
1991local y = func("hello" + 100)
1992my_func(5, 4, 3, 8, 9, 10)
1993cool_func(1, 2, 3, 4, 5, 6, 7, 8)
1994my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4)
1995local x = {
1996 1,
1997 2,
1998 3,
1999 4,
2000 a_func(4, 5, 5, 6),
2001 8,
2002 9,
2003 10
2004}
2005local y = {
2006 my_func(1, 2, 3, 4, 5),
2007 5,
2008 6,
2009 7
2010}
2011if func(1, 2, 3, "你好", "世界") then
2012 print("你好")
2013 print("我在if内部")
2014end
2015if func(1, 2, 3, "你好", "世界") then
2016 print("你好")
2017 print("我在if内部")
2018end
2019local f1
2020f1 = function(_arg_0)
2021 local a, b, c
2022 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
2023 return print(a, b, c)
2024end
2025f1({
2026 a = 1,
2027 b = "2",
2028 c = { }
2029})
2030local f2
2031f2 = function(_arg_0, c)
2032 local a1, b
2033 a1, b = _arg_0.a, _arg_0.b
2034 if a1 == nil then
2035 a1 = 123
2036 end
2037 if b == nil then
2038 b = 'abc'
2039 end
2040 if c == nil then
2041 c = { }
2042 end
2043 return print(a1, b, c)
2044end
2045local arg1 = {
2046 a = 0
2047}
2048f2(arg1, arg2)
2049local findFirstEven
2050findFirstEven = function(list)
2051 for _index_0 = 1, #list do
2052 local item = list[_index_0]
2053 if type(item) == "table" then
2054 for _index_1 = 1, #item do
2055 local sub = item[_index_1]
2056 if sub % 2 == 0 then
2057 return sub
2058 end
2059 end
2060 end
2061 end
2062 return nil
2063end
2064local findFirstEven
2065findFirstEven = function(list)
2066 for _index_0 = 1, #list do
2067 local item = list[_index_0]
2068 if type(item) == "table" then
2069 for _index_1 = 1, #item do
2070 local sub = item[_index_1]
2071 if sub % 2 == 0 then
2072 return sub
2073 end
2074 end
2075 end
2076 end
2077 return nil
2078end
2079local f
2080f = function(...)
2081 local t = {
2082 n = select("#", ...),
2083 ...
2084 }
2085 print("参数个数:", t.n)
2086 print("表长度:", #t)
2087 for i = 1, t.n do
2088 print(t[i])
2089 end
2090end
2091f(1, 2, 3)
2092f("a", "b", "c", "d")
2093f()
2094local process
2095process = function(...)
2096 local args = {
2097 n = select("#", ...),
2098 ...
2099 }
2100 local sum = 0
2101 for i = 1, args.n do
2102 if args[i] ~= nil and type(args[i]) == "number" then
2103 sum = sum + args[i]
2104 end
2105 end
2106 return sum
2107end
2108process(1, nil, 3, nil, 5)
2109f(function(x)
2110 return print("hello" .. x)
2111end)
2112f(function(self)
2113 return print(self.value)
2114end)
2115map(function(x)
2116 return x * 2
2117end, {
2118 1,
2119 2,
2120 3
2121})
2122local result, msg
2123do
2124 result, msg = readAsync("文件名.txt", function(data)
2125 print(data)
2126 return processAsync(data, function(info)
2127 return check(info)
2128 end)
2129 end)
2130end
2131print(result, msg)
2132f(function(x)
2133 return print("hello" .. x)
1051end) 2134end)
1052f(function(self) 2135f(function(self)
1053 return print(self.value) 2136 return print(self.value)
@@ -1137,6 +2220,74 @@ local some_values = {
1137local list_with_one_element = { 2220local list_with_one_element = {
1138 1 2221 1
1139} 2222}
2223local some_values = {
2224 1,
2225 2,
2226 3,
2227 4
2228}
2229local some_values = {
2230 name = "Bill",
2231 age = 200,
2232 ["favorite food"] = "rice"
2233}
2234local profile = {
2235 height = "4英尺",
2236 shoe_size = 13,
2237 favorite_foods = {
2238 "冰淇淋",
2239 "甜甜圈"
2240 }
2241}
2242local values = {
2243 1,
2244 2,
2245 3,
2246 4,
2247 5,
2248 6,
2249 7,
2250 8,
2251 name = "超人",
2252 occupation = "打击犯罪"
2253}
2254my_function({
2255 dance = "探戈",
2256 partner = "无"
2257})
2258local y = {
2259 type = "狗",
2260 legs = 4,
2261 tails = 1
2262}
2263local tbl = {
2264 ["do"] = "某事",
2265 ["end"] = "饥饿"
2266}
2267local hair = "金色"
2268local height = 200
2269local person = {
2270 hair = hair,
2271 height = height,
2272 shoe_size = 40
2273}
2274print_table({
2275 hair = hair,
2276 height = height
2277})
2278local t = {
2279 [1 + 2] = "你好",
2280 ["你好 世界"] = true
2281}
2282local some_values = {
2283 1,
2284 2,
2285 3,
2286 4
2287}
2288local list_with_one_element = {
2289 1
2290}
1140local items = { 2291local items = {
1141 1, 2292 1,
1142 2, 2293 2,
@@ -1335,15 +2486,250 @@ for _index_0 = _min_0, 1, -1 do
1335end 2486end
1336reverse_slice = _accum_0 2487reverse_slice = _accum_0
1337local sub_list 2488local sub_list
2489do
2490 local _accum_0 = { }
2491 local _len_0 = 1
2492 local _list_0 = items
2493 for _index_0 = 2, 4 do
2494 local _item_0 = _list_0[_index_0]
2495 _accum_0[_len_0] = _item_0
2496 _len_0 = _len_0 + 1
2497 end
2498 sub_list = _accum_0
2499end
2500local last_four_items
1338local _accum_0 = { } 2501local _accum_0 = { }
1339local _len_0 = 1 2502local _len_0 = 1
1340local _list_0 = items 2503local _list_0 = items
1341for _index_0 = 2, 4 do 2504local _min_0 = #_list_0 + -4 + 1
2505local _max_0 = #_list_0 + -1 + 1
2506for _index_0 = _min_0, _max_0 do
2507 local _item_0 = _list_0[_index_0]
2508 _accum_0[_len_0] = _item_0
2509 _len_0 = _len_0 + 1
2510end
2511last_four_items = _accum_0
2512local items = {
2513 1,
2514 2,
2515 3,
2516 4
2517}
2518local doubled
2519local _accum_0 = { }
2520local _len_0 = 1
2521for i, item in ipairs(items) do
2522 _accum_0[_len_0] = item * 2
2523 _len_0 = _len_0 + 1
2524end
2525doubled = _accum_0
2526local slice
2527local _accum_0 = { }
2528local _len_0 = 1
2529for i, item in ipairs(items) do
2530 if i > 1 and i < 3 then
2531 _accum_0[_len_0] = item
2532 _len_0 = _len_0 + 1
2533 end
2534end
2535slice = _accum_0
2536local doubled
2537local _accum_0 = { }
2538local _len_0 = 1
2539local _list_0 = items
2540for _index_0 = 1, #_list_0 do
2541 local item = _list_0[_index_0]
2542 _accum_0[_len_0] = item * 2
2543 _len_0 = _len_0 + 1
2544end
2545doubled = _accum_0
2546local data = {
2547 a = {
2548 1,
2549 2,
2550 3
2551 },
2552 b = {
2553 4,
2554 5,
2555 6
2556 }
2557}
2558local flat
2559local _accum_0 = { }
2560for k, v in pairs(data) do
2561 local _len_0 = #_accum_0 + 1
2562 for _index_0 = 1, #v do
2563 local _elm_0 = v[_index_0]
2564 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
2565 end
2566end
2567flat = _accum_0
2568local x_coords = {
2569 4,
2570 5,
2571 6,
2572 7
2573}
2574local y_coords = {
2575 9,
2576 2,
2577 3
2578}
2579local points
2580local _accum_0 = { }
2581local _len_0 = 1
2582for _index_0 = 1, #x_coords do
2583 local x = x_coords[_index_0]
2584 for _index_1 = 1, #y_coords do
2585 local y = y_coords[_index_1]
2586 _accum_0[_len_0] = {
2587 x,
2588 y
2589 }
2590 _len_0 = _len_0 + 1
2591 end
2592end
2593points = _accum_0
2594local evens
2595local _accum_0 = { }
2596local _len_0 = 1
2597for i = 1, 100 do
2598 if i % 2 == 0 then
2599 _accum_0[_len_0] = i
2600 _len_0 = _len_0 + 1
2601 end
2602end
2603evens = _accum_0
2604local thing = {
2605 color = "red",
2606 name = "fast",
2607 width = 123
2608}
2609local thing_copy
2610local _tbl_0 = { }
2611for k, v in pairs(thing) do
2612 _tbl_0[k] = v
2613end
2614thing_copy = _tbl_0
2615local no_color
2616local _tbl_0 = { }
2617for k, v in pairs(thing) do
2618 if k ~= "color" then
2619 _tbl_0[k] = v
2620 end
2621end
2622no_color = _tbl_0
2623local numbers = {
2624 1,
2625 2,
2626 3,
2627 4
2628}
2629local sqrts
2630local _tbl_0 = { }
2631for _index_0 = 1, #numbers do
2632 local i = numbers[_index_0]
2633 _tbl_0[i] = math.sqrt(i)
2634end
2635sqrts = _tbl_0
2636local tuples = {
2637 {
2638 "hello",
2639 "world"
2640 },
2641 {
2642 "foo",
2643 "bar"
2644 }
2645}
2646local tbl
2647local _tbl_0 = { }
2648for _index_0 = 1, #tuples do
2649 local tuple = tuples[_index_0]
2650 local _key_0, _val_0 = unpack(tuple)
2651 _tbl_0[_key_0] = _val_0
2652end
2653tbl = _tbl_0
2654local slice
2655local _accum_0 = { }
2656local _len_0 = 1
2657local _list_0 = items
2658for _index_0 = 1, 5 do
2659 local item = _list_0[_index_0]
2660 _accum_0[_len_0] = item
2661 _len_0 = _len_0 + 1
2662end
2663slice = _accum_0
2664local slice
2665local _accum_0 = { }
2666local _len_0 = 1
2667local _list_0 = items
2668local _max_0 = #_list_0
2669for _index_0 = 2, _max_0 do
2670 local item = _list_0[_index_0]
2671 _accum_0[_len_0] = item
2672 _len_0 = _len_0 + 1
2673end
2674slice = _accum_0
2675local slice
2676local _accum_0 = { }
2677local _len_0 = 1
2678local _list_0 = items
2679local _max_0 = #_list_0
2680for _index_0 = 1, _max_0, 2 do
2681 local item = _list_0[_index_0]
2682 _accum_0[_len_0] = item
2683 _len_0 = _len_0 + 1
2684end
2685slice = _accum_0
2686local slice
2687local _accum_0 = { }
2688local _len_0 = 1
2689local _list_0 = items
2690local _min_0 = #_list_0 + -4 + 1
2691local _max_0 = #_list_0 + -1 + 1
2692for _index_0 = _min_0, _max_0 do
2693 local item = _list_0[_index_0]
2694 _accum_0[_len_0] = item
2695 _len_0 = _len_0 + 1
2696end
2697slice = _accum_0
2698local reverse_slice
2699local _accum_0 = { }
2700local _len_0 = 1
2701local _list_0 = items
2702local _min_0 = #_list_0 + -1 + 1
2703for _index_0 = _min_0, 1, -1 do
2704 local item = _list_0[_index_0]
2705 _accum_0[_len_0] = item
2706 _len_0 = _len_0 + 1
2707end
2708reverse_slice = _accum_0
2709local sub_list
2710do
2711 local _accum_0 = { }
2712 local _len_0 = 1
2713 local _list_0 = items
2714 for _index_0 = 2, 4 do
2715 local _item_0 = _list_0[_index_0]
2716 _accum_0[_len_0] = _item_0
2717 _len_0 = _len_0 + 1
2718 end
2719 sub_list = _accum_0
2720end
2721local last_four_items
2722local _accum_0 = { }
2723local _len_0 = 1
2724local _list_0 = items
2725local _min_0 = #_list_0 + -4 + 1
2726local _max_0 = #_list_0 + -1 + 1
2727for _index_0 = _min_0, _max_0 do
1342 local _item_0 = _list_0[_index_0] 2728 local _item_0 = _list_0[_index_0]
1343 _accum_0[_len_0] = _item_0 2729 _accum_0[_len_0] = _item_0
1344 _len_0 = _len_0 + 1 2730 _len_0 = _len_0 + 1
1345end 2731end
1346sub_list = _accum_0 2732last_four_items = _accum_0
1347for i = 10, 20 do 2733for i = 10, 20 do
1348 print(i) 2734 print(i)
1349end 2735end
@@ -1408,6 +2794,86 @@ func_b = function()
1408end 2794end
1409print(func_a()) 2795print(func_a())
1410print(func_b()) 2796print(func_b())
2797for i = 10, 20 do
2798 print(i)
2799end
2800for k = 1, 15, 2 do
2801 print(k)
2802end
2803for key, value in pairs(object) do
2804 print(key, value)
2805end
2806local _list_0 = items
2807for _index_0 = 2, 4 do
2808 local item = _list_0[_index_0]
2809 print(item)
2810end
2811local _list_0 = items
2812for _index_0 = 1, #_list_0 do
2813 local item = _list_0[_index_0]
2814 print(item)
2815end
2816for j = 1, 10, 3 do
2817 print(j)
2818end
2819local doubled_evens
2820local _accum_0 = { }
2821local _len_0 = 1
2822for i = 1, 20 do
2823 if i % 2 == 0 then
2824 _accum_0[_len_0] = i * 2
2825 _len_0 = _len_0 + 1
2826 else
2827 _accum_0[_len_0] = i
2828 _len_0 = _len_0 + 1
2829 end
2830end
2831doubled_evens = _accum_0
2832local first_large
2833local _accum_0
2834local _list_0 = numbers
2835for _index_0 = 1, #_list_0 do
2836 local n = _list_0[_index_0]
2837 if n > 10 then
2838 _accum_0 = n
2839 break
2840 end
2841end
2842first_large = _accum_0
2843local func_a
2844func_a = function()
2845 for i = 1, 10 do
2846 print(i)
2847 end
2848end
2849local func_b
2850func_b = function()
2851 local _accum_0 = { }
2852 local _len_0 = 1
2853 for i = 1, 10 do
2854 _accum_0[_len_0] = i
2855 _len_0 = _len_0 + 1
2856 end
2857 return _accum_0
2858end
2859print(func_a())
2860print(func_b())
2861local i = 10
2862while i > 0 do
2863 print(i)
2864 i = i - 1
2865end
2866while running == true do
2867 my_function()
2868end
2869local i = 10
2870while not (i == 0) do
2871 print(i)
2872 i = i - 1
2873end
2874while not (running == false) do
2875 my_function()
2876end
1411local i = 10 2877local i = 10
1412repeat 2878repeat
1413 print(i) 2879 print(i)
@@ -1429,6 +2895,11 @@ end
1429while not (running == false) do 2895while not (running == false) do
1430 my_function() 2896 my_function()
1431end 2897end
2898local i = 10
2899repeat
2900 print(i)
2901 i = i - 1
2902until i == 0
1432local i = 0 2903local i = 0
1433while i < 10 do 2904while i < 10 do
1434 i = i + 1 2905 i = i + 1
@@ -1459,6 +2930,95 @@ for _index_0 = 1, #my_numbers do
1459 ::_continue_0:: 2930 ::_continue_0::
1460end 2931end
1461odds = _accum_0 2932odds = _accum_0
2933local i = 0
2934while i < 10 do
2935 i = i + 1
2936 if i % 2 == 0 then
2937 goto _continue_0
2938 end
2939 print(i)
2940 ::_continue_0::
2941end
2942local my_numbers = {
2943 1,
2944 2,
2945 3,
2946 4,
2947 5,
2948 6
2949}
2950local odds
2951local _accum_0 = { }
2952local _len_0 = 1
2953for _index_0 = 1, #my_numbers do
2954 local x = my_numbers[_index_0]
2955 if x % 2 == 1 then
2956 goto _continue_0
2957 end
2958 _accum_0[_len_0] = x
2959 _len_0 = _len_0 + 1
2960 ::_continue_0::
2961end
2962odds = _accum_0
2963local have_coins = false
2964if have_coins then
2965 print("有硬币")
2966else
2967 print("没有硬币")
2968end
2969local have_coins = false
2970if have_coins then
2971 print("有硬币")
2972else
2973 print("没有硬币")
2974end
2975local have_coins = false
2976print((function()
2977 if have_coins then
2978 return "有硬币"
2979 else
2980 return "没有硬币"
2981 end
2982end)())
2983local is_tall
2984is_tall = function(name)
2985 if name == "Rob" then
2986 return true
2987 else
2988 return false
2989 end
2990end
2991local message
2992if is_tall("Rob") then
2993 message = "我很高"
2994else
2995 message = "我不是很高"
2996end
2997print(message)
2998if not (os.date("%A") == "Monday") then
2999 print("今天不是星期一!")
3000end
3001if not (math.random() > 0.1) then
3002 print("你真幸运!")
3003end
3004local a = 5
3005if (1 == a or 3 == a or 5 == a or 7 == a) then
3006 print("检查离散值的相等性")
3007end
3008if (function()
3009 local _check_0 = list
3010 for _index_0 = 1, #_check_0 do
3011 if _check_0[_index_0] == a then
3012 return true
3013 end
3014 end
3015 return false
3016end)() then
3017 print("检查`a`是否在列表中")
3018end
3019if not (math.random() > 0.1) then
3020 print("你很幸运!")
3021end
1462local have_coins = false 3022local have_coins = false
1463if have_coins then 3023if have_coins then
1464 print("有硬币") 3024 print("有硬币")
@@ -1532,6 +3092,312 @@ end
1532while not reader:eof() do 3092while not reader:eof() do
1533 reader:parse_line() 3093 reader:parse_line()
1534end 3094end
3095if name == "Rob" then
3096 print("你好,世界")
3097end
3098local _list_0 = items
3099for _index_0 = 1, #_list_0 do
3100 local item = _list_0[_index_0]
3101 print("项目: ", item)
3102end
3103while game:isRunning() do
3104 game:update()
3105end
3106while not reader:eof() do
3107 reader:parse_line()
3108end
3109local name = "Dan"
3110if "Robert" == name then
3111 print("你是Robert")
3112elseif "Dan" == name or "Daniel" == name then
3113 print("你的名字是Dan")
3114else
3115 print("我不认识你,你的名字是" .. tostring(name))
3116end
3117local b = 1
3118local next_number
3119if 1 == b then
3120 next_number = 2
3121elseif 2 == b then
3122 next_number = 3
3123else
3124 next_number = error("数字数得太大了!")
3125end
3126local msg
3127local _exp_0 = math.random(1, 5)
3128if 1 == _exp_0 then
3129 msg = "你很幸运"
3130elseif 2 == _exp_0 then
3131 msg = "你差点很幸运"
3132else
3133 msg = "不太幸运"
3134end
3135do
3136 local _exp_0 = math.random(1, 5)
3137 if 1 == _exp_0 then
3138 print("你很幸运")
3139 else
3140 print("不太幸运")
3141 end
3142end
3143local _exp_0 = math.random(1, 5)
3144if 1 == _exp_0 then
3145 print("你很幸运")
3146else
3147 print("不太幸运")
3148end
3149local items = {
3150 {
3151 x = 100,
3152 y = 200
3153 },
3154 {
3155 width = 300,
3156 height = 400
3157 }
3158}
3159for _index_0 = 1, #items do
3160 local item = items[_index_0]
3161 local _type_0 = type(item)
3162 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3163 local _match_0 = false
3164 if _tab_0 then
3165 local x = item.x
3166 local y = item.y
3167 if x ~= nil and y ~= nil then
3168 _match_0 = true
3169 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
3170 end
3171 end
3172 if not _match_0 then
3173 if _tab_0 then
3174 local width = item.width
3175 local height = item.height
3176 if width ~= nil and height ~= nil then
3177 print("尺寸 " .. tostring(width) .. ", " .. tostring(height))
3178 end
3179 end
3180 end
3181end
3182local item = { }
3183local x, y = item.pos.x, item.pos.y
3184if x == nil then
3185 x = 50
3186end
3187if y == nil then
3188 y = 200
3189end
3190local _type_0 = type(item)
3191local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3192if _tab_0 then
3193 do
3194 local _obj_0 = item.pos
3195 local _type_1 = type(_obj_0)
3196 if "table" == _type_1 or "userdata" == _type_1 then
3197 x = _obj_0.x
3198 end
3199 end
3200 do
3201 local _obj_0 = item.pos
3202 local _type_1 = type(_obj_0)
3203 if "table" == _type_1 or "userdata" == _type_1 then
3204 y = _obj_0.y
3205 end
3206 end
3207 if x == nil then
3208 x = 50
3209 end
3210 if y == nil then
3211 y = 200
3212 end
3213 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
3214end
3215local _exp_0 = tb
3216local _type_0 = type(_exp_0)
3217local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3218local _match_0 = false
3219if _tab_0 then
3220 if 1 == _exp_0[1] and 2 == _exp_0[2] and 3 == _exp_0[3] then
3221 _match_0 = true
3222 print("1, 2, 3")
3223 end
3224end
3225if not _match_0 then
3226 local _match_1 = false
3227 if _tab_0 then
3228 local b = _exp_0[2]
3229 if 1 == _exp_0[1] and b ~= nil and 3 == _exp_0[3] then
3230 _match_1 = true
3231 print("1, " .. tostring(b) .. ", 3")
3232 end
3233 end
3234 if not _match_1 then
3235 if _tab_0 then
3236 local b = _exp_0[3]
3237 if b == nil then
3238 b = 3
3239 end
3240 if 1 == _exp_0[1] and 2 == _exp_0[2] then
3241 print("1, 2, " .. tostring(b))
3242 end
3243 end
3244 end
3245end
3246local _exp_0 = tb
3247local _type_0 = type(_exp_0)
3248local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3249local _match_0 = false
3250if _tab_0 then
3251 local result = _exp_0.result
3252 if true == _exp_0.success and result ~= nil then
3253 _match_0 = true
3254 print("成功", result)
3255 end
3256end
3257if not _match_0 then
3258 local _match_1 = false
3259 if _tab_0 then
3260 if false == _exp_0.success then
3261 _match_1 = true
3262 print("失败", result)
3263 end
3264 end
3265 if not _match_1 then
3266 print("无效值")
3267 end
3268end
3269local _exp_0 = tb
3270local _type_0 = type(_exp_0)
3271local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3272local _match_0 = false
3273if _tab_0 then
3274 local content
3275 do
3276 local _obj_0 = _exp_0.data
3277 local _type_1 = type(_obj_0)
3278 if "table" == _type_1 or "userdata" == _type_1 then
3279 content = _obj_0.content
3280 end
3281 end
3282 local _val_0
3283 do
3284 local _obj_0 = _exp_0.data
3285 if _obj_0 ~= nil then
3286 _val_0 = _obj_0.type
3287 end
3288 end
3289 if "success" == _val_0 and content ~= nil then
3290 _match_0 = true
3291 print("成功", content)
3292 end
3293end
3294if not _match_0 then
3295 local _match_1 = false
3296 if _tab_0 then
3297 local content
3298 do
3299 local _obj_0 = _exp_0.data
3300 local _type_1 = type(_obj_0)
3301 if "table" == _type_1 or "userdata" == _type_1 then
3302 content = _obj_0.content
3303 end
3304 end
3305 local _val_0
3306 do
3307 local _obj_0 = _exp_0.data
3308 if _obj_0 ~= nil then
3309 _val_0 = _obj_0.type
3310 end
3311 end
3312 if "error" == _val_0 and content ~= nil then
3313 _match_1 = true
3314 print("失败", content)
3315 end
3316 end
3317 if not _match_1 then
3318 print("无效值")
3319 end
3320end
3321local _exp_0 = tb
3322local _type_0 = type(_exp_0)
3323local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3324if _tab_0 then
3325 local fourth = _exp_0[4]
3326 local _val_0
3327 do
3328 local _obj_0 = _exp_0[1]
3329 if _obj_0 ~= nil then
3330 _val_0 = _obj_0.a
3331 end
3332 end
3333 local _val_1
3334 do
3335 local _obj_0 = _exp_0[1]
3336 if _obj_0 ~= nil then
3337 _val_1 = _obj_0.b
3338 end
3339 end
3340 local _val_2
3341 do
3342 local _obj_0 = _exp_0[2]
3343 if _obj_0 ~= nil then
3344 _val_2 = _obj_0.a
3345 end
3346 end
3347 local _val_3
3348 do
3349 local _obj_0 = _exp_0[2]
3350 if _obj_0 ~= nil then
3351 _val_3 = _obj_0.b
3352 end
3353 end
3354 local _val_4
3355 do
3356 local _obj_0 = _exp_0[3]
3357 if _obj_0 ~= nil then
3358 _val_4 = _obj_0.a
3359 end
3360 end
3361 local _val_5
3362 do
3363 local _obj_0 = _exp_0[3]
3364 if _obj_0 ~= nil then
3365 _val_5 = _obj_0.b
3366 end
3367 end
3368 if 1 == _val_0 and 2 == _val_1 and 3 == _val_2 and 4 == _val_3 and 5 == _val_4 and 6 == _val_5 and fourth ~= nil then
3369 print("匹配成功", fourth)
3370 end
3371end
3372local segments = {
3373 "admin",
3374 "users",
3375 "logs",
3376 "view"
3377}
3378local _type_0 = type(segments)
3379local _tab_0 = "table" == _type_0 or "userdata" == _type_0
3380if _tab_0 then
3381 local groups
3382 do
3383 local _accum_0 = { }
3384 local _len_0 = 1
3385 local _max_0 = #segments + -3 + 1
3386 for _index_0 = 1, _max_0 do
3387 local _item_0 = segments[_index_0]
3388 _accum_0[_len_0] = _item_0
3389 _len_0 = _len_0 + 1
3390 end
3391 groups = _accum_0
3392 end
3393 local resource = segments[#segments - 1]
3394 local action = segments[#segments]
3395 if resource ~= nil and action ~= nil then
3396 print("Group:", groups)
3397 print("Resource:", resource)
3398 print("Action:", action)
3399 end
3400end
1535local name = "Dan" 3401local name = "Dan"
1536if "Robert" == name then 3402if "Robert" == name then
1537 print("你是Robert") 3403 print("你是Robert")
@@ -2444,1940 +4310,6 @@ end
2444local y = Y() 4310local y = Y()
2445y:func() 4311y:func()
2446assert(y.__class.__parent ~= X) 4312assert(y.__class.__parent ~= X)
2447local _with_0 = Person()
2448_with_0.name = "Oswald"
2449_with_0:add_relative(my_dad)
2450_with_0:save()
2451print(_with_0.name)
2452local file
2453local _with_0 = File("favorite_foods.txt")
2454_with_0:set_encoding("utf8")
2455file = _with_0
2456local create_person
2457create_person = function(name, relatives)
2458 local _with_0 = Person()
2459 _with_0.name = name
2460 for _index_0 = 1, #relatives do
2461 local relative = relatives[_index_0]
2462 _with_0:add_relative(relative)
2463 end
2464 return _with_0
2465end
2466local me = create_person("Leaf", {
2467 dad,
2468 mother,
2469 sister
2470})
2471local str = "你好"
2472print("原始:", str)
2473print("大写:", str:upper())
2474local _with_0 = tb
2475_with_0[1] = 1
2476print(_with_0[2])
2477do
2478 local _with_1 = _with_0[abc]
2479 _with_1[3] = _with_1[2]:func()
2480 _with_1["key-name"] = value
2481end
2482_with_0[#_with_0 + 1] = "abc"
2483local _with_0 = obj
2484if _with_0 ~= nil then
2485 print(obj.name)
2486end
2487do
2488 local var = "hello"
2489 print(var)
2490end
2491print(var)
2492local counter
2493do
2494 local i = 0
2495 counter = function()
2496 i = i + 1
2497 return i
2498 end
2499end
2500print(counter())
2501print(counter())
2502local tbl = {
2503 key = (function()
2504 print("分配键值!")
2505 return 1234
2506 end)()
2507}
2508local my_object = {
2509 value = 1000,
2510 write = function(self)
2511 return print("值为:", self.value)
2512 end
2513}
2514local run_callback
2515run_callback = function(func)
2516 print("运行回调...")
2517 return func()
2518end
2519run_callback(my_object.write)
2520run_callback((function()
2521 local _base_0 = my_object
2522 local _fn_0 = _base_0.write
2523 return _fn_0 and function(...)
2524 return _fn_0(_base_0, ...)
2525 end
2526end)())
2527local i = 100
2528local my_func
2529my_func = function()
2530 i = 10
2531 while i > 0 do
2532 print(i)
2533 i = i - 1
2534 end
2535end
2536my_func()
2537print(i)
2538local i = 100
2539local my_func
2540my_func = function()
2541 local i = "hello"
2542end
2543my_func()
2544print(i)
2545local tmp = 1213
2546local i, k = 100, 50
2547local my_func
2548my_func = function(add)
2549 local tmp = tmp + add
2550 i = i + tmp
2551 k = k + tmp
2552end
2553my_func(22)
2554print(i, k)
2555local _module_0 = { }
2556local p, to_lua
2557do
2558 local _obj_0 = require("yue")
2559 p, to_lua = _obj_0.p, _obj_0.to_lua
2560end
2561local inventory = {
2562 equipment = {
2563 "sword",
2564 "shield"
2565 },
2566 items = {
2567 {
2568 name = "potion",
2569 count = 10
2570 },
2571 {
2572 name = "bread",
2573 count = 3
2574 }
2575 }
2576}
2577local map
2578map = function(arr, action)
2579 local _accum_0 = { }
2580 local _len_0 = 1
2581 for _index_0 = 1, #arr do
2582 local item = arr[_index_0]
2583 _accum_0[_len_0] = action(item)
2584 _len_0 = _len_0 + 1
2585 end
2586 return _accum_0
2587end
2588local filter
2589filter = function(arr, cond)
2590 local _accum_0 = { }
2591 local _len_0 = 1
2592 for _index_0 = 1, #arr do
2593 local item = arr[_index_0]
2594 if cond(item) then
2595 _accum_0[_len_0] = item
2596 _len_0 = _len_0 + 1
2597 end
2598 end
2599 return _accum_0
2600end
2601local reduce
2602reduce = function(arr, init, action)
2603 for _index_0 = 1, #arr do
2604 local item = arr[_index_0]
2605 init = action(init, item)
2606 end
2607 return init
2608end
2609print(reduce(filter(map({
2610 1,
2611 2,
2612 3
2613}, function(x)
2614 return x * 2
2615end), function(x)
2616 return x > 4
2617end), 0, function(a, b)
2618 return a + b
2619end))
2620local apple = setmetatable({
2621 size = 15,
2622}, {
2623 __index = {
2624 color = 0x00ffff
2625 }
2626})
2627if (getmetatable(apple) ~= nil) then
2628 p(apple.size, apple.color, getmetatable(apple).__index)
2629end
2630local _u1f31b = "月之脚本"
2631_module_0["🌛"] = _u1f31b
2632return _module_0
2633local area = 6.2831853071796 * 5
2634print('你好 世界')
2635do
2636 assert(item ~= nil)
2637end
2638local value = item
2639if (f1() and f2() and f3()) then
2640 print("OK")
2641end
2642do
2643 local funcA
2644 funcA = function() end
2645end
2646local funcA
2647funcA = function()
2648 return "无法访问宏生成月之脚本里定义的变量"
2649end
2650do
2651local function funcB() end
2652end
2653local funcB
2654funcB = function()
2655 return "无法访问宏生成 Lua 代码里定义的变量"
2656end
2657do
2658-- 插入原始Lua代码
2659if cond then
2660 print("输出")
2661end
2662end
2663print("yuescript")
2664print(2)
2665print("有效的枚举类型:", "Static")
2666do
2667 print(123, "hello")
2668end
2669do
2670 print(123, "hello")
2671end
2672if tb ~= nil then
2673 tb:func()
2674end
2675if tb ~= nil then
2676 tb:func()
2677end
2678print(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5)
2679local a = 5
2680print(1 <= a and a <= 10)
2681local v
2682v = function(x)
2683 print(x)
2684 return x
2685end
2686print((function()
2687 local _cond_0 = v(2)
2688 if not (v(1) < _cond_0) then
2689 return false
2690 else
2691 return _cond_0 <= v(3)
2692 end
2693end)())
2694print((function()
2695 local _cond_0 = v(2)
2696 if not (v(1) > _cond_0) then
2697 return false
2698 else
2699 return _cond_0 <= v(3)
2700 end
2701end)())
2702local tab = { }
2703tab[#tab + 1] = "Value"
2704local tbA = {
2705 1,
2706 2,
2707 3
2708}
2709local tbB = {
2710 4,
2711 5,
2712 6
2713}
2714local _len_0 = #tbA + 1
2715for _index_0 = 1, #tbB do
2716 local _elm_0 = tbB[_index_0]
2717 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
2718end
2719local parts = {
2720 "shoulders",
2721 "knees"
2722}
2723local lyrics
2724do
2725 local _tab_0 = {
2726 "head"
2727 }
2728 local _idx_0 = 1
2729 for _key_0, _value_0 in pairs(parts) do
2730 if _idx_0 == _key_0 then
2731 _tab_0[#_tab_0 + 1] = _value_0
2732 _idx_0 = _idx_0 + 1
2733 else
2734 _tab_0[_key_0] = _value_0
2735 end
2736 end
2737 _tab_0[#_tab_0 + 1] = "and"
2738 _tab_0[#_tab_0 + 1] = "toes"
2739 lyrics = _tab_0
2740end
2741local copy
2742do
2743 local _tab_0 = { }
2744 local _idx_0 = 1
2745 for _key_0, _value_0 in pairs(other) do
2746 if _idx_0 == _key_0 then
2747 _tab_0[#_tab_0 + 1] = _value_0
2748 _idx_0 = _idx_0 + 1
2749 else
2750 _tab_0[_key_0] = _value_0
2751 end
2752 end
2753 copy = _tab_0
2754end
2755local a = {
2756 1,
2757 2,
2758 3,
2759 x = 1
2760}
2761local b = {
2762 4,
2763 5,
2764 y = 1
2765}
2766local merge
2767local _tab_0 = { }
2768local _idx_0 = 1
2769for _key_0, _value_0 in pairs(a) do
2770 if _idx_0 == _key_0 then
2771 _tab_0[#_tab_0 + 1] = _value_0
2772 _idx_0 = _idx_0 + 1
2773 else
2774 _tab_0[_key_0] = _value_0
2775 end
2776end
2777local _idx_1 = 1
2778for _key_0, _value_0 in pairs(b) do
2779 if _idx_1 == _key_0 then
2780 _tab_0[#_tab_0 + 1] = _value_0
2781 _idx_1 = _idx_1 + 1
2782 else
2783 _tab_0[_key_0] = _value_0
2784 end
2785end
2786merge = _tab_0
2787local last
2788do
2789 local _item_0 = data.items
2790 last = _item_0[#_item_0]
2791end
2792local second_last
2793do
2794 local _item_0 = data.items
2795 second_last = _item_0[#_item_0 - 1]
2796end
2797local _obj_0 = data.items
2798_obj_0[#_obj_0] = 1
2799local mt = { }
2800local add
2801add = function(self, right)
2802 return setmetatable({
2803 value = self.value + right.value
2804 }, mt)
2805end
2806mt.__add = add
2807local a = setmetatable({
2808 value = 1
2809}, mt)
2810local b = setmetatable({
2811 value = 2
2812}, {
2813 __add = add
2814})
2815local c = setmetatable({
2816 value = 3
2817}, {
2818 __add = mt.__add
2819})
2820local d = a + b + c
2821print(d.value)
2822local _ <close> = setmetatable({ }, {
2823 __close = function()
2824 return print("超出范围")
2825 end
2826})
2827local tb = setmetatable({ }, {
2828 ["value"] = 123
2829})
2830getmetatable(tb).__index = getmetatable(tb)
2831print(tb.value)
2832setmetatable(tb, {
2833 __index = {
2834 item = "hello"
2835 }
2836})
2837print(tb.item)
2838local item, new, close, getter
2839do
2840 local _obj_0 = tb
2841 item, new = _obj_0[1], _obj_0.new
2842 do
2843 local _obj_1 = getmetatable(_obj_0)
2844 close, getter = _obj_1.__close, _obj_1.__index
2845 end
2846end
2847print(item, new, close, getter)
2848do
2849 local _obj_0 = func
2850 if _obj_0 ~= nil then
2851 _obj_0()
2852 end
2853end
2854print((function()
2855 local _obj_0 = abc
2856 if _obj_0 ~= nil then
2857 local _obj_1 = _obj_0["你好 世界"]
2858 if _obj_1 ~= nil then
2859 return _obj_1.xyz
2860 end
2861 return nil
2862 end
2863 return nil
2864end)())
2865local x
2866do
2867 local _obj_0 = tab
2868 if _obj_0 ~= nil then
2869 x = _obj_0.value
2870 end
2871end
2872local len = (function()
2873 local _obj_0 = utf8
2874 if _obj_0 ~= nil then
2875 return _obj_0.len
2876 end
2877 return nil
2878end)() or (function()
2879 local _obj_0 = string
2880 if _obj_0 ~= nil then
2881 return _obj_0.len
2882 end
2883 return nil
2884end)() or function(o)
2885 return #o
2886end
2887if print and (x ~= nil) then
2888 print(x)
2889end
2890local _with_0 = io.open("test.txt", "w")
2891if _with_0 ~= nil then
2892 _with_0:write("你好")
2893 _with_0:close()
2894end
2895print("你好")
2896print(1, 2)
2897print(1, 2, 3)
2898print(render(emit(parse(extract(readFile("example.txt"), language, { }), language))))
2899local a, b, c, d
2900if b ~= nil then
2901 a = b
2902else
2903 if c ~= nil then
2904 a = c
2905 else
2906 a = d
2907 end
2908end
2909func((function()
2910 if a ~= nil then
2911 return a
2912 else
2913 return { }
2914 end
2915end)())
2916if a == nil then
2917 a = false
2918end
2919local list = {
2920 1,
2921 2,
2922 3
2923}
2924func({
2925 1,
2926 2,
2927 3
2928})
2929local f
2930f = function()
2931 return {
2932 1,
2933 2,
2934 3
2935 }
2936end
2937local tb = {
2938 name = "abc",
2939 values = {
2940 "a",
2941 "b",
2942 "c"
2943 },
2944 objects = {
2945 {
2946 name = "a",
2947 value = 1,
2948 func = function(self)
2949 return self.value + 1
2950 end,
2951 tb = {
2952 fieldA = 1
2953 }
2954 },
2955 {
2956 name = "b",
2957 value = 2,
2958 func = function(self)
2959 return self.value + 2
2960 end,
2961 tb = { }
2962 }
2963 }
2964}
2965do
2966 local insert, concat = table.insert, table.concat
2967 local C, Ct, Cmt
2968 do
2969 local _obj_0 = require("lpeg")
2970 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
2971 end
2972 local x, y, z
2973 do
2974 local _obj_0 = require('mymodule')
2975 x, y, z = _obj_0.x, _obj_0.y, _obj_0.z
2976 end
2977 local a, b, c
2978 local _obj_0 = require('module')
2979 a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
2980end
2981do
2982 local module = require('module')
2983 local module_x = require('module_x')
2984 local d_a_s_h_e_s = require("d-a-s-h-e-s")
2985 local part = require("module.part")
2986end
2987do
2988 local PlayerModule = require("player")
2989 local C, Ct, Cmt
2990 do
2991 local _obj_0 = require("lpeg")
2992 C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt
2993 end
2994 local one, two, ch
2995 local _obj_0 = require("export")
2996 one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1]
2997end
2998do
2999 local tostring <const> = tostring
3000 local concat <const> = table.concat
3001 print(concat({
3002 "a",
3003 tostring(1)
3004 }))
3005end
3006do
3007 local print <const> = print
3008 local math <const> = math
3009 print("hello")
3010 math.random(3)
3011end
3012do
3013 local print <const> = print
3014 print(FLAG)
3015 FLAG = 123
3016end
3017local _module_0 = { }
3018local a, b, c = 1, 2, 3
3019_module_0["a"], _module_0["b"], _module_0["c"] = a, b, c
3020local cool = "cat"
3021_module_0["cool"] = cool
3022local What
3023if this then
3024 What = "abc"
3025else
3026 What = "def"
3027end
3028_module_0["What"] = What
3029local y
3030y = function()
3031 local hallo = 3434
3032end
3033_module_0["y"] = y
3034local Something
3035local _class_0
3036local _base_0 = {
3037 umm = "cool"
3038}
3039if _base_0.__index == nil then
3040 _base_0.__index = _base_0
3041end
3042_class_0 = setmetatable({
3043 __init = function() end,
3044 __base = _base_0,
3045 __name = "Something"
3046}, {
3047 __index = _base_0,
3048 __call = function(cls, ...)
3049 local _self_0 = setmetatable({ }, _base_0)
3050 cls.__init(_self_0, ...)
3051 return _self_0
3052 end
3053})
3054_base_0.__class = _class_0
3055Something = _class_0
3056_module_0["Something"] = Something
3057return _module_0
3058local _module_0 = { }
3059local loadstring, tolua
3060do
3061 local _obj_0 = yue
3062 loadstring, tolua = _obj_0.loadstring, _obj_0.to_lua
3063end
3064_module_0["loadstring"], _module_0["tolua"] = loadstring, tolua
3065local fieldA = tb.itemA.fieldA
3066if fieldA == nil then
3067 fieldA = '默认值'
3068end
3069_module_0["fieldA"] = fieldA
3070return _module_0
3071local _module_0 = setmetatable({ }, { })
3072_module_0.itemA = tb
3073getmetatable(_module_0).__index = items
3074_module_0["a-b-c"] = 123
3075return _module_0
3076local _module_0 = { }
3077local d, e, f = 3, 2, 1
3078_module_0[#_module_0 + 1] = d
3079_module_0[#_module_0 + 1] = e
3080_module_0[#_module_0 + 1] = f
3081if this then
3082 _module_0[#_module_0 + 1] = 123
3083else
3084 _module_0[#_module_0 + 1] = 456
3085end
3086local _with_0 = tmp
3087local j = 2000
3088_module_0[#_module_0 + 1] = _with_0
3089return _module_0
3090local _module_0 = nil
3091_module_0 = function()
3092 print("你好")
3093 return 123
3094end
3095return _module_0
3096local hello = "world"
3097local a, b, c = 1, 2, 3
3098hello = 123
3099local x = 1
3100x = x + 1
3101x = x - 1
3102x = x * 10
3103x = x / 10
3104x = x % 10
3105local s = s .. "world"
3106local arg = arg or "默认值"
3107local a = 0
3108local b = 0
3109local c = 0
3110local d = 0
3111local e = 0
3112local x = f()
3113local y = x
3114local z = x
3115do
3116 local a = 1
3117 local x, y, z
3118 print("预先声明后续所有变量为局部变量")
3119 x = function()
3120 return 1 + y + z
3121 end
3122 y, z = 2, 3
3123 instance = Item:new()
3124end
3125do
3126 local X = 1
3127 local B
3128 print("只预先声明后续大写的变量为局部变量")
3129 local a = 1
3130 B = 2
3131end
3132do
3133 a = 1
3134 print("预先声明所有变量为全局变量")
3135 x = function()
3136 return 1 + y + z
3137 end
3138 y, z = 2, 3
3139end
3140do
3141 x = 1
3142 print("只预先声明大写的变量为全局变量")
3143 local a = 1
3144 B = 2
3145 local Temp = "一个局部值"
3146end
3147local thing = {
3148 1,
3149 2
3150}
3151local a, b = thing[1], thing[2]
3152print(a, b)
3153local obj = {
3154 hello = "world",
3155 day = "tuesday",
3156 length = 20
3157}
3158local hello, the_day = obj.hello, obj.day
3159print(hello, the_day)
3160local day = obj.day
3161local obj2 = {
3162 numbers = {
3163 1,
3164 2,
3165 3,
3166 4
3167 },
3168 properties = {
3169 color = "green",
3170 height = 13.5
3171 }
3172}
3173local first, second = obj2.numbers[1], obj2.numbers[2]
3174print(first, second, color)
3175local first, second, color
3176local _obj_0 = obj2
3177first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color
3178local concat, insert
3179local _obj_0 = table
3180concat, insert = _obj_0.concat, _obj_0.insert
3181local mix, max, rand
3182local _obj_0 = math
3183mix, max, rand = _obj_0.mix, _obj_0.max, _obj_0.random
3184local name, job
3185local _obj_0 = person
3186name, job = _obj_0.name, _obj_0.job
3187if name == nil then
3188 name = "nameless"
3189end
3190if job == nil then
3191 job = "jobless"
3192end
3193local two, four
3194local _obj_0 = items
3195two, four = _obj_0[2], _obj_0[4]
3196local orders = {
3197 "first",
3198 "second",
3199 "third",
3200 "fourth",
3201 "last"
3202}
3203local first, bulk, last = orders[1], (function()
3204 local _accum_0 = { }
3205 local _len_0 = 1
3206 local _max_0 = #orders + -2 + 1
3207 for _index_0 = 2, _max_0 do
3208 local _item_0 = orders[_index_0]
3209 _accum_0[_len_0] = _item_0
3210 _len_0 = _len_0 + 1
3211 end
3212 return _accum_0
3213end)(), orders[#orders]
3214print(first)
3215print(bulk)
3216print(last)
3217local first, rest
3218do
3219 local _obj_0 = orders
3220 first, rest = _obj_0[1], (function()
3221 local _accum_0 = { }
3222 local _len_0 = 1
3223 local _max_0 = #_obj_0
3224 for _index_0 = 2, _max_0 do
3225 local _item_0 = _obj_0[_index_0]
3226 _accum_0[_len_0] = _item_0
3227 _len_0 = _len_0 + 1
3228 end
3229 return _accum_0
3230 end)()
3231end
3232local start, last
3233do
3234 local _obj_0 = orders
3235 start, last = (function()
3236 local _accum_0 = { }
3237 local _len_0 = 1
3238 local _max_0 = #_obj_0 + -2 + 1
3239 for _index_0 = 1, _max_0 do
3240 local _item_0 = _obj_0[_index_0]
3241 _accum_0[_len_0] = _item_0
3242 _len_0 = _len_0 + 1
3243 end
3244 return _accum_0
3245 end)(), _obj_0[#_obj_0]
3246end
3247local _obj_0 = orders
3248first, last = _obj_0[1], _obj_0[#_obj_0]
3249local tuples = {
3250 {
3251 "hello",
3252 "world"
3253 },
3254 {
3255 "egg",
3256 "head"
3257 }
3258}
3259for _index_0 = 1, #tuples do
3260 local _des_0 = tuples[_index_0]
3261 local left, right = _des_0[1], _des_0[2]
3262 print(left, right)
3263end
3264local user = database.find_user("moon")
3265if user then
3266 print(user.name)
3267end
3268local hello = os.getenv("hello")
3269if hello then
3270 print("你有 hello", hello)
3271else
3272 local world = os.getenv("world")
3273 if world then
3274 print("你有 world", world)
3275 else
3276 print("什么都没有 :(")
3277 end
3278end
3279do
3280 local success, result = pcall(function()
3281 return "无报错地获取结果"
3282 end)
3283 if success then
3284 print(result)
3285 end
3286end
3287print("好的")
3288repeat
3289 local byte = stream:read_one()
3290 if byte then
3291 print(byte)
3292 else
3293 break
3294 end
3295until false
3296local list = {
3297 1,
3298 2,
3299 3,
3300 4,
3301 5
3302}
3303local fn
3304fn = function(ok)
3305 return ok, table.unpack(list)
3306end
3307(function(_arg_0, ...)
3308 local ok = _arg_0
3309 local count = select('#', ...)
3310 local first = select(1, ...)
3311 return print(ok, count, first)
3312end)(fn(true))
3313local a = 1
3314local b = 2
3315print(a + b)
3316Rx.Observable.fromRange(1, 8):filter(function(x)
3317 return x % 2 == 0
3318end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
3319 return value .. '!'
3320end):subscribe(print)
3321local str = strA .. strB .. strC
3322func(3000, "192.168.1.1")
3323xpcall(function()
3324 return func(1, 2, 3)
3325end, function(err)
3326 return print(yue.traceback(err))
3327end)
3328local success, result = xpcall(function()
3329 return func(1, 2, 3)
3330end, function(err)
3331 return yue.traceback(err)
3332end)
3333xpcall(function()
3334 return func(1, 2, 3)
3335end, function(err)
3336 return print(yue.traceback(err))
3337end)
3338success, result = pcall(function()
3339 return func(1, 2, 3)
3340end)
3341pcall(function()
3342 print("尝试中")
3343 return func(1, 2, 3)
3344end)
3345success, result = xpcall(function()
3346 return func(1, 2, 3)
3347end, function(err)
3348 return print(yue.traceback(err))
3349end)
3350if success then
3351 print(result)
3352end
3353local a, b, c
3354do
3355 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
3356 return func()
3357 end)
3358 if _ok_0 then
3359 a, b, c = _ret_0, _ret_1, _ret_2
3360 end
3361end
3362do
3363 local _exp_0 = ((function()
3364 return (function(_arg_0, ...)
3365 local _ok_0 = _arg_0
3366 if _ok_0 then
3367 return ...
3368 end
3369 end)(pcall(function()
3370 return func()
3371 end))
3372 end)())
3373 if _exp_0 ~= nil then
3374 a = _exp_0
3375 else
3376 a = "default"
3377 end
3378end
3379f((function()
3380 return (function(_arg_0, ...)
3381 local _ok_0 = _arg_0
3382 if _ok_0 then
3383 return ...
3384 end
3385 end)(pcall(function()
3386 return func()
3387 end))
3388end)())
3389f((function()
3390 return (function(_arg_0, ...)
3391 local _ok_0 = _arg_0
3392 if _ok_0 then
3393 return ...
3394 end
3395 end)(xpcall(function()
3396 print(123)
3397 return func()
3398 end, function(e)
3399 print(e)
3400 return e
3401 end))
3402end)())
3403local a <const> = 123
3404local _ <close> = setmetatable({ }, {
3405 __close = function()
3406 return print("超出范围。")
3407 end
3408})
3409local a, b, c, d
3410local _obj_0 = tb
3411a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2]
3412Constant = 123
3413local some_string = "这是一个字符串\n 并包括一个换行。"
3414print("我有" .. tostring(math.random() * 100) .. "%的把握。")
3415local integer = 1000000
3416local hex = 0xEFBBBF
3417local binary = 19
3418local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
3419local fn
3420fn = function()
3421 local str = "foo:\n bar: baz"
3422 return str
3423end
3424local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
3425local my_function
3426my_function = function() end
3427my_function()
3428local func_a
3429func_a = function()
3430 return print("你好,世界")
3431end
3432local func_b
3433func_b = function()
3434 local value = 100
3435 return print("这个值是:", value)
3436end
3437func_a()
3438func_b()
3439local sum
3440sum = function(x, y)
3441 return print("数字的和", x + y)
3442end
3443sum(10, 20)
3444print(sum(10, 20))
3445a(b(c("a", "b", "c")))
3446print("x:", sum(10, 20), "y:", sum(30, 40))
3447local sum
3448sum = function(x, y)
3449 return x + y
3450end
3451print("数字的和是", sum(10, 20))
3452local sum
3453sum = function(x, y)
3454 return x + y
3455end
3456local mystery
3457mystery = function(x, y)
3458 return x + y, x - y
3459end
3460local a, b = mystery(10, 20)
3461local func
3462func = function(self, num)
3463 return self.value + num
3464end
3465local my_function
3466my_function = function(name, height)
3467 if name == nil then
3468 name = "某物"
3469 end
3470 if height == nil then
3471 height = 100
3472 end
3473 print("你好,我是", name)
3474 return print("我的高度是", height)
3475end
3476local some_args
3477some_args = function(x, y)
3478 if x == nil then
3479 x = 100
3480 end
3481 if y == nil then
3482 y = x + 1000
3483 end
3484 return print(x + y)
3485end
3486my_func(5, 4, 3, 8, 9, 10)
3487cool_func(1, 2, 3, 4, 5, 6, 7, 8)
3488my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4)
3489local x = {
3490 1,
3491 2,
3492 3,
3493 4,
3494 a_func(4, 5, 5, 6),
3495 8,
3496 9,
3497 10
3498}
3499local y = {
3500 my_func(1, 2, 3, 4, 5),
3501 5,
3502 6,
3503 7
3504}
3505if func(1, 2, 3, "你好", "世界") then
3506 print("你好")
3507 print("我在if内部")
3508end
3509if func(1, 2, 3, "你好", "世界") then
3510 print("你好")
3511 print("我在if内部")
3512end
3513local f1
3514f1 = function(_arg_0)
3515 local a, b, c
3516 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
3517 return print(a, b, c)
3518end
3519f1({
3520 a = 1,
3521 b = "2",
3522 c = { }
3523})
3524local f2
3525f2 = function(_arg_0, c)
3526 local a1, b
3527 a1, b = _arg_0.a, _arg_0.b
3528 if a1 == nil then
3529 a1 = 123
3530 end
3531 if b == nil then
3532 b = 'abc'
3533 end
3534 if c == nil then
3535 c = { }
3536 end
3537 return print(a1, b, c)
3538end
3539local arg1 = {
3540 a = 0
3541}
3542f2(arg1, arg2)
3543local findFirstEven
3544findFirstEven = function(list)
3545 for _index_0 = 1, #list do
3546 local item = list[_index_0]
3547 if type(item) == "table" then
3548 for _index_1 = 1, #item do
3549 local sub = item[_index_1]
3550 if sub % 2 == 0 then
3551 return sub
3552 end
3553 end
3554 end
3555 end
3556 return nil
3557end
3558local findFirstEven
3559findFirstEven = function(list)
3560 for _index_0 = 1, #list do
3561 local item = list[_index_0]
3562 if type(item) == "table" then
3563 for _index_1 = 1, #item do
3564 local sub = item[_index_1]
3565 if sub % 2 == 0 then
3566 return sub
3567 end
3568 end
3569 end
3570 end
3571 return nil
3572end
3573local f
3574f = function(...)
3575 local t = {
3576 n = select("#", ...),
3577 ...
3578 }
3579 print("参数个数:", t.n)
3580 print("表长度:", #t)
3581 for i = 1, t.n do
3582 print(t[i])
3583 end
3584end
3585f(1, 2, 3)
3586f("a", "b", "c", "d")
3587f()
3588local process
3589process = function(...)
3590 local args = {
3591 n = select("#", ...),
3592 ...
3593 }
3594 local sum = 0
3595 for i = 1, args.n do
3596 if args[i] ~= nil and type(args[i]) == "number" then
3597 sum = sum + args[i]
3598 end
3599 end
3600 return sum
3601end
3602process(1, nil, 3, nil, 5)
3603f(function()
3604 return print("hello")
3605end)
3606f(function(self)
3607 return print(self.value)
3608end)
3609map(function(x)
3610 return x * 2
3611end, {
3612 1,
3613 2,
3614 3
3615})
3616local result, msg
3617do
3618 result, msg = readAsync("文件名.txt", function(data)
3619 print(data)
3620 return processAsync(data, function(info)
3621 return check(info)
3622 end)
3623 end)
3624end
3625print(result, msg)
3626local some_values = {
3627 1,
3628 2,
3629 3,
3630 4
3631}
3632local some_values = {
3633 name = "Bill",
3634 age = 200,
3635 ["favorite food"] = "rice"
3636}
3637local profile = {
3638 height = "4英尺",
3639 shoe_size = 13,
3640 favorite_foods = {
3641 "冰淇淋",
3642 "甜甜圈"
3643 }
3644}
3645local values = {
3646 1,
3647 2,
3648 3,
3649 4,
3650 5,
3651 6,
3652 7,
3653 8,
3654 name = "超人",
3655 occupation = "打击犯罪"
3656}
3657my_function({
3658 dance = "探戈",
3659 partner = "无"
3660})
3661local y = {
3662 type = "狗",
3663 legs = 4,
3664 tails = 1
3665}
3666local tbl = {
3667 ["do"] = "某事",
3668 ["end"] = "饥饿"
3669}
3670local hair = "金色"
3671local height = 200
3672local person = {
3673 hair = hair,
3674 height = height,
3675 shoe_size = 40
3676}
3677print_table({
3678 hair = hair,
3679 height = height
3680})
3681local t = {
3682 [1 + 2] = "你好",
3683 ["你好 世界"] = true
3684}
3685local some_values = {
3686 1,
3687 2,
3688 3,
3689 4
3690}
3691local list_with_one_element = {
3692 1
3693}
3694local items = {
3695 1,
3696 2,
3697 3,
3698 4
3699}
3700local doubled
3701local _accum_0 = { }
3702local _len_0 = 1
3703for i, item in ipairs(items) do
3704 _accum_0[_len_0] = item * 2
3705 _len_0 = _len_0 + 1
3706end
3707doubled = _accum_0
3708local slice
3709local _accum_0 = { }
3710local _len_0 = 1
3711for i, item in ipairs(items) do
3712 if i > 1 and i < 3 then
3713 _accum_0[_len_0] = item
3714 _len_0 = _len_0 + 1
3715 end
3716end
3717slice = _accum_0
3718local doubled
3719local _accum_0 = { }
3720local _len_0 = 1
3721local _list_0 = items
3722for _index_0 = 1, #_list_0 do
3723 local item = _list_0[_index_0]
3724 _accum_0[_len_0] = item * 2
3725 _len_0 = _len_0 + 1
3726end
3727doubled = _accum_0
3728local data = {
3729 a = {
3730 1,
3731 2,
3732 3
3733 },
3734 b = {
3735 4,
3736 5,
3737 6
3738 }
3739}
3740local flat
3741local _accum_0 = { }
3742for k, v in pairs(data) do
3743 local _len_0 = #_accum_0 + 1
3744 for _index_0 = 1, #v do
3745 local _elm_0 = v[_index_0]
3746 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
3747 end
3748end
3749flat = _accum_0
3750local x_coords = {
3751 4,
3752 5,
3753 6,
3754 7
3755}
3756local y_coords = {
3757 9,
3758 2,
3759 3
3760}
3761local points
3762local _accum_0 = { }
3763local _len_0 = 1
3764for _index_0 = 1, #x_coords do
3765 local x = x_coords[_index_0]
3766 for _index_1 = 1, #y_coords do
3767 local y = y_coords[_index_1]
3768 _accum_0[_len_0] = {
3769 x,
3770 y
3771 }
3772 _len_0 = _len_0 + 1
3773 end
3774end
3775points = _accum_0
3776local evens
3777local _accum_0 = { }
3778local _len_0 = 1
3779for i = 1, 100 do
3780 if i % 2 == 0 then
3781 _accum_0[_len_0] = i
3782 _len_0 = _len_0 + 1
3783 end
3784end
3785evens = _accum_0
3786local thing = {
3787 color = "red",
3788 name = "fast",
3789 width = 123
3790}
3791local thing_copy
3792local _tbl_0 = { }
3793for k, v in pairs(thing) do
3794 _tbl_0[k] = v
3795end
3796thing_copy = _tbl_0
3797local no_color
3798local _tbl_0 = { }
3799for k, v in pairs(thing) do
3800 if k ~= "color" then
3801 _tbl_0[k] = v
3802 end
3803end
3804no_color = _tbl_0
3805local numbers = {
3806 1,
3807 2,
3808 3,
3809 4
3810}
3811local sqrts
3812local _tbl_0 = { }
3813for _index_0 = 1, #numbers do
3814 local i = numbers[_index_0]
3815 _tbl_0[i] = math.sqrt(i)
3816end
3817sqrts = _tbl_0
3818local tuples = {
3819 {
3820 "hello",
3821 "world"
3822 },
3823 {
3824 "foo",
3825 "bar"
3826 }
3827}
3828local tbl
3829local _tbl_0 = { }
3830for _index_0 = 1, #tuples do
3831 local tuple = tuples[_index_0]
3832 local _key_0, _val_0 = unpack(tuple)
3833 _tbl_0[_key_0] = _val_0
3834end
3835tbl = _tbl_0
3836local slice
3837local _accum_0 = { }
3838local _len_0 = 1
3839local _list_0 = items
3840for _index_0 = 1, 5 do
3841 local item = _list_0[_index_0]
3842 _accum_0[_len_0] = item
3843 _len_0 = _len_0 + 1
3844end
3845slice = _accum_0
3846local slice
3847local _accum_0 = { }
3848local _len_0 = 1
3849local _list_0 = items
3850local _max_0 = #_list_0
3851for _index_0 = 2, _max_0 do
3852 local item = _list_0[_index_0]
3853 _accum_0[_len_0] = item
3854 _len_0 = _len_0 + 1
3855end
3856slice = _accum_0
3857local slice
3858local _accum_0 = { }
3859local _len_0 = 1
3860local _list_0 = items
3861local _max_0 = #_list_0
3862for _index_0 = 1, _max_0, 2 do
3863 local item = _list_0[_index_0]
3864 _accum_0[_len_0] = item
3865 _len_0 = _len_0 + 1
3866end
3867slice = _accum_0
3868local slice
3869local _accum_0 = { }
3870local _len_0 = 1
3871local _list_0 = items
3872local _min_0 = #_list_0 + -4 + 1
3873local _max_0 = #_list_0 + -1 + 1
3874for _index_0 = _min_0, _max_0 do
3875 local item = _list_0[_index_0]
3876 _accum_0[_len_0] = item
3877 _len_0 = _len_0 + 1
3878end
3879slice = _accum_0
3880local reverse_slice
3881local _accum_0 = { }
3882local _len_0 = 1
3883local _list_0 = items
3884local _min_0 = #_list_0 + -1 + 1
3885for _index_0 = _min_0, 1, -1 do
3886 local item = _list_0[_index_0]
3887 _accum_0[_len_0] = item
3888 _len_0 = _len_0 + 1
3889end
3890reverse_slice = _accum_0
3891local sub_list
3892local _accum_0 = { }
3893local _len_0 = 1
3894local _list_0 = items
3895for _index_0 = 2, 4 do
3896 local _item_0 = _list_0[_index_0]
3897 _accum_0[_len_0] = _item_0
3898 _len_0 = _len_0 + 1
3899end
3900sub_list = _accum_0
3901for i = 10, 20 do
3902 print(i)
3903end
3904for k = 1, 15, 2 do
3905 print(k)
3906end
3907for key, value in pairs(object) do
3908 print(key, value)
3909end
3910local _list_0 = items
3911for _index_0 = 2, 4 do
3912 local item = _list_0[_index_0]
3913 print(item)
3914end
3915local _list_0 = items
3916for _index_0 = 1, #_list_0 do
3917 local item = _list_0[_index_0]
3918 print(item)
3919end
3920for j = 1, 10, 3 do
3921 print(j)
3922end
3923local doubled_evens
3924local _accum_0 = { }
3925local _len_0 = 1
3926for i = 1, 20 do
3927 if i % 2 == 0 then
3928 _accum_0[_len_0] = i * 2
3929 _len_0 = _len_0 + 1
3930 else
3931 _accum_0[_len_0] = i
3932 _len_0 = _len_0 + 1
3933 end
3934end
3935doubled_evens = _accum_0
3936local first_large
3937local _accum_0
3938local _list_0 = numbers
3939for _index_0 = 1, #_list_0 do
3940 local n = _list_0[_index_0]
3941 if n > 10 then
3942 _accum_0 = n
3943 break
3944 end
3945end
3946first_large = _accum_0
3947local func_a
3948func_a = function()
3949 for i = 1, 10 do
3950 print(i)
3951 end
3952end
3953local func_b
3954func_b = function()
3955 local _accum_0 = { }
3956 local _len_0 = 1
3957 for i = 1, 10 do
3958 _accum_0[_len_0] = i
3959 _len_0 = _len_0 + 1
3960 end
3961 return _accum_0
3962end
3963print(func_a())
3964print(func_b())
3965local i = 10
3966repeat
3967 print(i)
3968 i = i - 1
3969until i == 0
3970local i = 10
3971while i > 0 do
3972 print(i)
3973 i = i - 1
3974end
3975while running == true do
3976 my_function()
3977end
3978local i = 10
3979while not (i == 0) do
3980 print(i)
3981 i = i - 1
3982end
3983while not (running == false) do
3984 my_function()
3985end
3986local i = 0
3987while i < 10 do
3988 i = i + 1
3989 if i % 2 == 0 then
3990 goto _continue_0
3991 end
3992 print(i)
3993 ::_continue_0::
3994end
3995local my_numbers = {
3996 1,
3997 2,
3998 3,
3999 4,
4000 5,
4001 6
4002}
4003local odds
4004local _accum_0 = { }
4005local _len_0 = 1
4006for _index_0 = 1, #my_numbers do
4007 local x = my_numbers[_index_0]
4008 if x % 2 == 1 then
4009 goto _continue_0
4010 end
4011 _accum_0[_len_0] = x
4012 _len_0 = _len_0 + 1
4013 ::_continue_0::
4014end
4015odds = _accum_0
4016local have_coins = false
4017if have_coins then
4018 print("有硬币")
4019else
4020 print("没有硬币")
4021end
4022local have_coins = false
4023if have_coins then
4024 print("有硬币")
4025else
4026 print("没有硬币")
4027end
4028local have_coins = false
4029print((function()
4030 if have_coins then
4031 return "有硬币"
4032 else
4033 return "没有硬币"
4034 end
4035end)())
4036local is_tall
4037is_tall = function(name)
4038 if name == "Rob" then
4039 return true
4040 else
4041 return false
4042 end
4043end
4044local message
4045if is_tall("Rob") then
4046 message = "我很高"
4047else
4048 message = "我不是很高"
4049end
4050print(message)
4051if not (os.date("%A") == "Monday") then
4052 print("今天不是星期一!")
4053end
4054if not (math.random() > 0.1) then
4055 print("你真幸运!")
4056end
4057local a = 5
4058if (1 == a or 3 == a or 5 == a or 7 == a) then
4059 print("检查离散值的相等性")
4060end
4061if (function()
4062 local _check_0 = list
4063 for _index_0 = 1, #_check_0 do
4064 if _check_0[_index_0] == a then
4065 return true
4066 end
4067 end
4068 return false
4069end)() then
4070 print("检查`a`是否在列表中")
4071end
4072if not (math.random() > 0.1) then
4073 print("你很幸运!")
4074end
4075if name == "Rob" then
4076 print("你好,世界")
4077end
4078local _list_0 = items
4079for _index_0 = 1, #_list_0 do
4080 local item = _list_0[_index_0]
4081 print("项目: ", item)
4082end
4083while game:isRunning() do
4084 game:update()
4085end
4086while not reader:eof() do
4087 reader:parse_line()
4088end
4089local name = "Dan"
4090if "Robert" == name then
4091 print("你是Robert")
4092elseif "Dan" == name or "Daniel" == name then
4093 print("你的名字是Dan")
4094else
4095 print("我不认识你,你的名字是" .. tostring(name))
4096end
4097local b = 1
4098local next_number
4099if 1 == b then
4100 next_number = 2
4101elseif 2 == b then
4102 next_number = 3
4103else
4104 next_number = error("数字数得太大了!")
4105end
4106local msg
4107local _exp_0 = math.random(1, 5)
4108if 1 == _exp_0 then
4109 msg = "你很幸运"
4110elseif 2 == _exp_0 then
4111 msg = "你差点很幸运"
4112else
4113 msg = "不太幸运"
4114end
4115do
4116 local _exp_0 = math.random(1, 5)
4117 if 1 == _exp_0 then
4118 print("你很幸运")
4119 else
4120 print("不太幸运")
4121 end
4122end
4123local _exp_0 = math.random(1, 5)
4124if 1 == _exp_0 then
4125 print("你很幸运")
4126else
4127 print("不太幸运")
4128end
4129local items = {
4130 {
4131 x = 100,
4132 y = 200
4133 },
4134 {
4135 width = 300,
4136 height = 400
4137 }
4138}
4139for _index_0 = 1, #items do
4140 local item = items[_index_0]
4141 local _type_0 = type(item)
4142 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4143 local _match_0 = false
4144 if _tab_0 then
4145 local x = item.x
4146 local y = item.y
4147 if x ~= nil and y ~= nil then
4148 _match_0 = true
4149 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
4150 end
4151 end
4152 if not _match_0 then
4153 if _tab_0 then
4154 local width = item.width
4155 local height = item.height
4156 if width ~= nil and height ~= nil then
4157 print("尺寸 " .. tostring(width) .. ", " .. tostring(height))
4158 end
4159 end
4160 end
4161end
4162local item = { }
4163local x, y = item.pos.x, item.pos.y
4164if x == nil then
4165 x = 50
4166end
4167if y == nil then
4168 y = 200
4169end
4170local _type_0 = type(item)
4171local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4172if _tab_0 then
4173 do
4174 local _obj_0 = item.pos
4175 local _type_1 = type(_obj_0)
4176 if "table" == _type_1 or "userdata" == _type_1 then
4177 x = _obj_0.x
4178 end
4179 end
4180 do
4181 local _obj_0 = item.pos
4182 local _type_1 = type(_obj_0)
4183 if "table" == _type_1 or "userdata" == _type_1 then
4184 y = _obj_0.y
4185 end
4186 end
4187 if x == nil then
4188 x = 50
4189 end
4190 if y == nil then
4191 y = 200
4192 end
4193 print("Vec2 " .. tostring(x) .. ", " .. tostring(y))
4194end
4195local _exp_0 = tb
4196local _type_0 = type(_exp_0)
4197local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4198local _match_0 = false
4199if _tab_0 then
4200 if 1 == _exp_0[1] and 2 == _exp_0[2] and 3 == _exp_0[3] then
4201 _match_0 = true
4202 print("1, 2, 3")
4203 end
4204end
4205if not _match_0 then
4206 local _match_1 = false
4207 if _tab_0 then
4208 local b = _exp_0[2]
4209 if 1 == _exp_0[1] and b ~= nil and 3 == _exp_0[3] then
4210 _match_1 = true
4211 print("1, " .. tostring(b) .. ", 3")
4212 end
4213 end
4214 if not _match_1 then
4215 if _tab_0 then
4216 local b = _exp_0[3]
4217 if b == nil then
4218 b = 3
4219 end
4220 if 1 == _exp_0[1] and 2 == _exp_0[2] then
4221 print("1, 2, " .. tostring(b))
4222 end
4223 end
4224 end
4225end
4226local _exp_0 = tb
4227local _type_0 = type(_exp_0)
4228local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4229local _match_0 = false
4230if _tab_0 then
4231 local result = _exp_0.result
4232 if true == _exp_0.success and result ~= nil then
4233 _match_0 = true
4234 print("成功", result)
4235 end
4236end
4237if not _match_0 then
4238 local _match_1 = false
4239 if _tab_0 then
4240 if false == _exp_0.success then
4241 _match_1 = true
4242 print("失败", result)
4243 end
4244 end
4245 if not _match_1 then
4246 print("无效值")
4247 end
4248end
4249local _exp_0 = tb
4250local _type_0 = type(_exp_0)
4251local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4252local _match_0 = false
4253if _tab_0 then
4254 local content
4255 do
4256 local _obj_0 = _exp_0.data
4257 local _type_1 = type(_obj_0)
4258 if "table" == _type_1 or "userdata" == _type_1 then
4259 content = _obj_0.content
4260 end
4261 end
4262 local _val_0
4263 do
4264 local _obj_0 = _exp_0.data
4265 if _obj_0 ~= nil then
4266 _val_0 = _obj_0.type
4267 end
4268 end
4269 if "success" == _val_0 and content ~= nil then
4270 _match_0 = true
4271 print("成功", content)
4272 end
4273end
4274if not _match_0 then
4275 local _match_1 = false
4276 if _tab_0 then
4277 local content
4278 do
4279 local _obj_0 = _exp_0.data
4280 local _type_1 = type(_obj_0)
4281 if "table" == _type_1 or "userdata" == _type_1 then
4282 content = _obj_0.content
4283 end
4284 end
4285 local _val_0
4286 do
4287 local _obj_0 = _exp_0.data
4288 if _obj_0 ~= nil then
4289 _val_0 = _obj_0.type
4290 end
4291 end
4292 if "error" == _val_0 and content ~= nil then
4293 _match_1 = true
4294 print("失败", content)
4295 end
4296 end
4297 if not _match_1 then
4298 print("无效值")
4299 end
4300end
4301local _exp_0 = tb
4302local _type_0 = type(_exp_0)
4303local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4304if _tab_0 then
4305 local fourth = _exp_0[4]
4306 local _val_0
4307 do
4308 local _obj_0 = _exp_0[1]
4309 if _obj_0 ~= nil then
4310 _val_0 = _obj_0.a
4311 end
4312 end
4313 local _val_1
4314 do
4315 local _obj_0 = _exp_0[1]
4316 if _obj_0 ~= nil then
4317 _val_1 = _obj_0.b
4318 end
4319 end
4320 local _val_2
4321 do
4322 local _obj_0 = _exp_0[2]
4323 if _obj_0 ~= nil then
4324 _val_2 = _obj_0.a
4325 end
4326 end
4327 local _val_3
4328 do
4329 local _obj_0 = _exp_0[2]
4330 if _obj_0 ~= nil then
4331 _val_3 = _obj_0.b
4332 end
4333 end
4334 local _val_4
4335 do
4336 local _obj_0 = _exp_0[3]
4337 if _obj_0 ~= nil then
4338 _val_4 = _obj_0.a
4339 end
4340 end
4341 local _val_5
4342 do
4343 local _obj_0 = _exp_0[3]
4344 if _obj_0 ~= nil then
4345 _val_5 = _obj_0.b
4346 end
4347 end
4348 if 1 == _val_0 and 2 == _val_1 and 3 == _val_2 and 4 == _val_3 and 5 == _val_4 and 6 == _val_5 and fourth ~= nil then
4349 print("匹配成功", fourth)
4350 end
4351end
4352local segments = {
4353 "admin",
4354 "users",
4355 "logs",
4356 "view"
4357}
4358local _type_0 = type(segments)
4359local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4360if _tab_0 then
4361 local groups
4362 do
4363 local _accum_0 = { }
4364 local _len_0 = 1
4365 local _max_0 = #segments + -3 + 1
4366 for _index_0 = 1, _max_0 do
4367 local _item_0 = segments[_index_0]
4368 _accum_0[_len_0] = _item_0
4369 _len_0 = _len_0 + 1
4370 end
4371 groups = _accum_0
4372 end
4373 local resource = segments[#segments - 1]
4374 local action = segments[#segments]
4375 if resource ~= nil and action ~= nil then
4376 print("Group:", groups)
4377 print("Resource:", resource)
4378 print("Action:", action)
4379 end
4380end
4381local Inventory 4313local Inventory
4382local _class_0 4314local _class_0
4383local _base_0 = { 4315local _base_0 = {
@@ -5038,6 +4970,46 @@ local _with_0 = obj
5038if _with_0 ~= nil then 4970if _with_0 ~= nil then
5039 print(obj.name) 4971 print(obj.name)
5040end 4972end
4973local _with_0 = Person()
4974_with_0.name = "Oswald"
4975_with_0:add_relative(my_dad)
4976_with_0:save()
4977print(_with_0.name)
4978local file
4979local _with_0 = File("favorite_foods.txt")
4980_with_0:set_encoding("utf8")
4981file = _with_0
4982local create_person
4983create_person = function(name, relatives)
4984 local _with_0 = Person()
4985 _with_0.name = name
4986 for _index_0 = 1, #relatives do
4987 local relative = relatives[_index_0]
4988 _with_0:add_relative(relative)
4989 end
4990 return _with_0
4991end
4992local me = create_person("Leaf", {
4993 dad,
4994 mother,
4995 sister
4996})
4997local str = "你好"
4998print("原始:", str)
4999print("大写:", str:upper())
5000local _with_0 = tb
5001_with_0[1] = 1
5002print(_with_0[2])
5003do
5004 local _with_1 = _with_0[abc]
5005 _with_1[3] = _with_1[2]:func()
5006 _with_1["key-name"] = value
5007end
5008_with_0[#_with_0 + 1] = "abc"
5009local _with_0 = obj
5010if _with_0 ~= nil then
5011 print(obj.name)
5012end
5041do 5013do
5042 local var = "hello" 5014 local var = "hello"
5043 print(var) 5015 print(var)
@@ -5059,6 +5031,46 @@ local tbl = {
5059 return 1234 5031 return 1234
5060 end)() 5032 end)()
5061} 5033}
5034do
5035 local var = "hello"
5036 print(var)
5037end
5038print(var)
5039local counter
5040do
5041 local i = 0
5042 counter = function()
5043 i = i + 1
5044 return i
5045 end
5046end
5047print(counter())
5048print(counter())
5049local tbl = {
5050 key = (function()
5051 print("分配键值!")
5052 return 1234
5053 end)()
5054}
5055local my_object = {
5056 value = 1000,
5057 write = function(self)
5058 return print("值为:", self.value)
5059 end
5060}
5061local run_callback
5062run_callback = function(func)
5063 print("运行回调...")
5064 return func()
5065end
5066run_callback(my_object.write)
5067run_callback((function()
5068 local _base_0 = my_object
5069 local _fn_0 = _base_0.write
5070 return _fn_0 and function(...)
5071 return _fn_0(_base_0, ...)
5072 end
5073end)())
5062local my_object = { 5074local my_object = {
5063 value = 1000, 5075 value = 1000,
5064 write = function(self) 5076 write = function(self)
@@ -5106,3 +5118,31 @@ my_func = function(add)
5106end 5118end
5107my_func(22) 5119my_func(22)
5108print(i, k) 5120print(i, k)
5121local i = 100
5122local my_func
5123my_func = function()
5124 i = 10
5125 while i > 0 do
5126 print(i)
5127 i = i - 1
5128 end
5129end
5130my_func()
5131print(i)
5132local i = 100
5133local my_func
5134my_func = function()
5135 local i = "hello"
5136end
5137my_func()
5138print(i)
5139local tmp = 1213
5140local i, k = 100, 50
5141local my_func
5142my_func = function(add)
5143 local tmp = tmp + add
5144 i = i + tmp
5145 k = k + tmp
5146end
5147my_func(22)
5148print(i, k)
diff --git a/spec/outputs/compile_doc.lua b/spec/outputs/compile_doc.lua
index e7d307d..cb359a1 100644
--- a/spec/outputs/compile_doc.lua
+++ b/spec/outputs/compile_doc.lua
@@ -2,52 +2,113 @@ local outputFolder = ...
2local _list_0 = { 2local _list_0 = {
3 { 3 {
4 "codes_from_doc.lua", 4 "codes_from_doc.lua",
5 "doc/docs/doc/index.md" 5 {
6 "doc/docs/doc/introduction.md",
7 "doc/docs/doc/macro.md",
8 "doc/docs/doc/operator.md",
9 "doc/docs/doc/module.md",
10 "doc/docs/doc/assignment.md",
11 "doc/docs/doc/destructuring-assignment.md",
12 "doc/docs/doc/if-assignment.md",
13 "doc/docs/doc/varargs-assignment.md",
14 "doc/docs/doc/whitespace.md",
15 "doc/docs/doc/comment.md",
16 "doc/docs/doc/try.md",
17 "doc/docs/doc/attributes.md",
18 "doc/docs/doc/literals.md",
19 "doc/docs/doc/function-literals.md",
20 "doc/docs/doc/backcalls.md",
21 "doc/docs/doc/table-literals.md",
22 "doc/docs/doc/comprehensions.md",
23 "doc/docs/doc/for-loop.md",
24 "doc/docs/doc/while-loop.md",
25 "doc/docs/doc/continue.md",
26 "doc/docs/doc/conditionals.md",
27 "doc/docs/doc/line-decorators.md",
28 "doc/docs/doc/switch.md",
29 "doc/docs/doc/object-oriented-programming.md",
30 "doc/docs/doc/with-statement.md",
31 "doc/docs/doc/do.md",
32 "doc/docs/doc/function-stubs.md",
33 "doc/docs/doc/the-using-clause-controlling-destructive-assignment.md"
34 }
6 }, 35 },
7 { 36 {
8 "codes_from_doc_zh.lua", 37 "codes_from_doc_zh.lua",
9 "doc/docs/zh/doc/index.md" 38 {
39 "doc/docs/zh/doc/introduction.md",
40 "doc/docs/zh/doc/macro.md",
41 "doc/docs/zh/doc/operator.md",
42 "doc/docs/zh/doc/module.md",
43 "doc/docs/zh/doc/assignment.md",
44 "doc/docs/zh/doc/destructuring-assignment.md",
45 "doc/docs/zh/doc/if-assignment.md",
46 "doc/docs/zh/doc/varargs-assignment.md",
47 "doc/docs/zh/doc/whitespace.md",
48 "doc/docs/zh/doc/comment.md",
49 "doc/docs/zh/doc/try.md",
50 "doc/docs/zh/doc/attributes.md",
51 "doc/docs/zh/doc/literals.md",
52 "doc/docs/zh/doc/function-literals.md",
53 "doc/docs/zh/doc/backcalls.md",
54 "doc/docs/zh/doc/table-literals.md",
55 "doc/docs/zh/doc/comprehensions.md",
56 "doc/docs/zh/doc/for-loop.md",
57 "doc/docs/zh/doc/while-loop.md",
58 "doc/docs/zh/doc/continue.md",
59 "doc/docs/zh/doc/conditionals.md",
60 "doc/docs/zh/doc/line-decorators.md",
61 "doc/docs/zh/doc/switch.md",
62 "doc/docs/zh/doc/object-oriented-programming.md",
63 "doc/docs/zh/doc/with-statement.md",
64 "doc/docs/zh/doc/do.md",
65 "doc/docs/zh/doc/function-stubs.md",
66 "doc/docs/zh/doc/the-using-clause-controlling-destructive-assignment.md"
67 }
10 } 68 }
11} 69}
12for _index_0 = 1, #_list_0 do 70for _index_0 = 1, #_list_0 do
13 local _des_0 = _list_0[_index_0] 71 local _des_0 = _list_0[_index_0]
14 local compiledFile, docFile = _des_0[1], _des_0[2] 72 local compiledFile, docFiles = _des_0[1], _des_0[2]
15 local input 73 local codes = { }
16 local _with_0 = io.open(docFile) 74 for _index_1 = 1, #docFiles do
17 if _with_0 ~= nil then 75 local docFile = docFiles[_index_1]
18 local to_lua = require("yue").to_lua 76 local input
19 local text = _with_0:read("*a") 77 local _with_0 = io.open(docFile)
20 local codes = { } 78 if _with_0 ~= nil then
21 for code in text:gmatch("```yuescript[\r\n]+(.-)```[^%w]") do 79 local to_lua = require("yue").to_lua
22 local result, err = to_lua(code, { 80 local text = _with_0:read("*a")
23 implicit_return_root = false, 81 for code in text:gmatch("```yuescript[\r\n]+(.-)```[^%w]") do
24 reserve_line_number = false 82 local result, err = to_lua(code, {
25 }) 83 implicit_return_root = false,
26 if result then 84 reserve_line_number = false
27 codes[#codes + 1] = result 85 })
28 elseif not err:match("macro exporting module only accepts macro definition") then 86 if result then
29 print(err) 87 codes[#codes + 1] = result
30 os.exit(1) 88 elseif not err:match("macro exporting module only accepts macro definition") then
89 print(err)
90 os.exit(1)
91 end
31 end 92 end
32 end 93 for code in text:gmatch("```yue[\r\n]+(.-)```[^%w]") do
33 for code in text:gmatch("```yue[\r\n]+(.-)```[^%w]") do 94 local result, err = to_lua(code, {
34 local result, err = to_lua(code, { 95 implicit_return_root = false,
35 implicit_return_root = false, 96 reserve_line_number = false
36 reserve_line_number = false 97 })
37 }) 98 if result then
38 if result then 99 codes[#codes + 1] = result
39 codes[#codes + 1] = result 100 else
40 else 101 print(err)
41 print(err) 102 os.exit(1)
42 os.exit(1) 103 end
43 end 104 end
44 end 105 end
45 local output 106 input = _with_0
46 local _with_1 = io.open(tostring(outputFolder) .. "/" .. tostring(compiledFile), "w+") 107 local _close_0 <close> = input
47 _with_1:write(table.concat(codes))
48 output = _with_1
49 local _close_0 <close> = output
50 end 108 end
51 input = _with_0 109 local output
52 local _close_0 <close> = input 110 local _with_0 = io.open(tostring(outputFolder) .. "/" .. tostring(compiledFile), "w+")
111 _with_0:write(table.concat(codes))
112 output = _with_0
113 local _close_0 <close> = output
53end 114end