123 lines
3.3 KiB
Objective-C
123 lines
3.3 KiB
Objective-C
//
|
|
// MPExpression.h
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 17.04.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
@class MPExpression, MPFunction;
|
|
|
|
extern NSString *MPAdditionOperator;
|
|
extern NSString *MPSubtractionOperator;
|
|
extern NSString *MPMultiplicationOperator;
|
|
extern NSString *MPDivisionOperator;
|
|
|
|
@interface MPExpression : NSObject <NSCopying, NSMutableCopying, NSCoding>
|
|
|
|
#pragma mark Creation Methods
|
|
|
|
- (instancetype)initWithSymbols:(NSArray *)symbols;
|
|
|
|
#pragma mark Working With the Expression Tree
|
|
|
|
@property (nonatomic, weak) MPFunction *parent; // Documentation: Do not set, may be nil
|
|
|
|
- (void)fixSymbols;
|
|
|
|
#pragma mark Primitive Methods
|
|
|
|
- (NSUInteger)numberOfSymbols;
|
|
- (id)symbolAtIndex:(NSUInteger)index; // Either an NSString or a MPFunction (which can be mutated)
|
|
|
|
- (double)doubleValue; // Evaluates Expression
|
|
|
|
@end
|
|
|
|
@interface MPExpression (MPExpressionExtensionMethods)
|
|
|
|
+ (NSArray *)operators;
|
|
|
|
#pragma mark Creation Methods
|
|
|
|
- (id)init;
|
|
- (instancetype)initWithString:(NSString *)aString;
|
|
- (instancetype)initWithFunction:(MPFunction *)aFunction;
|
|
|
|
+ (instancetype)expression;
|
|
+ (instancetype)expressionWithString:(NSString *)aString;
|
|
+ (instancetype)expressionWithFunction:(MPFunction *)aFunction;
|
|
+ (instancetype)expressionWithSymbols:(NSArray *)symbols;
|
|
|
|
#pragma mark Working With the Expression Tree
|
|
|
|
- (NSUInteger)indexOfSymbol:(id)symbol;
|
|
- (id)symbolAtIndexPath:(NSIndexPath *)indexPath; // May also return MPExpression
|
|
|
|
#pragma mark Working With Expressions
|
|
|
|
- (NSUInteger)length;
|
|
|
|
- (MPExpression *)subexpressionFromIndex:(NSUInteger)from;
|
|
- (MPExpression *)subexpressionToIndex:(NSUInteger)to;
|
|
- (MPExpression *)subexpressionWithRange:(NSRange)range;
|
|
|
|
- (BOOL)isEqualToExpression:(MPExpression *)anExpression;
|
|
|
|
- (MPExpression *)expressionByAppendingString:(NSString *)aString;
|
|
- (MPExpression *)expressionByAppendingFunction:(MPFunction *)aFunction;
|
|
- (MPExpression *)expressionByAppendingExpression:(MPExpression *)anExpression;
|
|
- (MPExpression *)expressionByAppendingSymbols:(NSArray *)symbols;
|
|
|
|
#pragma mark Evaluating Expressions
|
|
|
|
- (float)floatValue;
|
|
- (int)intValue;
|
|
- (NSInteger)integerValue;
|
|
- (long long)longLongValue;
|
|
|
|
#pragma mark Querying an Expression's Contents
|
|
|
|
- (NSArray *)symbols;
|
|
|
|
- (NSString *)description;
|
|
|
|
- (NSUInteger)hash;
|
|
|
|
@end
|
|
|
|
@interface MPMutableExpression : MPExpression
|
|
|
|
- (void)replaceSymbolsInRange:(NSRange)range
|
|
withSymbols:(NSArray *)symbols;
|
|
|
|
- (void)beginEditing;
|
|
- (void)endEditing;
|
|
|
|
@end
|
|
|
|
@interface MPMutableExpression (MPMutableExpressionExtensionMethods)
|
|
|
|
- (void)insertString:(NSString *)aString
|
|
atIndex:(NSUInteger)loc;
|
|
- (void)insertFunction:(MPFunction *)aFunction
|
|
atIndex:(NSUInteger)loc;
|
|
- (void)insertExpression:(MPExpression *)anExpression
|
|
atIndex:(NSUInteger)loc;
|
|
- (void)insertSymbols:(NSArray *)symbols
|
|
atIndex:(NSUInteger)loc;
|
|
|
|
- (void)deleteSymbolsInRange:(NSRange)range;
|
|
|
|
- (void)appendString:(NSString *)aString;
|
|
- (void)appendFunction:(MPFunction *)aFunction;
|
|
- (void)appendExpression:(MPExpression *)anExpression;
|
|
- (void)appendSymbols:(NSArray *)symbols;
|
|
|
|
- (void)setString:(NSString *)aString;
|
|
- (void)setFunction:(MPFunction *)aFunction;
|
|
- (void)setExpression:(MPExpression *)anExpression;
|
|
- (void)setSymbols:(NSArray *)symbols;
|
|
|
|
@end
|