From 92b0bbf951db93e582cbb48f3cb952a0823a7c3c Mon Sep 17 00:00:00 2001 From: Neta London Date: Tue, 5 Mar 2024 18:06:20 +0200 Subject: [PATCH] Make vm editor writable --- components/src/stores/vm.store.ts | 26 +++++++++++++++++++++++--- web/src/pages/vm.tsx | 5 ++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/components/src/stores/vm.store.ts b/components/src/stores/vm.store.ts index 8c313ea10..63809a7e1 100644 --- a/components/src/stores/vm.store.ts +++ b/components/src/stores/vm.store.ts @@ -1,6 +1,5 @@ import { FileSystem } from "@davidsouther/jiffies/lib/esm/fs.js"; -// import { VM as multVM } from "@nand2tetris/simulator/testing/mult.js"; -import { isErr, unwrap } from "@davidsouther/jiffies/lib/esm/result.js"; +import { Result, isErr, unwrap } from "@davidsouther/jiffies/lib/esm/result.js"; import { FIBONACCI } from "@nand2tetris/projects/samples/vm.js"; import { KeyboardAdapter, @@ -157,6 +156,23 @@ export function makeVmStore( }, }; const actions = { + setVm(content: string) { + dispatch.current({ + action: "setVm", + payload: content, + }); + + const parseResult = VM.parse(content); + + if (isErr(parseResult)) { + dispatch.current({ action: "setValid", payload: false }); + setStatus(`Parse error: ${parseResult.err.message}`); + return false; + } + const instructions = unwrap(parseResult).instructions; + const buildResult = Vm.build(instructions); + return this.replaceVm(buildResult); + }, loadVm(files: VmFile[]) { for (const file of files) { if (file.content.endsWith("\n")) { @@ -194,12 +210,16 @@ export function makeVmStore( }); } const buildResult = Vm.buildFromFiles(parsed); - + return this.replaceVm(buildResult); + }, + replaceVm(buildResult: Result) { if (isErr(buildResult)) { dispatch.current({ action: "setValid", payload: false }); setStatus(`Build Error: ${buildResult.err.message}`); return false; } + dispatch.current({ action: "setValid", payload: true }); + setStatus("Compiled VM code successfully"); vm = unwrap(buildResult); test.vm = vm; diff --git a/web/src/pages/vm.tsx b/web/src/pages/vm.tsx index ae290f2d4..4fb778f29 100644 --- a/web/src/pages/vm.tsx +++ b/web/src/pages/vm.tsx @@ -197,11 +197,10 @@ const VM = () => { VM}>