-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLKEnvironment.m
201 lines (165 loc) · 4.79 KB
/
MLKEnvironment.m
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -*- mode: objc; coding: utf-8 -*- */
/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
* Copyright (C) 2008 Matthias Andreas Benkard.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSSet.h>
#import "MLKEnvironment.h"
#import "MLKUnboundVariableError.h"
#import "NSObject-MLKPrinting.h"
#import "runtime-compatibility.h"
#import "util.h"
@implementation MLKEnvironment
-(MLKEnvironment *) init
{
return [self initWithParent:nil values:nil];
}
-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent
values:(NSDictionary *)bindings
{
self = [super init];
_bindings = [[NSMutableDictionary alloc] initWithCapacity:10];
LASSIGN (_parent, parent);
[self addValues:bindings];
return self;
}
-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent
{
return [self initWithParent:parent values:nil];
}
-(MLKEnvironment *) initWithValues:(NSDictionary *)bindings
{
return [self initWithParent:nil values:bindings];
}
-(MLKEnvironment *) parent
{
return _parent;
}
-(NSArray *) bindingArray
{
NSMutableArray *array = [NSMutableArray arrayWithArray:[_bindings allKeys]];
if (_parent)
[array addObjectsFromArray:[_parent bindingArray]];
return array;
}
-(NSSet *) bindings
{
return [NSSet setWithArray:[self bindingArray]];
}
-(void) setValue:(id)value forSymbol:(MLKSymbol *)symbol;
{
MLKBinding *binding;
if (!(binding = [self bindingForSymbol:symbol]))
@throw LAUTORELEASE ([[MLKUnboundVariableError alloc]
initWithSymbol:symbol
inEnvironment:self]);
[binding setValue:value];
}
-(id) valueForSymbol:(MLKSymbol *)symbol
{
MLKBinding *binding;
if (!(binding = [self bindingForSymbol:symbol]))
@throw LAUTORELEASE ([[MLKUnboundVariableError alloc]
initWithSymbol:symbol
inEnvironment:self]);
return [binding value];
}
-(MLKBinding*) bindingForSymbol:(MLKSymbol *)symbol
{
MLKBinding *binding;
symbol = symbol ? (id)symbol : (id)[NSNull null];
if ((binding = [_bindings objectForKey:symbol]))
return binding;
else
if (_parent)
return [_parent bindingForSymbol:symbol];
else
return nil;
}
-(void) addBindings:(NSDictionary *)bindings
{
[_bindings addEntriesFromDictionary:bindings];
}
-(void) addValues:(NSDictionary *)bindings
{
int i;
NSArray *keys;
keys = [bindings allKeys];
for (i = 0; i < [keys count]; i++)
{
id key = [keys objectAtIndex:i];
id value = [bindings objectForKey:key];
value = (value == [NSNull null]) ? nil : value;
[_bindings setObject:[MLKBinding bindingWithValue:value]
forKey:key];
}
}
-(void) addValue:(id)value forSymbol:(MLKSymbol *)symbol;
{
symbol = symbol ? (id)symbol : (id)[NSNull null];
[_bindings setObject:[MLKBinding bindingWithValue:value]
forKey:symbol];
}
-(void) addBindingForSymbol:(MLKSymbol *)symbol
{
[self addBinding:[MLKBinding binding] forSymbol:symbol];
}
-(void) addBinding:(MLKBinding *)binding forSymbol:(MLKSymbol *)symbol
{
[_bindings setObject:binding
forKey:(symbol ? (id)symbol : (id)[NSNull null])];
}
-(void) setBinding:(MLKBinding *)binding forSymbol:(MLKSymbol *)symbol
{
if (![self bindingForSymbol:symbol])
@throw LAUTORELEASE ([[MLKUnboundVariableError alloc]
initWithSymbol:symbol
inEnvironment:self]);
[self addBinding:binding forSymbol:symbol];
}
-(MLKEnvironment *) environmentForSymbol:(MLKSymbol *)symbol
{
if ([_bindings objectForKey:symbol])
return self;
else if (_parent)
return [_parent environmentForSymbol:symbol];
else
return nil;
}
-(BOOL) boundp:(MLKSymbol *)symbol
{
MLKBinding *binding;
if ((binding = [self bindingForSymbol:symbol]))
return [binding boundp];
else
return NO;
}
-(void) makunbound:(MLKSymbol *)symbol
{
MLKBinding *binding;
if ((binding = [self bindingForSymbol:symbol]))
[binding makunbound];
}
-(void) dealloc
{
LRELEASE (_bindings);
LRELEASE (_parent);
[super dealloc];
}
@end