How Turf Works
Turflang is compiled through a robust pipeline integrating static analysis, control-flow reasoning, and a unique tiered AI-assisted diagnostic architecture. It lexes and parses your .tr file into an AST, performs semantic checks (including multi-tier diagnostics using localized SLMs when opted-in), and leverages established LLVM optimization passes to generate a native executable.
The primary research contribution lies in its three-tier diagnostic architecture:
- Tier 1: Deterministic, rule-based diagnostics translating specific error categories to structured hints.
- Tier 2: Offline AI component operating under constrained memory budgets for context-sensitive suggestions.
- Tier 3 (Optional): Online AI-assisted analysis interacting with remote compute for broader contextual reasoning or automatic error correction.
Compilation pipeline stages
- Lexer: Tokenizes the input source code and registers built-in keywords.
- Syntax Analyzer & Parser: Validates grammatic correctness and builds the AST for expressions like assignments, conditionals, loops, and functions.
- Semantic Analyzer: Ensures types, scopes, and context rules are upheld, embedding the adaptive diagnostic mechanisms.
- IR Generator: Translates the structured AST into Intermediate Representation.
- LLVM Backend: Targets native machine code by reusing established optimization passes, preserving safety and performance.
Forward function calls
The compiler uses a pre-pass that hoists function prototypes, so you can call a function before its definition.
Debug: emit LLVM IR
turf example.tr -o example --emit-llvm