Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPEvaluationContext.m
2014-09-28 23:50:18 +02:00

72 lines
1.4 KiB
Objective-C

//
// 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
{
if (sharedContext) {
return sharedContext;
}
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)defineVariable:(NSString *)variable withValue:(id)value
{
NSMutableDictionary *currentBindings = self.stack.lastObject;
currentBindings[variable] = value;
}
- (void)undefineVariable:(NSString *)variable
{
NSMutableDictionary *currentBindings = self.stack.lastObject;
[currentBindings removeObjectForKey:variable];
}
- (NSDecimalNumber *)valueForVariable:(NSString *)variable
{
NSMutableDictionary *currentBindings = self.stack.lastObject;
return currentBindings[variable];
}
- (BOOL)isVariableDefined:(NSString *)variable
{
return [self valueForVariable:variable] != nil;
}
@end