-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit.js
43 lines (36 loc) · 1.09 KB
/
jit.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
import * as Kont from "./kont.js";
import * as Instr from "./instr.js";
// Not really jitting. Knitting together closures but eh
const get = f => s => new Kont.Step(f(s), s);
const put = (newS, c) => s => new Kont.Step(c, newS);
const halt = value => s => new Kont.Halt(value);
const modify = (f, c) => s => new Kont.Step(c, f(s));
const fragment = (f, c) => s => {
const { state, value} = f(s);
return new Kont.Step(c(value), state);
};
export const jit = t => {
switch (true) {
case (t instanceof Instr.Get): {
const { next } = t
return get(s => jit(next(s)));
}
case (t instanceof Instr.Put): {
const { value, next } = t
return put(value, jit(next));
}
case (t instanceof Instr.Modify): {
const { f, next } = t
return modify(f, jit(next));
}
case (t instanceof Instr.Fragment): {
const { f, next } = t
return fragment(f, s => jit(next(s)));
}
case (t instanceof Instr.Halt):
return halt(t.value);
default:
throw new Error(`Unknown instruction ${t}`);
}
}
export default jit;