Mathematically evaluating NSStrings

I’ve recently posted the code to a project I’ve written called DDMathParser. It’s a way whereby you can take an NSString and evaluate it as a mathematical expression. For example, you can do:

NSLog(@"%@", [@"1 + 3 * 4 - 5" numberByEvaluatingString]); //logs "8"

There are several other ways to evaluate strings, including methods that allow you to use variables (1 + $a, and then substitute $a for a value later). You can even define custom functions, like mySuperSecretFunction(42).

This was a really fun project, because there were some really interesting challenges to work around (like how can I parse a left associative expression using a recursive descent parser without getting caught in an infinite recursion?). I’ll save those for another post.

For now, go check out the source on github and let me know what you think!

http://github.com/davedelong/DDMathParser

  1. funwithobjc posted this
Short URL for this post: http://tmblr.co/Zt522y1YiaxW
blog comments powered by Disqus