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 end
1789+ if rt == "str_int_hash" || rt == "str_str_hash"
1790+ return "string"
1791+ end
17891792 end
17901793 return "int"
17911794 end
@@ -12763,6 +12766,9 @@
1276312766 if (mname == "select" || mname == "reject") && @nd_block[nid] >= 0
1276412767 return compile_hash_select_reject(nid, "str_int_hash", rc, mname)
1276512768 end
12769+ if (mname == "count" || mname == "any?" || mname == "all?" || mname == "find" || mname == "detect") && @nd_block[nid] >= 0
12770+ return compile_hash_block_predicate(nid, "str_int_hash", rc, mname)
12771+ end
1276612772 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] >= 0
1283312839 return compile_hash_select_reject(nid, "str_str_hash", rc, mname)
1283412840 end
12841+ if (mname == "count" || mname == "any?" || mname == "all?" || mname == "find" || mname == "detect") && @nd_block[nid] >= 0
12842+ return compile_hash_block_predicate(nid, "str_str_hash", rc, mname)
12843+ end
1283512844 if mname == "fetch"
1283612845 args_id = @nd_arguments[nid]
1283712846 if args_id >= 0
@@ -17406,6 +17415,128 @@
1740617415 tmp
1740717416 end
1740817417
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+ end
17424+ if bp2 == ""
17425+ bp2 = "_v"
17426+ end
17427+ 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+ end
17433+ declare_var(bp1, "string")
17434+ declare_var(bp2, val_type)
17435+ itmp = new_temp
17436+ # Compile block expression
17437+ blk = @nd_block[nid]
17438+ bbody = @nd_body[blk]
17439+ bexpr = "0"
17440+ blk_stmts = "".split(",")
17441+ if bbody >= 0
17442+ bs = get_stmts(bbody)
17443+ if bs.length > 0
17444+ k = 0
17445+ while k < bs.length - 1
17446+ blk_stmts.push(bs[k].to_s)
17447+ k = k + 1
17448+ end
17449+ bexpr = "PLACEHOLDER"
17450+ end
17451+ end
17452+ if mname == "count"
17453+ tmp_c = new_temp
17454+ 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 >= 0
17459+ bs = get_stmts(bbody)
17460+ k = 0
17461+ while k < bs.length - 1
17462+ compile_stmt(bs[k])
17463+ k = k + 1
17464+ end
17465+ if bs.length > 0
17466+ bexpr = compile_expr(bs.last)
17467+ end
17468+ end
17469+ emit(" if (" + bexpr + ") " + tmp_c + "++;")
17470+ emit(" }")
17471+ return tmp_c
17472+ end
17473+ if mname == "any?"
17474+ tmp_r = new_temp
17475+ 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 >= 0
17480+ bs = get_stmts(bbody)
17481+ k = 0
17482+ while k < bs.length - 1
17483+ compile_stmt(bs[k])
17484+ k = k + 1
17485+ end
17486+ if bs.length > 0
17487+ bexpr = compile_expr(bs.last)
17488+ end
17489+ end
17490+ emit(" if (" + bexpr + ") { " + tmp_r + " = TRUE; break; }")
17491+ emit(" }")
17492+ return tmp_r
17493+ end
17494+ if mname == "all?"
17495+ tmp_r = new_temp
17496+ 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 >= 0
17501+ bs = get_stmts(bbody)
17502+ k = 0
17503+ while k < bs.length - 1
17504+ compile_stmt(bs[k])
17505+ k = k + 1
17506+ end
17507+ if bs.length > 0
17508+ bexpr = compile_expr(bs.last)
17509+ end
17510+ end
17511+ emit(" if (!(" + bexpr + ")) { " + tmp_r + " = FALSE; break; }")
17512+ emit(" }")
17513+ return tmp_r
17514+ end
17515+ # find / detect — return key of first match
17516+ if mname == "find" || mname == "detect"
17517+ tmp_r = new_temp
17518+ 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 >= 0
17523+ bs = get_stmts(bbody)
17524+ k = 0
17525+ while k < bs.length - 1
17526+ compile_stmt(bs[k])
17527+ k = k + 1
17528+ end
17529+ if bs.length > 0
17530+ bexpr = compile_expr(bs.last)
17531+ end
17532+ end
17533+ emit(" if (" + bexpr + ") { " + tmp_r + " = lv_" + bp1 + "; break; }")
17534+ emit(" }")
17535+ return tmp_r
17536+ end
17537+ "0"
17538+ end
17539+
1740917540 def compile_flat_map_expr(nid)
1741017541 # flat_map: for each element, block returns an array; concat all results
1741117542 rt = infer_type(@nd_receiver[nid])