Crystal: How is the compiler for Crystal built in Crystal?

Created on 3 Apr 2015  Â·  12Comments  Â·  Source: crystal-lang/crystal

This might be (is) a dumb question, but how is the compiler built in Crystal? I just can't wrap my mind around it... :confused: Doesn't the Crystal code of the compiler need to be compiled? How can the Crystal code compile if it's being compiled by _other Crystal code_?

question

All 12 comments

The compiler is compiled with a compiler that is already compiled. If that makes sense :laughing:

Let me try too:

Crystal code is compiled into a binary. Running the binary does not require any Crystal code. The compiler is a program like everything else. Like GCC is a program written in C, that compiles C code into a binary. Just like that, the compiler is a program written in Crystal, that compiles Crystal code into a binary.

It may also help to mention that the initial Crystal compiler was written in Ruby, until it was able to compile a compiler written in Crystal.

Well, maybe the missing piece in the puzzle is a bit of history. Initial crystal compiler was written in ruby. When that evolved enough to compile almost the same ruby code reading it as crystal source the story began. :-). Now each release use the previous. 

Read a bit more in http://crystal-lang.org/2013/11/14/good-bye-ruby-thursday.html

—
Sent from Mailbox

On Thu, Apr 2, 2015 at 9:38 PM, Jonne Haß [email protected]
wrote:

Let me try too:

Crystal code is compiled into a binary. Running the binary does not require any Crystal code. The compiler is a program like everything else. Like GCC is a program written in C that compiles C code into a binary. Just like that Crystal is a program written in Crystal that compiles Crystal code into a binary.

Reply to this email directly or view it on GitHub:
https://github.com/manastech/crystal/issues/509#issuecomment-89094045

On a related (or completely opposite?) note ... when is someone going to write a Ruby runtime in Crystal? :-)

So is the compiler still compiled in Ruby-compiled Crystal compiler?

Not anymore. It's all Crystal now.

But how does the old Crystal compiler compile the new Crystal compiler if it has new features?

The old compiler compiles your new feature, the product of that compilation is a new compiler with the new feature.

A tangentially related, classic paper: http://www.ece.cmu.edu/~ganger/712.fall02/papers/p761-thompson.pdf

I _guess_ that the code that implements a new feature is valid code for the compiler that will compile the new compiler.

In other words you only get into troubles if the new feature requires the new feature to be implemented, which is extremely unlikely.

Ok, that makes sense. Once you compile the new feature into the compiler, you can then use that feature in the next version of the compiler. :smile: Thanks for the clarification!

@phase As others said, when you introduce a new feature into the compiler you don't use that feature in that code (you can't, it's not present). However, in this newly created compiler you can start using that feature in the compiler's code.

One interesting anecdote is that in one point we didn't support the \e escape sequence. So we introduced it and assigned it's value to 92 (92.chr). Once we had this feature we could replace that 92.chr with '\e', and it's like that right now.

All of this make it look like the compiler uses its own features to built itself, kind of like the predestination paradoxes of some movies. If programmers like recursion, I think a bootstrapped compiler is one of the maximum expressions of that :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oprypin picture oprypin  Â·  3Comments

asterite picture asterite  Â·  3Comments

nabeelomer picture nabeelomer  Â·  3Comments

Papierkorb picture Papierkorb  Â·  3Comments

oprypin picture oprypin  Â·  3Comments