TigerC is a complete compiler implementation for the Tiger programming language, written in Rust. Tiger is a Pascal-like educational language from Andrew Appel's "Modern Compiler Implementation" books.
- Full Compiler Pipeline: Lexical analysis → Parsing → Semantic analysis → IR generation → x86-64 assembly → Executable
- Modern Implementation: Written in Rust with clean, modular architecture
- Tiger Language Support: Complete implementation including nested functions, arrays, records, and type inference
- x86-64 Code Generation: Native assembly output with register allocation
- Runtime Library: Memory management and I/O support for compiled programs
Install on Ubuntu/Debian:
sudo apt-get install nasm gccInstall on macOS:
brew install nasm
# GCC is provided by Xcode Command Line Tools# Build debug version
cargo build
# Build release version
cargo build --releaseCompile a Tiger source file:
cargo run -- input.tig output.tOr use the built binary:
./target/debug/tigerc input.tig output.tcargo run -- ./tigerc/tests/testcases/queens.tig ./tigerc/tests/testcases/queens.t
./tigerc/tests/testcases/queens.t/
├── tigerc/ # Main compiler crate
│ ├── src/ # Compiler source modules
│ └── tests/ # Test files and test cases
├── tigerc-macros/ # Procedural macros for symbol generation
└── runtime/ # Runtime static library for compiled programs
- Tokenization (
tokenizer.rs) - Lexical analysis with Unicode support - Parsing (
parser.rs) - Pratt (top-down operator precedence) parser - Escape Analysis (
escape.rs) - Detects variables escaping their scope - Type Checking (
type_inference.rs) - Semantic analysis and type inference - Translation (
translate.rs,ir_gen.rs) - AST to Intermediate Representation - Canonicalization (
canon.rs) - IR normalization and basic block formation - Code Generation (
asm_gen.rs,amd64.rs) - x86-64 assembly generation - Assembly & Linking - NASM + GCC to produce executable
# Run all tests
cargo test
# Run tests for specific crate
cargo test -p tigerc
# Run specific test
cargo test test_compile3
# Show print output during tests
cargo test -- --nocapture
# Update snapshots (when output intentionally changes)
cargo insta reviewTiger is a statically-typed language with:
- Primitive types:
int,string,nil - Composite types: Arrays, Records
- Control flow:
if-then-else,while,for,break - Functions: Nested functions with static links, recursion
- Variables: Mutable with
:=assignment - Let blocks: Declaration scopes with
let...in...end
let
var N := 8
type intArray = array of int
var col := intArray[N] of 0
function printBoard() = (
for i := 0 to N - 1 do (
for j := 0 to N - 1 do
print(if col[i] = j then " O" else " .");
print("\n")
);
print("\n")
)
in
printBoard()
end
# Check, format, lint
cargo check
cargo fmt
cargo clippy
# Run with debug logging
RUST_LOG=debug cargo run -- input.tig output.tThis project is for educational purposes.