OLD | NEW |
| (Empty) |
1 part of lisp; | |
2 | |
3 /** | |
4 * Environment of bindings. | |
5 */ | |
6 class Environment { | |
7 | |
8 /** The owning environment. */ | |
9 final Environment _owner; | |
10 | |
11 /** The internal environment bindings. */ | |
12 final Map<Name, dynamic> _bindings; | |
13 | |
14 /** Constructor for the nested environment. */ | |
15 Environment([this._owner]) : _bindings = new Map(); | |
16 | |
17 /** Constructor for a nested environment. */ | |
18 Environment create() => new Environment(this); | |
19 | |
20 /** Return the binding for [key]. */ | |
21 operator [](Name key) { | |
22 if (_bindings.containsKey(key)) { | |
23 return _bindings[key]; | |
24 } else if (_owner != null) { | |
25 return _owner[key]; | |
26 } else { | |
27 return _invalidBinding(key); | |
28 } | |
29 } | |
30 | |
31 /** Updates the binding for [key] with a [value]. */ | |
32 void operator []=(Name key, value) { | |
33 if (_bindings.containsKey(key)) { | |
34 _bindings[key] = value; | |
35 } else if (_owner != null) { | |
36 _owner[key] = value; | |
37 } else { | |
38 _invalidBinding(key); | |
39 } | |
40 } | |
41 | |
42 /** Defines a new binding from [key] to [value]. */ | |
43 define(Name key, value) { | |
44 return _bindings[key] = value; | |
45 } | |
46 | |
47 /** Returns the keys of the bindings. */ | |
48 Iterable<Name> get keys => _bindings.keys; | |
49 | |
50 /** Returns the parent of the bindings. */ | |
51 Environment get owner => _owner; | |
52 | |
53 /** Called when a missing binding is accessed. */ | |
54 _invalidBinding(Name key) { | |
55 throw new ArgumentError('Unknown binding for $key'); | |
56 } | |
57 } | |
OLD | NEW |