您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

main.mjs 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const $ = document.querySelector.bind(document);
  2. const nameEl = $("#name");
  3. const uploadEl = $("#upload");
  4. const programEl = $("#program");
  5. const contentTypeEl = $("#content-type");
  6. uploadEl.addEventListener("click", async () => {
  7. const code = programEl.value;
  8. // We use logical or instead of nullish coalescing because
  9. // [input] values are always coerced to a string
  10. const name = nameEl.value || nameEl.getAttribute("placeholder");
  11. const type = contentTypeEl.value;
  12. const res = await fetch("/upload", {
  13. method: "POST",
  14. headers: {
  15. "content-type": "application/json",
  16. },
  17. body: JSON.stringify({ type, program: { name, code } })
  18. });
  19. const url = await res.text();
  20. window.location.href = url;
  21. });
  22. const staticAst = `{
  23. "kind": "binop",
  24. "op": "divide",
  25. "values": [
  26. {
  27. "kind": "binop",
  28. "op": "add",
  29. "values": [
  30. {
  31. "kind": "unop",
  32. "op": "negate",
  33. "value": { "kind": "variable", "variable": "b"}
  34. },
  35. {
  36. "kind": "function",
  37. "name": "sqrt",
  38. "argument": {
  39. "kind": "binop",
  40. "op": "subtract",
  41. "values": [
  42. {
  43. "kind": "binop",
  44. "op": "exponent",
  45. "values": [
  46. { "kind": "variable", "variable": "b"},
  47. { "kind": "number", "value": 2}
  48. ]
  49. },
  50. {
  51. "kind": "binop",
  52. "op": "multiply",
  53. "values": [
  54. { "kind": "number", "value": 4 },
  55. {
  56. "kind": "binop",
  57. "op": "multiply",
  58. "values": [
  59. { "kind": "variable", "variable": "a" },
  60. { "kind": "variable", "variable": "c" }
  61. ]
  62. }
  63. ]
  64. }
  65. ]
  66. }
  67. }
  68. ]
  69. },
  70. {
  71. "kind": "binop",
  72. "op": "multiply",
  73. "values": [
  74. { "kind": "number", "value": 2 },
  75. { "kind": "variable", "variable": "a" }
  76. ]
  77. }
  78. ]
  79. }`;
  80. const staticCode = `(-b + sqrt(b^2 - 4a*c)) / 2a`;
  81. const resetTextArea = () => {
  82. const contentType = contentTypeEl.value;
  83. if (contentType === "application/x-yaca-ast") {
  84. programEl.value = staticAst;
  85. } else {
  86. programEl.value = staticCode;
  87. }
  88. }
  89. contentTypeEl.addEventListener("change", resetTextArea);
  90. resetTextArea();