Hi everyone,

I thought it'd be fun to write a mathematical expression evaluator, a la Graham 
Cox's GCMathParser, but one that was extensible.  So I dusted off my parsing 
skills and wrote DDMathParser: https://github.com/davedelong/DDMathParser

It's an NSString => NSNumber expression evaluator, and its major feature is 
that you can define custom mathematical functions.  So if you really need to 
have a multiplyBy42() function, then you can quickly write one, register it, 
and use it.

You can use it very simply:

        NSLog(@"%@", [@"1 + 2" numberByEvaluatingString]); //logs "3"

Or very complexly:

        DDMathParser * parser = [DDMathParser mathParserWithString:@"2 ** 3 ** 
2"];
        [parser setPowerAssociativity:DDMathParserAssociativityRight];
        DDExpression * e = [parser parsedExpression];
        NSLog(@"%@", [e evaluateWithSubstitutions:nil evaluator:nil]); //logs 
"512"

It supports pretty much all of the functions defined by NSExpression, plus many 
more (primarily trig functions: sin(), cos(), atanh(), etc).  And like I 
mentioned above, you can create new functions as well.

It supports variables of the same format used by NSPredicate/NSExpression 
("$variable") for substituting in values during evaluation.

It has rudimentary support for simplifying expressions, and I'm currently 
working on support for translating an arbitrary "DDExpression" into an 
NSExpression and still having it work properly (even with custom functions).

The source code is available on my github page, and I'd appreciate any comments 
or feedback you might have.

https://github.com/davedelong/DDMathParser

Cheers,

Dave DeLong
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to