diff --git a/src/3x5.ts b/src/3x5.ts index dbe127b..7bff5aa 100644 --- a/src/3x5.ts +++ b/src/3x5.ts @@ -1,19 +1,8 @@ -import { ALL as HTML } from "./lib/html"; -import { parse } from "./parser"; -import { runNoctl, Vm } from "./vm"; -import { AsHtml, AsText, TextPiece } from "./words"; - -/** - * Basic unit of information, also an "actor" in the programming system - */ -type Card = { - /** Unique identifier */ - id: number; - /** Key-value properties on the card */ - fields: Record; - /** Eventually: a markdown string containing code, but for now, just code */ - code: string; -}; +import { Card, CardVm, GetField } from './lib/card'; +import { ALL as HTML } from './lib/html'; +import { parse } from './parser'; +import { runNoctl, Vm } from './vm'; +import { AsHtml, AsText, TextPiece } from './words'; /** * Updates a card's fields @@ -43,7 +32,7 @@ function parseFields(card: Card, fields: string) { * @param code Script to run * @returns Markup to render / output */ -function renderCard(state: Vm, code: string) { +function renderCard(state: CardVm, code: string) { const script = parse(code); if (script[0]) { runNoctl(state, script[1], (word) => (state.output += AsHtml(word) + "\n")); @@ -128,10 +117,11 @@ function render() { parseFields(theCard, fieldInput.value); theCard.code = codeInput.value; - const vm: Vm = { + const vm: CardVm = { mode: "render", - commands: { ...HTML }, + commands: { ...HTML, get: GetField }, output: "", + card: theCard, }; const html = renderCard(vm, theCard.code); @@ -145,7 +135,7 @@ document.body.append( fieldInput, codeInput, rerender, - state, display, + state, debugDisplay ); diff --git a/src/lib/card.ts b/src/lib/card.ts new file mode 100644 index 0000000..7c925fa --- /dev/null +++ b/src/lib/card.ts @@ -0,0 +1,25 @@ +import { Vm } from '../vm'; +import { TextPiece } from '../words'; +import { getOpt } from './options'; + +/** + * Basic unit of information, also an "actor" in the programming system + */ +export type Card = { + /** Unique identifier */ + id: number; + /** Key-value properties on the card */ + fields: Record; + /** Eventually: a markdown string containing code, but for now, just code */ + code: string; +}; + +export type CardVm = Vm<{ + card: Card; +}>; + +export function GetField(vm: CardVm, argv: TextPiece[]): TextPiece { + const [{ error = false }, fieldName] = getOpt(argv, { min: 1, max: 1 }); + // TODO: handle error + return { text: vm.card.fields[fieldName] ?? "" }; +}