-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
240 lines (230 loc) · 6.96 KB
/
index.test.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import {
CESKM,
Env,
Store,
scalar,
parseCbpv,
cbpv_lit,
cbpv_op,
cbpv_is_positive
} from "./src/index";
import type { State, Value, Cbpv } from "./src/index";
import { test } from "tap";
// eslint-disable-next-line
type Val = number | boolean | null;
class DebugMachine<Val> extends CESKM<Val> {
constructor() {
super(
class extends Store<Val> {
constructor(protected store: { [addr: string]: Value<Val> } = {}) {
super(store);
this.bind("test-addr-1", scalar(1 as unknown as Val));
this.bind("test-addr-2", scalar(true as unknown as Val));
}
},
class extends Env {
constructor(protected env: { [name: string]: string | Cbpv } = {}) {
super(env);
this.bind("x", "test-addr-1");
this.bind("y", "test-addr-2");
}
}
);
}
public inject(control: Cbpv): State<Val> {
return {
...super.inject(control)
};
}
public run(program: string): Value<Val> {
return this.runCbpv(parseCbpv(program));
}
public runCbpv(expr: Cbpv): Value<Val> {
let result = this.step(this.inject(expr));
while (!result.done) {
result = this.step(result.value as State<Val>);
}
return result.value;
}
protected literal(v: Val): Value<Val> {
if ("number" === typeof v || "boolean" === typeof v || null === v) {
return scalar(v);
}
throw new Error(`${v} not a primitive value`);
}
protected op(op_sym: string, args: Value<Val>[]): Value<Val> {
switch (op_sym) {
case "op:mul": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v) {
throw new Error("arguments must be numbers");
}
const result: unknown = args[0].v * args[1].v;
return scalar(result as Val);
}
case "op:add": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v) {
throw new Error("arguments must be numbers");
}
const result: unknown = args[0].v + args[1].v;
return scalar(result as Val);
}
case "op:sub": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v) {
throw new Error("arguments must be numbers");
}
const result: unknown = args[0].v - args[1].v;
return scalar(result as Val);
}
case "op:div": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v) {
throw new Error("arguments must be numbers");
}
const result: unknown = args[0].v / args[1].v;
return scalar(result as Val);
}
case "op:mod": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v) {
throw new Error("arguments must be numbers");
}
const result: unknown = args[0].v % args[1].v;
return scalar(result as Val);
}
case "op:eq": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if (
("number" !== typeof args[0].v || "number" !== typeof args[1].v) &&
("boolean" !== typeof args[0].v || "boolean" !== typeof args[1].v)
) {
throw new Error("arguments must be numbers or booleans");
}
const result: unknown = args[0].v === args[1].v;
return scalar(result as Val);
}
case "op:lt": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v) {
throw new Error("arguments must be numbers");
}
const result: unknown = args[0].v < args[1].v;
return scalar(result as Val);
}
case "op:and": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("boolean" !== typeof args[0].v || "boolean" !== typeof args[1].v) {
throw new Error("arguments must be booleans");
}
const result: unknown = args[0].v && args[1].v;
return scalar(result as Val);
}
case "op:or": {
if (!("v" in args[0]) || !("v" in args[1])) {
throw new Error("arguments must be values");
}
if ("boolean" !== typeof args[0].v || "boolean" !== typeof args[1].v) {
throw new Error("arguments must be booleans");
}
const result: unknown = args[0].v || args[1].v;
return scalar(result as Val);
}
case "op:not": {
if (!("v" in args[0])) {
throw new Error("argument must be a value");
}
if ("boolean" !== typeof args[0].v) {
throw new Error("argument must be a boolean");
}
const result: unknown = !args[0].v;
return scalar(result as Val);
}
default:
return super.op(op_sym, args);
}
}
}
test("grammar: cbpv_is_positive", (t) => {
t.ok(cbpv_is_positive(cbpv_lit(1)));
t.ok(cbpv_is_positive(cbpv_op("op:test", [])));
t.end();
});
test("ceskm: evaluates literals correctly", (t) => {
const vm = new DebugMachine();
t.same(vm.run("#t"), {
v: true
});
t.same(vm.run("1"), {
v: 1
});
t.end();
});
test("ceskm: correctly transforms state on each step", (t) => {
const vm = new DebugMachine();
t.same(vm.run("1"), { v: 1 });
t.same(vm.run("y"), { v: true });
t.same(vm.run("(if #t 1 2)"), { v: 1 });
t.same(vm.run("(if #f 1 2)"), { v: 2 });
t.same(vm.run("(shift k k)"), { k: {} });
t.same(vm.run("(let z #t z)"), { v: true });
t.same(vm.run("(letrec ((a 4)) (? a))"), { v: 4 });
t.same(vm.run("(? 1)"), { v: 1 });
t.same(vm.run("( (λ (n) n) #f )"), { v: false });
t.same(vm.run("(reset #t)"), { v: true });
t.same(vm.run("(! #t)"), { v: true });
t.same(vm.run("_"), { k: {} });
t.throws(() => {
vm.run("(op:not-implemented 1)");
});
t.throws(() => {
vm.run("(if (! (λ (n) n)) 1 2)");
});
t.throws(() => {
vm.run("(if 5 1 2)");
});
t.throws(() => {
vm.run("(λ (n) n)");
});
t.throws(() => {
vm.run("((λ (n) n) (? 1))");
});
t.same(vm.run("(! (λ (n) n))"), {
k: {
_let: [],
_exp: {
tag: "cbpv_abstract",
args: ["n"],
body: {
tag: "cbpv_symbol",
v: "n"
}
},
_env: {
env: {
x: "test-addr-1",
y: "test-addr-2"
}
},
_k: {}
}
});
t.end();
});