Writing a C compiler in 500 lines of Python
Posted by ketralnis@reddit | programming | View on Reddit | 16 comments
Posted by ketralnis@reddit | programming | View on Reddit | 16 comments
BibianaAudris@reddit
Can't help but point at https://bellard.org/otcc/otcc.c
It's shorter, it self-compiles, and it emits machine code instead of WASM. It's a little harder to read though.
vancha113@reddit
Both my eyes and my head hurt now, thanks.
The_Northern_Light@reddit
Just a little though
sachiperez@reddit
500 characters to create a prompt and have AI do it. I hate this new reality…😒
The_Northern_Light@reddit
?
Only person talking about ai here is you.
This is a silly but highly reasoned post about achieving a fairly complex goal under tight constraints… it’s not ML slop. The only time he mentions ML is to say a future post will describe how to create an LLM by hand… which even if you’re not a fan of ML, that isn’t “get an ‘ai’ to do it for me” either.
sachiperez@reddit
!
sachiperez@reddit
you must get upset a lot. You’re pretty funny…
birdbrainswagtrain@reddit
I'm doing this myself, and it really is a blessing and a curse. It's much simpler than most "real" ISAs, not to mention "real" executable formats. But as this post mentions, the real problem is control flow. If you want to properly support goto, or even switch, you're going to eventually need some ridiculous algorithm to restructure it which still falls back to a dispatch loop in the worst case.
I strongly recommend Nora Sandler's Writing a C Compiler if this is something that interests you. It takes an incremental approach (meaning you've got a working compiler in chapter 1) and includes a test suite.
The_Northern_Light@reddit
Thanks for the shares! I really like that pedagogical style for programming especially (get something working ASAP then learn by iterative refinement), so I’ll definitely check that book out
arkie87@reddit
So a compiler that compiles C is used to run Python which can compile C? Straight to jail!
church-rosser@reddit
Toy compiler is toy compiler.
6502zx81@reddit
Yes, I doubt type declarations can be done in 500 lines. I mean array of pointers to functions taking pointers to structs containing ....
BibianaAudris@reddit
In some C70 variants you don't have to care. If you require all struct / union fields to have different names, you don't need the type to compute the offset. When everything uses one register, again you don't need any type to generate code for a function call.
That's why: -
int
and pointer can pass to each other without casting. - You don't have to declareprintf
orexit
to used them in C89. - Every (old) struct / union field in Unix libc has a different name.crocodus@reddit
It’s probably the most useless, most stupid idea I’ve heard. And I absolutely love it.
It sounds incredibly fun. And I think we need more of this.
But if anyone is thinking about doing something like this in production we need to have a serious talk.
MacASM@reddit
pretty interesting
HankOfClanMardukas@reddit
You’re doing it backwards.