Archived
1

Fundamental Redesign of Evaluation

This commit is contained in:
Kim Wittenburg
2014-11-24 22:42:44 +01:00
parent 10f0e73ad3
commit 7a32e3b0b6
45 changed files with 1398 additions and 350 deletions

View File

@@ -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];
}