TUI C Interpreter/Debugger in Rust
CRusTTY
A pedagogical C interpreter with time-travel debugging capabilities, built in Rust with a terminal-based UI.

Overview
CRusTTY is an educational tool for understanding C program execution. It provides:
- Interactive Execution: Step through C code line by line
- Time-Travel Debugging: Step backward and forward through execution history
- Memory Visualization: Real-time view of stack and heap memory
- Terminal Output: See printf output as your program runs
Features
Supported C Subset
- Data Types:
int,char,void, structs, pointers, arrays - Control Flow:
if/else,while,for,do-while,switch/case - Operators: Arithmetic, logical, bitwise, comparison, ternary
- Memory: Stack-based local variables, dynamic heap allocation via
malloc/free - Built-ins:
printfandscanf(with format specifiers),malloc,free,sizeof
TUI Interface
The terminal interface provides multiple panes:
- Source Code: Syntax-highlighted C code with execution line indicator
- Stack: Call stack with local variables and their values
- Heap: Dynamic memory allocations with type information
- Terminal: Output from
printfand input prompts fromscanf - Status Bar: Keybindings and execution state
Keybindings
n/Space: Step forwardb: Step backwardc: Continue executionr: Restart programq: Quitesc: Exit input mode (inscanfinput prompt)- Arrow keys: Navigate through stack/heap panes
Quick Start
Precompiled binaries for the following platforms will be available in the releases section of the GitHub repository:
- Windows (x86_64 and ARM)
- macOS (x86_64 and ARM)
- Linux (x86_64 and ARM)
.zip) for your platform, extract it, and add the directory containing the binary to your PATH.
Usage
crustty <source.c | example_name>
Examples:
# Run the bundled comprehensive example (features demo)
crustty default
Run your own C file
crustty path/to/your/file.c
Installation From Source
Prerequisites
- Rust toolchain (1.70 or later)
- Terminal with Unicode and color support
Building
cargo build --release
The binary will be available at target/release/crustty.
Project Structure
The codebase is organized into focused modules for maintainability:
src/
โโโ main.rs # Entry point and CLI argument handling
โโโ lib.rs # Library root and public API
โ
โโโ parser/ # C parser (source โ AST)
โ โโโ mod.rs # Module overview
โ โโโ lexer.rs # Tokenizer: source text โ Token stream
โ โโโ parse.rs # Parser entry point (Parser struct)
โ โโโ declarations.rs # Struct/function declaration parsing
โ โโโ statements.rs # Statement parsing
โ โโโ expressions.rs # Expression parsing with precedence climbing
โ โโโ ast.rs # AST node type definitions
โ
โโโ interpreter/ # C interpreter (AST โ execution)
โ โโโ mod.rs # Module overview and execution model docs
โ โโโ engine.rs # Interpreter struct, run(), rewind(), snapshots
โ โโโ statements.rs # Statement dispatch (if, while, decl, โฆ)
โ โโโ expressions.rs # Expression evaluation (largest file)
โ โโโ builtins.rs # Built-in functions (printf, scanf, malloc, free)
โ โโโ type_system.rs # Type inference helpers
โ โโโ loops.rs # while / do-while / for loop execution
โ โโโ jumps.rs # return / switch execution
โ โโโ heap_serial.rs # Value โ heap byte serialization
โ โโโ errors.rs # RuntimeError enum
โ โโโ constants.rs # Address-space constants
โ โโโ ops/ # Operator implementations (impl Interpreter)
โ โโโ mod.rs # Submodule declarations
โ โโโ binary.rs # Arithmetic, comparison, bitwise, compound-assign
โ โโโ unary.rs # Negation, NOT, increment/decrement
โ โโโ assign.rs # Assignment to lvalues
โ โโโ access.rs # Reading lvalues and resolving addresses
โ โโโ structs.rs # Struct initialization and field-offset helpers
โ
โโโ memory/ # Runtime memory model
โ โโโ mod.rs # sizeof, pointer arithmetic helpers
โ โโโ stack.rs # Call frames and local variables
โ โโโ heap.rs # First-fit heap allocator
โ โโโ value.rs # Value enum (Int, Char, Pointer, Struct, โฆ)
โ
โโโ snapshot/ # Time-travel debugging
โ โโโ mod.rs # Snapshot, SnapshotManager, MockTerminal
โ
โโโ ui/ # Terminal UI (ratatui + crossterm)
โโโ mod.rs # Module re-exports
โโโ app.rs # App struct, event loop, pane focus, scanf input
โโโ theme.rs # Color palette (DEFAULT_THEME)
โโโ panes/ # Stateless pane render functions
โโโ mod.rs # Re-exports for all pane modules
โโโ source.rs # Syntax-highlighted source code pane
โโโ stack.rs # Call stack visualization pane
โโโ heap.rs # Heap block visualization pane
โโโ terminal.rs # printf / scanf terminal output pane
โโโ status.rs # Status bar (keybindings, step counter)
โโโ utils/ # Shared rendering helpers
โโโ mod.rs # Re-exports from submodules
โโโ formatting.rs # Value/address formatting
โโโ memory.rs # Stack/heap data extraction helpers
โโโ rendering.rs # Low-level ratatui span/line builders
Architecture
Parser
Hand-written recursive descent parser with:
- Precedence climbing for binary operators
- Comprehensive error reporting with source locations
- Full AST representation of program structure
Interpreter
Executes the AST with:
- Stack-based execution with call frames
- Heap memory allocator with block tracking
- Snapshot system capturing execution state after each statement
- Specific error types for clear diagnostics
engine.rs owns the core
loop and public API; operator evaluation lives under ops/; loop control flow in
loops.rs; jump-style control flow (return, switch) in jumps.rs; and heap
serialization in heap_serial.rs.
Memory Model
- Stack: Local variables, function parameters, return addresses
0x0000_0004 and up (sequential variable IDs per frame)
- Heap: Dynamic allocations via
malloc
0x7fff_0000 and up
- First-fit allocation strategy
The two regions occupy non-overlapping address ranges so the TUI can distinguish stack and heap pointers without type annotation, and pointer arithmetic can be range-checked cheaply.
Time-Travel Debugging
The interpreter maintains a history of execution snapshots, each containing:
- Complete stack state
- Complete heap state
- Terminal output
- Current source location
Performance Optimizations
Recent optimizations include:
- Inline Hints: Hot-path functions marked with
#[inline] - Field Caching: Struct field offsets cached to avoid recomputation
- Fast Hashing:
FxHashMapused for non-cryptographic hashing needs
Development
Running Tests
# Unit tests
cargo test --lib
Integration tests
cargo test --test '*'
All tests
cargo test
Code Quality
# Format code
cargo fmt
Run linter
cargo clippy -- -D warnings
Check build
cargo check
Configuration Files
rustfmt.toml: Code formatting rulesCargo.toml: Dependencies and build configuration
Limitations
This is NOT a production C interpreter:
- Subset of C (no preprocessor, typedefs, unions, enums, function pointers)
- No optimization or JIT compilation
- Limited standard library (only built-in functions)
- No file I/O or external system interaction
- Fixed memory sizes
License
MIT License. See LICENSE.md file for details.