website/js/flox/main.ts
2024-08-21 00:25:05 +02:00

39 lines
919 B
TypeScript

import { EditorState } from '@codemirror/state';
import { EditorView, keymap, lineNumbers } from '@codemirror/view';
import { defaultKeymap } from '@codemirror/commands';
import * as wasm from './pkg/flox_wasm';
const doc = `
let n1 = 2;
let n2 = 5;
let add = fn a b -> a + b;
add n1 n2
`;
const htmlEditor = document.getElementById('editor')!;
const htmlOutput = document.getElementById('output')!;
const htmlRun = document.getElementById('run')!;
const state = EditorState.create({
doc,
extensions: [
keymap.of(defaultKeymap),
lineNumbers()
],
});
const view = new EditorView({ state, parent: htmlEditor });
WebAssembly.compileStreaming(fetch('flox_wasm_bg.wasm')).then((asm) => wasm.initSync(asm));
function run(code: string) {
return wasm.eval_expression(code);
}
htmlRun.addEventListener('click', () => {
const code = view.state.doc.toString();
htmlOutput.textContent = run(code);
});