Skip to content

Instantly share code, notes, and snippets.

@Altai-man
Created March 14, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Altai-man/a87e4d241b952afc955391165dcf3af7 to your computer and use it in GitHub Desktop.
Save Altai-man/a87e4d241b952afc955391165dcf3af7 to your computer and use it in GitHub Desktop.
#!/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