A little language named Egg implemented above javascript.
It's simple. But support function closures, high-order functions and so on. And it could include nearly any logic you could imagine.
Just for fun, origin from my favorite Eloquent Javascript。
All just for fun.
All is expressions. Valid expression are string/number/word/application.
do(define (total, 0), # Hey Egg!
define (count, 1),
while(<(count, 11),
do(define(total, +(total, count)),
define(count, +(count, 1)))),
print(total))
#
used for comment
+(1, 2) # plus 1 and 2
Run multiple expressions one by one. Return the last expression.
do(expr1[, expr2[, expr3[, ...]]])
Valid values include string, number, true and false.
1
"hack"
true
false
A name represent variables like values, function, nearly everything.
a
hehe
define
do
print
Return different value depending on conditions;
if(condition_expression, true_expression, false_expression)
Just like while
in other languages. Return false.
while(condition_expression, expression)
Define a variable in current scope. Return expression's returned value
define(word, expression)
Set variables out of current scope, if there isn't exist, throw ReferenceError. Return the newly set value.
set(word, expression)
Just as you see in other languages. These calculations are supported.
+(1, 2)
>(4, -(2, 1))
define a function. the last arguments will be the function body, all other arguments will be seen as arguments for this function.
do(define(add, fun(a, b, +(a, b))),
print(add(1, 2)))
A comment is started with #
in a line.
print(1) # a comment
Run egg.js
,just input your code line by line. Ctrl-D or Ctrl-Cto exit:
┌─[reverland@reverland-R478-R429] - [~/tmp/eloquentjs] - [2015-11-12 03:16:54]
└─[0] <git:(master 976b113✱✈) > node egg.js
> do(define (total, 0),
> ..define (count, 1),
> ..while(<(count, 11),
> ....do(define(total, +(total, count)),
> ......define(count, +(count, 1)))),
> ..print(total))
55
- more tests...
- repl
- compiler
- better parser. learn how Lex and Yacc works.
- evaluate when parse(JIT? 😂)
- reread [Eloquent Javascript] and have fun with it 😊