Added the MPExpressionTree Classes
This commit is contained in:
68
MathPad/MPVariable.m
Normal file
68
MathPad/MPVariable.m
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// MPVariable.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 09.10.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPVariable.h"
|
||||
#import "MPEvaluationContext.h"
|
||||
|
||||
@implementation MPVariable {
|
||||
NSRange _range;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithTokenStream:(MPTokenStream *)tokenStream
|
||||
{
|
||||
self = [self init];
|
||||
if (self) {
|
||||
[tokenStream beginIgnoringWhitespaceTokens];
|
||||
MPToken *token = tokenStream.currentToken;
|
||||
if (token.tokenType == MPVariableToken) {
|
||||
self.variableName = token.stringValue;
|
||||
_range = token.range;
|
||||
} else {
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Expected Variable" userInfo:nil];
|
||||
}
|
||||
[tokenStream currentTokenConsumed];
|
||||
[tokenStream endIgnoringOrAcceptingWhitespaceTokens];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSRange)range
|
||||
{
|
||||
return _range;
|
||||
}
|
||||
|
||||
- (BOOL)validate:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
if (![[MPEvaluationContext sharedContext] isVariableDefined:self.variableName]) {
|
||||
if (error) {
|
||||
*error = MPParseError(self.range, @"Undefined Variable");
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)evaluate
|
||||
{
|
||||
return [[MPEvaluationContext sharedContext] valueForVariable:self.variableName];
|
||||
}
|
||||
|
||||
- (NSArray *)expressionElements
|
||||
{
|
||||
return @[self.variableName];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user