Permalink
Browse files

DDMathEvaluator.angleMeasurementMode. Fixes issue #29.

DDMathEvaluator has a new property to specify that the arguments to the trigonometric functions should be interpreted as being in degrees versus radians (the default).
  • Loading branch information...
davedelong committed Aug 31, 2012
1 parent 3351b50 commit 29073d7b4e24da15c84374810327750e5a9725d8
View
@@ -83,6 +83,8 @@ int main (int argc, const char * argv[]) {
} while (1);
+ DD_RELEASE(evaluator);
+
printf("Goodbye!\n");
#if DD_HAS_ARC
@@ -13,6 +13,7 @@
@interface DDMathEvaluator ()
- (DDMathFunction) functionWithName:(NSString *)functionName;
+- (id) variableWithName:(NSString *)variableName;
- (BOOL) functionExpressionFailedToResolve:(_DDFunctionExpression *)functionExpression error:(NSError **)error;
@@ -13,15 +13,20 @@
@class DDExpression;
typedef DDMathFunction (^DDFunctionResolver)(NSString *);
+typedef NSNumber* (^DDVariableResolver)(NSString *);
@interface DDMathEvaluator : NSObject {
NSMutableArray *functions;
NSMutableDictionary * functionMap;
NSMutableArray *rewriteRules;
DDFunctionResolver functionResolver;
+ DDVariableResolver variableResolver;
+ DDAngleMeasurementMode angleMeasurementMode;
}
+@property (nonatomic) DDAngleMeasurementMode angleMeasurementMode; // default is Radians
@property (nonatomic, copy) DDFunctionResolver functionResolver;
+@property (nonatomic, copy) DDVariableResolver variableResolver;
+ (id) sharedMathEvaluator;
@@ -30,7 +30,9 @@ - (_DDFunctionContainer *)functionContainerWithName:(NSString *)functionName;
@implementation DDMathEvaluator
+@synthesize angleMeasurementMode=angleMeasurementMode;
@synthesize functionResolver=functionResolver;
+@synthesize variableResolver=variableResolver;
static DDMathEvaluator * _sharedEvaluator = nil;
@@ -47,6 +49,7 @@ - (id) init {
if (self) {
functions = [[NSMutableArray alloc] init];
functionMap = [[NSMutableDictionary alloc] init];
+ angleMeasurementMode = DDAngleMeasurementModeRadians;
[self _registerStandardFunctions];
}
@@ -62,6 +65,7 @@ - (void) dealloc {
[functionMap release];
[rewriteRules release];
[functionResolver release];
+ [variableResolver release];
[super dealloc];
#endif
}
@@ -126,6 +130,14 @@ - (BOOL) functionExpressionFailedToResolve:(_DDFunctionExpression *)functionExpr
return NO;
}
+- (id) variableWithName:(NSString *)variableName {
+ id value = nil;
+ if (variableResolver != nil) {
+ value = variableResolver(variableName);
+ }
+ return value;
+}
+
- (BOOL) addAlias:(NSString *)alias forFunctionName:(NSString *)functionName {
//we can't add an alias for a function that already exists
DDMathFunction function = [self functionWithName:alias];
@@ -78,7 +78,7 @@ - (NSString *)debugDescription {
[d appendFormat:@"arity:%@, ", arityNames[arity]];
NSInteger precedence = [self operatorPrecedence];
- [d appendFormat:@"precedence:%d, ", precedence];
+ [d appendFormat:@"precedence:%ld, ", precedence];
DDOperatorAssociativity assoc = [self operatorAssociativity];
NSString *assocNames[2] = { @"LEFT", @"RIGHT" };
View
@@ -49,4 +49,9 @@ typedef enum {
DDErrorCodeInvalidNumberOfArguments, //@"random() may only have up to 2 arguments"
DDErrorCodeInvalidArgument, //@"upper bound (%ld) of random() must be larger than lower bound (%ld)"
-} DDErrorCode;
+} DDErrorCode;
+
+typedef enum {
+ DDAngleMeasurementModeRadians,
+ DDAngleMeasurementModeDegrees
+} DDAngleMeasurementMode;
Oops, something went wrong.

0 comments on commit 29073d7

Please sign in to comment.