Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 829
Hortinstein Avatar asked Jun 02 '11 03:06

Hortinstein


People also ask

How do you write a CoffeeScript?

In general, we use parenthesis while declaring the function, calling it, and also to separate the code blocks to avoid ambiguity. In CoffeeScript, there is no need to use parentheses, and while creating functions, we use an arrow mark (->) instead of parentheses as shown below.

What is CoffeeScript written in?

CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart.

Is CoffeeScript typed?

But it is a strictly typed programming language. CoffeeScript has high object-oriented capabilities. But it is a dynamic type of programming language.

How do I use CoffeeScript in HTML?

If you are looking to implement coffee script in html, take a look at this. You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file.


2 Answers

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.

like image 177
Matt Ball Avatar answered Sep 20 '22 02:09

Matt Ball


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.

like image 32
Trevor Burnham Avatar answered Sep 20 '22 02:09

Trevor Burnham