diff --git a/src/3x5.ts b/src/3x5.ts index 2d2b410..128bf07 100644 --- a/src/3x5.ts +++ b/src/3x5.ts @@ -1,7 +1,8 @@ import { Card, CardVm, GetField } from "./lib/card"; +import { Expr } from "./lib/expr"; import { ALL as HTML } from "./lib/html"; import { parse } from "./parser"; -import { runNoctl, Vm } from "./vm"; +import { runNoctl } from "./vm"; import { AsHtml, AsText, TextPiece } from "./words"; /** @@ -119,7 +120,7 @@ function render() { const vm: CardVm = { mode: "render", - commands: { ...HTML, get: GetField }, + commands: { ...HTML, get: GetField, expr: Expr }, output: "", card: theCard, }; diff --git a/src/lib/expr.ts b/src/lib/expr.ts new file mode 100644 index 0000000..59e01f3 --- /dev/null +++ b/src/lib/expr.ts @@ -0,0 +1,12 @@ +import { AsText, ProcResult, TextPiece } from "../words"; + +export function Expr({}, argv: TextPiece[]): ProcResult { + const name = argv[0]; + if ("bare" in name && name.bare == "expr") { + // being called as [expr ...], not fallback for math + argv.splice(0, 1); + } + return { + text: `Will do math to solve ${argv.map(AsText).join(" ")} eventually`, + }; +} diff --git a/src/vm.ts b/src/vm.ts index e88e725..a187e71 100644 --- a/src/vm.ts +++ b/src/vm.ts @@ -59,6 +59,8 @@ function evaluateWord( } } +const NUMBER = /^\d+$/; + /** * Runs a script in the context of a Noctl state. Potentially mutates the state. * @@ -87,6 +89,8 @@ export function runNoctl( const name = AsText(argv[0]); if (name in state.commands) { returnWord = state.commands[name](state, argv); + } else if (NUMBER.test(name) && "expr" in state.commands) { + returnWord = state.commands.expr(state, argv); } else { returnWord = { error: `Unknown Command: ${name}` }; }