Added the MPExpressionTree Classes
This commit is contained in:
71
MathPad/MPSummand.m
Normal file
71
MathPad/MPSummand.m
Normal file
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// MPSummand.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 09.10.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPSummand.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;
|
||||
}
|
||||
|
||||
- (NSRange)range
|
||||
{
|
||||
return NSUnionRange(self.operatorChain.range, self.product.range);
|
||||
}
|
||||
|
||||
- (BOOL)validate:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
if (self.operatorChain.numberOfOperators > [MPMathRules sharedRules].maximumOperatorChainLength) {
|
||||
if (error) {
|
||||
*error = MPParseError(self.operatorChain.range, @"Too many operators.");
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
-(NSArray *)expressionElements
|
||||
{
|
||||
NSMutableArray *elements = [[NSMutableArray alloc] init];
|
||||
if (self.operatorChain) {
|
||||
[elements addObjectsFromArray:self.operatorChain.expressionElements];
|
||||
}
|
||||
[elements addObjectsFromArray:self.product.expressionElements];
|
||||
return @[self.operatorChain.expressionElements,];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user