-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugger.js
372 lines (325 loc) · 11.4 KB
/
Debugger.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
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
"use strict";
const DEFAULT_CELL_WIDTH = 256;
export const TokenType = Object.freeze({
// Standard Brainfuck commands
"BF_LOOP_OPEN": 1,
"BF_LOOP_CLOSE": 2,
"BF_ADD": 3,
"BF_SHIFT": 4,
"BF_OUTPUT": 5,
"BF_INPUT": 6,
// Represents a [-] like construct
// Automatically brings the value the pointer is pointing to to zero
"BF_ZERO": 7,
// Cushioning for the beginning and end of the token array
"BF_START": 8,
"BF_END": 9,
});
/*
* Stack structure that keeps records of what was popped
* When pushing, if there is a previously popped value,
* ignore the value the user wants to push, and push
* the historical value instead
*/
class EfficientStack {
constructor() {
this._internal_stack = []
this.clear();
}
clear() {
this._internal_stack.length = 0;
this._stack_pointer = 0;
}
push(val) {
if (this._stack_pointer > this._internal_stack.length) {
throw ("EfficientStack: can't push: sp>stack length");
}
if (this._stack_pointer === this._internal_stack.length) {
this._internal_stack.push(val)
}
this._stack_pointer++;
}
pop() {
if (this._stack_pointer === 0) {
throw ("EfficientStack: can't pop: nothing to pop");
}
this._stack_pointer--;
return this._internal_stack[this._stack_pointer];
}
get length() {
return this._stack_pointer;
}
}
// Adapted from Property404/dbfi/src/interpret.c
// See original source for helpful comments or lack thereof
function tokenize(source, optimize = true) {
let line_number = 0;
let column = 0;
let token_index = 0;
const tokens = [];
const skip_stack = [];
for (let i = 0; i < source.length; i++) {
const character = source[i];
if ("+-<>[].,".includes(character)) {
const new_token = {
type: null,
value: 1,
start: i,
line: line_number,
column: column
};
new_token.character = character;
switch (character) {
case "[":
// Optimize out [-] and [+] into one token
if (optimize && (source[i + 1] === "-" || source[i + 1] === "+") && source[i + 2] === "]") {
new_token.type = TokenType.BF_ZERO;
new_token.value_stack = [];
new_token.is_positive = (source[i + 1] === '+');
i += 2;
column += 2;
} else {
new_token.type = TokenType.BF_LOOP_OPEN;
skip_stack.push(token_index);
new_token.pc_stack = new EfficientStack();
}
break;
case "]":
new_token.type = TokenType.BF_LOOP_CLOSE;
new_token.pc_stack = new EfficientStack();
// [ and ] need to be mated
new_token.partner = skip_stack.pop();
if (
new_token.partner === undefined
) {
throw (`Unmatched ']' at line ${line_number+1} column ${column+1}`);
}
tokens[new_token.partner].partner = token_index;
break;
case '-':
case '+':
new_token.type = TokenType.BF_ADD;
new_token.value = (character == '+' ? 1 : -1);
break;
case '<':
case '>':
new_token.type = TokenType.BF_SHIFT;
new_token.value = (character == '>' ? 1 : -1);
break;
case '.':
new_token.type = TokenType.BF_OUTPUT;
break;
case ',':
new_token.type = TokenType.BF_INPUT;
new_token.value_stack = [];
break;
default:
break;
}
// Potentially condense series of <<<< >>>> ++++ or ----
if (optimize && token_index > 0 && new_token.type == tokens[tokens.length - 1].type && [TokenType.BF_SHIFT, TokenType.BF_ADD].indexOf(new_token.type) != -1) {
tokens[tokens.length - 1].value += new_token.value;
} else {
tokens.push(new_token);
token_index++;
}
}
if (character === "\n") {
line_number++;
column = -1;
}
column++;
}
tokens.unshift({
type: TokenType.BF_START,
value: 0
});
tokens.push({
type: TokenType.BF_END
});
// Since we added a token at beginning, we have to shift now
for (const index in tokens) {
if (tokens[index].partner) {
tokens[index].partner += 1;
}
}
return tokens;
}
export class Debugger {
constructor(source) {
if (source)
this.load(source);
this.output_callback = () => {};
this.input_callback = () => {
return 0;
};
this.cell_width = DEFAULT_CELL_WIDTH;
this.optimize = true;
this.allow_wrapping = true;
this.allow_negative_pointer = false;
}
load(source) {
this.source = source;
this.tokens = tokenize(source, this.optimize);
this.tape = {
"0": 0
};
this.reset();
}
getPositionInSource() {
const res = this.tokens[this.pc].start;
if (res === undefined)
return -1;
return res;
}
// Get a unique-ish integer valued tied to our current state
getStateHash() {
let total = 0;
for (let i = 0; i < 1000; i++) {
if (this.tape[i])
total += this.tape[i];
}
total += this.pointer * 100000;
let pcval = this.tokens[this.pc].type;
total += 1000 * pcval;
return total;
}
atEnd() {
if (!this.tokens) return false;
return this.pc >= this.tokens.length || this.tokens[this.pc].type == TokenType.BF_END;
}
atBeginning() {
if (!this.tokens) return true;
return this.pc == 0;
}
reset() {
this.last_pc = 0;
this.pc = 0;
this.pointer = 0;
for (const i in this.tape) {
this.tape[i] = 0;
}
for (const token of this.tokens) {
if (token.value_stack)
token.value_stack.length = 0;
if (token.pc_stack !== undefined) {
token.pc_stack.clear();
token.pc_stack.push(0);
}
}
}
step(reverse = false) {
let stepagain = false;
if (reverse)
this.pc--;
let pc_going_forth = this.pc;
if (this.pc < 0 || this.pc > this.tokens.length) {
throw ("Program counter out of bounds (pc = " + this.pc + ")");
}
const token = this.tokens[this.pc];
if (this.tape[this.pointer] == undefined)
this.tape[this.pointer] = 0;
if (token == undefined) {
throw "Found undefined token";
}
switch (token.type) {
case TokenType.BF_ZERO:
if (reverse) {
this.tape[this.pointer] = token.value_stack.pop();
} else {
token.value_stack.push(this.tape[this.pointer]);
if (this.tape[this.pointer] && !this.allow_wrapping && token.is_positive) {
throw ("[+]-type construct throws cell value out of bounds");
}
this.tape[this.pointer] = 0;
}
break;
case TokenType.BF_ADD:
if (reverse)
this.tape[this.pointer] -= token.value;
else
this.tape[this.pointer] += token.value;
if (
this.tape[this.pointer] < 0 ||
this.tape[this.pointer] >= this.cell_width
) {
if (!this.allow_wrapping) throw ("Cell value out of bounds");
this.tape[this.pointer] %= this.cell_width;
if (this.tape[this.pointer] < 0)
this.tape[this.pointer] = this.cell_width + this.tape[this.pointer];
}
break;
case TokenType.BF_SHIFT:
if (reverse)
this.pointer -= token.value;
else
this.pointer += token.value;
if (this.pointer < 0 && !this.allow_negative_pointer)
throw (`Pointer out of bounds(pointer=${this.pointer}, direction=${reverse?"reverse":"forward"}) at line ${token.line+1} column ${token.column+1}`);
break;
case TokenType.BF_INPUT:
if (!reverse) {
const old_val = this.tape[this.pointer];
const new_val = this.input_callback();
if (new_val === null) {
// Effectively do nothing, not even move PC
// This is to allow controller.js to get input from user
return;
} else if (Number.isInteger(new_val)) {
this.tape[this.pointer] = new_val;
token.value_stack.push(old_val);
} else {
throw "Debugger expected integer input(eg an ASCII value) but got: " + new_val;
}
} else {
this.tape[this.pointer] = token.value_stack.pop();
}
break;
case TokenType.BF_OUTPUT:
if (!reverse) {
const val = this.tape[this.pointer];
const ch = String.fromCharCode(val);
this.output_callback(ch);
}
break;
case TokenType.BF_LOOP_OPEN:
case TokenType.BF_LOOP_CLOSE:
if (!reverse) {
token.pc_stack.push(this.last_pc + 1);
if (
(token.type === TokenType.BF_LOOP_CLOSE && this.tape[this.pointer]) ||
(token.type === TokenType.BF_LOOP_OPEN && !this.tape[this.pointer])
) {
this.tokens[token.partner].pc_stack.push(this.pc);
this.pc = token.partner;
}
} else {
this.pc = token.pc_stack.pop();
if (this.pc == token.partner) {
this.tokens[token.partner].pc_stack.pop();
}
}
break;
case TokenType.BF_START:
case TokenType.BF_END:
break;
default:
throw "Found unknown token";
}
if (!reverse) {
this.pc++;
if (this.pc >= this.tokens.length) {
throw ("Program counter out of bounds (pc = " + this.pc + ")");
}
}
this.last_pc = pc_going_forth;
if (stepagain)
this.step(reverse);
}
get current_value() {
return this.tape[this.pointer];
}
set current_value(val) {
this.tape[this.pointer] = val;
}
}