-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
72 lines (57 loc) · 2.34 KB
/
ui.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
var OUTPUT_TEMPLATE = Handlebars.compile(
'{{#each contents}}' +
' <div class="panel is-{{type}}"><p class="panel-heading">{{name}}</p>' +
' <textArea class="textarea is-family-code" readonly>{{content}}</textArea>' +
' </div>' +
'{{/each}}');
function updateOutput(contents, type) {
document.getElementById('output').innerHTML = OUTPUT_TEMPLATE({
contents: Object.entries(contents).map(([name, content]) => ({name, content, type})),
});
}
function clearOutput() {
updateOutput({'Compiling': 'Please wait...'}, 'warning');
document.getElementById('downloadROM').disabled = true;
document.getElementById('error').value = '';
}
function setupStdin(fs, code) {
var i = 0;
fs.init(function () { return i < code.length ? code.charCodeAt(i++) : null; });
}
function stderr(asciiCode) {
var err = document.getElementById('error');
err.value = err.value + String.fromCharCode(asciiCode);
}
function executeApplication() {
var remainingTries = 3;
function tryRunning() {
try {
clearOutput();
var source = document.getElementById('input').value;
var preprocessed = preprocessBatariBasic(source);
console.info('Preprocessing results', preprocessed);
var preprocessedResults = {'Preprocessed': preprocessed};
updateOutput(preprocessedResults, 'info');
var assemblyFiles = compileBatariBasic(preprocessed);
console.info('Assembly files generated from BASIC source', assemblyFiles);
updateOutput(Object.assign({}, preprocessedResults, assemblyFiles), 'info');
var assemblyOutputs = assembleDASM(assemblyFiles);
console.info('Output files generated from assemblies', assemblyOutputs);
var downloadButton = document.getElementById('downloadROM');
downloadButton.onclick = () => saveAs(new Blob([assemblyOutputs.output]), 'generated-rom.bin');
downloadButton.disabled = false;
Javatari.fileLoader.loadFromContent('main.bin', assemblyOutputs.output);
} catch (e) {
console.error('Error while compiling', e);
document.getElementById('error').value = e;
}
}
var inputArea = document.getElementById('input');
var inputEvent = _.debounce(tryRunning, 300);
inputArea.onchange = inputEvent;
inputArea.onkeyup = inputEvent;
document.getElementById('compile-button').onclick = inputEvent;
console.log('First try...');
tryRunning();
}
setTimeout(executeApplication, 1000);