On Thu, Oct 2, 2014 at 2:45 AM, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > Code for evaluating mathematical expressions are very common, if you google > for "expression parser" I am sure you will find many examples. Don't limit > yourself to Python code, you can learn from code written in other languages > too, e.g. I have a Pascal programming book that develops a parser for both > polynomials and arithmetic expression.
Building expression evaluators is good fun! And then you can add specialty terms, like dice rolling. > roll 2d6 + d6 "Electric" + 12 "Strength" [ROLL] Rosuav rolls 2d6: 3, 6, totalling 9. [ROLL] Rosuav rolls d6: 1 ("Electric") [ROLL] Rosuav adds a bonus of 12 ("Strength") [ROLL] For 2d6 + d6 "Electric" + 12 "Strength", Rosuav totals: 22 The code behind that follows a fairly similar pattern to what you describe, only it deals with dice rolls (NdM -> roll N dice with M sides each) instead of powers of x. (And it allows comments/tags.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list