Skip to content

Commit 548ab1d

Browse files
committed
Added new syntax.
- Slice Expression. - Reversed Indexing,. - Range Destructuring. - Table Range Matching.
1 parent 98be64d commit 548ab1d

16 files changed

Lines changed: 678 additions & 51 deletions

spec/inputs/destructure.yue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,43 @@ do
240240
switch tb
241241
when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}}
242242
print meta_field, abc, def
243+
244+
do
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+
251+
do
252+
setupMeeting = (participants) ->
253+
[chair, ..._, secretary] = participants
254+
print chair, secretary
255+
256+
setupMeeting ["Alice", "Bob", "Charlie", "David"]
257+
-- Output: Alice David
258+
259+
do
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+
271+
do
272+
[
273+
_
274+
...middle
275+
_
276+
] = tb
277+
278+
do
279+
{a, :abc, b, :def, ...sub, d, e} = tb
280+
243281
nil
244282

spec/inputs/lists.yue

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,51 @@ do
8787
[a, b] = hello
8888
[name = "nameless", job = "jobless"] = person
8989

90+
do
91+
transactions = ["T001", "T002", "T003", "T004", "T005"]
92+
middleTransactions = transactions[2, -2]
93+
print middleTransactions -- => {"T002", "T003", "T004"}
94+
95+
do
96+
logs =
97+
- start: 0, end: 100
98+
- start: 100, end: 200
99+
- start: 200, end: 123
100+
print logs[#].end -- => 123
101+
102+
do
103+
pendingOrders = ["O001", "O002", "O003", "O004"]
104+
print pendingOrders[# - 1] -- => "O003"
105+
106+
do
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+
117+
do
118+
cloneList1 = (list) -> list[,]
119+
cloneList2 = (list) -> [...list,]
120+
cloneTable = (tb) -> {...tb}
121+
122+
do
123+
print(
124+
globalTB[#]
125+
a.b.c[# - 2]
126+
x?\y?!.z?[# - 3]
127+
)
128+
129+
do
130+
f = ->
131+
print(
132+
globalTB[#]\end 123
133+
a.b.c[5,-5][# - 2]
134+
x?\y?!.z?[# - 3]?[, -3]
135+
)
136+
90137
nil

spec/inputs/switch.yue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,21 @@ do
272272
else
273273
print "not matched"
274274

275+
do
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+
283+
do
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+
275292
nil

spec/outputs/codes_from_doc.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ local _accum_0 = { }
10631063
local _len_0 = 1
10641064
local _list_0 = items
10651065
local _max_0 = 5
1066-
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
1066+
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
10671067
local item = _list_0[_index_0]
10681068
_accum_0[_len_0] = item
10691069
_len_0 = _len_0 + 1
@@ -1100,7 +1100,7 @@ for key, value in pairs(object) do
11001100
end
11011101
local _list_0 = items
11021102
local _max_0 = 4
1103-
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
1103+
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
11041104
local item = _list_0[_index_0]
11051105
print(item)
11061106
end
@@ -3333,7 +3333,7 @@ local _accum_0 = { }
33333333
local _len_0 = 1
33343334
local _list_0 = items
33353335
local _max_0 = 5
3336-
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3336+
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
33373337
local item = _list_0[_index_0]
33383338
_accum_0[_len_0] = item
33393339
_len_0 = _len_0 + 1
@@ -3370,7 +3370,7 @@ for key, value in pairs(object) do
33703370
end
33713371
local _list_0 = items
33723372
local _max_0 = 4
3373-
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3373+
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
33743374
local item = _list_0[_index_0]
33753375
print(item)
33763376
end

spec/outputs/codes_from_doc_zh.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ local _accum_0 = { }
10571057
local _len_0 = 1
10581058
local _list_0 = items
10591059
local _max_0 = 5
1060-
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
1060+
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
10611061
local item = _list_0[_index_0]
10621062
_accum_0[_len_0] = item
10631063
_len_0 = _len_0 + 1
@@ -1094,7 +1094,7 @@ for key, value in pairs(object) do
10941094
end
10951095
local _list_0 = items
10961096
local _max_0 = 4
1097-
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
1097+
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
10981098
local item = _list_0[_index_0]
10991099
print(item)
11001100
end
@@ -3321,7 +3321,7 @@ local _accum_0 = { }
33213321
local _len_0 = 1
33223322
local _list_0 = items
33233323
local _max_0 = 5
3324-
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3324+
for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
33253325
local item = _list_0[_index_0]
33263326
_accum_0[_len_0] = item
33273327
_len_0 = _len_0 + 1
@@ -3358,7 +3358,7 @@ for key, value in pairs(object) do
33583358
end
33593359
local _list_0 = items
33603360
local _max_0 = 4
3361-
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
3361+
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
33623362
local item = _list_0[_index_0]
33633363
print(item)
33643364
end

spec/outputs/comprehension.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ do
244244
local _accum_0 = { }
245245
local _len_0 = 1
246246
local _max_0 = 3 + 4
247-
for _index_0 = 1 + 2, _max_0 < 0 and #items + _max_0 or _max_0 do
247+
for _index_0 = 1 + 2, _max_0 < 0 and #items + _max_0 + 1 or _max_0 do
248248
local item = items[_index_0]
249249
_accum_0[_len_0] = item
250250
_len_0 = _len_0 + 1
@@ -255,7 +255,7 @@ do
255255
local _accum_0 = { }
256256
local _len_0 = 1
257257
local _max_0 = 2 - thing[4]
258-
for _index_0 = hello() * 4, _max_0 < 0 and #items + _max_0 or _max_0 do
258+
for _index_0 = hello() * 4, _max_0 < 0 and #items + _max_0 + 1 or _max_0 do
259259
local item = items[_index_0]
260260
_accum_0[_len_0] = item
261261
_len_0 = _len_0 + 1

spec/outputs/destructure.lua

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,94 @@ do
621621
print(meta_field, abc, def)
622622
end
623623
end
624+
do
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 = -2
635+
for _index_0 = 2, _max_0 < 0 and #clients + _max_0 + 1 or _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)
645+
end
646+
do
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+
})
658+
end
659+
do
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)
685+
end
686+
do
687+
local middle
688+
local _accum_0 = { }
689+
local _len_0 = 1
690+
local _list_0 = tb
691+
local _max_0 = -2
692+
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _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
698+
end
699+
do
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 = -3
706+
for _index_0 = 3, _max_0 < 0 and #_obj_0 + _max_0 + 1 or _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]
713+
end
624714
return nil

0 commit comments

Comments
 (0)