From b0cd2b8f32315ecf54dc1a3cfe831e40b0edeb3b Mon Sep 17 00:00:00 2001 From: pmichaud Date: Wed, 7 Jul 2010 02:02:56 -0500 Subject: [PATCH] Add a $MAIN parameter to unit mainline (signals that the unit is the mainline). --- src/Perl6/Actions.pm | 19 +++++++++++++------ src/core/MAIN.pm | 4 ++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Perl6/Actions.pm b/src/Perl6/Actions.pm index a321f51880c..ab06f7ff8d3 100644 --- a/src/Perl6/Actions.pm +++ b/src/Perl6/Actions.pm @@ -51,7 +51,6 @@ method comp_unit($/, $key?) { # Get the block for the unit mainline code. my $unit := @BLOCK.shift; my $mainline := $.ast; - $unit.push($mainline); # Get the block for the entire compilation unit. my $outer := @BLOCK.shift; @@ -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); # 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 diff --git a/src/core/MAIN.pm b/src/core/MAIN.pm index ae0561a91c7..0fef7be6170 100644 --- a/src/core/MAIN.pm +++ b/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] @@ -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