* Documentation

Compiler Architecture

Overview of the Turflang Compiler Pipeline.

Turflang is a statically compiled programming language and compiler framework that integrates static analysis, control-flow reasoning, and tiered AI-assisted diagnostics into a unified architecture.

Pipeline Stages

The compilation pipeline translates high-level Turflang code into native machine code through several robust stages:

  1. Lexer: Tokenizes the input source code.
  2. Syntax Analyzer: Validates the structure and grammatic correctness of tokens.
  3. Semantic Analyzer: Ensures types, scopes, and context-dependent language rules are upheld.
  4. Parser: Constructs the Abstract Syntax Tree (AST).
  5. IR Generator: Translates the AST into Intermediate Representation.
  6. LLVM Backend: Leverages established LLVM optimization passes and generates native machine code without introducing a customized backend.

Example: Code Generation Stage

// Example of how the IR generator might interface with LLVM
llvm::Function* createFunction() {
    llvm::FunctionType *FT = llvm::FunctionType::get
        (llvm::Type::getInt32Ty(Context), false);
    llvm::Function *F = llvm::Function::Create
        (FT, llvm::Function::ExternalLinkage, "main", Module);
    
    llvm::BasicBlock *BB = llvm::BasicBlock::Create(Context, "entry", F);
    Builder.SetInsertPoint(BB);
    
    // Generate inner instructions...
    
    Builder.CreateRet(llvm::ConstantInt::get(Context, llvm::APInt(32, 0)));
    return F;
}

By retaining LLVM for backend optimization, Turflang gains industry-grade performance while maintaining the focus of its research on intelligent, embedded diagnostic reporting.