Skip to content

aidan729/Cool-R

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

COOL Compiler

A complete compiler for the COOL (Classroom Object-Oriented Language) programming language, written in Rust with Windows x64 assembly code generation.

Features

  • 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

Prerequisites

  • Rust (latest stable version)
  • FASM (Flat Assembler) for Windows x64

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd cool_compiler
  2. Build the compiler:

    cargo build --release
  3. Ensure FASM is in your PATH:

    # PowerShell (temporary)
    $env:Path += ";C:\path\to\fasm"
    
    # Or install via Chocolatey
    choco install fasm

Usage

Compile a COOL program:

cargo run --release -- -i <input.cl> -o <output>

This will generate <output>.exe which can be run directly on Windows.

Example

# Compile hello world
cargo run --release -- -i test/hello.cl -o hello

# Run the program
.\hello.exe

Command Line Options

  • -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

Example Programs

Hello World (test/hello.cl)

class Main {
    main() : Object {
        (new IO).out_string("Hello, World!\n")
    };
};

Minimal Program (test/minimal.cl)

class Main {
    main() : Int {
        42
    };
};

Project Structure

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

Built-in Classes

The compiler provides several built-in classes:

Object

  • abort() : Object - Terminate program with error
  • type_name() : String - Get type name of object
  • copy() : SELF_TYPE - Create shallow copy

IO

  • out_string(x : String) : SELF_TYPE - Output string
  • out_int(x : Int) : SELF_TYPE - Output integer
  • in_string() : String - Read string from stdin
  • in_int() : Int - Read integer from stdin

String

  • length() : Int - Get string length
  • concat(s : String) : String - Concatenate strings
  • substr(i : Int, l : Int) : String - Extract substring

Technical Details

Compilation Pipeline

  1. Lexical Analysis - Tokenize source code
  2. Parsing - Build abstract syntax tree (AST)
  3. Semantic Analysis - Type checking and inheritance resolution
  4. Code Generation - Convert to three-address code (TAC)
  5. Assembly Generation - Generate FASM-compatible x64 assembly
  6. Assembly - FASM assembles to native Windows PE64 executable

Runtime Library

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

Object Memory Layout

Offset  | Field
--------|----------------
0       | Type tag (class index)
8       | Object size (in qwords)
16      | Dispatch table pointer
24+     | Attributes

Notes

  • 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)

License

This project is provided as-is for educational purposes.

About

COOL Compiler written in Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors