Commit 887c1e30

887c1e30ce20Merged

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

Description

feat: add Array#cycle(n) with block

Iterates the array n times, calling the block for each element. Uses nested for loops. Works for all array types.

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

Details

Commit
887c1e30ce20
Parent
9fe24709c4ae

spinel_codegen.rbmodifiedView file

@@ -15259,6 +15259,32 @@
1525915259 end
1526015260 end
1526115261
15262+ if mname == "cycle"
15263+ if @nd_block[nid] >= 0 && recv >= 0
15264+ old = @in_loop
15265+ @in_loop = 1
15266+ rt = infer_type(recv)
15267+ rc = compile_expr(recv)
15268+ n = compile_arg0(nid)
15269+ bp1 = get_block_param(nid, 0)
15270+ if bp1 == ""
15271+ bp1 = "_x"
15272+ end
15273+ pfx = array_c_prefix(rt)
15274+ tmp_c = new_temp
15275+ tmp_i = new_temp
15276+ emit(" for (mrb_int " + tmp_c + " = 0; " + tmp_c + " < " + n + "; " + tmp_c + "++)")
15277+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
15278+ emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
15279+ @indent = @indent + 1
15280+ compile_stmts_body(@nd_body[@nd_block[nid]])
15281+ @indent = @indent - 1
15282+ emit(" }")
15283+ @in_loop = old
15284+ return 1
15285+ end
15286+ end
15287+
1526215288 # scan with block: str.scan(/re/) { |m| ... }
1526315289 if mname == "scan"
1526415290 if @nd_block[nid] >= 0