Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

21–30 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#21
post #15

It is interesting to think that 500 lines of code is something one can write in one or two days. But, writing a C compiler in 500 of comprehensible code (even in python) is challenge in itself that may take months after a few years of solid learning. I wonder if is this a good path to becoming an extremely productive developer. If some one spends time developing projects like this, but for different areas... A kernel…

> But, writing a C compiler in 500 of comprehensible code (even in python) is challenge in itself that may take months after a few years of solid learning. The people behind this project avoided that caveat by simply not implementing C. Apparently they kept a bit of the syntax but then proceeded to cherry-pick features that suited them and not make.an effort to even try to comply with any version of the standard.

It’s a little bit bigger than Small C https://en.m.wikipedia.org/wiki/Small-C

Re: Writing a C compiler in 500 lines of Python

#22

It is interesting to think that 500 lines of code is something one can write in one or two days. But, writing a C compiler in 500 of comprehensible code (even in python) is challenge in itself that may take months after a few years of solid learning. I wonder if is this a good path to becoming an extremely productive developer. If some one spends time developing projects like this, but for different areas... A kernel…

It does remind me of a project [1] Andrej Karpathy did, writing a neural network and training code in ~600 lines (although networks have easier logic to code than a compiler).

[1] https://github.com/karpathy/nanoGPT

Re: Writing a C compiler in 500 lines of Python

#23

It is interesting to think that 500 lines of code is something one can write in one or two days. But, writing a C compiler in 500 of comprehensible code (even in python) is challenge in itself that may take months after a few years of solid learning. I wonder if is this a good path to becoming an extremely productive developer. If some one spends time developing projects like this, but for different areas... A kernel…

> 0.1 Bellard Off topic, but a log scale might be useful: 0.1 Bellard --> -10 deciBellards. That allows for: 0.001 Bellard --> -30 deciBellards. Problem: Programmers with negative productivity cannot be represented on the same log scale.

Sure they can. Abs(). Or if you prefer to not have quite so much micro-nano, you could also use Cumulative Distribution Functions [1] which are basically just sums of Probability Density [2].

Are they a 4σ programmer, 1σ programmer, -0.5σ programmer, -2σ programmer?

Plus, most people are "average" not negative productivity, and CDFs let you use really fun stuff like Beta Distributions (variable, shapable distributions) and Gamma Distributions (exponential distributions). They're super sweet as far as probability statistics.

[1] https://en.wikipedia.org/wiki/Cumulative_distribution_functi...

[2] https://en.wikipedia.org/wiki/Probability_density_function

[3] https://en.wikipedia.org/wiki/Beta_distribution

[4] https://en.wikipedia.org/wiki/Gamma_distribution

Re: Writing a C compiler in 500 lines of Python

#24
Just for comparison the LOCs for some other small C or C like compilers. It's not that far away from Ritchie's

C4x86 | 0.6K (very close)

small C (x86) | 3.1K

Ritchie's earliest struct compiler | 2.3K

v7 Unix C compiler | 10.2K

chibicc | 8.4K

Biederman's romcc | 25.0K

Re: Writing a C compiler in 500 lines of Python

#26
post #12

For some value of “C”: > Notably, it doesn't support: > structs :-( would be possible with more code, the fundamentals were there, I just couldn't squeeze it in > enums / unions > preprocessor directives (this would probably be 500 lines by itself...) > floating point. would also be possible, the wasm_type stuff is in, again just couldn't squeeze it in > 8 byte types (long/long long or double) > some other small thin…

C--23

(Respect to the author for doing this, I just couldn’t resist the obvious joke)

Re: Writing a C compiler in 500 lines of Python

#27
post #24

Just for comparison the LOCs for some other small C or C like compilers. It's not that far away from Ritchie's C4x86 | 0.6K (very close) small C (x86) | 3.1K Ritchie's earliest struct compiler | 2.3K v7 Unix C compiler | 10.2K chibicc | 8.4K Biederman's romcc | 25.0K

Oh, C4 is neat—technically it has me beat since it also implements the VM to run the code—though their formatting definitely takes advantage of long lines :-)

Re: Writing a C compiler in 500 lines of Python

#28
post #6

> Instead, we'll be single-pass: code generation happens during parsing IIRC, C was specifically designed to allow single-pass compilation, right? I.e. in many languages you don't know what needs to be output without parsing the full AST, but in C, syntax directly implies semantics. I think I remember hearing this was because early computers couldn't necessarily fit the AST for an entire code file in memory at once

I'm not sure, haven't looked at the codebases of old compilers in a long time. Definitely a lot of the language is pretty amenable to it, especially if you have unstructured jumps for e.g. the for advancement statement. I had a distinct feeling while writing the compiler every time I added a new feature that "wow, the semantics work exactly how I'd like them to for ease of implementation."

Compare that to, say, Rust, which would be pretty painful to single-pass compile with all the non-local behavior around traits.

Re: Writing a C compiler in 500 lines of Python

#29
post #24

Just for comparison the LOCs for some other small C or C like compilers. It's not that far away from Ritchie's C4x86 | 0.6K (very close) small C (x86) | 3.1K Ritchie's earliest struct compiler | 2.3K v7 Unix C compiler | 10.2K chibicc | 8.4K Biederman's romcc | 25.0K

[deleted]

Re: Writing a C compiler in 500 lines of Python

#30
post #8

I made similar project in TypeScript[1]. Basically multipass compiler that generates x86 assembly, compiles it to binary and runs it. The worst thing were register allocator, designing IR code and assembler. [1] https://github.com/Mati365/ts-c-compiler

Ooh, this is cool! Using WASM let me avoid writing a register allocator (though I probably would have just used the stack if I had targeted x86/ARM since I wasn't going for speed).
Post reply on HN