Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Scrapscript Web REPL</title><link rel="shortcut icon" type="image/x-icon" href="data:image/vnd.microsoft.icon;base64,AAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAD/AAAA////AAAA/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAREBEQAAAAAQAQEAEAAAABABAQAQAAAAAREBEQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACIiIiIiIiIiIiIiIiIiIiIiERIiESEiIiIiESEiISIiIhEiISIhEiIiIREiESEhIiIiIiIiIiIiIiIiIiIiIiIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"><link rel="preconnect" href="https://fonts.googleapis.com" /><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /><link href="https://fonts.googleapis.com/css2?family=Fira+Code&display=swap" rel="stylesheet"/><link href="https://fonts.googleapis.com/css2?family=Rubik:ital,wght@0,500;0,700&display=swap" rel="stylesheet"/><link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:ital,wght@0,400;0,700;0,900;1,400&display=swap" rel="stylesheet"/><link rel="stylesheet" href="/style.css" /></head><body><main><div><p>See <a href="https://scrapscript.org/">scrapscript.org</a> for a slightlyout of date language reference.</p><p>This REPL is completely client-side and works by running<a href="https://github.com/tekknolagi/scrapscript">scrapscript.py</a> in thebrowser using <a href="https://pyodide.org/">Pyodide</a>.</p></div><div><!-- TODO(max): Add button to save to/load from disk. --><button id="clear-local-storage">Clear</button></div><div id="output" style="height: 400px; overflow: auto;">Output:</div><div><input id="input" type="text" disabled placeholder="Loading pyodide..." /><input id="submit-input" type="submit" value="Submit" /></div><script type="module">"use strict";
function updateHistory(inp, out) { const wrap = document.createElement("div"); const pre_inp = document.createElement("pre"); pre_inp.setAttribute("class", "language-text"); const code_inp = document.createElement("code"); code_inp.setAttribute("class", "language-text"); code_inp.append(`>>> ${inp}`); pre_inp.append(code_inp);
const pre_out = document.createElement("pre"); pre_out.setAttribute("class", "result language-text"); const code_out = document.createElement("code"); code_out.setAttribute("class", "language-text"); code_out.append(`${out}`); pre_out.append(code_out);
wrap.append(pre_inp); wrap.append(pre_out); output.append(wrap); output.scrollTop = output.scrollHeight;}
async function sendRequest(exp) { const scrap = document.scrapscript; const [result, monad] = document.monad.bind(scrap.parse(scrap.tokenize(exp))); return {result: scrap.pretty(result), monad, ok: true};}
let input = document.getElementById("input");const output = document.getElementById("output");const history = [];const clearButton = document.getElementById("clear-local-storage");const submitButton = document.getElementById("submit-input");
function renderHistory() { for (const [inp, out] of history) { updateHistory(inp, out); }}
function loadFromLocalStorage() { history.length = 0; output.innerHTML = ""; renderHistory();}
function expandInput() { if (input.tagName === "TEXTAREA") { return; } const textarea = document.createElement("textarea"); textarea.setAttribute("id", "input"); textarea.setAttribute("rows", "1"); textarea.setAttribute("style", "height: 1.5em;"); textarea.value = input.value; input.replaceWith(textarea); input = textarea; input.focus(); input.addEventListener("keyup", e => inputHandler(e));}
function shrinkInput() { if (input.tagName === "INPUT") { return; } const newInput = document.createElement("input"); newInput.setAttribute("id", "input"); newInput.setAttribute("type", "text"); newInput.value = input.value; input.replaceWith(newInput); input = newInput; input.focus(); input.addEventListener("keyup", e => inputHandler(e));}
async function submitInput() { const response = await sendRequest(input.value); if (response.ok) { const {result, monad} = response; updateHistory(input.value, result); history.push([input.value, result]); input.value = ""; document.monad = monad; } else { const msg = await response.text(); updateHistory(input.value, msg); input.value = ""; }}
loadFromLocalStorage();input.focus();async function inputHandler(e) { // TODO(max): Make up/down arrow keys navigate history if (e.key === "Enter") { if (e.shiftKey && input.tagName === "INPUT") { // Shift-Enter expands the input to a textarea and does not submit // input. expandInput(); return; } if (input.tagName === "TEXTAREA") { // Enter in a textarea should not submit input. if (e.shiftKey) { shrinkInput(); } return; } submitInput() }}input.addEventListener("keyup", e => inputHandler(e));clearButton.addEventListener("click", () => { window.localStorage.clear(); loadFromLocalStorage(); input.focus();});submitButton.addEventListener("click", () => { submitInput() input.focus();});</script><script>"use strict";
async function main() { // const scrapscript = await fetch('/scrapscript.py'); let indexURL = "https://cdn.jsdelivr.net/pyodide/v0.25.1/full/"; const urlParams = new URLSearchParams(window.location.search); const buildParam = urlParams.get("build"); if (buildParam) { if (["full", "debug", "pyc"].includes(buildParam)) { indexURL = indexURL.replace( "/full/", "/" + urlParams.get("build") + "/", ); } else { console.warn( 'Invalid URL parameter: build="' + buildParam + '". Using default "full".', ); } } const { loadPyodide } = await import(indexURL + "pyodide.mjs"); const pyodide = await loadPyodide({ stdout: (msg) => console.log(`Pyodide: ${msg}`), }); await pyodide.runPythonAsync(` from pyodide.http import pyfetch response = await pyfetch("/scrapscript.py") with open("scrapscript.py", "wb") as f: f.write(await response.bytes()) `) const scrapscript = await pyodide.pyimport("scrapscript");document.monad = scrapscript.ScrapMonad(scrapscript.boot_env());document.scrapscript = scrapscript; input.removeAttribute("disabled"); input.setAttribute("placeholder", "Enter Scrapscript code here..."); input.focus();}main();</script></main><script data-goatcounter="https://scrapscript.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script></body></html>