-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbg.js
85 lines (72 loc) · 2.75 KB
/
dbg.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
function Debugger() {
this.inp = new Interpreter(BF_INP.get.mems());
this.inp.init();
}
Debugger.prototype.start = function() {
BF_INP.button.disable('dbg-start');
BF_INP.button.enable('dbg-stop');
BF_INP.button.enable('dbg-run');
BF_INP.button.enable('dbg-step');
src_viewer.setValue(editor.getValue().BF());
src_viewer.clearSelection();
src_viewer.selection.setRange(new Range(0, 0, 0, 1));
this.update.mem(this.inp.data());
BF_INP.el.dbg.memp.style.display = 'inline';
}
Debugger.prototype.stop = function() {
BF_INP.button.enable('dbg-start');
BF_INP.button.disable('dbg-stop');
BF_INP.button.disable('dbg-run');
BF_INP.button.disable('dbg-step');
src_viewer.setValue('Source View'); src_viewer.clearSelection();
mem_viewer.setValue('Memory View'); mem_viewer.clearSelection();
BF_INP.el.dbg.memp.style.display = 'none';
BF_INP.el.dbg.eoc.style.display = 'none';
}
Debugger.prototype.run = function() {
let data;
if(BF_INP.dbg_mode) {
while (
(data = this.inp.data()).ip < data.code.length &&
data.code[data.ip] != '#'
) this.inp.step();
this.inp.skip();
}
else this.inp.run();
if(this.inp.eoc())
BF_INP.el.dbg.eoc.style.display = 'inline';
data = this.inp.data();
this.update.mem(data);
this.update.src_pointer(data);
BF_INP.el.dbg.ip.innerHtml = `ip: ${data.ip}`;
BF_INP.el.dbg.mp.innerHtml = `mp: ${data.mp}`;
}
Debugger.prototype.step = function() {
if(this.inp.eoc()) {
BF_INP.el.dbg.eoc.style.display = 'inline';
return;
}
let data, by = Math.max(Number(BF_INP.el.dbg.stepby.value), 1);
while (
by-- &&
(data = this.inp.data()).ip < data.code.length
) this.inp.step();
data = this.inp.data();
this.update.mem(data);
this.update.src_pointer(data);
BF_INP.el.dbg.ip.innerHtml = `ip: ${data.ip}`;
BF_INP.el.dbg.mp.innerHtml = `mp: ${data.mp}`;
}
Debugger.prototype.update = {
mem: (data) => {
let mem = data.mem.prev(5, data.mp).add(data.mp.pad(5)).add(data.mem.next(5, data.mp));
let mem_val = mem.map (mp => { return data.mem[Number(mp)].center(5); });
let ascii_val = mem_val.map(val => { return String.fromCharCode(Number(val)).replaceSpecial().center(5); });
mem_viewer.setValue(`${ascii_val.join(' ')}\n${mem_val.join(' ')}\n${mem.join(' ')}`);
mem_viewer.clearSelection();
},
src_pointer: (data) => {
src_viewer.renderer.scrollCursorIntoView({ row:0, column:data.ip }, 0.5);
src_viewer.selection.setRange(new Range(0, data.ip, 0, data.ip + 1));
}
};