Skip to content

Instantly share code, notes, and snippets.

@deepthawtz
Forked from ashleydev/gist:453195
Created June 28, 2010 18:10
Show Gist options
  • Save deepthawtz/456170 to your computer and use it in GitHub Desktop.
Save deepthawtz/456170 to your computer and use it in GitHub Desktop.
pmichaud@plum:~/rakudo$ cat x
grammar Expr {
rule TOP { <expr> }
rule expr { <integer> <addop> <integer> };
token addop { '+' | '-' }
token integer { \d+ }
}
class ExprActions {
method TOP($/) { make $<expr>.ast }
method expr($/) {
my $a = $<integer>[0].ast;
my $b = $<integer>[1].ast;
make $<addop> eq '+' ?? $a + $b !! $a - $b;
}
method integer($/) { make +$/; }
}
my $match = Expr.parse("3+4", :actions(ExprActions));
say $match.ast;
pmichaud@plum:~/rakudo$ ./perl6 x
7
pmichaud@plum:~/rakudo$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment