Skip to content

Commit e96422c

Browse files
committed
Implement sub MAIN. The multi case probably works too (if you declare a proto). Also should handle the case where it's not in the outermost scope (Rakudo gets that wrong...steal this fix! ;-)).
1 parent c910ccf commit e96422c

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/NQP/Actions.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ class NQP::Actions is HLL::Actions {
6969
PAST::Op.new( :pirop('get_class Ps'), 'LexPad' ),
7070
PAST::Op.new( :pirop('get_class Ps'), 'NQPLexPad' )
7171
));
72+
73+
# If we have a MAIN sub, call it at end of mainline.
74+
if $*MAIN_SUB {
75+
$mainline.push(PAST::Op.new(
76+
:pasttype('call'), PAST::Val.new( :value($*MAIN_SUB) )
77+
));
78+
}
7279

7380
# We force a return here, because we have other
7481
# :load/:init blocks to execute that we don't want
@@ -712,6 +719,11 @@ class NQP::Actions is HLL::Actions {
712719
else {
713720
$/.CURSOR.panic("$*SCOPE scoped routines are not supported yet");
714721
}
722+
723+
# Is it the MAIN sub?
724+
if $name eq 'MAIN' && $*MULTINESS ne 'multi' {
725+
$*MAIN_SUB := $block;
726+
}
715727
}
716728

717729
# Apply traits.

src/NQP/Grammar.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ grammar NQP::Grammar is HLL::Grammar {
108108

109109
token comp_unit {
110110
:my $*HAS_YOU_ARE_HERE := 0;
111+
:my $*MAIN_SUB;
111112
<.newpad>
112113
<.outerctx>
113114
<statementlist>

0 commit comments

Comments
 (0)