编译成JavaScript的新语言: Earl Grey
jopen
9年前
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.