Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: P6: Rule and RPN calc

by ayrnieu (Beadle)
on Mar 14, 2006 at 02:53 UTC ( #536453=note: print w/replies, xml ) Need Help??


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.

Replies are listed 'Best First'.
Re^2: P6: Rule and RPN calc
by eric256 (Parson) on Mar 14, 2006 at 14:23 UTC
    And yet, this was disappointing: none of what I wanted to do was implemented in pugs.

    Yea that is a pretty normal issue. On the other hand, if its lacking in pugs you can always work on getting it implemented. I've done that with a couple of things, and written tests to help push development of yet other things. I too wanted to eval the RPN as it was evaluated, but that just wasn't in the cards....yet. It will be cool when this is all finished. I know it often feels like pugs isn't actualy getting anywhere, but in the 6 months i've been following it, there have been huge steps forward and there are some very exciting things happening now that promise to bring it even farther in the next six months. Thanks for the alternate solution, i'm thinking about some combination of the two but i think i need something more interesting than RPN to work on now!

    Please post any ideas for other examples to add to the example directory!


    ___________
    Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://536453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-01-26 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?