1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- const $ = document.querySelector.bind(document);
- const nameEl = $("#name");
- const uploadEl = $("#upload");
- const programEl = $("#program");
- const contentTypeEl = $("#content-type");
-
- uploadEl.addEventListener("click", async () => {
- const code = programEl.value;
- // We use logical or instead of nullish coalescing because
- // [input] values are always coerced to a string
- const name = nameEl.value || nameEl.getAttribute("placeholder");
- const type = contentTypeEl.value;
-
- const res = await fetch("/upload", {
- method: "POST",
- headers: {
- "content-type": "application/json",
- },
- body: JSON.stringify({ type, program: { name, code } })
- });
-
- const url = await res.text();
- window.location.href = url;
- });
-
- const staticAst = `{
- "kind": "binop",
- "op": "divide",
- "values": [
- {
- "kind": "binop",
- "op": "add",
- "values": [
- {
- "kind": "unop",
- "op": "negate",
- "value": { "kind": "variable", "variable": "b"}
- },
- {
- "kind": "function",
- "name": "sqrt",
- "argument": {
- "kind": "binop",
- "op": "subtract",
- "values": [
- {
- "kind": "binop",
- "op": "exponent",
- "values": [
- { "kind": "variable", "variable": "b"},
- { "kind": "number", "value": 2}
- ]
- },
- {
- "kind": "binop",
- "op": "multiply",
- "values": [
- { "kind": "number", "value": 4 },
- {
- "kind": "binop",
- "op": "multiply",
- "values": [
- { "kind": "variable", "variable": "a" },
- { "kind": "variable", "variable": "c" }
- ]
- }
- ]
- }
- ]
- }
- }
- ]
- },
- {
- "kind": "binop",
- "op": "multiply",
- "values": [
- { "kind": "number", "value": 2 },
- { "kind": "variable", "variable": "a" }
- ]
- }
- ]
- }`;
-
- const staticCode = `(-b + sqrt(b^2 - 4a*c)) / 2a`;
-
- const resetTextArea = () => {
- const contentType = contentTypeEl.value;
-
- if (contentType === "application/x-yaca-ast") {
- programEl.value = staticAst;
- } else {
- programEl.value = staticCode;
- }
- }
-
- contentTypeEl.addEventListener("change", resetTextArea);
- resetTextArea();
|