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/outputs/lists.lua | |
| 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/outputs/lists.lua')
| -rw-r--r-- | spec/outputs/lists.lua | 192 |
1 files changed, 189 insertions, 3 deletions
diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua index 48ec9c8..1bdfa3e 100644 --- a/spec/outputs/lists.lua +++ b/spec/outputs/lists.lua | |||
| @@ -231,12 +231,12 @@ x = { | |||
| 231 | 7 | 231 | 7 |
| 232 | } | 232 | } |
| 233 | local _max_0 = -5 | 233 | local _max_0 = -5 |
| 234 | for _index_0 = 2, _max_0 < 0 and #x + _max_0 or _max_0, 2 do | 234 | for _index_0 = 2, _max_0 < 0 and #x + _max_0 + 1 or _max_0, 2 do |
| 235 | local y = x[_index_0] | 235 | local y = x[_index_0] |
| 236 | print(y) | 236 | print(y) |
| 237 | end | 237 | end |
| 238 | local _max_1 = 3 | 238 | local _max_1 = 3 |
| 239 | for _index_0 = 1, _max_1 < 0 and #x + _max_1 or _max_1 do | 239 | for _index_0 = 1, _max_1 < 0 and #x + _max_1 + 1 or _max_1 do |
| 240 | local y = x[_index_0] | 240 | local y = x[_index_0] |
| 241 | print(y) | 241 | print(y) |
| 242 | end | 242 | end |
| @@ -254,7 +254,7 @@ for _index_0 = 2, #x, 2 do | |||
| 254 | end | 254 | end |
| 255 | local a, b, c = 1, 5, 2 | 255 | local a, b, c = 1, 5, 2 |
| 256 | local _max_2 = b | 256 | local _max_2 = b |
| 257 | for _index_0 = a, _max_2 < 0 and #x + _max_2 or _max_2, c do | 257 | for _index_0 = a, _max_2 < 0 and #x + _max_2 + 1 or _max_2, c do |
| 258 | local y = x[_index_0] | 258 | local y = x[_index_0] |
| 259 | print(y) | 259 | print(y) |
| 260 | end | 260 | end |
| @@ -327,4 +327,190 @@ do | |||
| 327 | job = "jobless" | 327 | job = "jobless" |
| 328 | end | 328 | end |
| 329 | end | 329 | end |
| 330 | do | ||
| 331 | local transactions = { | ||
| 332 | "T001", | ||
| 333 | "T002", | ||
| 334 | "T003", | ||
| 335 | "T004", | ||
| 336 | "T005" | ||
| 337 | } | ||
| 338 | local middleTransactions | ||
| 339 | do | ||
| 340 | local _accum_0 = { } | ||
| 341 | local _len_0 = 1 | ||
| 342 | local _max_3 = -2 | ||
| 343 | for _index_0 = 2, _max_3 < 0 and #transactions + _max_3 + 1 or _max_3 do | ||
| 344 | local _item_0 = transactions[_index_0] | ||
| 345 | _accum_0[_len_0] = _item_0 | ||
| 346 | _len_0 = _len_0 + 1 | ||
| 347 | end | ||
| 348 | middleTransactions = _accum_0 | ||
| 349 | end | ||
| 350 | print(middleTransactions) | ||
| 351 | end | ||
| 352 | do | ||
| 353 | local logs = { | ||
| 354 | { | ||
| 355 | start = 0, | ||
| 356 | ["end"] = 100 | ||
| 357 | }, | ||
| 358 | { | ||
| 359 | start = 100, | ||
| 360 | ["end"] = 200 | ||
| 361 | }, | ||
| 362 | { | ||
| 363 | start = 200, | ||
| 364 | ["end"] = 123 | ||
| 365 | } | ||
| 366 | } | ||
| 367 | print(logs[#logs]["end"]) | ||
| 368 | end | ||
| 369 | do | ||
| 370 | local pendingOrders = { | ||
| 371 | "O001", | ||
| 372 | "O002", | ||
| 373 | "O003", | ||
| 374 | "O004" | ||
| 375 | } | ||
| 376 | print(pendingOrders[#pendingOrders - 1]) | ||
| 377 | end | ||
| 378 | do | ||
| 379 | local getOrders | ||
| 380 | getOrders = function() | ||
| 381 | return { | ||
| 382 | { | ||
| 383 | id = "O1001", | ||
| 384 | status = "pending" | ||
| 385 | }, | ||
| 386 | { | ||
| 387 | id = "O1002", | ||
| 388 | status = "processing" | ||
| 389 | }, | ||
| 390 | { | ||
| 391 | id = "O1003", | ||
| 392 | status = "done" | ||
| 393 | } | ||
| 394 | } | ||
| 395 | end | ||
| 396 | local lastStatus | ||
| 397 | do | ||
| 398 | local _item_0 = getOrders() | ||
| 399 | lastStatus = _item_0[#_item_0].status | ||
| 400 | end | ||
| 401 | assert(lastStatus == "done") | ||
| 402 | end | ||
| 403 | do | ||
| 404 | local cloneList1 | ||
| 405 | cloneList1 = function(list) | ||
| 406 | local _accum_0 = { } | ||
| 407 | local _len_0 = 1 | ||
| 408 | for _index_0 = 1, #list do | ||
| 409 | local _item_0 = list[_index_0] | ||
| 410 | _accum_0[_len_0] = _item_0 | ||
| 411 | _len_0 = _len_0 + 1 | ||
| 412 | end | ||
| 413 | return _accum_0 | ||
| 414 | end | ||
| 415 | local cloneList2 | ||
| 416 | cloneList2 = function(list) | ||
| 417 | local _tab_0 = { } | ||
| 418 | local _idx_0 = #_tab_0 + 1 | ||
| 419 | for _index_0 = 1, #list do | ||
| 420 | local _value_0 = list[_index_0] | ||
| 421 | _tab_0[_idx_0] = _value_0 | ||
| 422 | _idx_0 = _idx_0 + 1 | ||
| 423 | end | ||
| 424 | return _tab_0 | ||
| 425 | end | ||
| 426 | local cloneTable | ||
| 427 | cloneTable = function(tb) | ||
| 428 | local _tab_0 = { } | ||
| 429 | local _idx_0 = 1 | ||
| 430 | for _key_0, _value_0 in pairs(tb) do | ||
| 431 | if _idx_0 == _key_0 then | ||
| 432 | _tab_0[#_tab_0 + 1] = _value_0 | ||
| 433 | _idx_0 = _idx_0 + 1 | ||
| 434 | else | ||
| 435 | _tab_0[_key_0] = _value_0 | ||
| 436 | end | ||
| 437 | end | ||
| 438 | return _tab_0 | ||
| 439 | end | ||
| 440 | end | ||
| 441 | do | ||
| 442 | print((function() | ||
| 443 | local _item_0 = globalTB | ||
| 444 | return _item_0[#_item_0] | ||
| 445 | end)(), (function() | ||
| 446 | local _item_0 = a.b.c | ||
| 447 | return _item_0[#_item_0 - 2] | ||
| 448 | end)(), (function() | ||
| 449 | if x ~= nil then | ||
| 450 | local _obj_0 = x.y | ||
| 451 | if _obj_0 ~= nil then | ||
| 452 | local _obj_1 = _obj_0(x).z | ||
| 453 | if _obj_1 ~= nil then | ||
| 454 | return _obj_1[#_obj_1 - 3] | ||
| 455 | end | ||
| 456 | return nil | ||
| 457 | end | ||
| 458 | return nil | ||
| 459 | end | ||
| 460 | return nil | ||
| 461 | end)()) | ||
| 462 | end | ||
| 463 | local _anon_func_0 = function(globalTB) | ||
| 464 | local _item_0 = globalTB | ||
| 465 | local _call_0 = _item_0[#_item_0] | ||
| 466 | return _call_0["end"](_call_0, 123) | ||
| 467 | end | ||
| 468 | local _anon_func_1 = function(a) | ||
| 469 | local _item_0 | ||
| 470 | do | ||
| 471 | local _accum_0 = { } | ||
| 472 | local _len_0 = 1 | ||
| 473 | local _list_0 = a.b.c | ||
| 474 | local _max_3 = -5 | ||
| 475 | for _index_0 = 5, _max_3 < 0 and #_list_0 + _max_3 + 1 or _max_3 do | ||
| 476 | local _item_1 = _list_0[_index_0] | ||
| 477 | _accum_0[_len_0] = _item_1 | ||
| 478 | _len_0 = _len_0 + 1 | ||
| 479 | end | ||
| 480 | _item_0 = _accum_0 | ||
| 481 | end | ||
| 482 | return _item_0[#_item_0 - 2] | ||
| 483 | end | ||
| 484 | local _anon_func_2 = function(x) | ||
| 485 | if x ~= nil then | ||
| 486 | local _obj_0 = x.y | ||
| 487 | if _obj_0 ~= nil then | ||
| 488 | local _obj_1 = _obj_0(x).z | ||
| 489 | if _obj_1 ~= nil then | ||
| 490 | local _obj_2 = _obj_1[#_obj_1 - 3] | ||
| 491 | if _obj_2 ~= nil then | ||
| 492 | local _accum_0 = { } | ||
| 493 | local _len_0 = 1 | ||
| 494 | local _max_3 = -3 | ||
| 495 | for _index_0 = 1, _max_3 < 0 and #_obj_2 + _max_3 + 1 or _max_3 do | ||
| 496 | local _item_0 = _obj_2[_index_0] | ||
| 497 | _accum_0[_len_0] = _item_0 | ||
| 498 | _len_0 = _len_0 + 1 | ||
| 499 | end | ||
| 500 | return _accum_0 | ||
| 501 | end | ||
| 502 | return nil | ||
| 503 | end | ||
| 504 | return nil | ||
| 505 | end | ||
| 506 | return nil | ||
| 507 | end | ||
| 508 | return nil | ||
| 509 | end | ||
| 510 | do | ||
| 511 | local f | ||
| 512 | f = function() | ||
| 513 | return print(_anon_func_0(globalTB), _anon_func_1(a), _anon_func_2(x)) | ||
| 514 | end | ||
| 515 | end | ||
| 330 | return nil | 516 | return nil |
