Skip to content

Commit

Permalink
library functions without underscore; MODE 2; FN function without arg…
Browse files Browse the repository at this point in the history
…s; flush output on newline; RESTORE dataIndex fixed
  • Loading branch information
benchmarko committed Dec 15, 2024
1 parent d0e32b9 commit da0932d
Show file tree
Hide file tree
Showing 9 changed files with 719 additions and 237 deletions.
732 changes: 574 additions & 158 deletions dist/examples/examples.js

Large diffs are not rendered by default.

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.15</title>
<title>LocoBasic v0.1.16</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.15",
"version": "0.1.16",
"description": "# LocoBasic - Loco BASIC",
"type": "commonjs",
"scripts": {
Expand Down
39 changes: 32 additions & 7 deletions src/Core.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
// core.ts

import type { ICore, ConfigType } from "./Interfaces";
import type { ICore, ConfigType, ConfigEntryType } from "./Interfaces";
import { Parser } from "./Parser";
import { arithmetic } from "./arithmetic";
import { Semantics } from "./Semantics";

const vm = {
_output: "",
_fnOnCls: (() => undefined) as () => void,
_fnOnPrompt: ((_msg: string) => "") as (msg: string) => string,
_fnOnPrint: ((_msg: string) => undefined) as (msg: string) => void, // eslint-disable-line @typescript-eslint/no-unused-vars
_fnOnPrompt: ((_msg: string) => "") as (msg: string) => string, // eslint-disable-line @typescript-eslint/no-unused-vars
cls: () => {
vm._output = "";
vm._fnOnCls();
},
print: (...args: string[]) => vm._output += args.join(''),
print(...args: string[]) {
this._output += args.join('');
if (this._output.endsWith("\n")) {
this._fnOnPrint(this._output);
this._output = "";
}
},
prompt: (msg: string) => {
return vm._fnOnPrompt(msg);
},

getOutput: () => vm._output,
setOutput: (str: string) => vm._output = str,
setOnCls: (fn: () => void) => vm._fnOnCls = fn,
setOnPrint: (fn: (msg: string) => void) => vm._fnOnPrint = fn,
setOnPrompt: (fn: (msg: string) => string) => vm._fnOnPrompt = fn
};


export class Core implements ICore {
private readonly startConfig: ConfigType = {
action: "compile,run",
debug: 0,
example: "",
fileName: "",
Expand All @@ -47,8 +56,8 @@ export class Core implements ICore {
return this.startConfig;
}

public getConfig(name: string) {
return this.startConfig[name];
public getConfig<T extends ConfigEntryType>(name: string) {
return this.startConfig[name] as T;
}

public getExampleObject() {
Expand All @@ -67,6 +76,10 @@ export class Core implements ICore {
vm.setOnCls(fn);
}

public setOnPrint(fn: (msg: string) => void) {
vm.setOnPrint(fn);
}

public setOnPrompt(fn: (msg: string) => string) {
vm.setOnPrompt(fn);
}
Expand All @@ -83,8 +96,7 @@ export class Core implements ICore {
}
this.semantics.resetParser();

const compiledScript = this.arithmeticParser.parseAndEval(script);
return compiledScript;
return this.arithmeticParser.parseAndEval(script);
}

async executeScript(compiledScript: string) {
Expand Down Expand Up @@ -130,4 +142,17 @@ export class Core implements ICore {
}
return output;
}

public putScriptInFrame(script: string) {
const result =
`(function(_o) {
${script}
})({
_output: "",
cls: () => undefined,
print(...args: string[]) { this._output += args.join(''); },
prompt: (msg) => { console.log(msg); return ""; }
});`
return result;
}
}
5 changes: 4 additions & 1 deletion src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ export type ExampleType = Record<string, string>;

export interface ICore {
getConfigObject(): ConfigType,
getConfig(name: string): ConfigEntryType,
getConfig<T extends ConfigEntryType>(name: string): T,
getExampleObject(): ExampleType,
getExample(name: string): string,
setExample(key: string, script: string): void,
compileScript(script: string): string,
executeScript(compiledScript: string): Promise<string>,
putScriptInFrame(script: string): string,
setOnCls(fn: () => void): void,
setOnPrint(fn: (msg: string) => void): void,
setOnPrompt(fn: (msg: string) => string): void,
setOnCheckSyntax(fn: (s: string) => Promise<string>): void
}

export interface IUI {
parseUri(urlQuery: string, config: ConfigType): string[],
onWindowLoad(event: Event): void,
addOutputText(value: string): void,
setOutputText(value: string): void,
checkSyntax(str: string): Promise<string>
}
Loading

0 comments on commit da0932d

Please sign in to comment.