http://www.perlmonks.org?node_id=536453


in reply to P6: Rule and RPN calc

When I gave up:

#! /usr/bin/env perl use v6; my %operators = ( '+' => sub { [+] @_ }, '-' => sub { [-] @_ }, '*' => sub { [*] @_ }, '/' => sub { [/] @_ }, ); grammar rpn_data { rule value { -?\d+ } rule operator { <[+*/\-]> } rule expression { } } sub rpn_eval (Str $e) returns Num { my @stack; # die "Invalid RPN: $e" unless $e ~~ /<expression>/; for $e.split() -> $tok { if $tok ~~ /<value> / { @stack.push($tok); next } @stack.push(%operators{$tok}(reverse @stack.pop, @stack.pop)); } @stack[0] } say rpn_eval($_) while $_ = =<>

For the most part, writing this was very exciting -- for the first time I delved into the perl6 rules stuff and tried to use it, and from this I now see how I would've solved this problem -- with code blocks after matches to neatly fail if I discovered improper RPN, or even with a modified %operators in the operator rule that evaluated the RPN as it was parsed.

And yet, this was disappointing: none of what I wanted to do was implemented in pugs.

Still, I've now a much better idea of what I've to look forward to. Thanks.

Update: in case anyone asks, there are no checks for overflow/underflow or such in my rpn_eval because I expected to have already verified that the RPN wouldn't have such problems. No point in uglyifying this test code with such checks when I discover that I momentarily can't. With time, I'll have this as the core of a real implementation.