-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.js
50 lines (42 loc) · 978 Bytes
/
machine.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 Env from "./env.js";
import Heap from "./heap.js";
import Stack from "./stack.js";
export class Machine {
#e;
#s;
#k;
constructor(e, s, k) {
this.#e = e;
this.#s = s;
this.#k = k;
}
toString() {
const e = this.#e;
const s = this.#s;
const k = this.#k;
return `{e: ${e}, s: ${s}, k: ${k}}`;
}
static save(m) {
let e = m.#e;
let k = m.#k;
k = Stack.cons(e, k);
return new Machine(e, m.#s, k);
}
static restore(m) {
let e = m.#e;
let k = m.#k;
([e, k] = Stack.pop(k));
return new Machine(e, m.#s, k);
}
static set(name, value) {
const setter = Env.set(name, value);
return m => {
return new Machine(setter(m.#e), m.#s, m.#k);
};
}
static get(name) {
const getter = Env.get(name);
return m => getter(m.#e);
}
}
export default Machine;