Archived
1

Fundamental Redesign of Evaluation

This commit is contained in:
Kim Wittenburg
2014-11-24 22:42:44 +01:00
parent 10f0e73ad3
commit 7a32e3b0b6
45 changed files with 1398 additions and 350 deletions

85
MathPad/MPTerm.m Normal file
View 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