Commit 9be2460f
9be2460f3c14Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: reduce/inject type inference and multi-array-type codegen
- infer_method_name_type now returns the initial value type for reduce/inject, instead of defaulting to int
- compile_reduce_block now works with all array types (str_array, float_array, etc.) using array_c_prefix, not just int_array
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 9be2460f3c14
- Parent
- 4d11a7613962
spinel_codegen.rbmodifiedView file
@@ -1808,6 +1808,17 @@
18081808
end18091809
return "int_array"18101810
end1811+
if mname == "reduce" || mname == "inject"1812+
# Return type is the accumulator type, inferred from initial value1813+
args_id = @nd_arguments[nid]1814+
if args_id >= 01815+
aargs = get_args(args_id)1816+
if aargs.length > 01817+
return infer_type(aargs[0])1818+
end1819+
end1820+
return "int"1821+
end18111822
if mname == "[]"18121823
if recv >= 018131824
rt = infer_type(recv)@@ -16131,26 +16142,25 @@
1613116142
end1613216143
emit(" lv_" + bp1 + " = " + init_val + ";")1613316144
rt = infer_type(@nd_receiver[nid])16134-
if rt == "int_array"16135-
tmp = new_temp16136-
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + rc + "); " + tmp + "++) {")16137-
emit(" lv_" + bp2 + " = sp_IntArray_get(" + rc + ", " + tmp + ");")16138-
@indent = @indent + 116139-
blk = @nd_block[nid]16140-
if blk >= 016141-
body = @nd_body[blk]16142-
if body >= 016143-
stmts = get_stmts(body)16144-
if stmts.length > 016145-
last = stmts.last16146-
val = compile_expr(last)16147-
emit(" lv_" + bp1 + " = " + val + ";")16148-
end16145+
pfx = array_c_prefix(rt)16146+
tmp = new_temp16147+
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")16148+
emit(" lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")16149+
@indent = @indent + 116150+
blk = @nd_block[nid]16151+
if blk >= 016152+
body = @nd_body[blk]16153+
if body >= 016154+
stmts = get_stmts(body)16155+
if stmts.length > 016156+
last = stmts.last16157+
val = compile_expr(last)16158+
emit(" lv_" + bp1 + " = " + val + ";")1614916159
end1615016160
end16151-
@indent = @indent - 116152-
emit(" }")1615316161
end16162+
@indent = @indent - 116163+
emit(" }")1615416164
end1615516165
1615616166
def compile_reject_expr(nid)