aicheye
crustty
Rust

TUI C Interpreter/Debugger in Rust

Last updated Mar 29, 2026
54
Stars
1
Forks
0
Issues
0
Stars/day
Attention Score
26
Language breakdown
No language data available.
โ–ธ Files click to expand
README

CRusTTY

A pedagogical C interpreter with time-travel debugging capabilities, built in Rust with a terminal-based UI.

CRusTTY Screenshot

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: printf and scanf (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 printf and input prompts from scanf
  • Status Bar: Keybindings and execution state

Keybindings

  • n / Space: Step forward
  • b: Step backward
  • c: Continue execution
  • r: Restart program
  • q: Quit
  • esc: Exit input mode (in scanf input 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)
Download the appropriate binary (.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
The interpreter code is split across focused submodules: 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
- Address space: 0x0000_0004 and up (sequential variable IDs per frame)
  • Heap: Dynamic allocations via malloc
- Address space: 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
This enables stepping backward through execution to any previous point.

Performance Optimizations

Recent optimizations include:

  • Inline Hints: Hot-path functions marked with #[inline]
  • Field Caching: Struct field offsets cached to avoid recomputation
  • Fast Hashing: FxHashMap used 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 rules
  • Cargo.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.

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท aicheye/crustty ยท Updated daily from GitHub