Added the MPExpressionTree Classes
This commit is contained in:
95
MathPad/MPFactor.m
Normal file
95
MathPad/MPFactor.m
Normal file
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// MPFactor.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 09.10.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPFactor.h"
|
||||
#import "MPMathRules.h"
|
||||
|
||||
@implementation MPFactor {
|
||||
NSRange _multiplicationSymbolRange;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithTokenStream:(MPTokenStream *)tokenStream
|
||||
{
|
||||
self = [self init];
|
||||
if (self) {
|
||||
[tokenStream beginIgnoringWhitespaceTokens];
|
||||
MPToken *token = tokenStream.currentToken;
|
||||
if (token.tokenType == MPMultiplicationSymbolToken) {
|
||||
_multiplicationSymbolRange = token.range;
|
||||
[tokenStream currentTokenConsumed];
|
||||
} else {
|
||||
_multiplicationSymbolRange = NSMakeRange(NSNotFound, 0);
|
||||
}
|
||||
if (tokenStream.currentToken.tokenType == MPOperatorListToken) {
|
||||
_operatorChain = [[MPOperatorChain alloc] initWithTokenStream:tokenStream];
|
||||
}
|
||||
MPValueGroup *valueGroup = [[MPValueGroup alloc] initWithTokenStream:tokenStream];
|
||||
_value = [tokenStream consumeSuffixesForValue:valueGroup];
|
||||
[tokenStream endIgnoringOrAcceptingWhitespaceTokens];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSRange)range
|
||||
{
|
||||
if (_multiplicationSymbolRange.location == NSNotFound) {
|
||||
return self.value.range;
|
||||
}
|
||||
return NSUnionRange(_multiplicationSymbolRange, self.value.range);
|
||||
}
|
||||
|
||||
- (NSRange)multiplicationSymbolRange
|
||||
{
|
||||
return _multiplicationSymbolRange;
|
||||
}
|
||||
|
||||
- (BOOL)hasMultiplicationSymbol
|
||||
{
|
||||
return _multiplicationSymbolRange.location != NSNotFound;
|
||||
}
|
||||
|
||||
- (BOOL)validate:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
if (self.operatorChain.numberOfOperators > [[MPMathRules sharedRules] maximumOperatorChainLengthInMultiplication]) {
|
||||
if (error) {
|
||||
*error = MPParseError(self.operatorChain.range, @"Too many operators in Multiplication");
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
- (NSArray *)expressionElements
|
||||
{
|
||||
NSMutableArray *elements = [[NSMutableArray alloc] init];
|
||||
if (self.hasMultiplicationSymbol) {
|
||||
[elements addObject:@"*"];
|
||||
}
|
||||
[elements addObjectsFromArray:self.value.expressionElements];
|
||||
return elements;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user