Joe Maghzal

coolc, a compiler for Cool written in Swift

Cool is a small object-oriented language with classes, inheritance, dynamic dispatch and static types. coolc type-checks Cool programs and compiles them through LLVM into native executables.

An example

class Shape {
   name() : String { "shape" };
   area() : Int { 0 };
};

class Square inherits Shape {
   side : Int;
   init(s : Int) : SELF_TYPE { { side <- s; self; } };
   name() : String { "square" };
   area() : Int { side * side };
};

class Main inherits IO {
   main() : Object {
      let s : Shape <- (new Square).init(4) in
         out_string(s.name()).out_string(" has area ")
            .out_int(s.area()).out_string("\n")
   };
};

Compiling and running it:

$ coolc shapes.cl
$ ./shapes
square has area 16

Mistakes are reported with the line they're on:

shapes.cl:3:21: error: Expected expression after '+'
   area() : Int { 4 + };
                    ^

Install

Download the zip from the latest release. It runs on Apple Silicon and Intel Macs.

unzip coolc-0.1.0-macos.zip
xattr -d com.apple.quarantine coolc
./coolc program.cl
./program

Compiling needs clang, from Xcode or the Command Line Tools. coolc program.cl --emit-ir writes the LLVM IR instead.

How it works

  • The parser builds an AST and recovers from mistakes, so it can report several at once.

  • Semantic analysis resolves every name and type. SELF_TYPE is tracked as its own type, the dynamic type of self.

  • Code generation lowers the resolved program to LLVM IR. Each object starts with a vtable that points to its parent, which case expressions walk to find the closest matching branch.

  • clang links the IR with a small C runtime that's embedded in coolc, so the compiler ships as a single binary.

© 2026 Joe Maghzal