File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -348,6 +348,11 @@ class NQP::Actions is HLL::Actions {
348
348
);
349
349
}
350
350
351
+ method statement_prefix :sym <BEGIN >($/ ) {
352
+ $ * W . run_begin_block($ < blorst > . ast);
353
+ make PAST::Stmts. new ();
354
+ }
355
+
351
356
method statement_prefix :sym <INIT >($/ ) {
352
357
$ * W . cur_lexpad(). push ($ < blorst > . ast);
353
358
make PAST::Stmts. new ();
Original file line number Diff line number Diff line change @@ -235,7 +235,8 @@ grammar NQP::Grammar is HLL::Grammar {
235
235
}
236
236
237
237
proto token statement_prefix { <...> }
238
- token statement_prefix :sym <INIT > { <sym > <blorst > }
238
+ token statement_prefix :sym <BEGIN > { <sym > <blorst > }
239
+ token statement_prefix :sym <INIT > { <sym > <blorst > }
239
240
240
241
token statement_prefix :sym <try > {
241
242
<sym >
Original file line number Diff line number Diff line change @@ -443,6 +443,36 @@ class NQP::World is HLL::World {
443
443
}
444
444
}
445
445
446
+ # Runs a block at BEGIN time.
447
+ method run_begin_block ($ past ) {
448
+ # Create a wrapper that makes all outer symbols visible.
449
+ my $ wrapper := PAST::Block. new (
450
+ PAST::Stmts. new (),
451
+ $ past
452
+ );
453
+ my % seen ;
454
+ my $ i := + @ ! BLOCKS ;
455
+ while $ i > 0 {
456
+ $ i := $ i - 1 ;
457
+ my % symbols := @ ! BLOCKS [$ i ]. symtable();
458
+ for % symbols {
459
+ if ! % seen {$ _ . key } && pir:: exists ($ _ . value , ' value' ) {
460
+ try {
461
+ $ wrapper [0 ]. push (PAST::Var. new (
462
+ : name($ _ . key ), : scope(' lexical_6model' ), : isdecl(1 ),
463
+ : viviself(self . get_ref(($ _ . value )<value >))
464
+ ));
465
+ };
466
+ % seen {$ _ . key } := 1 ;
467
+ }
468
+ }
469
+ }
470
+
471
+ # Compile and run it.
472
+ my $ code := self . create_code($ wrapper , ' BEGIN block' , 0 );
473
+ $ code ();
474
+ }
475
+
446
476
# Sets NQP language defaults on a block for compilation.
447
477
method set_nqp_language_defaults ($ block ) {
448
478
# Need to load the NQP dynops/dympmcs, plus any extras requested.
You can’t perform that action at this time.
0 commit comments