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:

  1. Parser case in spinel_parse.c -- the bare `RedoNode` shape with no children.

  2. 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.

  3. 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

@@ -179,6 +179,12 @@
179179 @const_expr_ids = []
180180 @const_scope_names = "".split(",")
181181
182+ # `redo` -- labeled-goto target stack. Each loop emitter pushes
183+ # a fresh label name when entering an iteration body and pops on
184+ # exit; a `redo` jumps to the top of the innermost label.
185+ @redo_label_stack = "".split(",")
186+ @redo_label_counter = 0
187+
182188 # ---- Scope stack for local variables ----
183189 @scope_names = "".split(",")
184190 @scope_types = "".split(",")
@@ -24894,6 +24900,19 @@
2489424900 emit(" cvar_" + qname + " = " + val + ";")
2489524901 return
2489624902 end
24903+ if t == "RedoNode"
24904+ # `redo` -- jump to the top of the current iteration without
24905+ # advancing the iterator or re-evaluating the loop guard. The
24906+ # loop emitter installs a label via push_redo_label /
24907+ # emit_redo_label; we goto the innermost.
24908+ if @redo_label_stack.length == 0
24909+ $stderr.puts "Spinel: `redo` outside of a loop"
24910+ exit(1)
24911+ end
24912+ lbl = @redo_label_stack.last
24913+ emit(" goto " + lbl + ";")
24914+ return
24915+ end
2489724916 if t == "LocalVariableWriteNode"
2489824917 lname = @nd_name[nid]
2489924918 # Check for method(:name) assignment
@@ -29804,13 +29823,40 @@
2980429823 if bp1 != ""
2980529824 declare_var(bp1, "int")
2980629825 end
29826+ redo_label = push_redo_label
29827+ emit_redo_label(redo_label)
2980729828 compile_stmts_body(@nd_body[@nd_block[nid]])
29829+ pop_redo_label
2980829830 pop_scope
2980929831 @indent = @indent - 1
2981029832 emit(" }")
2981129833 @in_loop = old
2981229834 end
2981329835
29836+ # `redo` label-stack helpers. Each loop emitter wraps its body
29837+ # between push_redo_label / emit_redo_label / pop_redo_label so
29838+ # `redo` knows which label to jump to. Labels are unique per loop
29839+ # body via @redo_label_counter.
29840+ def push_redo_label
29841+ @redo_label_counter = @redo_label_counter + 1
29842+ lbl = "sp_redo_" + @redo_label_counter.to_s
29843+ @redo_label_stack.push(lbl)
29844+ lbl
29845+ end
29846+
29847+ def pop_redo_label
29848+ if @redo_label_stack.length > 0
29849+ @redo_label_stack.pop
29850+ end
29851+ end
29852+
29853+ # Emit the C label that `redo` jumps to. C requires labels to be
29854+ # followed by a statement; `;` lets a subsequent `}` stay valid
29855+ # even if the body is empty.
29856+ def emit_redo_label(lbl)
29857+ emit(" " + lbl + ": ;")
29858+ end
29859+
2981429860 def compile_upto_block(nid)
2981529861 old = @in_loop
2981629862 @in_loop = 1