Redesign of the Evaluation System
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#import "MPExpressionEvaluator.h"
|
||||
#import "MPExpression.h"
|
||||
#import "MPFunction.h"
|
||||
#import "MPParsedFactor.h"
|
||||
#import "MPFunction+MPParsedFactor.h"
|
||||
|
||||
@interface MPExpressionEvaluator ()
|
||||
@property (readwrite, nonatomic, strong) NSString *definedVariable;
|
||||
@@ -16,7 +18,6 @@
|
||||
@end
|
||||
|
||||
@implementation MPExpressionEvaluator {
|
||||
NSMutableDictionary *_variableBindings;
|
||||
MPElementParser *parser;
|
||||
}
|
||||
- (id)initWithExpression:(MPExpression *)expression
|
||||
@@ -24,29 +25,12 @@
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_expression = expression;
|
||||
_variableBindings = [[NSMutableDictionary alloc] init];
|
||||
parser = [[MPElementParser alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark Evaluating Expressions
|
||||
- (NSDictionary *)variableBindings
|
||||
{
|
||||
return [_variableBindings copy];
|
||||
}
|
||||
|
||||
- (void)bindValue:(NSDecimalNumber *)value toVariableName:(NSString *)name
|
||||
{
|
||||
[_variableBindings setObject:value
|
||||
forKey:name];
|
||||
}
|
||||
|
||||
- (void)unbindVariableName:(NSString *)name
|
||||
{
|
||||
[_variableBindings removeObjectForKey:name];
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
return [self evaluateVariableDefinition:NO error:error];
|
||||
@@ -68,14 +52,10 @@
|
||||
id<MPExpressionElement> element = [self.expression elementAtIndex:elementIndex];
|
||||
|
||||
if ([element isString]) {
|
||||
MPParsedFactor *nextFactor = nil;
|
||||
id<MPParsedFactor> nextFactor = nil;
|
||||
if (elementIndex < self.expression.numberOfElements - 1) {
|
||||
MPFunction *nextFunction = (MPFunction *)[self.expression elementAtIndex:elementIndex+1];
|
||||
NSDecimalNumber *functionValue = [nextFunction evaluate:error];
|
||||
if (!functionValue) {
|
||||
return nil;
|
||||
}
|
||||
nextFactor = [MPParsedFactor factorWithDecimalNumber:functionValue];
|
||||
nextFactor = nextFunction;
|
||||
}
|
||||
|
||||
NSArray *newProducts;
|
||||
@@ -105,14 +85,10 @@
|
||||
|
||||
elementIndex++;
|
||||
} else {
|
||||
NSDecimalNumber *functionValue = [(MPFunction *)element evaluate:error];
|
||||
if (!functionValue) {
|
||||
return nil;
|
||||
}
|
||||
if (!currentProduct) {
|
||||
currentProduct = [[MPParsedProduct alloc] init];
|
||||
}
|
||||
[currentProduct addFactor:[MPParsedFactor factorWithDecimalNumber:functionValue]];
|
||||
[currentProduct addFactor:(MPFunction *)element];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +96,11 @@
|
||||
|
||||
NSDecimalNumber *value = [NSDecimalNumber zero];
|
||||
for (MPParsedProduct *product in products) {
|
||||
value = [value decimalNumberByAdding:product.value];
|
||||
NSDecimalNumber *productValue = [product evaluateWithError:error];
|
||||
if (!productValue) {
|
||||
return nil;
|
||||
}
|
||||
value = [value decimalNumberByAdding:productValue];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user