Skip to content

Commit

Permalink
refactoring: remove vm from core
Browse files Browse the repository at this point in the history
  • Loading branch information
benchmarko committed Feb 26, 2025
1 parent 9ee463a commit fbe5bc5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="locobasic.css">
<title>LocoBasic v0.1.43</title>
<title>LocoBasic v0.1.44</title>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "locobasic",
"version": "0.1.43",
"version": "0.1.44",
"description": "# LocoBasic - Loco BASIC",
"type": "commonjs",
"scripts": {
Expand Down
11 changes: 1 addition & 10 deletions src/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function fnHereDoc(fn: () => void) {

export class Core implements ICore {
private config: ConfigType;
private vm?: IVmAdmin;
private readonly semantics = new Semantics();
private readonly examples: Record<string, string> = {};
private arithmeticParser: Parser | undefined;
Expand All @@ -18,10 +17,6 @@ export class Core implements ICore {
this.config = defaultConfig;
}

public setVm(vm: IVmAdmin): void {
this.vm = vm;
}

private onCheckSyntax = async (_s: string) => ""; // eslint-disable-line @typescript-eslint/no-unused-vars

public getConfigObject(): ConfigType {
Expand Down Expand Up @@ -58,11 +53,7 @@ export class Core implements ICore {
return this.arithmeticParser.parseAndEval(script);
}

public async executeScript(compiledScript: string): Promise<string> {
if (!this.vm) {
return "ERROR: No VM";
}
const vm = this.vm;
public async executeScript(compiledScript: string, vm: IVmAdmin): Promise<string> {
vm.setOutput("");

if (compiledScript.startsWith("ERROR")) {
Expand Down
5 changes: 2 additions & 3 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ export interface IVmAdmin extends IVm {
}

export interface ICore {
setVm(vm: IVmAdmin): void;
getConfigObject(): ConfigType;
getExampleObject(): ExampleType;
getExample(name: string): string;
setExample(key: string, script: string): void;
compileScript(script: string): string;
executeScript(compiledScript: string): Promise<string>;
executeScript(compiledScript: string, vm: IVmAdmin): Promise<string>;
setOnCheckSyntax(fn: (s: string) => Promise<string>): void;
addItem(key: string, input: string | (() => void)): void;
parseArgs(args: string[], config: Record<string, ConfigEntryType>): void;
Expand All @@ -50,7 +49,7 @@ export interface INodeParts {
}

export interface IUI {
onWindowLoadContinue(core: ICore): void;
onWindowLoadContinue(core: ICore, vm: IVmAdmin): void;
getEscape(): boolean;
addOutputText(value: string): void;
setOutputText(value: string): void;
Expand Down
14 changes: 7 additions & 7 deletions src/NodeParts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICore, IVm } from "./Interfaces";
import { ICore, IVm, IVmAdmin } from "./Interfaces";
import { BasicVmNode } from "./BasicVmNode";

interface NodeFs {
Expand Down Expand Up @@ -171,7 +171,7 @@ export class NodeParts {
return this.escape;
}

private start(core: ICore, input: string): Promise<void> | undefined {
private start(core: ICore, vm: IVmAdmin, input: string): Promise<void> | undefined {
const actionConfig = core.getConfigObject().action;
if (input !== "") {
core.setOnCheckSyntax((s: string) => Promise.resolve(this.nodeCheckSyntax(s)));
Expand All @@ -185,7 +185,7 @@ export class NodeParts {

if (actionConfig.includes("run")) {
return this.keepRunning(async () => {
const output = await core.executeScript(compiledScript);
const output = await core.executeScript(compiledScript, vm);
console.log(output.replace(/\n$/, ""));
if (this.fnOnKeyPressHandler) {
process.stdin.off('keypress', this.fnOnKeyPressHandler);
Expand All @@ -204,7 +204,7 @@ export class NodeParts {
}

public async nodeMain(core: ICore): Promise<void> {
core.setVm(new BasicVmNode(this));
const vm = new BasicVmNode(this);
const config = core.getConfigObject();
core.parseArgs(global.process.argv.slice(2), config);

Expand All @@ -213,7 +213,7 @@ export class NodeParts {
if (config.fileName) {
return this.keepRunning(async () => {
input = await this.nodeReadFile(config.fileName);
this.start(core, input);
this.start(core, vm, input);
}, 5000);
} else {
if (config.example) {
Expand All @@ -230,10 +230,10 @@ export class NodeParts {
return;
}
input = exampleScript;
this.start(core, input);
this.start(core, vm, input);
}, 5000);
}
this.start(core, input);
this.start(core, vm, input);
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/UI/UI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Editor } from 'codemirror';
import type { ConfigEntryType, ICore, IUI } from "../Interfaces";
import type { ConfigEntryType, ICore, IUI, IVmAdmin } from "../Interfaces";

// Worker:
type PlainErrorEventType = {
Expand Down Expand Up @@ -42,6 +42,7 @@ const workerFn = (): void => {

export class UI implements IUI {
private core?: ICore;
private vm?: IVmAdmin;
private basicCm?: Editor;
private compiledCm?: Editor;
private readonly keyBuffer: string[] = []; // buffered pressed keys
Expand All @@ -68,6 +69,13 @@ export class UI implements IUI {
})();
}

private getCore() {
if (!this.core) {
throw new Error("Core not initialized");
}
return this.core;
}

public getEscape() {
return this.escape;
}
Expand Down Expand Up @@ -112,13 +120,17 @@ export class UI implements IUI {
}

private async onExecuteButtonClick(_event: Event): Promise<void> { // eslint-disable-line @typescript-eslint/no-unused-vars
const core = this.getCore();
if (!this.vm) {
return;
}
const compiledText = document.getElementById("compiledText") as HTMLTextAreaElement;
const compiledScript = this.compiledCm ? this.compiledCm.getValue() as string : compiledText.value;
this.setButtonDisabled("executeButton", true);
this.setButtonDisabled("stopButton", false);
this.escape = false;
this.keyBuffer.length = 0;
const output = await this.core?.executeScript(compiledScript) || "";
const output = await core.executeScript(compiledScript, this.vm) || "";
this.setButtonDisabled("executeButton", false);
this.setButtonDisabled("stopButton", true);
this.addOutputText(output + (output.endsWith("\n") ? "" : "\n"));
Expand All @@ -135,12 +147,13 @@ export class UI implements IUI {
}

private onCompileButtonClick(_event: Event): void { // eslint-disable-line @typescript-eslint/no-unused-vars
const core = this.getCore();
this.setButtonDisabled("compileButton", true);
const basicText = document.getElementById("basicText") as HTMLTextAreaElement;
const compiledText = document.getElementById("compiledText") as HTMLTextAreaElement;
const input = this.basicCm ? this.basicCm.getValue() : basicText.value;
UI.asyncDelay(() => {
const compiledScript = this.core?.compileScript(input) || "";
const compiledScript = core.compileScript(input) || "";

if (this.compiledCm) {
this.compiledCm.setValue(compiledScript);
Expand Down Expand Up @@ -177,9 +190,10 @@ export class UI implements IUI {
}

private onExampleSelectChange(event: Event): void {
const core = this.getCore();
const exampleSelect = event.target as HTMLSelectElement;
const basicText = document.getElementById("basicText") as HTMLTextAreaElement;
const value = this.core?.getExample(exampleSelect.value) || "";
const value = core.getExample(exampleSelect.value) || "";
this.setOutputText("");

if (this.basicCm) {
Expand Down Expand Up @@ -328,8 +342,9 @@ export class UI implements IUI {
return args;
}

public onWindowLoadContinue(core: ICore): void {
public onWindowLoadContinue(core: ICore, vm: IVmAdmin): void {
this.core = core;
this.vm = vm;
const config = core.getConfigObject();
const args = this.parseUri(config);
core.parseArgs(args, config);
Expand Down Expand Up @@ -375,7 +390,7 @@ export class UI implements IUI {
}

UI.asyncDelay(() => {
const exampleObject = this.core?.getExampleObject() || {};
const exampleObject = core.getExampleObject() || {};
this.setExampleSelectOptions(exampleObject);

const example = config.example;
Expand Down
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ if (typeof window !== "undefined") {
window.onload = () => {
const UI = window.locobasicUI.UI; // we expect that it is already loaded in the HTML page
const ui = new UI();
core.setVm(new BasicVmBrowser(ui));
ui.onWindowLoadContinue(core);
ui.onWindowLoadContinue(core, new BasicVmBrowser(ui));
};
} else { // node.js
new NodeParts().nodeMain(core);
Expand Down

0 comments on commit fbe5bc5

Please sign in to comment.