Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grammar Math {
- rule TOP { ^ <expr> $ }
- rule expr { <term> [ <op> <expr> ]? }
- rule term { (\d+) }
- rule op { ('+'|'-') }
- }
- class Math::Actions {
- method TOP($/) { make $<expr>.ast }
- method expr($/) {
- if $<op> {
- my $lhs = +$<term>;
- my $rhs = +($<expr>[0].ast);
- given $<op>[0].ast {
- when '+' { make $lhs + $rhs }
- when '-' { make $lhs - $rhs }
- }
- } else {
- make $<term>;
- }
- }
- method op($/) { make $0 }
- }
- my $total = Math.parse("1 + 2 + 3", :actions(Math::Actions));
- say "$total = {$total.ast}";
- # Gives this error on call to Math.parse-
- # too many named arguments: 1 passed, 0 used
- # in !cursor_init at src/stage2/QRegex.nqp:491
- # in method parse at src/gen/CORE.setting:5915
- # in <anon> at foo.pl:23
- # in <anon> at foo.pl:1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement