Commit 36444f34
36444f34c4e9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#each_with_object
Iterates over an array with a mutable accumulator object, returning the accumulator. Block signature: |elem, obj|. The object's type is inferred from the initial argument. Works as both statement and expression. Hash version not yet supported (would need destructuring).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 36444f34c4e9
- Parent
- 2b662bc78585
spinel_codegen.rbmodifiedView file
@@ -1912,7 +1912,7 @@
19121912
end19131913
return "int_array"19141914
end1915-
if mname == "reduce" || mname == "inject"1915+
if mname == "reduce" || mname == "inject" || mname == "each_with_object"19161916
# Return type is the accumulator type, inferred from initial value19171917
args_id = @nd_arguments[nid]19181918
if args_id >= 0@@ -9367,6 +9367,29 @@
93679367
end93689368
elsif mname == "each_char" || mname == "each_line"93699369
types.push("string")9370+
elsif mname == "each_with_object"9371+
if bk == 09372+
# Element9373+
if recv_type == "str_array"9374+
types.push("string")9375+
elsif recv_type == "float_array"9376+
types.push("float")9377+
else9378+
types.push("int")9379+
end9380+
else9381+
# Object accumulator — infer from first argument9382+
args_id = @nd_arguments[nid]9383+
if args_id >= 09384+
aargs = get_args(args_id)9385+
if aargs.length > 09386+
types.push(infer_type(aargs[0]))9387+
bk = bk + 19388+
next9389+
end9390+
end9391+
types.push("int")9392+
end93709393
elsif mname == "each_slice" || mname == "each_cons"93719394
# Block param is a sub-array of the same type93729395
if recv_type == "str_array" || recv_type == "float_array" || recv_type == "int_array"@@ -12508,6 +12531,18 @@
1250812531
end1250912532
end1251012533
12534+
# each_with_object as expression: run the loop as side effect, return obj12535+
if mname == "each_with_object"12536+
if @nd_block[nid] >= 012537+
compile_each_with_object_block(nid)12538+
bp2 = get_block_param(nid, 1)12539+
if bp2 == ""12540+
bp2 = "_obj"12541+
end12542+
return "lv_" + bp212543+
end12544+
end12545+
1251112546
# select as expression1251212547
if mname == "select"1251312548
if @nd_block[nid] >= 0@@ -14833,6 +14868,13 @@
1483314868
end1483414869
end1483514870
14871+
if mname == "each_with_object"14872+
if @nd_block[nid] >= 0 && recv >= 014873+
compile_each_with_object_block(nid)14874+
return 114875+
end14876+
end14877+
1483614878
# scan with block: str.scan(/re/) { |m| ... }1483714879
if mname == "scan"1483814880
if @nd_block[nid] >= 0@@ -16300,6 +16342,34 @@
1630016342
@in_loop = old1630116343
end1630216344
16345+
def compile_each_with_object_block(nid)16346+
old = @in_loop16347+
@in_loop = 116348+
rt = infer_type(@nd_receiver[nid])16349+
rc = compile_expr(@nd_receiver[nid])16350+
obj_arg = compile_arg0(nid)16351+
bp1 = get_block_param(nid, 0)16352+
bp2 = get_block_param(nid, 1)16353+
if bp1 == ""16354+
bp1 = "_elem"16355+
end16356+
if bp2 == ""16357+
bp2 = "_obj"16358+
end16359+
tmp_i = new_temp16360+
if rt == "int_array" || rt == "str_array" || rt == "float_array"16361+
pfx = array_c_prefix(rt)16362+
emit(" lv_" + bp2 + " = " + obj_arg + ";")16363+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")16364+
emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")16365+
@indent = @indent + 116366+
compile_stmts_body(@nd_body[@nd_block[nid]])16367+
@indent = @indent - 116368+
emit(" }")16369+
end16370+
@in_loop = old16371+
end16372+
1630316373
def compile_each_with_index_block(nid)1630416374
old = @in_loop1630516375
@in_loop = 1