AlexDev

Join the Gang Gang and have the latest AI and Tech content.

Home page View on GitHub

Cool Compiler

Posted on 2 July 2024.
c data structures algorithms

About

Compiler for the Classroom Object-Oriented Language.

[!NOTE] This is project contains some extra features compared to the standard COOL language. This is because I want to be able to do interop with assembly code.

The compiler is written in C and it generates assembly code for x86-64. More over, the compiler will run the fasm assembler to generate an object file and then link it with ld.

The compiler can be stopped at different stages of the compilation process by using the --lex, --syn, --sem, --map, --tac and --asm flags.

The compiler accepts multiple files as positional arguments. It will parse each file individually and then merge the resulting ASTs into a single one. This allows for the definition of classes in different files and follows the same semantics as the original COOL compiler. Then it will run the semantic analysis and generate the assembly code.

The standard library of the COOL language is split into modules which can be loaded at the compile phase by using the --module flag. The available modules are:

You can watch the entire playlist of me building this thing live on YouTube https://www.youtube.com/watch?v=ekQLFFgGoXo&list=PLwHDUsnIdlMy52QnKX-2Unl6Hmfm9A6jt.

Requirements

Quickstart

To compile a single file use

make
./coolc <file.cl>

this will create a file main with the executable.

To run the compiler for a specific stage use

make
./coolc [--lex | --syn | --sem | --map | --tac | --asm] [--o outfile] <file.cl> ...

To run the checker for a specific implementation use

./checker.sh [--lex | --syn | --sem | --tac | --asm]

To compile the examples with the coolc compiler use

make examples

this will generate all the example executables in the build folder.

To create a distributable version of the compiler use

make dist

this will create coolc.tar.gz with the compiler and the standard library.

Conclusion