Live data from Hacker News

How I wrote a self-hosting C compiler in 40 days

sigbus.info

11–20 of 128 posts

Re: How I wrote a self-hosting C compiler in 40 days

#11
post #8
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

Same here. I have been trying the same project as you... and get stuck on the grammar, every.single.time. Are you done with that part yet?

What do you mean "stuck on the grammar"? Can't figure out how to write a grammar, can't get all the fiddly implications correct so OOO fails, can't figure out how to write the parser, can't muster up enough enthusiasm to finish the work...?

One option is to punt for a bit and use what I sometimes call the "Lisp non-grammar", even if you don't intend to be writing a Lisp; use parentheses to directly encode a parse tree, and whatever other hacky symbols you need for now to set up atom types or whatever. You might still be able to explore enough what you're interested in to figure out what you want in the grammar. Whatever the core idea of your toy language is, you should try to get to that as quickly as possible, punting on everything else you possibly can, so if the point of your language isn't to have an innovative new grammar, you might want to try to defer that work.

Re: How I wrote a self-hosting C compiler in 40 days

#12
post #8
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

Same here. I have been trying the same project as you... and get stuck on the grammar, every.single.time. Are you done with that part yet?

On which parts exactly are you stuck? I've rolled with Ragel as lexer, and Lemon as parser, and it was relatively simple.

[1] https://github.com/castel/libcastel/blob/master/parse/source...

[2] https://github.com/castel/libcastel/blob/master/parse/source...

Re: How I wrote a self-hosting C compiler in 40 days

#13
> I suspect that he [Dennis Ritchie] invented a syntax, wrote code for it, which turned out to be more complicated than he had expected. And that was eventually standardized by the ANSI committee. It's hard to implement a standardized language because you have to get everything right. It's rather easy to write your own toy language.

Love those lines

Re: How I wrote a self-hosting C compiler in 40 days

#14
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

I can't recommend the Coursera compilers course highly enough. It was enough to get me from zero to a fully working compiler. Albeit for a simplified language, and a really bad compiler :-)

If you did it in one of the languages for which scaffolding already exists, it'll be even easier (I did it in a different language, so had to build everything from scratch).

Re: How I wrote a self-hosting C compiler in 40 days

#16
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

I think the best inspiration to take here is that the best way to write a compiler in 40 days is to first write one in 400 days. From the first entry: > Implementing these features is easy because this is the second time for me.

And thus, the 10X programmer.

Re: How I wrote a self-hosting C compiler in 40 days

#17
post #8
post #3

Thought this was going to be an inspiration to me to continue with my pet project of writing my own little programming language. But it starts off on day 8 with him already having written a basic compiler, with no explanation of how he did any of the basics. Still interesting, just not what I thought it was.

Same here. I have been trying the same project as you... and get stuck on the grammar, every.single.time. Are you done with that part yet?

It's fine. "Compilers" topic is too wide. There are lot of sub-topics. I personally find lexing/parsing rather boring. It's after I have AST is when the fun begins for me (and right before actual/real code gen, where you have to know intricacies of particular CPU) :)

Re: How I wrote a self-hosting C compiler in 40 days

#18
post #15

What does it mean to be 'self-hosting' here? Does it just mean that it's a compiler that can compile itself?

I believe so. FTA:

"Looking at my code, even though I wrote it, it feels magical to me that it can handle itself as an input and translate it to assembly."

Post reply on HN