Options
All
  • Public
  • Public/Protected
  • All
Menu

precursor-ts

Precursor

Precursor is a small programming language which you may grow and build upon (a precursor, if you like).

The default distribution consists of 3 modules which work together "out of the box":

example
import { strict as assert } from "assert";
import { CESKM, parse_cbpv, scalar } from "precursor-ts";
import type { Value } from "precursor-ts";

type Base = boolean | null | string | number;
class VM extends CESKM<Base> {
public run(program: string): Value<Base> {
let result = this.step(this.inject(parse_cbpv(program)));
while (!result.done) {
result = this.step(result.value);
}
return result.value;
}
protected literal(v: Base): Value<Base> {
if ("number" === typeof v
|| "boolean" === typeof v
|| "string" === typeof v
|| null === v)
{ return scalar(v); }
throw new Error(`${v} not a primitive value`);
}
protected op(op_sym: string, args: Value<Base>[]): Value<Base> {
switch (op_sym) {
case "op:add": {
if (! ("v" in args[0]) || ! ("v" in args[1]))
{ throw new Error(`arguments must be values`); }
if ("number" !== typeof args[0].v || "number" !== typeof args[1].v)
{ throw new Error(`arguments must be numbers`); }
const result: unknown = args[0].v + args[1].v;
return scalar(result as Base);
}
// ...
default: return super.op(op_sym,args);
}
}
}

const vm = new VM();
try {
const three = vm.run(`
(op:add 1 2)
`);
assert.deepEqual(three, {
v: 3
});
}
catch (e) { console.error(e); }

Index

Classes Continuations & Values

Classes Environment & Store

Classes Language & Syntax

Classes VM

Interfaces Continuations & Values

Type aliases Continuations & Values

Type aliases Language & Syntax

Type aliases VM

Functions Continuations & Values

Functions Language & Syntax

Continuations & Values Type aliases

Kont

Kont<T>: Top | Args<T> | Let<T>

Continuations

A continuation is a computer science way of formalizing the call stack. They represent future work to be performed. The operators "shift" and "reset" are used to manipulate continuations and the machine state to do non-linear control flow, side effects, and other functions.

There are three types of continuation:

see

State

Type parameters

  • T

    The underlying TypeScript types which we wrap in our language.

Top

Top: Record<string, never>

⊤, the "top-level" continuation which has no successor.

remarks

While it is indeed the top-level continutation one notes that its place is at the bottom of the continuation / call stack.

see

Continuations

see

topk

internal

Value

Value<T>: Valuable<T> | { k: Kont<T> }

A value is either a wrapped continuation or a wrapped term of some TypeScript type T.

Type parameters

  • T

    The underlying TypeScript types which we wrap in our language.

Language & Syntax Type aliases

Cbpv

Cbpv: { tag: "cbpv_literal"; v: unknown } | { tag: "cbpv_symbol"; v: string } | { erands: Cbpv[]; op: string; tag: "cbpv_op" } | { exp: Cbpv; tag: "cbpv_suspend" } | { tag: "cbpv_resume"; v: Cbpv } | { args: string[]; body: Cbpv; tag: "cbpv_abstract" } | { erands: Cbpv[]; op: Cbpv; tag: "cbpv_apply" } | { body: Cbpv; exp: Cbpv; tag: "cbpv_let"; v: string[] } | { bindings: [string, Cbpv][]; body: Cbpv; tag: "cbpv_letrec" } | { exp: Cbpv; tag: "cbpv_reset" } | { body: Cbpv; karg: string; tag: "cbpv_shift" } | { c: Cbpv; e: Cbpv; t: Cbpv; tag: "cbpv_if" }

Call-By-Push-Value intermediate language.

VM Type aliases

State

State<T>: { control: Cbpv; environment: Env; kontinuation: Kont<T>; meta: Kont<T>[]; store: Store<T> }

The state of the virtual machine mutated over the course of execution.

Type parameters

  • T

    The underlying TypeScript type forming the basis of values in the language.

Type declaration

Continuations & Values Functions

Const closure

  • Constructs what would commonly be called a "closure".

    remarks

    In call-by-push-value any so-called "negative" term (ie, one which may perform side-effects or modify control flow) may be suspended into a continuation.

    Type parameters

    • T

      The underlying TypeScript type forming the basis of values in the language.

    Parameters

    Returns Value<T>

Const continuation

  • Value constructor for wrapped continuations.

    Type parameters

    • T

      The underlying TypeScript types forming the basis of values in the language. Passed along to nested values.

    Parameters

    Returns Value<T>

Const scalar

  • scalar<T>(v: T): Value<T>
  • Type parameters

    • T

      The underlying TypeScript types forming the basis of values in the language. Passed along to nested values.

    Parameters

    • v: T

    Returns Value<T>

Const topk

  • topk(): Record<string, never>
  • Constructs a continuation.

    Returns Record<string, never>

Language & Syntax Functions

Const build_cbpv

  • build_cbpv(ast: any): Cbpv
  • throws

    Error If the syntax is invalid.

    remarks

    I plagiarized the s-expression parser above and so convert its result into the correct form afterward.

    internal

    Parameters

    • ast: any

      A loosely-typed object representing an s-expression.

    Returns Cbpv

    A proper Cbpv syntax object fit for execution.

Const cbpv_app

Const cbpv_if

Const cbpv_is_positive

  • cbpv_is_positive(expr: Cbpv): boolean
  • A positive term is one which is fully evaluated - eg, applying CESKM.step will not change it. This helper answers whether a given term is positive or not.

    internal

    Parameters

    Returns boolean

Const cbpv_lam

  • cbpv_lam(args: string[], body: Cbpv): Cbpv

Const cbpv_let

Const cbpv_letrec

Const cbpv_lit

  • cbpv_lit(v: unknown): Cbpv

Const cbpv_op

  • cbpv_op(op: string, erands: Cbpv[]): Cbpv

Const cbpv_reset

Const cbpv_resume

Const cbpv_shift

  • cbpv_shift(karg: string, body: Cbpv): Cbpv

Const cbpv_suspend

Const cbpv_sym

  • cbpv_sym(v: string): Cbpv

Const parse_cbpv

  • parse_cbpv(source: string): Cbpv
  • function

    parse_cbpv

    remarks

    The source code is stripped of comments before parsing.

    Parameters

    • source: string

      The source code to parse.

    Returns Cbpv

    A syntax object ready to be executed.

Generated using TypeDoc