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 end
18091809 return "int_array"
18101810 end
1811+ if mname == "reduce" || mname == "inject"
1812+ # Return type is the accumulator type, inferred from initial value
1813+ args_id = @nd_arguments[nid]
1814+ if args_id >= 0
1815+ aargs = get_args(args_id)
1816+ if aargs.length > 0
1817+ return infer_type(aargs[0])
1818+ end
1819+ end
1820+ return "int"
1821+ end
18111822 if mname == "[]"
18121823 if recv >= 0
18131824 rt = infer_type(recv)
@@ -16131,26 +16142,25 @@
1613116142 end
1613216143 emit(" lv_" + bp1 + " = " + init_val + ";")
1613316144 rt = infer_type(@nd_receiver[nid])
16134- if rt == "int_array"
16135- tmp = new_temp
16136- emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_IntArray_length(" + rc + "); " + tmp + "++) {")
16137- emit(" lv_" + bp2 + " = sp_IntArray_get(" + rc + ", " + tmp + ");")
16138- @indent = @indent + 1
16139- blk = @nd_block[nid]
16140- if blk >= 0
16141- body = @nd_body[blk]
16142- if body >= 0
16143- stmts = get_stmts(body)
16144- if stmts.length > 0
16145- last = stmts.last
16146- val = compile_expr(last)
16147- emit(" lv_" + bp1 + " = " + val + ";")
16148- end
16145+ pfx = array_c_prefix(rt)
16146+ tmp = new_temp
16147+ emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
16148+ emit(" lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
16149+ @indent = @indent + 1
16150+ blk = @nd_block[nid]
16151+ if blk >= 0
16152+ body = @nd_body[blk]
16153+ if body >= 0
16154+ stmts = get_stmts(body)
16155+ if stmts.length > 0
16156+ last = stmts.last
16157+ val = compile_expr(last)
16158+ emit(" lv_" + bp1 + " = " + val + ";")
1614916159 end
1615016160 end
16151- @indent = @indent - 1
16152- emit(" }")
1615316161 end
16162+ @indent = @indent - 1
16163+ emit(" }")
1615416164 end
1615516165
1615616166 def compile_reject_expr(nid)