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/MPFactor.m
Kim Wittenburg f4f924bd71 Cleaned Imports
2014-11-08 01:05:08 +01:00

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