-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
57 lines (47 loc) · 1.78 KB
/
main.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
import parse from "./src/parser.js";
import handleSymbols from "./src/handleSymbols.js";
import assemble from "./src/assemble.js";
document.getElementById('fileInput').addEventListener('change', function() {
const file = this.files[0];
const fileName = file.name.substring(0, file.name.indexOf('.'));
const reader = new FileReader();
reader.onload = () => displayCode(reader.result, fileName);
reader.readAsText(file);
})
document.getElementById('inputCode').addEventListener('click', function() {
const codeInput = document.getElementById('codeInput').value;
displayCode(codeInput, 'program');
})
function displayCode (code, fileName) {
const outputOriginal = document.getElementById('outputOriginal');
const outputArea = document.getElementById('output');
outputOriginal.textContent = '';
outputArea.textContent = '';
const processedCode = process(code);
if (processedCode[1].length > 50)
download(fileName + '.hack', processedCode[1].join('\n'));
else {
for (let i = 0; i < processedCode[0].length; i++) {
outputOriginal.textContent += processedCode[0][i] + '\n';
outputArea.textContent += processedCode[1][i] + '\n';
}
}
if (outputArea.textContent !== '') {
outputOriginal.textContent += '\n';
outputArea.textContent += '\n';
}
}
function process(code) {
const parsedCode = parse(code);
const unSymbolized = handleSymbols(parsedCode);
return [unSymbolized, assemble(unSymbolized)];
}
function download(filename, text) {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}