Added the MPExpressionTree Classes
This commit is contained in:
75
MathPad/MPOperatorChain.m
Normal file
75
MathPad/MPOperatorChain.m
Normal file
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// MPOperatorChain.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 11.10.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPOperatorChain.h"
|
||||
|
||||
@implementation MPOperatorChain {
|
||||
NSRange _range;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithTokenStream:(MPTokenStream *)tokenStream
|
||||
{
|
||||
self = [self init];
|
||||
if (self) {
|
||||
[tokenStream beginIgnoringWhitespaceTokens];
|
||||
MPToken *currentToken = tokenStream.currentToken;
|
||||
if (currentToken.tokenType != MPOperatorListToken) {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Expected Operators" userInfo:nil];
|
||||
}
|
||||
NSString *operatorString = currentToken.stringValue;
|
||||
_range = currentToken.range;
|
||||
operatorString = [[operatorString componentsSeparatedByString:@" "] componentsJoinedByString:@""];
|
||||
_negating = NO;
|
||||
_numberOfOperators = operatorString.length;
|
||||
for (NSUInteger index = 0; index < _numberOfOperators; index++) {
|
||||
if ([[operatorString substringWithRange:NSMakeRange(index, 1)] isEqualToString:@"-"]) {
|
||||
_negating = !_negating;
|
||||
}
|
||||
}
|
||||
[tokenStream currentTokenConsumed];
|
||||
[tokenStream endIgnoringOrAcceptingWhitespaceTokens];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSRange)range
|
||||
{
|
||||
return _range;
|
||||
}
|
||||
|
||||
- (BOOL)validate:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)evaluate
|
||||
{
|
||||
return self.negating ? [[NSDecimalNumber alloc] initWithInteger:-1] : [NSDecimalNumber one];
|
||||
}
|
||||
|
||||
- (NSArray *)expressionElements
|
||||
{
|
||||
NSMutableString *text = [[NSMutableString alloc] init];
|
||||
for (NSUInteger index = 1; index < self.numberOfOperators; index++) {
|
||||
[text appendString:@"+"];
|
||||
}
|
||||
if (self.negating) {
|
||||
[text appendString:@"-"];
|
||||
}
|
||||
return @[text.copy];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user