-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.js
50 lines (44 loc) · 1.25 KB
/
env.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
44
45
46
47
48
49
50
import { error } from './utils';
import { Pointer } from './pointer';
export class Environment {
constructor(parent = null) {
this._parent = parent;
this._offset = 0;
this._offsets = Object.create(null);
}
push() { return new Environment(this); }
getOffset(name, throwIfNotFound=true) {
for (let fr = this; fr != null; fr = fr._parent)
if (name in fr._offsets)
return fr._offsets[name];
if (throwIfNotFound)
return error(`could not find ${name}`);
return -1;
}
setOffset(name, addr) {
this._offsets[name] = addr;
}
offset(name) {
if (name in this._offsets)
return this._offsets[name];
let rv = new Pointer();
this._offsets[name] = rv;
return rv;
/*
this._offsets[name] = this._offset++;
return this._offset - 1;
*/
}
toString() {
let e = this;
let str = 'Environment(';
while (e) {
if (e != this) str += '\n------';
for (let name in e._offsets) {
str += `\n ${name} = ${this._offsets[name]}`;
}
e = e._parent;
}
return str + ')';
}
}