An extended version of the Egg programming language from the "Eloquent Javascript" book.
Build | Version | Coverage | Quality | Documentation | License |
---|---|---|---|---|---|
$ egg --help
Usage: egg [options]
Options:
-V, --version output the version number
-r --run <fileName.egg> compiles the input egg program and runs it using the egg interpreter
-c --compile <fileName.egg> compile the input egg program to produce a JSON containing the resulting egg AST
-t --tokenize <fileName.egg> tokenize the input egg program and print the array of tokens produced
-o --optimize optimize the resulting AST, generating a smaller one
-i --interpret <fileName> interprets the input egg AST
-h, --help output usage information
def(x, 5),
if(<(x, 10),
print(x, "lower than 10"),
print(x, "bigger or equal to 10")
)
for(define(x, 0), <(x, 5), ++(x),
print(x)
) # Prints "1 2 3 4 5"
def(x, arr(1, 2, 3)),
foreach(x, x, print(x)) # Prints "1 2 3"
def(y, map(a: 1, b: 2, c: 3)),
foreach(key, y.keys(), print(key.toUpperCase())), # Prints "A B C"
print(4.toFixed(2)), # Prints "4.00"
def(x, object (
"c", 0,
"gc", ->{this.c},
"sc", ->{value, =(this, "c", value)},
"inc", ->{=(this, "c", +(this.c, 1))}
)),
/* print(x), */
print(x.gc), # 0
x.sc(4),
print(x.gc), # 4
x.inc,
print(x.gc), # 5
try(
do { # Try
throw(42)
},
do { # Catch
print(+("Caught error! ", __error__))
}
),
:=(d, r/
(?<year> \d{4} ) -? # year
(?<month> \d{2} ) -? # month
(?<day> \d{2} ) # day
/x),
print(d.test("1987-07-14")), # true
do {
set(module, "exports", object(
"three", 3,
)),
set(module, "exports", "consolelog", fun(x, print(x)))
}
do {
:=(mod, require("examples/module.egg")),
mod.consolelog(5), # Prints "5"
mod.consolelog(mod.three) # Prints "3"
}
From this program:
do(
print(true.toString())
)
The following AST is generated:
{
"type": "apply",
"operator": {
"type": "word",
"name": "do"
},
"args": [
{
"type": "apply",
"operator": {
"type": "word",
"name": "print"
},
"args": [
{
"type": "apply",
"operator": {
"type": "word",
"name": "true"
},
"args": [
{
"type": "value",
"value": "toString"
}
]
}
]
}
]
}
This AST is the interpreter's input for executing a program.
David Afonso Dorta - University of La Laguna. Computer Engineering, 3rd year