aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/inputs/destructure.yue50
-rw-r--r--spec/inputs/funcs.yue33
-rw-r--r--spec/inputs/import.yue12
-rw-r--r--spec/inputs/lists.yue190
-rw-r--r--spec/inputs/loops.yue15
-rw-r--r--spec/inputs/macro.yue54
-rw-r--r--spec/inputs/macro_export.yue31
-rw-r--r--spec/inputs/macro_teal.yue13
-rw-r--r--spec/inputs/macro_todo.yue7
-rw-r--r--spec/inputs/props.yue61
-rw-r--r--spec/inputs/string.yue73
-rw-r--r--spec/inputs/switch.yue71
-rw-r--r--spec/inputs/syntax.yue12
-rw-r--r--spec/inputs/tables.yue18
-rw-r--r--spec/inputs/test/format_spec.yue2
-rw-r--r--spec/inputs/try_catch.yue122
-rw-r--r--spec/inputs/unicode/destructure.yue2
-rw-r--r--spec/inputs/unicode/funcs.yue2
-rw-r--r--spec/inputs/unicode/import.yue2
-rw-r--r--spec/inputs/unicode/macro_export.yue4
-rw-r--r--spec/inputs/unicode/syntax.yue4
-rw-r--r--spec/inputs/unicode/vararg.yue4
-rw-r--r--spec/inputs/unicode/with.yue24
-rw-r--r--spec/inputs/vararg.yue48
-rw-r--r--spec/inputs/with.yue33
-rw-r--r--spec/outputs/5.1/loops.lua28
-rw-r--r--spec/outputs/5.1/try_catch.lua265
-rw-r--r--spec/outputs/assign.lua6
-rw-r--r--spec/outputs/codes_from_doc.lua665
-rw-r--r--spec/outputs/codes_from_doc_zh.lua665
-rw-r--r--spec/outputs/comprehension.lua10
-rw-r--r--spec/outputs/destructure.lua110
-rw-r--r--spec/outputs/funcs.lua153
-rw-r--r--spec/outputs/import.lua10
-rw-r--r--spec/outputs/lists.lua594
-rw-r--r--spec/outputs/loops.lua28
-rw-r--r--spec/outputs/macro.lua13
-rw-r--r--spec/outputs/props.lua240
-rw-r--r--spec/outputs/string.lua41
-rw-r--r--spec/outputs/switch.lua121
-rw-r--r--spec/outputs/syntax.lua11
-rw-r--r--spec/outputs/tables.lua22
-rw-r--r--spec/outputs/test/format_spec.lua2
-rw-r--r--spec/outputs/try_catch.lua266
-rw-r--r--spec/outputs/unicode/assign.lua6
-rw-r--r--spec/outputs/unicode/comprehension.lua10
-rw-r--r--spec/outputs/unicode/funcs.lua2
-rw-r--r--spec/outputs/unicode/import.lua4
-rw-r--r--spec/outputs/unicode/lists.lua23
-rw-r--r--spec/outputs/unicode/macro.lua46
-rw-r--r--spec/outputs/unicode/multiline_chain.lua6
-rw-r--r--spec/outputs/unicode/syntax.lua6
-rw-r--r--spec/outputs/unicode/vararg.lua28
-rw-r--r--spec/outputs/upvalue_func.lua48
-rw-r--r--spec/outputs/vararg.lua100
-rw-r--r--spec/outputs/with.lua51
56 files changed, 4198 insertions, 269 deletions
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue
index 674dfe4..179056c 100644
--- a/spec/inputs/destructure.yue
+++ b/spec/inputs/destructure.yue
@@ -94,7 +94,7 @@ do
94-- 94--
95 95
96do 96do
97 with {a,b} = thing 97 with {a,b} := thing
98 print a, b 98 print a, b
99 99
100 100
@@ -240,5 +240,53 @@ do
240 switch tb 240 switch tb
241 when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}} 241 when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}}
242 print meta_field, abc, def 242 print meta_field, abc, def
243
244do
245 clients = ["VIP_Alice", "User_Bob", "User_Clara", "VIP_Eva"]
246 [vipStart, ...regulars, vipEnd] = clients
247 print vipStart -- "VIP_Alice"
248 print regulars -- {"User_Bob", "User_Clara"}
249 print vipEnd -- "VIP_Eva"
250
251do
252 setupMeeting = (participants) ->
253 [chair, ..._, secretary] = participants
254 print chair, secretary
255
256 setupMeeting ["Alice", "Bob", "Charlie", "David"]
257 -- Output: Alice David
258
259do
260 getTransactions = ->
261 {
262 {id: "T1", amount: 100}
263 {id: "T2", amount: 200}
264 {id: "T3", amount: 300}
265 }
266
267 :id, :amount = getTransactions![#]
268 assert id == "T3"
269 assert amount == 300
270
271do
272 [
273 _
274 ...middle
275 _
276 ] = tb
277
278do
279 {a, :abc, b, :def, ...sub, d, e} = tb
280
281do
282 for {:a, :b} in *items
283 print a, b
284
285 for :a, :b in *items
286 print a, b
287
288 for :body in pairs data
289 print body if body
290
243nil 291nil
244 292
diff --git a/spec/inputs/funcs.yue b/spec/inputs/funcs.yue
index e647edc..d19c2d1 100644
--- a/spec/inputs/funcs.yue
+++ b/spec/inputs/funcs.yue
@@ -98,7 +98,7 @@ f(
98 98
99x = (a, 99x = (a,
100 b) -> 100 b) ->
101 print "what" 101 print "what"
102 102
103 103
104y = (a="hi", 104y = (a="hi",
@@ -193,4 +193,35 @@ do
193 func = (): -> check 123 193 func = (): -> check 123
194 print func! -- get nil 194 print func! -- get nil
195 195
196do
197 f = ({:a, :b, :c}) -> print a, b, c
198 f = (:a, :b, :c) -> print a, b, c
199 g = (x, :y) -> print x, y
200 i = ({a: ax = 0, b: by = 0}) -> print ax, by
201 j = (name, {id: uid = "n/a", :role = "guest"}) -> print name, uid, role
202 m = ({user: {:name, :age}, meta: {:ver = 1}}) -> print name, age, ver
203 m1 = ({user: {:name, :age}, :meta = {}}) -> print name, age, meta and meta.ver or "nil"
204 new = ({:name = "anon", :age = 0}) =>
205 @name = name
206 @age = age
207 set = ({:name = @name, :age = @age}) =>
208 @name = name
209 @age = age
210 logKV = ({:k, :v}, ...) ->
211 print "kv:", k, v
212 print "rest count:", select "#", ...
213 macro gen = (fname) -> |
214 #{fname} = ({:a, :b = 0}) -> print a, b
215 $gen foo
216 t1 = (:a, x) -> print a, x
217 t2 = (:a) -> print a
218 w = (
219 id
220 {:x = 0, :y = 0}
221 :flag
222 ) ->
223 print id, x, y, flag
224 g1 = ({:a, a: ax}) -> print a, ax
225 g4 = ({:a, :b, ...rest}) -> print a, b
226
196nil 227nil
diff --git a/spec/inputs/import.yue b/spec/inputs/import.yue
index b8ffc24..ee1fdfc 100644
--- a/spec/inputs/import.yue
+++ b/spec/inputs/import.yue
@@ -23,7 +23,7 @@ do
23 b, c from z 23 b, c from z
24 24
25do 25do
26 import a 26 import a,
27 b 27 b
28 c from z 28 c from z
29 29
@@ -139,3 +139,13 @@ do
139 import "m" as {c: d} 139 import "m" as {c: d}
140 import "m" as {g, {<close>: i}} 140 import "m" as {g, {<close>: i}}
141 141
142do
143 import require
144 import string as stringlib
145 import string.format
146 import io.read as io_read
147
148 type = ->
149 import type as tp
150 import 月 as yue
151
diff --git a/spec/inputs/lists.yue b/spec/inputs/lists.yue
index 921cae0..dd951a5 100644
--- a/spec/inputs/lists.yue
+++ b/spec/inputs/lists.yue
@@ -87,4 +87,194 @@ do
87 [a, b] = hello 87 [a, b] = hello
88 [name = "nameless", job = "jobless"] = person 88 [name = "nameless", job = "jobless"] = person
89 89
90do
91 transactions = ["T001", "T002", "T003", "T004", "T005"]
92 middleTransactions = transactions[2, -2]
93 print middleTransactions -- => {"T002", "T003", "T004"}
94
95do
96 logs =
97 - start: 0, end: 100
98 - start: 100, end: 200
99 - start: 200, end: 123
100 print logs[#].end -- => 123
101
102do
103 pendingOrders = ["O001", "O002", "O003", "O004"]
104 print pendingOrders[# - 1] -- => "O003"
105
106do
107 getOrders = ->
108 {
109 { id: "O1001", status: "pending" }
110 { id: "O1002", status: "processing" }
111 { id: "O1003", status: "done" }
112 }
113
114 lastStatus = getOrders()[#].status
115 assert lastStatus == "done"
116
117do
118 cloneList1 = (list) -> list[,]
119 cloneList2 = (list) -> [...list,]
120 cloneTable = (tb) -> {...tb}
121
122do
123 print(
124 globalTB[#]
125 a.b.c[# - 2]
126 x?\y?!.z?[# - 3]
127 )
128
129do
130 f = ->
131 print(
132 globalTB[#]\end 123
133 a.b.c[5,-5][# - 2]
134 x?\y?!.z?[# - 3]?[, -3]
135 )
136
137do
138 tb = [1, 2, 3]
139 tb[#] = 40
140 tb[# - 1] = 20
141
142do
143 a = b = c = "x"
144 lst = []
145 lst[#] = a
146 lst[# - 1] = b
147
148do
149 x, y, z = 1, 2, 3
150 arr = []
151 arr[#], head = x, y
152 arr[#] = z
153
154do
155 triple = ["keep", "skip", "tail"]
156 [head, _, tailv] = triple
157 buf = []
158 buf[#] = head
159 buf[#] = tailv
160
161do
162 src = ["a", "", "c", nil, "d"]
163 collected = []
164 for item in *src
165 if item and #item > 0
166 collected[#] = item
167
168do
169 nums = [1, 2, 3, 4, 5]
170 last_two = [v for v in *nums when v > 3]
171 nums[#] = last_two[1]
172 nums[#] = last_two[2]
173
174do
175 store = []
176 store[#] = { meta: { id: 1, ok: true }, payload: [10, 20] }
177 store[#] = { meta: { id: 1, ok: false }, payload: [10, 20, 30] }
178
179do
180 f = ->
181 q = []
182 tb.tmp = [n for n = 1, 4]
183 if #tb.tmp >= 3
184 q[#] = {head: tb.tmp[1], tail: tb.tmp[#]}
185
186do
187 make_pair = (a, b) -> [a, b]
188 pairs = []
189 p1 = make_pair 7, 8
190 pairs[#] = p1
191 k, v = "key", 42
192 pairs[#] = {k: k, v: v}
193
194do
195 cfg = {mode: "safe", tags: []}
196 if cfg.mode == "safe"
197 cfg.mode = "fast"
198 cfg.tags[#] = "newbie"
199
200do
201 mat = [ [1,2], [3,4], [5,6]]
202 last_row = mat[#]
203 rows = []
204 rows[#] = last_row[1]
205
206do
207 kv = []
208 kv[#] = {k: "a", v: 1}
209 kv[#] = {k: "b", v: 2}
210 pair_last = kv[#]
211 dict = {}
212 dict[pair_last.k] = pair_last.v
213 dict[pair_last.k] = 3
214
215do
216 base = [ i for i = 1, 4 ]
217 pack = []
218 pack[#] = [ base[1], base[#] ]
219 pack[#] = { first: base[1], last: base[#] }
220
221do
222 opts = {limit: 10}
223 {:limit, :offset = 0} = opts
224 pages = []
225 pages[#] = {limit: limit, offset: offset}
226
227do
228 chain = { a: { b: { c: 0 } }, list: [ {x:0}, {x:0} ] }
229 chain.a.b.c = 1
230 chain.list[1].x = 10
231 chain.list[#].x = 20
232 chain.list[# - 1] = { x: 30 }
233
234do
235 node = {left: {v:0}, right: {v:0}}
236 bag = []
237 { :left, :right } = node
238 bag[#], left.v, right.v = "k", 1, 2
239
240do
241 a1, a2, a3 = 100, 200, 300
242 mix = []
243 mix[#], mix[#], meta = a1, a2, {tag: "ok"}
244
245do
246 cfg2 = {limit: 5, opts: {flag: false}}
247 {limit: lim, opts: opt2} = cfg2
248 bucket = {xs: []}
249 bucket.xs[#], bucket.flag, opt2.flags[] = lim, true, 123
250
251do
252 ret2 = ()-> 7, 8
253 box = []
254 box[#], x1 = ret2!
255
256do
257 q = [1, 2]
258 lastq = q[#]
259 q[# - 1] = lastq * 10
260
261do
262 mat2 = [[9,8], [7,6]]
263 t = { hold: nil }
264 t.hold = mat2[#][1]
265
266do
267 f = -> globalTB[#][#] = 1
268 f1 = -> globalTB[#][# - 1]
269
270do
271 tbA[] = ...tbB
272 a, tb[], b[], c = 1, ...x, 3, 4
273
274 data =
275 a: {1,2,3}
276 b: {4,5,6}
277
278 flat = [...v for k,v in pairs data]
279
90nil 280nil
diff --git a/spec/inputs/loops.yue b/spec/inputs/loops.yue
index 9a91b42..5df10ca 100644
--- a/spec/inputs/loops.yue
+++ b/spec/inputs/loops.yue
@@ -251,4 +251,17 @@ do
251 if value > 5 251 if value > 5
252 item 252 item
253 253
254 254do
255 repeat print 1 until true
256
257 x = repeat
258 a = func!
259 break a.x
260 until a.v
261
262 items = repeat
263 item = getItem!
264 break unless item
265 item if item.value > 0
266 until false
267
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue
index 5d5f1a9..191f09f 100644
--- a/spec/inputs/macro.yue
+++ b/spec/inputs/macro.yue
@@ -60,6 +60,11 @@ macro NumAndStr = (num, str) ->
60 60
61print $NumAndStr 123, 'xyz' 61print $NumAndStr 123, 'xyz'
62 62
63macro NumAndStr2 = (num`Num, str`SingleString) -> |
64 [#{num}, #{str}]
65
66print $NumAndStr2 456, 'abc'
67
63$asserts item == nil 68$asserts item == nil
64 69
65$myconfig false 70$myconfig false
@@ -100,13 +105,14 @@ macro filter = (items, action)->
100 $showMacro "filter", "[_ for _ in *#{items} when #{action}]" 105 $showMacro "filter", "[_ for _ in *#{items} when #{action}]"
101 106
102macro reduce = (items, def, action)-> 107macro reduce = (items, def, action)->
103 $showMacro "reduce", "if ##{items} == 0 108 $showMacro "reduce", |
104 #{def} 109 if ##{items} == 0
105else 110 #{def}
106 _1 = #{def} 111 else
107 for _2 in *#{items} 112 _1 = #{def}
108 _1 = #{action} 113 for _2 in *#{items}
109 _1" 114 _1 = #{action}
115 _1
110 116
111macro foreach = (items, action)-> 117macro foreach = (items, action)->
112 $showMacro "foreach", "for _ in *#{items} 118 $showMacro "foreach", "for _ in *#{items}
@@ -154,13 +160,15 @@ macro curry = (...)->
154f = $curry x,y,z,do 160f = $curry x,y,z,do
155 print x,y,z 161 print x,y,z
156 162
157macro get_inner = (var)-> "do 163macro get_inner = (var)-> |
158 a = 1 164 do
159 a + 1" 165 a = 1
166 a + 1
160 167
161macro get_inner_hygienic = (var)-> "(-> 168macro get_inner_hygienic = (var)-> |
162 local a = 1 169 (->
163 a + 1)!" 170 local a = 1
171 a + 1)!
164 172
165do 173do
166 a = 8 174 a = 8
@@ -196,6 +204,18 @@ end
196 204
197print x 205print x
198 206
207import "yue"
208macro lua = (code`YAMLMultiline) -> {
209 code: yue.loadstring(code)!
210 type: "lua"
211}
212
213$lua |
214 local function f2(a)
215 return a + 1
216 end
217 x = x + f2(3)
218
199macro def = (fname, ...)-> 219macro def = (fname, ...)->
200 args = {...} 220 args = {...}
201 last = table.remove args 221 last = table.remove args
@@ -317,7 +337,13 @@ $chainC(
317 Destroy! 337 Destroy!
318) 338)
319 339
320macro tb = -> "{'abc', a:123, <call>:=> 998}" 340macro tb = -> |
341 {
342 'abc'
343 a: 123
344 <call>: => 998
345 }
346
321print $tb[1], $tb.a, ($tb)!, $tb! 347print $tb[1], $tb.a, ($tb)!, $tb!
322 348
323print "current line: #{ $LINE }" 349print "current line: #{ $LINE }"
diff --git a/spec/inputs/macro_export.yue b/spec/inputs/macro_export.yue
index cc7d459..22905b5 100644
--- a/spec/inputs/macro_export.yue
+++ b/spec/inputs/macro_export.yue
@@ -8,13 +8,12 @@ export macro config = (debugging = true)->
8 "" 8 ""
9 9
10export macro showMacro = (name, res)-> 10export macro showMacro = (name, res)->
11 if debugMacro then " 11 if debugMacro then |
12do 12 do
13 txt = #{res} 13 txt = #{res}
14 print '[macro ' .. #{name} .. ']' 14 print '[macro #{name}]'
15 print txt 15 print txt
16 txt 16 txt
17"
18 else 17 else
19 res 18 res
20 19
@@ -35,14 +34,16 @@ export macro copy = (src, dst, ...)->
35 src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" 34 src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_"
36 "copy targets can not be _src_ or _dst_" 35 "copy targets can not be _src_ or _dst_"
37 ) 36 )
38 " 37 copyFields = table.concat(
39do 38 ["_dst_.#{field} = _src_.#{field}" for field in *{...}]
40 local _src_, _dst_ 39 "\n\t\t\t"
41 with _dst_ = #{dst} 40 )
42 with _src_ = #{src} 41 |
43#{table.concat for field in *{...} do " 42 do
44 _dst_.#{field} = _src_.#{field} 43 local _src_, _dst_
45"}" 44 with _dst_ := #{dst}
45 with _src_ := #{src}
46 #{copyFields}
46 47
47export macro enum = (...) -> 48export macro enum = (...) ->
48 items = {...} 49 items = {...}
diff --git a/spec/inputs/macro_teal.yue b/spec/inputs/macro_teal.yue
index 0cfd862..e51bcd7 100644
--- a/spec/inputs/macro_teal.yue
+++ b/spec/inputs/macro_teal.yue
@@ -4,11 +4,16 @@ $ ->
4 options.target_extension = "tl" 4 options.target_extension = "tl"
5 package.path ..= ";./spec/lib/?.lua" 5 package.path ..= ";./spec/lib/?.lua"
6 6
7macro to_lua = (code)-> 7macro to_lua = (code)-> |
8 "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" 8 require('yue').to_lua #{code},
9 reserve_line_number: false
10 same_module: true
9 11
10macro trim = (name)-> 12macro trim = (name)-> |
11 "if result := #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" 13 if result := #{name}\match '[\'"](.*)[\'"]'
14 result
15 else
16 #{name}
12 17
13export macro local = (decl, value = nil)-> 18export macro local = (decl, value = nil)->
14 import "yue" as {options:{:tl_enabled}} 19 import "yue" as {options:{:tl_enabled}}
diff --git a/spec/inputs/macro_todo.yue b/spec/inputs/macro_todo.yue
index 752c9cb..c9c8f77 100644
--- a/spec/inputs/macro_todo.yue
+++ b/spec/inputs/macro_todo.yue
@@ -5,9 +5,6 @@ export macro todoInner = (module, line, msg)->
5 type: "lua" 5 type: "lua"
6 } 6 }
7 7
8export macro todo = (msg)-> 8export macro todo = (msg)-> |
9 if msg 9 $todoInner $FILE, $LINE#{msg and ", #{msg}" or ""}
10 "$todoInner $FILE, $LINE, #{msg}"
11 else
12 "$todoInner $FILE, $LINE"
13 10
diff --git a/spec/inputs/props.yue b/spec/inputs/props.yue
new file mode 100644
index 0000000..bbb7aae
--- /dev/null
+++ b/spec/inputs/props.yue
@@ -0,0 +1,61 @@
1class Props
2 __index: (name): nil =>
3 cls = @.<>
4 if item := cls.__getter?[name] -- access properties
5 return item @
6 elseif item := rawget cls, name -- access member functions
7 return item
8 else
9 c = cls
10 while c := c.<> -- recursive to access base classes
11 if item := c.__getter?[name]
12 cls.__getter ??= {}
13 cls.__getter[name] = item -- cache base properties to class
14 return item @
15 elseif item := rawget c, name
16 rawset cls, name, item -- cache base member to class
17 return item
18
19 __newindex: (name, value) =>
20 cls = @.<>
21 if item := cls.__setter?[name] -- access properties
22 item @, value
23 else
24 c = cls
25 while c := c.<> -- recursive to access base classes
26 if item := c.__setter?[name]
27 cls.__setter ??= {}
28 cls.__setter[name] = item -- cache base property to class
29 item @, value
30 return
31 rawset @, name, value -- assign field to self
32
33 assignReadOnly = -> error "assigning a readonly property"
34
35 prop: (name, props) =>
36 {
37 :get
38 :set = assignReadOnly
39 } = props
40 if getter := rawget @__base, "__getter"
41 getter[name] = get
42 else
43 rawset @__base, "__getter", [name]: get
44 if setter := rawget @__base, "__setter"
45 setter[name] = set
46 else
47 rawset @__base, "__setter", [name]: set
48
49class A extends Props
50 @prop 'x'
51 get: => @_x + 1000
52 set: (v) => @_x = v
53 new: =>
54 @_x = 0
55
56class B extends A
57 @prop 'abc', get: => "hello"
58
59b = B!
60b.x = 999
61print b.x, b.abc
diff --git a/spec/inputs/string.yue b/spec/inputs/string.yue
index f91383e..611205d 100644
--- a/spec/inputs/string.yue
+++ b/spec/inputs/string.yue
@@ -74,3 +74,76 @@ _ = "hello"
74something"hello"\world! 74something"hello"\world!
75something "hello"\world! 75something "hello"\world!
76 76
77do
78 str = |
79 key: value
80 str = |
81 config:
82 enabled: true
83 level: 5
84 str = |
85 header: start
86
87 footer: end
88 str = |
89 name: #{username}
90 str = |
91 count: #{total} items
92 str = |
93 user: #{name}
94 id: #{id}
95 str = |
96 path: "C:\\Program Files\\App"
97 desc: 'single "quote" test'
98 str = |
99 key: value
100 next: 123
101 str = |
102 list:
103 - "one"
104 - "two"
105 str = |
106 -- comment
107 content text
108 -- comment
109 str = |
110 #{1 + 2}
111 #{2 + 3}
112 #{"a" .. "b"}
113 obj =
114 settings: |
115 mode: #{mode}
116 flags:
117 - #{flag1}
118 - default
119 fn = -> |
120 Hello
121 name: #{userName}
122 str = |
123 result:
124 status: #{if ok then "pass" else "fail"}
125 code: #{code}
126 summary = |
127 date: #{os.date()}
128 values:
129 -
130 a: #{aVal}
131 b: #{bVal or defaultB}
132 msg = send |
133 Hello, #{user}!
134 Today is #{os.date("%A")}.
135 desc = do
136 prefix = "Result"
137 |
138 #{prefix}:
139 value: #{compute!}
140 (|
141 1
142 2
143 3
144 ) |> print
145
146export yaml = |
147 version: #{ver}
148 ok: true
149
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue
index 7ff3118..2b0669c 100644
--- a/spec/inputs/switch.yue
+++ b/spec/inputs/switch.yue
@@ -220,4 +220,73 @@ do
220 ] 220 ]
221 print "matched", sixth 221 print "matched", sixth
222 222
223nil \ No newline at end of file 223do
224 switch v := "hello"
225 when "hello"
226 print "matched hello"
227 else
228 print "not matched"
229 -- output: matched hello
230
231do
232 f = -> "ok"
233 switch val := f!
234 when "ok"
235 print "it's ok"
236 -- output: it's ok
237
238
239do
240 g = -> 42
241 switch result := g!
242 when 1, 2
243 print "small"
244 when 42
245 print "life universe everything"
246 else
247 print "other #{result}"
248 -- output: life universe everything
249
250do
251 check = ->
252 if true
253 "yes"
254 else
255 "no"
256
257 switch x := check!
258 when "yes"
259 print "affirmative"
260 else
261 print "negative"
262 -- output: affirmative
263
264do
265 t = (): tb ->
266 tb = {a: 1}
267 tb.a = 2
268
269 switch data := t!
270 when {a: 2}
271 print "matched"
272 else
273 print "not matched"
274
275do
276 clientData = ["Meta", "CUST_1001", "CHK123"]
277 switch clientData
278 when [...metadata, customerId, checksum]
279 print metadata -- {"Meta"}
280 print customerId -- "CUST_1001"
281 print checksum -- "CHK123"
282
283do
284 handlePath = (segments) ->
285 switch segments
286 when [..._, resource, action]
287 print "Resource:", resource
288 print "Action:", action
289
290 handlePath ["admin", "logs", "view"]
291
292nil
diff --git a/spec/inputs/syntax.yue b/spec/inputs/syntax.yue
index a414d6f..eee518a 100644
--- a/spec/inputs/syntax.yue
+++ b/spec/inputs/syntax.yue
@@ -392,7 +392,7 @@ invokeA(
392v = { 392v = {
393 a -1 393 a -1
394 a( 394 a(
395-1) 395 -1)
396 a \ 396 a \
397- 1 397- 1
398 a-1 398 a-1
@@ -405,7 +405,7 @@ v = {
405 405
406 a ~1 406 a ~1
407 a( 407 a(
408~1) 408 ~1)
409 a \ 409 a \
410~ 1 410~ 1
411 a~1 411 a~1
@@ -478,5 +478,13 @@ do
478 f2 = -> 478 f2 = ->
479 -- 479 --
480 480
481do
482 return res if res ~= ""
483
484
485do
486 return res if res ~= ""
487 --
488
481nil 489nil
482 490
diff --git a/spec/inputs/tables.yue b/spec/inputs/tables.yue
index 0de8a8c..702e04a 100644
--- a/spec/inputs/tables.yue
+++ b/spec/inputs/tables.yue
@@ -245,6 +245,24 @@ menus =
245 click: -> 245 click: ->
246 } 246 }
247 247
248_ =
249 boolean:
250 - true
251 - false
252 float:
253 - 3.14
254 - -6.8523015e+5
255 int:
256 - 123
257 - -0b1010_0111_0100_1010_1110
258 null:
259 nodeName: 'node'
260 parent: nil
261 string:
262 - 'Hello world'
263 - "newline
264newline2"
265
248tb = {...other} 266tb = {...other}
249 267
250tbMix = { 268tbMix = {
diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue
index cbd9d22..3ad2c7f 100644
--- a/spec/inputs/test/format_spec.yue
+++ b/spec/inputs/test/format_spec.yue
@@ -119,7 +119,7 @@ for file in *files
119 original_ast = yue.to_ast code 119 original_ast = yue.to_ast code
120 assert.is_not_nil original_ast 120 assert.is_not_nil original_ast
121 rewriteLineCol original_ast 121 rewriteLineCol original_ast
122 formated = yue.format code 122 formated = yue.format code, 0, false
123 ast = yue.to_ast formated 123 ast = yue.to_ast formated
124 assert.is_not_nil ast 124 assert.is_not_nil ast
125 rewriteLineCol ast 125 rewriteLineCol ast
diff --git a/spec/inputs/try_catch.yue b/spec/inputs/try_catch.yue
index 4e05bc6..6c29a52 100644
--- a/spec/inputs/try_catch.yue
+++ b/spec/inputs/try_catch.yue
@@ -62,6 +62,126 @@ f = ->
62 do 62 do
63 <- x 63 <- x
64 local tb, a, b, c 64 local tb, a, b, c
65 f = -> try tb.f a, b, c 65 f1 = -> try tb.f a, b, c
66
67 do
68 f1 = -> do
69 ok, ... = try func!
70 ...
71
72 do
73 local func
74 a, b, c = try? func!
75
76 do
77 a, b, c = try? func!
78
79 do
80 a = (try? func!) ?? "default"
81
82 do
83 f try? func!
84
85 do
86 f try?
87 print 123
88 func!
89 catch e
90 print e
91 e
66 92
67 nil 93 nil
94
95do
96 try
97 func 1, 2, 3
98 catch err
99 print err
100
101 try func 1, 2, 3
102 catch err
103 print err
104
105 try
106 print "trying"
107 func 1, 2, 3
108
109 do
110 success, result = try
111 func 1, 2, 3
112 catch err
113 print err
114
115 success, result = try func 1, 2, 3
116
117 tb = {}
118
119 try tb.func
120 try tb.func!
121 try tb.func()
122 try (tb.func!)
123 try (tb\func(1, 2, 3))
124
125 try tb.func 1
126 try tb.func(1)
127
128 if (try func 1
129 catch err
130 print err)
131 print "OK"
132
133 if try (func 1)
134 catch err
135 print err
136 print "OK"
137
138 do
139 if success, result := try func "abc", 123
140 print result
141
142 success, result = try func "abc", 123
143 catch err
144 print err
145
146 print result if success, result := try func "abc", 123
147 catch err
148 print err
149
150 do
151 try
152 func 1, 2, 3
153
154 try func 1, 2, 3
155
156 do
157 <- x
158 local tb, a, b, c
159 f1 = -> try tb.f a, b, c
160
161 do
162 f1 = -> do
163 ok, ... = try func!
164 ...
165
166 do
167 local func
168 a, b, c = try? func!
169
170 do
171 a, b, c = try? func!
172
173 do
174 a = (try? func!) ?? "default"
175
176 do
177 f try? func!
178
179 do
180 f try?
181 print 123
182 func!
183 catch e
184 print e
185 e
186
187nil
diff --git a/spec/inputs/unicode/destructure.yue b/spec/inputs/unicode/destructure.yue
index 3c3a369..a5ffd5d 100644
--- a/spec/inputs/unicode/destructure.yue
+++ b/spec/inputs/unicode/destructure.yue
@@ -84,7 +84,7 @@ do
84-- 84--
85 85
86do 86do
87 with {元素a,元素b} = 东西 87 with {元素a,元素b} := 东西
88 打印 元素a, 元素b 88 打印 元素a, 元素b
89 89
90 90
diff --git a/spec/inputs/unicode/funcs.yue b/spec/inputs/unicode/funcs.yue
index c563356..cb35500 100644
--- a/spec/inputs/unicode/funcs.yue
+++ b/spec/inputs/unicode/funcs.yue
@@ -98,7 +98,7 @@ _无效变量 = -> 真名 if 某物
98 98
99变量x = (参数a, 99变量x = (参数a,
100 参数b) -> 100 参数b) ->
101 打印 "什么" 101 打印 "什么"
102 102
103 103
104变量y = (参数a="hi", 104变量y = (参数a="hi",
diff --git a/spec/inputs/unicode/import.yue b/spec/inputs/unicode/import.yue
index c229edb..a7724ab 100644
--- a/spec/inputs/unicode/import.yue
+++ b/spec/inputs/unicode/import.yue
@@ -23,7 +23,7 @@ do
23 字段b, 字段c from 对象z 23 字段b, 字段c from 对象z
24 24
25do 25do
26 import 字段a 26 import 字段a,
27 字段b 27 字段b
28 字段c from 对象z 28 字段c from 对象z
29 29
diff --git a/spec/inputs/unicode/macro_export.yue b/spec/inputs/unicode/macro_export.yue
index 3c9a942..56571cd 100644
--- a/spec/inputs/unicode/macro_export.yue
+++ b/spec/inputs/unicode/macro_export.yue
@@ -37,8 +37,8 @@ export macro 复制 = (源, 目标, ...)->
37 " 37 "
38do 38do
39 local _源_, _目标_ 39 local _源_, _目标_
40 with _目标_ = #{目标} 40 with _目标_ := #{目标}
41 with _源_ = #{源} 41 with _源_ := #{源}
42#{table.concat for 字段 in *{...} do " 42#{table.concat for 字段 in *{...} do "
43 _目标_.#{字段} = _源_.#{字段} 43 _目标_.#{字段} = _源_.#{字段}
44"}" 44"}"
diff --git a/spec/inputs/unicode/syntax.yue b/spec/inputs/unicode/syntax.yue
index 8a98416..01d5c87 100644
--- a/spec/inputs/unicode/syntax.yue
+++ b/spec/inputs/unicode/syntax.yue
@@ -376,7 +376,7 @@ with 对象
376变量v = { 376变量v = {
377 变量a -1 377 变量a -1
378 变量a( 378 变量a(
379-1) 379 -1)
380 变量a \ 380 变量a \
381- 1 381- 1
382 变量a-1 382 变量a-1
@@ -389,7 +389,7 @@ with 对象
389 389
390 变量a ~1 390 变量a ~1
391 变量a( 391 变量a(
392~1) 392 ~1)
393 变量a \ 393 变量a \
394~ 1 394~ 1
395 变量a~1 395 变量a~1
diff --git a/spec/inputs/unicode/vararg.yue b/spec/inputs/unicode/vararg.yue
index e59e114..b508fbb 100644
--- a/spec/inputs/unicode/vararg.yue
+++ b/spec/inputs/unicode/vararg.yue
@@ -55,12 +55,12 @@
55 _ = -> 55 _ = ->
56 列表 = {1, 2, 3, 4, 5} 56 列表 = {1, 2, 3, 4, 5}
57 函数名 = (确定) -> 57 函数名 = (确定) ->
58 确定, table.unpack 列表 58 确定, table.unpack 列表
59 确定, ... = 函数名 true 59 确定, ... = 函数名 true
60 打印 确定, ... 60 打印 确定, ...
61 61
62 多参数函数 = -> 62 多参数函数 = ->
63 10, nil, 20, nil, 30 63 10, nil, 20, nil, 30
64 64
65 ... = 多参数函数! 65 ... = 多参数函数!
66 打印 select "#", ... 66 打印 select "#", ...
diff --git a/spec/inputs/unicode/with.yue b/spec/inputs/unicode/with.yue
index ecbfdab..3c15add 100644
--- a/spec/inputs/unicode/with.yue
+++ b/spec/inputs/unicode/with.yue
@@ -45,19 +45,19 @@ do
45 with 变量a 45 with 变量a
46 打印 .世界 46 打印 .世界
47 47
48 模块 = with _模块 = {} 48 模块 = with _模块 := {}
49 .事物 = "你好" 49 .事物 = "你好"
50 50
51 with 变量a, 变量b = 东西, 布 51 with 变量a, 变量b := 东西, 布
52 打印 .世界 52 打印 .世界
53 53
54 变量x = with 变量a, 变量b = 1, 2 54 变量x = with 变量a, 变量b := 1, 2
55 打印 变量a + 变量b 55 打印 变量a + 变量b
56 56
57 打印 with 变量a, 变量b = 1, 2 57 打印 with 变量a, 变量b := 1, 2
58 打印 变量a + 变量b 58 打印 变量a + 变量b
59 59
60 p = with 你好!.字段x, 世界!.字段y = 1, 2 60 p = with 你好!.字段x, 世界!.字段y := 1, 2
61 打印 变量a + 变量b 61 打印 变量a + 变量b
62 62
63-- 63--
@@ -68,16 +68,16 @@ do
68 变量x\大写! 68 变量x\大写!
69 69
70do 70do
71 with 变量k = "乔" 71 with 变量k := "乔"
72 打印 \大写! 72 打印 \大写!
73 73
74do 74do
75 with 变量a,变量b,变量c = "", "", "" 75 with 变量a,变量b,变量c := "", "", ""
76 打印 \大写! 76 打印 \大写!
77 77
78do 78do
79 变量a = "床铺" 79 变量a = "床铺"
80 with 变量a,变量b,变量c = "", "", "" 80 with 变量a,变量b,变量c := "", "", ""
81 打印 \大写! 81 打印 \大写!
82 82
83do 83do
@@ -85,7 +85,7 @@ do
85 打印 \大写! 85 打印 \大写!
86 86
87do 87do
88 with 变量k.变量j = "乔" 88 with 变量k.变量j := "乔"
89 打印 \大写! 89 打印 \大写!
90 90
91do 91do
@@ -96,7 +96,7 @@ do
96 96
97do 97do
98 with 变量a 98 with 变量a
99 with .b = 2 99 with .b := 2
100 打印 .c 100 打印 .c
101 101
102do 102do
@@ -131,12 +131,12 @@ do
131 131
132do 132do
133 global 掩码 133 global 掩码
134 with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 134 with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000
135 .触摸启用 = true 135 .触摸启用 = true
136 .吞噬触摸 = true 136 .吞噬触摸 = true
137 137
138do 138do
139 with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 139 with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000
140 .触摸启用 = true 140 .触摸启用 = true
141 .吞噬触摸 = true 141 .吞噬触摸 = true
142 142
diff --git a/spec/inputs/vararg.yue b/spec/inputs/vararg.yue
index 941cd97..4f8a0d7 100644
--- a/spec/inputs/vararg.yue
+++ b/spec/inputs/vararg.yue
@@ -55,12 +55,12 @@ join = (...) ->
55 _ = -> 55 _ = ->
56 list = {1, 2, 3, 4, 5} 56 list = {1, 2, 3, 4, 5}
57 fn = (ok) -> 57 fn = (ok) ->
58 ok, table.unpack list 58 ok, table.unpack list
59 ok, ... = fn true 59 ok, ... = fn true
60 print ok, ... 60 print ok, ...
61 61
62 fn_many_args = -> 62 fn_many_args = ->
63 10, nil, 20, nil, 30 63 10, nil, 20, nil, 30
64 64
65 ... = fn_many_args! 65 ... = fn_many_args!
66 print select "#", ... 66 print select "#", ...
@@ -86,3 +86,47 @@ join = (...) ->
86 print ... 86 print ...
87 nil 87 nil
88 88
89do
90 f1 = (...t) ->
91 print t.n
92 print #t
93 for i = 1, t.n
94 print t[i]
95
96 f1 1, 2, 3
97 f1 "a", "b", "c", "d"
98 f1!
99
100 f2 = (...args) ->
101 print "args count:", args.n
102 print "args length:", #args
103 for i = 1, args.n
104 if args[i] == nil
105 print "position", i, "is nil"
106 else
107 print "position", i, ":", args[i]
108
109 f2 1, nil, 3, nil, 5
110
111 f3 = (prefix, ...items) ->
112 result = {}
113 for i = 1, items.n
114 result[i] = prefix .. tostring items[i]
115 result
116
117 f3 "item_", 1, 2, 3
118
119 f4 = (...empty) ->
120 print "empty count:", empty.n
121 print "empty length:", #empty
122
123 f4!
124
125 process = (...data) ->
126 sum = 0
127 for i = 1, data.n
128 if type(data[i]) == "number"
129 sum += data[i]
130 sum
131
132 process 1, 2, 3, "skip", 5
diff --git a/spec/inputs/with.yue b/spec/inputs/with.yue
index 3fee48e..19ed2a7 100644
--- a/spec/inputs/with.yue
+++ b/spec/inputs/with.yue
@@ -48,21 +48,21 @@ do
48 with a -- only one value allowed 48 with a -- only one value allowed
49 print .world 49 print .world
50 50
51 mod = with _M = {} 51 mod = with _M := {}
52 .Thing = "hi" 52 .Thing = "hi"
53 53
54 -- operate on a only 54 -- operate on a only
55 with a, b = something, pooh 55 with a, b := something, pooh
56 print .world 56 print .world
57 57
58 x = with a, b = 1, 2 58 x = with a, b := 1, 2
59 print a + b 59 print a + b
60 60
61 print with a, b = 1, 2 61 print with a, b := 1, 2
62 print a + b 62 print a + b
63 63
64 -- assignment lhs must be evaluated in the order they appear 64 -- assignment lhs must be evaluated in the order they appear
65 p = with hello!.x, world!.y = 1, 2 65 p = with hello!.x, world!.y := 1, 2
66 print a + b 66 print a + b
67 67
68-- 68--
@@ -73,16 +73,16 @@ do
73 x\upper! 73 x\upper!
74 74
75do 75do
76 with k = "jo" 76 with k := "jo"
77 print \upper! 77 print \upper!
78 78
79do 79do
80 with a,b,c = "", "", "" 80 with a,b,c := "", "", ""
81 print \upper! 81 print \upper!
82 82
83do 83do
84 a = "bunk" 84 a = "bunk"
85 with a,b,c = "", "", "" 85 with a,b,c := "", "", ""
86 print \upper! 86 print \upper!
87 87
88do 88do
@@ -90,7 +90,7 @@ do
90 print \upper! 90 print \upper!
91 91
92do 92do
93 with k.j = "jo" 93 with k.j := "jo"
94 print \upper! 94 print \upper!
95 95
96do 96do
@@ -103,7 +103,7 @@ do
103do 103do
104 with a 104 with a
105 -- nested `with`s with assignments should change the scope correctly 105 -- nested `with`s with assignments should change the scope correctly
106 with .b = 2 106 with .b := 2
107 print .c 107 print .c
108 108
109do 109do
@@ -138,12 +138,12 @@ do
138 138
139do 139do
140 global mask 140 global mask
141 with? mask = SolidRect width: w, height: h, color: 0x66000000 141 with? mask := SolidRect width: w, height: h, color: 0x66000000
142 .touchEnabled = true 142 .touchEnabled = true
143 .swallowTouches = true 143 .swallowTouches = true
144 144
145do 145do
146 with? mask = SolidRect width: w, height: h, color: 0x66000000 146 with? mask := SolidRect width: w, height: h, color: 0x66000000
147 .touchEnabled = true 147 .touchEnabled = true
148 .swallowTouches = true 148 .swallowTouches = true
149 149
@@ -161,4 +161,13 @@ do
161 if .v 161 if .v
162 break .a 162 break .a
163 163
164 a = while true
165 break with? tb
166 break 1
167
168do
169 a = for i = 1, 100
170 with? x := tb[i]
171 break x if .id := 1
172
164nil 173nil
diff --git a/spec/outputs/5.1/loops.lua b/spec/outputs/5.1/loops.lua
index bc720f6..e4f2871 100644
--- a/spec/outputs/5.1/loops.lua
+++ b/spec/outputs/5.1/loops.lua
@@ -587,3 +587,31 @@ do
587 end 587 end
588 list = _accum_0 588 list = _accum_0
589end 589end
590do
591 repeat
592 print(1)
593 until true
594 do
595 local _accum_0
596 repeat
597 a = func()
598 _accum_0 = a.x
599 break
600 until a.v
601 x = _accum_0
602 end
603 local items
604 local _accum_0 = { }
605 local _len_0 = 1
606 repeat
607 local item = getItem()
608 if not item then
609 break
610 end
611 if item.value > 0 then
612 _accum_0[_len_0] = item
613 _len_0 = _len_0 + 1
614 end
615 until false
616 items = _accum_0
617end
diff --git a/spec/outputs/5.1/try_catch.lua b/spec/outputs/5.1/try_catch.lua
index efd92c6..d2b58bc 100644
--- a/spec/outputs/5.1/try_catch.lua
+++ b/spec/outputs/5.1/try_catch.lua
@@ -22,6 +22,43 @@ end
22local _anon_func_7 = function(a, b, c, tb) 22local _anon_func_7 = function(a, b, c, tb)
23 return tb.f(a, b, c) 23 return tb.f(a, b, c)
24end 24end
25local _anon_func_8 = function(_arg_0, ...)
26 local ok = _arg_0
27 return ...
28end
29local _anon_func_10 = function(_arg_0, ...)
30 local _ok_0 = _arg_0
31 if _ok_0 then
32 return ...
33 end
34end
35local _anon_func_9 = function(func, pcall)
36 return _anon_func_10(pcall(func))
37end
38local _anon_func_12 = function(_arg_0, ...)
39 local _ok_0 = _arg_0
40 if _ok_0 then
41 return ...
42 end
43end
44local _anon_func_11 = function(func, pcall)
45 return _anon_func_12(pcall(func))
46end
47local _anon_func_14 = function(_arg_0, ...)
48 local _ok_0 = _arg_0
49 if _ok_0 then
50 return ...
51 end
52end
53local _anon_func_13 = function(func, print, xpcall)
54 return _anon_func_14(xpcall(function()
55 print(123)
56 return func()
57 end, function(e)
58 print(e)
59 return e
60 end))
61end
25local f 62local f
26f = function() 63f = function()
27 xpcall(function() 64 xpcall(function()
@@ -104,10 +141,236 @@ f = function()
104 do 141 do
105 x(function() 142 x(function()
106 local tb, a, b, c 143 local tb, a, b, c
107 f = function() 144 local f1
145 f1 = function()
108 return pcall(_anon_func_7, a, b, c, tb) 146 return pcall(_anon_func_7, a, b, c, tb)
109 end 147 end
110 end) 148 end)
111 end 149 end
150 do
151 local f1
152 f1 = function()
153 do
154 return _anon_func_8(pcall(function()
155 return func()
156 end))
157 end
158 end
159 end
160 do
161 local func
162 local a, b, c
163 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(func)
164 if _ok_0 then
165 a, b, c = _ret_0, _ret_1, _ret_2
166 end
167 end
168 do
169 local a, b, c
170 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
171 return func()
172 end)
173 if _ok_0 then
174 a, b, c = _ret_0, _ret_1, _ret_2
175 end
176 end
177 do
178 local a
179 local _exp_0 = (_anon_func_9(func, pcall))
180 if _exp_0 ~= nil then
181 a = _exp_0
182 else
183 a = "default"
184 end
185 end
186 do
187 f(_anon_func_11(func, pcall))
188 end
189 do
190 f(_anon_func_13(func, print, xpcall))
191 end
112 return nil 192 return nil
113end 193end
194local _anon_func_15 = function(a, b, c, tb)
195 return tb.f(a, b, c)
196end
197local _anon_func_16 = function(_arg_0, ...)
198 local ok = _arg_0
199 return ...
200end
201do
202 xpcall(function()
203 return func(1, 2, 3)
204 end, function(err)
205 return print(err)
206 end)
207 xpcall(function()
208 return func(1, 2, 3)
209 end, function(err)
210 return print(err)
211 end)
212 pcall(function()
213 print("trying")
214 return func(1, 2, 3)
215 end)
216 do
217 local success, result = xpcall(function()
218 return func(1, 2, 3)
219 end, function(err)
220 return print(err)
221 end)
222 success, result = pcall(function()
223 return func(1, 2, 3)
224 end)
225 end
226 local tb = { }
227 pcall(function()
228 return tb.func
229 end)
230 pcall(function()
231 return tb.func()
232 end)
233 pcall(function()
234 return tb.func()
235 end)
236 pcall(function()
237 return (tb.func())
238 end)
239 pcall(function()
240 return (tb:func(1, 2, 3))
241 end)
242 pcall(function()
243 return tb.func(1)
244 end)
245 pcall(function()
246 return tb.func(1)
247 end)
248 if (xpcall(function()
249 return func(1)
250 end, function(err)
251 return print(err)
252 end)) then
253 print("OK")
254 end
255 if xpcall(function()
256 return (func(1))
257 end, function(err)
258 return print(err)
259 end) then
260 print("OK")
261 end
262 do
263 do
264 local success, result = pcall(function()
265 return func("abc", 123)
266 end)
267 if success then
268 print(result)
269 end
270 end
271 local success, result = xpcall(function()
272 return func("abc", 123)
273 end, function(err)
274 return print(err)
275 end)
276 success, result = xpcall(function()
277 return func("abc", 123)
278 end, function(err)
279 return print(err)
280 end)
281 if success then
282 print(result)
283 end
284 end
285 do
286 pcall(function()
287 return func(1, 2, 3)
288 end)
289 pcall(function()
290 return func(1, 2, 3)
291 end)
292 end
293 do
294 x(function()
295 local tb, a, b, c
296 local f1
297 f1 = function()
298 return pcall(_anon_func_15, a, b, c, tb)
299 end
300 end)
301 end
302 do
303 local f1
304 f1 = function()
305 do
306 return _anon_func_16(pcall(function()
307 return func()
308 end))
309 end
310 end
311 end
312 do
313 local func
314 local a, b, c
315 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(func)
316 if _ok_0 then
317 a, b, c = _ret_0, _ret_1, _ret_2
318 end
319 end
320 do
321 local a, b, c
322 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
323 return func()
324 end)
325 if _ok_0 then
326 a, b, c = _ret_0, _ret_1, _ret_2
327 end
328 end
329 do
330 local a
331 local _exp_0 = ((function()
332 return (function(_arg_0, ...)
333 local _ok_0 = _arg_0
334 if _ok_0 then
335 return ...
336 end
337 end)(pcall(function()
338 return func()
339 end))
340 end)())
341 if _exp_0 ~= nil then
342 a = _exp_0
343 else
344 a = "default"
345 end
346 end
347 do
348 f((function()
349 return (function(_arg_0, ...)
350 local _ok_0 = _arg_0
351 if _ok_0 then
352 return ...
353 end
354 end)(pcall(function()
355 return func()
356 end))
357 end)())
358 end
359 do
360 f((function()
361 return (function(_arg_0, ...)
362 local _ok_0 = _arg_0
363 if _ok_0 then
364 return ...
365 end
366 end)(xpcall(function()
367 print(123)
368 return func()
369 end, function(e)
370 print(e)
371 return e
372 end))
373 end)())
374 end
375end
376return nil
diff --git a/spec/outputs/assign.lua b/spec/outputs/assign.lua
index 162c5a8..89c5f8a 100644
--- a/spec/outputs/assign.lua
+++ b/spec/outputs/assign.lua
@@ -43,10 +43,8 @@ do
43 end 43 end
44end 44end
45local _anon_func_0 = function(print) 45local _anon_func_0 = function(print)
46 do 46 print(123)
47 print(123) 47 return { }
48 return { }
49 end
50end 48end
51return _(function() 49return _(function()
52 setmetatable(a, _anon_func_0(print)) 50 setmetatable(a, _anon_func_0(print))
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index 3ee20bb..de5abdd 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -73,8 +73,8 @@ local apple = setmetatable({
73if (getmetatable(apple) ~= nil) then 73if (getmetatable(apple) ~= nil) then
74 p(apple.size, apple.color, getmetatable(apple).__index) 74 p(apple.size, apple.color, getmetatable(apple).__index)
75end 75end
76local _ud83c_udf1b = "月之脚本" 76local _u1f31b = "月之脚本"
77_module_0["🌛"] = _ud83c_udf1b 77_module_0["🌛"] = _u1f31b
78return _module_0 78return _module_0
79local area = 6.2831853071796 * 5 79local area = 6.2831853071796 * 5
80print('hello world') 80print('hello world')
@@ -109,6 +109,12 @@ end
109print("yuescript") 109print("yuescript")
110print(3) 110print(3)
111print("Valid enum type:", "Static") 111print("Valid enum type:", "Static")
112do
113 print(123, "hello")
114end
115do
116 print(123, "hello")
117end
112if tb ~= nil then 118if tb ~= nil then
113 tb:func() 119 tb:func()
114end 120end
@@ -141,6 +147,21 @@ print((function()
141end)()) 147end)())
142local tab = { } 148local tab = { }
143tab[#tab + 1] = "Value" 149tab[#tab + 1] = "Value"
150local tbA = {
151 1,
152 2,
153 3
154}
155local tbB = {
156 4,
157 5,
158 6
159}
160local _len_0 = #tbA + 1
161for _index_0 = 1, #tbB do
162 local _elm_0 = tbB[_index_0]
163 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
164end
144local parts = { 165local parts = {
145 "shoulders", 166 "shoulders",
146 "knees" 167 "knees"
@@ -209,6 +230,18 @@ for _key_0, _value_0 in pairs(b) do
209 end 230 end
210end 231end
211merge = _tab_0 232merge = _tab_0
233local last
234do
235 local _item_0 = data.items
236 last = _item_0[#_item_0]
237end
238local second_last
239do
240 local _item_0 = data.items
241 second_last = _item_0[#_item_0 - 1]
242end
243local _obj_0 = data.items
244_obj_0[#_obj_0] = 1
212local mt = { } 245local mt = { }
213local add 246local add
214add = function(self, right) 247add = function(self, right)
@@ -339,6 +372,14 @@ func({
339 2, 372 2,
340 3 373 3
341}) 374})
375local f
376f = function()
377 return {
378 1,
379 2,
380 3
381 }
382end
342local tb = { 383local tb = {
343 name = "abc", 384 name = "abc",
344 values = { 385 values = {
@@ -579,6 +620,59 @@ end
579local two, four 620local two, four
580local _obj_0 = items 621local _obj_0 = items
581two, four = _obj_0[2], _obj_0[4] 622two, four = _obj_0[2], _obj_0[4]
623local orders = {
624 "first",
625 "second",
626 "third",
627 "fourth",
628 "last"
629}
630local first, bulk, last = orders[1], (function()
631 local _accum_0 = { }
632 local _len_0 = 1
633 local _max_0 = #orders + -2 + 1
634 for _index_0 = 2, _max_0 do
635 local _item_0 = orders[_index_0]
636 _accum_0[_len_0] = _item_0
637 _len_0 = _len_0 + 1
638 end
639 return _accum_0
640end)(), orders[#orders]
641print(first)
642print(bulk)
643print(last)
644local first, rest
645do
646 local _obj_0 = orders
647 first, rest = _obj_0[1], (function()
648 local _accum_0 = { }
649 local _len_0 = 1
650 local _max_0 = #_obj_0
651 for _index_0 = 2, _max_0 do
652 local _item_0 = _obj_0[_index_0]
653 _accum_0[_len_0] = _item_0
654 _len_0 = _len_0 + 1
655 end
656 return _accum_0
657 end)()
658end
659local start, last
660do
661 local _obj_0 = orders
662 start, last = (function()
663 local _accum_0 = { }
664 local _len_0 = 1
665 local _max_0 = #_obj_0 + -2 + 1
666 for _index_0 = 1, _max_0 do
667 local _item_0 = _obj_0[_index_0]
668 _accum_0[_len_0] = _item_0
669 _len_0 = _len_0 + 1
670 end
671 return _accum_0
672 end)(), _obj_0[#_obj_0]
673end
674local _obj_0 = orders
675first, last = _obj_0[1], _obj_0[#_obj_0]
582local tuples = { 676local tuples = {
583 { 677 {
584 "hello", 678 "hello",
@@ -643,6 +737,36 @@ end
643 local first = select(1, ...) 737 local first = select(1, ...)
644 return print(ok, count, first) 738 return print(ok, count, first)
645end)(fn(true)) 739end)(fn(true))
740local f
741f = function(...)
742 local t = {
743 n = select("#", ...),
744 ...
745 }
746 print("argument count:", t.n)
747 print("table length:", #t)
748 for i = 1, t.n do
749 print(t[i])
750 end
751end
752f(1, 2, 3)
753f("a", "b", "c", "d")
754f()
755local process
756process = function(...)
757 local args = {
758 n = select("#", ...),
759 ...
760 }
761 local sum = 0
762 for i = 1, args.n do
763 if args[i] ~= nil and type(args[i]) == "number" then
764 sum = sum + args[i]
765 end
766 end
767 return sum
768end
769process(1, nil, 3, nil, 5)
646Rx.Observable.fromRange(1, 8):filter(function(x) 770Rx.Observable.fromRange(1, 8):filter(function(x)
647 return x % 2 == 0 771 return x % 2 == 0
648end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) 772end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
@@ -680,6 +804,56 @@ end)
680if success then 804if success then
681 print(result) 805 print(result)
682end 806end
807local a, b, c
808do
809 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
810 return func()
811 end)
812 if _ok_0 then
813 a, b, c = _ret_0, _ret_1, _ret_2
814 end
815end
816do
817 local _exp_0 = ((function()
818 return (function(_arg_0, ...)
819 local _ok_0 = _arg_0
820 if _ok_0 then
821 return ...
822 end
823 end)(pcall(function()
824 return func()
825 end))
826 end)())
827 if _exp_0 ~= nil then
828 a = _exp_0
829 else
830 a = "default"
831 end
832end
833f((function()
834 return (function(_arg_0, ...)
835 local _ok_0 = _arg_0
836 if _ok_0 then
837 return ...
838 end
839 end)(pcall(function()
840 return func()
841 end))
842end)())
843f((function()
844 return (function(_arg_0, ...)
845 local _ok_0 = _arg_0
846 if _ok_0 then
847 return ...
848 end
849 end)(xpcall(function()
850 print(123)
851 return func()
852 end, function(e)
853 print(e)
854 return e
855 end))
856end)())
683local a <const> = 123 857local a <const> = 123
684local _ <close> = setmetatable({ }, { 858local _ <close> = setmetatable({ }, {
685 __close = function() 859 __close = function()
@@ -695,6 +869,13 @@ print("I am " .. tostring(math.random() * 100) .. "% sure.")
695local integer = 1000000 869local integer = 1000000
696local hex = 0xEFBBBF 870local hex = 0xEFBBBF
697local binary = 19 871local binary = 19
872local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
873local fn
874fn = function()
875 local str = "foo:\n bar: baz"
876 return str
877end
878local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
698local my_function 879local my_function
699my_function = function() end 880my_function = function() end
700my_function() 881my_function()
@@ -789,6 +970,36 @@ if func(1, 2, 3, "hello", "world") then
789 print("hello") 970 print("hello")
790 print("I am inside if") 971 print("I am inside if")
791end 972end
973local f1
974f1 = function(_arg_0)
975 local a, b, c
976 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
977 return print(a, b, c)
978end
979f1({
980 a = 1,
981 b = "2",
982 c = { }
983})
984local f2
985f2 = function(_arg_0, c)
986 local a1, b
987 a1, b = _arg_0.a, _arg_0.b
988 if a1 == nil then
989 a1 = 123
990 end
991 if b == nil then
992 b = 'abc'
993 end
994 if c == nil then
995 c = { }
996 end
997 return print(a1, b, c)
998end
999local arg1 = {
1000 a = 0
1001}
1002f2(arg1, arg2)
792f(function() 1003f(function()
793 return print("hello") 1004 return print("hello")
794end) 1005end)
@@ -914,6 +1125,28 @@ for _index_0 = 1, #_list_0 do
914 _len_0 = _len_0 + 1 1125 _len_0 = _len_0 + 1
915end 1126end
916doubled = _accum_0 1127doubled = _accum_0
1128local data = {
1129 a = {
1130 1,
1131 2,
1132 3
1133 },
1134 b = {
1135 4,
1136 5,
1137 6
1138 }
1139}
1140local flat
1141local _accum_0 = { }
1142for k, v in pairs(data) do
1143 local _len_0 = #_accum_0 + 1
1144 for _index_0 = 1, #v do
1145 local _elm_0 = v[_index_0]
1146 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
1147 end
1148end
1149flat = _accum_0
917local x_coords = { 1150local x_coords = {
918 4, 1151 4,
919 5, 1152 5,
@@ -1004,8 +1237,18 @@ local slice
1004local _accum_0 = { } 1237local _accum_0 = { }
1005local _len_0 = 1 1238local _len_0 = 1
1006local _list_0 = items 1239local _list_0 = items
1007local _max_0 = 5 1240for _index_0 = 1, 5 do
1008for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do 1241 local item = _list_0[_index_0]
1242 _accum_0[_len_0] = item
1243 _len_0 = _len_0 + 1
1244end
1245slice = _accum_0
1246local slice
1247local _accum_0 = { }
1248local _len_0 = 1
1249local _list_0 = items
1250local _max_0 = #_list_0
1251for _index_0 = 2, _max_0 do
1009 local item = _list_0[_index_0] 1252 local item = _list_0[_index_0]
1010 _accum_0[_len_0] = item 1253 _accum_0[_len_0] = item
1011 _len_0 = _len_0 + 1 1254 _len_0 = _len_0 + 1
@@ -1015,7 +1258,8 @@ local slice
1015local _accum_0 = { } 1258local _accum_0 = { }
1016local _len_0 = 1 1259local _len_0 = 1
1017local _list_0 = items 1260local _list_0 = items
1018for _index_0 = 2, #_list_0 do 1261local _max_0 = #_list_0
1262for _index_0 = 1, _max_0, 2 do
1019 local item = _list_0[_index_0] 1263 local item = _list_0[_index_0]
1020 _accum_0[_len_0] = item 1264 _accum_0[_len_0] = item
1021 _len_0 = _len_0 + 1 1265 _len_0 = _len_0 + 1
@@ -1025,12 +1269,35 @@ local slice
1025local _accum_0 = { } 1269local _accum_0 = { }
1026local _len_0 = 1 1270local _len_0 = 1
1027local _list_0 = items 1271local _list_0 = items
1028for _index_0 = 1, #_list_0, 2 do 1272local _min_0 = #_list_0 + -4 + 1
1273local _max_0 = #_list_0 + -1 + 1
1274for _index_0 = _min_0, _max_0 do
1029 local item = _list_0[_index_0] 1275 local item = _list_0[_index_0]
1030 _accum_0[_len_0] = item 1276 _accum_0[_len_0] = item
1031 _len_0 = _len_0 + 1 1277 _len_0 = _len_0 + 1
1032end 1278end
1033slice = _accum_0 1279slice = _accum_0
1280local reverse_slice
1281local _accum_0 = { }
1282local _len_0 = 1
1283local _list_0 = items
1284local _min_0 = #_list_0 + -1 + 1
1285for _index_0 = _min_0, 1, -1 do
1286 local item = _list_0[_index_0]
1287 _accum_0[_len_0] = item
1288 _len_0 = _len_0 + 1
1289end
1290reverse_slice = _accum_0
1291local sub_list
1292local _accum_0 = { }
1293local _len_0 = 1
1294local _list_0 = items
1295for _index_0 = 2, 4 do
1296 local _item_0 = _list_0[_index_0]
1297 _accum_0[_len_0] = _item_0
1298 _len_0 = _len_0 + 1
1299end
1300sub_list = _accum_0
1034for i = 10, 20 do 1301for i = 10, 20 do
1035 print(i) 1302 print(i)
1036end 1303end
@@ -1041,8 +1308,7 @@ for key, value in pairs(object) do
1041 print(key, value) 1308 print(key, value)
1042end 1309end
1043local _list_0 = items 1310local _list_0 = items
1044local _max_0 = 4 1311for _index_0 = 2, 4 do
1045for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
1046 local item = _list_0[_index_0] 1312 local item = _list_0[_index_0]
1047 print(item) 1313 print(item)
1048end 1314end
@@ -1226,7 +1492,7 @@ if "Robert" == name then
1226elseif "Dan" == name or "Daniel" == name then 1492elseif "Dan" == name or "Daniel" == name then
1227 print("Your name, it's Dan") 1493 print("Your name, it's Dan")
1228else 1494else
1229 print("I don't know about your name") 1495 print("I don't know about you with name " .. tostring(name))
1230end 1496end
1231local b = 1 1497local b = 1
1232local next_number 1498local next_number
@@ -1483,6 +1749,35 @@ if _tab_0 then
1483 print("matched", fourth) 1749 print("matched", fourth)
1484 end 1750 end
1485end 1751end
1752local segments = {
1753 "admin",
1754 "users",
1755 "logs",
1756 "view"
1757}
1758local _type_0 = type(segments)
1759local _tab_0 = "table" == _type_0 or "userdata" == _type_0
1760if _tab_0 then
1761 local groups
1762 do
1763 local _accum_0 = { }
1764 local _len_0 = 1
1765 local _max_0 = #segments + -3 + 1
1766 for _index_0 = 1, _max_0 do
1767 local _item_0 = segments[_index_0]
1768 _accum_0[_len_0] = _item_0
1769 _len_0 = _len_0 + 1
1770 end
1771 groups = _accum_0
1772 end
1773 local resource = segments[#segments - 1]
1774 local action = segments[#segments]
1775 if resource ~= nil and action ~= nil then
1776 print("Group:", groups)
1777 print("Resource:", resource)
1778 print("Action:", action)
1779 end
1780end
1486local Inventory 1781local Inventory
1487local _class_0 1782local _class_0
1488local _base_0 = { 1783local _base_0 = {
@@ -2139,6 +2434,10 @@ do
2139 _with_1["key-name"] = value 2434 _with_1["key-name"] = value
2140end 2435end
2141_with_0[#_with_0 + 1] = "abc" 2436_with_0[#_with_0 + 1] = "abc"
2437local _with_0 = obj
2438if _with_0 ~= nil then
2439 print(obj.name)
2440end
2142do 2441do
2143 local var = "hello" 2442 local var = "hello"
2144 print(var) 2443 print(var)
@@ -2282,8 +2581,8 @@ local apple = setmetatable({
2282if (getmetatable(apple) ~= nil) then 2581if (getmetatable(apple) ~= nil) then
2283 p(apple.size, apple.color, getmetatable(apple).__index) 2582 p(apple.size, apple.color, getmetatable(apple).__index)
2284end 2583end
2285local _ud83c_udf1b = "月之脚本" 2584local _u1f31b = "月之脚本"
2286_module_0["🌛"] = _ud83c_udf1b 2585_module_0["🌛"] = _u1f31b
2287return _module_0 2586return _module_0
2288local area = 6.2831853071796 * 5 2587local area = 6.2831853071796 * 5
2289print('hello world') 2588print('hello world')
@@ -2318,6 +2617,12 @@ end
2318print("yuescript") 2617print("yuescript")
2319print(3) 2618print(3)
2320print("Valid enum type:", "Static") 2619print("Valid enum type:", "Static")
2620do
2621 print(123, "hello")
2622end
2623do
2624 print(123, "hello")
2625end
2321if tb ~= nil then 2626if tb ~= nil then
2322 tb:func() 2627 tb:func()
2323end 2628end
@@ -2350,6 +2655,21 @@ print((function()
2350end)()) 2655end)())
2351local tab = { } 2656local tab = { }
2352tab[#tab + 1] = "Value" 2657tab[#tab + 1] = "Value"
2658local tbA = {
2659 1,
2660 2,
2661 3
2662}
2663local tbB = {
2664 4,
2665 5,
2666 6
2667}
2668local _len_0 = #tbA + 1
2669for _index_0 = 1, #tbB do
2670 local _elm_0 = tbB[_index_0]
2671 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
2672end
2353local parts = { 2673local parts = {
2354 "shoulders", 2674 "shoulders",
2355 "knees" 2675 "knees"
@@ -2418,6 +2738,18 @@ for _key_0, _value_0 in pairs(b) do
2418 end 2738 end
2419end 2739end
2420merge = _tab_0 2740merge = _tab_0
2741local last
2742do
2743 local _item_0 = data.items
2744 last = _item_0[#_item_0]
2745end
2746local second_last
2747do
2748 local _item_0 = data.items
2749 second_last = _item_0[#_item_0 - 1]
2750end
2751local _obj_0 = data.items
2752_obj_0[#_obj_0] = 1
2421local mt = { } 2753local mt = { }
2422local add 2754local add
2423add = function(self, right) 2755add = function(self, right)
@@ -2548,6 +2880,14 @@ func({
2548 2, 2880 2,
2549 3 2881 3
2550}) 2882})
2883local f
2884f = function()
2885 return {
2886 1,
2887 2,
2888 3
2889 }
2890end
2551local tb = { 2891local tb = {
2552 name = "abc", 2892 name = "abc",
2553 values = { 2893 values = {
@@ -2788,6 +3128,59 @@ end
2788local two, four 3128local two, four
2789local _obj_0 = items 3129local _obj_0 = items
2790two, four = _obj_0[2], _obj_0[4] 3130two, four = _obj_0[2], _obj_0[4]
3131local orders = {
3132 "first",
3133 "second",
3134 "third",
3135 "fourth",
3136 "last"
3137}
3138local first, bulk, last = orders[1], (function()
3139 local _accum_0 = { }
3140 local _len_0 = 1
3141 local _max_0 = #orders + -2 + 1
3142 for _index_0 = 2, _max_0 do
3143 local _item_0 = orders[_index_0]
3144 _accum_0[_len_0] = _item_0
3145 _len_0 = _len_0 + 1
3146 end
3147 return _accum_0
3148end)(), orders[#orders]
3149print(first)
3150print(bulk)
3151print(last)
3152local first, rest
3153do
3154 local _obj_0 = orders
3155 first, rest = _obj_0[1], (function()
3156 local _accum_0 = { }
3157 local _len_0 = 1
3158 local _max_0 = #_obj_0
3159 for _index_0 = 2, _max_0 do
3160 local _item_0 = _obj_0[_index_0]
3161 _accum_0[_len_0] = _item_0
3162 _len_0 = _len_0 + 1
3163 end
3164 return _accum_0
3165 end)()
3166end
3167local start, last
3168do
3169 local _obj_0 = orders
3170 start, last = (function()
3171 local _accum_0 = { }
3172 local _len_0 = 1
3173 local _max_0 = #_obj_0 + -2 + 1
3174 for _index_0 = 1, _max_0 do
3175 local _item_0 = _obj_0[_index_0]
3176 _accum_0[_len_0] = _item_0
3177 _len_0 = _len_0 + 1
3178 end
3179 return _accum_0
3180 end)(), _obj_0[#_obj_0]
3181end
3182local _obj_0 = orders
3183first, last = _obj_0[1], _obj_0[#_obj_0]
2791local tuples = { 3184local tuples = {
2792 { 3185 {
2793 "hello", 3186 "hello",
@@ -2852,6 +3245,36 @@ end
2852 local first = select(1, ...) 3245 local first = select(1, ...)
2853 return print(ok, count, first) 3246 return print(ok, count, first)
2854end)(fn(true)) 3247end)(fn(true))
3248local f
3249f = function(...)
3250 local t = {
3251 n = select("#", ...),
3252 ...
3253 }
3254 print("argument count:", t.n)
3255 print("table length:", #t)
3256 for i = 1, t.n do
3257 print(t[i])
3258 end
3259end
3260f(1, 2, 3)
3261f("a", "b", "c", "d")
3262f()
3263local process
3264process = function(...)
3265 local args = {
3266 n = select("#", ...),
3267 ...
3268 }
3269 local sum = 0
3270 for i = 1, args.n do
3271 if args[i] ~= nil and type(args[i]) == "number" then
3272 sum = sum + args[i]
3273 end
3274 end
3275 return sum
3276end
3277process(1, nil, 3, nil, 5)
2855Rx.Observable.fromRange(1, 8):filter(function(x) 3278Rx.Observable.fromRange(1, 8):filter(function(x)
2856 return x % 2 == 0 3279 return x % 2 == 0
2857end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) 3280end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
@@ -2889,6 +3312,56 @@ end)
2889if success then 3312if success then
2890 print(result) 3313 print(result)
2891end 3314end
3315local a, b, c
3316do
3317 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
3318 return func()
3319 end)
3320 if _ok_0 then
3321 a, b, c = _ret_0, _ret_1, _ret_2
3322 end
3323end
3324do
3325 local _exp_0 = ((function()
3326 return (function(_arg_0, ...)
3327 local _ok_0 = _arg_0
3328 if _ok_0 then
3329 return ...
3330 end
3331 end)(pcall(function()
3332 return func()
3333 end))
3334 end)())
3335 if _exp_0 ~= nil then
3336 a = _exp_0
3337 else
3338 a = "default"
3339 end
3340end
3341f((function()
3342 return (function(_arg_0, ...)
3343 local _ok_0 = _arg_0
3344 if _ok_0 then
3345 return ...
3346 end
3347 end)(pcall(function()
3348 return func()
3349 end))
3350end)())
3351f((function()
3352 return (function(_arg_0, ...)
3353 local _ok_0 = _arg_0
3354 if _ok_0 then
3355 return ...
3356 end
3357 end)(xpcall(function()
3358 print(123)
3359 return func()
3360 end, function(e)
3361 print(e)
3362 return e
3363 end))
3364end)())
2892local a <const> = 123 3365local a <const> = 123
2893local _ <close> = setmetatable({ }, { 3366local _ <close> = setmetatable({ }, {
2894 __close = function() 3367 __close = function()
@@ -2903,6 +3376,14 @@ local some_string = "Here is a string\n that has a line break in it."
2903print("I am " .. tostring(math.random() * 100) .. "% sure.") 3376print("I am " .. tostring(math.random() * 100) .. "% sure.")
2904local integer = 1000000 3377local integer = 1000000
2905local hex = 0xEFBBBF 3378local hex = 0xEFBBBF
3379local binary = 19
3380local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
3381local fn
3382fn = function()
3383 local str = "foo:\n bar: baz"
3384 return str
3385end
3386local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
2906local my_function 3387local my_function
2907my_function = function() end 3388my_function = function() end
2908my_function() 3389my_function()
@@ -2997,6 +3478,66 @@ if func(1, 2, 3, "hello", "world") then
2997 print("hello") 3478 print("hello")
2998 print("I am inside if") 3479 print("I am inside if")
2999end 3480end
3481local f1
3482f1 = function(_arg_0)
3483 local a, b, c
3484 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
3485 return print(a, b, c)
3486end
3487f1({
3488 a = 1,
3489 b = "2",
3490 c = { }
3491})
3492local f2
3493f2 = function(_arg_0, c)
3494 local a1, b
3495 a1, b = _arg_0.a, _arg_0.b
3496 if a1 == nil then
3497 a1 = 123
3498 end
3499 if b == nil then
3500 b = 'abc'
3501 end
3502 if c == nil then
3503 c = { }
3504 end
3505end
3506print(a1, b, c)
3507local arg1 = {
3508 a = 0
3509}
3510f2(arg1, arg2)
3511local findFirstEven
3512findFirstEven = function(list)
3513 for _index_0 = 1, #list do
3514 local item = list[_index_0]
3515 if type(item) == "table" then
3516 for _index_1 = 1, #item do
3517 local sub = item[_index_1]
3518 if sub % 2 == 0 then
3519 return sub
3520 end
3521 end
3522 end
3523 end
3524 return nil
3525end
3526local findFirstEven
3527findFirstEven = function(list)
3528 for _index_0 = 1, #list do
3529 local item = list[_index_0]
3530 if type(item) == "table" then
3531 for _index_1 = 1, #item do
3532 local sub = item[_index_1]
3533 if sub % 2 == 0 then
3534 return sub
3535 end
3536 end
3537 end
3538 end
3539 return nil
3540end
3000f(function() 3541f(function()
3001 return print("hello") 3542 return print("hello")
3002end) 3543end)
@@ -3122,6 +3663,28 @@ for _index_0 = 1, #_list_0 do
3122 _len_0 = _len_0 + 1 3663 _len_0 = _len_0 + 1
3123end 3664end
3124doubled = _accum_0 3665doubled = _accum_0
3666local data = {
3667 a = {
3668 1,
3669 2,
3670 3
3671 },
3672 b = {
3673 4,
3674 5,
3675 6
3676 }
3677}
3678local flat
3679local _accum_0 = { }
3680for k, v in pairs(data) do
3681 local _len_0 = #_accum_0 + 1
3682 for _index_0 = 1, #v do
3683 local _elm_0 = v[_index_0]
3684 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
3685 end
3686end
3687flat = _accum_0
3125local x_coords = { 3688local x_coords = {
3126 4, 3689 4,
3127 5, 3690 5,
@@ -3212,8 +3775,7 @@ local slice
3212local _accum_0 = { } 3775local _accum_0 = { }
3213local _len_0 = 1 3776local _len_0 = 1
3214local _list_0 = items 3777local _list_0 = items
3215local _max_0 = 5 3778for _index_0 = 1, 5 do
3216for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3217 local item = _list_0[_index_0] 3779 local item = _list_0[_index_0]
3218 _accum_0[_len_0] = item 3780 _accum_0[_len_0] = item
3219 _len_0 = _len_0 + 1 3781 _len_0 = _len_0 + 1
@@ -3223,7 +3785,8 @@ local slice
3223local _accum_0 = { } 3785local _accum_0 = { }
3224local _len_0 = 1 3786local _len_0 = 1
3225local _list_0 = items 3787local _list_0 = items
3226for _index_0 = 2, #_list_0 do 3788local _max_0 = #_list_0
3789for _index_0 = 2, _max_0 do
3227 local item = _list_0[_index_0] 3790 local item = _list_0[_index_0]
3228 _accum_0[_len_0] = item 3791 _accum_0[_len_0] = item
3229 _len_0 = _len_0 + 1 3792 _len_0 = _len_0 + 1
@@ -3233,12 +3796,46 @@ local slice
3233local _accum_0 = { } 3796local _accum_0 = { }
3234local _len_0 = 1 3797local _len_0 = 1
3235local _list_0 = items 3798local _list_0 = items
3236for _index_0 = 1, #_list_0, 2 do 3799local _max_0 = #_list_0
3800for _index_0 = 1, _max_0, 2 do
3237 local item = _list_0[_index_0] 3801 local item = _list_0[_index_0]
3238 _accum_0[_len_0] = item 3802 _accum_0[_len_0] = item
3239 _len_0 = _len_0 + 1 3803 _len_0 = _len_0 + 1
3240end 3804end
3241slice = _accum_0 3805slice = _accum_0
3806local slice
3807local _accum_0 = { }
3808local _len_0 = 1
3809local _list_0 = items
3810local _min_0 = #_list_0 + -4 + 1
3811local _max_0 = #_list_0 + -1 + 1
3812for _index_0 = _min_0, _max_0 do
3813 local item = _list_0[_index_0]
3814 _accum_0[_len_0] = item
3815 _len_0 = _len_0 + 1
3816end
3817slice = _accum_0
3818local reverse_slice
3819local _accum_0 = { }
3820local _len_0 = 1
3821local _list_0 = items
3822local _min_0 = #_list_0 + -1 + 1
3823for _index_0 = _min_0, 1, -1 do
3824 local item = _list_0[_index_0]
3825 _accum_0[_len_0] = item
3826 _len_0 = _len_0 + 1
3827end
3828reverse_slice = _accum_0
3829local sub_list
3830local _accum_0 = { }
3831local _len_0 = 1
3832local _list_0 = items
3833for _index_0 = 2, 4 do
3834 local _item_0 = _list_0[_index_0]
3835 _accum_0[_len_0] = _item_0
3836 _len_0 = _len_0 + 1
3837end
3838sub_list = _accum_0
3242for i = 10, 20 do 3839for i = 10, 20 do
3243 print(i) 3840 print(i)
3244end 3841end
@@ -3249,8 +3846,7 @@ for key, value in pairs(object) do
3249 print(key, value) 3846 print(key, value)
3250end 3847end
3251local _list_0 = items 3848local _list_0 = items
3252local _max_0 = 4 3849for _index_0 = 2, 4 do
3253for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3254 local item = _list_0[_index_0] 3850 local item = _list_0[_index_0]
3255 print(item) 3851 print(item)
3256end 3852end
@@ -3434,7 +4030,7 @@ if "Robert" == name then
3434elseif "Dan" == name or "Daniel" == name then 4030elseif "Dan" == name or "Daniel" == name then
3435 print("Your name, it's Dan") 4031 print("Your name, it's Dan")
3436else 4032else
3437 print("I don't know about your name") 4033 print("I don't know about you with name " .. tostring(name))
3438end 4034end
3439local b = 1 4035local b = 1
3440local next_number 4036local next_number
@@ -3691,6 +4287,35 @@ if _tab_0 then
3691 print("matched", fourth) 4287 print("matched", fourth)
3692 end 4288 end
3693end 4289end
4290local segments = {
4291 "admin",
4292 "users",
4293 "logs",
4294 "view"
4295}
4296local _type_0 = type(segments)
4297local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4298if _tab_0 then
4299 local groups
4300 do
4301 local _accum_0 = { }
4302 local _len_0 = 1
4303 local _max_0 = #segments + -3 + 1
4304 for _index_0 = 1, _max_0 do
4305 local _item_0 = segments[_index_0]
4306 _accum_0[_len_0] = _item_0
4307 _len_0 = _len_0 + 1
4308 end
4309 groups = _accum_0
4310 end
4311 local resource = segments[#segments - 1]
4312 local action = segments[#segments]
4313 if resource ~= nil and action ~= nil then
4314 print("Group:", groups)
4315 print("Resource:", resource)
4316 print("Action:", action)
4317 end
4318end
3694local Inventory 4319local Inventory
3695local _class_0 4320local _class_0
3696local _base_0 = { 4321local _base_0 = {
@@ -4347,6 +4972,10 @@ do
4347 _with_1["key-name"] = value 4972 _with_1["key-name"] = value
4348end 4973end
4349_with_0[#_with_0 + 1] = "abc" 4974_with_0[#_with_0 + 1] = "abc"
4975local _with_0 = obj
4976if _with_0 ~= nil then
4977 print(obj.name)
4978end
4350do 4979do
4351 local var = "hello" 4980 local var = "hello"
4352 print(var) 4981 print(var)
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index 52204b7..6a6c38c 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -73,8 +73,8 @@ local apple = setmetatable({
73if (getmetatable(apple) ~= nil) then 73if (getmetatable(apple) ~= nil) then
74 p(apple.size, apple.color, getmetatable(apple).__index) 74 p(apple.size, apple.color, getmetatable(apple).__index)
75end 75end
76local _ud83c_udf1b = "月之脚本" 76local _u1f31b = "月之脚本"
77_module_0["🌛"] = _ud83c_udf1b 77_module_0["🌛"] = _u1f31b
78return _module_0 78return _module_0
79local area = 6.2831853071796 * 5 79local area = 6.2831853071796 * 5
80print('你好 世界') 80print('你好 世界')
@@ -109,6 +109,12 @@ end
109print("yuescript") 109print("yuescript")
110print(3) 110print(3)
111print("有效的枚举类型:", "Static") 111print("有效的枚举类型:", "Static")
112do
113 print(123, "hello")
114end
115do
116 print(123, "hello")
117end
112if tb ~= nil then 118if tb ~= nil then
113 tb:func() 119 tb:func()
114end 120end
@@ -141,6 +147,21 @@ print((function()
141end)()) 147end)())
142local tab = { } 148local tab = { }
143tab[#tab + 1] = "Value" 149tab[#tab + 1] = "Value"
150local tbA = {
151 1,
152 2,
153 3
154}
155local tbB = {
156 4,
157 5,
158 6
159}
160local _len_0 = #tbA + 1
161for _index_0 = 1, #tbB do
162 local _elm_0 = tbB[_index_0]
163 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
164end
144local parts = { 165local parts = {
145 "shoulders", 166 "shoulders",
146 "knees" 167 "knees"
@@ -209,6 +230,18 @@ for _key_0, _value_0 in pairs(b) do
209 end 230 end
210end 231end
211merge = _tab_0 232merge = _tab_0
233local last
234do
235 local _item_0 = data.items
236 last = _item_0[#_item_0]
237end
238local second_last
239do
240 local _item_0 = data.items
241 second_last = _item_0[#_item_0 - 1]
242end
243local _obj_0 = data.items
244_obj_0[#_obj_0] = 1
212local mt = { } 245local mt = { }
213local add 246local add
214add = function(self, right) 247add = function(self, right)
@@ -339,6 +372,14 @@ func({
339 2, 372 2,
340 3 373 3
341}) 374})
375local f
376f = function()
377 return {
378 1,
379 2,
380 3
381 }
382end
342local tb = { 383local tb = {
343 name = "abc", 384 name = "abc",
344 values = { 385 values = {
@@ -579,6 +620,59 @@ end
579local two, four 620local two, four
580local _obj_0 = items 621local _obj_0 = items
581two, four = _obj_0[2], _obj_0[4] 622two, four = _obj_0[2], _obj_0[4]
623local orders = {
624 "first",
625 "second",
626 "third",
627 "fourth",
628 "last"
629}
630local first, bulk, last = orders[1], (function()
631 local _accum_0 = { }
632 local _len_0 = 1
633 local _max_0 = #orders + -2 + 1
634 for _index_0 = 2, _max_0 do
635 local _item_0 = orders[_index_0]
636 _accum_0[_len_0] = _item_0
637 _len_0 = _len_0 + 1
638 end
639 return _accum_0
640end)(), orders[#orders]
641print(first)
642print(bulk)
643print(last)
644local first, rest
645do
646 local _obj_0 = orders
647 first, rest = _obj_0[1], (function()
648 local _accum_0 = { }
649 local _len_0 = 1
650 local _max_0 = #_obj_0
651 for _index_0 = 2, _max_0 do
652 local _item_0 = _obj_0[_index_0]
653 _accum_0[_len_0] = _item_0
654 _len_0 = _len_0 + 1
655 end
656 return _accum_0
657 end)()
658end
659local start, last
660do
661 local _obj_0 = orders
662 start, last = (function()
663 local _accum_0 = { }
664 local _len_0 = 1
665 local _max_0 = #_obj_0 + -2 + 1
666 for _index_0 = 1, _max_0 do
667 local _item_0 = _obj_0[_index_0]
668 _accum_0[_len_0] = _item_0
669 _len_0 = _len_0 + 1
670 end
671 return _accum_0
672 end)(), _obj_0[#_obj_0]
673end
674local _obj_0 = orders
675first, last = _obj_0[1], _obj_0[#_obj_0]
582local tuples = { 676local tuples = {
583 { 677 {
584 "hello", 678 "hello",
@@ -643,6 +737,36 @@ end
643 local first = select(1, ...) 737 local first = select(1, ...)
644 return print(ok, count, first) 738 return print(ok, count, first)
645end)(fn(true)) 739end)(fn(true))
740local f
741f = function(...)
742 local t = {
743 n = select("#", ...),
744 ...
745 }
746 print("参数个数:", t.n)
747 print("表长度:", #t)
748 for i = 1, t.n do
749 print(t[i])
750 end
751end
752f(1, 2, 3)
753f("a", "b", "c", "d")
754f()
755local process
756process = function(...)
757 local args = {
758 n = select("#", ...),
759 ...
760 }
761 local sum = 0
762 for i = 1, args.n do
763 if args[i] ~= nil and type(args[i]) == "number" then
764 sum = sum + args[i]
765 end
766 end
767 return sum
768end
769process(1, nil, 3, nil, 5)
646Rx.Observable.fromRange(1, 8):filter(function(x) 770Rx.Observable.fromRange(1, 8):filter(function(x)
647 return x % 2 == 0 771 return x % 2 == 0
648end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) 772end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
@@ -680,6 +804,56 @@ end)
680if success then 804if success then
681 print(result) 805 print(result)
682end 806end
807local a, b, c
808do
809 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
810 return func()
811 end)
812 if _ok_0 then
813 a, b, c = _ret_0, _ret_1, _ret_2
814 end
815end
816do
817 local _exp_0 = ((function()
818 return (function(_arg_0, ...)
819 local _ok_0 = _arg_0
820 if _ok_0 then
821 return ...
822 end
823 end)(pcall(function()
824 return func()
825 end))
826 end)())
827 if _exp_0 ~= nil then
828 a = _exp_0
829 else
830 a = "default"
831 end
832end
833f((function()
834 return (function(_arg_0, ...)
835 local _ok_0 = _arg_0
836 if _ok_0 then
837 return ...
838 end
839 end)(pcall(function()
840 return func()
841 end))
842end)())
843f((function()
844 return (function(_arg_0, ...)
845 local _ok_0 = _arg_0
846 if _ok_0 then
847 return ...
848 end
849 end)(xpcall(function()
850 print(123)
851 return func()
852 end, function(e)
853 print(e)
854 return e
855 end))
856end)())
683local a <const> = 123 857local a <const> = 123
684local _ <close> = setmetatable({ }, { 858local _ <close> = setmetatable({ }, {
685 __close = function() 859 __close = function()
@@ -695,6 +869,13 @@ print("我有" .. tostring(math.random() * 100) .. "%的把握。")
695local integer = 1000000 869local integer = 1000000
696local hex = 0xEFBBBF 870local hex = 0xEFBBBF
697local binary = 19 871local binary = 19
872local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
873local fn
874fn = function()
875 local str = "foo:\n bar: baz"
876 return str
877end
878local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
698local my_function 879local my_function
699my_function = function() end 880my_function = function() end
700my_function() 881my_function()
@@ -783,6 +964,36 @@ if func(1, 2, 3, "你好", "世界") then
783 print("hello") 964 print("hello")
784 print("我在if内部") 965 print("我在if内部")
785end 966end
967local f1
968f1 = function(_arg_0)
969 local a, b, c
970 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
971 return print(a, b, c)
972end
973f1({
974 a = 1,
975 b = "2",
976 c = { }
977})
978local f2
979f2 = function(_arg_0, c)
980 local a1, b
981 a1, b = _arg_0.a, _arg_0.b
982 if a1 == nil then
983 a1 = 123
984 end
985 if b == nil then
986 b = 'abc'
987 end
988 if c == nil then
989 c = { }
990 end
991 return print(a1, b, c)
992end
993local arg1 = {
994 a = 0
995}
996f2(arg1, arg2)
786f(function() 997f(function()
787 return print("hello") 998 return print("hello")
788end) 999end)
@@ -908,6 +1119,28 @@ for _index_0 = 1, #_list_0 do
908 _len_0 = _len_0 + 1 1119 _len_0 = _len_0 + 1
909end 1120end
910doubled = _accum_0 1121doubled = _accum_0
1122local data = {
1123 a = {
1124 1,
1125 2,
1126 3
1127 },
1128 b = {
1129 4,
1130 5,
1131 6
1132 }
1133}
1134local flat
1135local _accum_0 = { }
1136for k, v in pairs(data) do
1137 local _len_0 = #_accum_0 + 1
1138 for _index_0 = 1, #v do
1139 local _elm_0 = v[_index_0]
1140 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
1141 end
1142end
1143flat = _accum_0
911local x_coords = { 1144local x_coords = {
912 4, 1145 4,
913 5, 1146 5,
@@ -998,8 +1231,18 @@ local slice
998local _accum_0 = { } 1231local _accum_0 = { }
999local _len_0 = 1 1232local _len_0 = 1
1000local _list_0 = items 1233local _list_0 = items
1001local _max_0 = 5 1234for _index_0 = 1, 5 do
1002for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do 1235 local item = _list_0[_index_0]
1236 _accum_0[_len_0] = item
1237 _len_0 = _len_0 + 1
1238end
1239slice = _accum_0
1240local slice
1241local _accum_0 = { }
1242local _len_0 = 1
1243local _list_0 = items
1244local _max_0 = #_list_0
1245for _index_0 = 2, _max_0 do
1003 local item = _list_0[_index_0] 1246 local item = _list_0[_index_0]
1004 _accum_0[_len_0] = item 1247 _accum_0[_len_0] = item
1005 _len_0 = _len_0 + 1 1248 _len_0 = _len_0 + 1
@@ -1009,7 +1252,8 @@ local slice
1009local _accum_0 = { } 1252local _accum_0 = { }
1010local _len_0 = 1 1253local _len_0 = 1
1011local _list_0 = items 1254local _list_0 = items
1012for _index_0 = 2, #_list_0 do 1255local _max_0 = #_list_0
1256for _index_0 = 1, _max_0, 2 do
1013 local item = _list_0[_index_0] 1257 local item = _list_0[_index_0]
1014 _accum_0[_len_0] = item 1258 _accum_0[_len_0] = item
1015 _len_0 = _len_0 + 1 1259 _len_0 = _len_0 + 1
@@ -1019,12 +1263,35 @@ local slice
1019local _accum_0 = { } 1263local _accum_0 = { }
1020local _len_0 = 1 1264local _len_0 = 1
1021local _list_0 = items 1265local _list_0 = items
1022for _index_0 = 1, #_list_0, 2 do 1266local _min_0 = #_list_0 + -4 + 1
1267local _max_0 = #_list_0 + -1 + 1
1268for _index_0 = _min_0, _max_0 do
1023 local item = _list_0[_index_0] 1269 local item = _list_0[_index_0]
1024 _accum_0[_len_0] = item 1270 _accum_0[_len_0] = item
1025 _len_0 = _len_0 + 1 1271 _len_0 = _len_0 + 1
1026end 1272end
1027slice = _accum_0 1273slice = _accum_0
1274local reverse_slice
1275local _accum_0 = { }
1276local _len_0 = 1
1277local _list_0 = items
1278local _min_0 = #_list_0 + -1 + 1
1279for _index_0 = _min_0, 1, -1 do
1280 local item = _list_0[_index_0]
1281 _accum_0[_len_0] = item
1282 _len_0 = _len_0 + 1
1283end
1284reverse_slice = _accum_0
1285local sub_list
1286local _accum_0 = { }
1287local _len_0 = 1
1288local _list_0 = items
1289for _index_0 = 2, 4 do
1290 local _item_0 = _list_0[_index_0]
1291 _accum_0[_len_0] = _item_0
1292 _len_0 = _len_0 + 1
1293end
1294sub_list = _accum_0
1028for i = 10, 20 do 1295for i = 10, 20 do
1029 print(i) 1296 print(i)
1030end 1297end
@@ -1035,8 +1302,7 @@ for key, value in pairs(object) do
1035 print(key, value) 1302 print(key, value)
1036end 1303end
1037local _list_0 = items 1304local _list_0 = items
1038local _max_0 = 4 1305for _index_0 = 2, 4 do
1039for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
1040 local item = _list_0[_index_0] 1306 local item = _list_0[_index_0]
1041 print(item) 1307 print(item)
1042end 1308end
@@ -1220,7 +1486,7 @@ if "Robert" == name then
1220elseif "Dan" == name or "Daniel" == name then 1486elseif "Dan" == name or "Daniel" == name then
1221 print("你的名字是Dan") 1487 print("你的名字是Dan")
1222else 1488else
1223 print("我不知道你的名字") 1489 print("我不认识,你的名字" .. tostring(name))
1224end 1490end
1225local b = 1 1491local b = 1
1226local next_number 1492local next_number
@@ -1477,6 +1743,35 @@ if _tab_0 then
1477 print("匹配成功", fourth) 1743 print("匹配成功", fourth)
1478 end 1744 end
1479end 1745end
1746local segments = {
1747 "admin",
1748 "users",
1749 "logs",
1750 "view"
1751}
1752local _type_0 = type(segments)
1753local _tab_0 = "table" == _type_0 or "userdata" == _type_0
1754if _tab_0 then
1755 local groups
1756 do
1757 local _accum_0 = { }
1758 local _len_0 = 1
1759 local _max_0 = #segments + -3 + 1
1760 for _index_0 = 1, _max_0 do
1761 local _item_0 = segments[_index_0]
1762 _accum_0[_len_0] = _item_0
1763 _len_0 = _len_0 + 1
1764 end
1765 groups = _accum_0
1766 end
1767 local resource = segments[#segments - 1]
1768 local action = segments[#segments]
1769 if resource ~= nil and action ~= nil then
1770 print("Group:", groups)
1771 print("Resource:", resource)
1772 print("Action:", action)
1773 end
1774end
1480local Inventory 1775local Inventory
1481local _class_0 1776local _class_0
1482local _base_0 = { 1777local _base_0 = {
@@ -2133,6 +2428,10 @@ do
2133 _with_1["key-name"] = value 2428 _with_1["key-name"] = value
2134end 2429end
2135_with_0[#_with_0 + 1] = "abc" 2430_with_0[#_with_0 + 1] = "abc"
2431local _with_0 = obj
2432if _with_0 ~= nil then
2433 print(obj.name)
2434end
2136do 2435do
2137 local var = "hello" 2436 local var = "hello"
2138 print(var) 2437 print(var)
@@ -2276,8 +2575,8 @@ local apple = setmetatable({
2276if (getmetatable(apple) ~= nil) then 2575if (getmetatable(apple) ~= nil) then
2277 p(apple.size, apple.color, getmetatable(apple).__index) 2576 p(apple.size, apple.color, getmetatable(apple).__index)
2278end 2577end
2279local _ud83c_udf1b = "月之脚本" 2578local _u1f31b = "月之脚本"
2280_module_0["🌛"] = _ud83c_udf1b 2579_module_0["🌛"] = _u1f31b
2281return _module_0 2580return _module_0
2282local area = 6.2831853071796 * 5 2581local area = 6.2831853071796 * 5
2283print('你好 世界') 2582print('你好 世界')
@@ -2312,6 +2611,12 @@ end
2312print("yuescript") 2611print("yuescript")
2313print(3) 2612print(3)
2314print("有效的枚举类型:", "Static") 2613print("有效的枚举类型:", "Static")
2614do
2615 print(123, "hello")
2616end
2617do
2618 print(123, "hello")
2619end
2315if tb ~= nil then 2620if tb ~= nil then
2316 tb:func() 2621 tb:func()
2317end 2622end
@@ -2344,6 +2649,21 @@ print((function()
2344end)()) 2649end)())
2345local tab = { } 2650local tab = { }
2346tab[#tab + 1] = "Value" 2651tab[#tab + 1] = "Value"
2652local tbA = {
2653 1,
2654 2,
2655 3
2656}
2657local tbB = {
2658 4,
2659 5,
2660 6
2661}
2662local _len_0 = #tbA + 1
2663for _index_0 = 1, #tbB do
2664 local _elm_0 = tbB[_index_0]
2665 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
2666end
2347local parts = { 2667local parts = {
2348 "shoulders", 2668 "shoulders",
2349 "knees" 2669 "knees"
@@ -2412,6 +2732,18 @@ for _key_0, _value_0 in pairs(b) do
2412 end 2732 end
2413end 2733end
2414merge = _tab_0 2734merge = _tab_0
2735local last
2736do
2737 local _item_0 = data.items
2738 last = _item_0[#_item_0]
2739end
2740local second_last
2741do
2742 local _item_0 = data.items
2743 second_last = _item_0[#_item_0 - 1]
2744end
2745local _obj_0 = data.items
2746_obj_0[#_obj_0] = 1
2415local mt = { } 2747local mt = { }
2416local add 2748local add
2417add = function(self, right) 2749add = function(self, right)
@@ -2542,6 +2874,14 @@ func({
2542 2, 2874 2,
2543 3 2875 3
2544}) 2876})
2877local f
2878f = function()
2879 return {
2880 1,
2881 2,
2882 3
2883 }
2884end
2545local tb = { 2885local tb = {
2546 name = "abc", 2886 name = "abc",
2547 values = { 2887 values = {
@@ -2782,6 +3122,59 @@ end
2782local two, four 3122local two, four
2783local _obj_0 = items 3123local _obj_0 = items
2784two, four = _obj_0[2], _obj_0[4] 3124two, four = _obj_0[2], _obj_0[4]
3125local orders = {
3126 "first",
3127 "second",
3128 "third",
3129 "fourth",
3130 "last"
3131}
3132local first, bulk, last = orders[1], (function()
3133 local _accum_0 = { }
3134 local _len_0 = 1
3135 local _max_0 = #orders + -2 + 1
3136 for _index_0 = 2, _max_0 do
3137 local _item_0 = orders[_index_0]
3138 _accum_0[_len_0] = _item_0
3139 _len_0 = _len_0 + 1
3140 end
3141 return _accum_0
3142end)(), orders[#orders]
3143print(first)
3144print(bulk)
3145print(last)
3146local first, rest
3147do
3148 local _obj_0 = orders
3149 first, rest = _obj_0[1], (function()
3150 local _accum_0 = { }
3151 local _len_0 = 1
3152 local _max_0 = #_obj_0
3153 for _index_0 = 2, _max_0 do
3154 local _item_0 = _obj_0[_index_0]
3155 _accum_0[_len_0] = _item_0
3156 _len_0 = _len_0 + 1
3157 end
3158 return _accum_0
3159 end)()
3160end
3161local start, last
3162do
3163 local _obj_0 = orders
3164 start, last = (function()
3165 local _accum_0 = { }
3166 local _len_0 = 1
3167 local _max_0 = #_obj_0 + -2 + 1
3168 for _index_0 = 1, _max_0 do
3169 local _item_0 = _obj_0[_index_0]
3170 _accum_0[_len_0] = _item_0
3171 _len_0 = _len_0 + 1
3172 end
3173 return _accum_0
3174 end)(), _obj_0[#_obj_0]
3175end
3176local _obj_0 = orders
3177first, last = _obj_0[1], _obj_0[#_obj_0]
2785local tuples = { 3178local tuples = {
2786 { 3179 {
2787 "hello", 3180 "hello",
@@ -2846,6 +3239,36 @@ end
2846 local first = select(1, ...) 3239 local first = select(1, ...)
2847 return print(ok, count, first) 3240 return print(ok, count, first)
2848end)(fn(true)) 3241end)(fn(true))
3242local f
3243f = function(...)
3244 local t = {
3245 n = select("#", ...),
3246 ...
3247 }
3248 print("参数个数:", t.n)
3249 print("表长度:", #t)
3250 for i = 1, t.n do
3251 print(t[i])
3252 end
3253end
3254f(1, 2, 3)
3255f("a", "b", "c", "d")
3256f()
3257local process
3258process = function(...)
3259 local args = {
3260 n = select("#", ...),
3261 ...
3262 }
3263 local sum = 0
3264 for i = 1, args.n do
3265 if args[i] ~= nil and type(args[i]) == "number" then
3266 sum = sum + args[i]
3267 end
3268 end
3269 return sum
3270end
3271process(1, nil, 3, nil, 5)
2849Rx.Observable.fromRange(1, 8):filter(function(x) 3272Rx.Observable.fromRange(1, 8):filter(function(x)
2850 return x % 2 == 0 3273 return x % 2 == 0
2851end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) 3274end):concat(Rx.Observable.of('who do we appreciate')):map(function(value)
@@ -2883,6 +3306,56 @@ end)
2883if success then 3306if success then
2884 print(result) 3307 print(result)
2885end 3308end
3309local a, b, c
3310do
3311 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
3312 return func()
3313 end)
3314 if _ok_0 then
3315 a, b, c = _ret_0, _ret_1, _ret_2
3316 end
3317end
3318do
3319 local _exp_0 = ((function()
3320 return (function(_arg_0, ...)
3321 local _ok_0 = _arg_0
3322 if _ok_0 then
3323 return ...
3324 end
3325 end)(pcall(function()
3326 return func()
3327 end))
3328 end)())
3329 if _exp_0 ~= nil then
3330 a = _exp_0
3331 else
3332 a = "default"
3333 end
3334end
3335f((function()
3336 return (function(_arg_0, ...)
3337 local _ok_0 = _arg_0
3338 if _ok_0 then
3339 return ...
3340 end
3341 end)(pcall(function()
3342 return func()
3343 end))
3344end)())
3345f((function()
3346 return (function(_arg_0, ...)
3347 local _ok_0 = _arg_0
3348 if _ok_0 then
3349 return ...
3350 end
3351 end)(xpcall(function()
3352 print(123)
3353 return func()
3354 end, function(e)
3355 print(e)
3356 return e
3357 end))
3358end)())
2886local a <const> = 123 3359local a <const> = 123
2887local _ <close> = setmetatable({ }, { 3360local _ <close> = setmetatable({ }, {
2888 __close = function() 3361 __close = function()
@@ -2897,6 +3370,14 @@ local some_string = "这是一个字符串\n 并包括一个换行。"
2897print("我有" .. tostring(math.random() * 100) .. "%的把握。") 3370print("我有" .. tostring(math.random() * 100) .. "%的把握。")
2898local integer = 1000000 3371local integer = 1000000
2899local hex = 0xEFBBBF 3372local hex = 0xEFBBBF
3373local binary = 19
3374local str = "key: value\nlist:\n - item1\n - " .. tostring(expr)
3375local fn
3376fn = function()
3377 local str = "foo:\n bar: baz"
3378 return str
3379end
3380local str = "path: \"C:\\Program Files\\App\"\nnote: 'He said: \"" .. tostring(Hello) .. "!\"'"
2900local my_function 3381local my_function
2901my_function = function() end 3382my_function = function() end
2902my_function() 3383my_function()
@@ -2985,6 +3466,66 @@ if func(1, 2, 3, "你好", "世界") then
2985 print("你好") 3466 print("你好")
2986 print("我在if内部") 3467 print("我在if内部")
2987end 3468end
3469local f1
3470f1 = function(_arg_0)
3471 local a, b, c
3472 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
3473 return print(a, b, c)
3474end
3475f1({
3476 a = 1,
3477 b = "2",
3478 c = { }
3479})
3480local f2
3481f2 = function(_arg_0, c)
3482 local a1, b
3483 a1, b = _arg_0.a, _arg_0.b
3484 if a1 == nil then
3485 a1 = 123
3486 end
3487 if b == nil then
3488 b = 'abc'
3489 end
3490 if c == nil then
3491 c = { }
3492 end
3493 return print(a1, b, c)
3494end
3495local arg1 = {
3496 a = 0
3497}
3498f2(arg1, arg2)
3499local findFirstEven
3500findFirstEven = function(list)
3501 for _index_0 = 1, #list do
3502 local item = list[_index_0]
3503 if type(item) == "table" then
3504 for _index_1 = 1, #item do
3505 local sub = item[_index_1]
3506 if sub % 2 == 0 then
3507 return sub
3508 end
3509 end
3510 end
3511 end
3512 return nil
3513end
3514local findFirstEven
3515findFirstEven = function(list)
3516 for _index_0 = 1, #list do
3517 local item = list[_index_0]
3518 if type(item) == "table" then
3519 for _index_1 = 1, #item do
3520 local sub = item[_index_1]
3521 if sub % 2 == 0 then
3522 return sub
3523 end
3524 end
3525 end
3526 end
3527 return nil
3528end
2988f(function() 3529f(function()
2989 return print("hello") 3530 return print("hello")
2990end) 3531end)
@@ -3110,6 +3651,28 @@ for _index_0 = 1, #_list_0 do
3110 _len_0 = _len_0 + 1 3651 _len_0 = _len_0 + 1
3111end 3652end
3112doubled = _accum_0 3653doubled = _accum_0
3654local data = {
3655 a = {
3656 1,
3657 2,
3658 3
3659 },
3660 b = {
3661 4,
3662 5,
3663 6
3664 }
3665}
3666local flat
3667local _accum_0 = { }
3668for k, v in pairs(data) do
3669 local _len_0 = #_accum_0 + 1
3670 for _index_0 = 1, #v do
3671 local _elm_0 = v[_index_0]
3672 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
3673 end
3674end
3675flat = _accum_0
3113local x_coords = { 3676local x_coords = {
3114 4, 3677 4,
3115 5, 3678 5,
@@ -3200,8 +3763,7 @@ local slice
3200local _accum_0 = { } 3763local _accum_0 = { }
3201local _len_0 = 1 3764local _len_0 = 1
3202local _list_0 = items 3765local _list_0 = items
3203local _max_0 = 5 3766for _index_0 = 1, 5 do
3204for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3205 local item = _list_0[_index_0] 3767 local item = _list_0[_index_0]
3206 _accum_0[_len_0] = item 3768 _accum_0[_len_0] = item
3207 _len_0 = _len_0 + 1 3769 _len_0 = _len_0 + 1
@@ -3211,7 +3773,8 @@ local slice
3211local _accum_0 = { } 3773local _accum_0 = { }
3212local _len_0 = 1 3774local _len_0 = 1
3213local _list_0 = items 3775local _list_0 = items
3214for _index_0 = 2, #_list_0 do 3776local _max_0 = #_list_0
3777for _index_0 = 2, _max_0 do
3215 local item = _list_0[_index_0] 3778 local item = _list_0[_index_0]
3216 _accum_0[_len_0] = item 3779 _accum_0[_len_0] = item
3217 _len_0 = _len_0 + 1 3780 _len_0 = _len_0 + 1
@@ -3221,12 +3784,46 @@ local slice
3221local _accum_0 = { } 3784local _accum_0 = { }
3222local _len_0 = 1 3785local _len_0 = 1
3223local _list_0 = items 3786local _list_0 = items
3224for _index_0 = 1, #_list_0, 2 do 3787local _max_0 = #_list_0
3788for _index_0 = 1, _max_0, 2 do
3225 local item = _list_0[_index_0] 3789 local item = _list_0[_index_0]
3226 _accum_0[_len_0] = item 3790 _accum_0[_len_0] = item
3227 _len_0 = _len_0 + 1 3791 _len_0 = _len_0 + 1
3228end 3792end
3229slice = _accum_0 3793slice = _accum_0
3794local slice
3795local _accum_0 = { }
3796local _len_0 = 1
3797local _list_0 = items
3798local _min_0 = #_list_0 + -4 + 1
3799local _max_0 = #_list_0 + -1 + 1
3800for _index_0 = _min_0, _max_0 do
3801 local item = _list_0[_index_0]
3802 _accum_0[_len_0] = item
3803 _len_0 = _len_0 + 1
3804end
3805slice = _accum_0
3806local reverse_slice
3807local _accum_0 = { }
3808local _len_0 = 1
3809local _list_0 = items
3810local _min_0 = #_list_0 + -1 + 1
3811for _index_0 = _min_0, 1, -1 do
3812 local item = _list_0[_index_0]
3813 _accum_0[_len_0] = item
3814 _len_0 = _len_0 + 1
3815end
3816reverse_slice = _accum_0
3817local sub_list
3818local _accum_0 = { }
3819local _len_0 = 1
3820local _list_0 = items
3821for _index_0 = 2, 4 do
3822 local _item_0 = _list_0[_index_0]
3823 _accum_0[_len_0] = _item_0
3824 _len_0 = _len_0 + 1
3825end
3826sub_list = _accum_0
3230for i = 10, 20 do 3827for i = 10, 20 do
3231 print(i) 3828 print(i)
3232end 3829end
@@ -3237,8 +3834,7 @@ for key, value in pairs(object) do
3237 print(key, value) 3834 print(key, value)
3238end 3835end
3239local _list_0 = items 3836local _list_0 = items
3240local _max_0 = 4 3837for _index_0 = 2, 4 do
3241for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3242 local item = _list_0[_index_0] 3838 local item = _list_0[_index_0]
3243 print(item) 3839 print(item)
3244end 3840end
@@ -3422,7 +4018,7 @@ if "Robert" == name then
3422elseif "Dan" == name or "Daniel" == name then 4018elseif "Dan" == name or "Daniel" == name then
3423 print("你的名字是Dan") 4019 print("你的名字是Dan")
3424else 4020else
3425 print("我不知道你的名字") 4021 print("我不认识,你的名字" .. tostring(name))
3426end 4022end
3427local b = 1 4023local b = 1
3428local next_number 4024local next_number
@@ -3679,6 +4275,35 @@ if _tab_0 then
3679 print("匹配成功", fourth) 4275 print("匹配成功", fourth)
3680 end 4276 end
3681end 4277end
4278local segments = {
4279 "admin",
4280 "users",
4281 "logs",
4282 "view"
4283}
4284local _type_0 = type(segments)
4285local _tab_0 = "table" == _type_0 or "userdata" == _type_0
4286if _tab_0 then
4287 local groups
4288 do
4289 local _accum_0 = { }
4290 local _len_0 = 1
4291 local _max_0 = #segments + -3 + 1
4292 for _index_0 = 1, _max_0 do
4293 local _item_0 = segments[_index_0]
4294 _accum_0[_len_0] = _item_0
4295 _len_0 = _len_0 + 1
4296 end
4297 groups = _accum_0
4298 end
4299 local resource = segments[#segments - 1]
4300 local action = segments[#segments]
4301 if resource ~= nil and action ~= nil then
4302 print("Group:", groups)
4303 print("Resource:", resource)
4304 print("Action:", action)
4305 end
4306end
3682local Inventory 4307local Inventory
3683local _class_0 4308local _class_0
3684local _base_0 = { 4309local _base_0 = {
@@ -4335,6 +4960,10 @@ do
4335 _with_1["key-name"] = value 4960 _with_1["key-name"] = value
4336end 4961end
4337_with_0[#_with_0 + 1] = "abc" 4962_with_0[#_with_0 + 1] = "abc"
4963local _with_0 = obj
4964if _with_0 ~= nil then
4965 print(obj.name)
4966end
4338do 4967do
4339 local var = "hello" 4968 local var = "hello"
4340 print(var) 4969 print(var)
diff --git a/spec/outputs/comprehension.lua b/spec/outputs/comprehension.lua
index 9a7c478..663bd44 100644
--- a/spec/outputs/comprehension.lua
+++ b/spec/outputs/comprehension.lua
@@ -243,8 +243,11 @@ end
243do 243do
244 local _accum_0 = { } 244 local _accum_0 = { }
245 local _len_0 = 1 245 local _len_0 = 1
246 local _min_0 = 1 + 2
246 local _max_0 = 3 + 4 247 local _max_0 = 3 + 4
247 for _index_0 = 1 + 2, _max_0 < 0 and #items + _max_0 or _max_0 do 248 _min_0 = _min_0 < 0 and #items + _min_0 + 1 or _min_0
249 _max_0 = _max_0 < 0 and #items + _max_0 + 1 or _max_0
250 for _index_0 = _min_0, _max_0 do
248 local item = items[_index_0] 251 local item = items[_index_0]
249 _accum_0[_len_0] = item 252 _accum_0[_len_0] = item
250 _len_0 = _len_0 + 1 253 _len_0 = _len_0 + 1
@@ -254,8 +257,11 @@ end
254do 257do
255 local _accum_0 = { } 258 local _accum_0 = { }
256 local _len_0 = 1 259 local _len_0 = 1
260 local _min_0 = hello() * 4
257 local _max_0 = 2 - thing[4] 261 local _max_0 = 2 - thing[4]
258 for _index_0 = hello() * 4, _max_0 < 0 and #items + _max_0 or _max_0 do 262 _min_0 = _min_0 < 0 and #items + _min_0 + 1 or _min_0
263 _max_0 = _max_0 < 0 and #items + _max_0 + 1 or _max_0
264 for _index_0 = _min_0, _max_0 do
259 local item = items[_index_0] 265 local item = items[_index_0]
260 _accum_0[_len_0] = item 266 _accum_0[_len_0] = item
261 _len_0 = _len_0 + 1 267 _len_0 = _len_0 + 1
diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua
index 44da58b..4e19aca 100644
--- a/spec/outputs/destructure.lua
+++ b/spec/outputs/destructure.lua
@@ -621,4 +621,114 @@ do
621 print(meta_field, abc, def) 621 print(meta_field, abc, def)
622 end 622 end
623end 623end
624do
625 local clients = {
626 "VIP_Alice",
627 "User_Bob",
628 "User_Clara",
629 "VIP_Eva"
630 }
631 local vipStart, regulars, vipEnd = clients[1], (function()
632 local _accum_0 = { }
633 local _len_0 = 1
634 local _max_0 = #clients + -2 + 1
635 for _index_0 = 2, _max_0 do
636 local _item_0 = clients[_index_0]
637 _accum_0[_len_0] = _item_0
638 _len_0 = _len_0 + 1
639 end
640 return _accum_0
641 end)(), clients[#clients]
642 print(vipStart)
643 print(regulars)
644 print(vipEnd)
645end
646do
647 local setupMeeting
648 setupMeeting = function(participants)
649 local chair, secretary = participants[1], participants[#participants]
650 return print(chair, secretary)
651 end
652 setupMeeting({
653 "Alice",
654 "Bob",
655 "Charlie",
656 "David"
657 })
658end
659do
660 local getTransactions
661 getTransactions = function()
662 return {
663 {
664 id = "T1",
665 amount = 100
666 },
667 {
668 id = "T2",
669 amount = 200
670 },
671 {
672 id = "T3",
673 amount = 300
674 }
675 }
676 end
677 local id, amount
678 do
679 local _item_0 = getTransactions()
680 local _obj_0 = _item_0[#_item_0]
681 id, amount = _obj_0.id, _obj_0.amount
682 end
683 assert(id == "T3")
684 assert(amount == 300)
685end
686do
687 local middle
688 local _accum_0 = { }
689 local _len_0 = 1
690 local _list_0 = tb
691 local _max_0 = #_list_0 + -2 + 1
692 for _index_0 = 2, _max_0 do
693 local _item_0 = _list_0[_index_0]
694 _accum_0[_len_0] = _item_0
695 _len_0 = _len_0 + 1
696 end
697 middle = _accum_0
698end
699do
700 local a, abc, b, def, sub, d, e
701 local _obj_0 = tb
702 a, abc, b, def, sub, d, e = _obj_0[1], _obj_0.abc, _obj_0[2], _obj_0.def, (function()
703 local _accum_0 = { }
704 local _len_0 = 1
705 local _max_0 = #_obj_0 + -3 + 1
706 for _index_0 = 3, _max_0 do
707 local _item_0 = _obj_0[_index_0]
708 _accum_0[_len_0] = _item_0
709 _len_0 = _len_0 + 1
710 end
711 return _accum_0
712 end)(), _obj_0[#_obj_0 - 1], _obj_0[#_obj_0]
713end
714do
715 local _list_0 = items
716 for _index_0 = 1, #_list_0 do
717 local _des_0 = _list_0[_index_0]
718 local a, b = _des_0.a, _des_0.b
719 print(a, b)
720 end
721 local _list_1 = items
722 for _index_0 = 1, #_list_1 do
723 local _des_0 = _list_1[_index_0]
724 local a, b = _des_0.a, _des_0.b
725 print(a, b)
726 end
727 for _des_0 in pairs(data) do
728 local body = _des_0.body
729 if body then
730 print(body)
731 end
732 end
733end
624return nil 734return nil
diff --git a/spec/outputs/funcs.lua b/spec/outputs/funcs.lua
index c1735c4..db7ed67 100644
--- a/spec/outputs/funcs.lua
+++ b/spec/outputs/funcs.lua
@@ -283,4 +283,157 @@ do
283 end 283 end
284 print(func()) 284 print(func())
285end 285end
286local _anon_func_0 = function(_arg_0)
287 local _accum_0 = { }
288 local _len_0 = 1
289 local _max_0 = #_arg_0
290 for _index_0 = 1, _max_0 do
291 local _item_0 = _arg_0[_index_0]
292 _accum_0[_len_0] = _item_0
293 _len_0 = _len_0 + 1
294 end
295 return _accum_0
296end
297do
298 local f
299 f = function(_arg_0)
300 local a, b, c
301 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
302 return print(a, b, c)
303 end
304 f = function(_arg_0)
305 local a, b, c
306 a, b, c = _arg_0.a, _arg_0.b, _arg_0.c
307 return print(a, b, c)
308 end
309 local g
310 g = function(x, _arg_0)
311 local y
312 y = _arg_0.y
313 return print(x, y)
314 end
315 local i
316 i = function(_arg_0)
317 local ax, by
318 ax, by = _arg_0.a, _arg_0.b
319 if ax == nil then
320 ax = 0
321 end
322 if by == nil then
323 by = 0
324 end
325 return print(ax, by)
326 end
327 j = function(name, _arg_0)
328 local uid, role
329 uid, role = _arg_0.id, _arg_0.role
330 if uid == nil then
331 uid = "n/a"
332 end
333 if role == nil then
334 role = "guest"
335 end
336 return print(name, uid, role)
337 end
338 local m
339 m = function(_arg_0)
340 local name, age, ver
341 name, age, ver = _arg_0.user.name, _arg_0.user.age, _arg_0.meta.ver
342 if ver == nil then
343 ver = 1
344 end
345 return print(name, age, ver)
346 end
347 local m1
348 m1 = function(_arg_0)
349 local name, age, meta
350 name, age, meta = _arg_0.user.name, _arg_0.user.age, _arg_0.meta
351 if meta == nil then
352 meta = { }
353 end
354 return print(name, age, meta and meta.ver or "nil")
355 end
356 local new
357 new = function(self, _arg_0)
358 local name, age
359 name, age = _arg_0.name, _arg_0.age
360 if name == nil then
361 name = "anon"
362 end
363 if age == nil then
364 age = 0
365 end
366 self.name = name
367 self.age = age
368 end
369 local set
370 set = function(self, _arg_0)
371 local name, age
372 name, age = _arg_0.name, _arg_0.age
373 if name == nil then
374 name = self.name
375 end
376 if age == nil then
377 age = self.age
378 end
379 self.name = name
380 self.age = age
381 end
382 local logKV
383 logKV = function(_arg_0, ...)
384 local k, v
385 k, v = _arg_0.k, _arg_0.v
386 print("kv:", k, v)
387 return print("rest count:", select("#", ...))
388 end
389 do
390 local foo
391 foo = function(_arg_0)
392 local a, b
393 a, b = _arg_0.a, _arg_0.b
394 if b == nil then
395 b = 0
396 end
397 return print(a, b)
398 end
399 end
400 local t1
401 t1 = function(_arg_0, x)
402 local a
403 a = _arg_0.a
404 return print(a, x)
405 end
406 local t2
407 t2 = function(_arg_0)
408 local a
409 a = _arg_0.a
410 return print(a)
411 end
412 local w
413 w = function(id, _arg_0, _arg_1)
414 local x, y
415 x, y = _arg_0.x, _arg_0.y
416 if x == nil then
417 x = 0
418 end
419 if y == nil then
420 y = 0
421 end
422 local flag
423 flag = _arg_1.flag
424 return print(id, x, y, flag)
425 end
426 local g1
427 g1 = function(_arg_0)
428 local a, ax
429 a, ax = _arg_0.a, _arg_0.a
430 return print(a, ax)
431 end
432 local g4
433 g4 = function(_arg_0)
434 local a, b, rest
435 a, b, rest = _arg_0.a, _arg_0.b, _anon_func_0(_arg_0)
436 return print(a, b)
437 end
438end
286return nil 439return nil
diff --git a/spec/outputs/import.lua b/spec/outputs/import.lua
index 83c99e2..7aa130f 100644
--- a/spec/outputs/import.lua
+++ b/spec/outputs/import.lua
@@ -166,3 +166,13 @@ do
166 local _obj_1 = require("m") 166 local _obj_1 = require("m")
167 g, i = _obj_1[1], getmetatable(_obj_1[2]).__close 167 g, i = _obj_1[1], getmetatable(_obj_1[2]).__close
168end 168end
169do
170 local require <const> = require
171 local stringlib <const> = string
172 local format <const> = string.format
173 local io_read <const> = io.read
174 local type
175 type = function() end
176 local tp <const> = _G.type
177 local yue <const> = _G["月"]
178end
diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua
index 48ec9c8..75f04fa 100644
--- a/spec/outputs/lists.lua
+++ b/spec/outputs/lists.lua
@@ -230,31 +230,36 @@ x = {
230 6, 230 6,
231 7 231 7
232} 232}
233local _max_0 = -5 233local _max_0 = #x + -5 + 1
234for _index_0 = 2, _max_0 < 0 and #x + _max_0 or _max_0, 2 do 234for _index_0 = 2, _max_0, 2 do
235 local y = x[_index_0] 235 local y = x[_index_0]
236 print(y) 236 print(y)
237end 237end
238local _max_1 = 3 238for _index_0 = 1, 3 do
239for _index_0 = 1, _max_1 < 0 and #x + _max_1 or _max_1 do
240 local y = x[_index_0] 239 local y = x[_index_0]
241 print(y) 240 print(y)
242end 241end
243for _index_0 = 2, #x do 242local _max_1 = #x
243for _index_0 = 2, _max_1 do
244 local y = x[_index_0] 244 local y = x[_index_0]
245 print(y) 245 print(y)
246end 246end
247for _index_0 = 1, #x, 2 do 247local _max_2 = #x
248for _index_0 = 1, _max_2, 2 do
248 local y = x[_index_0] 249 local y = x[_index_0]
249 print(y) 250 print(y)
250end 251end
251for _index_0 = 2, #x, 2 do 252local _max_3 = #x
253for _index_0 = 2, _max_3, 2 do
252 local y = x[_index_0] 254 local y = x[_index_0]
253 print(y) 255 print(y)
254end 256end
255local a, b, c = 1, 5, 2 257local a, b, c = 1, 5, 2
256local _max_2 = b 258local _min_0 = a
257for _index_0 = a, _max_2 < 0 and #x + _max_2 or _max_2, c do 259local _max_4 = b
260_min_0 = _min_0 < 0 and #x + _min_0 + 1 or _min_0
261_max_4 = _max_4 < 0 and #x + _max_4 + 1 or _max_4
262for _index_0 = _min_0, _max_4, c do
258 local y = x[_index_0] 263 local y = x[_index_0]
259 print(y) 264 print(y)
260end 265end
@@ -287,7 +292,10 @@ do
287 a 292 a
288 }) 293 })
289 local _list_0 = f 294 local _list_0 = f
290 for _index_0 = a, #_list_0 do 295 local _min_1 = a
296 local _max_5 = #_list_0
297 _min_1 = _min_1 < 0 and #_list_0 + _min_1 + 1 or _min_1
298 for _index_0 = _min_1, _max_5 do
291 local v = _list_0[_index_0] 299 local v = _list_0[_index_0]
292 print(v) 300 print(v)
293 end 301 end
@@ -327,4 +335,570 @@ do
327 job = "jobless" 335 job = "jobless"
328 end 336 end
329end 337end
338do
339 local transactions = {
340 "T001",
341 "T002",
342 "T003",
343 "T004",
344 "T005"
345 }
346 local middleTransactions
347 do
348 local _accum_0 = { }
349 local _len_0 = 1
350 local _max_5 = #transactions + -2 + 1
351 for _index_0 = 2, _max_5 do
352 local _item_0 = transactions[_index_0]
353 _accum_0[_len_0] = _item_0
354 _len_0 = _len_0 + 1
355 end
356 middleTransactions = _accum_0
357 end
358 print(middleTransactions)
359end
360do
361 local logs = {
362 {
363 start = 0,
364 ["end"] = 100
365 },
366 {
367 start = 100,
368 ["end"] = 200
369 },
370 {
371 start = 200,
372 ["end"] = 123
373 }
374 }
375 print(logs[#logs]["end"])
376end
377do
378 local pendingOrders = {
379 "O001",
380 "O002",
381 "O003",
382 "O004"
383 }
384 print(pendingOrders[#pendingOrders - 1])
385end
386do
387 local getOrders
388 getOrders = function()
389 return {
390 {
391 id = "O1001",
392 status = "pending"
393 },
394 {
395 id = "O1002",
396 status = "processing"
397 },
398 {
399 id = "O1003",
400 status = "done"
401 }
402 }
403 end
404 local lastStatus
405 do
406 local _item_0 = getOrders()
407 lastStatus = _item_0[#_item_0].status
408 end
409 assert(lastStatus == "done")
410end
411do
412 local cloneList1
413 cloneList1 = function(list)
414 local _accum_0 = { }
415 local _len_0 = 1
416 local _max_5 = #list
417 for _index_0 = 1, _max_5 do
418 local _item_0 = list[_index_0]
419 _accum_0[_len_0] = _item_0
420 _len_0 = _len_0 + 1
421 end
422 return _accum_0
423 end
424 local cloneList2
425 cloneList2 = function(list)
426 local _tab_0 = { }
427 local _idx_0 = #_tab_0 + 1
428 for _index_0 = 1, #list do
429 local _value_0 = list[_index_0]
430 _tab_0[_idx_0] = _value_0
431 _idx_0 = _idx_0 + 1
432 end
433 return _tab_0
434 end
435 local cloneTable
436 cloneTable = function(tb)
437 local _tab_0 = { }
438 local _idx_0 = 1
439 for _key_0, _value_0 in pairs(tb) do
440 if _idx_0 == _key_0 then
441 _tab_0[#_tab_0 + 1] = _value_0
442 _idx_0 = _idx_0 + 1
443 else
444 _tab_0[_key_0] = _value_0
445 end
446 end
447 return _tab_0
448 end
449end
450do
451 print((function()
452 local _item_0 = globalTB
453 return _item_0[#_item_0]
454 end)(), (function()
455 local _item_0 = a.b.c
456 return _item_0[#_item_0 - 2]
457 end)(), (function()
458 if x ~= nil then
459 local _obj_0 = x.y
460 if _obj_0 ~= nil then
461 local _obj_1 = _obj_0(x).z
462 if _obj_1 ~= nil then
463 return _obj_1[#_obj_1 - 3]
464 end
465 return nil
466 end
467 return nil
468 end
469 return nil
470 end)())
471end
472local _anon_func_0 = function(globalTB)
473 local _call_0
474 do
475 local _item_0 = globalTB
476 _call_0 = _item_0[#_item_0]
477 end
478 return _call_0["end"](_call_0, 123)
479end
480local _anon_func_1 = function(a)
481 local _item_0
482 do
483 local _accum_0 = { }
484 local _len_0 = 1
485 local _list_0 = a.b.c
486 local _max_5 = #_list_0 + -5 + 1
487 for _index_0 = 5, _max_5 do
488 local _item_1 = _list_0[_index_0]
489 _accum_0[_len_0] = _item_1
490 _len_0 = _len_0 + 1
491 end
492 _item_0 = _accum_0
493 end
494 return _item_0[#_item_0 - 2]
495end
496local _anon_func_2 = function(x)
497 if x ~= nil then
498 local _obj_0 = x.y
499 if _obj_0 ~= nil then
500 local _obj_1 = _obj_0(x).z
501 if _obj_1 ~= nil then
502 local _obj_2 = _obj_1[#_obj_1 - 3]
503 if _obj_2 ~= nil then
504 local _accum_0 = { }
505 local _len_0 = 1
506 local _max_5 = #_obj_2 + -3 + 1
507 for _index_0 = 1, _max_5 do
508 local _item_0 = _obj_2[_index_0]
509 _accum_0[_len_0] = _item_0
510 _len_0 = _len_0 + 1
511 end
512 return _accum_0
513 end
514 return nil
515 end
516 return nil
517 end
518 return nil
519 end
520 return nil
521end
522do
523 local f
524 f = function()
525 return print(_anon_func_0(globalTB), _anon_func_1(a), _anon_func_2(x))
526 end
527end
528do
529 local tb = {
530 1,
531 2,
532 3
533 }
534 tb[#tb] = 40
535 tb[#tb - 1] = 20
536end
537do
538 a = "x"
539 b = a
540 c = a
541 local lst = { }
542 lst[#lst] = a
543 lst[#lst - 1] = b
544end
545do
546 local y, z
547 x, y, z = 1, 2, 3
548 local arr = { }
549 local head
550 arr[#arr], head = x, y
551 arr[#arr] = z
552end
553do
554 local triple = {
555 "keep",
556 "skip",
557 "tail"
558 }
559 local head, tailv = triple[1], triple[3]
560 local buf = { }
561 buf[#buf] = head
562 buf[#buf] = tailv
563end
564do
565 local src = {
566 "a",
567 "",
568 "c",
569 nil,
570 "d"
571 }
572 local collected = { }
573 for _index_0 = 1, #src do
574 local item = src[_index_0]
575 if item and #item > 0 then
576 collected[#collected] = item
577 end
578 end
579end
580do
581 local nums = {
582 1,
583 2,
584 3,
585 4,
586 5
587 }
588 local last_two
589 do
590 local _accum_0 = { }
591 local _len_0 = 1
592 for _index_0 = 1, #nums do
593 local v = nums[_index_0]
594 if v > 3 then
595 _accum_0[_len_0] = v
596 _len_0 = _len_0 + 1
597 end
598 end
599 last_two = _accum_0
600 end
601 nums[#nums] = last_two[1]
602 nums[#nums] = last_two[2]
603end
604do
605 local store = { }
606 store[#store] = {
607 meta = {
608 id = 1,
609 ok = true
610 },
611 payload = {
612 10,
613 20
614 }
615 }
616 store[#store] = {
617 meta = {
618 id = 1,
619 ok = false
620 },
621 payload = {
622 10,
623 20,
624 30
625 }
626 }
627end
628local _anon_func_3 = function(tb)
629 local _item_0 = tb.tmp
630 return _item_0[#_item_0]
631end
632do
633 local f
634 f = function()
635 local q = { }
636 do
637 local _accum_0 = { }
638 local _len_0 = 1
639 for n = 1, 4 do
640 _accum_0[_len_0] = n
641 _len_0 = _len_0 + 1
642 end
643 tb.tmp = _accum_0
644 end
645 if #tb.tmp >= 3 then
646 q[#q] = {
647 head = tb.tmp[1],
648 tail = _anon_func_3(tb)
649 }
650 end
651 end
652end
653do
654 local make_pair
655 make_pair = function(a, b)
656 return {
657 a,
658 b
659 }
660 end
661 local pairs = { }
662 local p1 = make_pair(7, 8)
663 pairs[#pairs] = p1
664 local k, v = "key", 42
665 pairs[#pairs] = {
666 k = k,
667 v = v
668 }
669end
670do
671 local cfg = {
672 mode = "safe",
673 tags = { }
674 }
675 if cfg.mode == "safe" then
676 cfg.mode = "fast"
677 local _obj_0 = cfg.tags
678 _obj_0[#_obj_0] = "newbie"
679 end
680end
681do
682 local mat = {
683 {
684 1,
685 2
686 },
687 {
688 3,
689 4
690 },
691 {
692 5,
693 6
694 }
695 }
696 local last_row = mat[#mat]
697 local rows = { }
698 rows[#rows] = last_row[1]
699end
700do
701 local kv = { }
702 kv[#kv] = {
703 k = "a",
704 v = 1
705 }
706 kv[#kv] = {
707 k = "b",
708 v = 2
709 }
710 local pair_last = kv[#kv]
711 local dict = { }
712 dict[pair_last.k] = pair_last.v
713 dict[pair_last.k] = 3
714end
715do
716 local base
717 do
718 local _accum_0 = { }
719 local _len_0 = 1
720 for i = 1, 4 do
721 _accum_0[_len_0] = i
722 _len_0 = _len_0 + 1
723 end
724 base = _accum_0
725 end
726 local pack = { }
727 pack[#pack] = {
728 base[1],
729 base[#base]
730 }
731 pack[#pack] = {
732 first = base[1],
733 last = base[#base]
734 }
735end
736do
737 local opts = {
738 limit = 10
739 }
740 local limit, offset = opts.limit, opts.offset
741 if offset == nil then
742 offset = 0
743 end
744 local pages = { }
745 pages[#pages] = {
746 limit = limit,
747 offset = offset
748 }
749end
750do
751 local chain = {
752 a = {
753 b = {
754 c = 0
755 }
756 },
757 list = {
758 {
759 x = 0
760 },
761 {
762 x = 0
763 }
764 }
765 }
766 chain.a.b.c = 1
767 chain.list[1].x = 10;
768 ((function()
769 local _item_0 = chain.list
770 return _item_0[#_item_0]
771 end)()).x = 20
772 local _obj_0 = chain.list
773 _obj_0[#_obj_0 - 1] = {
774 x = 30
775 }
776end
777do
778 local node = {
779 left = {
780 v = 0
781 },
782 right = {
783 v = 0
784 }
785 }
786 local bag = { }
787 local left, right = node.left, node.right
788 bag[#bag], left.v, right.v = "k", 1, 2
789end
790do
791 local a1, a2, a3 = 100, 200, 300
792 local mix = { }
793 local meta
794 mix[#mix], mix[#mix], meta = a1, a2, {
795 tag = "ok"
796 }
797end
798do
799 local cfg2 = {
800 limit = 5,
801 opts = {
802 flag = false
803 }
804 }
805 local lim, opt2 = cfg2.limit, cfg2.opts
806 local bucket = {
807 xs = { }
808 }
809 local _obj_0 = bucket.xs
810 _obj_0[#_obj_0] = lim
811 bucket.flag = true
812 local _obj_1 = opt2.flags
813 _obj_1[#_obj_1 + 1] = 123
814end
815do
816 local ret2
817 ret2 = function()
818 return 7, 8
819 end
820 local box = { }
821 local x1
822 box[#box], x1 = ret2()
823end
824do
825 local q = {
826 1,
827 2
828 }
829 local lastq = q[#q]
830 q[#q - 1] = lastq * 10
831end
832do
833 local mat2 = [[9,8], [7,6]]
834 local t = {
835 hold = nil
836 }
837 t.hold = mat2[#mat2][1]
838end
839do
840 local f
841 f = function()
842 local _obj_0
843 do
844 local _item_0 = globalTB
845 _obj_0 = _item_0[#_item_0]
846 end
847 _obj_0[#_obj_0] = 1
848 end
849 local f1
850 f1 = function()
851 do
852 local _item_0 = globalTB
853 do
854 local _item_1 = _item_0[#_item_0]
855 return _item_1[#_item_1 - 1]
856 end
857 end
858 end
859end
860do
861 do
862 local _obj_0 = tbA
863 local _len_0 = #_obj_0 + 1
864 local _list_0 = tbB
865 for _index_0 = 1, #_list_0 do
866 local _elm_0 = _list_0[_index_0]
867 _obj_0[_len_0], _len_0 = _elm_0, _len_0 + 1
868 end
869 end
870 a = 1
871 do
872 local _obj_0 = tb
873 local _len_0 = #_obj_0 + 1
874 for _index_0 = 1, #x do
875 local _elm_0 = x[_index_0]
876 _obj_0[_len_0], _len_0 = _elm_0, _len_0 + 1
877 end
878 end
879 b[#b + 1] = 3
880 c = 4
881 local data = {
882 a = {
883 1,
884 2,
885 3
886 },
887 b = {
888 4,
889 5,
890 6
891 }
892 }
893 local flat
894 local _accum_0 = { }
895 for k, v in pairs(data) do
896 local _len_0 = #_accum_0 + 1
897 for _index_0 = 1, #v do
898 local _elm_0 = v[_index_0]
899 _accum_0[_len_0], _len_0 = _elm_0, _len_0 + 1
900 end
901 end
902 flat = _accum_0
903end
330return nil 904return nil
diff --git a/spec/outputs/loops.lua b/spec/outputs/loops.lua
index 9a47579..6ab4bbb 100644
--- a/spec/outputs/loops.lua
+++ b/spec/outputs/loops.lua
@@ -468,3 +468,31 @@ do
468 end 468 end
469 list = _accum_0 469 list = _accum_0
470end 470end
471do
472 repeat
473 print(1)
474 until true
475 do
476 local _accum_0
477 repeat
478 a = func()
479 _accum_0 = a.x
480 break
481 until a.v
482 x = _accum_0
483 end
484 local items
485 local _accum_0 = { }
486 local _len_0 = 1
487 repeat
488 local item = getItem()
489 if not item then
490 break
491 end
492 if item.value > 0 then
493 _accum_0[_len_0] = item
494 _len_0 = _len_0 + 1
495 end
496 until false
497 items = _accum_0
498end
diff --git a/spec/outputs/macro.lua b/spec/outputs/macro.lua
index 9f5507c..89c6e63 100644
--- a/spec/outputs/macro.lua
+++ b/spec/outputs/macro.lua
@@ -26,6 +26,10 @@ print({
26 123, 26 123,
27 'xyz' 27 'xyz'
28}) 28})
29print({
30 456,
31 'abc'
32})
29do 33do
30 assert(item == nil) 34 assert(item == nil)
31end 35end
@@ -213,6 +217,13 @@ function tb:func()
213end 217end
214end 218end
215print(x) 219print(x)
220local yue = require("yue")
221do
222local function f2(a)
223 return a + 1
224end
225x = x + f2(3)
226end
216local sel 227local sel
217sel = function(a, b, c) 228sel = function(a, b, c)
218 if a then 229 if a then
@@ -317,7 +328,7 @@ print((setmetatable({
317 return 998 328 return 998
318 end 329 end
319})) 330}))
320print("current line: " .. tostring(323)) 331print("current line: " .. tostring(349))
321do 332do
322 do 333 do
323-- TODO 334-- TODO
diff --git a/spec/outputs/props.lua b/spec/outputs/props.lua
new file mode 100644
index 0000000..2c282e0
--- /dev/null
+++ b/spec/outputs/props.lua
@@ -0,0 +1,240 @@
1local Props
2do
3 local _class_0
4 local assignReadOnly
5 local _base_0 = {
6 __index = function(self, name)
7 local cls = getmetatable(self)
8 do
9 local item
10 do
11 local _obj_0 = cls.__getter
12 if _obj_0 ~= nil then
13 item = _obj_0[name]
14 end
15 end
16 if item then
17 return item(self)
18 else
19 item = rawget(cls, name)
20 if item then
21 return item
22 else
23 local c = cls
24 repeat
25 c = getmetatable(c)
26 if c then
27 local _obj_0 = c.__getter
28 if _obj_0 ~= nil then
29 item = _obj_0[name]
30 end
31 if item then
32 if cls.__getter == nil then
33 cls.__getter = { }
34 end
35 cls.__getter[name] = item
36 return item(self)
37 else
38 item = rawget(c, name)
39 if item then
40 rawset(cls, name, item)
41 return item
42 end
43 end
44 else
45 break
46 end
47 until false
48 end
49 end
50 end
51 return nil
52 end,
53 __newindex = function(self, name, value)
54 local cls = getmetatable(self)
55 local item
56 local _obj_0 = cls.__setter
57 if _obj_0 ~= nil then
58 item = _obj_0[name]
59 end
60 if item then
61 return item(self, value)
62 else
63 local c = cls
64 repeat
65 c = getmetatable(c)
66 if c then
67 local _obj_1 = c.__setter
68 if _obj_1 ~= nil then
69 item = _obj_1[name]
70 end
71 if item then
72 if cls.__setter == nil then
73 cls.__setter = { }
74 end
75 cls.__setter[name] = item
76 item(self, value)
77 return
78 end
79 else
80 break
81 end
82 until false
83 return rawset(self, name, value)
84 end
85 end,
86 prop = function(self, name, props)
87 local get, set = props.get, props.set
88 if set == nil then
89 set = assignReadOnly
90 end
91 do
92 local getter = rawget(self.__base, "__getter")
93 if getter then
94 getter[name] = get
95 else
96 rawset(self.__base, "__getter", {
97 [name] = get
98 })
99 end
100 end
101 local setter = rawget(self.__base, "__setter")
102 if setter then
103 setter[name] = set
104 else
105 return rawset(self.__base, "__setter", {
106 [name] = set
107 })
108 end
109 end
110 }
111 if _base_0.__index == nil then
112 _base_0.__index = _base_0
113 end
114 _class_0 = setmetatable({
115 __init = function() end,
116 __base = _base_0,
117 __name = "Props"
118 }, {
119 __index = _base_0,
120 __call = function(cls, ...)
121 local _self_0 = setmetatable({ }, _base_0)
122 cls.__init(_self_0, ...)
123 return _self_0
124 end
125 })
126 _base_0.__class = _class_0
127 local self = _class_0;
128 assignReadOnly = function()
129 return error("assigning a readonly property")
130 end
131 Props = _class_0
132end
133local A
134do
135 local _class_0
136 local _parent_0 = Props
137 local _base_0 = { }
138 for _key_0, _val_0 in pairs(_parent_0.__base) do
139 if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then
140 _base_0[_key_0] = _val_0
141 end
142 end
143 if _base_0.__index == nil then
144 _base_0.__index = _base_0
145 end
146 setmetatable(_base_0, _parent_0.__base)
147 _class_0 = setmetatable({
148 __init = function(self)
149 self._x = 0
150 end,
151 __base = _base_0,
152 __name = "A",
153 __parent = _parent_0
154 }, {
155 __index = function(cls, name)
156 local val = rawget(_base_0, name)
157 if val == nil then
158 local parent = rawget(cls, "__parent")
159 if parent then
160 return parent[name]
161 end
162 else
163 return val
164 end
165 end,
166 __call = function(cls, ...)
167 local _self_0 = setmetatable({ }, _base_0)
168 cls.__init(_self_0, ...)
169 return _self_0
170 end
171 })
172 _base_0.__class = _class_0
173 local self = _class_0;
174 self:prop('x', {
175 get = function(self)
176 return self._x + 1000
177 end,
178 set = function(self, v)
179 self._x = v
180 end
181 })
182 if _parent_0.__inherited then
183 _parent_0.__inherited(_parent_0, _class_0)
184 end
185 A = _class_0
186end
187local B
188do
189 local _class_0
190 local _parent_0 = A
191 local _base_0 = { }
192 for _key_0, _val_0 in pairs(_parent_0.__base) do
193 if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then
194 _base_0[_key_0] = _val_0
195 end
196 end
197 if _base_0.__index == nil then
198 _base_0.__index = _base_0
199 end
200 setmetatable(_base_0, _parent_0.__base)
201 _class_0 = setmetatable({
202 __init = function(self, ...)
203 return _class_0.__parent.__init(self, ...)
204 end,
205 __base = _base_0,
206 __name = "B",
207 __parent = _parent_0
208 }, {
209 __index = function(cls, name)
210 local val = rawget(_base_0, name)
211 if val == nil then
212 local parent = rawget(cls, "__parent")
213 if parent then
214 return parent[name]
215 end
216 else
217 return val
218 end
219 end,
220 __call = function(cls, ...)
221 local _self_0 = setmetatable({ }, _base_0)
222 cls.__init(_self_0, ...)
223 return _self_0
224 end
225 })
226 _base_0.__class = _class_0
227 local self = _class_0;
228 self:prop('abc', {
229 get = function(self)
230 return "hello"
231 end
232 })
233 if _parent_0.__inherited then
234 _parent_0.__inherited(_parent_0, _class_0)
235 end
236 B = _class_0
237end
238local b = B()
239b.x = 999
240return print(b.x, b.abc)
diff --git a/spec/outputs/string.lua b/spec/outputs/string.lua
index febea62..bdfd676 100644
--- a/spec/outputs/string.lua
+++ b/spec/outputs/string.lua
@@ -1,3 +1,4 @@
1local _module_0 = { }
1local hi = "hello" 2local hi = "hello"
2local hello = "what the heckyes" 3local hello = "what the heckyes"
3print(hi) 4print(hi)
@@ -41,4 +42,42 @@ local _ = "hello";
41("hello"):format().hello(1, 2, 3); 42("hello"):format().hello(1, 2, 3);
42("hello"):format(1, 2, 3) 43("hello"):format(1, 2, 3)
43something("hello"):world() 44something("hello"):world()
44return something(("hello"):world()) 45something(("hello"):world())
46do
47 local str = "key: value"
48 str = "config:\n\tenabled: true\n\tlevel: 5"
49 str = "header: start\nfooter: end"
50 str = "name: " .. tostring(username)
51 str = "count: " .. tostring(total) .. " items"
52 str = "user: " .. tostring(name) .. "\nid: " .. tostring(id)
53 str = "path: \"C:\\\\Program Files\\\\App\"\ndesc: 'single \"quote\" test'"
54 str = "key: value \nnext: 123 "
55 str = "list:\n\t- \"one\"\n\t- \"two\""
56 str = "-- comment\ncontent text\n-- comment"
57 str = tostring(1 + 2) .. '\n' .. tostring(2 + 3) .. '\n' .. tostring("a" .. "b")
58 local obj = {
59 settings = "mode: " .. tostring(mode) .. "\nflags:\n\t- " .. tostring(flag1) .. "\n\t- default"
60 }
61 local fn
62 fn = function()
63 return "Hello\nname: " .. tostring(userName)
64 end
65 str = "result:\n\tstatus: " .. tostring((function()
66 if ok then
67 return "pass"
68 else
69 return "fail"
70 end
71 end)()) .. "\n\tcode: " .. tostring(code)
72 local summary = "date: " .. tostring(os.date()) .. "\nvalues:\n\t-\n\t\ta: " .. tostring(aVal) .. "\n\t\tb: " .. tostring(bVal or defaultB)
73 local msg = send("Hello, " .. tostring(user) .. "!\nToday is " .. tostring(os.date("%A")) .. ".")
74 local desc
75 do
76 local prefix = "Result"
77 desc = tostring(prefix) .. ":\nvalue: " .. tostring(compute())
78 end
79 print(("1\n2\n3"))
80end
81local yaml = "version: " .. tostring(ver) .. "\nok: true"
82_module_0["yaml"] = yaml
83return _module_0
diff --git a/spec/outputs/switch.lua b/spec/outputs/switch.lua
index 0f8bba2..7c1004b 100644
--- a/spec/outputs/switch.lua
+++ b/spec/outputs/switch.lua
@@ -656,4 +656,125 @@ do
656 end 656 end
657 end 657 end
658end 658end
659do
660 local v = "hello"
661 if "hello" == v then
662 print("matched hello")
663 else
664 print("not matched")
665 end
666end
667do
668 local f
669 f = function()
670 return "ok"
671 end
672 local val = f()
673 if "ok" == val then
674 print("it's ok")
675 end
676end
677do
678 local g
679 g = function()
680 return 42
681 end
682 local result = g()
683 if 1 == result or 2 == result then
684 print("small")
685 elseif 42 == result then
686 print("life universe everything")
687 else
688 print("other " .. tostring(result))
689 end
690end
691do
692 local check
693 check = function()
694 if true then
695 return "yes"
696 else
697 return "no"
698 end
699 end
700 local x = check()
701 if "yes" == x then
702 print("affirmative")
703 else
704 print("negative")
705 end
706end
707do
708 local t
709 t = function()
710 local tb = {
711 a = 1
712 }
713 tb.a = 2
714 return tb
715 end
716 local data = t()
717 local _type_0 = type(data)
718 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
719 local _match_0 = false
720 if _tab_0 then
721 if 2 == data.a then
722 _match_0 = true
723 print("matched")
724 end
725 end
726 if not _match_0 then
727 print("not matched")
728 end
729end
730do
731 local clientData = {
732 "Meta",
733 "CUST_1001",
734 "CHK123"
735 }
736 local _type_0 = type(clientData)
737 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
738 if _tab_0 then
739 local metadata
740 do
741 local _accum_0 = { }
742 local _len_0 = 1
743 local _max_0 = #clientData + -3 + 1
744 for _index_0 = 1, _max_0 do
745 local _item_0 = clientData[_index_0]
746 _accum_0[_len_0] = _item_0
747 _len_0 = _len_0 + 1
748 end
749 metadata = _accum_0
750 end
751 local customerId = clientData[#clientData - 1]
752 local checksum = clientData[#clientData]
753 if customerId ~= nil and checksum ~= nil then
754 print(metadata)
755 print(customerId)
756 print(checksum)
757 end
758 end
759end
760do
761 local handlePath
762 handlePath = function(segments)
763 local _type_0 = type(segments)
764 local _tab_0 = "table" == _type_0 or "userdata" == _type_0
765 if _tab_0 then
766 local resource = segments[#segments - 1]
767 local action = segments[#segments]
768 if resource ~= nil and action ~= nil then
769 print("Resource:", resource)
770 return print("Action:", action)
771 end
772 end
773 end
774 handlePath({
775 "admin",
776 "logs",
777 "view"
778 })
779end
659return nil 780return nil
diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua
index 040a325..2df3473 100644
--- a/spec/outputs/syntax.lua
+++ b/spec/outputs/syntax.lua
@@ -430,4 +430,15 @@ do
430 local f2 430 local f2
431 f2 = function() end 431 f2 = function() end
432end 432end
433do
434 if res ~= "" then
435 return res
436 end
437end
438do
439 return res((function()
440 if res ~= "" then
441 end
442 end)())
443end
433return nil 444return nil
diff --git a/spec/outputs/tables.lua b/spec/outputs/tables.lua
index f358811..3f851de 100644
--- a/spec/outputs/tables.lua
+++ b/spec/outputs/tables.lua
@@ -366,6 +366,28 @@ local menus = {
366 } 366 }
367 } 367 }
368} 368}
369_ = {
370 boolean = {
371 true,
372 false
373 },
374 float = {
375 3.14,
376 -6.8523015e+5
377 },
378 int = {
379 123,
380 -685230
381 },
382 null = {
383 nodeName = 'node',
384 parent = nil
385 },
386 string = {
387 'Hello world',
388 "newline\nnewline2"
389 }
390}
369local tb 391local tb
370do 392do
371 local _tab_0 = { } 393 local _tab_0 = { }
diff --git a/spec/outputs/test/format_spec.lua b/spec/outputs/test/format_spec.lua
index ed0fbee..7aa85cd 100644
--- a/spec/outputs/test/format_spec.lua
+++ b/spec/outputs/test/format_spec.lua
@@ -122,7 +122,7 @@ return describe("format", function()
122 local original_ast = yue.to_ast(code) 122 local original_ast = yue.to_ast(code)
123 assert.is_not_nil(original_ast) 123 assert.is_not_nil(original_ast)
124 rewriteLineCol(original_ast) 124 rewriteLineCol(original_ast)
125 local formated = yue.format(code) 125 local formated = yue.format(code, 0, false)
126 local ast = yue.to_ast(formated) 126 local ast = yue.to_ast(formated)
127 assert.is_not_nil(ast) 127 assert.is_not_nil(ast)
128 rewriteLineCol(ast) 128 rewriteLineCol(ast)
diff --git a/spec/outputs/try_catch.lua b/spec/outputs/try_catch.lua
index efd92c6..edb2341 100644
--- a/spec/outputs/try_catch.lua
+++ b/spec/outputs/try_catch.lua
@@ -22,6 +22,44 @@ end
22local _anon_func_7 = function(a, b, c, tb) 22local _anon_func_7 = function(a, b, c, tb)
23 return tb.f(a, b, c) 23 return tb.f(a, b, c)
24end 24end
25local _anon_func_8 = function(_arg_0, ...)
26 local ok = _arg_0
27 return ...
28end
29local _anon_func_10 = function(_arg_0, ...)
30 local _ok_0 = _arg_0
31 if _ok_0 then
32 return ...
33 end
34end
35local _anon_func_9 = function(func, pcall)
36 return _anon_func_10(pcall(func))
37end
38local _anon_func_12 = function(_arg_0, ...)
39 local _ok_0 = _arg_0
40 if _ok_0 then
41 return ...
42 end
43end
44local _anon_func_11 = function(func, pcall)
45 return _anon_func_12(pcall(func))
46end
47local _anon_func_14 = function(_arg_0, ...)
48 local _ok_0 = _arg_0
49 if _ok_0 then
50 return ...
51 end
52end
53local _anon_func_15 = function(func, print)
54 print(123)
55 return func()
56end
57local _anon_func_13 = function(func, print, xpcall)
58 return _anon_func_14(xpcall(_anon_func_15, function(e)
59 print(e)
60 return e
61 end, func, print))
62end
25local f 63local f
26f = function() 64f = function()
27 xpcall(function() 65 xpcall(function()
@@ -104,10 +142,236 @@ f = function()
104 do 142 do
105 x(function() 143 x(function()
106 local tb, a, b, c 144 local tb, a, b, c
107 f = function() 145 local f1
146 f1 = function()
108 return pcall(_anon_func_7, a, b, c, tb) 147 return pcall(_anon_func_7, a, b, c, tb)
109 end 148 end
110 end) 149 end)
111 end 150 end
151 do
152 local f1
153 f1 = function()
154 do
155 return _anon_func_8(pcall(function()
156 return func()
157 end))
158 end
159 end
160 end
161 do
162 local func
163 local a, b, c
164 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(func)
165 if _ok_0 then
166 a, b, c = _ret_0, _ret_1, _ret_2
167 end
168 end
169 do
170 local a, b, c
171 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
172 return func()
173 end)
174 if _ok_0 then
175 a, b, c = _ret_0, _ret_1, _ret_2
176 end
177 end
178 do
179 local a
180 local _exp_0 = (_anon_func_9(func, pcall))
181 if _exp_0 ~= nil then
182 a = _exp_0
183 else
184 a = "default"
185 end
186 end
187 do
188 f(_anon_func_11(func, pcall))
189 end
190 do
191 f(_anon_func_13(func, print, xpcall))
192 end
112 return nil 193 return nil
113end 194end
195local _anon_func_16 = function(a, b, c, tb)
196 return tb.f(a, b, c)
197end
198local _anon_func_17 = function(_arg_0, ...)
199 local ok = _arg_0
200 return ...
201end
202do
203 xpcall(function()
204 return func(1, 2, 3)
205 end, function(err)
206 return print(err)
207 end)
208 xpcall(function()
209 return func(1, 2, 3)
210 end, function(err)
211 return print(err)
212 end)
213 pcall(function()
214 print("trying")
215 return func(1, 2, 3)
216 end)
217 do
218 local success, result = xpcall(function()
219 return func(1, 2, 3)
220 end, function(err)
221 return print(err)
222 end)
223 success, result = pcall(function()
224 return func(1, 2, 3)
225 end)
226 end
227 local tb = { }
228 pcall(function()
229 return tb.func
230 end)
231 pcall(function()
232 return tb.func()
233 end)
234 pcall(function()
235 return tb.func()
236 end)
237 pcall(function()
238 return (tb.func())
239 end)
240 pcall(function()
241 return (tb:func(1, 2, 3))
242 end)
243 pcall(function()
244 return tb.func(1)
245 end)
246 pcall(function()
247 return tb.func(1)
248 end)
249 if (xpcall(function()
250 return func(1)
251 end, function(err)
252 return print(err)
253 end)) then
254 print("OK")
255 end
256 if xpcall(function()
257 return (func(1))
258 end, function(err)
259 return print(err)
260 end) then
261 print("OK")
262 end
263 do
264 do
265 local success, result = pcall(function()
266 return func("abc", 123)
267 end)
268 if success then
269 print(result)
270 end
271 end
272 local success, result = xpcall(function()
273 return func("abc", 123)
274 end, function(err)
275 return print(err)
276 end)
277 success, result = xpcall(function()
278 return func("abc", 123)
279 end, function(err)
280 return print(err)
281 end)
282 if success then
283 print(result)
284 end
285 end
286 do
287 pcall(function()
288 return func(1, 2, 3)
289 end)
290 pcall(function()
291 return func(1, 2, 3)
292 end)
293 end
294 do
295 x(function()
296 local tb, a, b, c
297 local f1
298 f1 = function()
299 return pcall(_anon_func_16, a, b, c, tb)
300 end
301 end)
302 end
303 do
304 local f1
305 f1 = function()
306 do
307 return _anon_func_17(pcall(function()
308 return func()
309 end))
310 end
311 end
312 end
313 do
314 local func
315 local a, b, c
316 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(func)
317 if _ok_0 then
318 a, b, c = _ret_0, _ret_1, _ret_2
319 end
320 end
321 do
322 local a, b, c
323 local _ok_0, _ret_0, _ret_1, _ret_2 = pcall(function()
324 return func()
325 end)
326 if _ok_0 then
327 a, b, c = _ret_0, _ret_1, _ret_2
328 end
329 end
330 do
331 local a
332 local _exp_0 = ((function()
333 return (function(_arg_0, ...)
334 local _ok_0 = _arg_0
335 if _ok_0 then
336 return ...
337 end
338 end)(pcall(function()
339 return func()
340 end))
341 end)())
342 if _exp_0 ~= nil then
343 a = _exp_0
344 else
345 a = "default"
346 end
347 end
348 do
349 f((function()
350 return (function(_arg_0, ...)
351 local _ok_0 = _arg_0
352 if _ok_0 then
353 return ...
354 end
355 end)(pcall(function()
356 return func()
357 end))
358 end)())
359 end
360 do
361 f((function()
362 return (function(_arg_0, ...)
363 local _ok_0 = _arg_0
364 if _ok_0 then
365 return ...
366 end
367 end)(xpcall(function()
368 print(123)
369 return func()
370 end, function(e)
371 print(e)
372 return e
373 end))
374 end)())
375 end
376end
377return nil
diff --git a/spec/outputs/unicode/assign.lua b/spec/outputs/unicode/assign.lua
index d4ad56a..e00d016 100644
--- a/spec/outputs/unicode/assign.lua
+++ b/spec/outputs/unicode/assign.lua
@@ -43,10 +43,8 @@ do
43 end 43 end
44end 44end
45local _anon_func_0 = function(_u6253_u5370) 45local _anon_func_0 = function(_u6253_u5370)
46 do 46 _u6253_u5370(123)
47 _u6253_u5370(123) 47 return { }
48 return { }
49 end
50end 48end
51return __u65e0_u6548_u53d8_u91cf(function() 49return __u65e0_u6548_u53d8_u91cf(function()
52 setmetatable(a_u53d8_u91cf, _anon_func_0(_u6253_u5370)) 50 setmetatable(a_u53d8_u91cf, _anon_func_0(_u6253_u5370))
diff --git a/spec/outputs/unicode/comprehension.lua b/spec/outputs/unicode/comprehension.lua
index 60e490f..92bce69 100644
--- a/spec/outputs/unicode/comprehension.lua
+++ b/spec/outputs/unicode/comprehension.lua
@@ -243,8 +243,11 @@ end
243do 243do
244 local _accum_0 = { } 244 local _accum_0 = { }
245 local _len_0 = 1 245 local _len_0 = 1
246 local _min_0 = 1 + 2
246 local _max_0 = 3 + 4 247 local _max_0 = 3 + 4
247 for _index_0 = 1 + 2, _max_0 < 0 and #_u5217_u8868 + _max_0 or _max_0 do 248 _min_0 = _min_0 < 0 and #_u5217_u8868 + _min_0 + 1 or _min_0
249 _max_0 = _max_0 < 0 and #_u5217_u8868 + _max_0 + 1 or _max_0
250 for _index_0 = _min_0, _max_0 do
248 local _u9879_u76ee = _u5217_u8868[_index_0] 251 local _u9879_u76ee = _u5217_u8868[_index_0]
249 _accum_0[_len_0] = _u9879_u76ee 252 _accum_0[_len_0] = _u9879_u76ee
250 _len_0 = _len_0 + 1 253 _len_0 = _len_0 + 1
@@ -254,8 +257,11 @@ end
254do 257do
255 local _accum_0 = { } 258 local _accum_0 = { }
256 local _len_0 = 1 259 local _len_0 = 1
260 local _min_0 = _u4f60_u597d() * 4
257 local _max_0 = 2 - _u4e1c_u897f[4] 261 local _max_0 = 2 - _u4e1c_u897f[4]
258 for _index_0 = _u4f60_u597d() * 4, _max_0 < 0 and #_u5217_u8868 + _max_0 or _max_0 do 262 _min_0 = _min_0 < 0 and #_u5217_u8868 + _min_0 + 1 or _min_0
263 _max_0 = _max_0 < 0 and #_u5217_u8868 + _max_0 + 1 or _max_0
264 for _index_0 = _min_0, _max_0 do
259 local _u9879_u76ee = _u5217_u8868[_index_0] 265 local _u9879_u76ee = _u5217_u8868[_index_0]
260 _accum_0[_len_0] = _u9879_u76ee 266 _accum_0[_len_0] = _u9879_u76ee
261 _len_0 = _len_0 + 1 267 _len_0 = _len_0 + 1
diff --git a/spec/outputs/unicode/funcs.lua b/spec/outputs/unicode/funcs.lua
index 6e94587..06b24b1 100644
--- a/spec/outputs/unicode/funcs.lua
+++ b/spec/outputs/unicode/funcs.lua
@@ -24,7 +24,7 @@ _u5f00(function()
24end) 24end)
25local _u53d8_u91cfh 25local _u53d8_u91cfh
26_u53d8_u91cfh = function() 26_u53d8_u91cfh = function()
27 return _ud83d_udc4b 27 return _u1f44b
28end 28end
29_u5403(function() end, _u4e16_u754c); 29_u5403(function() end, _u4e16_u754c);
30(function() end)() 30(function() end)()
diff --git a/spec/outputs/unicode/import.lua b/spec/outputs/unicode/import.lua
index 7c31ceb..e055c81 100644
--- a/spec/outputs/unicode/import.lua
+++ b/spec/outputs/unicode/import.lua
@@ -11,10 +11,10 @@ local _u5b57_u6bb5x, _u5b57_u6bb5y, _u5b57_u6bb5z = _u9053_u5177_u7ec4["字段x"
11 return _fn_0(_base_0, ...) 11 return _fn_0(_base_0, ...)
12 end 12 end
13end)(), _u9053_u5177_u7ec4["字段z"] 13end)(), _u9053_u5177_u7ec4["字段z"]
14local _u9886_u4e3b, _ud83d_udc7b 14local _u9886_u4e3b, _u1f47b
15do 15do
16 local _obj_1 = _u627e_u5230("我的表") 16 local _obj_1 = _u627e_u5230("我的表")
17 _u9886_u4e3b, _ud83d_udc7b = _obj_1["领主"], (function() 17 _u9886_u4e3b, _u1f47b = _obj_1["领主"], (function()
18 local _base_0 = _obj_1 18 local _base_0 = _obj_1
19 local _fn_0 = _base_0["👻"] 19 local _fn_0 = _base_0["👻"]
20 return _fn_0 and function(...) 20 return _fn_0 and function(...)
diff --git a/spec/outputs/unicode/lists.lua b/spec/outputs/unicode/lists.lua
index aafd516..3bf6f50 100644
--- a/spec/outputs/unicode/lists.lua
+++ b/spec/outputs/unicode/lists.lua
@@ -229,31 +229,36 @@ _u53d8_u91cfx = {
229 6, 229 6,
230 7 230 7
231} 231}
232local _max_0 = -5 232local _max_0 = #_u53d8_u91cfx + -5 + 1
233for _index_0 = 2, _max_0 < 0 and #_u53d8_u91cfx + _max_0 or _max_0, 2 do 233for _index_0 = 2, _max_0, 2 do
234 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0] 234 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0]
235 _u6253_u5370(_u53d8_u91cfy) 235 _u6253_u5370(_u53d8_u91cfy)
236end 236end
237local _max_1 = 3 237for _index_0 = 1, 3 do
238for _index_0 = 1, _max_1 < 0 and #_u53d8_u91cfx + _max_1 or _max_1 do
239 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0] 238 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0]
240 _u6253_u5370(_u53d8_u91cfy) 239 _u6253_u5370(_u53d8_u91cfy)
241end 240end
242for _index_0 = 2, #_u53d8_u91cfx do 241local _max_1 = #_u53d8_u91cfx
242for _index_0 = 2, _max_1 do
243 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0] 243 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0]
244 _u6253_u5370(_u53d8_u91cfy) 244 _u6253_u5370(_u53d8_u91cfy)
245end 245end
246for _index_0 = 1, #_u53d8_u91cfx, 2 do 246local _max_2 = #_u53d8_u91cfx
247for _index_0 = 1, _max_2, 2 do
247 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0] 248 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0]
248 _u6253_u5370(_u53d8_u91cfy) 249 _u6253_u5370(_u53d8_u91cfy)
249end 250end
250for _index_0 = 2, #_u53d8_u91cfx, 2 do 251local _max_3 = #_u53d8_u91cfx
252for _index_0 = 2, _max_3, 2 do
251 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0] 253 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0]
252 _u6253_u5370(_u53d8_u91cfy) 254 _u6253_u5370(_u53d8_u91cfy)
253end 255end
254local _u53d8_u91cfa, _u53d8_u91cfb, _u53d8_u91cfc = 1, 5, 2 256local _u53d8_u91cfa, _u53d8_u91cfb, _u53d8_u91cfc = 1, 5, 2
255local _max_2 = _u53d8_u91cfb 257local _min_0 = _u53d8_u91cfa
256for _index_0 = _u53d8_u91cfa, _max_2 < 0 and #_u53d8_u91cfx + _max_2 or _max_2, _u53d8_u91cfc do 258local _max_4 = _u53d8_u91cfb
259_min_0 = _min_0 < 0 and #_u53d8_u91cfx + _min_0 + 1 or _min_0
260_max_4 = _max_4 < 0 and #_u53d8_u91cfx + _max_4 + 1 or _max_4
261for _index_0 = _min_0, _max_4, _u53d8_u91cfc do
257 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0] 262 local _u53d8_u91cfy = _u53d8_u91cfx[_index_0]
258 _u6253_u5370(_u53d8_u91cfy) 263 _u6253_u5370(_u53d8_u91cfy)
259end 264end
diff --git a/spec/outputs/unicode/macro.lua b/spec/outputs/unicode/macro.lua
index 3b9327a..b4e78cd 100644
--- a/spec/outputs/unicode/macro.lua
+++ b/spec/outputs/unicode/macro.lua
@@ -216,17 +216,15 @@ do
216end 216end
217local _ = require('下划线') 217local _ = require('下划线')
218local _anon_func_0 = function(_) 218local _anon_func_0 = function(_)
219 do 219 local _call_0 = (_({
220 local _call_0 = (_({ 220 1,
221 1, 221 2,
222 2, 222 3,
223 3, 223 4,
224 4, 224 -2,
225 -2, 225 3
226 3 226 }))
227 })) 227 return _call_0["链"](_call_0)
228 return _call_0["链"](_call_0)
229 end
230end 228end
231local _call_0 = ((function() 229local _call_0 = ((function()
232 local _call_0 = ((function() 230 local _call_0 = ((function()
@@ -241,17 +239,15 @@ local _call_0 = ((function()
241end)()) 239end)())
242local _u7ed3_u679ca = _call_0["取值"](_call_0) 240local _u7ed3_u679ca = _call_0["取值"](_call_0)
243local _anon_func_1 = function(_) 241local _anon_func_1 = function(_)
244 do 242 local _call_1 = (_({
245 local _call_1 = (_({ 243 1,
246 1, 244 2,
247 2, 245 3,
248 3, 246 4,
249 4, 247 -2,
250 -2, 248 3
251 3 249 }))
252 })) 250 return _call_1["链"](_call_1)
253 return _call_1["链"](_call_1)
254 end
255end 251end
256do 252do
257 local _call_1 = ((function() 253 local _call_1 = ((function()
@@ -270,10 +266,8 @@ do
270 end) 266 end)
271end 267end
272local _anon_func_2 = function(_u539f_u70b9) 268local _anon_func_2 = function(_u539f_u70b9)
273 do 269 local _call_1 = _u539f_u70b9["变换"]["根节点"]["游戏对象"]
274 local _call_1 = _u539f_u70b9["变换"]["根节点"]["游戏对象"] 270 return _call_1["父节点"](_call_1)
275 return _call_1["父节点"](_call_1)
276 end
277end 271end
278local _call_1 = ((function() 272local _call_1 = ((function()
279 local _call_1 = ((function() 273 local _call_1 = ((function()
diff --git a/spec/outputs/unicode/multiline_chain.lua b/spec/outputs/unicode/multiline_chain.lua
index c1da13f..61e7057 100644
--- a/spec/outputs/unicode/multiline_chain.lua
+++ b/spec/outputs/unicode/multiline_chain.lua
@@ -59,10 +59,8 @@ _u51fd_u6570 = function()
59 return _accum_0 59 return _accum_0
60end 60end
61local _anon_func_0 = function(_u53d8_u91cfa) 61local _anon_func_0 = function(_u53d8_u91cfa)
62 do 62 local _call_1 = _u53d8_u91cfa
63 local _call_1 = _u53d8_u91cfa 63 return (_call_1["变量b"](_call_1, 123))["变量c"]("abc")
64 return (_call_1["变量b"](_call_1, 123))["变量c"]("abc")
65 end
66end 64end
67local _u51fd_u65701 65local _u51fd_u65701
68_u51fd_u65701 = function() 66_u51fd_u65701 = function()
diff --git a/spec/outputs/unicode/syntax.lua b/spec/outputs/unicode/syntax.lua
index 1984f40..a13302b 100644
--- a/spec/outputs/unicode/syntax.lua
+++ b/spec/outputs/unicode/syntax.lua
@@ -286,10 +286,8 @@ _ = 5 - _u4ec0_u4e48(_u65e0_u804a)
286_u4ec0_u4e48(_u65e0_u804a - 5) 286_u4ec0_u4e48(_u65e0_u804a - 5)
287_u53d8_u91cfx = _u4f60_u597d - _u4e16_u754c - _u67d0_u7269 287_u53d8_u91cfx = _u4f60_u597d - _u4e16_u754c - _u67d0_u7269
288local _anon_func_0 = function(_u4ec0_u4e48) 288local _anon_func_0 = function(_u4ec0_u4e48)
289 do 289 local _call_8 = _u4ec0_u4e48
290 local _call_8 = _u4ec0_u4e48 290 return _call_8["酷"](_call_8, 100)
291 return _call_8["酷"](_call_8, 100)
292 end
293end 291end
294(function(_u67d0_u7269) 292(function(_u67d0_u7269)
295 if _u67d0_u7269 == nil then 293 if _u67d0_u7269 == nil then
diff --git a/spec/outputs/unicode/vararg.lua b/spec/outputs/unicode/vararg.lua
index b837006..fc894ff 100644
--- a/spec/outputs/unicode/vararg.lua
+++ b/spec/outputs/unicode/vararg.lua
@@ -125,14 +125,10 @@ local _anon_func_11 = function(_u9879_u76ee, ...)
125 return _tbl_0 125 return _tbl_0
126end 126end
127local _anon_func_12 = function(_u51fd_u6570) 127local _anon_func_12 = function(_u51fd_u6570)
128 do 128 return _u51fd_u6570()
129 return _u51fd_u6570()
130 end
131end 129end
132local _anon_func_13 = function(_u51fd_u6570, ...) 130local _anon_func_13 = function(_u51fd_u6570, ...)
133 do 131 return _u51fd_u6570(...)
134 return _u51fd_u6570(...)
135 end
136end 132end
137local _anon_func_14 = function(_u51fd_u6570) 133local _anon_func_14 = function(_u51fd_u6570)
138 local _accum_0 = { } 134 local _accum_0 = { }
@@ -195,15 +191,11 @@ local _anon_func_23 = function(_u51fd_u6570, ...)
195 return nil 191 return nil
196end 192end
197local _anon_func_24 = function(_u6253_u5370, select, ...) 193local _anon_func_24 = function(_u6253_u5370, select, ...)
198 do 194 _u6253_u5370(select("#", ...))
199 _u6253_u5370(select("#", ...)) 195 return _u6253_u5370(...)
200 return _u6253_u5370(...)
201 end
202end 196end
203local _anon_func_25 = function(_u6253_u5370, ...) 197local _anon_func_25 = function(_u6253_u5370, ...)
204 do 198 return _u6253_u5370(...)
205 return _u6253_u5370(...)
206 end
207end 199end
208local _anon_func_26 = function(_u53d8_u91cfx, _u8868, _u88682) 200local _anon_func_26 = function(_u53d8_u91cfx, _u8868, _u88682)
209 if 1 == _u53d8_u91cfx then 201 if 1 == _u53d8_u91cfx then
@@ -214,9 +206,7 @@ local _anon_func_26 = function(_u53d8_u91cfx, _u8868, _u88682)
214 end 206 end
215end 207end
216local _anon_func_27 = function(_u6253_u5370, ...) 208local _anon_func_27 = function(_u6253_u5370, ...)
217 do 209 return _u6253_u5370(...)
218 return _u6253_u5370(...)
219 end
220end 210end
221local _anon_func_28 = function(_u6761_u4ef6) 211local _anon_func_28 = function(_u6761_u4ef6)
222 if _u6761_u4ef6 then 212 if _u6761_u4ef6 then
@@ -224,10 +214,8 @@ local _anon_func_28 = function(_u6761_u4ef6)
224 end 214 end
225end 215end
226local _anon_func_29 = function(_u6253_u5370, _arg_0, ...) 216local _anon_func_29 = function(_u6253_u5370, _arg_0, ...)
227 do 217 local _u8868 = _arg_0
228 local _u8868 = _arg_0 218 return _u6253_u5370(...)
229 return _u6253_u5370(...)
230 end
231end 219end
232local _u8fde_u63a5 220local _u8fde_u63a5
233_u8fde_u63a5 = function(...) 221_u8fde_u63a5 = function(...)
diff --git a/spec/outputs/upvalue_func.lua b/spec/outputs/upvalue_func.lua
index 3181adf..3e088be 100644
--- a/spec/outputs/upvalue_func.lua
+++ b/spec/outputs/upvalue_func.lua
@@ -214,10 +214,8 @@ local _anon_func_1 = function(valueB)
214 end 214 end
215end 215end
216local _anon_func_2 = function(print, select, _arg_0, ...) 216local _anon_func_2 = function(print, select, _arg_0, ...)
217 do 217 local ok = _arg_0
218 local ok = _arg_0 218 return print(select(3, ...))
219 return print(select(3, ...))
220 end
221end 219end
222local _anon_func_3 = function(tb) 220local _anon_func_3 = function(tb)
223 if tb ~= nil then 221 if tb ~= nil then
@@ -242,11 +240,9 @@ local _anon_func_5 = function(getmetatable, tb)
242 return _obj_0[1 + 1](_obj_0, "abc") 240 return _obj_0[1 + 1](_obj_0, "abc")
243end 241end
244local _anon_func_6 = function(tb) 242local _anon_func_6 = function(tb)
245 do 243 local _call_0 = tb
246 local _call_0 = tb 244 local _call_1 = _call_0["end"](_call_0)
247 local _call_1 = _call_0["end"](_call_0) 245 return _call_1["🤣"](_call_1, 123)
248 return _call_1["🤣"](_call_1, 123)
249 end
250end 246end
251local _anon_func_7 = function(itemA, listA) 247local _anon_func_7 = function(itemA, listA)
252 for _index_0 = 1, #listA do 248 for _index_0 = 1, #listA do
@@ -354,17 +350,13 @@ local _anon_func_16 = function(pairs, tb, tostring)
354 return _tbl_0 350 return _tbl_0
355end 351end
356local _anon_func_17 = function(print) 352local _anon_func_17 = function(print)
357 do 353 print(123)
358 print(123) 354 return "abc"
359 return "abc"
360 end
361end 355end
362local _anon_func_18 = function(print, select, _arg_0, ...) 356local _anon_func_18 = function(print, select, _arg_0, ...)
363 do 357 local success = _arg_0
364 local success = _arg_0 358 if success then
365 if success then 359 return print(select('#', ...))
366 return print(select('#', ...))
367 end
368 end 360 end
369end 361end
370local _anon_func_19 = function(cond, i) 362local _anon_func_19 = function(cond, i)
@@ -459,11 +451,9 @@ local _anon_func_25 = function(itemA, listA)
459 return false 451 return false
460end 452end
461local _anon_func_24 = function(itemA, listA, tb) 453local _anon_func_24 = function(itemA, listA, tb)
462 do 454 local _call_0 = tb
463 local _call_0 = tb 455 local _call_1 = _call_0["end"](_call_0)
464 local _call_1 = _call_0["end"](_call_0) 456 return _call_1["🤣"](_call_1, 123 and (#listA > 0 and _anon_func_25(itemA, listA)))
465 return _call_1["🤣"](_call_1, 123 and (#listA > 0 and _anon_func_25(itemA, listA)))
466 end
467end 457end
468GameEngine:onEvent("SomeEvent", function() 458GameEngine:onEvent("SomeEvent", function()
469 return func(value + (_anon_func_21(cond)) + (_anon_func_22(valueB)) > _anon_func_23(tb) + _anon_func_24(itemA, listA, tb)) 459 return func(value + (_anon_func_21(cond)) + (_anon_func_22(valueB)) > _anon_func_23(tb) + _anon_func_24(itemA, listA, tb))
@@ -503,13 +493,11 @@ local _anon_func_27 = function(char)
503 return nil 493 return nil
504end 494end
505local _anon_func_28 = function(os, _arg_0, ...) 495local _anon_func_28 = function(os, _arg_0, ...)
506 do 496 local ok = _arg_0
507 local ok = _arg_0 497 if ok then
508 if ok then 498 return ...
509 return ... 499 else
510 else 500 return os.exit(1)
511 return os.exit(1)
512 end
513 end 501 end
514end 502end
515local _anon_func_29 = function(debug_env_after, debug_env_before, env, func) 503local _anon_func_29 = function(debug_env_after, debug_env_before, env, func)
diff --git a/spec/outputs/vararg.lua b/spec/outputs/vararg.lua
index dabba44..9f97681 100644
--- a/spec/outputs/vararg.lua
+++ b/spec/outputs/vararg.lua
@@ -125,14 +125,10 @@ local _anon_func_11 = function(items, ...)
125 return _tbl_0 125 return _tbl_0
126end 126end
127local _anon_func_12 = function(func) 127local _anon_func_12 = function(func)
128 do 128 return func()
129 return func()
130 end
131end 129end
132local _anon_func_13 = function(func, ...) 130local _anon_func_13 = function(func, ...)
133 do 131 return func(...)
134 return func(...)
135 end
136end 132end
137local _anon_func_14 = function(func) 133local _anon_func_14 = function(func)
138 local _accum_0 = { } 134 local _accum_0 = { }
@@ -195,15 +191,11 @@ local _anon_func_23 = function(func, ...)
195 return nil 191 return nil
196end 192end
197local _anon_func_24 = function(print, select, ...) 193local _anon_func_24 = function(print, select, ...)
198 do 194 print(select("#", ...))
199 print(select("#", ...)) 195 return print(...)
200 return print(...)
201 end
202end 196end
203local _anon_func_25 = function(print, ...) 197local _anon_func_25 = function(print, ...)
204 do 198 return print(...)
205 return print(...)
206 end
207end 199end
208local _anon_func_26 = function(tb, tb2, x) 200local _anon_func_26 = function(tb, tb2, x)
209 if 1 == x then 201 if 1 == x then
@@ -214,9 +206,7 @@ local _anon_func_26 = function(tb, tb2, x)
214 end 206 end
215end 207end
216local _anon_func_27 = function(print, ...) 208local _anon_func_27 = function(print, ...)
217 do 209 return print(...)
218 return print(...)
219 end
220end 210end
221local _anon_func_28 = function(cond) 211local _anon_func_28 = function(cond)
222 if cond then 212 if cond then
@@ -224,10 +214,8 @@ local _anon_func_28 = function(cond)
224 end 214 end
225end 215end
226local _anon_func_29 = function(print, _arg_0, ...) 216local _anon_func_29 = function(print, _arg_0, ...)
227 do 217 local tb = _arg_0
228 local tb = _arg_0 218 return print(...)
229 return print(...)
230 end
231end 219end
232local join 220local join
233join = function(...) 221join = function(...)
@@ -306,3 +294,75 @@ join = function(...)
306 end 294 end
307 return nil 295 return nil
308end 296end
297do
298 local f1
299 f1 = function(...)
300 local t = {
301 n = select("#", ...),
302 ...
303 }
304 print(t.n)
305 print(#t)
306 for i = 1, t.n do
307 print(t[i])
308 end
309 end
310 f1(1, 2, 3)
311 f1("a", "b", "c", "d")
312 f1()
313 local f2
314 f2 = function(...)
315 local args = {
316 n = select("#", ...),
317 ...
318 }
319 print("args count:", args.n)
320 print("args length:", #args)
321 for i = 1, args.n do
322 if args[i] == nil then
323 print("position", i, "is nil")
324 else
325 print("position", i, ":", args[i])
326 end
327 end
328 end
329 f2(1, nil, 3, nil, 5)
330 local f3
331 f3 = function(prefix, ...)
332 local items = {
333 n = select("#", ...),
334 ...
335 }
336 local result = { }
337 for i = 1, items.n do
338 result[i] = prefix .. tostring(items[i])
339 end
340 return result
341 end
342 f3("item_", 1, 2, 3)
343 local f4
344 f4 = function(...)
345 local empty = {
346 n = select("#", ...),
347 ...
348 }
349 print("empty count:", empty.n)
350 return print("empty length:", #empty)
351 end
352 f4()
353 local process
354 process = function(...)
355 local data = {
356 n = select("#", ...),
357 ...
358 }
359 local sum = 0
360 for i = 1, data.n do
361 if type(data[i]) == "number" then
362 sum = sum + data[i]
363 end
364 end
365 return sum
366 end
367 return process(1, 2, 3, "skip", 5)
368end
diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua
index 20c5d44..530915e 100644
--- a/spec/outputs/with.lua
+++ b/spec/outputs/with.lua
@@ -192,26 +192,61 @@ do
192 local _with_0 = item 192 local _with_0 = item
193 do 193 do
194 local _accum_0 194 local _accum_0
195 while true do 195 repeat
196 if _with_0.id > 0 then 196 if _with_0.id > 0 then
197 _accum_0 = _with_0.content 197 _accum_0 = _with_0.content
198 break 198 break
199 end 199 end
200 end 200 until true
201 _with_0 = _accum_0 201 _with_0 = _accum_0
202 end 202 end
203 return _with_0 203 return _with_0
204 end)()) 204 end)())
205 local a 205 local a
206 local _with_0 = tb 206 do
207 local _with_0 = tb
208 do
209 local _accum_0
210 repeat
211 if _with_0.v then
212 _accum_0 = _with_0.a
213 break
214 end
215 until true
216 _with_0 = _accum_0
217 end
218 a = _with_0
219 end
207 local _accum_0 220 local _accum_0
208 while true do 221 while true do
209 if _with_0.v then 222 local _with_0 = tb
210 _accum_0 = _with_0.a 223 local _accum_1
211 break 224 repeat
225 if _with_0 ~= nil then
226 _accum_1 = 1
227 break
228 end
229 until true
230 _with_0 = _accum_1
231 _accum_0 = _with_0
232 break
233 end
234 a = _accum_0
235end
236do
237 local a
238 local _accum_0
239 for i = 1, 100 do
240 local x = tb[i]
241 if x ~= nil then
242 local _des_0 = 1
243 if _des_0 then
244 x.id = _des_0
245 _accum_0 = x
246 break
247 end
212 end 248 end
213 end 249 end
214 _with_0 = _accum_0 250 a = _accum_0
215 a = _with_0
216end 251end
217return nil 252return nil