Skip to content

Commit

Permalink
New: API to add your own operators and to add new tokens for existing…
Browse files Browse the repository at this point in the history
… operators
  • Loading branch information
davedelong committed Jan 4, 2014
1 parent 8f4370d commit 78750f5
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 78 deletions.
23 changes: 19 additions & 4 deletions Command line demo/main.m
@@ -1,9 +1,11 @@
#import <Foundation/Foundation.h>
#import "DDMathParser.h"
#import "DDMathStringTokenizer.h"
#import "DDMathOperator.h"

NSString* readLine(void);
void listFunctions(void);
void listOperators(void);

NSString* readLine() {
NSMutableData *data = [NSMutableData data];
Expand All @@ -27,16 +29,28 @@ void listFunctions() {
}
}

void listOperators() {
printf("\nOperators available:\n");
NSArray *knownOperators = [DDMathOperator allOperators];
for (DDMathOperator *op in knownOperators) {
printf("\t%s (%s, %s associative) invokes %s()\n",
[[op.tokens componentsJoinedByString:@", "] UTF8String],
op.arity == DDOperatorArityBinary ? "binary" : (op.arity == DDOperatorArityUnary ? "unary" : "unknown"),
op.associativity == DDOperatorAssociativityLeft ? "left" : "right",
[op.function UTF8String]);
}
}

int main (int argc, const char * argv[]) {
#pragma unused(argc, argv)

@autoreleasepool {

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");
printf("\tType a mathematical expression to evaluate it.\n");
printf("\tType \"list\" to show available functions\n");
printf("\tType \"operators\" to show available operators\n");
printf("\tType \"exit\" to quit\n");

[DDParser setDefaultPowerAssociativity:DDOperatorAssociativityRight];
DDMathEvaluator *evaluator = [[DDMathEvaluator alloc] init];
Expand All @@ -52,6 +66,7 @@ int main (int argc, const char * argv[]) {
line = readLine();
if ([line isEqual:@"exit"]) { break; }
if ([line isEqual:@"list"]) { listFunctions(); continue; }
if ([line isEqual:@"operators"]) { listOperators(); continue; }

NSError *error = nil;

Expand Down
2 changes: 2 additions & 0 deletions DDMathParser.xcodeproj/project.pbxproj
Expand Up @@ -131,6 +131,7 @@
557067B613CC0D19004CB1B2 /* _DDParserTerm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _DDParserTerm.h; sourceTree = "<group>"; };
557067B713CC0D19004CB1B2 /* _DDParserTerm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = _DDParserTerm.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
5570E3B112AA2D8D002FE945 /* DDParserTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDParserTypes.h; sourceTree = "<group>"; };
55890B631875288C0051E3B1 /* DDMathOperator_Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DDMathOperator_Internal.h; sourceTree = "<group>"; };
558A285514A2ED36000FBE0C /* UnitTestMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnitTestMacros.h; sourceTree = "<group>"; };
558A285614A2EEE9000FBE0C /* UnitTestMacros.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UnitTestMacros.m; sourceTree = "<group>"; };
55ABBAA9142ECB1300BDF29A /* _DDRewriteRule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = _DDRewriteRule.h; path = DDMathParser/_DDRewriteRule.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -243,6 +244,7 @@
55E2FCB5129DC90400CD6023 /* DDParser.h */,
55E2FCB6129DC90400CD6023 /* DDParser.m */,
554243551437654800BA293D /* DDMathOperator.h */,
55890B631875288C0051E3B1 /* DDMathOperator_Internal.h */,
554243561437654800BA293D /* DDMathOperator.m */,
5565122613CAB1D7004B7C67 /* Terms */,
);
Expand Down
16 changes: 14 additions & 2 deletions DDMathParser/DDMathOperator.h
Expand Up @@ -14,11 +14,23 @@
@property (nonatomic, readonly, strong) NSString *function;
@property (nonatomic, readonly, strong) NSArray *tokens;
@property (nonatomic, readonly) DDOperatorArity arity;
@property (nonatomic, assign) DDOperatorAssociativity defaultAssociativity;
@property (nonatomic, readonly) NSInteger precedence;
@property (nonatomic, readonly) DDOperatorAssociativity associativity;

+ (NSArray *)allOperators;
+ (instancetype)infoForOperatorFunction:(NSString *)function;
+ (NSArray *)infosForOperatorToken:(NSString *)token;

// the only reason you'd want to init a new Operator is so you can pass it to the +addOperator:... methods
- (id)initWithOperatorFunction:(NSString *)function tokens:(NSArray *)tokens arity:(DDOperatorArity)arity associativity:(DDOperatorAssociativity)associativity;


// modifying operators is not a threadsafe operation
// if you want to do this, you should do it before any evaluation occurs

+ (void)addTokens:(NSArray *)tokens forOperatorFunction:(NSString *)operatorFunction;

+ (void)addOperator:(DDMathOperator *)newOperator withSamePrecedenceAsOperator:(DDMathOperator *)existingOperator;
+ (void)addOperator:(DDMathOperator *)newOperator withLowerPrecedenceThanOperator:(DDMathOperator *)existingOperator;
+ (void)addOperator:(DDMathOperator *)newOperator withHigherPrecedenceThanOperator:(DDMathOperator *)existingOperator;

@end

0 comments on commit 78750f5

Please sign in to comment.