-
-
Save Altai-man/a87e4d241b952afc955391165dcf3af7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use v6; | |
# FROM: http://daniel.ruoso.com/categoria/perl/rpn-calculator-perl6 by daniel@ruoso.com | |
multi infix:<rpn> (@stack, $num where /^ \d+[\.\d+]? $/) { | |
[ @stack, $num ]; | |
} | |
multi infix:<rpn> (@stack, $op where /^ '+' | '-' | '*' | '/' $/) { | |
my $lhs = @stack.pop; | |
my $rhs = @stack.pop; | |
my $res = do given $op { | |
when '+' { $rhs + $lhs } | |
when '-' { $rhs - $lhs } | |
when '*' { $rhs * $lhs } | |
when '/' { $rhs / $lhs } | |
} | |
[ @stack, $res ] | |
} | |
multi infix:<rpn> ($any, $unknown) { | |
die "Unknown expression near $any $unknown"; | |
} | |
say [rpn] [], (~@*ARGS).words; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment