Commit 6b6d232e
6b6d232ee24cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: complete proc/block support with &block parameter and statement bodies
Fixes bm_proc by properly handling &block parameter type in collect_toplevel_method, compiling block bodies with side-effect statements, and routing block arguments as sp_Proc values.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 6b6d232ee24c
- Parent
- df5402ab0b24
spinel_codegen.rbmodifiedView file
@@ -2614,6 +2614,16 @@
26142614
ptypes_str = ptypes_str + "int_array"26152615
end26162616
end2617+
# Block param (&block)2618+
blk = @nd_block[params]2619+
if blk >= 02620+
if @nd_type[blk] == "BlockParameterNode"2621+
if ptypes_str != ""2622+
ptypes_str = ptypes_str + ","2623+
end2624+
ptypes_str = ptypes_str + "proc"2625+
end2626+
end26172627
end26182628
26192629
@meth_names.push(mname)@@ -9250,18 +9260,50 @@
92509260
@proc_counter = @proc_counter + 192519261
fname = "_sp_proc_fn_" + @proc_counter.to_s92529262
bbody = @nd_body[blk]9253-
# Compile body as expression9263+
# Save current output, compile body into a separate buffer9264+
save_out = @out9265+
@out = ""9266+
push_scope9267+
declare_var(bp, "int")92549268
bexpr = "0"9269+
body_stmts = ""92559270
if bbody >= 092569271
bs = get_stmts(bbody)92579272
if bs.length > 09258-
push_scope9259-
declare_var(bp, "int")9260-
bexpr = compile_expr(bs[bs.length - 1])9261-
pop_scope9273+
# Compile all statements (including side effects of last)9274+
k = 09275+
while k < bs.length9276+
lt = infer_type(bs[k])9277+
if k == bs.length - 19278+
if lt != "void"9279+
# Last statement: compile all previous, then get return expr9280+
body_stmts = @out9281+
@out = ""9282+
bexpr = compile_expr(bs[k])9283+
extra = @out9284+
@out = ""9285+
body_stmts = body_stmts + extra9286+
else9287+
# Last is void (like puts): compile as statement, return 09288+
compile_stmt(bs[k])9289+
end9290+
else9291+
compile_stmt(bs[k])9292+
end9293+
k = k + 19294+
end9295+
if body_stmts == ""9296+
body_stmts = @out9297+
@out = ""9298+
end92629299
end92639300
end9264-
# Use GCC nested function to define function inline9301+
pop_scope9302+
@out = save_out9303+
# Build function body9304+
if body_stmts != ""9305+
return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { " + body_stmts.strip + " return " + bexpr + "; } sp_proc_new(" + fname + "); })"9306+
end92659307
return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { return " + bexpr + "; } sp_proc_new(" + fname + "); })"92669308
end92679309