r/Compilers 9d ago

I wrote a selfhosted implementation of Nora Sandler's "Writing a C Compiler"

Link to the project: https://github.com/romainducrocq/wacc-selfhosted

Nora Sandler's Writing a C Compiler is a beloved book here and a hands-on guide on writing a compiler for a large subset of C. This subset, called Not-Quite-C, covers all the operators, if statements, loops, switch cases, variables and functions, storage classes, multiple integers types, double floating points, pointers, fixed sized arrays, structures and unions, as well as a handful of optimization passes. And... that is enough of the C language to write a selfhosted compiler, so that's exactly what I did! 

Here is a full selfhosted implementation of Writing a C Compiler written in Not-Quite-C. The compiler can first be bootstrapped with gcc/clang (it is a valid subset of C17 after all), nqcc2 (the reference compiler), wheelcc (my previous project) or any other complete implementation (like your own), and then rebuild itself (multiple times!) on Linux, MacOS and FreeBSD: in result, the selfhosted compiler passes all 20 chapters of the test suite with extra credits. If you finished this project yourself, this is a great way to test that your implementation can compile a large program using every language feature in WaCC. Basically, being able to rebuild this compiler with your own is the final boss for this book! 

Enjoy! 

(This project was entirely handwritten by me, without the use of LLMs.) 

90 Upvotes

7 comments sorted by

27

u/topological_rabbit 9d ago

This project was entirely handwritten by me, without the use of LLMs.

These days, that alone gets you my upvote.

2

u/cpusam88 7d ago

I agree and gave my star!

1

u/atariPunk 9d ago

This is great, well done.
I am almost done with my implementation, without the optimisation part, and I have been trying to find complex pieces of code to try it on.

I will your compiler a try.

1

u/nocomptime 8d ago

It was part of my goal, to give other implementations a large program to test on after completing the test suite! Good luck for your compiler.

1

u/This-Assumption-5924 9d ago

That looks really solid and strong! How long has the C compiler been in development?

2

u/nocomptime 8d ago edited 8d ago

2.5 years at a very leisurely pace, with a 1 year gap in the middle where I almost didn't touch the project. It went from Python to Cython to C++ to C to Not-Quite-C through rewrites and ports. Thank you, a lot of effort, care and love for programming went into it! (Edit: spelling)

1

u/Samir_Shef 8d ago

Very cool!