Skip to content

nlsandler/c-compiler-implementations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 

Repository files navigation

c-compiler-implementations

This is a list of reader implementations of the project from Writing a C Compiler, shamelessly ripped off from lovingly inspired by the list of implementations of Lox from Crafting Interpreters. I hope this will be a useful resource for readers planning to complete the project in a lots of different languages.

See the contributing section for details on how to add your implementation to the list. Incomplete, unpolished, and hacky implementations are welcome!

Disclaimer: I haven't code reviewed these implementations. As with any untrusted code, please be cautious about running these on your own computer.

Contents

Contributing

Please add your C compiler to this page if:

  1. It's basically following the design in Writing a C Compiler (the book, not the blog posts, which are quite different).
  2. You've finished at least chapter 1.

Your compiler does not need to be complete, polished, or production-ready, and it does not need to follow the book exactly. Compilers that implement extra language features, target different processors or are organized into different passes are totally welcome.

Open a pull request to add your compiler to the list for the appropriate implementation language, in alphabetical order. If there isn't already a subheading for your implementation language, please go ahead and add one (make sure to add it to the the table of contents too!) If you don't have a Github account, you can email me and ask me to add it for you.

For projects hosted on Github, please use the following format:

  • USERNAME/REPO - A few optional sentences about your project. E.g. if you've made different design decisions than the book suggests or implemented extra features, you can mention it here.

For projects hosted elsewhere, use this format:

  • name of project - A few optional sentences about your project. E.g. if you've made different design decisions than the book suggests or implemented extra features, you can mention it here.

Implementations

Ada

  • JeremyGrosser/wacc - Handwritten lexer, using record types with discriminants to model ASDL structures.

C

  • LesleyLai/mcc - A C23 implementation. While loosely following the book, I also consulted other resources, and this implementation has notable deviations, including a handwritten lexer, name resolution within the parser, and a slightly different IR design.

C++

  • Gnomeball/C-Compiler - Implemented on Linux; only notable change from the intended design is the use of a byte code representation instead of an AST while parsing; so far only chapter one is 'complete', though it's extremely rough.

  • romainducrocq/wheelcc - C++ (>=17) implementation for Linux: with some builtin preprocessing, comprehensive error messages with source locations and more features to come! (Part 1, 2 and extra-credits done. Part 3 in progress.)

Haskell

  • 0xpantera/halcyon - Uses parser combinators for lexer and parser instead of regular expressions. Work in progress. Check out the project README for details on current functionality and progress.

Java

  • dplassgit/adventofcompilers - Java 21 implementation. Hand-rolled lexer (no regexp). Pretty faithful to the book, except with more meaningful names to disambiguate AST nodes from Tacky nodes from Assembly nodes. Complete through Chapter 11 (not including extra credits.)

OCaml

  • nlsandler/nqcc2 - This is the official reference implementation for the book!

  • lc - Work in progress, Part I completed, implements extra credit features (Duff's device!). Follows the book fairly closely. Has no dependencies beyond the OCaml stdlib.

Perl

  • sir-galahad/adcc - C compiler in Perl, for Linux seemed like a good idea, Perl hashes make it quick to build complex data structures.

Python

  • gh-nate/wacc - Part I completed without extra credits. Verification relies on test suite mentioned in the book.

Rust

  • ceronman/writing-a-c-compiler - Work in progress, I have implemented up to chapter 12 including all extra-credit. Only macOS supported for now. I push updates almost every day.

  • ianyourgod/batpu-c-compiler - Rust implementation for the BATPU2, a CPU made for/in minecraft: uses the GCC preprocessor, and has absolutely terrible error messages. All parts finished. A few part 2 features are missing (floats, longs, unsigned), as the CPU does not support them.

  • neildanson/c_compiler - Rust implementation based entirely on the book, following x86-64. Verified working on WSL2 on Windows and MacOS. Work In progress.

  • ysono/compiler-c-sandler - Implementation details diverge from the book's instructions in various ways. The behavior is consistent with the way the book clamps down on undefined behaviors. Tested on Linux and Intel Mac. Work in progress.

About

Reader Implementations of "Writing a C Compiler"

Topics

Resources

Stars

Watchers

Forks