Finally, one can have inefficient C.
Writing a C compiler in 500 lines of Python
31–40 of 183 posts
Re: Writing a C compiler in 500 lines of Python
#32It 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.
The function of the university in the near future will probably just be to have like-minded curious people to discuss ideas with, and to get a better grasp of what problems need to be solved (specifically scientific ideas, rather than just applying engineering).
The prestige element (specifically of certain universities over others, perhaps not university over high school) is dwindling, and hopefully will be abolished with this new generation.
Re: Writing a C compiler in 500 lines of Python
#33Finally, one can have inefficient C.
Why would language choice of compiler make any difference for efficiency of final output?
Re: Writing a C compiler in 500 lines of Python
#34Is there a C compiler written in Python that aims for maximum readability rather than trying to get as much done under X lines of code?
IMO, being under X lines of code is part of the readability—10,000 lines of code is hard to approach no matter how readable it otherwise is.
Re: Writing a C compiler in 500 lines of Python
#35It 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.
This is similar to the problem of price-to-earnings ratio. The ratio goes asymptotic as earnings goes through zero. It would be better to quote earnings-to-price ratio. Another screwy reciprocal unit is miles per gallon for cars.
Re: Writing a C compiler in 500 lines of Python
#36> 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,…
Re: Writing a C compiler in 500 lines of Python
#37For 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
#38It 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
#39> 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
Re: Writing a C compiler in 500 lines of Python
#40For 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…