Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

11–20 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#11

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…

at the very least it'll remove a lot of 'magic' from programming. Today a lot of people seem to be not so fond of university education but I'm personally very glad it made me go through implementing a shell, a compiler, a little toy kernel and so on.

The feeling that you write code somewhere in the skies and have no idea how something works underneath has always really bugged me when I've used something.

Re: Writing a C compiler in 500 lines of Python

#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 things like pre/post cremements, in-place initialization, etc., which just didn't quite fit any sort of standard library or i/o that isn't returning an integer from main()

> casting expressions

Re: Writing a C compiler in 500 lines of Python

#13
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

using recursive descent, you don't need to build an ast

Re: Writing a C compiler in 500 lines of Python

#14
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…

[deleted]

Re: Writing a C compiler in 500 lines of Python

#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.

Re: Writing a C compiler in 500 lines of Python

#16

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.

Re: Writing a C compiler in 500 lines of Python

#18
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 once read that this is why the MSVC compiler didn't support two-pass template instantiation until very recently: the original compiler implemented templates almost like a macro that re-emitted a stream of tokens with the template parameters replaced.

Re: Writing a C compiler in 500 lines of Python

#19
post #13
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

using recursive descent, you don't need to build an ast

the call stack during recursive descent is an ephemeral ast, for the recursive descent parsers I've written.

Re: Writing a C compiler in 500 lines of Python

#20

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…

[deleted]
Post reply on HN