Archived
1

Improved Evaluation

This commit is contained in:
Kim Wittenburg
2014-09-28 23:50:18 +02:00
parent 43b6f78afb
commit d67a1949e9
19 changed files with 841 additions and 89 deletions

90
MathPad/MPTokenStream.m Normal file
View 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