2023-09-08 04:03:48 +00:00
|
|
|
import {
|
2023-10-20 21:47:51 +00:00
|
|
|
AsText,
|
|
|
|
Concat,
|
2023-10-20 23:06:01 +00:00
|
|
|
ErrorResult,
|
2023-10-20 21:47:51 +00:00
|
|
|
InterpolatedPiece,
|
2023-10-20 23:06:01 +00:00
|
|
|
ProcResult,
|
2023-10-20 21:47:51 +00:00
|
|
|
Script,
|
|
|
|
TextPiece,
|
|
|
|
Word,
|
|
|
|
} from "./words";
|
2023-09-08 04:03:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* "Mode" of the environment a script runs in; determines access to mutability features and such.
|
|
|
|
*
|
|
|
|
* "action": response to a UI action; allowed to modify card fields and access time and random numbers.
|
|
|
|
*
|
|
|
|
* "render": deterministic generation of display markup from card and workspace state; can only modify temporary variables.
|
|
|
|
*/
|
|
|
|
export type ScriptType = "action" | "render";
|
|
|
|
|
2023-10-19 02:54:38 +00:00
|
|
|
export type Proc<Context> = (
|
|
|
|
state: Vm<Context>,
|
|
|
|
argv: TextPiece[]
|
2023-10-20 23:06:01 +00:00
|
|
|
) => ProcResult;
|
2023-09-08 04:03:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* State for running a script in.
|
|
|
|
*/
|
2023-10-19 02:54:38 +00:00
|
|
|
export type Vm<Context = {}> = {
|
2023-09-08 04:03:48 +00:00
|
|
|
/** Mutability status */
|
|
|
|
mode: ScriptType;
|
|
|
|
/** Implementations of commands scripts can run */
|
2023-10-19 02:54:38 +00:00
|
|
|
commands: Record<string, Proc<Context>>;
|
2023-09-08 04:03:48 +00:00
|
|
|
/** Markup to render / output */
|
|
|
|
output: string;
|
2023-10-19 02:54:38 +00:00
|
|
|
} & Context;
|
2023-09-08 04:03:48 +00:00
|
|
|
|
2023-10-19 02:54:38 +00:00
|
|
|
function evaluateWord<Context>(
|
|
|
|
state: Vm<Context>,
|
2023-09-08 04:03:48 +00:00
|
|
|
word: Word | InterpolatedPiece
|
2023-10-20 23:06:01 +00:00
|
|
|
): TextPiece | ErrorResult {
|
2023-10-19 00:06:52 +00:00
|
|
|
if ("bare" in word || "text" in word || "html" in word) {
|
2023-09-08 04:03:48 +00:00
|
|
|
return word;
|
|
|
|
} else if ("variable" in word) {
|
|
|
|
return { text: "" };
|
|
|
|
} else if ("script" in word) {
|
|
|
|
return runNoctl(state, word.script);
|
|
|
|
} else {
|
2023-10-20 23:06:01 +00:00
|
|
|
let fullWord = null;
|
|
|
|
for (const piece of word.pieces) {
|
|
|
|
const result = evaluateWord(state, piece);
|
|
|
|
if ("error" in result) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
fullWord = Concat(fullWord, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fullWord ?? { text: "" };
|
2023-09-08 04:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a script in the context of a Noctl state. Potentially mutates the state.
|
|
|
|
*
|
|
|
|
* @param onReturn callback optionally invoked with the return word for each top-level command (not triggered by command substitutions)
|
|
|
|
* @returns the return word of the final command in the script, or empty text if the script is empty.
|
|
|
|
*/
|
2023-10-19 02:54:38 +00:00
|
|
|
export function runNoctl<Context>(
|
|
|
|
state: Vm<Context>,
|
2023-09-08 04:03:48 +00:00
|
|
|
script: Script,
|
2023-10-20 23:06:01 +00:00
|
|
|
onReturn?: (word: TextPiece | ErrorResult) => void
|
|
|
|
): TextPiece | ErrorResult {
|
|
|
|
let returnWord: TextPiece | ErrorResult = { text: "" };
|
|
|
|
|
|
|
|
for (const command of script) {
|
|
|
|
const argv: TextPiece[] = [];
|
|
|
|
for (const word of command) {
|
|
|
|
const processedWord = evaluateWord(state, word);
|
|
|
|
if ("error" in processedWord) {
|
|
|
|
onReturn?.(processedWord);
|
|
|
|
return processedWord;
|
|
|
|
} else {
|
|
|
|
argv.push(processedWord);
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 04:03:48 +00:00
|
|
|
|
|
|
|
const name = AsText(argv[0]);
|
|
|
|
if (name in state.commands) {
|
|
|
|
returnWord = state.commands[name](state, argv);
|
|
|
|
} else {
|
2023-10-20 23:06:01 +00:00
|
|
|
returnWord = { error: `Unknown Command: ${name}` };
|
2023-09-08 04:03:48 +00:00
|
|
|
}
|
|
|
|
onReturn?.(returnWord);
|
2023-10-20 23:06:01 +00:00
|
|
|
}
|
2023-09-08 04:03:48 +00:00
|
|
|
|
|
|
|
return returnWord;
|
|
|
|
}
|