-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.js
173 lines (147 loc) · 5.88 KB
/
executor.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
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
var cells = new Uint8Array(2048);
var cellContainer = document.querySelector("#cells");
var scriptContent = document.querySelector("#script");
var outputContent = document.querySelector("#output");
var startButton = document.querySelector("#start");
var insertButton = document.querySelector("#insert");
var inputChars = [];
var executed = true;
var executing = false;
var duration = 500;
var delay = ms => new Promise(r => setTimeout(r, ms));
script.value = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
if (location.search) {
duration = location.search.replace(/\D+/, "") * 1 || duration;
}
with (EventTarget) prototype.on = prototype.addEventListener;
// location.hash = "#cell-0";
[...cells].map((v, i) =>
Object.assign(document.createElement("div"), {
id: `cell-${i}`,
title: `cell: ${i}`,
className: "cell",
innerText: "0"
})
).forEach(el => cellContainer.appendChild(el));
async function execute() {
if (!executed) return;
executed = false;
var { value: script } = scriptContent;
scriptContent.readOnly = true;
executing = true;
outputContent.value = "";
startButton.innerText = "stop"
cells.forEach((v, i) => {
cells[i] = 0;
cellContainer.childNodes[i].innerText = "0";
cellContainer.childNodes[i].className = "cell";
});
cellContainer.childNodes[0].className = "cell active";
// location.hash = "#cell-0";
var dataPointer = 0;
for (var commandPointer = 0; commandPointer < script.length; commandPointer++) {
if (duration) await delay(duration);
if (duration) scriptContent.focus();
if (duration) scriptContent.selectionStart = commandPointer;
if (duration) scriptContent.selectionEnd = commandPointer + 1;
switch (script[commandPointer]) {
case "+":
cells[dataPointer]++;
if (duration) cellContainer.childNodes[dataPointer].innerText = cells[dataPointer];
break;
case "-":
cells[dataPointer]--;
if (duration) cellContainer.childNodes[dataPointer].innerText = cells[dataPointer];
break;
case ">":
if (duration) cellContainer.childNodes[dataPointer].className = "cell";
dataPointer++;
if (duration) cellContainer.childNodes[dataPointer].className = "cell active";
// location.hash = `#cell-${dataPointer}`;
break;
case "<":
if (duration) cellContainer.childNodes[dataPointer].className = "cell";
dataPointer--;
if (duration) cellContainer.childNodes[dataPointer].className = "cell active";
// location.hash = `#cell-${dataPointer}`;
break;
case ",":
var char = inputChars.pop() || prompt("input one char, cancel can input code");
if (char === null) {
char = prompt("input code: (0-255)");
if (char === null) char = 255;
else char *= 1;
} else char = char.charCodeAt();
cells[dataPointer] = char;
if (duration) cellContainer.childNodes[dataPointer].innerText = cells[dataPointer];
break;
case ".":
outputContent.value += String.fromCharCode(cells[dataPointer]);
break;
case "[":
if (!cells[dataPointer]) {
var level = 1;
for (var i = commandPointer + 1; i < script.length; i++) {
if (script[i] == "[") level++;
else if (script[i] == "]") level--;
if (level == 0) {
commandPointer = i - 1;
break;
}
}
}
break;
case "]":
if (cells[dataPointer]) {
var level = 1;
for (var i = commandPointer - 1; i > -1; i--) {
if (script[i] == "]") level++;
else if (script[i] == "[") level--;
if (level == 0) {
commandPointer = i - 1;
break;
}
}
}
break;
default:
for (var i = commandPointer + 1; i < script.length; i++) {
if (["+", "-", ">", "<", ",", ".", "[", "]"].indexOf(script[i]) > -1) {
commandPointer = i - 1;
break;
}
}
}
if (!executing) break;
}
executing = false;
executed = true;
scriptContent.readOnly = false;
startButton.innerText = "start"
}
window.on("keydown", e => {
var { ctrlKey, keyCode } = e;
if (ctrlKey) {
switch (keyCode) {
case 83:
e.preventDefault();
if (startButton.innerText == "start") execute();
else executing = false;
break;
case 45:
inputChars = prompt("default input chars");
if (inputChars) inputChars = inputChars.split("").reverse();
else inputChars = [];
break;
}
}
});
startButton.on("click", e => {
if (startButton.innerText == "stop") executing = false;
else execute();
});
insertButton.on("click", e => {
inputChars = prompt("default input chars");
if (inputChars) inputChars = inputChars.split("").reverse();
else inputChars = [];
});