-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.test.ts
444 lines (350 loc) · 9.94 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import { expect, test } from "bun:test";
import { iserr } from "variants-ts";
import { actionsNameToBinary, registerNameToBinary } from "./src/common";
import { compile, init } from "./src/compile";
import { run } from "./src/run";
test("should compile `move`", () => {
const result = compile("move r0 r1");
if (iserr(result)) {
throw new Error(JSON.stringify(result.data));
}
expect(result.data.memory[0]).toBe(actionsNameToBinary.move_reg_reg);
expect(result.data.memory[1]).toBe(registerNameToBinary.r0);
expect(result.data.memory[2]).toBe(registerNameToBinary.r1);
});
test("should compile `move` with literal", () => {
const result = compile("move r0 101");
if (iserr(result)) {
throw new Error(JSON.stringify(result.data));
}
expect(result.data.memory[0]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[1]).toEqual(registerNameToBinary.r0);
expect(result.data.memory[2]).toEqual(5);
});
test("should compile `move` with dereference", () => {
const result = compile("move r0 *r1");
if (iserr(result)) {
throw new Error(JSON.stringify(result.data));
}
expect(result.data.memory[0]).toEqual(actionsNameToBinary.move_reg_dreg);
expect(result.data.memory[1]).toEqual(registerNameToBinary.r0);
expect(result.data.memory[2]).toEqual(registerNameToBinary.r1);
});
test("should compile example 1", () => {
const result = compile(`
move r0 11
move r1 10
add r0 r1
`);
if (iserr(result)) {
throw new Error(JSON.stringify(result.data));
}
expect(result.data.memory[0]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[1]).toEqual(registerNameToBinary.r0);
expect(result.data.memory[2]).toEqual(3);
expect(result.data.memory[3]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[4]).toEqual(registerNameToBinary.r1);
expect(result.data.memory[5]).toEqual(2);
expect(result.data.memory[6]).toEqual(actionsNameToBinary.add_reg);
expect(result.data.memory[7]).toEqual(registerNameToBinary.r0);
expect(result.data.memory[8]).toEqual(registerNameToBinary.r1);
});
test("should compile example 2", () => {
const result = compile(`
# Define a start label
!main
# Initialize variables
move r1 101
move r2 1
# Loop to calculate factorial
!loop
mul r2 r1
add r1 1111111111111111
cmp r1 0
jumpeq @end
jump @loop
# Print the result
!end
push r2
call <print
pop r2
# Halt the program
halt
# Define a print procedure
>print
pop r1
move r3 r1
move r4 0
# Loop to print each digit
!print_loop
div r3 1010
mod r3 1010
add r2 110000
push r2
add r4 1
cmp r3 0
jumpeq @print_end
jump @print_loop
# Print the digits from the stack
!print_end
cmp r4 0
jumpeq @print_done
add r4 1111111111111111
pop r2
# Print the ASCII digit (you can implement this part)
jump @print_end
# End of print procedure
!print_done
ret
`);
if (iserr(result)) {
throw new Error(JSON.stringify(result.data));
}
expect(result.data.memory[0]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[1]).toEqual(registerNameToBinary.r1);
expect(result.data.memory[2]).toEqual(5);
expect(result.data.memory[3]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[4]).toEqual(registerNameToBinary.r2);
expect(result.data.memory[5]).toEqual(1);
});
test("should compile and run example3", () => {
const result = compile(`
# Define a start label
!main
move r0 0
move r1 101
move r2 1
!loop
add r1 r2
cmp r1 1010
jumpeq @end
jump @loop
!end
halt
`);
if (iserr(result)) {
throw new Error(JSON.stringify(result.data));
}
expect(result.data.memory[0]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[1]).toEqual(registerNameToBinary.r0);
expect(result.data.memory[2]).toEqual(0);
expect(result.data.memory[3]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[4]).toEqual(registerNameToBinary.r1);
expect(result.data.memory[5]).toEqual(5);
expect(result.data.memory[6]).toEqual(actionsNameToBinary.move_reg_lit);
expect(result.data.memory[7]).toEqual(registerNameToBinary.r2);
expect(result.data.memory[8]).toEqual(1);
expect(result.data.memory[9]).toEqual(actionsNameToBinary.add_reg);
expect(result.data.memory[10]).toEqual(registerNameToBinary.r1);
expect(result.data.memory[11]).toEqual(registerNameToBinary.r2);
expect(result.data.memory[12]).toEqual(actionsNameToBinary.cmp_reg_lit);
expect(result.data.memory[13]).toEqual(registerNameToBinary.r1);
expect(result.data.memory[14]).toEqual(10);
expect(result.data.memory[15]).toEqual(actionsNameToBinary.jumpeq);
expect(result.data.memory[16]).toEqual(19);
expect(result.data.memory[17]).toEqual(actionsNameToBinary.jump);
expect(result.data.memory[18]).toEqual(9);
expect(result.data.memory[19]).toEqual(actionsNameToBinary.halt);
const vm = init(result.data);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(0);
let line;
let ip;
// move r0 0
ip = vm.registers[registerNameToBinary.ip!];
line = vm.maps.get(ip);
expect(line).toEqual(4);
run(vm);
expect(vm.registers[registerNameToBinary.r0!]).toEqual(0);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(3);
// move r1 101
ip = vm.registers[registerNameToBinary.ip!];
line = vm.maps.get(ip);
expect(line).toEqual(5);
run(vm);
expect(vm.registers[registerNameToBinary.r1!]).toEqual(5);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(6);
// move r2 1
run(vm);
expect(vm.registers[registerNameToBinary.r2!]).toEqual(1);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(9);
// add r1 r2
run(vm);
expect(vm.registers[registerNameToBinary.r1!]).toEqual(6);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(12);
// cmp r0 1010
run(vm);
expect(vm.registers[registerNameToBinary.r0!]).toEqual(0);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(15);
// jumpeq @end (noop)
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(17);
// jump @loop
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(9);
// add r1 r2
run(vm);
expect(vm.registers[registerNameToBinary.r1!]).toEqual(7);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(12);
// cmp r0 1010
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(15);
// jumpeq @end (noop)
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(17);
// jump @loop
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(9);
// add r1 r2
run(vm);
expect(vm.registers[registerNameToBinary.r1!]).toEqual(8);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(12);
// cmp r0 1010
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(15);
// jumpeq @end (noop)
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(17);
// jump @loop
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(9);
// add r1 r2
run(vm);
expect(vm.registers[registerNameToBinary.r1!]).toEqual(9);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(12);
// cmp r0 1010
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(15);
// jumpeq @end (noop)
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(17);
// jump @loop
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(9);
// add r1 r2
run(vm);
expect(vm.registers[registerNameToBinary.r1!]).toEqual(10);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(12);
// cmp r1 1010
run(vm);
expect(vm.registers[registerNameToBinary.cr!]).toEqual(0);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(15);
// jumpeq @end
ip = vm.registers[registerNameToBinary.ip!];
line = vm.maps.get(ip);
expect(line).toEqual(10);
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(19);
// halt
const { shouldHalt } = run(vm);
expect(shouldHalt).toEqual(true);
});
test("should overflow on add", () => {
const compiled = compile(`
move r0 1
move r1 1111111111111111
add r0 r1
`);
if (iserr(compiled)) {
throw new Error(JSON.stringify(compiled.data));
}
const vm = init(compiled.data);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(0);
run(vm);
run(vm);
run(vm);
expect(vm.registers[registerNameToBinary.r0!]).toEqual(0);
expect(vm.registers[registerNameToBinary.cr!]).toEqual(1);
});
test("should jumpge on equal", () => {
const compiled = compile(`
!loop
move r0 1
move r1 1111111111111111
add r0 r1
cmp r0 0
jumpge @end
jump @loop
!end
halt
`);
if (iserr(compiled)) {
throw new Error(JSON.stringify(compiled.data));
}
const vm = init(compiled.data);
// move r0 1
run(vm);
// move r1 1111111111111111
run(vm);
// add r0 r1
run(vm);
// cmp r0 0
run(vm);
expect(vm.registers[registerNameToBinary.cr!]).toEqual(0);
// jumpge @end
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(16);
run(vm);
run(vm);
});
test("should jumpge on greater", () => {
const compiled = compile(`
!loop
move r0 1010
move r1 11111
add r0 r1
cmp r0 10
jumpge @end
jump @loop
!end
halt
`);
if (iserr(compiled)) {
throw new Error(JSON.stringify(compiled.data));
}
const vm = init(compiled.data);
// move r0 1010
run(vm);
// move r1 11111
run(vm);
// add r0 r1
run(vm);
// cmp r0 10
run(vm);
expect(vm.registers[registerNameToBinary.cr!]).toBeGreaterThan(0);
// jumpge @end
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(16);
run(vm);
run(vm);
});
test("should not jumpge on less", () => {
const compiled = compile(`
!loop
move r0 1010
move r1 11111
add r0 r1
cmp r0 1111111
jumpge @end
jump @loop
!end
halt
`);
if (iserr(compiled)) {
throw new Error(JSON.stringify(compiled.data));
}
const vm = init(compiled.data);
// move r0 1010
run(vm);
// move r1 11111
run(vm);
// add r0 r1
run(vm);
// cmp r0 10
run(vm);
expect(vm.registers[registerNameToBinary.cr!]).toEqual(0b1111_1111_1111_1111);
// jumpge @end
run(vm);
expect(vm.registers[registerNameToBinary.ip!]).toEqual(14);
run(vm);
run(vm);
});