ktr is a domain-specific language for parametric sewing patterns. It describes
patterns as geometry and constraints that compile into a stable, diffable
intermediate representation (.ktrir) consumed by runtimes.
Status: Early development (v0.1.0). See Implementation Status below.
input head = 100mm {
assert head > 0mm
assert head < 800mm
}
input target_neck = 200mm {
assert target_neck > 0mm
assert target_neck < 500mm
}
fn neck_quarter(tweak: f64) {
let right = point(tweak * head / 10, 0mm)
let bottom = point(0mm, tweak * head / 12)
let cp1 = right.up(bottom.dy(right) / 2)
let cp2 = bottom.right(bottom.dx(right) / 2)
return bezier(right, cp1, cp2, bottom)
}
let tweak = search (t: f64) {
bounds t [0.6 .. 1.6]
tolerance 1mm
require neck_quarter(t).length == target_neck
}
export neck_quarter(tweak) as "Neck Curve"
- Define sewing patterns as precise geometry with explicit drafting constraints.
- Support fully parametric patterns driven by body measurements.
- Compile to a stable, diffable IR suitable for version control and visual editors.
- Provide TypeScript and Zig runtimes out of the box.
- Expose a C-defined runtime interface, enabling portable runtimes and bindings in any language.
- Ship first-class tooling: compiler, LSP and editor integrations.
ktr is designed for reproducible pattern drafting, not for general-purpose programming.
ktrc/ Compiler (Zig)
src/ Lexer, parser, sema, IR lowering, emit/parse/decompile
spec/ Formal grammars (ktr.ebnf, ktrir.ebnf) and language reference
ktrr/ Runtime (Zig)
src/ IR evaluator, unit normalization, WASM bindings
website/ Documentation site (Astro)
src/pages/ Landing page, reference docs, playground
public/ktrc.wasm Compiler WASM module for the browser playground
public/ktrr.wasm Runtime WASM module for the browser playground
The compiler and runtime require Zig (0.15+).
The WASM build also requires wasm-opt from Binaryen for post-processing optimisations:
# macOS
brew install binaryen
# or grab a release from https://github.com/WebAssembly/binaryen/releasescd ktrc
# Run all tests
zig build test
# Build the CLI
zig build
# Compile a .ktr file to .ktrir
zig build run -- compile program.ktr
zig build run -- compile program.ktr -o output.ktrir
# Decompile .ktrir back to .ktr
zig build run -- decompile output.ktrir
# Pipe through both directions
cat file.ktr | zig build run -- compile | zig build run -- decompile
# Build the WASM module (requires wasm-opt from Binaryen)
zig build wasmcd ktrr
# Run all tests
zig build test
# Build the WASM module (requires wasm-opt from Binaryen)
zig build wasmThe website requires Bun:
cd website
bun install
bun run devThe full language reference is in ktrc/spec/lang.md.
Standalone EBNF grammars:
ktr.ebnf-- source language grammarktrir.ebnf-- intermediate representation grammar
The compiler currently implements:
-
letbindings with literal values (100mm,50%,42) -
letbindings with identifier references (let y = x) - Arithmetic expressions (
+,-,*,/) with precedence - Parenthesized grouping
- Type inference for
length,percentage,f64 - Duplicate binding detection
- Undefined reference detection
- Poison type for error suppression
- IR lowering, emission, parsing, and decompilation
- Full roundtrip:
.ktr→ IR →.ktrir→ IR →.ktr - WASM build target with browser playground
- Runtime evaluator (
ktrr) with unit normalization (cm → mm) - Runtime WASM module with JSON output
-
inputdeclarations with literal defaults (input head = 100mm) - Runtime input overrides (parametric evaluation)
-
point,bezier,lineconstructor types with full pipeline support -
fndefinitions with typed parameters - Function calls (user-defined and built-in constructors)
Planned:
-
inputassertion blocks (assert head > 0mm) - Unary negation (
-expr) - Method calls (
.up(),.dx(), etc.) -
searchsolver blocks -
exportstatements -
booltype