-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (74 loc) · 2.59 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
(function() {
var lc2inst = new LC2;
var display;
var file_cache = {};
var loader = document.getElementById('loader');
var errors = document.getElementById('errors');
var assembleBtn = document.getElementById('assemble');
var displayDiv = document.getElementById('lc2');
var codeFollowPC = document.getElementById('codeFollowPC');
function getPath() {
return loader.options[loader.selectedIndex].value;
}
var editor = LC2.editorFromTextArea(document.getElementById('editor'));
editor.onError = function(err) {
errors.innerHTML = err.message + ' on line ' + err.line;
};
editor.onChange = function(change, value) {
errors.innerHTML = ' ';
file_cache[getPath()] = value;
};
function loadFile(filename) {
for(let i = 0; i < loader.options.length; ++i) {
var option = loader.options[i];
if(option.value === filename)
loader.selectedIndex = i;
}
loaderChange();
}
function reset() {
displayDiv.innerHTML = '';
lc2inst.reset();
lc2inst.pc.val = parseInt('3000', 16);
display = lc2inst.displayFromDiv(displayDiv);
display.onUpdate = function() {
var file_info = lc2inst.map[lc2inst.pc.uval];
if(file_info && codeFollowPC.checked) {
if(file_info && file_info.filename !== getPath()) {
loadFile(file_info.filename);
}
editor.setMarker(file_info.line - 1, LC2.pcMarker());
editor.setCursor(file_info.line - 1);
} else if(file_info && file_info.filename === getPath()) {
editor.setMarker(file_info.line - 1, LC2.pcMarker());
} else {
editor.clearMarker();
}
};
display.update();
}
function assemble(){
reset();
LC2.loadOS(lc2inst, file_cache);
var meta = {filename: getPath()};
editor.assemble(meta, function(prg) {
lc2inst.load_program(prg, meta);
display.update();
});
}
assembleBtn.addEventListener('click',assemble);
function loaderChange(fn) {
var path = getPath();
if(!(path in file_cache)) {
lib.readFromURL(path, function(file) {
file_cache[path] = file;
editor.setValue(file_cache[path]);
if(fn) fn();
});
} else {
editor.setValue(file_cache[path]);
}
}
loaderChange(assemble);
loader.addEventListener('change', loaderChange);
})();