Commit b180fa87
b180fa87ce26Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Object#tap, #then, #yield_self
tap runs the block with receiver bound to the block param and returns the receiver. then/yield_self do the same but return the block's result. Block parameter type is inferred from receiver.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- b180fa87ce26
- Parent
- 7f3ee54de9c0
spinel_codegen.rbmodifiedView file
@@ -1542,8 +1542,25 @@
15421542
if mname == "clamp"15431543
return "int"15441544
end1545-
if mname == "itself"1545+
if mname == "itself" || mname == "tap"1546+
if recv >= 01547+
return infer_type(recv)1548+
end1549+
return "int"1550+
end1551+
if mname == "then" || mname == "yield_self"1552+
# Return type is the block's return type15461553
if recv >= 01554+
blk = @nd_block[nid]1555+
if blk >= 01556+
bbody = @nd_body[blk]1557+
if bbody >= 01558+
bbs = get_stmts(bbody)1559+
if bbs.length > 01560+
return infer_type(bbs.last)1561+
end1562+
end1563+
end15471564
return infer_type(recv)15481565
end15491566
return "int"@@ -9418,6 +9435,9 @@
94189435
end94199436
elsif mname == "each_char" || mname == "each_line"94209437
types.push("string")9438+
elsif mname == "tap" || mname == "then" || mname == "yield_self"9439+
# Block param gets receiver type9440+
types.push(recv_type)94219441
elsif mname == "each_with_object"94229442
if bk == 094239443
# Element@@ -12698,6 +12718,20 @@
1269812718
end1269912719
end1270012720
12721+
# tap: run block with receiver, return receiver12722+
if mname == "tap"12723+
if @nd_block[nid] >= 012724+
return compile_tap_expr(nid)12725+
end12726+
end12727+
12728+
# then / yield_self: pass receiver to block, return block result12729+
if mname == "then" || mname == "yield_self"12730+
if @nd_block[nid] >= 012731+
return compile_then_expr(nid)12732+
end12733+
end12734+
1270112735
# select as expression1270212736
if mname == "select" || mname == "filter"1270312737
if @nd_block[nid] >= 0@@ -16757,6 +16791,57 @@
1675716791
@in_loop = old1675816792
end1675916793
16794+
def compile_tap_expr(nid)16795+
# Execute block with receiver bound to block param, return receiver16796+
rt = infer_type(@nd_receiver[nid])16797+
rc = compile_expr(@nd_receiver[nid])16798+
bp = get_block_param(nid, 0)16799+
if bp == ""16800+
bp = "_x"16801+
end16802+
declare_var(bp, rt)16803+
tmp = new_temp16804+
emit(" " + c_type(rt) + " " + tmp + " = " + rc + ";")16805+
emit(" lv_" + bp + " = " + tmp + ";")16806+
blk = @nd_block[nid]16807+
bbody = @nd_body[blk]16808+
if bbody >= 016809+
bs = get_stmts(bbody)16810+
k = 016811+
while k < bs.length16812+
compile_stmt(bs[k])16813+
k = k + 116814+
end16815+
end16816+
tmp16817+
end16818+
16819+
def compile_then_expr(nid)16820+
# Execute block with receiver bound to block param, return block result16821+
rt = infer_type(@nd_receiver[nid])16822+
rc = compile_expr(@nd_receiver[nid])16823+
bp = get_block_param(nid, 0)16824+
if bp == ""16825+
bp = "_x"16826+
end16827+
declare_var(bp, rt)16828+
emit(" lv_" + bp + " = " + rc + ";")16829+
blk = @nd_block[nid]16830+
bbody = @nd_body[blk]16831+
if bbody >= 016832+
bs = get_stmts(bbody)16833+
k = 016834+
while k < bs.length - 116835+
compile_stmt(bs[k])16836+
k = k + 116837+
end16838+
if bs.length > 016839+
return compile_expr(bs.last)16840+
end16841+
end16842+
"0"16843+
end16844+
1676016845
def compile_array_predicate_block(nid, rc, recv_type, mname)1676116846
# Implements any?/all?/none? with block by short-circuit loop1676216847
bp1 = get_block_param(nid, 0)