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
end18091809
return "int_array"18101810
end1811+
if mname == "flat_map"1812+
if recv >= 01813+
# Block returns an array; result type matches block return type1814+
blk = @nd_block[nid]1815+
if blk >= 01816+
bbody = @nd_body[blk]1817+
if bbody >= 01818+
bbs = get_stmts(bbody)1819+
if bbs.length > 01820+
bret = infer_type(bbs.last)1821+
# If block returns an array type, use it as result type1822+
if bret == "int_array" || bret == "str_array" || bret == "float_array"1823+
return bret1824+
end1825+
end1826+
end1827+
end1828+
return infer_type(recv)1829+
end1830+
return "int_array"1831+
end18111832
if mname == "sort_by"18121833
if recv >= 018131834
return infer_type(recv)@@ -12444,6 +12465,13 @@
1244412465
end1244512466
end1244612467
12468+
# flat_map as expression12469+
if mname == "flat_map"12470+
if @nd_block[nid] >= 012471+
return compile_flat_map_expr(nid)12472+
end12473+
end12474+
1244712475
# select as expression1244812476
if mname == "select"1244912477
if @nd_block[nid] >= 0@@ -16269,6 +16297,69 @@
1626916297
@in_loop = old1627016298
end1627116299
16300+
def compile_flat_map_expr(nid)16301+
# flat_map: for each element, block returns an array; concat all results16302+
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+
end16308+
# Determine result array type from block return type16309+
blk = @nd_block[nid]16310+
block_ret = "int_array"16311+
if blk >= 016312+
bbody = @nd_body[blk]16313+
if bbody >= 016314+
bbs = get_stmts(bbody)16315+
if bbs.length > 016316+
block_ret = infer_type(bbs.last)16317+
end16318+
end16319+
end16320+
# Fall back to receiver type if block doesn't return an array16321+
if block_ret != "int_array" && block_ret != "str_array" && block_ret != "float_array"16322+
block_ret = rt16323+
end16324+
@needs_gc = 116325+
pfx_src = array_c_prefix(rt)16326+
pfx_dst = array_c_prefix(block_ret)16327+
tmp_arr = new_temp16328+
tmp_i = new_temp16329+
tmp_inner = new_temp16330+
tmp_j = new_temp16331+
emit(" " + c_type(block_ret) + tmp_arr + " = sp_" + pfx_dst + "_new();")16332+
# Declare block param type from receiver element type16333+
elem_type = "int"16334+
if rt == "str_array"16335+
elem_type = "string"16336+
elsif rt == "float_array"16337+
elem_type = "float"16338+
end16339+
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 + 116343+
bbody2 = @nd_body[blk]16344+
if bbody2 >= 016345+
bbs2 = get_stmts(bbody2)16346+
# Compile all but last statement16347+
k = 016348+
while k < bbs2.length - 116349+
compile_stmt(bbs2[k])16350+
k = k + 116351+
end16352+
last = bbs2.last16353+
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+
end16358+
@indent = @indent - 116359+
emit(" }")16360+
tmp_arr16361+
end16362+
1627216363
def compile_map_expr(nid)1627316364
rt = infer_type(@nd_receiver[nid])1627416365
rc_expr = compile_expr(@nd_receiver[nid])