Skip to content
This repository
Browse code

added support for logical not

  • Loading branch information...
commit 4ee87166178129c46460c5439aa9d466026d4efe 1 parent 34c91e5
Dave DeLong authored
1  DDMathParser/DDMathEvaluator.m
@@ -271,6 +271,7 @@ + (NSSet *) _standardFunctions {
271 271 //logical functions
272 272 @"l_and",
273 273 @"l_or",
  274 + @"l_not",
274 275 nil];
275 276 });
276 277 return standardFunctions;
27 DDMathParser/DDMathStringTokenizer.m
@@ -92,12 +92,12 @@ - (id)initWithString:(NSString *)expressionString error:(NSError **)error {
92 92 return nil;
93 93 }
94 94 }
  95 +
  96 + [self _processToken:nil withError:error];
95 97
96 98 if (error && *error) {
97 99 DD_RELEASE(self);
98 100 self = nil;
99   - } else {
100   - [self _processToken:nil withError:nil];
101 101 }
102 102 }
103 103
@@ -119,10 +119,7 @@ - (void)dealloc {
119 119
120 120 - (BOOL)_processToken:(DDMathStringToken *)token withError:(NSError **)error {
121 121 //figure out if "-" and "+" are unary or binary
122   - BOOL shouldContinue = [self _processUnknownOperatorToken:token withError:error];
123   - if (!shouldContinue) {
124   - return NO;
125   - }
  122 + (void)[self _processUnknownOperatorToken:token withError:error];
126 123
127 124 if ([token operatorType] == DDOperatorUnaryPlus) {
128 125 // the unary + operator is a no-op operator. It does nothing, so we'll throw it out
@@ -140,8 +137,8 @@ - (BOOL)_processToken:(DDMathStringToken *)token withError:(NSError **)error {
140 137 }
141 138
142 139 - (BOOL)_processUnknownOperatorToken:(DDMathStringToken *)token withError:(NSError **)error {
  140 + DDMathStringToken *previousToken = [_tokens lastObject];
143 141 if ([token tokenType] == DDTokenTypeOperator && [token operatorType] == DDOperatorInvalid) {
144   - DDMathStringToken *previousToken = [_tokens lastObject];
145 142 DDOperator resolvedOperator = DDOperatorInvalid;
146 143
147 144 BOOL shouldBeUnary = NO;
@@ -168,6 +165,14 @@ - (BOOL)_processUnknownOperatorToken:(DDMathStringToken *)token withError:(NSErr
168 165 }
169 166 }
170 167
  168 + if ([[token token] isEqual:@"!"]) {
  169 + if (previousToken == nil) {
  170 + resolvedOperator = DDOperatorLogicalNot;
  171 + } else if ([previousToken tokenType] == DDTokenTypeOperator && [previousToken operatorType] != DDOperatorParenthesisClose) {
  172 + resolvedOperator = DDOperatorLogicalNot;
  173 + }
  174 + }
  175 +
171 176 [token resolveToOperator:resolvedOperator];
172 177
173 178 if ([token operatorType] == DDOperatorInvalid) {
@@ -177,6 +182,14 @@ - (BOOL)_processUnknownOperatorToken:(DDMathStringToken *)token withError:(NSErr
177 182 return NO;
178 183 }
179 184 }
  185 +
  186 + if (token == nil && [[previousToken token] isEqual:@"!"]) {
  187 + [previousToken resolveToOperator:DDOperatorFactorial];
  188 + if (error != nil) {
  189 + *error = nil;
  190 + }
  191 + }
  192 +
180 193 return YES;
181 194 }
182 195
2  DDMathParser/DDParser.m
@@ -145,6 +145,8 @@ - (DDOperatorAssociativity) associativityForOperator:(DDOperator)operatorType {
145 145
146 146 // factorial is always left associative
147 147 case DDOperatorFactorial: return DDOperatorAssociativityLeft;
  148 + // logical not is always right associative
  149 + case DDOperatorLogicalNot: return DDOperatorAssociativityRight;
148 150
149 151 default: return DDOperatorAssociativityLeft;
150 152 }
1  DDMathParser/DDParserTypes.h
@@ -25,6 +25,7 @@ typedef enum {
25 25
26 26 DDOperatorLogicalOr,
27 27 DDOperatorLogicalAnd,
  28 + DDOperatorLogicalNot,
28 29
29 30 DDOperatorBitwiseOr,
30 31 DDOperatorBitwiseXor,
3  DDMathParser/NSString+DDMathParsing.m
@@ -18,9 +18,8 @@ - (NSNumber *) numberByEvaluatingString {
18 18 - (NSNumber *) numberByEvaluatingStringWithSubstitutions:(NSDictionary *)substitutions {
19 19 NSError *error = nil;
20 20 NSNumber *returnValue = [self numberByEvaluatingStringWithSubstitutions:substitutions error:&error];
21   - if (error != nil) {
  21 + if (returnValue == nil) {
22 22 NSLog(@"error: %@", error);
23   - return nil;
24 23 }
25 24 return returnValue;
26 25 }
1  DDMathParser/_DDFunctionUtilities.h
@@ -98,5 +98,6 @@
98 98
99 99 + (DDMathFunction) l_andFunction;
100 100 + (DDMathFunction) l_orFunction;
  101 ++ (DDMathFunction) l_notFunction;
101 102
102 103 @end
12 DDMathParser/_DDFunctionUtilities.m
@@ -1097,4 +1097,16 @@ + (DDMathFunction) l_orFunction {
1097 1097 return DD_AUTORELEASE([function copy]);
1098 1098 }
1099 1099
  1100 ++ (DDMathFunction) l_notFunction {
  1101 + DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
  1102 +#pragma unused(variables, evaluator)
  1103 + REQUIRE_N_ARGS(1);
  1104 + NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
  1105 + NSNumber *result = [NSNumber numberWithBool:![n boolValue]];
  1106 + return [DDExpression numberExpressionWithNumber:result];
  1107 +
  1108 + };
  1109 + return DD_AUTORELEASE([function copy]);
  1110 +}
  1111 +
1100 1112 @end
3  DDMathParser/_DDOperatorInfo.m
@@ -50,6 +50,9 @@ + (NSArray *)_buildOperators {
50 50 [operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"&&" function:@"l_and"]];
51 51 precedence++;
52 52
  53 + [operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"!" function:@"l_not"]];
  54 + precedence++;
  55 +
53 56 [operators addObject:[self infoForOperator:DDOperatorBitwiseOr arity:DDOperatorArityBinary precedence:precedence token:@"|" function:@"or"]];
54 57 precedence++;
55 58

0 comments on commit 4ee8716

Please sign in to comment.
Something went wrong with that request. Please try again.