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 to228+
# 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
end56485652
}56495653
5650-
# Top-level `alias $copy $orig`. Records the rewrite in5651-
# @galias_* so sanitize_gvar / scan_features / infer_type5652-
# treat $copy and $orig as the same storage slot.5654+
# Top-level `alias $copy $orig` and BEGIN. Aliases are5655+
# recorded into @galias_* (consulted by sanitize_gvar /5656+
# scan_features / infer_type so $copy and $orig share5657+
# storage). BEGIN bodies are queued for emit_main to hoist5658+
# 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
end56615667
end5668+
if @nd_type[sid] == "PreExecutionNode"5669+
# Parser maps the "statements" field onto @nd_body via5670+
# set_ref_field at line 706.5671+
bid = @nd_body[sid]5672+
if bid >= 05673+
@pre_execution_blocks.push(bid)5674+
end5675+
end56625676
}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 types1841218426
scan_lambda_ret_types(stmts)1841318427
18428+
# `BEGIN { ... }` hoist: each PreExecutionNode body runs at the18429+
# very top of main, in source-encounter order, BEFORE any other18430+
# top-level statements. Bodies were collected by collect_all.18431+
pi = 018432+
while pi < @pre_execution_blocks.length18433+
bnid = @pre_execution_blocks[pi]18434+
if bnid >= 018435+
compile_stmt(bnid)18436+
end18437+
pi = pi + 118438+
end18439+
1841418440
# Compile main statements1841518441
stmts.each { |sid|1841618442
if @nd_type[sid] != "DefNode"@@ -26277,6 +26303,13 @@
2627726303
# already recorded the removal in @undef_*; nothing to emit.2627826304
return2627926305
end26306+
if t == "PreExecutionNode"26307+
# `BEGIN { ... }`. collect_all already pushed the body onto26308+
# @pre_execution_blocks; emit_main hoists it to the top of26309+
# main(). The toplevel statement itself is a no-op at its26310+
# source location.26311+
return26312+
end2628026313
if t == "LocalVariableWriteNode"2628126314
lname = @nd_name[nid]2628226315
# 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…