Skip to content

joschmaCYU/brouss

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Brouss

A small educational compiler for a simple C-like language that emits x86-64 NASM assembly.

You can visualize it

Overview

Inspired by: Compiler Tutorial Series

The compiler reads a .bro source file and produces out.asm. You can assemble and link it on Linux to get an executable.

Requirements

  • CMake 3.10+
  • C++17 compiler
  • NASM
  • GNU (works only on linux)

Build

cd build
make

Run

By default, the binary looks for test.bro in the build directory and writes out.asm and out.o.

cd build
./brouss
nasm -felf64 out.asm -o out.o
./out
echo $?

Example

// This is a comment
int x = 1 * 3 - 6 / 2 + 2

string str = "Hello World! "

while(x) {
  print(str)
  x = x - 1
}

if (x) {
  exit(20)
} elif (1) {
  exit(1)
} else {
  exit(0)
}

Grammar

Formal grammar (BNF-like) with simple operator precedence:

$$ \begin{aligned} [\text{Prog}] [\text{Prog}] &\to [\text{Stmt}]^* \\ [\text{Statement}] &\to \begin{cases} \text{exit}([\text{Expr}]) \\ \text{TYPE}\ \text{ident} = [\text{Expr}] \\ \text{ident} = [\text{Expr}] \\ \text{print}([\text{Expr}]) \\ \text{if } ([\text{Expr}]) [\text{Scope}] \\ \text{while } ([\text{Expr}]) [\text{Scope}] \\ [\text{Scope}] \end{cases} \\ [\text{Scope}] &\to { [\text{Statement}]^* } \\ [\text{Expr}] &\to [\text{Term}] \mid [\text{BinExpr}] \\ [\text{BinExpr}] &\to \begin{cases} [\text{Expr}] * [\text{Expr}] & \text{priority} = 1 \\ [\text{Expr}] / [\text{Expr}] & \text{priority} = 1 \\ [\text{Expr}] + [\text{Expr}] & \text{priority} = 0 \\ [\text{Expr}] - [\text{Expr}] & \text{priority} = 0 \end{cases} \\ [\text{Term}] &\to \begin{cases} \text{int\_lit} \\ \text{string\_lit} \\ \text{ident} \\ ([\text{Expr}]) \end{cases} \end{aligned} $$

About

My first compiler it's a test project!

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors