Skip to content

Commit

Permalink
[wip] Added rpn calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpilot committed Oct 30, 2009
1 parent ae1080b commit 3e737d1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions wip/rpm.p6
@@ -0,0 +1,28 @@
#!/usr/bin/perl6

# RPN calulator from the bottom of http://use.perl.org/~pmichaud/journal/38580

# Excellent example, not sure where/if it fits

use v6;

my %op_dispatch_table = {
'+' => { .push(.pop + .pop) },
'-' => { .push(.pop R- .pop) },
'*' => { .push(.pop * .pop) },
'/' => { .push(.pop R/ .pop) },
'sqrt' => { .push(.pop.sqrt) },
};

sub evaluate (%odt, $expr) {
my @stack;
my @tokens = $expr.split(/\s+/);
for @tokens {
when /\d+/ { @stack.push($_); }
when ?%odt{$_} { %odt{$_}(@stack); }
default { die "Unrecognized token '$_'; aborting"; }
}
@stack.pop;
}

say "Result: { evaluate(%op_dispatch_table, @*ARGS[0]) }";

0 comments on commit 3e737d1

Please sign in to comment.