blob: 6df4e110c4849c9b5ef6dc0c2f9c13c65a09afe5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/* -*- mode: objc; coding: utf-8 -*- */
/* Copyright 2008, Matthias Benkard. */
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>
#import "MLKEnvironment.h"
#import "MLKUndefinedVariableException.h"
@implementation MLKEnvironment
-(MLKEnvironment *) init
{
return [self initWithParent:nil bindings:nil];
}
-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent bindings:(NSDictionary *)bindings
{
_bindings = [[NSMutableDictionary alloc] initWithCapacity:10];
ASSIGN (_parent, parent);
[self addBindings: bindings];
return self;
}
-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent
{
return [self initWithParent:parent bindings:nil];
}
-(MLKEnvironment *) initWithBindings:(NSDictionary *)bindings
{
return [self initWithParent:nil bindings:bindings];
}
-(void) setBinding:(MLKSymbol *)symbol to:(id)value
{
[self setBinding:symbol to:value inEnvironment:self];
}
-(void) setBinding:(MLKSymbol *)symbol to:(id)value inEnvironment:(MLKEnvironment *)env
{
if ([[_bindings allKeys] containsObject:symbol])
[_bindings setObject:value forKey:symbol];
else
if (_parent)
[_parent setBinding:symbol to:value inEnvironment:env];
else
[[[MLKUndefinedVariableException alloc] initWithEnvironment:env
variableName:symbol]
raise];
}
-(id) valueForBinding:(MLKSymbol *)symbol
{
return [self valueForBinding:symbol];
}
-(id) valueForBinding:(MLKSymbol *)symbol inEnvironment:(MLKEnvironment *)env
{
if ([[_bindings allKeys] containsObject:symbol])
return [_bindings objectForKey:symbol];
else
if (_parent)
return [_parent valueForBinding:symbol];
else
[[[MLKUndefinedVariableException alloc] initWithEnvironment:env
variableName:symbol]
raise];;
return nil; // avoid a stupid compiler warning
}
-(void) addBindings:(NSDictionary *)bindings
{
[_bindings addEntriesFromDictionary:bindings];
}
-(void) addBinding:(MLKSymbol *)symbol to:(id)value
{
[_bindings setObject:value forKey:symbol];
}
@end
|