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
|
var websocket = undefined;
function connect() {
websocket = (function() {
const host = window.location.host;
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const ws = new WebSocket(protocol + "//" + host + "/repl/1235");
ws.binaryType = "arraybuffer";
return ws;
})();
websocket.onopen = function(e) {
document.getElementById("command").classList.remove("disconnected");
document.getElementById("command").classList.add("connected");
document.getElementById("connect").src = "images/connected.png";
document.getElementById("connect").removeEventListener("click", connect);
};
websocket.onclose = function(e) {
document.getElementById("command").classList.remove("connected");
document.getElementById("command").classList.add("disconnected");
document.getElementById("connect").src = "images/connect.png";
document.getElementById("connect").addEventListener("click", connect);
};
websocket.onmessage = function(e) {
const reader = new FileReader();
reader.addEventListener('loadend', readMessageAndAppend(reader));
if(e.data instanceof ArrayBuffer) {
reader.readAsText(new Blob([e.data]));
} else {
reader.readAsText(e.data);
}
};
}
var commandHistory = [];
function appendOutput(text) {
const root = document.getElementById("minilang");
const sub = document.createElement("div");
sub.classList.add("output");
sub.appendChild(text);
if(root.firstChild) {
root.insertBefore(sub, root.firstChild);
} else {
root.appendChild(sub);
}
};
function formatResult(text) {
const result = JSON.parse(text);
if (result.rendered) {
return result.rendered;
} else if (result.tag === "Msg" ) {
return result.contents;
}
return text;
}
function readMessageAndAppend(reader) {
return () => {
appendOutput(document.createTextNode(formatResult(reader.result)));
};
};
function makeCommand(val) {
if(val === ":env") {
return {tag:"Com",contents:{tag:"DumpEnv"}};
} else if(val === ":clear") {
return {tag:"Com",contents:{tag:"ClearEnv"}};
} else if(val === ":help") {
return {tag:"Com",contents:{tag:"Help"}};
} else {
return {tag:"In",contents:val};
}
};
function sendCommand(e) {
const input = document.getElementById("command");
if (e.keyCode == 13) {
const command = makeCommand(input.value);
appendOutput(document.createTextNode("> "+ input.value));
commandHistory.push(input.value);
websocket.send(JSON.stringify(command));
input.value = "";
return false;
} else if(e.keyCode == 38) {
input.value = commandHistory.pop() || "";
return false;
}
return true;
};
function loadText(e) {
const text = document.getElementById("program").value;
const command = {tag:"Com",
contents:{ tag:"Load",
contents:{ tag:"Data",
contents:text}}};
websocket.send(JSON.stringify(command));
return false;
};
document.getElementById("command").addEventListener("keyup", sendCommand);
document.getElementById("connect").addEventListener("click", connect);
document.getElementById("send").addEventListener("click", loadText);
connect();
|