Skip to content

Commit

Permalink
Add a $MAIN parameter to unit mainline (signals that the unit is the …
Browse files Browse the repository at this point in the history
…mainline).
  • Loading branch information
pmichaud committed Jul 7, 2010
1 parent 5f40d37 commit b0cd2b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/Perl6/Actions.pm
Expand Up @@ -51,7 +51,6 @@ method comp_unit($/, $key?) {
# Get the block for the unit mainline code.
my $unit := @BLOCK.shift;
my $mainline := $<statementlist>.ast;
$unit.push($mainline);

# Get the block for the entire compilation unit.
my $outer := @BLOCK.shift;
Expand All @@ -60,26 +59,34 @@ method comp_unit($/, $key?) {

# If it's the setting, just need to run the mainline.
if $*SETTING_MODE {
$unit.push($mainline);
$unit.hll($?RAKUDO_HLL);
$unit.pirflags(':init :load');
make $unit;
return 1;
}

my $mainparam := PAST::Var.new(:name('$MAIN'), :scope('parameter'),
:viviself( PAST::Val.new( :value(0) ) ) );
$unit.symbol('$MAIN', :scope<lexical>);
# run MAIN subs
# TODO: run this only when not in a module
# TODO: find a less hacky solution than IN_EVAL
# TODO: find a way to inject MAIN_HELPER call without modifying
# the return value of the compilation unit
if !IN_EVAL() && $unit.symbol('&MAIN') {
$unit.push(
$mainline :=
PAST::Op.new(
:pasttype('call'),
:name('&MAIN_HELPER')
)
);
:name('&MAIN_HELPER'),
$mainline,
$mainparam
);
}

else {
$unit.push($mainparam);
}
$unit.push($mainline);

# Executing the compilation unit causes the mainline to be executed.
# We force a tailcall here, because we have other :load/:init blocks
Expand Down
4 changes: 2 additions & 2 deletions src/core/MAIN.pm
@@ -1,4 +1,4 @@
our sub MAIN_HELPER() {
our sub MAIN_HELPER($retval, $MAIN?) {
my $m = Q:PIR {
$P0 = getinterp
$P0 = $P0['lexpad';1]
Expand All @@ -11,7 +11,7 @@ our sub MAIN_HELPER() {
done:
};
unless $m {
return;
return $retval;
}

# We found MAIN, let's process the command line arguments accordingly
Expand Down

0 comments on commit b0cd2b8

Please sign in to comment.