Skip to main content
A primitive is the smallest unit the simulator runs. You define one with gate() by giving it a name, its ports and a pure evaluation function.

The evaluation function

evaluate receives the current value of every input and must return an object with a value for every declared output and nothing else.
  • It must be pure and synchronous. The simulator may call it any number of times and assumes the same inputs always give the same outputs.
  • It must handle "X". Decide which inputs dominate (like 0 for NAND), and return "X" when the answer is truly unknown.
  • The inputs object it receives is frozen.
The simulator checks every result. Missing outputs, extra outputs, non-signal values like true or 2, and thrown exceptions all become a SimulationError that names the primitive and its path:

Multiple outputs

Primitives can have any number of inputs and outputs:

Constant sources

A primitive with no inputs is evaluated once on the first tick, which makes it a constant:

Typing

Port names are captured as literal types, so the evaluation function’s parameter and return type are checked:

Naming rules

Gate names and port names must be identifiers: letters, digits and underscores, not starting with a digit. Port names must be unique across a gate’s inputs and outputs combined. Breaking either rule throws a CircuitError immediately.
The standard library uses NAND as its only primitive, but you are free to define others. For example, an AND3 primitive keeps a large design fast when you don’t need to see its gate-level detail.