119 lines
3.2 KiB
Objective-C
119 lines
3.2 KiB
Objective-C
//
|
|
// MPExpressionTree.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 09.10.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPExpressionTree.h"
|
|
|
|
#import "MPSummand.h"
|
|
|
|
#import "MPTokenStream.h"
|
|
#import "MPToken.h"
|
|
|
|
#import "MPExpression.h"
|
|
|
|
@implementation MPExpressionTree {
|
|
NSMutableArray *_summands;
|
|
}
|
|
|
|
- (id)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_summands = [[NSMutableArray alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithTokenStream:(MPTokenStream *)tokenStream
|
|
{
|
|
self = [self init];
|
|
if (self) {
|
|
[tokenStream beginIgnoringWhitespaceTokens];
|
|
|
|
self.definedVariable = nil;
|
|
if (tokenStream.currentToken.tokenType == MPVariableToken) {
|
|
if (tokenStream.peekNextToken.tokenType == MPEqualsToken) {
|
|
self.definedVariable = tokenStream.currentToken.stringValue;
|
|
[tokenStream currentTokenConsumed];
|
|
[tokenStream currentTokenConsumed];
|
|
}
|
|
}
|
|
while ([tokenStream hasMoreTokens]) {
|
|
[_summands addObject:[[MPSummand alloc] initWithTokenStream:tokenStream]];
|
|
}
|
|
|
|
[tokenStream endIgnoringOrAcceptingWhitespaceTokens];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (NSArray *)summands
|
|
{
|
|
return _summands;
|
|
}
|
|
|
|
- (BOOL)validate:(NSError *__autoreleasing *)error
|
|
{
|
|
return [self validateExpectingVariableDefinition:NO
|
|
error:error];
|
|
}
|
|
|
|
- (BOOL)validateExpectingVariableDefinition:(BOOL)flag
|
|
error:(NSError *__autoreleasing *)error
|
|
{
|
|
if (flag) {
|
|
if (!self.definedVariable) {
|
|
if (error) {
|
|
*error = MPParseError(1,
|
|
NSLocalizedString(@"Expected Variable Definition.", @"Error message. This is displayed when an expected variable definition was not found."),
|
|
nil);
|
|
}
|
|
return NO;
|
|
}
|
|
} else {
|
|
if (self.definedVariable) {
|
|
if (error) {
|
|
*error = MPParseError(2,
|
|
NSLocalizedString(@"Unexpected Variable Definition.", @"Error message. This is displayed when no variable definition was expected but one was found."),
|
|
nil);
|
|
}
|
|
return NO;
|
|
}
|
|
}
|
|
if (_summands.count == 0) {
|
|
if (error) {
|
|
*error = MPParseError(3,
|
|
NSLocalizedString(@"Empty Expression.", @"Error message. This is displayed when the user tried to evaluate an empty expression."),
|
|
nil);
|
|
}
|
|
return NO;
|
|
}
|
|
for (MPSummand *summand in _summands) {
|
|
if (![summand validate:error]) {
|
|
return NO;
|
|
}
|
|
}
|
|
return YES;
|
|
}
|
|
|
|
- (NSDecimalNumber *)evaluate
|
|
{
|
|
NSDecimalNumber *value = [NSDecimalNumber zero];
|
|
for (MPSummand *summand in _summands) {
|
|
NSDecimalNumber *currentValue = [summand evaluate];
|
|
if ([currentValue isEqualToNumber:[NSDecimalNumber notANumber]]) {
|
|
value = currentValue;
|
|
break;
|
|
}
|
|
value = [value decimalNumberByAdding:currentValue];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
|
|
@end
|