Commit bb3ab402

bb3ab402289bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Array#flat_map codegen

For each element, calls the block (which returns an array) and concatenates all results. Works for int_array, str_array, and float_array; both receiver and block return types are inferred.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
bb3ab402289b
Parent
e3ba02d84085

spinel_codegen.rbmodifiedView file

@@ -1808,6 +1808,27 @@
18081808 end
18091809 return "int_array"
18101810 end
1811+ if mname == "flat_map"
1812+ if recv >= 0
1813+ # Block returns an array; result type matches block return type
1814+ blk = @nd_block[nid]
1815+ if blk >= 0
1816+ bbody = @nd_body[blk]
1817+ if bbody >= 0
1818+ bbs = get_stmts(bbody)
1819+ if bbs.length > 0
1820+ bret = infer_type(bbs.last)
1821+ # If block returns an array type, use it as result type
1822+ if bret == "int_array" || bret == "str_array" || bret == "float_array"
1823+ return bret
1824+ end
1825+ end
1826+ end
1827+ end
1828+ return infer_type(recv)
1829+ end
1830+ return "int_array"
1831+ end
18111832 if mname == "sort_by"
18121833 if recv >= 0
18131834 return infer_type(recv)
@@ -12444,6 +12465,13 @@
1244412465 end
1244512466 end
1244612467
12468+ # flat_map as expression
12469+ if mname == "flat_map"
12470+ if @nd_block[nid] >= 0
12471+ return compile_flat_map_expr(nid)
12472+ end
12473+ end
12474+
1244712475 # select as expression
1244812476 if mname == "select"
1244912477 if @nd_block[nid] >= 0
@@ -16269,6 +16297,69 @@
1626916297 @in_loop = old
1627016298 end
1627116299
16300+ def compile_flat_map_expr(nid)
16301+ # flat_map: for each element, block returns an array; concat all results
16302+ rt = infer_type(@nd_receiver[nid])
16303+ rc = compile_expr(@nd_receiver[nid])
16304+ bp1 = get_block_param(nid, 0)
16305+ if bp1 == ""
16306+ bp1 = "_x"
16307+ end
16308+ # Determine result array type from block return type
16309+ blk = @nd_block[nid]
16310+ block_ret = "int_array"
16311+ if blk >= 0
16312+ bbody = @nd_body[blk]
16313+ if bbody >= 0
16314+ bbs = get_stmts(bbody)
16315+ if bbs.length > 0
16316+ block_ret = infer_type(bbs.last)
16317+ end
16318+ end
16319+ end
16320+ # Fall back to receiver type if block doesn't return an array
16321+ if block_ret != "int_array" && block_ret != "str_array" && block_ret != "float_array"
16322+ block_ret = rt
16323+ end
16324+ @needs_gc = 1
16325+ pfx_src = array_c_prefix(rt)
16326+ pfx_dst = array_c_prefix(block_ret)
16327+ tmp_arr = new_temp
16328+ tmp_i = new_temp
16329+ tmp_inner = new_temp
16330+ tmp_j = new_temp
16331+ emit(" " + c_type(block_ret) + tmp_arr + " = sp_" + pfx_dst + "_new();")
16332+ # Declare block param type from receiver element type
16333+ elem_type = "int"
16334+ if rt == "str_array"
16335+ elem_type = "string"
16336+ elsif rt == "float_array"
16337+ elem_type = "float"
16338+ end
16339+ declare_var(bp1, elem_type)
16340+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx_src + "_length(" + rc + "); " + tmp_i + "++) {")
16341+ emit(" lv_" + bp1 + " = sp_" + pfx_src + "_get(" + rc + ", " + tmp_i + ");")
16342+ @indent = @indent + 1
16343+ bbody2 = @nd_body[blk]
16344+ if bbody2 >= 0
16345+ bbs2 = get_stmts(bbody2)
16346+ # Compile all but last statement
16347+ k = 0
16348+ while k < bbs2.length - 1
16349+ compile_stmt(bbs2[k])
16350+ k = k + 1
16351+ end
16352+ last = bbs2.last
16353+ val = compile_expr(last)
16354+ emit(" " + c_type(block_ret) + tmp_inner + " = " + val + ";")
16355+ emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < sp_" + pfx_dst + "_length(" + tmp_inner + "); " + tmp_j + "++)")
16356+ emit(" sp_" + pfx_dst + "_push(" + tmp_arr + ", sp_" + pfx_dst + "_get(" + tmp_inner + ", " + tmp_j + "));")
16357+ end
16358+ @indent = @indent - 1
16359+ emit(" }")
16360+ tmp_arr
16361+ end
16362+
1627216363 def compile_map_expr(nid)
1627316364 rt = infer_type(@nd_receiver[nid])
1627416365 rc_expr = compile_expr(@nd_receiver[nid])