forked from inexorabletash/jsbasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
293 lines (258 loc) · 8.06 KB
/
index.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
window.addEventListener('DOMContentLoaded', function() {
var $ = function(s) { return document.querySelector(s); };
$('#lb_files').selectedIndex = 0;
var frame = $('#frame');
var keyboard = frame;
if (/iPod|iPad|iPhone/.test(navigator.platform)) {
keyboard = document.createElement('input');
keyboard.type = 'text';
keyboard.style.width = keyboard.style.height = '1px';
keyboard.style.border = 'none';
keyboard.style.position = 'absolute';
keyboard.style.left = keyboard.style.top = '-100px';
frame.removeAttribute('tabIndex');
frame.addEventListener('click', function() { keyboard.focus(); });
frame.parentNode.insertBefore(keyboard, frame);
}
var tty = new TTY($('#screen'), keyboard);
(function() {
// Install output hook for bell
var b = new Bell(/^.*\/|/.exec(window.location)[0]);
var orig = tty.writeChar;
tty.writeChar = function index_writeChar(c) {
if (c.charCodeAt(0) === 7)
b.play();
else
orig(c);
};
}());
var dos = new DOS(tty);
var lores = new LoRes($('#lores'), 40, 48);
var hires = new HiRes($('#hires'), 280, 192);
var hires2 = new HiRes($('#hires2'), 280, 192);
var display = {
state: { graphics: false, full: true, page1: true, lores: true },
setState: function(state, value /* ... */) {
var args = Array.prototype.slice.call(arguments);
while (args.length) {
state = args.shift();
value = args.shift();
this.state[state] = value;
}
if (this.state.graphics) {
lores.show(this.state.lores);
hires.show(!this.state.lores && this.state.page1);
hires2.show(!this.state.lores && !this.state.page1);
tty.splitScreen(tty.getScreenSize().height - (this.state.full ? 0 : 4));
} else {
lores.show(false);
hires.show(false);
hires2.show(false);
tty.splitScreen(0);
}
},
getState: function() {
return Object.assign({}, this.state);
}
};
var pdl = [0, 0, 0, 0];
// Lexical highlighting, if available
var editor;
if (typeof CodeMirror === 'function') {
editor = new CodeMirror($('#editorframe'), {
mode: 'basic',
tabMode: 'default',
content: $('#source').value,
height: '100%'
});
} else {
editor = (function() {
var textArea = document.createElement('textarea');
$('#editorframe').appendChild(textArea);
textArea.style.width = '598px';
textArea.style.height = '384px';
return {
getValue: function() {
return textArea.value;
},
setValue: function(value) {
textArea.value = value;
},
setCursor: function(line, column) {
// TODO: Implement me!
}
};
}());
}
$('#btn_share').onclick = function(e) {
// Load up the hidden text area with the current source
$('#source').value = getSource();
};
function getSource() {
return editor.getValue();
}
function setSource(source) {
editor.setValue(source);
}
var program;
$('#btn_run').addEventListener('click', function(e) {
e.preventDefault();
dos.reset();
tty.reset();
tty.autoScroll = true;
try {
program = basic.compile(getSource());
} catch (e) {
if (e instanceof basic.ParseError) {
editor.setCursor({ line: e.line - 1, ch: e.column - 1 });
console.log(e.message +
' (source line:' + e.line + ', column:' + e.column + ')');
}
alert(e);
return;
}
stopped = false;
updateUI();
$('#btn_stop').focus();
program.init({
tty: tty,
hires: hires,
hires2: hires2,
lores: lores,
display: display,
paddle: function(n) { return pdl[n]; }
});
setTimeout(driver, 0);
});
$('#btn_stop').addEventListener('click', function(e) {
e.preventDefault();
tty.reset(); // cancel any blocking input
stopped = true;
updateUI();
});
$('#lb_files').addEventListener('change', function() {
var sel = $('#lb_files');
loadFile('samples/' + sel.value + ".txt", setSource);
});
var current_file_name;
$('#btn_save').addEventListener('click', function(e) {
e.preventDefault();
var a = document.createElement('a');
a.download = current_file_name || 'basic_program.txt';
a.href = 'data:text/plain;base64,' + window.btoa(getSource());
document.body.appendChild(a);
a.click();
a.parentElement.removeChild(a);
});
$('#btn_load').addEventListener('click', function(e) {
e.preventDefault();
var input = document.createElement('input');
input.type = 'file';
input.addEventListener('change', function(e) {
var file = e.target.files[0];
current_file_name = file.name;
var reader = new FileReader();
reader.addEventListener('load', function() {
setSource(reader.result);
});
reader.readAsText(file);
});
document.body.appendChild(input);
input.click();
input.parentElement.removeChild(input);
});
// Add a "printer" on demand
var printer = null;
var paper = $('#paper');
$('#show_paper').addEventListener('click', function(e) {
e.preventDefault();
document.body.classList.add('printout');
printer = new Printer(tty, paper);
});
$('#hide_paper').addEventListener('click', function(e) {
e.preventDefault();
document.body.classList.remove('printout');
printer.close();
printer = null;
});
// Mouse-as-Joystick
var wrapper = $('#screen-wrapper');
wrapper.addEventListener('mousemove', function(e) {
var rect = wrapper.getBoundingClientRect(),
x = e.clientX - rect.left, y = e.clientY - rect.top;
function clamp(n, min, max) { return n < min ? min : n > max ? max : n; }
pdl[0] = clamp(x / (rect.width - 1), 0, 1);
pdl[1] = clamp(y / (rect.height - 1), 0, 1);
});
var stopped = true;
function updateUI() {
var btnFocus = (document.activeElement === $("#btn_run") ||
document.activeElement === $("#btn_stop"));
$("#btn_stop").disabled = stopped ? "disabled" : "";
$("#btn_run").disabled = stopped ? "" : "disabled";
$("#lb_files").disabled = stopped ? "" : "disabled";
if (btnFocus || stopped) {
$(stopped ? "#btn_run" : "#btn_stop").focus();
} else {
tty.focus();
}
}
// TODO: Expose a RESPONSIVE |---+--------| FAST slider in the UI
// Number of steps to execute before yielding execution
// (Use a prime to minimize risk of phasing with loops)
var NUM_SYNCHRONOUS_STEPS = 37;
function driver() {
var state = basic.STATE_RUNNING;
var statements = NUM_SYNCHRONOUS_STEPS;
while (!stopped && state === basic.STATE_RUNNING && statements > 0) {
try {
state = program.step(driver);
} catch (e) {
console.log(e);
alert(e.message ? e.message : e);
stopped = true;
updateUI();
return;
}
statements -= 1;
}
if (state === basic.STATE_STOPPED || stopped) {
stopped = true;
updateUI();
} else if (state === basic.STATE_BLOCKED) {
// Fall out
} else { // state === basic.STATE_RUNNING
setTimeout(driver, 0); // Keep going
}
}
function parseQueryParams() {
var params = {};
var query = document.location.search.substring(1);
query.split(/&/g).forEach(function(pair) {
pair = pair.replace(/\+/g, " ").split(/=/).map(decodeURIComponent);
params[pair[0]] = pair.length === 1 ? pair[0] : pair[1];
});
return params;
}
function loadFile(filename, callback) {
var req = new XMLHttpRequest();
var url = encodeURI(filename); // not encodeURIComponent, so paths can be specified
var async = true;
req.open("GET", url, async);
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
if (req.status === 200 || req.status === 0) {
callback(req.responseText);
}
}
};
req.send(null);
}
// load default
var params = parseQueryParams();
if ('source' in params) {
setSource(params.source);
} else {
loadFile('samples/sample.default.txt', setSource);
}
});