Skip to content

Commit 1260447

Browse files
committed
Minor refactor
1 parent e70576e commit 1260447

10 files changed

Lines changed: 93 additions & 54 deletions

File tree

src/ts/basic/config/commandset.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { GostartCommand } from "../commands/gostart";
2+
import { GosubCommand } from "../commands/gosub";
3+
import { IfCommand } from "../commands/if";
4+
import { KillCommand } from "../commands/kill";
5+
import { PrintCommand } from "../commands/print";
6+
import { SoundbusCommand } from "../commands/soundbus";
7+
import { StopCommand } from "../commands/stop";
8+
import { SubCommand } from "../commands/sub";
9+
import { VarCommand } from "../commands/var";
10+
import { WhileCommand } from "../commands/while";
11+
import type { BasicCommand } from "../engine/command";
12+
13+
export const BasicCommandSet: (typeof BasicCommand)[] = [
14+
PrintCommand,
15+
IfCommand,
16+
VarCommand,
17+
GosubCommand,
18+
SubCommand,
19+
StopCommand,
20+
KillCommand,
21+
GostartCommand,
22+
WhileCommand,
23+
SoundbusCommand,
24+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { IArcTerminal } from "$interfaces/IArcTerminal";
2+
import { EnvFn } from "$ts/basic/functions/env";
3+
import { FsreadFn } from "$ts/basic/functions/fsread";
4+
import { InputFn } from "$ts/basic/functions/input";
5+
import { MathFn } from "$ts/basic/functions/math";
6+
import { UuidFn } from "$ts/basic/functions/uuid";
7+
8+
export function BasicTerminalFunctions(term: IArcTerminal) {
9+
return {
10+
env: EnvFn(),
11+
input: InputFn(),
12+
math: MathFn(),
13+
fsread: FsreadFn(term),
14+
uuid: UuidFn(),
15+
}
16+
}

src/ts/basic/config/term.ts

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
import type { IArcTerminal } from "$interfaces/IArcTerminal";
2-
import { ArcOSVersion, Env } from "$ts/env";
3-
import { ArcBuild } from "$ts/metadata/build";
4-
import { ArcMode } from "$ts/metadata/mode";
2+
import { ArcOSVersion } from "$ts/env";
53
import { BRRED, RESET } from "$ts/terminal/colors";
64
import { unescapeEscapeChars } from "$ts/util";
75
import { arrayBufferToText } from "$ts/util/convert";
8-
import { UUID } from "$ts/util/uuid";
96
import type { BasicLang } from "$types/system/basic";
10-
import { evaluate } from "mathjs";
11-
import { GostartCommand } from "../commands/gostart";
12-
import { GosubCommand } from "../commands/gosub";
13-
import { IfCommand } from "../commands/if";
14-
import { KillCommand } from "../commands/kill";
15-
import { PrintCommand } from "../commands/print";
16-
import { StopCommand } from "../commands/stop";
17-
import { SubCommand } from "../commands/sub";
18-
import { VarCommand } from "../commands/var";
19-
import { WhileCommand } from "../commands/while";
20-
import { SoundbusCommand } from "../commands/soundbus";
7+
import { BasicCommandSet } from "./commandset";
8+
import { BasicTerminalFunctions } from "./functions/terminal";
9+
import { BasicBuiltinVariables } from "./variables";
2110

2211
export function ArcTermBasicConfig(term: IArcTerminal): BasicLang.Config {
2312
return {
2413
version: ArcOSVersion,
2514
stdin: async () => {
15+
// TODO: FIND SOMETHING BETTER
2616
return new Promise((resolve) => {
2717
let val = "";
2818
const disposer = term.term.onKey((e) => {
@@ -45,46 +35,10 @@ export function ArcTermBasicConfig(term: IArcTerminal): BasicLang.Config {
4535
stderr: (msg: string) => {
4636
term.rl?.write(`${BRRED}${msg}${RESET}`);
4737
},
48-
functions: {
49-
env: (val) => {
50-
return Env.get(val) ?? "";
51-
},
52-
input: async (val, interpreter) => {
53-
interpreter.sendToStdout(`\n${val}`);
54-
return await interpreter.getFromStdin();
55-
},
56-
math: (val) => {
57-
return `${evaluate(val)}`;
58-
},
59-
fsread: async (path, interpreter) => {
60-
const content = await term.readFile(path);
61-
if (!content) {
62-
interpreter.error("FILE NOT FOUND", true);
63-
return "";
64-
}
65-
66-
return arrayBufferToText(content) ?? "";
67-
},
68-
uuid: () => UUID(),
69-
len: (s) => s?.length ?? 0,
70-
},
38+
functions: BasicTerminalFunctions(term),
7139
slowdown: 10,
72-
commands: [
73-
PrintCommand,
74-
IfCommand,
75-
VarCommand,
76-
GosubCommand,
77-
SubCommand,
78-
StopCommand,
79-
KillCommand,
80-
GostartCommand,
81-
WhileCommand,
82-
SoundbusCommand,
83-
],
84-
builtinVariables: {
85-
mode: () => ArcMode(),
86-
build: () => ArcBuild(),
87-
},
40+
commands: BasicCommandSet,
41+
builtinVariables: BasicBuiltinVariables,
8842
readScriptFile: async (path) => {
8943
const content = await term.readFile(path);
9044
return content ? arrayBufferToText(content) : undefined;

src/ts/basic/config/variables.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ArcBuild } from "$ts/metadata/build";
2+
import { ArcMode } from "$ts/metadata/mode";
3+
import type { BasicLang } from "$types/system/basic";
4+
5+
export const BasicBuiltinVariables: Record<string, BasicLang.VariableCallback> = {
6+
mode: () => ArcMode(),
7+
build: () => ArcBuild(),
8+
};

src/ts/basic/functions/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Env } from "$ts/env";
2+
import type { BasicLang } from "$types/system/basic";
3+
4+
export const EnvFn = (() => (val) => {
5+
return Env.get(val) ?? "";
6+
}) satisfies BasicLang.TerminalBuiltinFn;

src/ts/basic/functions/fsread.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { arrayBufferToText } from "$ts/util/convert";
2+
import type { BasicLang } from "$types/system/basic";
3+
4+
export const FsreadFn = ((term) => async (val, interpreter) => {
5+
const content = await term.readFile(val);
6+
if (!content) {
7+
interpreter.error("File not found", true);
8+
return "";
9+
}
10+
11+
return arrayBufferToText(content) ?? "";
12+
}) satisfies BasicLang.TerminalBuiltinFn;

src/ts/basic/functions/input.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { BasicLang } from "$types/system/basic";
2+
3+
export const InputFn = (() => async (val, interpreter) => {
4+
interpreter.sendToStdout(`\n${val}`);
5+
6+
return await interpreter.getFromStdin();
7+
}) satisfies BasicLang.TerminalBuiltinFn;

src/ts/basic/functions/math.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { BasicLang } from "$types/system/basic";
2+
import { evaluate } from "mathjs";
3+
4+
export const MathFn = (() => (val) => {
5+
return `${evaluate(val)}`;
6+
}) satisfies BasicLang.TerminalBuiltinFn;

src/ts/basic/functions/uuid.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { UUID } from "$ts/util/uuid";
2+
import type { BasicLang } from "$types/system/basic";
3+
4+
export const UuidFn = (() => () => UUID()) satisfies BasicLang.TerminalBuiltinFn;

src/types/system/basic.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { BasicCommand } from "../../ts/basic/engine/command";
22
import { ArcBasicEngine } from "../../ts/basic/engine";
33
import type { MaybePromise } from "$types/shared/common";
4+
import type { IArcTerminal } from "$interfaces/IArcTerminal";
45

56
export namespace BasicLang {
67
export type Fn = (val: string, interpreter: ArcBasicEngine) => MaybePromise<any>;
78

89
export type StdinCallback = () => Promise<string> | string;
910
export type StdoutCallback = (msg: string) => void;
1011
export type VariableCallback = () => Promise<any> | any;
12+
export type TerminalBuiltinFn = (term: IArcTerminal) => Fn;
1113

1214
export interface Config {
1315
version: string;

0 commit comments

Comments
 (0)