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