aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-06-04 16:07:06 +0800
committerLi Jin <dragon-fly@qq.com>2025-06-04 16:07:06 +0800
commit0e72454a11f65d9ac800dedb698ddfcc15933785 (patch)
tree6cded6335c22c4dd204f7d3bc66efa0f76a41ef0
parent548ab1d9ff5b831050f14f1355a3314a41163ad6 (diff)
downloadyuescript-0e72454a11f65d9ac800dedb698ddfcc15933785.tar.gz
yuescript-0e72454a11f65d9ac800dedb698ddfcc15933785.tar.bz2
yuescript-0e72454a11f65d9ac800dedb698ddfcc15933785.zip
Added more reversed index support for slice. [skip CI]
-rw-r--r--spec/outputs/codes_from_doc.lua24
-rw-r--r--spec/outputs/codes_from_doc_zh.lua24
-rw-r--r--spec/outputs/comprehension.lua10
-rw-r--r--spec/outputs/destructure.lua12
-rw-r--r--spec/outputs/lists.lua43
-rw-r--r--spec/outputs/switch.lua4
-rw-r--r--spec/outputs/unicode/comprehension.lua10
-rw-r--r--spec/outputs/unicode/lists.lua23
-rw-r--r--src/yuescript/yue_compiler.cpp91
9 files changed, 174 insertions, 67 deletions
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index b8dd9b7..e137823 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -1062,8 +1062,7 @@ local slice
1062local _accum_0 = { } 1062local _accum_0 = { }
1063local _len_0 = 1 1063local _len_0 = 1
1064local _list_0 = items 1064local _list_0 = items
1065local _max_0 = 5 1065for _index_0 = 1, 5 do
1066for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
1067 local item = _list_0[_index_0] 1066 local item = _list_0[_index_0]
1068 _accum_0[_len_0] = item 1067 _accum_0[_len_0] = item
1069 _len_0 = _len_0 + 1 1068 _len_0 = _len_0 + 1
@@ -1073,7 +1072,8 @@ local slice
1073local _accum_0 = { } 1072local _accum_0 = { }
1074local _len_0 = 1 1073local _len_0 = 1
1075local _list_0 = items 1074local _list_0 = items
1076for _index_0 = 2, #_list_0 do 1075local _max_0 = #_list_0
1076for _index_0 = 2, _max_0 do
1077 local item = _list_0[_index_0] 1077 local item = _list_0[_index_0]
1078 _accum_0[_len_0] = item 1078 _accum_0[_len_0] = item
1079 _len_0 = _len_0 + 1 1079 _len_0 = _len_0 + 1
@@ -1083,7 +1083,8 @@ local slice
1083local _accum_0 = { } 1083local _accum_0 = { }
1084local _len_0 = 1 1084local _len_0 = 1
1085local _list_0 = items 1085local _list_0 = items
1086for _index_0 = 1, #_list_0, 2 do 1086local _max_0 = #_list_0
1087for _index_0 = 1, _max_0, 2 do
1087 local item = _list_0[_index_0] 1088 local item = _list_0[_index_0]
1088 _accum_0[_len_0] = item 1089 _accum_0[_len_0] = item
1089 _len_0 = _len_0 + 1 1090 _len_0 = _len_0 + 1
@@ -1099,8 +1100,7 @@ for key, value in pairs(object) do
1099 print(key, value) 1100 print(key, value)
1100end 1101end
1101local _list_0 = items 1102local _list_0 = items
1102local _max_0 = 4 1103for _index_0 = 2, 4 do
1103for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
1104 local item = _list_0[_index_0] 1104 local item = _list_0[_index_0]
1105 print(item) 1105 print(item)
1106end 1106end
@@ -3332,8 +3332,7 @@ local slice
3332local _accum_0 = { } 3332local _accum_0 = { }
3333local _len_0 = 1 3333local _len_0 = 1
3334local _list_0 = items 3334local _list_0 = items
3335local _max_0 = 5 3335for _index_0 = 1, 5 do
3336for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
3337 local item = _list_0[_index_0] 3336 local item = _list_0[_index_0]
3338 _accum_0[_len_0] = item 3337 _accum_0[_len_0] = item
3339 _len_0 = _len_0 + 1 3338 _len_0 = _len_0 + 1
@@ -3343,7 +3342,8 @@ local slice
3343local _accum_0 = { } 3342local _accum_0 = { }
3344local _len_0 = 1 3343local _len_0 = 1
3345local _list_0 = items 3344local _list_0 = items
3346for _index_0 = 2, #_list_0 do 3345local _max_0 = #_list_0
3346for _index_0 = 2, _max_0 do
3347 local item = _list_0[_index_0] 3347 local item = _list_0[_index_0]
3348 _accum_0[_len_0] = item 3348 _accum_0[_len_0] = item
3349 _len_0 = _len_0 + 1 3349 _len_0 = _len_0 + 1
@@ -3353,7 +3353,8 @@ local slice
3353local _accum_0 = { } 3353local _accum_0 = { }
3354local _len_0 = 1 3354local _len_0 = 1
3355local _list_0 = items 3355local _list_0 = items
3356for _index_0 = 1, #_list_0, 2 do 3356local _max_0 = #_list_0
3357for _index_0 = 1, _max_0, 2 do
3357 local item = _list_0[_index_0] 3358 local item = _list_0[_index_0]
3358 _accum_0[_len_0] = item 3359 _accum_0[_len_0] = item
3359 _len_0 = _len_0 + 1 3360 _len_0 = _len_0 + 1
@@ -3369,8 +3370,7 @@ for key, value in pairs(object) do
3369 print(key, value) 3370 print(key, value)
3370end 3371end
3371local _list_0 = items 3372local _list_0 = items
3372local _max_0 = 4 3373for _index_0 = 2, 4 do
3373for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
3374 local item = _list_0[_index_0] 3374 local item = _list_0[_index_0]
3375 print(item) 3375 print(item)
3376end 3376end
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index 80644a5..c6ba43d 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -1056,8 +1056,7 @@ local slice
1056local _accum_0 = { } 1056local _accum_0 = { }
1057local _len_0 = 1 1057local _len_0 = 1
1058local _list_0 = items 1058local _list_0 = items
1059local _max_0 = 5 1059for _index_0 = 1, 5 do
1060for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
1061 local item = _list_0[_index_0] 1060 local item = _list_0[_index_0]
1062 _accum_0[_len_0] = item 1061 _accum_0[_len_0] = item
1063 _len_0 = _len_0 + 1 1062 _len_0 = _len_0 + 1
@@ -1067,7 +1066,8 @@ local slice
1067local _accum_0 = { } 1066local _accum_0 = { }
1068local _len_0 = 1 1067local _len_0 = 1
1069local _list_0 = items 1068local _list_0 = items
1070for _index_0 = 2, #_list_0 do 1069local _max_0 = #_list_0
1070for _index_0 = 2, _max_0 do
1071 local item = _list_0[_index_0] 1071 local item = _list_0[_index_0]
1072 _accum_0[_len_0] = item 1072 _accum_0[_len_0] = item
1073 _len_0 = _len_0 + 1 1073 _len_0 = _len_0 + 1
@@ -1077,7 +1077,8 @@ local slice
1077local _accum_0 = { } 1077local _accum_0 = { }
1078local _len_0 = 1 1078local _len_0 = 1
1079local _list_0 = items 1079local _list_0 = items
1080for _index_0 = 1, #_list_0, 2 do 1080local _max_0 = #_list_0
1081for _index_0 = 1, _max_0, 2 do
1081 local item = _list_0[_index_0] 1082 local item = _list_0[_index_0]
1082 _accum_0[_len_0] = item 1083 _accum_0[_len_0] = item
1083 _len_0 = _len_0 + 1 1084 _len_0 = _len_0 + 1
@@ -1093,8 +1094,7 @@ for key, value in pairs(object) do
1093 print(key, value) 1094 print(key, value)
1094end 1095end
1095local _list_0 = items 1096local _list_0 = items
1096local _max_0 = 4 1097for _index_0 = 2, 4 do
1097for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
1098 local item = _list_0[_index_0] 1098 local item = _list_0[_index_0]
1099 print(item) 1099 print(item)
1100end 1100end
@@ -3320,8 +3320,7 @@ local slice
3320local _accum_0 = { } 3320local _accum_0 = { }
3321local _len_0 = 1 3321local _len_0 = 1
3322local _list_0 = items 3322local _list_0 = items
3323local _max_0 = 5 3323for _index_0 = 1, 5 do
3324for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
3325 local item = _list_0[_index_0] 3324 local item = _list_0[_index_0]
3326 _accum_0[_len_0] = item 3325 _accum_0[_len_0] = item
3327 _len_0 = _len_0 + 1 3326 _len_0 = _len_0 + 1
@@ -3331,7 +3330,8 @@ local slice
3331local _accum_0 = { } 3330local _accum_0 = { }
3332local _len_0 = 1 3331local _len_0 = 1
3333local _list_0 = items 3332local _list_0 = items
3334for _index_0 = 2, #_list_0 do 3333local _max_0 = #_list_0
3334for _index_0 = 2, _max_0 do
3335 local item = _list_0[_index_0] 3335 local item = _list_0[_index_0]
3336 _accum_0[_len_0] = item 3336 _accum_0[_len_0] = item
3337 _len_0 = _len_0 + 1 3337 _len_0 = _len_0 + 1
@@ -3341,7 +3341,8 @@ local slice
3341local _accum_0 = { } 3341local _accum_0 = { }
3342local _len_0 = 1 3342local _len_0 = 1
3343local _list_0 = items 3343local _list_0 = items
3344for _index_0 = 1, #_list_0, 2 do 3344local _max_0 = #_list_0
3345for _index_0 = 1, _max_0, 2 do
3345 local item = _list_0[_index_0] 3346 local item = _list_0[_index_0]
3346 _accum_0[_len_0] = item 3347 _accum_0[_len_0] = item
3347 _len_0 = _len_0 + 1 3348 _len_0 = _len_0 + 1
@@ -3357,8 +3358,7 @@ for key, value in pairs(object) do
3357 print(key, value) 3358 print(key, value)
3358end 3359end
3359local _list_0 = items 3360local _list_0 = items
3360local _max_0 = 4 3361for _index_0 = 2, 4 do
3361for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do
3362 local item = _list_0[_index_0] 3362 local item = _list_0[_index_0]
3363 print(item) 3363 print(item)
3364end 3364end
diff --git a/spec/outputs/comprehension.lua b/spec/outputs/comprehension.lua
index ac4f9d8..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 + 1 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 + 1 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 216d921..ba216b5 100644
--- a/spec/outputs/destructure.lua
+++ b/spec/outputs/destructure.lua
@@ -631,8 +631,8 @@ do
631 local vipStart, regulars, vipEnd = clients[1], (function() 631 local vipStart, regulars, vipEnd = clients[1], (function()
632 local _accum_0 = { } 632 local _accum_0 = { }
633 local _len_0 = 1 633 local _len_0 = 1
634 local _max_0 = -2 634 local _max_0 = #clients + -2 + 1
635 for _index_0 = 2, _max_0 < 0 and #clients + _max_0 + 1 or _max_0 do 635 for _index_0 = 2, _max_0 do
636 local _item_0 = clients[_index_0] 636 local _item_0 = clients[_index_0]
637 _accum_0[_len_0] = _item_0 637 _accum_0[_len_0] = _item_0
638 _len_0 = _len_0 + 1 638 _len_0 = _len_0 + 1
@@ -688,8 +688,8 @@ do
688 local _accum_0 = { } 688 local _accum_0 = { }
689 local _len_0 = 1 689 local _len_0 = 1
690 local _list_0 = tb 690 local _list_0 = tb
691 local _max_0 = -2 691 local _max_0 = #_list_0 + -2 + 1
692 for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 + 1 or _max_0 do 692 for _index_0 = 2, _max_0 do
693 local _item_0 = _list_0[_index_0] 693 local _item_0 = _list_0[_index_0]
694 _accum_0[_len_0] = _item_0 694 _accum_0[_len_0] = _item_0
695 _len_0 = _len_0 + 1 695 _len_0 = _len_0 + 1
@@ -702,8 +702,8 @@ do
702 a, abc, b, def, sub, d, e = _obj_0[1], _obj_0.abc, _obj_0[2], _obj_0.def, (function() 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 = { } 703 local _accum_0 = { }
704 local _len_0 = 1 704 local _len_0 = 1
705 local _max_0 = -3 705 local _max_0 = #_obj_0 + -3 + 1
706 for _index_0 = 3, _max_0 < 0 and #_obj_0 + _max_0 + 1 or _max_0 do 706 for _index_0 = 3, _max_0 do
707 local _item_0 = _obj_0[_index_0] 707 local _item_0 = _obj_0[_index_0]
708 _accum_0[_len_0] = _item_0 708 _accum_0[_len_0] = _item_0
709 _len_0 = _len_0 + 1 709 _len_0 = _len_0 + 1
diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua
index 1bdfa3e..2ed7b95 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 + 1 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 + 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 + 1 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
@@ -339,8 +347,8 @@ do
339 do 347 do
340 local _accum_0 = { } 348 local _accum_0 = { }
341 local _len_0 = 1 349 local _len_0 = 1
342 local _max_3 = -2 350 local _max_5 = #transactions + -2 + 1
343 for _index_0 = 2, _max_3 < 0 and #transactions + _max_3 + 1 or _max_3 do 351 for _index_0 = 2, _max_5 do
344 local _item_0 = transactions[_index_0] 352 local _item_0 = transactions[_index_0]
345 _accum_0[_len_0] = _item_0 353 _accum_0[_len_0] = _item_0
346 _len_0 = _len_0 + 1 354 _len_0 = _len_0 + 1
@@ -405,7 +413,8 @@ do
405 cloneList1 = function(list) 413 cloneList1 = function(list)
406 local _accum_0 = { } 414 local _accum_0 = { }
407 local _len_0 = 1 415 local _len_0 = 1
408 for _index_0 = 1, #list do 416 local _max_5 = #list
417 for _index_0 = 1, _max_5 do
409 local _item_0 = list[_index_0] 418 local _item_0 = list[_index_0]
410 _accum_0[_len_0] = _item_0 419 _accum_0[_len_0] = _item_0
411 _len_0 = _len_0 + 1 420 _len_0 = _len_0 + 1
@@ -471,8 +480,8 @@ local _anon_func_1 = function(a)
471 local _accum_0 = { } 480 local _accum_0 = { }
472 local _len_0 = 1 481 local _len_0 = 1
473 local _list_0 = a.b.c 482 local _list_0 = a.b.c
474 local _max_3 = -5 483 local _max_5 = #_list_0 + -5 + 1
475 for _index_0 = 5, _max_3 < 0 and #_list_0 + _max_3 + 1 or _max_3 do 484 for _index_0 = 5, _max_5 do
476 local _item_1 = _list_0[_index_0] 485 local _item_1 = _list_0[_index_0]
477 _accum_0[_len_0] = _item_1 486 _accum_0[_len_0] = _item_1
478 _len_0 = _len_0 + 1 487 _len_0 = _len_0 + 1
@@ -491,8 +500,8 @@ local _anon_func_2 = function(x)
491 if _obj_2 ~= nil then 500 if _obj_2 ~= nil then
492 local _accum_0 = { } 501 local _accum_0 = { }
493 local _len_0 = 1 502 local _len_0 = 1
494 local _max_3 = -3 503 local _max_5 = #_obj_2 + -3 + 1
495 for _index_0 = 1, _max_3 < 0 and #_obj_2 + _max_3 + 1 or _max_3 do 504 for _index_0 = 1, _max_5 do
496 local _item_0 = _obj_2[_index_0] 505 local _item_0 = _obj_2[_index_0]
497 _accum_0[_len_0] = _item_0 506 _accum_0[_len_0] = _item_0
498 _len_0 = _len_0 + 1 507 _len_0 = _len_0 + 1
diff --git a/spec/outputs/switch.lua b/spec/outputs/switch.lua
index 7a11bac..7c1004b 100644
--- a/spec/outputs/switch.lua
+++ b/spec/outputs/switch.lua
@@ -740,8 +740,8 @@ do
740 do 740 do
741 local _accum_0 = { } 741 local _accum_0 = { }
742 local _len_0 = 1 742 local _len_0 = 1
743 local _max_0 = -3 743 local _max_0 = #clientData + -3 + 1
744 for _index_0 = 1, _max_0 < 0 and #clientData + _max_0 + 1 or _max_0 do 744 for _index_0 = 1, _max_0 do
745 local _item_0 = clientData[_index_0] 745 local _item_0 = clientData[_index_0]
746 _accum_0[_len_0] = _item_0 746 _accum_0[_len_0] = _item_0
747 _len_0 = _len_0 + 1 747 _len_0 = _len_0 + 1
diff --git a/spec/outputs/unicode/comprehension.lua b/spec/outputs/unicode/comprehension.lua
index 894e78f..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 + 1 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 + 1 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/lists.lua b/spec/outputs/unicode/lists.lua
index a3c329b..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 + 1 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 + 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 + 1 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/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index cbf976f..9f5a41e 100644
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -8180,6 +8180,11 @@ private:
8180 } 8180 }
8181 8181
8182 bool transformForEachHead(AssignableNameList_t* nameList, ast_node* loopTarget, str_list& out, bool inClosure) { 8182 bool transformForEachHead(AssignableNameList_t* nameList, ast_node* loopTarget, str_list& out, bool inClosure) {
8183 enum class NumState {
8184 Unknown,
8185 Positive,
8186 Negtive
8187 };
8183 auto x = nameList; 8188 auto x = nameList;
8184 str_list temp; 8189 str_list temp;
8185 str_list vars; 8190 str_list vars;
@@ -8246,15 +8251,35 @@ private:
8246 for (auto item : chainList) { 8251 for (auto item : chainList) {
8247 chain->items.push_back(item); 8252 chain->items.push_back(item);
8248 } 8253 }
8249 std::string startValue("1"sv); 8254 std::string startValue;
8255 NumState startStatus = NumState::Unknown;
8250 if (auto exp = slice->startValue.as<Exp_t>()) { 8256 if (auto exp = slice->startValue.as<Exp_t>()) {
8251 transformExp(exp, temp, ExpUsage::Closure); 8257 transformExp(exp, temp, ExpUsage::Closure);
8258 if (temp.back().at(0) == '-') {
8259 if (_parser.match<Num_t>(temp.back().substr(1))) {
8260 startStatus = NumState::Negtive;
8261 }
8262 } else {
8263 if (_parser.match<Num_t>(temp.back())) {
8264 startStatus = NumState::Positive;
8265 }
8266 }
8252 startValue = std::move(temp.back()); 8267 startValue = std::move(temp.back());
8253 temp.pop_back(); 8268 temp.pop_back();
8254 } 8269 }
8255 std::string stopValue; 8270 std::string stopValue;
8271 NumState stopStatus = NumState::Unknown;
8256 if (auto exp = slice->stopValue.as<Exp_t>()) { 8272 if (auto exp = slice->stopValue.as<Exp_t>()) {
8257 transformExp(exp, temp, ExpUsage::Closure); 8273 transformExp(exp, temp, ExpUsage::Closure);
8274 if (temp.back().at(0) == '-') {
8275 if (_parser.match<Num_t>(temp.back().substr(1))) {
8276 stopStatus = NumState::Negtive;
8277 }
8278 } else {
8279 if (_parser.match<Num_t>(temp.back())) {
8280 stopStatus = NumState::Positive;
8281 }
8282 }
8258 stopValue = std::move(temp.back()); 8283 stopValue = std::move(temp.back());
8259 temp.pop_back(); 8284 temp.pop_back();
8260 } 8285 }
@@ -8276,8 +8301,33 @@ private:
8276 transformChainValue(chain, temp, ExpUsage::Closure); 8301 transformChainValue(chain, temp, ExpUsage::Closure);
8277 _buf << prefix << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); 8302 _buf << prefix << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList);
8278 } 8303 }
8304 if (startValue.empty()) {
8305 startValue = "1"s;
8306 startStatus = NumState::Positive;
8307 }
8308 std::string minVar;
8309 if (startStatus != NumState::Positive) {
8310 std::string prefix;
8311 if (!extraScope && !inClosure && needScope) {
8312 extraScope = true;
8313 prefix = indent() + "do"s + nll(x);
8314 pushScope();
8315 }
8316 minVar = getUnusedName("_min_"sv);
8317 varBefore.push_back(minVar);
8318 if (startStatus == NumState::Negtive) {
8319 _buf << prefix << indent() << "local "sv << minVar << " = "sv << "#"sv << listVar << " + "sv << startValue << " + 1"sv << nll(nameList);
8320 } else {
8321 _buf << prefix << indent() << "local "sv << minVar << " = "sv << startValue << nll(nameList);
8322 }
8323 }
8324 bool defaultStop = false;
8325 if (stopValue.empty()) {
8326 stopValue = "#"s + listVar;
8327 defaultStop = true;
8328 }
8279 std::string maxVar; 8329 std::string maxVar;
8280 if (!stopValue.empty()) { 8330 if (stopStatus != NumState::Positive) {
8281 std::string prefix; 8331 std::string prefix;
8282 if (!extraScope && !inClosure && needScope) { 8332 if (!extraScope && !inClosure && needScope) {
8283 extraScope = true; 8333 extraScope = true;
@@ -8286,14 +8336,45 @@ private:
8286 } 8336 }
8287 maxVar = getUnusedName("_max_"sv); 8337 maxVar = getUnusedName("_max_"sv);
8288 varBefore.push_back(maxVar); 8338 varBefore.push_back(maxVar);
8289 _buf << prefix << indent() << "local "sv << maxVar << " = "sv << stopValue << nll(nameList); 8339 if (stopStatus == NumState::Negtive) {
8340 _buf << indent() << "local "sv << maxVar << " = "sv << "#"sv << listVar << " + "sv << stopValue << " + 1"sv << nll(nameList);
8341 } else {
8342 _buf << prefix << indent() << "local "sv << maxVar << " = "sv << stopValue << nll(nameList);
8343 }
8344 }
8345 if (startStatus == NumState::Unknown) {
8346 _buf << indent() << minVar << " = "sv << minVar << " < 0 and #"sv << listVar << " + "sv << minVar << " + 1 or "sv << minVar << nll(nameList);
8347 }
8348 if (!defaultStop && stopStatus == NumState::Unknown) {
8349 _buf << indent() << maxVar << " = "sv << maxVar << " < 0 and #"sv << listVar << " + "sv << maxVar << " + 1 or "sv << maxVar << nll(nameList);
8290 } 8350 }
8291 _buf << indent() << "for "sv << indexVar << " = "sv; 8351 _buf << indent() << "for "sv << indexVar << " = "sv;
8292 _buf << startValue << ", "sv; 8352 if (startValue.empty()) {
8353 _buf << "1"sv;
8354 } else {
8355 switch (startStatus) {
8356 case NumState::Unknown:
8357 case NumState::Negtive:
8358 _buf << minVar;
8359 break;
8360 case NumState::Positive:
8361 _buf << startValue;
8362 break;
8363 }
8364 }
8365 _buf << ", "sv;
8293 if (stopValue.empty()) { 8366 if (stopValue.empty()) {
8294 _buf << "#"sv << listVar; 8367 _buf << "#"sv << listVar;
8295 } else { 8368 } else {
8296 _buf << maxVar << " < 0 and #"sv << listVar << " + "sv << maxVar << " + 1 or "sv << maxVar; 8369 switch (stopStatus) {
8370 case NumState::Unknown:
8371 case NumState::Negtive:
8372 _buf << maxVar;
8373 break;
8374 case NumState::Positive:
8375 _buf << stopValue;
8376 break;
8377 }
8297 } 8378 }
8298 if (!stepValue.empty()) { 8379 if (!stepValue.empty()) {
8299 _buf << ", "sv << stepValue; 8380 _buf << ", "sv << stepValue;