Commit 708fbb9d

708fbb9df0bdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

codegen: Array#reduce(init, :op) — 2-arg explicit-init form

The 1-arg sym form (`arr.reduce(:+)`) was wired in 22298 via reduce_sym_op_for. The 2-arg form (`arr.reduce(10, :+)`) seeds with the init operand and starts the iteration at index 0; without this arm, it fell through to the unresolved-call path.

Fixes #982

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
708fbb9df0bd
Parent
b91a5d5315c2

spinel_codegen.rbmodifiedView file

@@ -22298,6 +22298,18 @@
2229822298 if (mname == "reduce" || mname == "inject") && reduce_sym_op_for(nid) != ""
2229922299 return "({ sp_IntArray *_arr = " + rc + "; mrb_int _acc = _arr->len > 0 ? sp_IntArray_get(_arr, 0) : 0; for (mrb_int _i = 1; _i < _arr->len; _i++) _acc = _acc " + reduce_sym_op_for(nid) + " sp_IntArray_get(_arr, _i); _acc; })"
2230022300 end
22301+ # `reduce(init, :op)` — explicit initial value plus a symbol
22302+ # operator. Differs from the 1-arg sym form by seeding with `init`
22303+ # and starting the iteration at index 0.
22304+ if (mname == "reduce" || mname == "inject") && @nd_arguments[nid] >= 0
22305+ ra_args = get_args(@nd_arguments[nid])
22306+ if ra_args.length == 2 && @nd_type[ra_args[1]] == "SymbolNode"
22307+ ra_op = @nd_content[ra_args[1]]
22308+ if ra_op == "+" || ra_op == "*" || ra_op == "-" || ra_op == "/" || ra_op == "%" || ra_op == "&" || ra_op == "|" || ra_op == "^"
22309+ return "({ sp_IntArray *_arr = " + rc + "; mrb_int _acc = " + compile_expr_as_int(ra_args[0]) + "; for (mrb_int _i = 0; _i < _arr->len; _i++) _acc = _acc " + ra_op + " sp_IntArray_get(_arr, _i); _acc; })"
22310+ end
22311+ end
22312+ end
2230122313 if mname == "to_a"
2230222314 return "sp_IntArray_dup(" + rc + ")"
2230322315 end