summaryrefslogtreecommitdiff
path: root/MLKEnvironment.m
blob: 56bafb7315934328b2b883744382e34c80f51173 (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
/* -*- mode: objc; coding: utf-8 -*- */
/* Copyright 2008, Matthias Benkard. */

#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>

#import "MLKEnvironment.h"
#import "MLKLinkedList.h"
#import "MLKCons.h"
#import "MLKUndefinedVariableException.h"


@implementation MLKEnvironment
-(MLKEnvironment *) init
{
  _bindings = [[MLKLinkedList alloc] init];
  return self;
}

-(MLKEnvironment *) initWithParent:(MLKEnvironment *)parent
{
  ASSIGN (_bindings, parent->_bindings);
  return self;
}

-(void) setBinding:(MLKSymbol *)symbol to:(id)value
{
  MLKCons *cons;
  for (cons = [_bindings firstCons]; cons; cons = [cons cdr])
    {
      NSMutableDictionary *dict = [cons car];
      if ([[dict allKeys] containsObject:symbol])
        {
          [dict setObject:value forKey:symbol];
          break;
        }
    }
  
  [[[MLKUndefinedVariableException alloc] initWithEnvironment: self
                                          variableName: symbol]
    raise];
}

-(id) valueForBinding:(MLKSymbol *)symbol
{
  MLKCons *cons;
  for (cons = [_bindings firstCons]; cons; cons = [cons cdr])
    {
      NSMutableDictionary *dict = [cons car];
      if ([[dict allKeys] containsObject:symbol])
        {
          return [dict objectForKey:symbol];
        }
    }

  [[[MLKUndefinedVariableException alloc] initWithEnvironment: self
                                          variableName: symbol]
    raise];
  return nil;
}
@end