Permalink
Browse files

Added ability to resolve functions lazily

  • Loading branch information...
1 parent bfd75c2 commit b132b37491cfc1ae4fbb34d0c706ec18e434b5e4 @davedelong davedelong committed Feb 23, 2012
Showing with 214 additions and 415 deletions.
  1. +41 −35 Command line demo/main.m
  2. +5 −0 DDMathParser/DDMathEvaluator.h
  3. +8 −1 DDMathParser/DDMathEvaluator.m
  4. +114 −374 Demo/Demo.xib
  5. +46 −5 Demo/DemoController.m
View
@@ -36,47 +36,53 @@ int main (int argc, const char * argv[]) {
#if DD_HAS_ARC
@autoreleasepool {
#else
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
#endif
-
- printf("Math Evaluator!\n");
- printf("\ttype a mathematical expression to evaluate it.\n");
- printf("\nStandard operators available: + - * / %% ! & | ~ ^ << >>\n");
- printf("\nType \"list\" to show available functions\n");
- printf("Type \"exit\" to quit\n");
-
- [DDParser setDefaultPowerAssociativity:DDOperatorAssociativityRight];
-
- NSString * line = nil;
- do {
- printf("> ");
- line = readLine();
- if ([line isEqual:@"exit"]) { break; }
- if ([line isEqual:@"list"]) { listFunctions(); continue; }
-
- NSError *error = nil;
- DDMathStringTokenizer *tokenizer = [[DDMathStringTokenizer alloc] initWithString:line error:&error];
- DDParser *parser = [DDParser parserWithTokenizer:tokenizer error:&error];
- DDExpression *expression = [parser parsedExpressionWithError:&error];
- DDExpression *rewritten = [[DDMathEvaluator sharedMathEvaluator] expressionByRewritingExpression:expression];
+ printf("Math Evaluator!\n");
+ printf("\ttype a mathematical expression to evaluate it.\n");
+ printf("\nStandard operators available: + - * / %% ! & | ~ ^ << >>\n");
+ printf("\nType \"list\" to show available functions\n");
+ printf("Type \"exit\" to quit\n");
- NSNumber *value = [rewritten evaluateWithSubstitutions:nil evaluator:nil error:&error];
- DD_RELEASE(tokenizer);
+ [DDParser setDefaultPowerAssociativity:DDOperatorAssociativityRight];
+ DDMathEvaluator *evaluator = [[DDMathEvaluator alloc] init];
+ [evaluator setFunctionResolver:^DDMathFunction (NSString *name) {
+ return DD_AUTORELEASE([^DDExpression* (NSArray *args, NSDictionary *substitutions, DDMathEvaluator *eval, NSError **error) {
+ return [DDExpression numberExpressionWithNumber:[NSNumber numberWithInt:42]];
+ } copy]);
+ }];
- if (value == nil) {
- printf("\tERROR: %s\n", [[error description] UTF8String]);
- } else {
- if (rewritten != expression) {
- printf("\t%s REWRITTEN AS %s\n", [[expression description] UTF8String], [[rewritten description] UTF8String]);
+ NSString * line = nil;
+ do {
+ printf("> ");
+ line = readLine();
+ if ([line isEqual:@"exit"]) { break; }
+ if ([line isEqual:@"list"]) { listFunctions(); continue; }
+
+ NSError *error = nil;
+
+ DDMathStringTokenizer *tokenizer = [[DDMathStringTokenizer alloc] initWithString:line error:&error];
+ DDParser *parser = [DDParser parserWithTokenizer:tokenizer error:&error];
+
+ DDExpression *expression = [parser parsedExpressionWithError:&error];
+ DDExpression *rewritten = [evaluator expressionByRewritingExpression:expression];
+
+ NSNumber *value = [rewritten evaluateWithSubstitutions:nil evaluator:evaluator error:&error];
+ DD_RELEASE(tokenizer);
+
+ if (value == nil) {
+ printf("\tERROR: %s\n", [[error description] UTF8String]);
+ } else {
+ if (rewritten != expression) {
+ printf("\t%s REWRITTEN AS %s\n", [[expression description] UTF8String], [[rewritten description] UTF8String]);
+ }
+ printf("\t%s = %s\n", [[rewritten description] UTF8String], [[value description] UTF8String]);
}
- printf("\t%s = %s\n", [[rewritten description] UTF8String], [[value description] UTF8String]);
- }
+
+
+ } while (1);
-
- } while (1);
-
- // insert code here...
printf("Goodbye!\n");
#if DD_HAS_ARC
@@ -12,12 +12,17 @@
@class DDMathEvaluator;
@class DDExpression;
+typedef DDMathFunction (^DDFunctionResolver)(NSString *);
+
@interface DDMathEvaluator : NSObject {
NSMutableArray *functions;
NSMutableDictionary * functionMap;
NSMutableArray *rewriteRules;
+ DDFunctionResolver functionResolver;
}
+@property (nonatomic, copy) DDFunctionResolver functionResolver;
+
+ (id) sharedMathEvaluator;
- (BOOL) registerFunction:(DDMathFunction)function forName:(NSString *)functionName;
@@ -30,6 +30,8 @@ - (_DDFunctionContainer *)functionContainerWithName:(NSString *)functionName;
@implementation DDMathEvaluator
+@synthesize functionResolver=functionResolver;
+
static DDMathEvaluator * _sharedEvaluator = nil;
+ (id) sharedMathEvaluator {
@@ -59,6 +61,7 @@ - (void) dealloc {
[functions release];
[functionMap release];
[rewriteRules release];
+ [functionResolver release];
[super dealloc];
#endif
}
@@ -99,7 +102,11 @@ - (_DDFunctionContainer *)functionContainerWithName:(NSString *)functionName {
- (DDMathFunction) functionWithName:(NSString *)functionName {
_DDFunctionContainer *container = [self functionContainerWithName:functionName];
- return [container function];
+ DDMathFunction function = [container function];
+ if (function == nil && functionResolver != nil) {
+ function = functionResolver(functionName);
+ }
+ return function;
}
- (NSArray *) registeredFunctions {
Oops, something went wrong.

0 comments on commit b132b37

Please sign in to comment.