diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-06-04 11:38:34 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-06-04 11:38:34 +0800 |
| commit | 548ab1d9ff5b831050f14f1355a3314a41163ad6 (patch) | |
| tree | d84b6b64b4e547070c7c43db53821b83b819ab8e /spec/inputs | |
| parent | 98be64dd52df92f7fdd40bae225c279db1676cab (diff) | |
| download | yuescript-548ab1d9ff5b831050f14f1355a3314a41163ad6.tar.gz yuescript-548ab1d9ff5b831050f14f1355a3314a41163ad6.tar.bz2 yuescript-548ab1d9ff5b831050f14f1355a3314a41163ad6.zip | |
Added new syntax.
- Slice Expression.
- Reversed Indexing,.
- Range Destructuring.
- Table Range Matching.
Diffstat (limited to 'spec/inputs')
| -rw-r--r-- | spec/inputs/destructure.yue | 38 | ||||
| -rw-r--r-- | spec/inputs/lists.yue | 47 | ||||
| -rw-r--r-- | spec/inputs/switch.yue | 17 |
3 files changed, 102 insertions, 0 deletions
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue index 9f01a20..b6250d0 100644 --- a/spec/inputs/destructure.yue +++ b/spec/inputs/destructure.yue | |||
| @@ -240,5 +240,43 @@ 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 | |||
| 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 | |||
| 243 | nil | 281 | nil |
| 244 | 282 | ||
diff --git a/spec/inputs/lists.yue b/spec/inputs/lists.yue index 921cae0..c493b68 100644 --- a/spec/inputs/lists.yue +++ b/spec/inputs/lists.yue | |||
| @@ -87,4 +87,51 @@ do | |||
| 87 | [a, b] = hello | 87 | [a, b] = hello |
| 88 | [name = "nameless", job = "jobless"] = person | 88 | [name = "nameless", job = "jobless"] = person |
| 89 | 89 | ||
| 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 | |||
| 90 | nil | 137 | nil |
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue index 5097db3..2b0669c 100644 --- a/spec/inputs/switch.yue +++ b/spec/inputs/switch.yue | |||
| @@ -272,4 +272,21 @@ do | |||
| 272 | else | 272 | else |
| 273 | print "not matched" | 273 | print "not matched" |
| 274 | 274 | ||
| 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 | |||
| 275 | nil | 292 | nil |
