Redesign of the Evaluation System
This commit is contained in:
47
MathPad/MPParsedVariable.m
Normal file
47
MathPad/MPParsedVariable.m
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// MPParsedVariable.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 13.09.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPParsedVariable.h"
|
||||
#import "MPEvaluationContext.h"
|
||||
|
||||
@interface MPParsedVariable ()
|
||||
@property (nonatomic) NSRange range;
|
||||
@property (nonatomic, strong) NSString *variableName;
|
||||
@end
|
||||
|
||||
@implementation MPParsedVariable
|
||||
|
||||
- (instancetype)initWithRange:(NSRange)range
|
||||
inString:(NSString *)string
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_range = range;
|
||||
if (range.location != NSNotFound) {
|
||||
_variableName = [string substringWithRange:range];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)exists
|
||||
{
|
||||
return self.range.location != NSNotFound;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
NSDecimalNumber *value = [[MPEvaluationContext sharedContext] valueForVariableName:self.variableName];
|
||||
if (!value) {
|
||||
*error = MPParseError(self.range, @"Undefined Variable.");
|
||||
return nil;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user