Skip to main content

Architecture

Infr follows a classic compiler pipeline:

Source (.infr) → Lexer → Parser → Checker → Transpiler → Output (.R)

The implementation is in Rust (2024 edition).

Core modules

src/lexer/

Tokenizer for R + Infr extensions. Produces a Token stream with Span positions for source mapping. Handles pragmas (@infr-ignore, @infr-nocheck).

src/parser/

Recursive descent parser with operator precedence climbing. ast.rs defines the full AST:

  • ProgramVec<Stmt>
  • StmtStmtKind (assignments, expressions, control flow)
  • ExprExprKind (literals, operators, calls, etc.)

src/types/

Type definitions and the type environment:

  • Type enum — all types in the system (including literal types and Overloaded for function overloads)
  • TypeEnvironment — scoped symbol table
  • BindingInfo — tracks const/let/plain status
  • Assignability rules between types (literal types widen to base types)

src/checker/

The type checker. Handles:

  • Type inference from literals, operators, and known functions
  • Type narrowing via guards (is.*(), is.null())
  • Const enforcement
  • Type alias resolution (stored in a type_aliases registry)
  • Literal type checking (LiteralString, LiteralNumeric, etc.)
  • Function overload resolution (picks best matching signature from Type::Overloaded)
  • Diagnostic generation
  • builtins.rs — ~350+ built-in R function signatures

src/transpiler/

AST → R code generation. Strips const/let keywords and type annotations, preserving comments and structure. Output should be indistinguishable from hand-written R.

src/declarations/

Parser for .d.infr declaration files (analogous to TypeScript's .d.ts files).

src/config/

Parses infr.toml project configuration.

src/cli/

Clap subcommands: check, build, watch, init, lsp.

src/lsp/

Language Server Protocol implementation via tower-lsp/tokio. Provides diagnostics, hover, completion, inlay hints (inferred types for unannotated variables), and code actions (add type annotation, convert to const/let).

Other key directories

DirectoryDescription
declarations/ (root)Built-in .d.infr type declarations for R packages
editors/vscode/VS Code extension (TextMate grammar + LSP client)
tests/conformance/Self-contained .infr test files with expected diagnostics
tests/snapshots/Input/output .infr.R pairs for snapshot testing