編譯成JavaScript的新語言: Earl Grey

jopen 9年前發布 | 12K 次閱讀 Earl Grey JavaScript開發

Earl Grey (website)是一種新的語言能夠編譯成JavaScript (ES6)。以下是它的一些驚人的特性:

  • 類似Python的語法
  • 完全兼容node.js生態系統
  • 生成器和async/await (no callback hell!)
  • 功能強大,深度集成模式匹配
    • 用于分配,函數聲明,循環,異常...
  • A DOM-building DSL with customizable behavior
  • A very powerful hygienic macro system!
    • Define your own control structures or DSLs
    • Macros integrate seamlessly with the language
    • Macro libraries! Test with earl-mocha, build with earl-gulp, make dynamic pages with earl-react, etc.
  • And much more!

Examples

Counting all words in a block of test. Note thatcount-wordsis a variable name, not a subtraction (it is equivalent to the namecountWords, if that's the notation you prefer).

count-words(text) =
   counts = new Map()
   words = text.split(R"\W+")
   words each word ->
      current-count = counts.get(word) or 0
      counts.set(word, current-count + 1)
   consume(counts.entries()).sort(compare) where
      compare({w1, c1}, {w2, c2}) = c2 - c1

{x, y, ...}is the notation for arrays in Earl Grey. Objects are denoted{field = value, field2 = value2, ...}

Generators: the following defines a generator for the Fibonacci sequence and then prints all the even Fibonacci numbers less than 100. It shows off a little bit of everything:

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

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

Theeachoperator accepts multiple clauses, which makes it especially easy to work on heterogenous arrays.

項目主頁:http://www.baiduhome.net/lib/view/home/1431415101372

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!