Skip to content
This repository
Newer
Older
100644 115 lines (99 sloc) 3.773 kb
6fa4925a »
2010-11-20 initial commit
1 //
2 // DDExpression.m
3 // DDMathParser
4 //
5 // Created by Dave DeLong on 11/16/10.
6 // Copyright 2010 Home. All rights reserved.
7 //
8
b7940ccf »
2011-09-29 Moving the ARC macros to DDMathParser.h
9 #import "DDMathParser.h"
6fa4925a »
2010-11-20 initial commit
10 #import "DDExpression.h"
11 #import "DDMathEvaluator.h"
12 #import "DDMathEvaluator+Private.h"
80a44b27 »
2010-12-04 Replaced parser with new operator precedence parser
13 #import "DDParser.h"
6fa4925a »
2010-11-20 initial commit
14
15 #import "_DDNumberExpression.h"
16 #import "_DDFunctionExpression.h"
17 #import "_DDVariableExpression.h"
18
19
20 @implementation DDExpression
21
832ed113 »
2011-12-16 % can now be used as a percent
22 @synthesize parentExpression=_parentExpression;
23
3c188144 »
2011-04-02 change to an NSError API instead of an NSException API
24 + (id) expressionFromString:(NSString *)expressionString error:(NSError **)error {
db4197e5 »
2011-07-10 Easier support for specifying a custom tokenizer. Also added a phi() …
25 DDParser *parser = [DDParser parserWithString:expressionString error:error];
1336f94a »
2011-07-13 removed class method on DDExpression
26 return [parser parsedExpressionWithError:error];
6fa4925a »
2010-11-20 initial commit
27 }
28
29 + (id) numberExpressionWithNumber:(NSNumber *)number {
b7940ccf »
2011-09-29 Moving the ARC macros to DDMathParser.h
30 return DD_AUTORELEASE([[_DDNumberExpression alloc] initWithNumber:number]);
6fa4925a »
2010-11-20 initial commit
31 }
32
3c188144 »
2011-04-02 change to an NSError API instead of an NSException API
33 + (id) functionExpressionWithFunction:(NSString *)function arguments:(NSArray *)arguments error:(NSError **)error {
b7940ccf »
2011-09-29 Moving the ARC macros to DDMathParser.h
34 return DD_AUTORELEASE([[_DDFunctionExpression alloc] initWithFunction:function arguments:arguments error:error]);
6fa4925a »
2010-11-20 initial commit
35 }
36
37 + (id) variableExpressionWithVariable:(NSString *)variable {
b7940ccf »
2011-09-29 Moving the ARC macros to DDMathParser.h
38 return DD_AUTORELEASE([[_DDVariableExpression alloc] initWithVariable:variable]);
6fa4925a »
2010-11-20 initial commit
39 }
40
41 #pragma mark -
42 #pragma mark Abstract method implementations
43
44 - (DDExpressionType) expressionType {
45 [NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
46 return 0;
47 }
3c188144 »
2011-04-02 change to an NSError API instead of an NSException API
48 - (NSNumber *) evaluateWithSubstitutions:(NSDictionary *)substitutions evaluator:(DDMathEvaluator *)evaluator error:(NSError **)error {
049f06f8 »
2011-04-11 Fixed some warnings
49 #pragma unused(substitutions, evaluator, error)
6fa4925a »
2010-11-20 initial commit
50 [NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
51 return nil;
52 }
c8f9b8ff »
2010-11-20 added ability to rudimentarily simplify expressions
53 - (DDExpression *) simplifiedExpression {
3c188144 »
2011-04-02 change to an NSError API instead of an NSException API
54 NSError *error = nil;
55 DDExpression *simplified = [self simplifiedExpressionWithEvaluator:[DDMathEvaluator sharedMathEvaluator] error:&error];
56 if (error != nil) {
57 NSLog(@"error: %@", error);
58 return nil;
59 }
60 return simplified;
c8f9b8ff »
2010-11-20 added ability to rudimentarily simplify expressions
61 }
3c188144 »
2011-04-02 change to an NSError API instead of an NSException API
62 - (DDExpression *) simplifiedExpressionWithEvaluator:(DDMathEvaluator *)evaluator error:(NSError **)error {
049f06f8 »
2011-04-11 Fixed some warnings
63 #pragma unused(evaluator, error)
6fa4925a »
2010-11-20 initial commit
64 [NSException raise:NSInvalidArgumentException format:@"this method should be overridden: %@", NSStringFromSelector(_cmd)];
65 return nil;
66 }
67 - (NSNumber *) number {
68 [NSException raise:NSInvalidArgumentException format:@"This is not a numeric expression"];
69 return nil;
70 }
71 - (NSString *) function {
72 [NSException raise:NSInvalidArgumentException format:@"This is not a function expression"];
73 return nil;
74 }
75 - (NSArray *) arguments {
76 [NSException raise:NSInvalidArgumentException format:@"This is not a function expression"];
77 return nil;
78 }
79 - (NSString *) variable {
80 [NSException raise:NSInvalidArgumentException format:@"This is not a variable expression"];
81 return nil;
82 }
c8f9b8ff »
2010-11-20 added ability to rudimentarily simplify expressions
83 - (BOOL)isEqual:(id)object {
84 if ([object isKindOfClass:[DDExpression class]] == NO) { return NO; }
85 DDExpression * expression = (DDExpression *)object;
86 if ([expression expressionType] != [self expressionType]) { return NO; }
87 if ([self expressionType] == DDExpressionTypeNumber) {
88 return [[self number] isEqualToNumber:[expression number]];
89 }
90 if ([self expressionType] == DDExpressionTypeVariable) {
91 return [[self variable] isEqual:[expression variable]];
92 }
93 if ([self expressionType] == DDExpressionTypeFunction) {
94 return ([[self function] isEqual:[expression function]] &&
95 [[self arguments] isEqual:[expression arguments]]);
96 }
97 return NO;
98 }
6fa4925a »
2010-11-20 initial commit
99
27dfa4b0 »
2011-06-21 DDExpression now conforms to <NSCoding>
100 - (id)initWithCoder:(NSCoder *)aDecoder {
101 #pragma unused(aDecoder)
102 return self;
103 }
104
105 - (void)encodeWithCoder:(NSCoder *)aCoder {
106 #pragma unused(aCoder)
107 return;
108 }
109
832ed113 »
2011-12-16 % can now be used as a percent
110 - (void)_setParentExpression:(DDExpression *)parent {
111 _parentExpression = parent;
112 }
113
6fa4925a »
2010-11-20 initial commit
114 @end
Something went wrong with that request. Please try again.