summaryrefslogtreecommitdiff
path: root/MLKEnvironment.m
blob: 8488dd20ad49b31af6b833388666cbc1def3e5dc (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
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
/* -*- mode: objc; coding: utf-8 -*- */
/* Étoilisp/Mulklisp, 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/NSNull.h>

#import "MLKEnvironment.h"
#import "MLKUndefinedVariableException.h"


static id UNBOUND;


@implementation MLKEnvironment
+(void) initialize
{
  UNBOUND = [[NSObject alloc] init];
}

-(MLKEnvironment *) init
{
  return [self initWithParent:nil bindings:nil];
}

-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent bindings:(NSDictionary *)bindings
{
  self = [super init];
  _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];
}

-(MLKEnvironment *) parent
{
  return _parent;
}

-(void) setValue:(id)value forBinding:(MLKSymbol *)symbol;
{
  [self setBinding:symbol to:value inEnvironment:self];
}

-(void) setBinding:(MLKSymbol *)symbol to:(id)value inEnvironment:(MLKEnvironment *)env
{
  if ([_bindings objectForKey: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
{
  id value;
  if ((value = [_bindings objectForKey:symbol]))
    {
      if (value == [NSNull null])
        return nil;
      else if (value == UNBOUND)
        [[[MLKUndefinedVariableException alloc] initWithEnvironment:env
                                                variableName:symbol]
          raise];
      else
        return value;
    }
  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) addValue:(id)value forBinding:(MLKSymbol *)symbol;
{
  [_bindings setObject:value forKey:symbol];
}

-(void) addBinding:(MLKSymbol *)symbol
{
  [_bindings setObject:UNBOUND forKey:symbol];
}

-(MLKEnvironment *) environmentForBinding:(MLKSymbol *)symbol
{
  if ([_bindings objectForKey:symbol])
    return self;
  else if (_parent)
    return [_parent environmentForBinding:symbol];
  else
    return nil;
}

-(BOOL) boundp:(MLKSymbol *)symbol
{
  id value;
  if ((value = [_bindings objectForKey:symbol]))
    return (value != UNBOUND);
  else if (_parent)
    return [_parent boundp:symbol];
  else
    return NO;
}

-(void) makunbound:(MLKSymbol *)symbol
{
  if ([_bindings objectForKey:symbol])
    [_bindings setObject:UNBOUND forKey:symbol];
  else if (_parent)
    return [_parent makunbound:symbol];
}

-(void) dealloc
{
  RELEASE (_bindings);
  RELEASE (_parent);
  [super dealloc];
}
@end