Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
[nqp]: Add an optimization to inline immediate blocks w/o lexical dec…
Browse files Browse the repository at this point in the history
…larations.
  • Loading branch information
pmichaud committed Oct 27, 2009
1 parent 79ebf77 commit 3fa19de
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/NQP/Actions.pm
Expand Up @@ -18,7 +18,7 @@ method statementlist($/) {
for $<statement> {
my $ast := $_.ast;
if $ast.isa(PAST::Block) && !$ast.blocktype {
$ast.blocktype('immediate');
$ast := block_immediate($ast);
}
$past.push( $ast );
}
Expand All @@ -35,9 +35,7 @@ method statement($/) {
}

method xblock($/) {
my $pblock := $<pblock>.ast;
$pblock.blocktype('immediate');
make PAST::Op.new( $<EXPR>.ast, $pblock, :pasttype('if'), :node($/) );
make PAST::Op.new( $<EXPR>.ast, $<pblock>.ast, :pasttype('if'), :node($/) );
}

method pblock($/) {
Expand All @@ -61,30 +59,28 @@ method newpad($/) {

method statement_control:sym<if>($/) {
my $count := +$<xblock> - 1;
my $past := $<xblock>[$count].ast;
my $past := xblock_immediate( $<xblock>[$count].ast );
if $<else> {
my $else := $<else>[0].ast;
$else.blocktype('immediate');
$past.push($else);
$past.push( block_immediate( $<else>[0].ast ) );
}
# build if/then/elsif structure
while $count > 0 {
$count--;
my $else := $past;
$past := $<xblock>[$count].ast;
$past := xblock_immediate( $<xblock>[$count].ast );
$past.push($else);
}
make $past;
}

method statement_control:sym<unless>($/) {
my $past := $<xblock>.ast;
my $past := xblock_immediate( $<xblock>.ast );
$past.pasttype('unless');
make $past;
}

method statement_control:sym<while>($/) {
my $past := $<xblock>.ast;
my $past := xblock_immediate( $<xblock>.ast );
$past.pasttype(~$<sym>);
make $past;
}
Expand All @@ -93,13 +89,11 @@ method statement_control:sym<repeat>($/) {
my $pasttype := 'repeat_' ~ ~$<wu>;
my $past;
if $<xblock> {
$past := $<xblock>.ast;
$past := xblock_immediate( $<xblock>.ast );
$past.pasttype($pasttype);
}
else {
$past := $<pblock>.ast;
$past.blocktype('immediate');
$past := PAST::Op.new( $<EXPR>.ast, $past,
$past := PAST::Op.new( $<EXPR>.ast, block_immediate( $<pblock>.ast ),
:pasttype($pasttype), :node($/) );
}
make $past;
Expand All @@ -112,6 +106,7 @@ method statement_control:sym<for>($/) {
$block[0].push( PAST::Var.new( :name('$_'), :scope('parameter') ) );
$block.symbol('$_', :scope('lexical') );
$block.arity(1);
$block.blocktype('immediate');
make $past;
}

Expand Down Expand Up @@ -333,6 +328,21 @@ method postfix:sym<-->($/) {
:pasttype('inline') );
}

sub xblock_immediate($xblock) {
$xblock[1] := block_immediate($xblock[1]);
$xblock;
}

sub block_immediate($block) {
$block.blocktype('immediate');
unless $block.symtable() {
my $stmts := PAST::Stmts.new( :node($block) );
for $block.list { $stmts.push($_); }
$block := $stmts;
}
$block;
}

sub sigiltype($sigil) {
$sigil eq '%'
?? 'Hash'
Expand Down

0 comments on commit 3fa19de

Please sign in to comment.