๐Ÿ“ฆ huangsam / virtuc

A piece of C with LLVM ๐Ÿฐ

โ˜… 0 stars โ‘‚ 0 forks ๐Ÿ‘ 0 watching โš–๏ธ MIT License
clangcompilerllvmparserrust
๐Ÿ“ฅ Clone https://github.com/huangsam/virtuc.git
HTTPS git clone https://github.com/huangsam/virtuc.git
SSH git clone git@github.com:huangsam/virtuc.git
CLI gh repo clone huangsam/virtuc
๐Ÿ“‚ 4ba8ca1c493b992fef328672e83539a0f57eb9fa View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ src
๐Ÿ“ tests
๐Ÿ“„ .gitignore
๐Ÿ“„ AGENTS.md
๐Ÿ“„ Cargo.lock
๐Ÿ“„ Cargo.toml
๐Ÿ“„ LICENSE
๐Ÿ“„ README.md
๐Ÿ“„ README.md

VirtuC

GitHub Actions Workflow Status License

A compiler for a subset of the C programming language, implemented in Rust. VirtuC supports lexical analysis, parsing, semantic analysis, code generation to LLVM IR, and execution via a custom virtual machine.

Features

  • Lexer: Tokenizes C subset source code using the logos crate.
  • Parser: Parses tokens into an Abstract Syntax Tree (AST) using the nom parser combinator library.
  • Semantic Analysis: Performs type checking, symbol resolution, and validation.
  • Code Generation: Translates AST to LLVM Intermediate Representation (IR) using the inkwell crate.
  • Virtual Machine: Executes compiled bytecode with a stack-based architecture.
  • CLI: Command-line interface for compiling source files to executables.

Supported C Subset

  • Primitive types: int (64-bit), float (64-bit)
  • Variables and assignments
  • Arithmetic and comparison operators
  • Control flow: if-else, for loops
  • Functions with parameters and return values
  • Function calls

Building

Ensure you have Rust and LLVM installed. Then:

cargo build --release

Usage

Compile a C subset source file to an executable:

cargo run -- compile source.c -o output

Run the executable:

./output