Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Only define operator characters in one place
  • Loading branch information
davedelong committed Oct 18, 2011
1 parent 3d57a41 commit f0a3f95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 5 additions & 8 deletions DDMathParser/DDMathStringTokenizer.m
Expand Up @@ -10,6 +10,7 @@
#import "DDMathStringTokenizer.h"
#import "DDMathParserMacros.h"
#import "DDMathStringToken.h"
#import "_DDOperatorInfo.h"

#define DD_IS_DIGIT(_c) ((_c) >= '0' && (_c) <= '9')
#define DD_IS_WHITESPACE(_c) ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:(_c)])
Expand Down Expand Up @@ -55,14 +56,10 @@ + (NSCharacterSet *)_operatorCharacterSet {
static dispatch_once_t onceToken;
static NSCharacterSet *_operatorSet = nil;
dispatch_once(&onceToken, ^{
// \u2228 is ∨
// \u2227 is ∧
// \u00AC is ¬
// \u2264 is ≤
// \u2265 is ≥
// \u00f7 is ÷
// \u00d7 is ×
_operatorSet = DD_RETAIN([NSCharacterSet characterSetWithCharactersInString:@"+-*/&|!%^~()<>,=\u2228\u2227\u00ac\u2264\u2265\u00f7\u00d7"]);
NSArray *allOperators = [_DDOperatorInfo allOperators];
NSArray *operatorTokens = [allOperators valueForKey:@"token"];
NSString *operatorString = [operatorTokens componentsJoinedByString:@""];
_operatorSet = DD_RETAIN([NSCharacterSet characterSetWithCharactersInString:operatorString]);
});
return _operatorSet;
}
Expand Down
7 changes: 7 additions & 0 deletions DDMathParser/_DDOperatorInfo.m
Expand Up @@ -89,10 +89,12 @@ + (NSArray *)_buildOperators {
NSInteger precedence = 0;

[operators addObject:[self infoForOperator:DDOperatorLogicalOr arity:DDOperatorArityBinary precedence:precedence token:@"||" function:@"l_or" associativity:DDOperatorAssociativityLeft]];
// \u2228 is ∨
[operators addObject:[self infoForOperator:DDOperatorLogicalOr arity:DDOperatorArityBinary precedence:precedence token:@"\u2228" function:@"l_or" associativity:DDOperatorAssociativityLeft]];
precedence++;

[operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"&&" function:@"l_and" associativity:DDOperatorAssociativityLeft]];
// \u2227 is ∧
[operators addObject:[self infoForOperator:DDOperatorLogicalAnd arity:DDOperatorArityBinary precedence:precedence token:@"\u2227" function:@"l_and" associativity:DDOperatorAssociativityLeft]];
precedence++;

Expand All @@ -109,14 +111,17 @@ + (NSArray *)_buildOperators {
precedence++;

[operators addObject:[self infoForOperator:DDOperatorLogicalLessThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"<=" function:@"l_ltoe" associativity:DDOperatorAssociativityLeft]];
// \u2264 is ≤
[operators addObject:[self infoForOperator:DDOperatorLogicalLessThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"\u2264" function:@"l_ltoe" associativity:DDOperatorAssociativityLeft]];
precedence++;

[operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@">=" function:@"l_gtoe" associativity:DDOperatorAssociativityLeft]];
// \u2265 is ≥
[operators addObject:[self infoForOperator:DDOperatorLogicalGreaterThanOrEqual arity:DDOperatorArityBinary precedence:precedence token:@"\u2265" function:@"l_gtoe" associativity:DDOperatorAssociativityLeft]];
precedence++;

[operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"!" function:@"l_not" associativity:DDOperatorAssociativityRight]];
// \u00AC is ¬
[operators addObject:[self infoForOperator:DDOperatorLogicalNot arity:DDOperatorArityUnary precedence:precedence token:@"\u00ac" function:@"l_not" associativity:DDOperatorAssociativityRight]];
precedence++;

Expand All @@ -142,8 +147,10 @@ + (NSArray *)_buildOperators {

// multiplication and division have the same precedence
[operators addObject:[self infoForOperator:DDOperatorMultiply arity:DDOperatorArityBinary precedence:precedence token:@"*" function:@"multiply" 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]];
// \u00f7 is ÷
[operators addObject:[self infoForOperator:DDOperatorDivide arity:DDOperatorArityBinary precedence:precedence token:@"\u00f7" function:@"divide" associativity:DDOperatorAssociativityLeft]];
precedence++;

Expand Down

0 comments on commit f0a3f95

Please sign in to comment.