-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintepreter.js
271 lines (254 loc) · 7.94 KB
/
intepreter.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
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
var ptr = 0;
var mem_cell = []
var bck_trck = []
var inpt = "";
var inpt_prg = 0;
var time = 100;
var inter;
var progress;
var RegEx = /([+[>,.\]<-])+/g
var flagg = false;
var c_num;
var c_max = 30;
var c_counter = 1;
var is_running = false;
function start_run() {
is_running = true;
document.getElementById("output").value = ""
inpt = document.getElementById("input_box").value;
if (!inpt) inpt = "";
inpt_prog = 0;
ptr = 0;
flagg = true;
mem_cell = [];
progress = 0;
c_max = 30;
c_counter = 1;
str = ""
let tempstr = document.getElementById("code_block").value.match(RegEx)
for (let i = 0; i < tempstr.length; i++) {
str += tempstr[i]
}
}
function playpause() {
document.getElementById("rbt").disabled = true;
document.getElementById("plbt").disabled = true;
document.getElementById("ppbt").disabled = false;
document.getElementById("stbt").disabled = false;
document.getElementById("doon").disabled = true;
if (flagg) {
flagg = false;
clearInterval(inter)
} else {
flagg = true;
inter = setInterval(proc, time)
}
}
function slower() {
if (time <= 10000) time += 100;
if (flagg == true) {
clearInterval(inter);
inter = setInterval(proc, time);
}
document.getElementById("inter_num").value = time;
}
function faster() {
if (time > 100) time -= 100;
if (flagg == true) {
clearInterval(inter);
inter = setInterval(proc, time);
}
document.getElementById("inter_num").value = time;
}
function stop() {
document.getElementById("rbt").disabled = false;
document.getElementById("plbt").disabled = false;
document.getElementById("ppbt").disabled = true;
document.getElementById("stbt").disabled = true;
document.getElementById("doon").disabled = false;
clearInterval(inter);
flagg = false;
}
function begin() {
document.getElementById("rbt").disabled = false;
document.getElementById("plbt").disabled = false;
document.getElementById("ppbt").disabled = true;
document.getElementById("stbt").disabled = true;
document.getElementById("doon").disabled = false;
start_run();
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 3; j++) {
document.getElementById("celltable").rows[1 + j * 2].cells[i].innerHTML = 0;
}
}
c_counter = 1;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 10; j++) {
document.getElementById("celltable").rows[2 * i].cells[j].innerHTML = "cell#" + (c_counter);
c_counter++;
}
}
for (; flagg;) {
proc();
}
}
function play() {
document.getElementById("rbt").disabled = true;
document.getElementById("plbt").disabled = true;
document.getElementById("ppbt").disabled = false;
document.getElementById("stbt").disabled = false;
document.getElementById("doon").disabled = true;
start_run();
for (let ii = 0; ii < 10; ii++) {
for (let jj = 0; jj < 3; jj++) {
document.getElementById("celltable").rows[1 + jj * 2].cells[ii].innerHTML = 0;
}
}
c_counter = 1;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 10; j++) {
document.getElementById("celltable").rows[2 * i].cells[j].innerHTML = "cell#" + (c_counter);
c_counter++;
}
}
inter = setInterval(proc, time);
}
function do_once() {
if (!is_running) {
start_run();
proc();
} else {
proc();
}
}
function proc() {
if (progress >= str.length) {
clearInterval(inter);
flagg = false;
is_running = false;
document.getElementById("rbt").disabled = false;
document.getElementById("plbt").disabled = false;
document.getElementById("ppbt").disabled = true;
document.getElementById("stbt").disabled = true;
document.getElementById("doon").disabled = false;
return;
}
document.getElementById("c_cmd").innerHTML = "Current command: " + str[progress];
switch (str[progress]) {
case '+':
add();
c_num = parseInt(parseInt(ptr / 10) % 3);
document.getElementById("celltable").rows[1 + (2 * c_num)].cells[ptr % 10].innerHTML = mem_cell[ptr]
break;
case '-':
minus();
c_num = parseInt(parseInt(ptr / 10) % 3);
document.getElementById("celltable").rows[1 + (2 * c_num)].cells[ptr % 10].innerHTML = mem_cell[ptr]
break;
case '>':
shiftright();
break;
case '<':
shiftleft();
break;
case ',':
input();
c_num = parseInt(parseInt(ptr / 10) % 3);
document.getElementById("celltable").rows[1 + (2 * c_num)].cells[ptr % 10].innerHTML = mem_cell[ptr]
break;
case '.':
output();
break;
case '[':
start_loop();
break;
case ']':
end_loop();
break;
default:
break;
}
progress++;
}
function add() {
if (!(mem_cell[ptr])) mem_cell[ptr] = 0;
else if (mem_cell[ptr] == 255) mem_cell[ptr] = -1;
mem_cell[ptr]++;
}
function minus() {
if (!(mem_cell[ptr])) mem_cell[ptr] = 256;
else if (mem_cell[ptr] == 0) mem_cell[ptr] = 256;
mem_cell[ptr]--;
}
function shiftright() {
if (ptr == (c_max - 1)) {
c_counter = 1;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 10; j++) {
document.getElementById("celltable").rows[2 * i].cells[j].innerHTML = "cell#" + (c_max + c_counter);
c_counter++;
}
}
c_max += 30;
}
$('.currently_on').attr('class', '');
ptr++;
c_num = parseInt(parseInt(ptr / 10) % 3);
document.getElementById("celltable").rows[(2 * c_num)].cells[ptr % 10].setAttribute("class", "currently_on")
document.getElementById("celltable").rows[1 + (2 * c_num)].cells[ptr % 10].setAttribute("class", "currently_on")
}
function shiftleft() {
if (ptr <= 0) {
ptr = 0;
} else {
if (ptr == (c_max - 31)) {
c_max -= 60;
c_counter = 1;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 10; j++) {
document.getElementById("celltable").rows[2 * i].cells[j].innerHTML = "cell#" + (c_max + c_counter);
c_counter++;
}
}
c_max += 30;
}
$('.currently_on').attr('class', '');
ptr--;
c_num = parseInt(parseInt(ptr / 10) % 3);
document.getElementById("celltable").rows[(2 * c_num)].cells[ptr % 10].setAttribute("class", "currently_on")
document.getElementById("celltable").rows[1 + (2 * c_num)].cells[ptr % 10].setAttribute("class", "currently_on")
}
}
function input() {
if (inpt[inpt_prog] && inpt != "") {
mem_cell[ptr] = inpt.charCodeAt(inpt_prog)
} else {
mem_cell[ptr] = 0
return;
}
inpt_prog++;
}
function output() {
document.getElementById("output").value += String.fromCharCode(mem_cell[ptr])
}
function start_loop() {
if (mem_cell[ptr] ? mem_cell[ptr] == 0 ? true : false : true) {
let counter = 1;
for (progress = progress + 1; counter > 0; progress++) {
if (str[progress] == ']') counter--;
else if (str[progress] == '[') counter++;
}
progress--;
return;
} else {
bck_trck[bck_trck.length] = progress;
}
}
function end_loop() {
if (mem_cell[ptr] ? mem_cell[ptr] != 0 ? true : false : false) {
progress = bck_trck[(bck_trck.length) - 1];
} else {
bck_trck.pop()
return;
}
}