Archived
1

Added Expression Tree Inspection Methods

Cleaned Code
This commit is contained in:
Kim Wittenburg
2014-04-20 23:28:17 +02:00
parent 35215923ef
commit 1daec37a25
6 changed files with 113 additions and 23 deletions

View File

@@ -21,8 +21,7 @@ NSString *MPDivisionOperator = @"/";
- (NSInteger)lengthOfSymbol:(id)symbol;
- (void)validateSymbols:(NSArray *)symbols;
- (NSArray *)repairedSymbols:(NSArray *)symbols; // Merges subsequent strings
- (void)repairSymbols:(NSMutableArray *)symbols;
- (NSArray *)fixedSymbols:(NSArray *)symbols;
- (void)getSplitOffset:(out NSUInteger *)offset
inSymbolAtIndex:(out NSUInteger *)symbolIndex
forSplitLocation:(NSUInteger)loc;
@@ -42,13 +41,28 @@ NSString *MPDivisionOperator = @"/";
[self validateSymbols:symbols];
self = [super init];
if (self) {
symbols = [self repairedSymbols:symbols];
_symbols = [[NSArray alloc] initWithArray:symbols
copyItems:YES];
[self fixSymbols];
}
return self;
}
#pragma mark Working With the Expression Tree
- (void)functionSymbolChanged:(MPFunction *)symbol atLocalIndexPath:(NSIndexPath *)indexPath
{
NSUInteger index = [_symbols indexOfObject:symbol];
if (index != NSNotFound) {
[self.parent childChanged:self atLocalIndexPath:[indexPath indexPathByAddingIndex:index]];
}
}
- (void)fixSymbols
{
_symbols = [self fixedSymbols:_symbols];
}
#pragma mark Primitive Methods
- (NSUInteger)numberOfSymbols
@@ -108,7 +122,6 @@ NSString *MPDivisionOperator = @"/";
return 1;
}
// TODO: Deal with subsequent strings
- (void)validateSymbols:(NSArray *)symbols
{
for (id symbol in symbols) {
@@ -121,31 +134,27 @@ NSString *MPDivisionOperator = @"/";
}
}
// TODO: Deal with empty strings
- (NSArray *)repairedSymbols:(NSArray *)symbols
- (NSArray *)fixedSymbols:(NSArray *)symbols
{
NSMutableArray *mutableSymbols = [symbols mutableCopy];
[self repairSymbols:mutableSymbols];
return [mutableSymbols copy];
}
- (void)repairSymbols:(NSMutableArray *)symbols
{
for (NSInteger index = 0; index < symbols.count; index++) {
id next = index+1 < symbols.count ? symbols[index+1] : nil;
id current = symbols[index];
for (NSInteger index = 0; index < mutableSymbols.count; index++) {
id next = index+1 < mutableSymbols.count ? mutableSymbols[index+1] : nil;
id current = mutableSymbols[index];
if ([current isString]) {
if ([current length] == 0) {
[symbols removeObjectAtIndex:index];
[mutableSymbols removeObjectAtIndex:index];
index--;
} else if ([next isString]) {
NSString *new = [NSString stringWithFormat:@"%@%@", current, next];
[symbols replaceObjectAtIndex:index withObject:new];
[symbols removeObjectAtIndex:index+1];
[mutableSymbols replaceObjectAtIndex:index withObject:new];
[mutableSymbols removeObjectAtIndex:index+1];
index--;
}
} else {
[(MPFunction *)current setParent:self];
}
}
return [mutableSymbols copy];
}
- (void)getSplitOffset:(out NSUInteger *)offset
@@ -396,20 +405,28 @@ NSString *MPDivisionOperator = @"/";
@end
@implementation MPMutableExpression
@implementation MPMutableExpression {
@protected
NSInteger _editCount;
}
- (instancetype)initWithSymbols:(NSArray *)symbols
{
[self validateSymbols:symbols];
self = [super initWithSymbols:nil];
if (self) {
symbols = [self repairedSymbols:symbols];
_symbols = [[NSMutableArray alloc] initWithArray:symbols
copyItems:YES];
[self fixSymbols];
}
return self;
}
- (void)fixSymbols
{
_symbols = [[self fixedSymbols:_symbols] mutableCopy];
}
- (NSArray *)symbols
{
// Return an immutable array:
@@ -451,7 +468,9 @@ NSString *MPDivisionOperator = @"/";
}
// Revalidate structure and invalidate length
[self repairSymbols:(NSMutableArray *)_symbols];
if (_editCount == 0) {
[self fixSymbols];
}
_length = 0;
}
@@ -481,6 +500,22 @@ NSString *MPDivisionOperator = @"/";
*insertionIndex = splitSymbolIndex;
}
- (void)beginEditing
{
_editCount++;
}
- (void)endEditing
{
if (_editCount == 0) {
return;
}
_editCount--;
if (_editCount == 0) {
[self fixSymbols];
}
}
@end
@implementation MPMutableExpression (MPMutableExpressionExtensionMethods)