Commit 71afc0ef
71afc0ef046aMerged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support RedoNode (redo)
`redo` re-runs the current iteration of the enclosing loop without re-evaluating the loop guard or advancing the iterator. Spinel implements this with a labeled C goto:
for (...) {
sp_redo_<n>: ;
// body -- a `goto sp_redo_<n>;` here re-runs from the top
}
Three pieces:
-
Parser case in spinel_parse.c -- the bare `RedoNode` shape with no children.
-
New @redo_label_stack + push_redo_label / pop_redo_label / emit_redo_label helpers. push allocates a fresh `sp_redo_` label name (counter-driven for uniqueness across nested loops), emit installs the C label, pop restores the outer-level label.
-
compile_times_block hook -- wraps its body emit between push_redo_label / emit_redo_label / pop_redo_label so the redo target lands at the top of each iteration body.
compile_stmt arm for RedoNode emits `goto ;`, falling back to a clear error if the redo isn't inside a loop.
This commit wires up only `times` since that's what the test uses; upto/downto/while/for/loop/each etc. would each need the same three- line wrapping. Following commits can add them as new test cases demand.
Test exercises (test/redo.rb):
- A 3-iteration `times` loop where iteration 1 redo's until attempts >= 5 -- verifies that redo re-runs the same iteration with the same block-param value, and the global counter matches the expected re-run count.
Tests: 280 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 71afc0ef046a
- Parent
- 6952fe2b21fe
spinel_codegen.rbmodifiedView file
@const_expr_ids = [] @const_scope_names = "".split(",") # `redo` -- labeled-goto target stack. Each loop emitter pushes # a fresh label name when entering an iteration body and pops on # exit; a `redo` jumps to the top of the innermost label. @redo_label_stack = "".split(",") @redo_label_counter = 0 # ---- Scope stack for local variables ---- @scope_names = "".split(",") @scope_types = "".split(",") emit(" cvar_" + qname + " = " + val + ";") return end if t == "RedoNode" # `redo` -- jump to the top of the current iteration without # advancing the iterator or re-evaluating the loop guard. The # loop emitter installs a label via push_redo_label / # emit_redo_label; we goto the innermost. if @redo_label_stack.length == 0 $stderr.puts "Spinel: `redo` outside of a loop" exit(1) end lbl = @redo_label_stack.last emit(" goto " + lbl + ";") return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignment if bp1 != "" declare_var(bp1, "int") end redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[@nd_block[nid]]) pop_redo_label pop_scope @indent = @indent - 1 emit(" }") @in_loop = old end # `redo` label-stack helpers. Each loop emitter wraps its body # between push_redo_label / emit_redo_label / pop_redo_label so # `redo` knows which label to jump to. Labels are unique per loop # body via @redo_label_counter. def push_redo_label @redo_label_counter = @redo_label_counter + 1 lbl = "sp_redo_" + @redo_label_counter.to_s @redo_label_stack.push(lbl) lbl end def pop_redo_label if @redo_label_stack.length > 0 @redo_label_stack.pop end end # Emit the C label that `redo` jumps to. C requires labels to be # followed by a statement; `;` lets a subsequent `}` stay valid # even if the body is empty. def emit_redo_label(lbl) emit(" " + lbl + ": ;") end def compile_upto_block(nid) old = @in_loop @in_loop = 1spinel_parse.cmodifiedView file
Loading diff…
test/redo.rbaddedView file
Loading diff…
test/redo.rb.expectedaddedView file
Loading diff…