-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
197 lines (174 loc) · 6.9 KB
/
helpers.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
function htmlToElement(html) {
var template = document.createElement('template');
html = html.trim();
template.innerHTML = html;
return template.content.firstChild;
}
const int16checker = value => value >= 0 && value < 2 ** 16 && value % 1 === 0;
class Register {
constructor(number, value = 0) {
this._number = number;
this._value = value;
this.checkValue = int16checker;
this._element = htmlToElement(`
<div class="collection-item register">${String.fromCharCode(number+97)}:
<span id="register${this._number}" class="registerValue new badge light-blue lighten-5 light-blue-text" data-badge-caption="">${this._value}</span>
</div>
`);
document.getElementById('registers').appendChild(this._element);
}
get value() {
return this._value;
}
set value(value) {
if (this.checkValue(value)) {
document.getElementById(`register${this._number}`).innerHTML = value;
this._value = value;
}
}
}
class ProgramState {
constructor(commands, line) {
this.commands = commands;
this.line = line;
this.executedCount = 0;
this.currentLine = () => this.commands[this.line];
this.labels = [];
for(let i=0; i<commands.length; ++i)
if(commands[i].startsWith("label"))
{
let label_name = commands[i].substring(6);
this.labels[label_name] = i;
}
this.getLabelLine = (label_name) => {
if(label_name in this.labels) return this.labels[label_name];
else
{
alert("Navestie s neexistujucim nazvom.");
return this.commands.length-1;
}
};
}
}
class Queue {
constructor() {
this._queue = [];
this._offset = 0;
this.checkValue = int16checker;
document.getElementById('queue').innerHTML = "";
this.enqueue = item => {
if (this.checkValue(item)) {
item = this._queue.push(item);
document.getElementById('queue').innerHTML = this._queue.slice(this._offset).join(' ');
return item;
}
};
this.dequeue = () => {
if (this._queue.length === 0) return undefined;
let item = this._queue[this._offset];
if (++this._offset * 2 >= this._queue.length) {
this._queue = this._queue.slice(this._offset);
this._offset = 0;
}
document.getElementById('queue').innerHTML = this._queue.slice(this._offset).join(' ');
return item;
};
this.peek = () => (this._queue.length > 0 ? this._queue[this._offset] : undefined);
}
get length() {
return this._queue.length - this._offset;
}
}
display = (element, shown=true) => shown ? element.classList.remove("hidden") : element.classList.add("hidden");
const numberOfRegisters = 26;
let executionInterval = 10;
let registers, queue, programState;
createStateFromRawCode = (rawInput) =>{
return new ProgramState(rawInput.replace(/;/g, String.fromCharCode(10)).split(String.fromCharCode(10)).map(x => x.trim()), 0);
};
clearRegisters = () => registers.forEach(x => x.value = 0);
clearQueue = () => queue = new Queue();
function postLoad() {
document.getElementById("stepButton").addEventListener("click", stepListener);
document.getElementById("runButton").addEventListener("click", runListener);
document.getElementById("stopButton").addEventListener("click", stopListener);
document.getElementById("fasterButton").addEventListener("click", fasterListener);
document.getElementById("slowerButton").addEventListener("click", slowerListener);
registers = Array(numberOfRegisters).fill(0).map((x, i) => new Register(i));
queue = new Queue();
}
let runListener = () => {
document.getElementById("runButton").innerHTML = "<i class=\"material-icons\">pause</i>";
document.getElementById("runButton").removeEventListener("click", runListener);
document.getElementById("runButton").addEventListener("click", pauseListener);
if(!programState){
document.getElementById("stopButton").classList.remove("disabled");
clearQueue();
clearRegisters();
changeCodeTextToLines();
document.getElementById("output").innerHTML = "";
programState = createStateFromRawCode(document.getElementById('code').value);
}
start();
};
let pauseListener = () => {
document.getElementById("runButton").innerHTML = "<i class=\"material-icons\">play_arrow</i>";
document.getElementById("runButton").addEventListener("click", runListener);
document.getElementById("runButton").removeEventListener("click", pauseListener);
pause();
};
stopListener = () => {
document.getElementById("stopButton").classList.add("disabled");
stop();
};
resetUIAfterEndOfProgramEvaluationTheLongestMethodNameInThisProject = () =>{
changeCodeLinesToText();
document.getElementById("stopButton").classList.add("disabled");
document.getElementById("runButton").innerHTML = "<i class=\"material-icons\">play_arrow</i>";
document.getElementById("runButton").addEventListener("click", runListener);
document.getElementById("runButton").removeEventListener("click", pauseListener);
};
let fasterListener = () => {
executionInterval /= 1.3;
if(handle) {
pause();
start();
}
};
let slowerListener = () => {
executionInterval *= 1.3;
if(handle) {
pause();
start();
}
};
let stepListener = () => {
if(!programState){
document.getElementById("stopButton").classList.remove("disabled");
clearQueue();
clearRegisters();
changeCodeTextToLines();
document.getElementById("output").innerHTML = "";
programState = createStateFromRawCode(document.getElementById('code').value);
}
step();
};
let changeCodeTextToLines = () => {
let elements = document.getElementById('code').value
.replace(/;/g, ";~")
.split(String.fromCharCode(10)).join("<br class='codeLineBreaker'>"+"~")
.split("~")
.map(x => x)
.map((x,i) => document.getElementById('codeLines')
.appendChild(htmlToElement(`<span id="codeLine${i}" class="codeLine">${x}</span>`)));
document.getElementById('code').classList.add('hidden');
document.getElementById('codeLines').classList.remove('hidden');
};
let changeCodeLinesToText = () => {
[...document.getElementsByClassName('codeLine')].forEach(x => x.parentElement.removeChild(x));
document.getElementById('code').classList.remove('hidden');
document.getElementById('codeLines').classList.add('hidden');
};
if (window.addEventListener) window.addEventListener("load", postLoad, false);
else if (window.attachEvent) window.attachEvent("onload", postLoad);
else window.onload = postLoad;