Fundamental Redesign of Evaluation
This commit is contained in:
85
MathPad/MPTerm.m
Normal file
85
MathPad/MPTerm.m
Normal file
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// MPTerm.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 11.11.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPTerm.h"
|
||||
|
||||
#import "MPParsedExpression.h"
|
||||
#import "MPSumTerm.h"
|
||||
#import "MPEvaluationContext.h"
|
||||
|
||||
@implementation MPTerm
|
||||
|
||||
- (NSDecimalNumber *)evaluate:(NSError *__autoreleasing *)error
|
||||
{
|
||||
[[MPEvaluationContext sharedContext] push];
|
||||
NSDecimalNumber *result = [self doEvaluation:error];
|
||||
[[MPEvaluationContext sharedContext] pop];
|
||||
return result;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)doEvaluation:(NSError *__autoreleasing *)error
|
||||
{
|
||||
NSLog(@"%@", self.class);
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL)defineVariable:(NSString *)variableName
|
||||
value:(NSDecimalNumber *)value
|
||||
error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
BOOL couldDefineVariable = [[MPEvaluationContext sharedContext] defineVariable:variableName
|
||||
value:value];
|
||||
if (!couldDefineVariable) {
|
||||
if (error) {
|
||||
NSString *localizedDescription = [NSString stringWithFormat:NSLocalizedString(@"Redifinition of variable \"%@\".", nil), variableName];
|
||||
*error = [NSError errorWithDomain:MPMathKitErrorDomain
|
||||
code:100
|
||||
userInfo:@{NSLocalizedDescriptionKey: localizedDescription}];
|
||||
}
|
||||
}
|
||||
return couldDefineVariable;
|
||||
}
|
||||
|
||||
- (BOOL)redefineVariable:(NSString *)variableName
|
||||
value:(NSDecimalNumber *)value
|
||||
error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
BOOL couldDefineVariable = [[MPEvaluationContext sharedContext] redefineVariable:variableName
|
||||
value:value];
|
||||
if (!couldDefineVariable) {
|
||||
if (error) {
|
||||
NSString *localizedDescription = [NSString stringWithFormat:NSLocalizedString(@"Redifinition of variable \"%@\".", nil), variableName];
|
||||
*error = [NSError errorWithDomain:MPMathKitErrorDomain
|
||||
code:100
|
||||
userInfo:@{NSLocalizedDescriptionKey: localizedDescription}];
|
||||
}
|
||||
}
|
||||
return couldDefineVariable;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)valueForVariable:(NSString *)variableName
|
||||
error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
NSDecimalNumber *value = [[MPEvaluationContext sharedContext] valueForVariable:variableName];
|
||||
if (!value) {
|
||||
if (error) {
|
||||
NSString *localizedDescription = [NSString stringWithFormat:NSLocalizedString(@"Undefined variable \"%@\".", nil), variableName];
|
||||
*error = [NSError errorWithDomain:MPMathKitErrorDomain
|
||||
code:101
|
||||
userInfo:@{NSLocalizedDescriptionKey: localizedDescription}];
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
- (void)undefineVariable:(NSString *)variableName
|
||||
{
|
||||
[[MPEvaluationContext sharedContext] undefineVariable:variableName];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user