Fundamental Redesign of Evaluation
This commit is contained in:
@@ -14,9 +14,9 @@
|
||||
#import "MPRangePath.h"
|
||||
|
||||
#import "MPExpressionTokenizer.h"
|
||||
#import "MPTokenStream.h"
|
||||
#import "MPToken.h"
|
||||
#import "MPExpressionTree.h"
|
||||
#import "MPExpressionParser.h"
|
||||
#import "MPParsedExpression.h"
|
||||
|
||||
#import "NSIndexPath+MPAdditions.h"
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
NSString *const MPIllegalElementException = @"MPIllegalElementException";
|
||||
NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptionElementKey";
|
||||
|
||||
NSString *const MPMathKitErrorDomain = @"MPMathKitErrorDomain";
|
||||
NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
|
||||
|
||||
|
||||
@interface MPExpression () {
|
||||
NSMutableArray * _elements;
|
||||
@@ -328,7 +324,11 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
break;
|
||||
|
||||
case MPTokenReferenceFrame:
|
||||
symbolIndex = [self.tokens[anIndex] range].location;
|
||||
[self.tokens enumerateObjectsUsingBlock:^(id<MPToken> obj, NSUInteger idx, BOOL *stop) {
|
||||
symbolIndex += obj.range.length;
|
||||
*stop = idx >= anIndex - 1;
|
||||
}];
|
||||
// symbolIndex = [self.tokens[anIndex] range].location;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -556,17 +556,38 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
#pragma mark Evaluating Expressions
|
||||
|
||||
|
||||
- (NSDecimalNumber *)evaluateWithError:(NSError *__autoreleasing *)error
|
||||
- (NSDecimalNumber *)evaluateWithErrors:(NSArray *__autoreleasing *)errors
|
||||
{
|
||||
MPExpressionTree *tree = [self parse];
|
||||
return [tree validate:error] ? [tree evaluate] : nil;
|
||||
NSArray *parsingErrors;
|
||||
MPParsedExpression *parsedExpression = [self parse:&parsingErrors];
|
||||
NSError *evaluationError;
|
||||
NSDecimalNumber *result = parsedExpression ? [parsedExpression evaluate:&evaluationError] : nil;
|
||||
if (errors) {
|
||||
NSMutableArray *localErrors = [[NSMutableArray alloc] initWithCapacity:parsingErrors.count+1];
|
||||
if (parsingErrors) {
|
||||
[localErrors addObjectsFromArray:parsingErrors];
|
||||
}
|
||||
if (evaluationError) {
|
||||
[localErrors addObject:evaluationError];
|
||||
}
|
||||
*errors = localErrors;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
- (MPExpressionTree *)parse
|
||||
- (MPParsedExpression *)parse:(NSArray *__autoreleasing *)errors
|
||||
{
|
||||
MPTokenStream *tokenStream = [[MPTokenStream alloc] initWithTokens:self.tokens];
|
||||
return [[MPExpressionTree alloc] initWithTokenStream:tokenStream];
|
||||
return [self parseExpectingVariable:NO
|
||||
errors:errors];
|
||||
}
|
||||
|
||||
|
||||
- (MPParsedExpression *)parseExpectingVariable:(BOOL)flag
|
||||
errors:(NSArray *__autoreleasing *)errors
|
||||
{
|
||||
return [[[MPExpressionParser alloc] initWithExpression:self] parseExpectingVariableDefinition:flag
|
||||
errors:errors];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user