xenusynth

A compiler synthesizer

XenuSynth

XenuSynth is a block-oriented language workbench, written in Crystal. A .xns file is a specification of a language: its syntax, abstract syntax, static checks, semantics, evaluator, and code generator. The example in examples/TillyZ.xns is the normative specimen used by the appendix; examples/MiniC.xns is a second, C-like profile that also drives the compiler plugin pipeline.

The core knows how to find blocks, ask registered handlers to validate them, assemble a frontend, and expose its service contract as JSON. It does not know what a block means. New block kinds, analyses, and backends are added through the SDK rather than by editing the reader, parser, or command-line driver.

Build and check

From the repository root:

make            # builds build/xenusynth
make check      # runs check.sh
make spec       # runs crystal spec
make test       # check + spec

check.sh detects the checkout layout. When this repository is the xenusynth/ directory inside the book, the same targets work from the book root:

make -C xenusynth
make -C xenusynth check
make -C xenusynth spec

make check validates the TillyZ specification, runs every reference case in the specification, compares the evaluator with the textual bytecode VM, checks the documented error codes, exercises the coverage extension block, and builds a generated standalone frontend. When examples/MiniC.xns is present and non-empty it also checks the MiniC reference cases, the immutable-binding error, assembly emission, every pipeline stage, and execution of the emitted assembly on the MiniRISC reference machine. make plugins type-checks the top-level plugin loader shims under plugins/.

Commands

xs=build/xenusynth

$xs check examples/TillyZ.xns
$xs check examples/TillyZ.xns --semantics=denotational
$xs describe --json

printf '%s' 'let x = 2; x + 3;' \
  | $xs run examples/TillyZ.xns
# 5

printf '%s' 'var n = 0; while (n < 3) { n = n + 1; } n;' \
  | $xs compile examples/TillyZ.xns > build/tillyz.tbc
$xs execute build/tillyz.tbc
# 3

run and compile read stdin unless --source=TEXT or --source-file=FILE is supplied. --semantics accepts denotational, action, hoare, or the full block name; requesting an absent formalism is an error.

lower and emulate drive the compiler plugins:

$xs lower SPEC.xns [--source=TEXT | --source-file=FILE] \
  [--target=NAME] [--stage=STAGE] [--out=FILE] [--json]
$xs emulate FILE [--target=NAME]

lower checks the source and runs the backend selected by target in the specification's lowering block (or by --target). Without --stage it emits the final assembly. With --stage=cfg|isel|schedule|regalloc|emit it stops after that pass and prints a deterministic text artifact. --json reports the target, the stage, every recorded artifact, and the selected content. emulate executes a final artifact; emulate - reads it from stdin. The MiniRISC backend is registered under minirisc and mini-risc.

The MiniC profile and MiniRISC

examples/MiniC.xns is a C-like profile with int/bool declarations (const for immutable bindings), assignment, blocks, if/else, while, and for, C-style operator precedence, and // / /* ... */ comments. It lowers to the same active AST that the checker and evaluator understand, so check, run, and compile work on it too.

$xs check examples/MiniC.xns --semantics=denotational

$xs run examples/MiniC.xns --source='int x = 2; x + 3;'
# 5

$xs lower examples/MiniC.xns --source='int x = 2; x + 3;' > build/minic.asm
$xs emulate build/minic.asm
# 5

$xs lower examples/MiniC.xns --source='int x = 2; x + 3;' --stage=cfg

Small complete programs live in examples/minic/; each carries an // expect: VALUE comment that the end-to-end spec uses as its oracle.

The plugin pipeline is:

ISel.select(checked)                     -> MFunction
CtrlFlow.build(function)                 -> CFG
ISched.schedule(function, cfg)           -> MFunction
RegAlloc.allocate(scheduled, cfg)        -> Allocation
MiniRISC.emit(scheduled, allocation)     -> assembly text
MiniRISC.run_assembly(assembly text)     -> displayed result

MiniRISC is a small register machine. Its textual assembly declares .function, .frame, .result, .registers, and .scratch, then contains labelled basic blocks with three-address operations, frame loads/stores, branches, and ret. Spilled values are materialised through the reserved scratch registers; the interpreter uses arbitrary-precision integers and traps E_DIV_ZERO.

Build an interoperable frontend

$xs build examples/TillyZ.xns --out build/tillyz
crystal build --release build/tillyz/tillyz.cr -o build/tillyz/tillyz
printf '%s' 'let x = 2; x + 3;' | build/tillyz/tillyz run -

build writes two artifacts:

  • tillyz.xsm.json: a normalized specification manifest. The schema is xenusynth/spec@1; each block carries its kind, name, body, and source range.
  • tillyz.cr: a Crystal driver with the manifest embedded and a copy of the runtime under src/. The driver can be compiled without the original .xns file.

The manifest is the interoperability boundary. Another tool can read it, add blocks, or generate a frontend in another language without parsing Crystal.

Layout

src/xenusynth/
  sdk.cr              SDK: Block, Spec, Context, handlers, registry
  reader.cr           .xns block framing and source locations
  backend.cr          SDK::Backend and BackendRegistry
  meta/               meta-grammar, actions, absyn schema
  runtime/            generated lexer, parser, checker, evaluator, bytecode
  plugins/
    ir.cr             machine IR: MFunction, MBlock, MInstr, MOp, VReg
    isel.cr           instruction selection
    ctrlflow.cr       CFG, dominators, loops, liveness
    isched.cr         basic-block list scheduling
    regalloc.cr       linear-scan allocation and spilling
    minirisc.cr       pipeline orchestration, emission, interpreter, backend
    install.cr        registers the lowering handler and MiniRISC backend
  ext/
    coverage.cr       example SDK extension
    lowering.cr       lowering block parser and configuration
  blocks/handlers.cr  built-in block handlers
  frontend.cr         validated parse/check/evaluate/compile service
  generated.cr        manifest-driven generated frontend
  cli.cr              command-line interface
examples/
  TillyZ.xns          normative language specimen
  MiniC.xns           C-like profile for the plugin pipeline
  minic/*.mc          small MiniC programs with expected results
plugins/              top-level loader shims for the compiler plugins
spec/                 unit, contract, and end-to-end pipeline specs

Add a block

A handler implements XenuSynth::SDK::BlockHandler:

class MyHandler < XenuSynth::SDK::BlockHandler
  def kind : String
    "my-block"
  end

  def validate(block : XenuSynth::Block, spec : XenuSynth::Spec,
               ctx : XenuSynth::Context) : Nil
    ctx.error("E_MY_BLOCK", "message", block.span) unless block.body.includes?("ok")
  end
end

XenuSynth::SDK::Registry.register(MyHandler.new)

src/xenusynth/ext/coverage.cr is a complete example. It reads require Constructor lines and checks them against the absyn schema. Add the file to api.cr and register it in Blocks.install_extensions!; no core file changes.

A compiler backend implements XenuSynth::SDK::Backend and registers itself with SDK::BackendRegistry.register. src/xenusynth/plugins/minirisc.cr shows the target, stage, and execute contract.

Limits

The built-in runtime implements the active TillyZ and MiniC profiles. The declarative static-check, semantic-primitive, denotational, action, and Hoare blocks are validated as contracts; the shared checker and evaluator are the executable reference. The plugin pipeline currently produces textual artifacts and runs them on the MiniRISC reference interpreter; there is no native object or machine-code backend. The parser is recursive descent with memoization and requires a non-left-recursive grammar. There is no host eval, filesystem access, or foreign-function interface in the generated frontend.

Repository

xenusynth

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • 28 minutes ago
  • December 6, 2026
License

MIT License

Links
Synced at

Sun, 06 Dec 2026 03:35:38 GMT

Languages