Commit 60f95775
60f95775a68cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
codegen: loop { break VAL } as expression
Kernel#loop only had a statement form; result = loop { break 42 } lost
the break value and the LHS was typed int with value 0. Add an expression
arm to compile_call_expr that allocates a temp slot, threads it through a
new @loop_expr_break_var stack, and rewrites break VAL inside the body
to tmp = VAL; break;.
The parser was dropping BreakNode/NextNode arguments — mirror
ReturnNode and serialize them so codegen can read the value.
Fixes #916
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 60f95775a68c
- Parent
- 7a78b1d89b48
spinel_codegen.rbmodifiedView file
@@ -356,6 +356,12 @@
356356
@redo_label_stack = "".split(",", -1)357357
@redo_label_counter = 0358358
359+
# `loop { break VAL }` as expression — stack of temp variable names360+
# holding the captured break value. Pushed by compile_loop_expr,361+
# consulted by compile_break_stmt which writes VAL into the top var362+
# before the `break;`.363+
@loop_expr_break_var = "".split(",", -1)364+
359365
# `alias $copy $orig` -- maps new gvar name to its target.360366
# Populated by collect_all from AliasGlobalVariableNode361367
# statements; consulted by sanitize_gvar / scan_features /@@ -17352,6 +17358,12 @@
1735217358
return compile_catch_expr(nid)1735317359
end1735417360
end17361+
# Kernel#loop as expression — captures `break VAL` into a tmp.17362+
if mname == "loop"17363+
if @nd_block[nid] >= 017364+
return compile_loop_expr(nid)17365+
end17366+
end1735517367
if mname == "block_given?"1735617368
return compile_block_given_expr1735717369
end@@ -32643,7 +32655,7 @@
3264332655
return3264432656
end3264532657
if t == "BreakNode"32646-
emit(" break;")32658+
compile_break_stmt(nid)3264732659
return3264832660
end3264932661
if t == "NextNode"@@ -42703,6 +42715,40 @@
4270342715
result + ")"4270442716
end4270542717
42718+
def compile_loop_expr(nid)42719+
blk = @nd_block[nid]42720+
tmp = new_temp42721+
emit(" mrb_int " + tmp + " = 0;")42722+
42723+
old_in_loop = @in_loop42724+
@in_loop = 142725+
@loop_expr_break_var.push(tmp)42726+
emit(" while (1) {")42727+
@indent = @indent + 142728+
redo_label = push_redo_label42729+
emit_redo_label(redo_label)42730+
compile_stmts_body(@nd_body[blk])42731+
pop_redo_label42732+
@indent = @indent - 142733+
emit(" }")42734+
@loop_expr_break_var.pop42735+
@in_loop = old_in_loop42736+
tmp42737+
end42738+
42739+
def compile_break_stmt(nid)42740+
args_id = @nd_arguments[nid]42741+
if args_id >= 0 && @loop_expr_break_var.length > 042742+
arg_ids = get_args(args_id)42743+
if arg_ids.length >= 142744+
tmp = @loop_expr_break_var.last42745+
val = compile_expr(arg_ids[0])42746+
emit(" " + tmp + " = " + val + ";")42747+
end42748+
end42749+
emit(" break;")42750+
end42751+
4270642752
def compile_catch_expr(nid)4270742753
@needs_setjmp = 14270842754
tag = compile_str_arg0(nid)spinel_parse.cmodifiedView file
Loading diff…
test/i916.rbaddedView file
Loading diff…
test/i916.rb.expectedaddedView file
Loading diff…