Course of Raku / Addendum 🆕 / Bringing it together / Mixed mini-projects / Exercises / A reverse-Polish calculator

Solution: A reverse-Polish calculator

Here is a possible solution to the task.

Code

my $expression = '3 4 + 5 *';
my @stack;

for $expression.words -> $token {
    if $token ~~ / ^ \d+ $ / {
        @stack.push($token.Int);
    }
    else {
        my $b = @stack.pop;
        my $a = @stack.pop;
        @stack.push( do given $token {
            when '+' { $a + $b }
            when '-' { $a - $b }
            when '*' { $a * $b }
            when '/' { $a / $b }
        });
    }
}

say @stack[0];

🦋 You can find the source code in the file rpn-calculator.raku.

Output

35

Comments

  1. Numbers are pushed onto a stack. An operator pops the two most recent values, combines them, and pushes the result back — the essence of postfix evaluation.

  2. do given $token turns the operator into the right arithmetic, and the order $a then $b (popped in reverse) keeps subtraction and division the right way round.

Course navigation

A reverse-Polish calculator   |   A text histogram