Live data from Hacker News

Let's Build a Compiler

generalproblem.net

41–48 of 48 posts

Re: Let's Build a Compiler

#42
post #27

I have attempted following Ghuloum's paper, too. One difference is I wanted to make it as self-contained as possible and didn't want to depend on a C compiler or binutils. So I wrote a simple assembler. Here it is, all in Clojure: https://github.com/nathell/lithium It's dormant – I was stuck on implementing environments around step 7 of 24 – but someday I will return to it and make progress.

When I went over the tiger book back at the university, our teacher had a cool approach to overcome that.

Generate bytecode, but in a form that could be easily mapped to macros on a Macro Assembler, thus we only needed to write such macros for each target platform.

From performance point of view it was quite bad, but we got complete AOT static binaries out of it anyway.

Re: Let's Build a Compiler

#43

Earlier quoted context omitted.

I did the Nand2Tetris course which includes building a basic compiler. It just helps fully understand how you go from words in a file to actually doing computations and how purely abstract ideas like a 'class' are implemented. To be fair, I studied Physics and not CS so I didn't have the opportunity to study Compilers at University.

> so I didn't have the opportunity to study Compilers at University. Lots of CS people haven't either. My university moved compiler theory to the Masters program.

In some countries that is kind of irrelevant because most end up doing masters anyway.

Re: Let's Build a Compiler

#44

Earlier quoted context omitted.

Yes they are. Embedded applications outnumber top end computers by an order of magnitude.

Perhaps he means that they are not generally self-hosted, in that we generally tend to cross-compile for the embedded hardware, rather than compiling on the embedded hardware.

That's precisely what I meant, and I don't understand how it could be misunderstood if you know what "self-hosting" means in this context.

Re: Let's Build a Compiler

#46
post #30

it puzzles me sometimes why we programmers are so fascinated by compilers, interpreters, VMs, runtimes, etc. many of these will never make it to the level of, say, a production c++ compiler or a Java VM. and yet we keep building small compilers.

Relevant Steve Yegge blog post: https://steve-yegge.blogspot.com/2007/06/rich-programmer-foo...

Re: Let's Build a Compiler

#47
post #30

it puzzles me sometimes why we programmers are so fascinated by compilers, interpreters, VMs, runtimes, etc. many of these will never make it to the level of, say, a production c++ compiler or a Java VM. and yet we keep building small compilers.

Is it not a rite of passage to design & implement your own language, distilling your two years of knowledge and arrogance into one pathetic failure of a design? And from then on appreciating some sense of the difficulty of constructing and maintaining such things?

Perhaps not always languages, but such experiences are vital to our industry!

Re: Let's Build a Compiler

#48

This looks like something similar to Crenshaw's excellent tutorial of the same name: https://compilers.iecc.com/crenshaw/ and its x86 port: https://github.com/lotabout/Let-s-build-a-compiler As interesting as Lisp-family languages are, I still think it's better to use something with more traditional syntax and start with parsing, because that both reaches a much wider audience and gives a very early introduction to t…

I think I partially agree, but that it mainly depends on the audience. I've found that parsing and recursion appear in more areas than just compilers, to the point where it'd be relatively difficult to avoid them. Machine code generation and the underlying implementation of high-level primitives (closures, tail-call recursion, and the like) hasn't, at least in my experience, been "naturally-occurring" to the same ext…

I've worked my way through quite a few tutorials on implementing Scheme I think you're absolutely correct that detailed material on implementing high level features, especially in machine code, is lacking. I'm very grateful for this tutorial. Can't wait for the next part!
Post reply on HN