Improved Evaluation
This commit is contained in:
90
MathPad/MPTokenStream.m
Normal file
90
MathPad/MPTokenStream.m
Normal file
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// MPTokenStream.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 20.09.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPTokenStream.h"
|
||||
|
||||
@interface MPTokenStream ()
|
||||
@property (readwrite, nonatomic) NSUInteger currentLocation;
|
||||
@end
|
||||
|
||||
@implementation MPTokenStream {
|
||||
NSUInteger currentTokenIndex;
|
||||
NSUInteger eofLocation;
|
||||
}
|
||||
|
||||
- (instancetype)initWithTokens:(NSArray *)tokens
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.tokens = tokens;
|
||||
self.ignoringWhitespaceTokens = YES;
|
||||
if (tokens.count > 0) {
|
||||
eofLocation = NSMaxRange([tokens.lastObject range]);
|
||||
}
|
||||
[self reset];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)skipWhitespaces
|
||||
{
|
||||
if (!self.isIgnoringWhitespaceTokens) {
|
||||
return;
|
||||
}
|
||||
while (currentTokenIndex < self.tokens.count) {
|
||||
MPToken *token = self.tokens[currentTokenIndex];
|
||||
if (token.tokenType != MPWhitespaceToken) {
|
||||
return;
|
||||
}
|
||||
self.currentLocation = NSMaxRange(token.range);
|
||||
++currentTokenIndex;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)reset
|
||||
{
|
||||
currentTokenIndex = 0;
|
||||
self.currentLocation = 0;
|
||||
[self skipWhitespaces];
|
||||
}
|
||||
|
||||
- (BOOL)hasMoreTokens
|
||||
{
|
||||
[self skipWhitespaces];
|
||||
return currentTokenIndex < self.tokens.count;
|
||||
}
|
||||
|
||||
- (MPToken *)nextToken
|
||||
{
|
||||
[self skipWhitespaces];
|
||||
if (currentTokenIndex >= self.tokens.count) {
|
||||
return [[MPToken alloc] initEOFTokenAtLocation:eofLocation];
|
||||
} else {
|
||||
MPToken *token = self.tokens[currentTokenIndex++];
|
||||
self.currentLocation = NSMaxRange(token.range);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
- (MPToken *)nextTokenOfType:(MPTokenType)type
|
||||
{
|
||||
[self skipWhitespaces];
|
||||
if (currentTokenIndex >= self.tokens.count) {
|
||||
return nil;
|
||||
} else {
|
||||
MPToken *token = self.tokens[currentTokenIndex];
|
||||
if (token.tokenType != type) {
|
||||
return nil;
|
||||
}
|
||||
++currentTokenIndex;
|
||||
self.currentLocation = NSMaxRange(token.range);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user