-
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 - Uses Pest-compatible Parser Expression Grammars for syntax definition
- Declarative Semantics - JSON command blocks describe how to build AST nodes
- 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
- Semantic actions - JSON blocks defining what AST nodes to create
// Grammar rule
integer_literal = @{ "-"? ~ ASCII_DIGIT+ }
-> TypedExpression {
"get_text": true,
"parse_int": true,
"define": "int_literal",
"args": { "value": "$result" }
}
When this rule matches, Zyn:
- Extracts the matched text (
get_text) - Parses it as an integer (
parse_int) - Calls
create_int_literal(value)on the AST builder
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 │
│ (*.zig) │
└────────┬────────┘
│
▼
┌─────────────────┐ ┌─────────────────┐
│ Zyn Grammar │────▶│ Pest Parser │
│ (*.zyn) │ │ (generated) │
└─────────────────┘ └────────┬────────┘
│
▼
┌─────────────────┐
│ Command │
│ Interpreter │
└────────┬────────┘
│
▼
┌─────────────────┐
│ TypedAST │
│ Builder │
└────────┬────────┘
│
▼
┌─────────────────┐
│ TypedProgram │
│ (JSON/Binary) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Compiler │
│ Backend │
└─────────────────┘
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 JSON commands 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