39

So as a new web programmer (background is mostely in C,C++, and Python) with no javascript experience (or desire to experience it, based on what I have seen) I have been doing some precursory research on CoffeeScript and am really liking what I see. One cool little nuance I noticed was that CoffeeScript was written in CoffeeScript, which is cool... but I don't really understand how that is possible. Unfortunately I opted to take Network Security instead of Compilers during my last senior level Computer Science courses.

I have heard of people writing languages in target language to interpret using existing compilers for that language, but I can't dig up any info on how this might work since this is the first implementation.

My guess is an embedded shell script that might take care of laying down some of the initial framework for building a self-referential (does this term properly describe this behavior?) language?

Samuel Katz
  • 24,066
  • 8
  • 71
  • 57
Hortinstein
  • 2,667
  • 1
  • 22
  • 22
  • 4
    http://en.wikipedia.org/wiki/Bootstrapping_(compilers) – Mauricio Scheffer Jun 02 '11 at 03:43
  • 3
    be warned that you *need* to know Javascript well to code in CoffeeScript, since all runtime errors will point to the compiled source. – Ricardo Tomasi Jun 05 '11 at 04:59
  • Re terminology: You could say that the CoffeeScript compiler was **bootstrapped** from its original Ruby implementation. You could also say that the CoffeeScript language is **self-hosting**. – Quuxplusone Oct 04 '12 at 21:55
  • possible duplicate of [Bootstrapping a language](http://stackoverflow.com/questions/13537/bootstrapping-a-language) – nawfal Jul 21 '14 at 11:09

2 Answers2

46

This is nothing new. C compilers have been written in C. Python has been written in Python.

It's possible to use a compiler for Language X to compile a newer version of itself, with more features. It's called bootstrapping.

BTW, if you want to learn more about compilers, despite having missed out at uni, have a look at Learning to write a compiler — specifically, the Dragon Book.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • I have heard the term Bootstrapped before, but had no real clear understandign of it. This is really interesting, thanks for the great links, I will do some reading up on bootstrapping. I have heard of the dragon book as well reading "Must read programming books" threas on SO, I will have to check it out – Hortinstein Jun 02 '11 at 04:08
  • 1
    I agree with Matt. But, if you want to learn Coffeescript, I highly suggest learning Javascript first. It may look a bit alien-like to people from different language-backgrounds, but its concepts (like function and prototype base) are useful for a variety of software/application solutions and frameworks. Again, Coffeescript compiles to Javascript. So, obviously, hidden underneath the classes and different-looking concepts in CS, Javascript's concepts play a great role. And do read The Dragon Book to understand how compilers work. – Nir Lanka Jul 20 '12 at 14:51
43

Matt's answer is excellent. Let me just elaborate with some CoffeeScript-specific info:

The original version of the CoffeeScript compiler was written in Ruby, but it was transitioned to CoffeeScript for v0.5.0, on Feb 21, 2010.

As a practical matter, the bootstrapped compiler can make things difficult with an ever-changing language, as the compiler has to be rewritten to accomodate those changes. This paragraph from the official docs gives you some idea of the challenges involved:

git checkout lib && bin/cake build:full is a good command to run when you're working on the core language. It'll refresh the lib directory (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there's a good chance you've made a successful change.

The lib directory contains the compiled JavaScript of the CoffeeScript compiler (got that?), providing a helpful intermediary in the bootstrapping process. Since the CoffeeScript code of the compiler never runs directly on itself, that makes it easier to make breaking changes to the language.

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196