Skip to content

abulgit/Mini-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini Compiler

I built this to understand how compilers actually work (at least in theory). Not just reading about it, but trying to implement each phase myself.

Why Rust?

I'm learning Rust, and it's memory-safe without garbage collection. Building this project seemed like a good way to practice Rust concepts.

What This Does

Takes simple math expressions and variables, compiles them to bytecode, then runs them.

let x = 10
let y = 20
print x + y

That's it.

The Pipeline

Lexer -> Breaks text into tokens (numbers, operators, keywords)

Parser -> Builds a tree structure (AST) from tokens using recursive descent

Compiler -> Converts the tree into bytecode instructions

VM -> Executes the bytecode on a stack

Running It

cargo run examples/tests.txt
cargo run examples/variables.txt
cargo run examples/errors.txt

What I Learned

  • How operator precedence works (multiplication before addition)
  • Why recursive descent parsing is intuitive
  • Stack-based VM Implementations are simpler than I thought
  • Error handling with line/column numbers

The Code

  • lexer.rs - Character by character scanning
  • parser.rs - Recursive descent, one function per precedence level
  • compiler.rs - Walks the AST and emits bytecode
  • vm.rs - Stack machine that executes instructions
  • bytecode.rs - Instruction definitions (Add, Multiply, LoadVar, etc.)
  • ast.rs - Tree node types
  • token.rs - Token types
  • error.rs - Error handling with locations

Language Features

Variables, arithmetic (+, -, *, /), parentheses, negative numbers, print statements, comments. That's all.

Could Add Later

  • If-Else statements
  • Loops
  • Functions
  • Strings

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages