You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

calc.mjs 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import astToJs from "/js/ast-to-js.mjs";
  2. import evalCode from "/js/eval-code.mjs";
  3. import lex from "/js/lex.mjs";
  4. import parse from "/js/parse.mjs";
  5. const $ = document.querySelector.bind(document);
  6. const nameEl = $("#name");
  7. const errorEl = $("#error");
  8. const reportEl = $("#report");
  9. const astProgram = $("#program");
  10. const program = JSON.parse(astProgram.textContent);
  11. nameEl.innerText = `Running: ${program.name}`;
  12. reportEl.addEventListener("click", () => {
  13. const file = location.pathname.split("/").slice(-1)[0];
  14. reportEl.disabled = true;
  15. reportEl.innerText = "Reported!";
  16. fetch(`/report`, {
  17. method: "POST",
  18. headers: {
  19. "Content-Type": "application/json"
  20. },
  21. body: JSON.stringify({ file })
  22. });
  23. })
  24. try {
  25. let ast;
  26. if (astProgram.type === "application/x-yaca-code") {
  27. const tokens = lex(program.code);
  28. ast = parse(tokens);
  29. } else {
  30. ast = JSON.parse(program.code);
  31. }
  32. const jsProgram = astToJs(ast);
  33. evalCode(jsProgram);
  34. } catch(e) {
  35. console.error(e);
  36. const msg = e instanceof Error ? e.message : "Something went wrong";
  37. errorEl.innerText = msg;
  38. }