Back to projects

RustedBrains

Compiler-learning project that tokenizes a small Rust subset, builds an AST, and generates Brainfuck from variables, arithmetic, control flow, and print statements.

PrototypeRust to Brainfuck transpiler / 2025

Problem

Compiler concepts are easier to understand when they are implemented end to end, even against a deliberately tiny source language and target runtime.

Solution

Build a pure-Rust CLI that reads source, prints debug output, then emits `.bf` through a lexer, parser, AST model, generator, and error layer.

Key Features

Lexer for identifiers, integer literals, keywords, operators, braces, parentheses, and semicolons
Recursive descent parser with expression precedence for equality, comparison, addition/subtraction, and multiplication/division
AST model for let bindings, assignments, print calls, if statements, while loops, and binary expressions
Brainfuck generator with variable cell allocation, temporary cells, value copying, arithmetic helpers, and output instructions
Position-aware transpiler errors and CLI failure handling
Example programs for simple variables, arithmetic/comparison, and control flow
Integration tests for compilation, output file creation, and missing-file error handling

Architecture Map

Mermaid
flowchart LR
  Source["Subset Rust source file"] --> CLI["RustedBrains CLI"]
  CLI --> Lexer["Lexer: tokens and position errors"]
  Lexer --> Parser["Recursive descent parser"]
  Parser --> AST["AST: statements and expressions"]
  AST --> Codegen["Brainfuck code generator"]
  Codegen --> Memory["Memory cells: variables and temps"]
  Memory --> Output[".bf output file"]
  Parser --> Debug["Debug output: tokens and AST"]
  Codegen --> Debug