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 end
19131913 return "int_array"
19141914 end
1915- 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 value
19171917 args_id = @nd_arguments[nid]
19181918 if args_id >= 0
@@ -9367,6 +9367,29 @@
93679367 end
93689368 elsif mname == "each_char" || mname == "each_line"
93699369 types.push("string")
9370+ elsif mname == "each_with_object"
9371+ if bk == 0
9372+ # Element
9373+ if recv_type == "str_array"
9374+ types.push("string")
9375+ elsif recv_type == "float_array"
9376+ types.push("float")
9377+ else
9378+ types.push("int")
9379+ end
9380+ else
9381+ # Object accumulator — infer from first argument
9382+ args_id = @nd_arguments[nid]
9383+ if args_id >= 0
9384+ aargs = get_args(args_id)
9385+ if aargs.length > 0
9386+ types.push(infer_type(aargs[0]))
9387+ bk = bk + 1
9388+ next
9389+ end
9390+ end
9391+ types.push("int")
9392+ end
93709393 elsif mname == "each_slice" || mname == "each_cons"
93719394 # Block param is a sub-array of the same type
93729395 if recv_type == "str_array" || recv_type == "float_array" || recv_type == "int_array"
@@ -12508,6 +12531,18 @@
1250812531 end
1250912532 end
1251012533
12534+ # each_with_object as expression: run the loop as side effect, return obj
12535+ if mname == "each_with_object"
12536+ if @nd_block[nid] >= 0
12537+ compile_each_with_object_block(nid)
12538+ bp2 = get_block_param(nid, 1)
12539+ if bp2 == ""
12540+ bp2 = "_obj"
12541+ end
12542+ return "lv_" + bp2
12543+ end
12544+ end
12545+
1251112546 # select as expression
1251212547 if mname == "select"
1251312548 if @nd_block[nid] >= 0
@@ -14833,6 +14868,13 @@
1483314868 end
1483414869 end
1483514870
14871+ if mname == "each_with_object"
14872+ if @nd_block[nid] >= 0 && recv >= 0
14873+ compile_each_with_object_block(nid)
14874+ return 1
14875+ end
14876+ end
14877+
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 = old
1630116343 end
1630216344
16345+ def compile_each_with_object_block(nid)
16346+ old = @in_loop
16347+ @in_loop = 1
16348+ 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+ end
16356+ if bp2 == ""
16357+ bp2 = "_obj"
16358+ end
16359+ tmp_i = new_temp
16360+ 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 + 1
16366+ compile_stmts_body(@nd_body[@nd_block[nid]])
16367+ @indent = @indent - 1
16368+ emit(" }")
16369+ end
16370+ @in_loop = old
16371+ end
16372+
1630316373 def compile_each_with_index_block(nid)
1630416374 old = @in_loop
1630516375 @in_loop = 1