// // MPExpression.h // MathPad // // Created by Kim Wittenburg on 10.08.14. // Copyright (c) 2014 Kim Wittenburg. All rights reserved. // @import Foundation; #import "NSString+MPExpressionElement.h" @class MPExpression, MPFunction, MPRangePath; @protocol MPExpressionElement; @interface MPExpression : NSObject #pragma mark Creation Methods - (instancetype)init; // Convenience - (instancetype)initWithElement:(id)element; // Convenience - (instancetype)initWithElements:(NSArray *)elements; // Designated Initializer #pragma mark Working With the Expression Tree @property (nonatomic, weak) MPFunction *parent; // Set automatically, nil for root expression - (void)fixElements; // Called automatically, removes empty elements, joins subsequent strings #pragma mark Primitive Methods - (NSUInteger)length; - (NSUInteger)numberOfElements; - (id)elementAtIndex:(NSUInteger)index; - (NSArray *)elementsInRange:(NSRange)range; - (NSUInteger)indexOfElement:(id)element; - (void)replaceElementsInRange:(NSRange)range withElements:(NSArray *)elements; // TODO: - (NSUInteger)indexOfElementAtLocation:(NSUInteger)location; - (double)doubleValue; // Evaluates Expression #pragma mark Notifications // All notification methods should create a new rangePath with the receiver's index added to the beginning of the path and then ascend the message to it's parent // TODO: More notifications - (void)didChangeElementsInRangePath:(MPRangePath *)rangePath replacementLength:(NSUInteger)replacementLength; #pragma mark Basic NSObject Methods - (BOOL)isEqualToExpression:(MPExpression *)anExpression; - (NSString *)description; - (NSUInteger)hash; @end @interface MPExpression (MPExpressionExtension) #pragma mark Working With the Expression Tree - (id)elementAtIndexPath:(NSIndexPath *)indexPath; // Returns an MPExpression or id - (NSArray *)elementsInRangePath:(MPRangePath *)rangePath; #pragma mark Working With Expressions - (MPExpression *)subexpressionFromLocation:(NSUInteger)from; - (MPExpression *)subexpressionToLocation:(NSUInteger)to; - (MPExpression *)subexpressionWithRange:(NSRange)range; #pragma mark Mutating Expressions - (void)appendElement:(id)anElement; - (void)appendElements:(NSArray *)elements; - (void)insertElement:(id)anElement atLocation:(NSUInteger)index; - (void)insertElements:(NSArray *)elements atLocation:(NSUInteger)index; - (void)deleteElementsInRange:(NSRange)range; #pragma mark Evaluating Expressions - (float)floatValue; - (int)intValue; - (NSInteger)integerValue; - (long long)longLongValue; #pragma mark Querying Expressions - (NSArray *)elements; @end