wavegraph-compiler 0.1.0

Compile named textual audio patches to WaveGraph signal bytecode
Documentation
# WaveGraph Language Grammar Notes

WaveGraph Language (WGL) starts with a graph that a musician can write, a
reviewer can read one-line-at-- time, and a compiler can resolve against the
host's catalog of DSP objects. The [language reference](README.md#language)
describes the accepted statements. The [PEG grammar](src/syntax.rs) implements
their syntax.

## Influences

[Pure Data (Pd)][pd] gives us a vocabulary of objects, inlets, outlets, and
patch "cords". On a Pd canvas, a cord visibly joins two boxes. Its saved form,
`#X connect 2 0 5 1;`, identifies the source and destination by their
positions in the canvas's object list and by their port numbers. Inserting a
box earlier in that list can force unrelated connections to be renumbered.
Max's modern `.maxpat` JSON gives each box an ID, but a typical endpoint
such as `["obj-14", 0]` still takes a lookup to understand. In WGL,
`connect drums909.kick strip1.key` explains which signal drives the sidechain
even in isolation. Names also permit forward references. The compiler assigns
the compact numeric bindings when it builds the graph.

Cmajor's named endpoints and left-to-right chains show how little text a serial
signal path can take. Faust permits serial composition with `A : B` when
the number of outputs of `A` equals the number of inputs of `B`.
Any future WGL chain shorthand should resolve to explicit connections.
For a strip with both an audio inlet and a sidechain inlet, guessing an inlet
could silently turn a sidechain into the main audio path.

## Where the graph grammar ends

A host can add a processor and its constructor options without teaching the
parser its name. Consider `steps=36!+39,42,-` in the desktop drum sequencer.
The graph grammar divides the option at the first `=`. The sequencer interprets
`+` as simultaneous hits and `!` as an accent. The delay interprets the
value in `beats=3/4` as a beat division. Pd's expression objects and Max's GenExpr
`codebox` are precedents for this boundary. Within a Max Gen patcher, text
expressions and graphical operators compile to the same representation.

Rhythmic grouping is a plausible extension of the sequencer's notation.
In TidalCycles, `bd [sd sd]` fits two snares in the second half of a cycle.
A WGL extension needs to specify how its groups subdivide an existing step.
Literal spelling also matters. A comma within a Pd expression box is escaped
as `\,` when saved in a patch. WGL currently reserves `#` anywhere for
comments, so `C#4` cannot pass through as a note name. Adding pitch literals
means deciding how accidentals and comments coexist.

## Why a Parsing Expression Grammar? (PEG)

Each current statement starts with a distinct keyword and occupies one line.
A PEG rule can describe the entire production, from the keyword through its
operands to the Rust value it constructs. The parser can borrow both names
in `drums909.kick` from the source. A later pass checks the host catalog for
the named outlet and resolves it to a bus. This division also gives errors a
natural home. The grammar can identify `99` as an invalid alias in
`node 99 voice`, while the resolver can identify an unknown outlet on an
otherwise valid node. The generated parser works with `no_std` and `alloc`.

PEG choice is ordered. In `A / B`, a successful `A` wins. Adding a broad
bare-word alternative before a more specific form can silently shadow it.
Keep keyword boundaries explicit and require the entire statement to match.
For example, `gainful` is a valid alias, and using it as a statement keyword in
`gainful v.out 64` should produce an error. These five keyword forms need
little of the conflict analysis an LR generator offers. If expressions become
part of WGL, `peg` also supplies precedence climbing for a local expression rule.
[Rust PEG reference][peg]

## Open questions

These are design sketches. The current compiler parses each physical line
independently. Multiline blocks would need a patch-level rule that consumes
newlines explicitly and keeps their locations for diagnostics.

- A reusable `patch Acid(in notes: Note, out audio: Signal)` needs scoped
  names and per-instance state. In Pd, `$1` in an abstraction's object box
  names its first creation argument. Inside a message box the same text
  selects the first argument of an incoming message. WGL parameters and event
  arguments should name their bindings where each enters scope.
- In `every 1/16 as step`, the duration belongs to the clock. In the accent
  expression `step % 4 == 0`, remainder binds more tightly than equality.
  The grammar can express both, but a scheduler still has to decide when an
  edit such as `at next_bar` becomes visible. Pd control fan-out can depend
  on which patch cord was drawn first. Its `[trigger]` object makes order
  explicit. A WGL score should define whether the new pattern plays on the
  first step of that bar, including when the bar starts inside an audio block.

## See also:

- [Pd patch writer][pd-writer]
- [Pd expression examples][pd-expr]
- [Pd escaping][pd-escaping]
- [Pd abstractions][pd-abstractions]
- [Pd message order][pd-order]
- [Hillerson on editing and merging Pd patches][pd-editing]
- [Max patch example][max-patch]
- [Cmajor connections][cmajor]
- [Faust composition][faust]
- [GenExpr][max-gen]
- [Tidal mini-notation][tidal]

[pd]: https://puredata.info/
[pd-writer]: https://github.com/pure-data/pure-data/blob/bfa6db08d00a3eb73fb0ae37264391dc3dcad19c/src/g_readwrite.c#L770-L780
[pd-editing]: https://media.pragprog.com/titles/thsound/intro.pdf#page=10
[pd-expr]: https://github.com/pure-data/pure-data/blob/bfa6db08d00a3eb73fb0ae37264391dc3dcad19c/doc/5.reference/expr-help.pd
[pd-escaping]: https://msp.ucsd.edu/Pd_documentation/2.theory.of.operation.htm#s2.4.6
[pd-abstractions]: https://github.com/pure-data/pure-data/blob/bfa6db08d00a3eb73fb0ae37264391dc3dcad19c/doc/1.manual/2.theory.of.operation.htm#L1806-L1818
[pd-order]: https://msp.ucsd.edu/Pd_documentation/2.theory.of.operation.htm#s2.4.3
[max-patch]: https://github.com/Cycling74/n4m-examples/blob/master/echo/n4m.echo.maxpat
[max-gen]: https://docs.cycling74.com/userguide/gen/gen_genexpr/
[cmajor]: https://cmajor.dev/docs/LanguageReference#connections
[faust]: https://faustdoc.grame.fr/manual/syntax/#diagram-composition-operations
[tidal]: https://tidalcycles.org/docs/reference/mini_notation/
[peg]: https://docs.rs/peg/0.8/peg/