2024-09-08 15:38:21 +02:00
|
|
|
import { EditorState } from "@codemirror/state";
|
|
|
|
import { EditorView, keymap, lineNumbers } from "@codemirror/view";
|
|
|
|
import { defaultKeymap } from "@codemirror/commands";
|
|
|
|
import { evaluate } from "./wasm";
|
2024-08-21 00:25:05 +02:00
|
|
|
|
|
|
|
const doc = `
|
|
|
|
let n1 = 2;
|
|
|
|
let n2 = 5;
|
|
|
|
|
2024-08-21 23:44:08 +02:00
|
|
|
let add a b = a + b;
|
2024-08-21 00:25:05 +02:00
|
|
|
|
2024-08-21 23:44:08 +02:00
|
|
|
2
|
|
|
|
|> add 2
|
|
|
|
|> add 1
|
|
|
|
|> fn n -> n * -1
|
2024-08-21 00:25:05 +02:00
|
|
|
`;
|
|
|
|
|
2024-09-08 15:38:21 +02:00
|
|
|
const htmlEditor = document.getElementById("editor")!;
|
|
|
|
const htmlOutput = document.getElementById("output")!;
|
|
|
|
const htmlRun = document.getElementById("run")!;
|
2024-08-21 00:25:05 +02:00
|
|
|
|
|
|
|
const state = EditorState.create({
|
|
|
|
doc,
|
2024-09-08 15:38:21 +02:00
|
|
|
extensions: [keymap.of(defaultKeymap), lineNumbers()],
|
2024-08-21 00:25:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const view = new EditorView({ state, parent: htmlEditor });
|
|
|
|
|
2024-09-08 15:38:21 +02:00
|
|
|
htmlRun.addEventListener("click", () => {
|
|
|
|
htmlOutput.textContent = evaluate(view.state.doc.toString());
|
2024-08-21 00:25:05 +02:00
|
|
|
});
|