-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
108 lines (95 loc) · 1.9 KB
/
basic.js
File metadata and controls
108 lines (95 loc) · 1.9 KB
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
'use strict';
const { table } = require('table');
const { TIS, Node, StackMemoryNode } = require('.');
const left = new Node(`
ADD 1
MOV ACC, RIGHT
`.trim());
const right = new Node(`
START:
MOV LEFT, ACC
SAV
CHECK:
SUB 2
JLZ START
JEZ EVEN
JMP CHECK
EVEN:
SWP
MOV ACC, DOWN
`.trim());
const stack = new StackMemoryNode();
const tis = new TIS([
[left, right],
[null, stack],
]);
const DISABLED_NODE = ` Disabled Node X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X`;
let steps = 0;
let lastBlocked = false;
setInterval(() => {
if (tis.step()) {
if (lastBlocked) {
throw new Error('All nodes blocked');
} else {
lastBlocked = true;
}
} else {
lastBlocked = false;
}
steps += 1;
const options = {
columns: {},
};
const cells = tis.grid.map((row) =>
row.map((node, column) => {
options.columns[column] = { width: 22 };
if (node === null) {
return DISABLED_NODE;
}
const lines = [
node.name,
`ACC: ${node.ACC} | BAK: ${node.BAK}${node.blocked ? ' | BL' : ''}`,
'',
];
if (node.stack) {
node.stack.forEach((item, i) => {
if (i === node.stack.length - 1) {
lines.push(`> ${item}`);
} else {
lines.push(` ${item}`);
}
});
} else {
node.source
.split('\n')
.forEach((l, i) => {
if (i === node.line) {
lines.push(`> ${l}`);
} else {
lines.push(` ${l}`);
}
});
}
return Object.assign(Array.from({ length: 17 }), lines).join('\n');
}));
console.clear();
console.log(`TIS-100 (JavaScript) Step #${steps}`);
console.log(table(cells, options));
}, 45);