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