Commit 5524d56c
5524d56c2230Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support PreExecutionNode (BEGIN { ... })
CRuby runs all BEGIN blocks in source order BEFORE any other top- level statements, regardless of where in the file they appear. The classic Perl-inheritance quirk -- BEGIN at the bottom still fires first.
Spinel implements this by:
-
Parser case in spinel_parse.c -- emits the node with the
statementsref slot. -
collect_all Pass 2.1 walks the toplevel for PreExecutionNode and queues each body onto @pre_execution_blocks (in source-encounter order).
-
emit_main runs through @pre_execution_blocks BEFORE the regular toplevel-statement loop, calling compile_stmt on each body. The PreExecutionNode at its source location is a no-op (compile_stmt arm).
This puts every BEGIN body at the top of main() in source order even if its source location is at file end -- matching CRuby.
Out of scope:
- BEGIN inside a method body (CRuby doesn't allow this either; we rely on Prism rejecting it at parse time).
- BEGIN inside a class body that affects class init ordering -- Spinel doesn't run class bodies at startup, so the question doesn't arise.
Test exercises (test/pre_execution.rb):
- Two BEGIN blocks both before any toplevel `puts` -- verify they run before "middle" and "last", in source-encounter order.
Tests: 278 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 5524d56c2230
- Parent
- 26ed1c916a4a
spinel_codegen.rbmodifiedView file
@undef_class_idx = [] @undef_method = "".split(",") # `BEGIN { ... }` bodies, in source-encounter order. Hoisted to # the top of main() during emit_main. @pre_execution_blocks = [] # ---- Scope stack for local variables ---- @scope_names = "".split(",") @scope_types = "".split(",") end } # Top-level `alias $copy $orig`. Records the rewrite in # @galias_* so sanitize_gvar / scan_features / infer_type # treat $copy and $orig as the same storage slot. # Top-level `alias $copy $orig` and BEGIN. Aliases are # recorded into @galias_* (consulted by sanitize_gvar / # scan_features / infer_type so $copy and $orig share # storage). BEGIN bodies are queued for emit_main to hoist # to the top of main() in source-encounter order. stmts.each { |sid| if @nd_type[sid] == "AliasGlobalVariableNode" nn = @nd_name[@nd_new_name[sid]] @galias_old.push(on) end end if @nd_type[sid] == "PreExecutionNode" # Parser maps the "statements" field onto @nd_body via # set_ref_field at line 706. bid = @nd_body[sid] if bid >= 0 @pre_execution_blocks.push(bid) end end } # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into # Pre-scan: map lambda variable names to their return types scan_lambda_ret_types(stmts) # `BEGIN { ... }` hoist: each PreExecutionNode body runs at the # very top of main, in source-encounter order, BEFORE any other # top-level statements. Bodies were collected by collect_all. pi = 0 while pi < @pre_execution_blocks.length bnid = @pre_execution_blocks[pi] if bnid >= 0 compile_stmt(bnid) end pi = pi + 1 end # Compile main statements stmts.each { |sid| if @nd_type[sid] != "DefNode" # already recorded the removal in @undef_*; nothing to emit. return end if t == "PreExecutionNode" # `BEGIN { ... }`. collect_all already pushed the body onto # @pre_execution_blocks; emit_main hoists it to the top of # main(). The toplevel statement itself is a no-op at its # source location. return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/pre_execution.rbaddedView file
Loading diff…
test/pre_execution.rb.expectedaddedView file
Loading diff…