2023-08-26 00:38:52 +00:00
|
|
|
import { parse } from "./parser";
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-07-22 04:19:16 +00:00
|
|
|
/**
|
2023-08-05 05:09:33 +00:00
|
|
|
* Basic unit of information, also an "actor" in the programming system
|
2023-07-22 04:19:16 +00:00
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
type Card = {
|
|
|
|
/** Unique identifier */
|
|
|
|
id: number;
|
|
|
|
/** Key-value properties on the card */
|
|
|
|
fields: Record<string, string>;
|
|
|
|
/** Eventually: a markdown string containing code, but for now, just code */
|
|
|
|
code: string;
|
|
|
|
};
|
2023-07-22 02:19:10 +00:00
|
|
|
|
2023-07-27 02:14:16 +00:00
|
|
|
/**
|
2023-08-05 05:09:33 +00:00
|
|
|
* "Mode" of the environment a script runs in; determines access to mutability features and such.
|
2023-07-27 02:14:16 +00:00
|
|
|
*
|
|
|
|
* "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.
|
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
type ScriptType = "action" | "render";
|
|
|
|
|
2023-07-27 02:14:16 +00:00
|
|
|
/**
|
2023-08-05 05:09:33 +00:00
|
|
|
* State for running a script in.
|
2023-07-27 02:14:16 +00:00
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
type Vm = {
|
|
|
|
/** Mutability status */
|
|
|
|
mode: ScriptType;
|
|
|
|
/** Markup to render / output */
|
|
|
|
output: string;
|
|
|
|
};
|
2023-07-27 02:14:16 +00:00
|
|
|
|
|
|
|
/**
|
2023-08-06 06:09:30 +00:00
|
|
|
* @param state VM state
|
|
|
|
* @param code Script to run
|
|
|
|
* @returns Markup to render / output
|
2023-07-27 02:14:16 +00:00
|
|
|
*/
|
2023-08-06 06:09:30 +00:00
|
|
|
function renderCard(state: Vm, code: string) {
|
2023-08-05 05:09:33 +00:00
|
|
|
const script = parse(code);
|
2023-08-04 04:26:15 +00:00
|
|
|
if (script[0]) {
|
|
|
|
state.output = JSON.stringify(script[1], null, 2);
|
|
|
|
} else {
|
|
|
|
state.output = script[1];
|
|
|
|
}
|
2023-07-27 02:14:16 +00:00
|
|
|
return state.output;
|
|
|
|
}
|
|
|
|
|
2023-07-22 04:19:16 +00:00
|
|
|
/**
|
|
|
|
* Global state: a single card
|
|
|
|
*/
|
2023-08-06 06:09:30 +00:00
|
|
|
let theCard: Card = {
|
2023-07-22 04:19:16 +00:00
|
|
|
id: 100,
|
|
|
|
fields: {},
|
2023-07-28 01:08:34 +00:00
|
|
|
code: String.raw`
|
2023-07-28 22:48:09 +00:00
|
|
|
h1 "Hello, World!"
|
2023-07-22 04:19:16 +00:00
|
|
|
para [2 + 2]
|
|
|
|
block {
|
2023-07-27 02:14:16 +00:00
|
|
|
This is a paragraph of text, with one [b bold] word. Yes, this means there has to be some magic in text processing... <b>this</b> won't work.
|
2023-07-22 04:19:16 +00:00
|
|
|
}
|
|
|
|
block -red "Beware!"
|
2023-07-27 00:06:21 +00:00
|
|
|
para "All text should be quoted, it's clearer that way. & blockquotes already should contain paragraphs. (maybe normalize nested paragraphs)"
|
|
|
|
block {
|
|
|
|
First block
|
|
|
|
} {
|
|
|
|
Second block
|
|
|
|
|
|
|
|
Is this markdown-parsed?
|
|
|
|
|
2023-08-04 19:52:26 +00:00
|
|
|
[button "No we want to render UI" \\{noop}]
|
2023-07-27 00:06:21 +00:00
|
|
|
} {
|
|
|
|
Since we want escapes to work, these blocks [i will] be subject to substitutions.
|
2023-08-04 19:52:26 +00:00
|
|
|
}
|
2023-07-27 05:54:06 +00:00
|
|
|
# A comment
|
2023-07-27 02:14:16 +00:00
|
|
|
para {
|
|
|
|
line endings escaped\
|
|
|
|
one slash
|
|
|
|
|
|
|
|
not escaped if \\
|
|
|
|
two slashes
|
|
|
|
|
|
|
|
escaped with a slash if \\\
|
|
|
|
three slashes
|
|
|
|
|
|
|
|
not escaped with two slashes if \\\\
|
|
|
|
four slashes
|
|
|
|
|
|
|
|
escaped with two slashes if \\\\\
|
|
|
|
five slashes
|
|
|
|
|
|
|
|
not escaped with three slashes if \\\\\\
|
|
|
|
six slashes
|
|
|
|
}
|
2023-07-22 04:19:16 +00:00
|
|
|
`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const state = document.createElement("pre");
|
|
|
|
const display = document.createElement("blockquote");
|
2023-07-27 02:14:16 +00:00
|
|
|
const debugDisplay = document.createElement("pre");
|
2023-07-22 04:19:16 +00:00
|
|
|
|
|
|
|
function render() {
|
2023-08-06 06:09:30 +00:00
|
|
|
const vm: Vm = {
|
|
|
|
mode: "render",
|
2023-07-28 22:49:43 +00:00
|
|
|
output: "",
|
|
|
|
};
|
|
|
|
const html = renderCard(vm, theCard.code);
|
2023-07-27 02:14:16 +00:00
|
|
|
|
2023-07-22 04:19:16 +00:00
|
|
|
state.textContent = JSON.stringify(theCard, null, 2);
|
2023-07-27 02:14:16 +00:00
|
|
|
display.innerHTML = html;
|
|
|
|
debugDisplay.textContent = html;
|
2023-07-22 04:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render();
|
2023-07-27 02:14:16 +00:00
|
|
|
document.body.append(state, display, debugDisplay);
|