Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPSummand.m
Kim Wittenburg f4f924bd71 Cleaned Imports
2014-11-08 01:05:08 +01:00

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