-
-
Notifications
You must be signed in to change notification settings - Fork 0
01 Introduction
Zyn (ZynPEG) is a grammar-driven language frontend system that transforms source code into a typed abstract syntax tree (TypedAST). It combines three powerful concepts:
- PEG Parsing - Parser Expression Grammars with Packrat memoization for O(n) parsing
- TypedAST Actions - Typed construct expressions build AST nodes directly at parse time
- Universal TypedAST - A target representation that supports multiple programming paradigms
Building a language frontend traditionally requires:
Source Code β Lexer β Parser β AST Builder β Type Checker β IR
Each stage requires significant code:
- Lexer rules and token definitions
- Parser with precedence handling
- AST node types and construction logic
- Visitor patterns for tree traversal
Zyn collapses the parser and AST builder into a single declarative specification:
Source Code β Zyn Grammar β TypedAST β Compiler Backend
A Zyn grammar file (.zyn) contains:
- Grammar rules - PEG syntax defining what to parse, with named bindings
- Semantic actions - TypedAST construct expressions defining what AST nodes to create
// Atomic rule (@) captures matched text automatically
// The binding name 'integer_literal' holds that text in the action
integer_literal = @{ "-"? ~ ASCII_DIGIT+ }
-> TypedExpression::IntLiteral { value: integer_literal }
When this rule matches, Zyn:
- Captures the matched text automatically (the
@modifier) - Evaluates the action expression, constructing a
TypedExpression::IntLiteralnode - Returns the node directly to the parent rule β no separate AST building pass needed
The TypedAST is a universal intermediate representation that can express:
- Multiple paradigms: OOP, functional, procedural
- Rich type system: Generics, traits, enums, structs
- Language features: Pattern matching, async/await, error handling
Every node carries:
- Type information - Full type annotation
- Source spans - Location for error reporting
- Semantic data - Names, operators, modifiers
pub struct TypedNode<T> {
pub node: T, // The actual AST node
pub ty: Type, // Type annotation
pub span: Span, // Source location
}Define a new language syntax in hours, not weeks. The grammar and semantics live in one file.
Declarative specifications are easier to verify than imperative AST construction code.
The TypedAST can target multiple backends: JIT compilation, LLVM, interpreters.
Grammar changes automatically propagate to AST construction - no separate files to update.
βββββββββββββββββββ
β Source File β
β (*.zynml) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ ββββββββββββββββββββββββ
β Zyn Grammar ββββββΆβ GrammarInterpreter β
β (*.zyn) β β (Packrat memoized β
βββββββββββββββββββ β PEG, O(n) time) β
ββββββββββββ¬ββββββββββββ
β
β TypedAST actions
β evaluated inline
βΌ
βββββββββββββββββββ
β TypedProgram β
β (direct, no β
β JSON/VM) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Compiler β
β Backend β
β (HIR β SSA β β
β Native Code) β
βββββββββββββββββββ
In the following chapters, you'll learn:
- Chapter 2: Set up your environment and write your first grammar
- Chapter 3: Use the CLI for compilation and REPL testing
- Chapter 4: Master PEG syntax for parsing
- Chapter 5: Use TypedAST actions to build AST nodes
- Chapter 6: Understand the TypedAST structure
- Chapter 7: Use the builder API directly
- Chapter 8: Walk through a complete Zig implementation