boba
is a compiler project to learn about implementing type system, code optimization and code generation. It compiles Rust-like source code to x86-64 assembly.
Example:
fn sum(array: [i32; 5]) -> i32 {
let mut total = 0;
for (let mut i = 0; i < 5; i = i + 1) {
total = total + array[i];
}
return total;
}
fn main() {
let array = [1, 2, 3, 4, 5];
let total = sum(array);
println("Sum of {} = {}", array, total); // Sum of [1, 2, 3, 4, 5] = 15
}
To build the CLI:
cargo build
NOTE: boba requires rustc
version 1.70.0 or newer.
Usage: boba [OPTIONS] <FILENAME>
Arguments:
<FILENAME> Name of the file to be compiled
Options:
-p, --print-ast Print the parsed AST
-h, --help Print help
-V, --version Print version
The compiler generates a x86-64 assembly (AT&T syntax) file. Use gcc
or clang
to compile the assembly to an ELF executable and then run it on your system.
gcc <FILENAME>.s && ./a.out
Check language documentation for details about the syntax and semantics of the language.
The test/ directory contains a bunch of example programs that can be compiled with this compiler.
To test the compiler against all the programs in the directory:
cargo test
NOTE: This requires gcc
to be available on your $PATH
.
An attempt to document my development process: