coolr
is a compiler for the COOL programming language targeting 32-bit x86 architecture on Linux. It is written in C++ using only standard libraries - no lexing or parsing generators are used.
After completing the Compilers course from StanfordOnline, I had produced my very first working compiler. I was quite happy with this achievement, yet I somehow felt a bit dissatisfied.
- I didn't really understand how the modules of my compiler powered by Flex and Bison worked; the inner workings of those tools still seemed like magic to me.
- I didn't really understand the support code provided by the course instructors. It made the assignments much more manageable, but it felt like cheating, especially in regards to the abstract syntax tree and other important data structures.
- I didn't like using the SPIM emulator to execute my compiled programs. I wanted my compiler to produce x86 code I could actually run directly on my own machine and debug with gdb.
To satisfy my compiler cravings, I decided to rm -rf
my existing course project and start from scratch - no Flex, no Bison, no support code and no SPIM. This repository contains the result of this venture.
COOL (Classroom Object-Oriented Language) is a programming language designed for teaching compiler construction. For a language designed as a teaching tool, it is actually fairly complex and includes features such as:
- Object-oriented features: Inheritance, method overriding, static and dynamic dispatches.
- Type features: Static typing, type safety and support for dynamic type checking.
- Run-time error handling: Clean exit on run-time errors - no segfaults!
A precise and formal description of the language is given in the COOL manual by Alex Aiken. I've included the manual in this repository for reference.
To try out the compiler, simply clone the repository and build the project. This requires C++17 or later.
git clone https://github.com/JeppW/coolr
cd coolr
make coolr
Once completed, you can compile a program using ./coolr filename.cl
. A few examples have been provided in examples/
. This will output a NASM file which you can assemble into a working binary with the provided assemble.sh
script.
~# ./coolr examples/hello_world.cl --out out.S && scripts/assemble.sh out.S myprogram
~# ./myprogram
Hello, world!
Naturally, this will only work on machines that support 32-bit x86 architecture.
The grading test cases from the StanfordOnline Compilers course have been used to test this compiler. Some of them have been slightly altered to reflect the changes I've introduced along the way. Where relevant, this has been described in the README files of the compiler modules in the src/compiler
directory.
All tests are currently passing. You can run the tests yourself by navigating to the subdirectories of the tests/
directory and executing the test.sh
scripts.
This compiler is divided into four modules: a lexical analyzer, a parser, a semantic analyzer and a code generator. The inner workings of each of the four modules are described in detail in the README files in their respective subfolders in src/compiler
.
As it turns out, making compilers is pretty complicated. I have fixed a number of obscure bugs and I would expect more still persist. If you decide to give this compiler a spin and encounter a problem, I would love to know about it, so feel free to raise an issue.