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 end
26162616 end
2617+ # Block param (&block)
2618+ blk = @nd_block[params]
2619+ if blk >= 0
2620+ if @nd_type[blk] == "BlockParameterNode"
2621+ if ptypes_str != ""
2622+ ptypes_str = ptypes_str + ","
2623+ end
2624+ ptypes_str = ptypes_str + "proc"
2625+ end
2626+ end
26172627 end
26182628
26192629 @meth_names.push(mname)
@@ -9250,18 +9260,50 @@
92509260 @proc_counter = @proc_counter + 1
92519261 fname = "_sp_proc_fn_" + @proc_counter.to_s
92529262 bbody = @nd_body[blk]
9253- # Compile body as expression
9263+ # Save current output, compile body into a separate buffer
9264+ save_out = @out
9265+ @out = ""
9266+ push_scope
9267+ declare_var(bp, "int")
92549268 bexpr = "0"
9269+ body_stmts = ""
92559270 if bbody >= 0
92569271 bs = get_stmts(bbody)
92579272 if bs.length > 0
9258- push_scope
9259- declare_var(bp, "int")
9260- bexpr = compile_expr(bs[bs.length - 1])
9261- pop_scope
9273+ # Compile all statements (including side effects of last)
9274+ k = 0
9275+ while k < bs.length
9276+ lt = infer_type(bs[k])
9277+ if k == bs.length - 1
9278+ if lt != "void"
9279+ # Last statement: compile all previous, then get return expr
9280+ body_stmts = @out
9281+ @out = ""
9282+ bexpr = compile_expr(bs[k])
9283+ extra = @out
9284+ @out = ""
9285+ body_stmts = body_stmts + extra
9286+ else
9287+ # Last is void (like puts): compile as statement, return 0
9288+ compile_stmt(bs[k])
9289+ end
9290+ else
9291+ compile_stmt(bs[k])
9292+ end
9293+ k = k + 1
9294+ end
9295+ if body_stmts == ""
9296+ body_stmts = @out
9297+ @out = ""
9298+ end
92629299 end
92639300 end
9264- # Use GCC nested function to define function inline
9301+ pop_scope
9302+ @out = save_out
9303+ # Build function body
9304+ if body_stmts != ""
9305+ return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { " + body_stmts.strip + " return " + bexpr + "; } sp_proc_new(" + fname + "); })"
9306+ end
92659307 return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { return " + bexpr + "; } sp_proc_new(" + fname + "); })"
92669308 end
92679309