Commit a23beb01
a23beb012056Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Hash#select and Hash#reject with block
Filters a hash by block condition, returning a new hash of the same type. Block receives (key, value) pairs. Works for both StrIntHash and StrStrHash.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- a23beb012056
- Parent
- 9aafc3203f07
spinel_codegen.rbmodifiedView file
@@ -1903,6 +1903,12 @@
19031903
end19041904
return "int_array"19051905
end1906+
if mname == "reject"1907+
if recv >= 01908+
return infer_type(recv)1909+
end1910+
return "int_array"1911+
end19061912
if mname == "reduce" || mname == "inject"19071913
# Return type is the accumulator type, inferred from initial value19081914
args_id = @nd_arguments[nid]@@ -12382,6 +12388,9 @@
1238212388
if mname == "values"1238312389
return "sp_StrIntHash_values(" + rc + ")"1238412390
end12391+
if (mname == "select" || mname == "reject") && @nd_block[nid] >= 012392+
return compile_hash_select_reject(nid, "str_int_hash", rc, mname)12393+
end1238512394
if mname == "fetch"1238612395
args_id = @nd_arguments[nid]1238712396
if args_id >= 0@@ -12448,6 +12457,9 @@
1244812457
if mname == "invert"1244912458
return "sp_StrStrHash_invert(" + rc + ")"1245012459
end12460+
if (mname == "select" || mname == "reject") && @nd_block[nid] >= 012461+
return compile_hash_select_reject(nid, "str_str_hash", rc, mname)12462+
end1245112463
if mname == "fetch"1245212464
args_id = @nd_arguments[nid]1245312465
if args_id >= 0@@ -16374,6 +16386,66 @@
1637416386
@in_loop = old1637516387
end1637616388
16389+
def compile_hash_select_reject(nid, hash_type, rc, mname)16390+
# Build a new hash by filtering entries using the block16391+
bp1 = get_block_param(nid, 0)16392+
bp2 = get_block_param(nid, 1)16393+
if bp1 == ""16394+
bp1 = "_k"16395+
end16396+
if bp2 == ""16397+
bp2 = "_v"16398+
end16399+
ctor = ""16400+
getter = ""16401+
setter = ""16402+
val_type = ""16403+
if hash_type == "str_int_hash"16404+
ctor = "sp_StrIntHash_new"16405+
getter = "sp_StrIntHash_get"16406+
setter = "sp_StrIntHash_set"16407+
val_type = "int"16408+
@needs_str_int_hash = 116409+
else16410+
ctor = "sp_StrStrHash_new"16411+
getter = "sp_StrStrHash_get"16412+
setter = "sp_StrStrHash_set"16413+
val_type = "string"16414+
@needs_str_str_hash = 116415+
end16416+
@needs_gc = 116417+
declare_var(bp1, "string")16418+
declare_var(bp2, val_type)16419+
tmp = new_temp16420+
itmp = new_temp16421+
emit(" " + c_type(hash_type) + tmp + " = " + ctor + "();")16422+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")16423+
emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")16424+
emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")16425+
blk = @nd_block[nid]16426+
bbody = @nd_body[blk]16427+
bexpr = "0"16428+
if bbody >= 016429+
bs = get_stmts(bbody)16430+
if bs.length > 016431+
# Emit all but last as stmts16432+
k = 016433+
while k < bs.length - 116434+
compile_stmt(bs[k])16435+
k = k + 116436+
end16437+
bexpr = compile_expr(bs.last)16438+
end16439+
end16440+
cond = bexpr16441+
if mname == "reject"16442+
cond = "!(" + bexpr + ")"16443+
end16444+
emit(" if (" + cond + ") " + setter + "(" + tmp + ", lv_" + bp1 + ", lv_" + bp2 + ");")16445+
emit(" }")16446+
tmp16447+
end16448+
1637716449
def compile_flat_map_expr(nid)1637816450
# flat_map: for each element, block returns an array; concat all results1637916451
rt = infer_type(@nd_receiver[nid])