Permalink
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...
1 parent 36b7b0e commit 4ce5ae4c149479ccea3ea58affeac17c05eb7c8e @davedelong committed Oct 20, 2011
@@ -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;
};
@@ -23,22 +23,22 @@ typedef enum {
NSNumber *numberValue;
DDTokenType tokenType;
_DDOperatorInfo *operatorInfo;
- DDOperator operatorType;
+ NSString *operatorType;
BOOL ambiguous;
}
+ (id) mathStringTokenWithToken:(NSString *)t type:(DDTokenType)type;
@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;
@property (nonatomic,readonly) NSString *operatorFunction;
- (NSNumber *) numberValue;
-- (void)resolveToOperator:(DDOperator)operator;
+- (void)resolveToOperator:(NSString *)operator;
@end
@@ -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]);
@@ -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;
@@ -71,6 +71,6 @@
- (id) initWithString:(NSString *)string error:(NSError **)error;
- (DDExpression *) parsedExpressionWithError:(NSError **)error;
-- (DDOperatorAssociativity) associativityForOperator:(DDOperator)operatorType;
+- (DDOperatorAssociativity) associativityForOperator:(NSString *)operator;
@end
@@ -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 {
@@ -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;
@@ -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";
+
@@ -10,23 +10,21 @@
#import "DDMathParser.h"
@interface _DDOperatorInfo : NSObject {
- DDOperator _operator;
DDOperatorArity _arity;
DDOperatorAssociativity _defaultAssociativity;
NSInteger _precedence;
NSString *_token;
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
Oops, something went wrong. Retry.

0 comments on commit 4ce5ae4

Please sign in to comment.