Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

41–50 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#41
post #32

Earlier quoted context omitted.

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.

You don't need a university education to do those things, just some curiosity. 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, perhap…

I'm largely taught outside the academic world. so I sympathize with your position.

however, the engineering culture which took the time to tell me about all these cool things and let me grow into being an expert in them seems to be largely gone.

Re: Writing a C compiler in 500 lines of Python

#42
post #2

Finally, one can have inefficient C.

Why would language choice of compiler make any difference for efficiency of final output?

Maybe not the language choice, but the codegen of this compiler is terrible because of the single-pass shortcuts (for example, it unconditionally loads the result of all assignment operations back to the stack just in case you want to write `a = b = 1`, even though 99% of the time that load is immediately thrown away.)

Re: Writing a C compiler in 500 lines of Python

#44
post #2

Finally, one can have inefficient C.

There's always the CINT interpreter for C and C++. https://root.cern.ch/root/html534/guides/users-guide/CINT.ht...

A PTSD trigger for me. Only half joking. Funny thing is, I never checked out Cling to see if it was at long last the real deal.

Re: Writing a C compiler in 500 lines of Python

#45

This looks a lot like the Tiny Pascal compiler that BYTE published a listing of back in 1978. http://www.trs-80.org/tiny-pascal/ I figured out the basics of how a compiler works by going through it line by line.

Oh, that's neat (funny that they skipped out on similar things to me, like GOTO and structs :-)

I didn't see a link to the source in the article, but this seems to be it: https://sourceforge.net/p/tiny-pascal/code/HEAD/tree/NorthSt...

Re: Writing a C compiler in 500 lines of Python

#46

This looks a lot like the Tiny Pascal compiler that BYTE published a listing of back in 1978. http://www.trs-80.org/tiny-pascal/ I figured out the basics of how a compiler works by going through it line by line.

I think Borland’s Turbo Pascal was also a single pass compiler that emitted machine code as COM files.

Re: Writing a C compiler in 500 lines of Python

#48
post #10
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

Linked from another thread: http://cm.bell-labs.co/who/dmr/chist.html It explains the memory limits and what happened :) > After the TMG version of B was working, Thompson rewrote B in itself (a bootstrapping step). During development, he continually struggled against memory limitations: each language addition inflated the compiler so it could barely fit, but each rewrite taking advantage of the feature reduced its s…

I wonder why =+ is so obviously a mistake. It does look vaguely wrong for some reason, but I’m prejudiced by current languages.

Re: Writing a C compiler in 500 lines of Python

#49
post #10

Earlier quoted context omitted.

Linked from another thread: http://cm.bell-labs.co/who/dmr/chist.html It explains the memory limits and what happened :) > After the TMG version of B was working, Thompson rewrote B in itself (a bootstrapping step). During development, he continually struggled against memory limitations: each language addition inflated the compiler so it could barely fit, but each rewrite taking advantage of the feature reduced its s…

I wonder why =+ is so obviously a mistake. It does look vaguely wrong for some reason, but I’m prejudiced by current languages.

I think because it's ambiguous with unary plus (a = +b), since C isn't supposed to have significant whitespace in most circumstances.
Post reply on HN