Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I stumbled across Dice::Simple and RPG::Dice in the code section, and it reminded me of a Pascal program I wrote back in the early '90s for a class. A typical calculator assignment, that I later tweaked for RPG use. I'd love to port it to Perl, but because of the syntax I can't cheat and use eval() like the aforementioned modules. Attached is the help doc as well as a few run examples. Should I change the syntax, or what's a good starting point other than brute-force character-by-character parsing as I did originally?
Help Doc: ---------------------------- Key: n = number (integers only) l = list (of anything) ln = list of numbers exp = expression (i.e. any valid expression) Operators: +,-,/,* Integer math only d ex. n1dn2 = Roll a n2 sided die n1 times and sum the results. @[] ex. n@[expr] = evaluate expr n times and put the results in a list. <,> Selectors. ex. n1>ln = select the greatest n1 items from the list ln. & ex. &ln = sum the numbers in the list ln. Default order of prececence, highest to lowest: ()'s d *,/ +,- @ >,< & Examples: 3d6 ! Returns a value 3 to 18. (1d4)d20 ! Rolls a 20 sided die 1 to 4 times and sums the results. 6@[3d6] ! Returns 6 values of 3 to 18 in a list. 6>12@[3d6] ! Returns the best 6 of 12 3d6's in a list. 6@[&3>4@[(1d5)+1]] ! Equivelant to the ol' "Roll 6 stats -- 4d6, re-roll 1's, drop the lowest die." Note that n1dn2 is logically equivelant to &n1@[1dn2] ---------------------------- Sample output: ---------------------------- Command: 3d6 Value: 9 Command: 3d6 Value: 10 Command: 6@[3d6] ( 14 13 11 13 9 13) Command: 6@[3d6] ( 7 11 15 17 7 7) Command: 6@[&3>4@[(1d5)+1]] ( 14 12 15 12 10 11) Command: 6@[&3>4@[(1d5)+1]] ( 16 15 12 14 16 14) Command: 4@[6@[3d6]] ( ( 13 8 6 9 4 12) ( 13 7 9 10 11 9) ( 9 12 10 9 13 13) ( 10 8 9 7 7 12) ) Command: X = 3d6 Symbol "X" created. Value: 0 Command: 4@[X] ( 10 12 14 9) Command: X<20@[X] ( 6 7 7 7 8 9 9 10 10 10 11 11 11 12 12) Command: X<20@[X] ( 4 5 7 7) ----------------------------

Replies are listed 'Best First'.
Re: Dice calcs?
by BigLug (Chaplain) on Sep 18, 2002 at 03:27 UTC
    OK. I've taken a look at this and there's are a problem with your order of preference: @[] has to be at the top otherwise 6@[3d8] will roll an 8 sided dice three times and return that value six times. Thus you'll get something like {6, 6, 6}. With the @[] at the top it will run 3d6 three times and you'll get three different values.

    Below is an example of a dice calculator using your given rules. There are no symbol defs though and its not interactive. There's also a lot of code in there for verbose output. You might want to can it by setting $verbose=0 :)

    You'll notice I've changed the precedence to (), @, <>, &, d, *, /, +, - in order to get your examples to work.

      Hmmmm.... I hadn't thought about in-place substitutions. My original assignment was an infix-to-postfix calculator, and the precedence order I listed was required for that algorithm to work.

      I did find a few bugs in your solution. I realize your code was just an example, and I appreciate the insight, I just want to make sure anyone that downloads it realizes there are issues. For example, your code wouldn't allow for multiple @[] operators at the same level (i.e. &3>4@[1d6]+&1>4@[1d100]). Division should be integer. Negative numbers (and thus unary -) need to be implemented. There's no general syntax checking.

      I went ahead with the in-place substitution idea and should have some code ready to post soon. I've addressed all of the above issues and a few more. There are still issues with precedence lists, though.... with this method it is impossible/difficult for operators to share the same precedence. For example, normally something like 5*20/3*3 should equal 99, but in this case it comes up 11 (100/9). That's not a problem as long as it's documented.

        Oh, good bug-find. Here's a couple of fixes that you've prolly already done

        1. To get it to do the multiple @ operators at the same level you need to

        use Regexp::Common
        Then replace the 'Precedence 5' regexp with:
        $term=~s/(\d+)@($RE{balanced}{-parens=>'[]'})/rpt($1,$2)/eg;

        2.To get it to do the * and / from left to right, change 'Precedence 3' to this single regexp:

        $term=~s/(\d+)([\*\/])(\d+)/int(eval("$1$2$3"))/eg;
        and change 'Precedence 4' to
        $term=~s/(\d+)([+-])(\d+)/eval("$1$2$3")/eg;

        Hope these help you with your efforts. There is still no syntax checking but I figure you can handle stopping people from putting 3*+/4. Oh, and you probably want to handle negative numbers before 'Precedence 4'