diff options
| author | Arnaud Bailly <arnaud@pankzsoft.com> | 2025-11-03 20:08:59 +0100 |
|---|---|---|
| committer | Arnaud Bailly <arnaud@pankzsoft.com> | 2025-11-03 20:08:59 +0100 |
| commit | 05800b377de4e2883faad1ae047139ea8fef00ba (patch) | |
| tree | 9523ef4686c43d80c52479539dcd675d2c08ed16 | |
| parent | 6ef423e2b2a32b5eca00d354283b1ce312129608 (diff) | |
| download | lambda-nantes-05800b377de4e2883faad1ae047139ea8fef00ba.tar.gz | |
Allow :lang command
| -rw-r--r-- | lambda-calcul/haskell/public/repl.js | 183 |
1 files changed, 93 insertions, 90 deletions
diff --git a/lambda-calcul/haskell/public/repl.js b/lambda-calcul/haskell/public/repl.js index a99494a..c0de921 100644 --- a/lambda-calcul/haskell/public/repl.js +++ b/lambda-calcul/haskell/public/repl.js @@ -1,127 +1,130 @@ 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 = (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.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.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)); + 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); - } - }; + 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); - } + 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 prettyTerm(term) { - if(term.tag === "Var") { - return term.contents; - } else if(term.tag === "Lam") { - return "(lam (" + term.contents[0] + ") " + prettyTerm(term.contents[1]) + ")"; - } else if(term.tag === "App") { - return "(" + prettyTerm(term.contents[0]) + " " + prettyTerm(term.contents[1]) + ")"; - } - return JSON.stringify(term); + if(term.tag === "Var") { + return term.contents; + } else if(term.tag === "Lam") { + return "(lam (" + term.contents[0] + ") " + prettyTerm(term.contents[1]) + ")"; + } else if(term.tag === "App") { + return "(" + prettyTerm(term.contents[0]) + " " + prettyTerm(term.contents[1]) + ")"; + } + return JSON.stringify(term); } function prettyType(type) { - if(type.tag === "TyVar") { - return type.contents; - } else if(type.tag === "TyArr") { - return "(" + prettyType(type.contents[0]) + " -> " + prettyType(type.contents[1]) + ")"; - } - return JSON.stringify(type); + if(type.tag === "TyVar") { + return type.contents; + } else if(type.tag === "TyArr") { + return "(" + prettyType(type.contents[0]) + " -> " + prettyType(type.contents[1]) + ")"; + } + return JSON.stringify(type); } function formatResult(text) { - const result = JSON.parse(text); - if (result.tag === "Evaluated") { - return prettyTerm(result.contents[0]) + ':' + prettyType(result.contents[1]); - } else if (result.tag === "Msg" ) { - return result.contents; - } - return text; + const result = JSON.parse(text); + if (result.tag === "Evaluated") { + return prettyTerm(result.contents[0]) + ':' + prettyType(result.contents[1]); + } else if (result.tag === "Msg" ) { + return result.contents; + } + return text; } function readMessageAndAppend(reader) { - return () => { - appendOutput(document.createTextNode(formatResult(reader.result))); - }; + 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}; - } + const words = val.split(/ +/); + if(words[0] === ":env") { + return {tag:"Com",contents:{tag:"DumpEnv"}}; + } else if(words[0] === ":clear") { + return {tag:"Com",contents:{tag:"ClearEnv"}}; + } else if(words[0] === ":help") { + return {tag:"Com",contents:{tag:"Help"}}; + } else if(words[0] === ":lang") { + return {tag:"Com",contents:{tag:"Set", contents:{tag: "SelectLanguage", contents:words[1]}}}; + } 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; - } + 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; + 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; + 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); |
