Earl Grey

2015-05-08 17:56:47 +0100 by Alex R. Young

Earl Grey (GitHub: breuleux/earl-grey, License: MIT, npm: earlgrey) by Olivier Breuleux is a language that compiles to JavaScript. It's aware of ES6 features and actually compiles to ES6. It's built using great libraries like babel. Naturally Earl Grey supports sourcemaps.

The syntax is Python inspired, but modern JavaScript shines through. Look at how generators are handled:

gen fib() =
   var {a, b} = {0, 1}
   while true:
      yield a
      {a, b} = {b, a + b}

fib() each
   > 100 ->
   n when n mod 2 == 0 ->
      print n

It also has pattern matching, as in the functional language weak type testing:

match thing:
   0 ->
      print "The thing is zero"
   < 0 ->
      print "The thing is negative"
   R"hello (.*)"? x ->
      ;; note: R"..." is a regular expression
      print 'The thing is saying hello'

One thing that surprised me about Earl Grey is the inclusion of the async and await keywords:

require: request
g = promisify(request.get)

async getXKCD(n = "") =
   response = await g('http://xkcd.com/{n}/info.0.json')

   requests = await all 1..10 each i -> getXKCD(i)
   requests each req -> print req.alt

I saw Earl Grey on Hacker News, and wondered how long Olivier has been working on it. According to GitHub, he started work back in March last year, and older versions were mostly written in Earl Grey with a single JavaScript bootstrap file. Over time more dependencies were added, like opg, and more recently Babel for ES5 compilation.

You can find the lexer, parser, and language implementation in the src folder. It's all written in Earl Grey and demonstrates the language features really well -- take a look at the macro files to see what I mean.

The real question is, do you drink Earl Grey with milk? Some say bergamot doesn't go with milk, others say the tannins in black tea needs milk due to a chemical reaction involving fat. I personally prefer milk in Earl Grey, although I almost always drink coffee black.