all repos — hex @ 6a2089c3ab044375a42c7699fd458af166e9f0c9

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Attempting to override IO.
h3rald h3rald@h3rald.com
Sun, 01 Dec 2024 21:21:27 +0100
commit

6a2089c3ab044375a42c7699fd458af166e9f0c9

parent

d2773f50bc14f0b32c287a6e496f151ae84f01f9

1 files changed, 41 insertions(+), 18 deletions(-)

jump to
M web/assets/hex-playground.jsweb/assets/hex-playground.js

@@ -1,21 +1,44 @@

-let stdinBuffer = []; -const inputBox = document.querySelector("article input") -inputBox.addEventListener("keydown", (event) => { - if (event.key === "Enter" && !event.shiftKey) { - event.preventDefault(); - const input = inputBox.value; - inputBox.value = ""; // Clear the textarea - stdinBuffer.push(...input + "\n"); // Add input to the buffer +Module.preRun = () => { + + let stdinBuffer = []; + const inputBox = document.querySelector("article input") + const input = inputBox.value; + inputBox.addEventListener("keydown", (event) => { + if (event.key === "Enter" && !event.shiftKey) { + event.preventDefault(); + inputBox.value = ""; // Clear the textarea + stdinBuffer.push(...input + "\n"); // Add input to the buffer + } + }); + function stdin() { + if (i < input.length) { + var code = input.charCodeAt(i); + ++i; + return code; + } else { + return null; + } + }; + + let stdoutBuffer = ""; + function stdout(code) { + if (code === "\n".charCodeAt(0) && stdoutBuffer !== "") { + document.querySelector("article section").textContent += stdoutBuffer + "\n"; + stdoutBuffer = ""; + } else { + stdoutBuffer += String.fromCharCode(code); } -}); + } -Module.print = (text) => { - document.querySelector("article section").textContent += text + "\n"; -}; -Module.printErr = (text) => { - document.querySelector("article section").textContent += text + "\n"; -}; -Module.stdin = () => { - return stdinBuffer.length > 0 ? stdinBuffer.shift().charCodeAt(0) : null; -}; + let stderrBuffer = ""; + function stderr(code) { + if (code === "\n".charCodeAt(0) && stderrBuffer !== "") { + document.querySelector("article section").textContent += stderrBuffer + "\n"; + stderrBuffer = ""; + } else { + stderrBuffer += String.fromCharCode(code); + } + } + FS.init(stdin, stdout, stderr); +}