Commit 9aafc320

9aafc3203f07Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Array#each_slice(n) and Array#each_cons(n)

each_slice iterates over non-overlapping n-element chunks. each_cons iterates over sliding n-element windows. Block parameter is a sub-array of the same element type. Works for int_array, str_array, and float_array.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
9aafc3203f07
Parent
bb3ab402289b

spinel_codegen.rbmodifiedView file

@@ -9356,6 +9356,13 @@
93569356 # Index
93579357 types.push("int")
93589358 end
9359+ elsif mname == "each_slice" || mname == "each_cons"
9360+ # Block param is a sub-array of the same type
9361+ if recv_type == "str_array" || recv_type == "float_array" || recv_type == "int_array"
9362+ types.push(recv_type)
9363+ else
9364+ types.push("int_array")
9365+ end
93599366 elsif mname == "reduce" || mname == "inject"
93609367 if bk == 0
93619368 # Accumulator: infer from initial value argument
@@ -14725,6 +14732,20 @@
1472514732 end
1472614733 end
1472714734
14735+ if mname == "each_slice"
14736+ if @nd_block[nid] >= 0
14737+ compile_each_slice_block(nid)
14738+ return 1
14739+ end
14740+ end
14741+
14742+ if mname == "each_cons"
14743+ if @nd_block[nid] >= 0
14744+ compile_each_cons_block(nid)
14745+ return 1
14746+ end
14747+ end
14748+
1472814749 # scan with block: str.scan(/re/) { |m| ... }
1472914750 if mname == "scan"
1473014751 if @nd_block[nid] >= 0
@@ -16136,6 +16157,62 @@
1613616157 ""
1613716158 end
1613816159
16160+ def compile_each_slice_block(nid)
16161+ old = @in_loop
16162+ @in_loop = 1
16163+ rt = infer_type(@nd_receiver[nid])
16164+ rc = compile_expr(@nd_receiver[nid])
16165+ n = compile_arg0(nid)
16166+ bp1 = get_block_param(nid, 0)
16167+ if bp1 == ""
16168+ bp1 = "_slice"
16169+ end
16170+ tmp_i = new_temp
16171+ tmp_j = new_temp
16172+ tmp_len = new_temp
16173+ pfx = array_c_prefix(rt)
16174+ declare_var(bp1, rt)
16175+ @needs_gc = 1
16176+ emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")
16177+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_len + "; " + tmp_i + " += " + n + ") {")
16178+ emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
16179+ emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + " && " + tmp_i + " + " + tmp_j + " < " + tmp_len + "; " + tmp_j + "++)")
16180+ emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")
16181+ @indent = @indent + 1
16182+ compile_stmts_body(@nd_body[@nd_block[nid]])
16183+ @indent = @indent - 1
16184+ emit(" }")
16185+ @in_loop = old
16186+ end
16187+
16188+ def compile_each_cons_block(nid)
16189+ old = @in_loop
16190+ @in_loop = 1
16191+ rt = infer_type(@nd_receiver[nid])
16192+ rc = compile_expr(@nd_receiver[nid])
16193+ n = compile_arg0(nid)
16194+ bp1 = get_block_param(nid, 0)
16195+ if bp1 == ""
16196+ bp1 = "_cons"
16197+ end
16198+ tmp_i = new_temp
16199+ tmp_j = new_temp
16200+ tmp_len = new_temp
16201+ pfx = array_c_prefix(rt)
16202+ declare_var(bp1, rt)
16203+ @needs_gc = 1
16204+ emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")
16205+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " + " + n + " <= " + tmp_len + "; " + tmp_i + "++) {")
16206+ emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")
16207+ emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + "; " + tmp_j + "++)")
16208+ emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")
16209+ @indent = @indent + 1
16210+ compile_stmts_body(@nd_body[@nd_block[nid]])
16211+ @indent = @indent - 1
16212+ emit(" }")
16213+ @in_loop = old
16214+ end
16215+
1613916216 def compile_each_with_index_block(nid)
1614016217 old = @in_loop
1614116218 @in_loop = 1