Re: [ANN] DDMathParser
Re: [ANN] DDMathParser
- Subject: Re: [ANN] DDMathParser
- From: Carter Allen <email@hidden>
- Date: Mon, 22 Nov 2010 23:05:20 -0700
Hey Dave,
I'm currently working on an app that loads expressions from plugin-like files, and currently we're having the expressions be written in JavaScript syntax and then using a WebView to evaluate the JavaScript. Obviously, this is less than ideal. I'm wondering if you think that DDMathParser is equal, greater, or lesser in raw mathematical capability (remember, everything has to be done based on an input string) than using JavaScript. If it's lesser, then perhaps there would be some way of having your class detect when it has hit an expression it can't figure out, and at that point pass the expression to WebKit.
Sincerely,
Carter Allen
On Nov 22, 2010, at 9:41 PM, Dave DeLong wrote:
> 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@hidden)
>
> 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:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden