62 lines
1.6 KiB
Objective-C
62 lines
1.6 KiB
Objective-C
//
|
|
// MPOperatorChain.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 11.10.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPOperatorChain.h"
|
|
|
|
#import "MPTokenStream.h"
|
|
#import "MPToken.h"
|
|
|
|
@implementation MPOperatorChain
|
|
|
|
- (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;
|
|
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;
|
|
}
|
|
|
|
- (BOOL)validate:(NSError *__autoreleasing *)error
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (NSDecimalNumber *)evaluate
|
|
{
|
|
return self.negating ? [[NSDecimalNumber alloc] initWithInteger:-1] : [NSDecimalNumber one];
|
|
}
|
|
|
|
@end
|