A complete compiler for the COOL (Classroom Object-Oriented Language) programming language, written in Rust with Windows x64 assembly code generation.
- Complete lexical analysis, parsing, and semantic analysis
- Three-address code (TAC) intermediate representation
- Native Windows x64 assembly generation (FASM)
- Runtime library with built-in classes (Object, IO, Int, String, Bool)
- Support for all COOL language features including inheritance, dynamic dispatch, and case expressions
- Rust (latest stable version)
- FASM (Flat Assembler) for Windows x64
- Download from https://flatassembler.net/
- Or install via Chocolatey:
choco install fasm
-
Clone the repository:
git clone <repository-url> cd cool_compiler
-
Build the compiler:
cargo build --release
-
Ensure FASM is in your PATH:
# PowerShell (temporary) $env:Path += ";C:\path\to\fasm" # Or install via Chocolatey choco install fasm
Compile a COOL program:
cargo run --release -- -i <input.cl> -o <output>This will generate <output>.exe which can be run directly on Windows.
# Compile hello world
cargo run --release -- -i test/hello.cl -o hello
# Run the program
.\hello.exe-i <file>- Input COOL source file-o <name>- Output executable name (without .exe extension)--tokens- Display lexer tokens--ast- Display abstract syntax tree--tac- Display three-address code--map- Display semantic mapping
class Main {
main() : Object {
(new IO).out_string("Hello, World!\n")
};
};class Main {
main() : Int {
42
};
};cool_compiler/
├── src/
│ ├── main.rs - Entry point and CLI handling
│ ├── lexer.rs - Lexical analysis
│ ├── parser.rs - Syntax analysis
│ ├── semantic.rs - Semantic analysis
│ ├── codegen.rs - TAC generation
│ ├── assembler.rs - Assembly code generation
│ ├── print_ast.rs - AST printer
│ ├── print_tac.rs - TAC printer
│ └── display.rs - Display utilities
├── lib/
│ └── prelude/
│ ├── Object.cl - Built-in class definitions
│ └── runtime.asm - Runtime library implementation
└── test/
├── hello.cl - Hello world example
└── minimal.cl - Minimal example
The compiler provides several built-in classes:
abort() : Object- Terminate program with errortype_name() : String- Get type name of objectcopy() : SELF_TYPE- Create shallow copy
out_string(x : String) : SELF_TYPE- Output stringout_int(x : Int) : SELF_TYPE- Output integerin_string() : String- Read string from stdinin_int() : Int- Read integer from stdin
length() : Int- Get string lengthconcat(s : String) : String- Concatenate stringssubstr(i : Int, l : Int) : String- Extract substring
- Lexical Analysis - Tokenize source code
- Parsing - Build abstract syntax tree (AST)
- Semantic Analysis - Type checking and inheritance resolution
- Code Generation - Convert to three-address code (TAC)
- Assembly Generation - Generate FASM-compatible x64 assembly
- Assembly - FASM assembles to native Windows PE64 executable
The runtime library (lib/prelude/runtime.asm) provides:
- Object memory management with heap allocation
- Windows API integration (kernel32.dll)
- Built-in method implementations
- Object layout and dispatch table management
Offset | Field
--------|----------------
0 | Type tag (class index)
8 | Object size (in qwords)
16 | Dispatch table pointer
24+ | Attributes
- The linker error "ld: cannot find .o" is expected and harmless - FASM creates the .exe directly
- All executables are generated as Windows PE64 console applications
- The compiler uses Windows API calls (ExitProcess, GetStdHandle, WriteFile, ReadFile)
This project is provided as-is for educational purposes.