Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A bit of refactoring of _DDOperatorInfo

Since all operators of the same type invoke the same function, we now just use the function to identify the operator, as opposed to an enum value.
  • Loading branch information...
commit 4ce5ae4c149479ccea3ea58affeac17c05eb7c8e 1 parent 36b7b0e
@davedelong authored
View
4 DDMathParser.xcodeproj/project.pbxproj
@@ -26,6 +26,7 @@
550C416A13BA8BCA00A041BC /* DDMathStringTokenizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 550C416713BA8BCA00A041BC /* DDMathStringTokenizer.m */; };
5514EF161347EBA10010B9D8 /* DDTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 5526E9E01310EF880018B4C9 /* DDTypes.m */; };
5514EF171347EBA20010B9D8 /* DDTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 5526E9E01310EF880018B4C9 /* DDTypes.m */; };
+ 551C8C4A144E5CFC00520BDA /* DDParserTypes.m in Sources */ = {isa = PBXBuildFile; fileRef = 551C8C49144E5CFC00520BDA /* DDParserTypes.m */; };
551EA4B813CD657400302C67 /* _DDOperatorTerm.m in Sources */ = {isa = PBXBuildFile; fileRef = 551EA4B713CD657400302C67 /* _DDOperatorTerm.m */; };
551EA4C513CF415100302C67 /* _DDFunctionContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 551EA4C413CF415100302C67 /* _DDFunctionContainer.m */; };
552532B712C1C92D00A942A7 /* _DDFunctionUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 552532B612C1C92D00A942A7 /* _DDFunctionUtilities.m */; };
@@ -99,6 +100,7 @@
550C416613BA8BCA00A041BC /* DDMathStringTokenizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DDMathStringTokenizer.h; sourceTree = "<group>"; };
550C416713BA8BCA00A041BC /* DDMathStringTokenizer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = DDMathStringTokenizer.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
5514EE291347CE9B0010B9D8 /* DDMathParserMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DDMathParserMacros.h; path = DDMathParser/DDMathParserMacros.h; sourceTree = "<group>"; };
+ 551C8C49144E5CFC00520BDA /* DDParserTypes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DDParserTypes.m; sourceTree = "<group>"; };
551EA4B613CD657300302C67 /* _DDOperatorTerm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _DDOperatorTerm.h; sourceTree = "<group>"; };
551EA4B713CD657400302C67 /* _DDOperatorTerm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _DDOperatorTerm.m; sourceTree = "<group>"; };
551EA4C313CF415100302C67 /* _DDFunctionContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _DDFunctionContainer.h; sourceTree = "<group>"; };
@@ -314,6 +316,7 @@
isa = PBXGroup;
children = (
5570E3B112AA2D8D002FE945 /* DDParserTypes.h */,
+ 551C8C49144E5CFC00520BDA /* DDParserTypes.m */,
5570E4E412AABEF3002FE945 /* Tokens */,
55325A0312A8C34400DE9EB0 /* Precedence Parser */,
55BA679E1299FA19002279A5 /* NSString+DDMathParsing.h */,
@@ -560,6 +563,7 @@
551EA4C513CF415100302C67 /* _DDFunctionContainer.m in Sources */,
55ABBAAB142ECB1300BDF29A /* _DDRewriteRule.m in Sources */,
554243571437654800BA293D /* _DDOperatorInfo.m in Sources */,
+ 551C8C4A144E5CFC00520BDA /* DDParserTypes.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
6 DDMathParser/DDMathStringToken.h
@@ -23,7 +23,7 @@ typedef enum {
NSNumber *numberValue;
DDTokenType tokenType;
_DDOperatorInfo *operatorInfo;
- DDOperator operatorType;
+ NSString *operatorType;
BOOL ambiguous;
}
@@ -31,7 +31,7 @@ typedef enum {
@property (nonatomic,readonly) NSString * token;
@property (nonatomic,readonly) DDTokenType tokenType;
-@property (nonatomic,readonly) DDOperator operatorType;
+@property (nonatomic,readonly) NSString *operatorType;
@property (nonatomic,readonly) DDOperatorArity operatorArity;
@property (nonatomic,readonly) DDOperatorAssociativity operatorAssociativity;
@property (nonatomic,readonly) NSInteger operatorPrecedence;
@@ -39,6 +39,6 @@ typedef enum {
- (NSNumber *) numberValue;
-- (void)resolveToOperator:(DDOperator)operator;
+- (void)resolveToOperator:(NSString *)operator;
@end
View
8 DDMathParser/DDMathStringToken.m
@@ -72,9 +72,9 @@ - (NSString *)token {
return token;
}
-- (DDOperator)operatorType {
+- (NSString *)operatorType {
if (ambiguous) { return DDOperatorInvalid; }
- return [operatorInfo operator];
+ return [operatorInfo function];
}
- (NSInteger)operatorPrecedence {
@@ -97,11 +97,11 @@ - (DDOperatorAssociativity)operatorAssociativity {
return [operatorInfo defaultAssociativity];
}
-- (void)resolveToOperator:(DDOperator)operator {
+- (void)resolveToOperator:(NSString *)operator {
DD_RELEASE(operatorInfo);
operatorInfo = nil;
- NSArray *matching = [_DDOperatorInfo infosForOperator:operator];
+ NSArray *matching = [_DDOperatorInfo infosForOperatorFunction:operator];
if ([matching count] > 0) {
ambiguous = NO;
operatorInfo = DD_RETAIN([matching objectAtIndex:0]);
View
2  DDMathParser/DDMathStringTokenizer.m
@@ -157,7 +157,7 @@ - (BOOL)_processToken:(DDMathStringToken *)token withError:(NSError **)error {
- (BOOL)_processUnknownOperatorToken:(DDMathStringToken *)token withError:(NSError **)error {
DDMathStringToken *previousToken = [_tokens lastObject];
if ([token tokenType] == DDTokenTypeOperator && [token operatorType] == DDOperatorInvalid) {
- DDOperator resolvedOperator = DDOperatorInvalid;
+ NSString *resolvedOperator = DDOperatorInvalid;
BOOL shouldBeUnary = NO;
View
2  DDMathParser/DDParser.h
@@ -71,6 +71,6 @@
- (id) initWithString:(NSString *)string error:(NSError **)error;
- (DDExpression *) parsedExpressionWithError:(NSError **)error;
-- (DDOperatorAssociativity) associativityForOperator:(DDOperator)operatorType;
+- (DDOperatorAssociativity) associativityForOperator:(NSString *)operator;
@end
View
124 DDMathParser/DDParser.m
@@ -17,6 +17,19 @@
#import "DDExpression.h"
#import "_DDOperatorInfo.h"
+static inline void DDOperatorSetAssociativity(NSString *o, DDOperatorAssociativity a) {
+ NSArray *ops = [_DDOperatorInfo infosForOperatorFunction:o];
+ for (_DDOperatorInfo *info in ops) {
+ [info setDefaultAssociativity:a];
+ }
+}
+
+static inline DDOperatorAssociativity DDOperatorGetAssociativity(NSString *o) {
+ NSArray *ops = [_DDOperatorInfo infosForOperatorFunction:o];
+ _DDOperatorInfo *info = [ops objectAtIndex:0];
+ return [info defaultAssociativity];
+}
+
@implementation DDParser
@synthesize bitwiseOrAssociativity;
@@ -43,51 +56,38 @@ + (void) initialize {
[super initialize];
}
-#define GET_ASSOCIATIVITY(_op) { \
-NSArray *ops = [_DDOperatorInfo infosForOperator:(_op)]; \
-_DDOperatorInfo *info = [ops objectAtIndex:0]; \
-return [info defaultAssociativity]; \
-}
-
-#define SET_ASSOCIATIVITY(_op,_a) { \
-NSArray *ops = [_DDOperatorInfo infosForOperator:(_op)]; \
-for (_DDOperatorInfo *info in ops) { \
-[info setDefaultAssociativity:(_a)]; \
-} \
-}
++ (DDOperatorAssociativity) defaultBitwiseOrAssociativity { return DDOperatorGetAssociativity(DDOperatorBitwiseOr); }
++ (void) setDefaultBitwiseOrAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorBitwiseOr, newAssociativity); }
-+ (DDOperatorAssociativity) defaultBitwiseOrAssociativity { GET_ASSOCIATIVITY(DDOperatorBitwiseOr); }
-+ (void) setDefaultBitwiseOrAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorBitwiseOr, newAssociativity); }
++ (DDOperatorAssociativity) defaultBitwiseXorAssociativity { return DDOperatorGetAssociativity(DDOperatorBitwiseXor); }
++ (void) setDefaultBitwiseXorAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorBitwiseXor, newAssociativity); }
-+ (DDOperatorAssociativity) defaultBitwiseXorAssociativity { GET_ASSOCIATIVITY(DDOperatorBitwiseXor); }
-+ (void) setDefaultBitwiseXorAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorBitwiseXor, newAssociativity); }
++ (DDOperatorAssociativity) defaultBitwiseAndAssociativity { return DDOperatorGetAssociativity(DDOperatorBitwiseAnd); }
++ (void) setDefaultBitwiseAndAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorBitwiseAnd, newAssociativity); }
-+ (DDOperatorAssociativity) defaultBitwiseAndAssociativity { GET_ASSOCIATIVITY(DDOperatorBitwiseAnd); }
-+ (void) setDefaultBitwiseAndAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorBitwiseAnd, newAssociativity); }
++ (DDOperatorAssociativity) defaultBitwiseLeftShiftAssociativity { return DDOperatorGetAssociativity(DDOperatorLeftShift); }
++ (void) setDefaultBitwiseLeftShiftAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorLeftShift, newAssociativity); }
-+ (DDOperatorAssociativity) defaultBitwiseLeftShiftAssociativity { GET_ASSOCIATIVITY(DDOperatorLeftShift); }
-+ (void) setDefaultBitwiseLeftShiftAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorLeftShift, newAssociativity); }
++ (DDOperatorAssociativity) defaultBitwiseRightShiftAssociativity { return DDOperatorGetAssociativity(DDOperatorRightShift); }
++ (void) setDefaultBitwiseRightShiftAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorRightShift, newAssociativity); }
-+ (DDOperatorAssociativity) defaultBitwiseRightShiftAssociativity { GET_ASSOCIATIVITY(DDOperatorRightShift); }
-+ (void) setDefaultBitwiseRightShiftAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorRightShift, newAssociativity); }
-
-+ (DDOperatorAssociativity) defaultAdditionAssociativity { GET_ASSOCIATIVITY(DDOperatorAdd); }
++ (DDOperatorAssociativity) defaultAdditionAssociativity { return DDOperatorGetAssociativity(DDOperatorAdd); }
+ (void) setDefaultAdditionAssociativity:(DDOperatorAssociativity)newAssociativity {
- SET_ASSOCIATIVITY(DDOperatorAdd, newAssociativity);
- SET_ASSOCIATIVITY(DDOperatorMinus, newAssociativity);
+ DDOperatorSetAssociativity(DDOperatorAdd, newAssociativity);
+ DDOperatorSetAssociativity(DDOperatorMinus, newAssociativity);
}
-+ (DDOperatorAssociativity) defaultMultiplicationAssociativity { GET_ASSOCIATIVITY(DDOperatorMultiply); }
++ (DDOperatorAssociativity) defaultMultiplicationAssociativity { return DDOperatorGetAssociativity(DDOperatorMultiply); }
+ (void) setDefaultMultiplicationAssociativity:(DDOperatorAssociativity)newAssociativity {
- SET_ASSOCIATIVITY(DDOperatorMultiply, newAssociativity);
- SET_ASSOCIATIVITY(DDOperatorDivide, newAssociativity);
+ DDOperatorSetAssociativity(DDOperatorMultiply, newAssociativity);
+ DDOperatorSetAssociativity(DDOperatorDivide, newAssociativity);
}
-+ (DDOperatorAssociativity) defaultModAssociativity { GET_ASSOCIATIVITY(DDOperatorModulo); }
-+ (void) setDefaultModAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorModulo, newAssociativity); }
++ (DDOperatorAssociativity) defaultModAssociativity { return DDOperatorGetAssociativity(DDOperatorModulo); }
++ (void) setDefaultModAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorModulo, newAssociativity); }
-+ (DDOperatorAssociativity) defaultPowerAssociativity { GET_ASSOCIATIVITY(DDOperatorPower); }
-+ (void) setDefaultPowerAssociativity:(DDOperatorAssociativity)newAssociativity { SET_ASSOCIATIVITY(DDOperatorPower, newAssociativity); }
++ (DDOperatorAssociativity) defaultPowerAssociativity { return DDOperatorGetAssociativity(DDOperatorPower); }
++ (void) setDefaultPowerAssociativity:(DDOperatorAssociativity)newAssociativity { DDOperatorSetAssociativity(DDOperatorPower, newAssociativity); }
+ (id) parserWithString:(NSString *)string error:(NSError **)error {
@@ -133,34 +133,36 @@ - (void) dealloc {
}
#endif
-- (DDOperatorAssociativity) associativityForOperator:(DDOperator)operatorType {
- switch (operatorType) {
- // binary operators can have customizable associativity
- case DDOperatorBitwiseOr: return bitwiseOrAssociativity;
- case DDOperatorBitwiseXor: return bitwiseXorAssociativity;
- case DDOperatorBitwiseAnd: return bitwiseAndAssociativity;
- case DDOperatorLeftShift: return bitwiseLeftShiftAssociativity;
- case DDOperatorRightShift: return bitwiseRightShiftAssociativity;
- case DDOperatorMinus:
- case DDOperatorAdd: return additionAssociativity;
- case DDOperatorDivide:
- case DDOperatorMultiply: return multiplicationAssociativity;
- case DDOperatorModulo: return modAssociativity;
- case DDOperatorPower: return powerAssociativity;
-
- // unary operators are always right associative (except for factorial)
- case DDOperatorUnaryPlus:
- case DDOperatorUnaryMinus:
- case DDOperatorBitwiseNot: return DDOperatorAssociativityRight;
-
- // factorial is always left associative
- case DDOperatorFactorial: return DDOperatorAssociativityLeft;
- // logical not is always right associative
- case DDOperatorLogicalNot: return DDOperatorAssociativityRight;
-
- default: return DDOperatorAssociativityLeft;
- }
- return DDOperatorAssociativityLeft;
+- (DDOperatorAssociativity) associativityForOperator:(NSString *)operatorType {
+ if (operatorType == DDOperatorBitwiseOr) {
+ return bitwiseOrAssociativity;
+ }
+ if (operatorType == DDOperatorBitwiseXor) {
+ return bitwiseXorAssociativity;
+ }
+ if (operatorType == DDOperatorBitwiseAnd) {
+ return bitwiseAndAssociativity;
+ }
+ if (operatorType == DDOperatorLeftShift) {
+ return bitwiseLeftShiftAssociativity;
+ }
+ if (operatorType == DDOperatorRightShift) {
+ return bitwiseRightShiftAssociativity;
+ }
+ if (operatorType == DDOperatorMinus || operatorType == DDOperatorAdd) {
+ return additionAssociativity;
+ }
+ if (operatorType == DDOperatorDivide || operatorType == DDOperatorMultiply) {
+ return multiplicationAssociativity;
+ }
+ if (operatorType == DDOperatorModulo) {
+ return modAssociativity;
+ }
+ if (operatorType == DDOperatorPower) {
+ return powerAssociativity;
+ }
+
+ return DDOperatorGetAssociativity(operatorType);
}
- (DDExpression *) parsedExpressionWithError:(NSError **)error {
View
64 DDMathParser/DDParserTypes.h
@@ -14,43 +14,37 @@ typedef enum {
} DDOperatorAssociativity;
typedef enum {
- DDOperatorInvalid = 0,
-
- DDOperatorLogicalOr,
- DDOperatorLogicalAnd,
- DDOperatorLogicalNot,
-
- DDOperatorLogicalEqual,
- DDOperatorLogicalNotEqual,
- DDOperatorLogicalLessThan,
- DDOperatorLogicalGreaterThan,
- DDOperatorLogicalLessThanOrEqual,
- DDOperatorLogicalGreaterThanOrEqual,
-
- DDOperatorBitwiseOr,
- DDOperatorBitwiseXor,
- DDOperatorBitwiseAnd,
- DDOperatorLeftShift,
- DDOperatorRightShift,
- DDOperatorMinus,
- DDOperatorAdd,
- DDOperatorDivide,
- DDOperatorMultiply,
- DDOperatorModulo,
- DDOperatorBitwiseNot,
- DDOperatorFactorial,
- DDOperatorPower,
- DDOperatorParenthesisOpen,
- DDOperatorParenthesisClose,
-
- DDOperatorComma,
- DDOperatorUnaryMinus,
- DDOperatorUnaryPlus
-} DDOperator;
-
-typedef enum {
DDOperatorArityUnknown = 0,
DDOperatorArityUnary,
DDOperatorArityBinary
} DDOperatorArity;
+
+extern NSString *const DDOperatorInvalid;
+extern NSString *const DDOperatorLogicalOr;
+extern NSString *const DDOperatorLogicalAnd;
+extern NSString *const DDOperatorLogicalNot;
+extern NSString *const DDOperatorLogicalEqual;
+extern NSString *const DDOperatorLogicalNotEqual;
+extern NSString *const DDOperatorLogicalLessThan;
+extern NSString *const DDOperatorLogicalGreaterThan;
+extern NSString *const DDOperatorLogicalLessThanOrEqual;
+extern NSString *const DDOperatorLogicalGreaterThanOrEqual;
+extern NSString *const DDOperatorBitwiseOr;
+extern NSString *const DDOperatorBitwiseXor;
+extern NSString *const DDOperatorBitwiseAnd;
+extern NSString *const DDOperatorLeftShift;
+extern NSString *const DDOperatorRightShift;
+extern NSString *const DDOperatorMinus;
+extern NSString *const DDOperatorAdd;
+extern NSString *const DDOperatorDivide;
+extern NSString *const DDOperatorMultiply;
+extern NSString *const DDOperatorModulo;
+extern NSString *const DDOperatorBitwiseNot;
+extern NSString *const DDOperatorFactorial;
+extern NSString *const DDOperatorPower;
+extern NSString *const DDOperatorParenthesisOpen;
+extern NSString *const DDOperatorParenthesisClose;
+extern NSString *const DDOperatorComma;
+extern NSString *const DDOperatorUnaryMinus;
+extern NSString *const DDOperatorUnaryPlus;
View
39 DDMathParser/DDParserTypes.m
@@ -0,0 +1,39 @@
+//
+// DDParserTypes.m
+// DDMathParser
+//
+// Created by Dave DeLong on 10/18/11.
+// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "DDParserTypes.h"
+
+NSString *const DDOperatorInvalid = @"invalid";
+NSString *const DDOperatorLogicalOr = @"l_or";
+NSString *const DDOperatorLogicalAnd = @"l_and";
+NSString *const DDOperatorLogicalNot = @"l_not";
+NSString *const DDOperatorLogicalEqual = @"l_eq";
+NSString *const DDOperatorLogicalNotEqual = @"l_neq";
+NSString *const DDOperatorLogicalLessThan = @"l_lt";
+NSString *const DDOperatorLogicalGreaterThan = @"l_gt";
+NSString *const DDOperatorLogicalLessThanOrEqual = @"l_ltoe";
+NSString *const DDOperatorLogicalGreaterThanOrEqual = @"l_gtoe";
+NSString *const DDOperatorBitwiseOr = @"or";
+NSString *const DDOperatorBitwiseXor = @"xor";
+NSString *const DDOperatorBitwiseAnd = @"and";
+NSString *const DDOperatorLeftShift = @"lshift";
+NSString *const DDOperatorRightShift = @"rshift";
+NSString *const DDOperatorMinus = @"subtract";
+NSString *const DDOperatorAdd = @"add";
+NSString *const DDOperatorDivide = @"divide";
+NSString *const DDOperatorMultiply = @"multiply";
+NSString *const DDOperatorModulo = @"modulo";
+NSString *const DDOperatorBitwiseNot = @"not";
+NSString *const DDOperatorFactorial = @"factorial";
+NSString *const DDOperatorPower = @"pow";
+NSString *const DDOperatorParenthesisOpen = @"open_paren";
+NSString *const DDOperatorParenthesisClose = @"close_paren";
+NSString *const DDOperatorComma = @"comma";
+NSString *const DDOperatorUnaryMinus = @"negate";
+NSString *const DDOperatorUnaryPlus = @"positive";
+
View
8 DDMathParser/_DDOperatorInfo.h
@@ -10,7 +10,6 @@
#import "DDMathParser.h"
@interface _DDOperatorInfo : NSObject {
- DDOperator _operator;
DDOperatorArity _arity;
DDOperatorAssociativity _defaultAssociativity;
NSInteger _precedence;
@@ -18,15 +17,14 @@
NSString *_function;
}
-@property (nonatomic, readonly) DDOperator operator;
+@property (nonatomic, readonly, DD_STRONG) NSString *function;
+@property (nonatomic, readonly, DD_STRONG) NSString *token;
@property (nonatomic, readonly) DDOperatorArity arity;
@property (nonatomic, assign) DDOperatorAssociativity defaultAssociativity;
@property (nonatomic, readonly) NSInteger precedence;
-@property (nonatomic, readonly, DD_STRONG) NSString *token;
-@property (nonatomic, readonly, DD_STRONG) NSString *function;
+ (NSArray *)allOperators;
-+ (NSArray *)infosForOperator:(DDOperator)operator;
++ (NSArray *)infosForOperatorFunction:(NSString *)operator;
+ (NSArray *)infosForOperatorToken:(NSString *)token;
@end
View
91 DDMathParser/_DDOperatorInfo.m
@@ -10,31 +10,29 @@
@implementation _DDOperatorInfo
-@synthesize operator=_operator;
@synthesize arity=_arity;
@synthesize defaultAssociativity=_defaultAssociativity;
@synthesize precedence=_precedence;
@synthesize token=_token;
@synthesize function=_function;
-- (id)initWithOperator:(DDOperator)operator arity:(DDOperatorArity)arity precedence:(NSInteger)precedence token:(NSString *)token function:(NSString *)function associativity:(DDOperatorAssociativity)associativity {
+- (id)initWithOperatorFunction:(NSString *)function token:(NSString *)token arity:(DDOperatorArity)arity precedence:(NSInteger)precedence associativity:(DDOperatorAssociativity)associativity {
self = [super init];
if (self) {
- _operator = operator;
_arity = arity;
_defaultAssociativity = associativity;
_precedence = precedence;
_token = DD_RETAIN(token);
- _function = DD_RETAIN(function);
+ _function = DD_RETAIN(function);
}
return self;
}
-+ (id)infoForOperator:(DDOperator)operator arity:(DDOperatorArity)arity precedence:(NSInteger)precedence token:(NSString *)token function:(NSString *)function associativity:(DDOperatorAssociativity)associativity {
- return DD_AUTORELEASE([[self alloc] initWithOperator:operator arity:arity precedence:precedence token:token function:function associativity:associativity]);
++ (id)infoForOperatorFunction:(NSString *)function token:(NSString *)token arity:(DDOperatorArity)arity precedence:(NSInteger)precedence associativity:(DDOperatorAssociativity)associativity {
+ return DD_AUTORELEASE([[self alloc] initWithOperatorFunction:function token:token arity:arity precedence:precedence associativity:associativity]);
}
-+ (NSArray *)infosForOperator:(DDOperator)operator {
++ (NSArray *)infosForOperatorFunction:(NSString *)operator {
static dispatch_once_t onceToken;
static NSMutableDictionary *_operatorLookup = nil;
dispatch_once(&onceToken, ^{
@@ -42,8 +40,7 @@ + (NSArray *)infosForOperator:(DDOperator)operator {
NSArray *operators = [self allOperators];
for (_DDOperatorInfo *info in operators) {
- DDOperator op = [info operator];
- NSNumber *key = [NSNumber numberWithInt:op];
+ NSString *key = [info function];
NSMutableArray *value = [_operatorLookup objectForKey:key];
if (value == nil) {
@@ -53,7 +50,7 @@ + (NSArray *)infosForOperator:(DDOperator)operator {
[value addObject:info];
}
});
- return [_operatorLookup objectForKey:[NSNumber numberWithInt:operator]];
+ return [_operatorLookup objectForKey:operator];
}
+ (NSArray *)infosForOperatorToken:(NSString *)token {
@@ -88,97 +85,97 @@ + (NSArray *)_buildOperators {
NSMutableArray *operators = [NSMutableArray array];
NSInteger precedence = 0;
- [operators addObject:[self infoForOperator:DDOperatorLogicalOr arity:DDOperatorArityBinary precedence:precedence token:@"||" function:@"l_or" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalOr token:@"||" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
// \u2228 is ∨
- [operators addObject:[self infoForOperator:DDOperatorLogicalOr arity:DDOperatorArityBinary precedence:precedence token:@"\u2228" function:@"l_or" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalOr token:@"\u2228" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"&&" function:@"l_and" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalAnd token:@"&&" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
// \u2227 is ∧
- [operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"\u2227" function:@"l_and" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalAnd token:@"\u2227" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
// == and != have the same precedence
- [operators addObject:[self infoForOperator:DDOperatorLogicalEqual arity:DDOperatorArityBinary precedence:precedence token:@"==" function:@"l_eq" associativity:DDOperatorAssociativityLeft]];
- [operators addObject:[self infoForOperator:DDOperatorLogicalEqual arity:DDOperatorArityBinary precedence:precedence token:@"=" function:@"l_eq" associativity:DDOperatorAssociativityLeft]];
- [operators addObject:[self infoForOperator:DDOperatorLogicalNotEqual arity:DDOperatorArityBinary precedence:precedence token:@"!=" function:@"l_neq" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalEqual token:@"==" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalEqual token:@"=" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalNotEqual token:@"!=" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLogicalLessThan arity:DDOperatorArityBinary precedence:precedence token:@"<" function:@"l_lt" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalLessThan token:@"<" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThan arity:DDOperatorArityBinary precedence:precedence token:@">" function:@"l_gt" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalGreaterThan token:@">" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLogicalLessThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"<=" function:@"l_ltoe" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalLessThanOrEqual token:@"<=" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
// \u2264 is ≤
- [operators addObject:[self infoForOperator:DDOperatorLogicalLessThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"\u2264" function:@"l_ltoe" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalLessThanOrEqual token:@"\u2264" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@">=" function:@"l_gtoe" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalGreaterThanOrEqual token:@">=" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
// \u2265 is ≥
- [operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"\u2265" function:@"l_gtoe" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalGreaterThanOrEqual token:@"\u2265" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"!" function:@"l_not" associativity:DDOperatorAssociativityRight]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalNot token:@"!" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityRight]];
// \u00AC is ¬
- [operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"\u00ac" function:@"l_not" associativity:DDOperatorAssociativityRight]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLogicalNot token:@"\u00ac" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityRight]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorBitwiseOr arity:DDOperatorArityBinary precedence:precedence token:@"|" function:@"or" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorBitwiseOr token:@"|" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorBitwiseXor arity:DDOperatorArityBinary precedence:precedence token:@"^" function:@"xor" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorBitwiseXor token:@"^" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorBitwiseAnd arity:DDOperatorArityBinary precedence:precedence token:@"&" function:@"and" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorBitwiseAnd token:@"&" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorLeftShift arity:DDOperatorArityBinary precedence:precedence token:@"<<" function:@"lshift" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorLeftShift token:@"<<" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorRightShift arity:DDOperatorArityBinary precedence:precedence token:@">>" function:@"rshift" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorRightShift token:@">>" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
-
+
// addition and subtraction have the same precedence
- [operators addObject:[self infoForOperator:DDOperatorAdd arity:DDOperatorArityBinary precedence:precedence token:@"+" function:@"add" associativity:DDOperatorAssociativityLeft]];
- [operators addObject:[self infoForOperator:DDOperatorMinus arity:DDOperatorArityBinary precedence:precedence token:@"-" function:@"subtract" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorAdd token:@"+" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorMinus token:@"-" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
-
+
// multiplication and division have the same precedence
- [operators addObject:[self infoForOperator:DDOperatorMultiply arity:DDOperatorArityBinary precedence:precedence token:@"*" function:@"multiply" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorMultiply token:@"*" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
// \u00d7 is ×
- [operators addObject:[self infoForOperator:DDOperatorMultiply arity:DDOperatorArityBinary precedence:precedence token:@"\u00d7" function:@"multiply" associativity:DDOperatorAssociativityLeft]];
- [operators addObject:[self infoForOperator:DDOperatorDivide arity:DDOperatorArityBinary precedence:precedence token:@"/" function:@"divide" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorMultiply token:@"\u00d7" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorDivide token:@"/" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
// \u00f7 is ÷
- [operators addObject:[self infoForOperator:DDOperatorDivide arity:DDOperatorArityBinary precedence:precedence token:@"\u00f7" function:@"divide" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorDivide token:@"\u00f7" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorModulo arity:DDOperatorArityBinary precedence:precedence token:@"%" function:@"mod" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorModulo token:@"%" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorBitwiseNot arity:DDOperatorArityUnary precedence:precedence token:@"~" function:@"not" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorBitwiseNot token:@"~" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
// right associative unary operators have the same precedence
- [operators addObject:[self infoForOperator:DDOperatorUnaryMinus arity:DDOperatorArityUnary precedence:precedence token:@"-" function:@"negate" associativity:DDOperatorAssociativityRight]];
- [operators addObject:[self infoForOperator:DDOperatorUnaryPlus arity:DDOperatorArityUnary precedence:precedence token:@"+" function:@"" associativity:DDOperatorAssociativityRight]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorUnaryMinus token:@"-" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityRight]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorUnaryPlus token:@"+" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityRight]];
precedence++;
// there's only one left associative unary operator
- [operators addObject:[self infoForOperator:DDOperatorFactorial arity:DDOperatorArityUnary precedence:precedence token:@"!" function:@"factorial" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorFactorial token:@"!" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorPower arity:DDOperatorArityBinary precedence:precedence token:@"**" function:@"pow" associativity:DDOperatorAssociativityRight]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorPower token:@"**" arity:DDOperatorArityBinary precedence:precedence associativity:DDOperatorAssociativityRight]];
precedence++;
// ( and ) have the same precedence
// these are defined as unary right/left associative for convenience
- [operators addObject:[self infoForOperator:DDOperatorParenthesisOpen arity:DDOperatorArityUnary precedence:precedence token:@"(" function:@"" associativity:DDOperatorAssociativityRight]];
- [operators addObject:[self infoForOperator:DDOperatorParenthesisClose arity:DDOperatorArityUnary precedence:precedence token:@")" function:@"" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorParenthesisOpen token:@"(" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityRight]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorParenthesisClose token:@")" arity:DDOperatorArityUnary precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
- [operators addObject:[self infoForOperator:DDOperatorComma arity:DDOperatorArityUnknown precedence:precedence token:@"," function:@"" associativity:DDOperatorAssociativityLeft]];
+ [operators addObject:[self infoForOperatorFunction:DDOperatorComma token:@"," arity:DDOperatorArityUnknown precedence:precedence associativity:DDOperatorAssociativityLeft]];
precedence++;
return operators;
View
2  DDMathParser/_DDOperatorTerm.h
@@ -11,7 +11,7 @@
@interface _DDOperatorTerm : _DDParserTerm
-@property (nonatomic,readonly) DDOperator operatorType;
+@property (nonatomic,readonly) NSString *operatorType;
@property (nonatomic,readonly) NSInteger operatorPrecedence;
@property (nonatomic,readonly) DDOperatorArity operatorArity;
@property (nonatomic,readonly) NSString *operatorFunction;
View
2  DDMathParser/_DDOperatorTerm.m
@@ -13,7 +13,7 @@ @implementation _DDOperatorTerm
- (DDParserTermType)type { return DDParserTermTypeOperator; }
-- (DDOperator)operatorType {
+- (NSString *)operatorType {
return [[self token] operatorType];
}
Please sign in to comment.
Something went wrong with that request. Please try again.