Redesign of the Evaluation System
This commit is contained in:
63
MathPad/MPEvaluationContext.m
Normal file
63
MathPad/MPEvaluationContext.m
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// MPEvaluationContext.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 12.09.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPEvaluationContext.h"
|
||||
|
||||
@interface MPEvaluationContext ()
|
||||
@property (nonatomic, strong) NSMutableArray *stack;
|
||||
@end
|
||||
|
||||
@implementation MPEvaluationContext
|
||||
|
||||
static MPEvaluationContext *sharedContext;
|
||||
+ (MPEvaluationContext *)sharedContext
|
||||
{
|
||||
if (!sharedContext) {
|
||||
sharedContext = [[MPEvaluationContext alloc] init];
|
||||
}
|
||||
return sharedContext;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_stack = [[NSMutableArray alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)push
|
||||
{
|
||||
[self.stack addObject:[[NSMutableDictionary alloc] init]];
|
||||
}
|
||||
|
||||
- (void)pop
|
||||
{
|
||||
[self.stack removeLastObject];
|
||||
}
|
||||
|
||||
- (void)bindValue:(NSDecimalNumber *)value toName:(NSString *)variableName
|
||||
{
|
||||
NSMutableDictionary *currentBindings = self.stack.lastObject;
|
||||
currentBindings[variableName] = value;
|
||||
}
|
||||
|
||||
- (void)unbindVariableName:(NSString *)variableName
|
||||
{
|
||||
NSMutableDictionary *currentBindings = self.stack.lastObject;
|
||||
[currentBindings removeObjectForKey:variableName];
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)valueForVariableName:(NSString *)variableName
|
||||
{
|
||||
NSMutableDictionary *currentBindings = self.stack.lastObject;
|
||||
return currentBindings[variableName];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user