circuit(): a spec with its name and external ports, plus a builder callback that adds parts and wires them together.
nand, nand feeds not, and not drives the output.
The builder context
Every wiring call reads destination ← source, like an assignment:
add doesn’t care whether a definition is primitive or composite, and nesting depth is unlimited.
Sources
A source is anything that can drive a wire:- the circuit’s own inputs (
inputs.a) - the outputs of parts you’ve added (
nand.outputs.out)
Naming parts
Part names are optional:- Explicit names must be identifiers and unique within the circuit.
inputsandoutputsare reserved. - Generated names have the form
<Definition>#<n>, counted per definition in the order you calladd, for exampleXOR#0andXOR#1. Leave the name out and pass the inputs second:add(NOT, { a: inputs.a }).
root.fa3.or rather than root.FullAdder#3.OR#0), so prefer them for anything you plan to inspect.
Feedback
Sometimes a part needs a signal from a part that comes later. Memory works this way. Leave that input out of theadd call, then wire it with feed once the source exists:
feed, every feedback wire stands out when you read a circuit. Cycles are never rejected just for being cycles. See Sequential logic for how they behave.
The one exception is a loop made only of wires, with no gate anywhere in it. That can happen through pass-through composites, and since such a wire has no driver, it is reported when the circuit is elaborated.
Checks
The builder checks your wiring and reports problems as aCircuitError prefixed with the circuit name. Most of them are also compile-time errors.
After the builder returns, every part input that was never wired and every output that was never driven is reported together:
When the builder runs
The builder callback runs immediately, insidecircuit(), and the result is checked before circuit() returns. A wiring mistake is reported where you define the circuit, not later when you first simulate it.
It also means a circuit can only contain parts that already exist. A circuit can never contain itself, so every circuit elaborates into a finite set of gates. Builders must be synchronous, and the builder context can’t be used after the callback returns.