Skip to content

Commit

Permalink
Modernizing some syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
davedelong committed Jan 4, 2014
1 parent 487bd4c commit f28b855
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
12 changes: 5 additions & 7 deletions Command line demo/main.m
Expand Up @@ -6,14 +6,12 @@
void listFunctions(void);

NSString* readLine() {
// NSCharacterSet *valid = [DDMathStringTokenizer legalCharacters];
NSMutableData *data = [NSMutableData data];


do {
char c = getchar();
if ([[NSCharacterSet newlineCharacterSet] characterIsMember:(unichar)c]) { break; }
// if (![valid characterIsMember:(unichar)c]) { continue; }

[data appendBytes:&c length:sizeof(char)];
} while (1);
Expand Down Expand Up @@ -46,11 +44,11 @@ int main (int argc, const char * argv[]) {

[DDParser setDefaultPowerAssociativity:DDOperatorAssociativityRight];
DDMathEvaluator *evaluator = [[DDMathEvaluator alloc] init];
// [evaluator setFunctionResolver:^DDMathFunction (NSString *name) {
// return DD_AUTORELEASE([^DDExpression* (NSArray *args, NSDictionary *substitutions, DDMathEvaluator *eval, NSError **error) {
// return [DDExpression numberExpressionWithNumber:[NSNumber numberWithInt:42]];
// } copy]);
// }];
[evaluator setFunctionResolver:^DDMathFunction (NSString *name) {
return DD_AUTORELEASE([^DDExpression* (NSArray *args, NSDictionary *substitutions, DDMathEvaluator *eval, NSError **error) {
return [DDExpression numberExpressionWithNumber:@42];
} copy]);
}];

NSString * line = nil;
do {
Expand Down
2 changes: 1 addition & 1 deletion DDMathParser/DDMathParserMacros.h
Expand Up @@ -15,7 +15,7 @@
#endif

#ifndef ERR
#define ERR(_c,_f,...) [NSError errorWithDomain:DDMathParserErrorDomain code:(_c) userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:(_f), ##__VA_ARGS__] forKey:NSLocalizedDescriptionKey]]
#define ERR(_c,_f,...) [NSError errorWithDomain:DDMathParserErrorDomain code:(_c) userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:(_f), ##__VA_ARGS__]}]
#endif

#define DDMathParserDeprecated(_r) __attribute__((deprecated(_r)))
22 changes: 10 additions & 12 deletions DDMathParser/_DDFunctionEvaluator.m
Expand Up @@ -409,7 +409,7 @@ - (DDExpression *)random:(NSArray *)arguments variables:(NSDictionary *)variable
random = arc4random_uniform(upperBound);
}

return [DDExpression numberExpressionWithNumber:[NSNumber numberWithLongLong:random]];
return [DDExpression numberExpressionWithNumber:@(random)];
}

- (DDExpression *)log:(NSArray *)arguments variables:(NSDictionary *)variables error:(NSError **)error {
Expand Down Expand Up @@ -954,24 +954,22 @@ - (DDExpression *)l_and:(NSArray *)arguments variables:(NSDictionary *)variables
REQUIRE_N_ARGS(2);
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSNumber *result = [NSNumber numberWithBool:[left boolValue] && [right boolValue]];
NSNumber *result = @([left boolValue] && [right boolValue]);
return [DDExpression numberExpressionWithNumber:result];
}

- (DDExpression *)l_or:(NSArray *)arguments variables:(NSDictionary *)variables error:(NSError **)error {
REQUIRE_N_ARGS(2);
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSNumber *result = [NSNumber numberWithBool:[left boolValue] ||

[right boolValue]];
NSNumber *result = @([left boolValue] || [right boolValue]);
return [DDExpression numberExpressionWithNumber:result];
}

- (DDExpression *)l_not:(NSArray *)arguments variables:(NSDictionary *)variables error:(NSError **)error {
REQUIRE_N_ARGS(1);
NSNumber *n = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *result = [NSNumber numberWithBool:![n boolValue]];
NSNumber *result = @(![n boolValue]);
return [DDExpression numberExpressionWithNumber:result];
}

Expand All @@ -980,7 +978,7 @@ - (DDExpression *)l_eq:(NSArray *)arguments variables:(NSDictionary *)variables
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSComparisonResult compare = [left compare:right];
NSNumber *result = [NSNumber numberWithBool:compare == NSOrderedSame];
NSNumber *result = @(compare == NSOrderedSame);
return [DDExpression numberExpressionWithNumber:result];
}

Expand All @@ -989,7 +987,7 @@ - (DDExpression *)l_neq:(NSArray *)arguments variables:(NSDictionary *)variables
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSComparisonResult compare = [left compare:right];
NSNumber *result = [NSNumber numberWithBool:compare != NSOrderedSame];
NSNumber *result = @(compare != NSOrderedSame);
return [DDExpression numberExpressionWithNumber:result];
}

Expand All @@ -998,7 +996,7 @@ - (DDExpression *)l_lt:(NSArray *)arguments variables:(NSDictionary *)variables
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSComparisonResult compare = [left compare:right];
NSNumber *result = [NSNumber numberWithBool:compare == NSOrderedAscending];
NSNumber *result = @(compare == NSOrderedAscending);
return [DDExpression numberExpressionWithNumber:result];
}

Expand All @@ -1007,7 +1005,7 @@ - (DDExpression *)l_gt:(NSArray *)arguments variables:(NSDictionary *)variables
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSComparisonResult compare = [left compare:right];
NSNumber *result = [NSNumber numberWithBool:compare == NSOrderedDescending];
NSNumber *result = @(compare == NSOrderedDescending);
return [DDExpression numberExpressionWithNumber:result];
}

Expand All @@ -1016,7 +1014,7 @@ - (DDExpression *)l_ltoe:(NSArray *)arguments variables:(NSDictionary *)variable
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSComparisonResult compare = [left compare:right];
NSNumber *result = [NSNumber numberWithBool:compare == NSOrderedSame || compare == NSOrderedAscending];
NSNumber *result = @(compare == NSOrderedSame || compare == NSOrderedAscending);
return [DDExpression numberExpressionWithNumber:result];
}

Expand All @@ -1025,7 +1023,7 @@ - (DDExpression *)l_gtoe:(NSArray *)arguments variables:(NSDictionary *)variable
NSNumber *left = [[self evaluator] evaluateExpression:[arguments objectAtIndex:0] withSubstitutions:variables error:error];
NSNumber *right = [[self evaluator] evaluateExpression:[arguments objectAtIndex:1] withSubstitutions:variables error:error];
NSComparisonResult compare = [left compare:right];
NSNumber *result = [NSNumber numberWithBool:compare == NSOrderedSame || compare == NSOrderedDescending];
NSNumber *result = @(compare == NSOrderedSame || compare == NSOrderedDescending);
return [DDExpression numberExpressionWithNumber:result];
}

Expand Down
2 changes: 1 addition & 1 deletion Demo/DemoController.m
Expand Up @@ -98,7 +98,7 @@ - (void)updateVariablesWithExpression:(DDExpression *)e {
[variables removeObjectsForKeys:[keysThatShouldBeRemoved allObjects]];
for (NSString * variable in v) {
if ([variables objectForKey:variable] == nil) {
[variables setObject:[NSNumber numberWithInt:0] forKey:variable];
[variables setObject:@0 forKey:variable];
}
}

Expand Down

0 comments on commit f28b855

Please sign in to comment.