Advertisement
Guest User

Untitled

a guest
Dec 26th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.88 KB | None | 0 0
  1. grammar Math {
  2.     rule TOP { ^ <expr> $ }
  3.     rule expr { <term> [ <op> <expr> ]? }
  4.     rule term { (\d+) }
  5.     rule op { ('+'|'-') }
  6. }
  7. class Math::Actions {
  8.     method TOP($/) { make $<expr>.ast }
  9.     method expr($/) {
  10.         if $<op> {
  11.             my $lhs = +$<term>;
  12.             my $rhs = +($<expr>[0].ast);
  13.             given $<op>[0].ast {
  14.                 when '+' { make $lhs + $rhs }
  15.                 when '-' { make $lhs - $rhs }
  16.             }
  17.         } else {
  18.             make $<term>;
  19.         }
  20.     }
  21.     method op($/) { make $0 }
  22. }
  23. my $total = Math.parse("1 + 2 + 3", :actions(Math::Actions));
  24. say "$total = {$total.ast}";
  25.  
  26. # Gives this error on call to Math.parse-
  27. # too many named arguments: 1 passed, 0 used
  28. #   in !cursor_init at src/stage2/QRegex.nqp:491
  29. #   in method parse at src/gen/CORE.setting:5915
  30. #   in <anon> at foo.pl:23
  31. #   in <anon> at foo.pl:1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement