
AMDG On 04/06/2011 03:12 PM, Littlefield, Tyler wrote:
Sorry for all the questions; I am kind of easing into boost and finding cool things i can do with it, as well as uses for a couple of projects. Eventually I would like to write a small scripting language using boost::spirit, just to do it and get the practice using it. As a starting point, I wrote a quick calculator (that just does small single operations like x+x, x*x, etc). Now i have two questions. 1) My rule seems to be off. the first number comes through clearly, but no matter what I put in, I always get the first number and then a 42. 2) How would I go about extending this to implement order of operations, parenthases, and longer operations (like 3+3*3)? I'm going to try to implement a small function table so I can do tan(3), so I assume I will need nultiple rules, how might that be accomplished? Any comments and suggestions would be welcome. I have pasted the code below. I was working from a tutorial, so early appologies if it feels messy; I plan to clean up the namespace aliases and the using keywords.
Spirit used to have a calculator example, but I can't seem to find it in the new docs. Basically you have two choices. You can add semantic actions to evaluate each subexpression as it's parsed, or you can let spirit generate an AST and write a recursive function that walks the tree and evaluates it. In Christ, Steven Watanabe