Commit 7fc5e446

7fc5e446c39fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

Merge pull request #308 from ryanseys/rs-up-pre-execution

feat(parse,codegen): support PreExecutionNode (BEGIN { ... })

Details

Commit
7fc5e446c39f
Parent
26ed1c916a4a
Merge parent
5524d56c2230

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