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:

  1. Parser case in spinel_parse.c -- emits the node with the statements ref slot.

  2. collect_all Pass 2.1 walks the toplevel for PreExecutionNode and queues each body onto @pre_execution_blocks (in source-encounter order).

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

@@ -224,6 +224,10 @@
224224 @undef_class_idx = []
225225 @undef_method = "".split(",")
226226
227+ # `BEGIN { ... }` bodies, in source-encounter order. Hoisted to
228+ # the top of main() during emit_main.
229+ @pre_execution_blocks = []
230+
227231 # ---- Scope stack for local variables ----
228232 @scope_names = "".split(",")
229233 @scope_types = "".split(",")
@@ -5647,9 +5651,11 @@
56475651 end
56485652 }
56495653
5650- # Top-level `alias $copy $orig`. Records the rewrite in
5651- # @galias_* so sanitize_gvar / scan_features / infer_type
5652- # treat $copy and $orig as the same storage slot.
5654+ # Top-level `alias $copy $orig` and BEGIN. Aliases are
5655+ # recorded into @galias_* (consulted by sanitize_gvar /
5656+ # scan_features / infer_type so $copy and $orig share
5657+ # storage). BEGIN bodies are queued for emit_main to hoist
5658+ # to the top of main() in source-encounter order.
56535659 stmts.each { |sid|
56545660 if @nd_type[sid] == "AliasGlobalVariableNode"
56555661 nn = @nd_name[@nd_new_name[sid]]
@@ -5659,6 +5665,14 @@
56595665 @galias_old.push(on)
56605666 end
56615667 end
5668+ if @nd_type[sid] == "PreExecutionNode"
5669+ # Parser maps the "statements" field onto @nd_body via
5670+ # set_ref_field at line 706.
5671+ bid = @nd_body[sid]
5672+ if bid >= 0
5673+ @pre_execution_blocks.push(bid)
5674+ end
5675+ end
56625676 }
56635677
56645678 # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into
@@ -18411,6 +18425,18 @@
1841118425 # Pre-scan: map lambda variable names to their return types
1841218426 scan_lambda_ret_types(stmts)
1841318427
18428+ # `BEGIN { ... }` hoist: each PreExecutionNode body runs at the
18429+ # very top of main, in source-encounter order, BEFORE any other
18430+ # top-level statements. Bodies were collected by collect_all.
18431+ pi = 0
18432+ while pi < @pre_execution_blocks.length
18433+ bnid = @pre_execution_blocks[pi]
18434+ if bnid >= 0
18435+ compile_stmt(bnid)
18436+ end
18437+ pi = pi + 1
18438+ end
18439+
1841418440 # Compile main statements
1841518441 stmts.each { |sid|
1841618442 if @nd_type[sid] != "DefNode"
@@ -26277,6 +26303,13 @@
2627726303 # already recorded the removal in @undef_*; nothing to emit.
2627826304 return
2627926305 end
26306+ if t == "PreExecutionNode"
26307+ # `BEGIN { ... }`. collect_all already pushed the body onto
26308+ # @pre_execution_blocks; emit_main hoists it to the top of
26309+ # main(). The toplevel statement itself is a no-op at its
26310+ # source location.
26311+ return
26312+ end
2628026313 if t == "LocalVariableWriteNode"
2628126314 lname = @nd_name[nid]
2628226315 # Check for method(:name) assignment