81 lines
2.2 KiB
Objective-C
81 lines
2.2 KiB
Objective-C
//
|
|
// MPFactor.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 09.10.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPFactor.h"
|
|
|
|
#import "MPOperatorChain.h"
|
|
#import "MPValueGroup.h"
|
|
|
|
#import "MPTokenStream.h"
|
|
#import "MPToken.h"
|
|
|
|
#import "MPExpression.h"
|
|
#import "MPMathRules.h"
|
|
|
|
@implementation MPFactor {
|
|
BOOL _multiplicationSymbolPresent;
|
|
}
|
|
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithTokenStream:(MPTokenStream *)tokenStream
|
|
{
|
|
self = [self init];
|
|
if (self) {
|
|
[tokenStream beginIgnoringWhitespaceTokens];
|
|
MPToken *token = tokenStream.currentToken;
|
|
_multiplicationSymbolPresent = token.tokenType == MPMultiplicationSymbolToken;
|
|
if (_multiplicationSymbolPresent) {
|
|
[tokenStream currentTokenConsumed];
|
|
}
|
|
if (tokenStream.currentToken.tokenType == MPOperatorListToken) {
|
|
_operatorChain = [[MPOperatorChain alloc] initWithTokenStream:tokenStream];
|
|
}
|
|
MPValueGroup *valueGroup = [[MPValueGroup alloc] initWithTokenStream:tokenStream];
|
|
_value = [tokenStream consumeSuffixesForValue:valueGroup];
|
|
[tokenStream endIgnoringOrAcceptingWhitespaceTokens];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (BOOL)hasMultiplicationSymbol
|
|
{
|
|
return _multiplicationSymbolPresent;
|
|
}
|
|
|
|
- (BOOL)validate:(NSError *__autoreleasing *)error
|
|
{
|
|
if (self.operatorChain.numberOfOperators > [[MPMathRules sharedRules] maximumOperatorChainLengthInMultiplication]) {
|
|
if (error) {
|
|
*error = MPParseError(7,
|
|
NSLocalizedString(@"Too many operators in Multiplication.", @"Error message. This is displayed when there are too many operators between a multiplication symbol and a value."),
|
|
nil);
|
|
}
|
|
return NO;
|
|
}
|
|
return [self.value validate:error];
|
|
}
|
|
|
|
- (NSDecimalNumber *)evaluate
|
|
{
|
|
NSDecimalNumber *value = [self.value evaluate];
|
|
if ([value isNotEqualTo:[NSDecimalNumber notANumber]] && self.operatorChain) {
|
|
value = [value decimalNumberByMultiplyingBy:[self.operatorChain evaluate]];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
@end
|