Not Quite Perl Not Quite Perl - A lightweight Perl 6 #15

Example "compiler", part 1 of 2

# parser
grammar ABC::Grammar {
    rule TOP { <expr> }
    rule expr { <integer> <addop> <integer> };
    token addop { '+' | '-' }
    token integer { \d+ }
}

# transform to ast
class ABC::Actions {
    method TOP($/) {
        make PAST::Block.new( $<expr>.ast );
    }

    method expr($/) { 
        my $pirop := $<addop> eq '-' ?? 'sub' !! 'add';
        make PAST::Op.new( :pirop($pirop), 
                 +$<integer>[0], +$<integer>[1]);
    }
}
Copyright © 2010
http://www.pmichaud.com/2010/pres/