-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
232 lines (191 loc) · 5.46 KB
/
main.js
File metadata and controls
232 lines (191 loc) · 5.46 KB
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
/*
* When you actually enjoy doing something but you're not satisfied with the
* tools you have to do it, it's time to write better ones. So I did.
*/
"use strict";
var code_in;
var code_out;
var sim_out;
var warning_box;
var myProgram;
var mySimulation;
function warn(string) {
if (string === undefined)
string = "[undefined]";
else if (string === null)
string = "[null]";
else if (string === Infinity)
string = "[Infinity]";
else if (string === -Infinity)
string = "[-Infinity]";
else if (string === NaN)
string = "[NaN]";
if (console && console.warn)
console.warn(string);
else if (console && console.log)
console.log("warning: " + string);
warning_box.textContent += string + "\n";
}
function selectLine(lineNumber) {
// the first line is line number 0
var lines = code_in.value.split("\n");
if (lineNumber > lines) {
warn("line number out of bounds");
return false;
}
var i, start = 0, end;
for (i = 0; i < lineNumber; i++)
start += lines[i].length + 1;
end = start + lines[i].length;
if (code_in.setSelectionRange) { // W3 standard way
code_in.focus();
code_in.setSelectionRange(start, end);
} else if (code_in.createTextRange) { // microsoft crap
code_in.focus();
var range = code_in.createTextRange();
range.collapse(true);
range.moveStart("character", start);
range.moveEnd("character", end);
range.select();
} else {
warn("no selection method detected");
return false;
}
return true;
}
var isFirstTime = true;
function loadCode() {
if (!isFirstTime)
warning_box.textContent = "";
isFirstTime = false;
try {
myProgram = new Program(code_in.value);
} catch (e) {
var message = e.message;
if (e.hasOwnProperty("line")) {
message += " on line " + e.line;
selectLine(e.line);
}
warn(message);
myProgram = undefined;
}
makeOutputTable();
}
function makeTables() {
makeSimulationTable();
makeOutputTable();
}
function makeSimulationTable() {
var results = mySimulation.getTable();
if (results !== undefined) {
sim_out.id = "";
results.id = "sim_out";
sim_out.parentNode.replaceChild(results, sim_out);
sim_out = results;
}
}
function makeOutputTable() {
/*
* I want to show the offset to the left of the instruction, but not
* have it selected.
*
* It would be nice if <col> elements effected how things were selected,
* but alas, they don't, so do a stupid ugly hack instead where we have
* two side by side tables.
*/
if (myProgram === undefined) {
code_out.innerHTML = "";
return;
}
var output = document.createElement("div");
var offsets = document.createElement("table");
var instructions = document.createElement("table");
var tr = document.createElement("tr");
var th = document.createElement("th");
th.textContent = "Offset";
tr.appendChild(th);
offsets.appendChild(tr);
for (var i = 0; i < myProgram.instructions.length; i++) {
var tr = document.createElement("tr");
var td = document.createElement("td");
td.textContent = i.toString(settings.get("radix"));
tr.appendChild(td);
offsets.appendChild(tr);
}
output.appendChild(offsets);
tr = document.createElement("tr");
var bytecode = document.createElement("th");
var comment = document.createElement("th");
bytecode.textContent = "Bytecode";
comment.textContent = "Comment";
tr.appendChild(bytecode);
tr.appendChild(comment);
instructions.appendChild(tr);
for (var i = 0; i < myProgram.instructions.length; i++) {
var instruction = myProgram.instructions[i];
tr = document.createElement("tr");
bytecode = document.createElement("td");
comment = document.createElement("td");
bytecode.textContent = instruction.toBytecodeString();
switch (settings.get("source")) {
case "literal":
comment.textContent = instruction.source;
break;
case "assembly":
comment.textContent = instruction.toAssemblyString(settings.get("align_ops"), settings.get("r_prefix"));
break;
default:
comment.textContent = "Invalid Source Type!";
}
comment.innerHTML = "<span>//</span>" + comment.innerHTML;
tr.appendChild(bytecode);
tr.appendChild(comment);
instructions.appendChild(tr);
}
output.appendChild(instructions);
code_out.parentNode.replaceChild(output, code_out);
code_out.id = "";
output.id = "code_out";
code_out = output;
return true;
}
function runSim() {
if (myProgram === undefined) {
warn("You must load valid code before you can simulate it!");
return false;
}
mySimulation = new Simulation(myProgram);
mySimulation.run();
makeSimulationTable();
return true;
}
window.addEventListener("load", function() {
settings.init();
code_in = document.getElementById("code_in");
code_out = document.getElementById("code_out");
sim_out = document.getElementById("sim_out");
warning_box = document.getElementById("warnings");
document.getElementById("btn_load").addEventListener('click', loadCode);
document.getElementById("btn_run").addEventListener('click', runSim);
document.addEventListener('keydown', function() {
/*
* Make {control, command}+R just re-evaluate the code instead
* of reloading the page (and making the user lose what they
* were working on!)
*/
if (event.keyCode === 82 && (event.ctrlDown || event.metaKey)) {
try {
loadCode();
} catch (err) {
warn(err.message);
}
event.preventDefault();
}
}, false);
loadCode();
runSim();
settings.registerHook("radix", makeTables);
settings.registerHook("source", makeTables);
settings.registerHook("r_prefix", makeTables);
settings.registerHook("align_ops", makeTables);
}, false);