A Nock interpreter
npm install @tdjsnelling/nockA small Nock interpreter written in TypeScript. Use it either:
- as an interactive REPL, or
- as a module for evaluating Nock formulas from your own code.
```
yarn add @tdjsnelling/nock
``
git clone https://github.com/tdjsnelling/nock
yarn install
yarn build
yarn start:repl
#### What you can do in the REPL
- Paste/type a Nock expression in the bracketed, space-separated form.
- The REPL will parse it and evaluate it, printing the result.
The package entry point exports:
- parse(input: string) to turn a string into a Nock cell (AST/noun structure)types
- the public types from
- the interpreter functionality as default
`ts
import Nock, { parse } from "nock";
const formula = parse("[42 0 1]");
const subject = formula[0]; // 42
const argument = formula[1]; // [0, 1]
const interpreter = new Nock();
const result = interpreter.nock(subject, argument);
console.log(result);
`
The interpreter constructor can take some extra options:
- onHint: the function to call for the 'hint' opcode (11). Must return a noun (in which case this noun is the result of the evaluation), or null (in which case the hint acts as a side effect and normal evaluation of the operation proceeds).debug
- : enable debugging features.customDebug
- : by default, if debug is enabled, console.log is used to print helpful info during evaluation. This default behaviour can be replaced by providing a customDebug function which receives the parameters that would ordinarily be logged to the console.
`ts`
const interpreter = new Nock({
onHint: (tag: Noun, clue: Noun | null, subject: Noun, argument: Cell) => Noun | null,
debug: boolean,
customDebug: (data: DebugPayload) => void,
})
If you only need parsing (e.g., to validate or transform input), you can do:
`ts
import { parse } from "nock";
const cell = parse("[[1 2] [3 4]]");
console.log(cell);
`
`ts
import { checkAtom, checkCell } from "nock";
checkAtom(42); // Ok
checkAtom([0, 1]); // Exception
checkCell(42); // Exception
checkCell([0, 1]); // Ok
`
The package ships some TypeScript types:
- Atom: a natural number
- Cell: an ordered pair of nouns
- Noun: atom or cell
`ts``
import type { Atom, Cell, Noun } from "nock";