开发者

How can CoffeeScript be written in CoffeeScript?

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 开发者_Python百科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?


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.

  • Bootstrapping a language
  • Writing a compiler in its own language
  • How do you write a compiler for a language in that language?
  • Bootstrapping a compiler: why?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜