67 lines
1.7 KiB
Objective-C
67 lines
1.7 KiB
Objective-C
//
|
|
// MPSummand.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 09.10.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPSummand.h"
|
|
|
|
#import "MPOperatorChain.h"
|
|
#import "MPProduct.h"
|
|
|
|
#import "MPTokenStream.h"
|
|
#import "MPToken.h"
|
|
|
|
#import "MPExpression.h"
|
|
#import "MPMathRules.h"
|
|
|
|
@implementation MPSummand
|
|
|
|
- (id)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithTokenStream:(MPTokenStream *)tokenStream
|
|
{
|
|
self = [self init];
|
|
if (self) {
|
|
[tokenStream beginIgnoringWhitespaceTokens];
|
|
if (tokenStream.currentToken.tokenType == MPOperatorListToken) {
|
|
_operatorChain = [[MPOperatorChain alloc] initWithTokenStream:tokenStream];
|
|
}
|
|
_product = [[MPProduct alloc] initWithTokenStream:tokenStream];
|
|
[tokenStream endIgnoringOrAcceptingWhitespaceTokens];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (BOOL)validate:(NSError *__autoreleasing *)error
|
|
{
|
|
if (self.operatorChain.numberOfOperators > [MPMathRules sharedRules].maximumOperatorChainLength) {
|
|
if (error) {
|
|
*error = MPParseError(4,
|
|
NSLocalizedString(@"Too many operators.", @"Error message. This is displayed when there are too many subsequent operators in an expression."),
|
|
nil);
|
|
}
|
|
return NO;
|
|
}
|
|
return [self.product validate:error];
|
|
}
|
|
|
|
- (NSDecimalNumber *)evaluate
|
|
{
|
|
NSDecimalNumber *value = [self.product evaluate];
|
|
if ([value isNotEqualTo:[NSDecimalNumber notANumber]] && self.operatorChain) {
|
|
value = [value decimalNumberByMultiplyingBy:[self.operatorChain evaluate]];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
@end
|