Implement basic script evaluation
This commit is contained in:
parent
1b8929657a
commit
ba3a3b5a26
2 changed files with 79 additions and 21 deletions
src
26
src/3x5.ts
26
src/3x5.ts
|
@ -1,4 +1,6 @@
|
|||
import { parse } from "./parser";
|
||||
import { parse } from './parser';
|
||||
import { runNoctl, Vm } from './vm';
|
||||
import { AsHtml } from './words';
|
||||
|
||||
/**
|
||||
* Basic unit of information, also an "actor" in the programming system
|
||||
|
@ -12,25 +14,6 @@ type Card = {
|
|||
code: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* "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.
|
||||
*/
|
||||
type ScriptType = "action" | "render";
|
||||
|
||||
/**
|
||||
* State for running a script in.
|
||||
*/
|
||||
type Vm = {
|
||||
/** Mutability status */
|
||||
mode: ScriptType;
|
||||
/** Markup to render / output */
|
||||
output: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param state VM state
|
||||
* @param code Script to run
|
||||
|
@ -39,7 +22,7 @@ type Vm = {
|
|||
function renderCard(state: Vm, code: string) {
|
||||
const script = parse(code);
|
||||
if (script[0]) {
|
||||
state.output = JSON.stringify(script[1], null, 2);
|
||||
runNoctl(state, script[1], (word) => (state.output += AsHtml(word) + "\n"));
|
||||
} else {
|
||||
state.output = script[1];
|
||||
}
|
||||
|
@ -101,6 +84,7 @@ const debugDisplay = document.createElement("pre");
|
|||
function render() {
|
||||
const vm: Vm = {
|
||||
mode: "render",
|
||||
commands: {},
|
||||
output: "",
|
||||
};
|
||||
const html = renderCard(vm, theCard.code);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue