Commit 93f74383
93f743834642Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Hash#count, #any?, #all?, #find with block
Block receives (key, value) pairs. count returns the number of matching entries, any?/all? use short-circuit evaluation, find returns the key of the first matching entry (Ruby returns [k,v] but Spinel lacks array-of-array support).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 93f743834642
- Parent
- 3804f3e757bd
spinel_codegen.rbmodifiedView file
@@ -1786,6 +1786,9 @@
17861786
if rt == "float_array"17871787
return "float"17881788
end1789+
if rt == "str_int_hash" || rt == "str_str_hash"1790+
return "string"1791+
end17891792
end17901793
return "int"17911794
end@@ -12763,6 +12766,9 @@
1276312766
if (mname == "select" || mname == "reject") && @nd_block[nid] >= 01276412767
return compile_hash_select_reject(nid, "str_int_hash", rc, mname)1276512768
end12769+
if (mname == "count" || mname == "any?" || mname == "all?" || mname == "find" || mname == "detect") && @nd_block[nid] >= 012770+
return compile_hash_block_predicate(nid, "str_int_hash", rc, mname)12771+
end1276612772
if mname == "fetch"1276712773
args_id = @nd_arguments[nid]1276812774
if args_id >= 0@@ -12832,6 +12838,9 @@
1283212838
if (mname == "select" || mname == "reject") && @nd_block[nid] >= 01283312839
return compile_hash_select_reject(nid, "str_str_hash", rc, mname)1283412840
end12841+
if (mname == "count" || mname == "any?" || mname == "all?" || mname == "find" || mname == "detect") && @nd_block[nid] >= 012842+
return compile_hash_block_predicate(nid, "str_str_hash", rc, mname)12843+
end1283512844
if mname == "fetch"1283612845
args_id = @nd_arguments[nid]1283712846
if args_id >= 0@@ -17406,6 +17415,128 @@
1740617415
tmp1740717416
end1740817417
17418+
def compile_hash_block_predicate(nid, hash_type, rc, mname)17419+
bp1 = get_block_param(nid, 0)17420+
bp2 = get_block_param(nid, 1)17421+
if bp1 == ""17422+
bp1 = "_k"17423+
end17424+
if bp2 == ""17425+
bp2 = "_v"17426+
end17427+
val_type = "int"17428+
getter = "sp_StrIntHash_get"17429+
if hash_type == "str_str_hash"17430+
val_type = "string"17431+
getter = "sp_StrStrHash_get"17432+
end17433+
declare_var(bp1, "string")17434+
declare_var(bp2, val_type)17435+
itmp = new_temp17436+
# Compile block expression17437+
blk = @nd_block[nid]17438+
bbody = @nd_body[blk]17439+
bexpr = "0"17440+
blk_stmts = "".split(",")17441+
if bbody >= 017442+
bs = get_stmts(bbody)17443+
if bs.length > 017444+
k = 017445+
while k < bs.length - 117446+
blk_stmts.push(bs[k].to_s)17447+
k = k + 117448+
end17449+
bexpr = "PLACEHOLDER"17450+
end17451+
end17452+
if mname == "count"17453+
tmp_c = new_temp17454+
emit(" mrb_int " + tmp_c + " = 0;")17455+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")17456+
emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")17457+
emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")17458+
if bbody >= 017459+
bs = get_stmts(bbody)17460+
k = 017461+
while k < bs.length - 117462+
compile_stmt(bs[k])17463+
k = k + 117464+
end17465+
if bs.length > 017466+
bexpr = compile_expr(bs.last)17467+
end17468+
end17469+
emit(" if (" + bexpr + ") " + tmp_c + "++;")17470+
emit(" }")17471+
return tmp_c17472+
end17473+
if mname == "any?"17474+
tmp_r = new_temp17475+
emit(" mrb_bool " + tmp_r + " = FALSE;")17476+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")17477+
emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")17478+
emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")17479+
if bbody >= 017480+
bs = get_stmts(bbody)17481+
k = 017482+
while k < bs.length - 117483+
compile_stmt(bs[k])17484+
k = k + 117485+
end17486+
if bs.length > 017487+
bexpr = compile_expr(bs.last)17488+
end17489+
end17490+
emit(" if (" + bexpr + ") { " + tmp_r + " = TRUE; break; }")17491+
emit(" }")17492+
return tmp_r17493+
end17494+
if mname == "all?"17495+
tmp_r = new_temp17496+
emit(" mrb_bool " + tmp_r + " = TRUE;")17497+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")17498+
emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")17499+
emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")17500+
if bbody >= 017501+
bs = get_stmts(bbody)17502+
k = 017503+
while k < bs.length - 117504+
compile_stmt(bs[k])17505+
k = k + 117506+
end17507+
if bs.length > 017508+
bexpr = compile_expr(bs.last)17509+
end17510+
end17511+
emit(" if (!(" + bexpr + ")) { " + tmp_r + " = FALSE; break; }")17512+
emit(" }")17513+
return tmp_r17514+
end17515+
# find / detect — return key of first match17516+
if mname == "find" || mname == "detect"17517+
tmp_r = new_temp17518+
emit(" const char *" + tmp_r + " = \"\";")17519+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")17520+
emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")17521+
emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")17522+
if bbody >= 017523+
bs = get_stmts(bbody)17524+
k = 017525+
while k < bs.length - 117526+
compile_stmt(bs[k])17527+
k = k + 117528+
end17529+
if bs.length > 017530+
bexpr = compile_expr(bs.last)17531+
end17532+
end17533+
emit(" if (" + bexpr + ") { " + tmp_r + " = lv_" + bp1 + "; break; }")17534+
emit(" }")17535+
return tmp_r17536+
end17537+
"0"17538+
end17539+
1740917540
def compile_flat_map_expr(nid)1741017541
# flat_map: for each element, block returns an array; concat all results1741117542
rt = infer_type(@nd_receiver[nid])