Commit 8897b70f
8897b70f6018Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support PostExecutionNode (END { ... })
CRuby runs END blocks at program exit in REVERSE registration order (LIFO). Spinel emits each block as a static C function and registers them via atexit() at main() startup -- atexit naturally invokes handlers LIFO, so the order matches without any extra bookkeeping.
Three pieces:
-
Parser case in spinel_parse.c -- emits the node with the `statements` ref slot.
-
collect_all Pass 2.1 walks the toplevel for PostExecutionNode and queues each body onto @post_execution_blocks (in source-encounter order).
-
emit_post_execution_funcs (called from compile() right before emit_main) emits one zero-arg static C function per block:
static void sp_end_block_<n>(void) { /* body */ }emit_main then inserts `atexit(sp_end_block_);` calls in source order, after the BEGIN-block hoist and before the regular statement loop.
The compile_stmt arm for PostExecutionNode at its source location is a no-op.
Out of scope:
- END blocks that capture closure state from the surrounding scope (the static C function has no enclosing locals to read).
Test exercises (test/post_execution.rb):
- Two END blocks with `puts "last-1"` and `puts "last-2"` -- verifies LIFO order ("last-2" prints before "last-1") and that END runs after the regular statement-flow output.
Tests: 279 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 8897b70f6018
- Parent
- 7fc5e446c39f
spinel_codegen.rbmodifiedView file
# the top of main() during emit_main. @pre_execution_blocks = [] # `END { ... }` bodies, in source-encounter order. Each emits a # static C function; main() startup registers them via atexit() # which naturally invokes handlers LIFO -- matches CRuby's # reverse-of-source-order END execution. @post_execution_blocks = [] # ---- Scope stack for local variables ---- @scope_names = "".split(",") @scope_types = "".split(",") @pre_execution_blocks.push(bid) end end if @nd_type[sid] == "PostExecutionNode" bid = @nd_body[sid] if bid >= 0 @post_execution_blocks.push(bid) end end } # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into emit_class_methods emit_ieval_funcs emit_toplevel_methods # `END { ... }` -- emit one zero-arg static C function per # PostExecutionNode body. main() startup will atexit()-register # them in source order; atexit invokes handlers LIFO, matching # CRuby's reverse-order END execution. emit_post_execution_funcs # Emit lambda functions before main (they are generated during compilation) # We emit them in emit_main after forward declarations emit_main end end # `END { ... }` -- one zero-arg static C function per registered # PostExecutionNode body. main() registers them via atexit() at # startup. atexit invokes handlers LIFO, matching CRuby's # reverse-of-source-order END execution. def emit_post_execution_funcs pe = 0 while pe < @post_execution_blocks.length bnid = @post_execution_blocks[pe] emit_raw("static void sp_end_block_" + pe.to_s + "(void) {") @indent = 1 @in_main = 1 push_scope if bnid >= 0 compile_stmt(bnid) end pop_scope @in_main = 0 emit_raw("}") emit_raw("") pe = pe + 1 end end def emit_toplevel_method(mi) mfullname = @meth_names[mi] @current_method_name = mfullname pi = pi + 1 end # `END { ... }` register-atexit: each PostExecutionNode body # was emitted as a static `sp_end_block_<n>` function during # emit_post_execution_funcs. We register them in source order; # atexit invokes handlers LIFO, matching CRuby's reverse-order # END semantics. pe = 0 while pe < @post_execution_blocks.length emit(" atexit(sp_end_block_" + pe.to_s + ");") pe = pe + 1 end # Compile main statements stmts.each { |sid| if @nd_type[sid] != "DefNode" # source location. return end if t == "PostExecutionNode" # `END { ... }`. collect_all pushed the body onto # @post_execution_blocks; emit_post_execution_funcs emits the # static sp_end_block_<n> function and emit_main inserts an # atexit() call at startup. return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/post_execution.rbaddedView file
Loading diff…
test/post_execution.rb.expectedaddedView file
Loading diff…