-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environment.js
43 lines (43 loc) · 910 Bytes
/
Environment.js
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
class Environment {
constructor(scope, parent) {
this.scope = scope;
this.parent = parent;
}
resolve(name) {
var resolution = this.scope.resolve(name);
if (resolution) {
var [value, key] = resolution;
return [value, [0, key]];
}
if (this.parent) {
var resolution = this.parent.resolve(name);
if (resolution) {
var [value, [depth, key]] = resolution;
return [value, [depth + 1, key]];
}
}
}
*[Symbol.iterator]() {
yield this.scope;
if (this.parent)
yield* this.parent;
}
find(f, filter) {
var depth = 0;
for (var scope of this) {
var result = scope.find(f, filter);
if (result) {
var [value, name, key] = result;
return [value, name, key, depth];
}
depth++;
}
}
ancestor(depth) {
return depth ?
this.parent.ancestor(depth - 1) :
this;
}
push(scope) { return new Environment(scope, this); }
}
module.exports = Environment;