Skip to content

An extended version of the Egg programming language from the "Eloquent Javascript" book.

License

Notifications You must be signed in to change notification settings

davafons/eggtended-js

Repository files navigation

Eggtended Logo

An extended version of the Egg programming language from the "Eloquent Javascript" book.

Build Version Coverage Quality Documentation License
Build Status npm version Coverage Status Codacy Badge Document Badge License: GPL v3

REPL Loop

https://i.imgur.com/Yo8XjPr.png

Help

$ 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

Code examples

If

def(x, 5),
if(<(x, 10),
  print(x, "lower than 10"),
  print(x, "bigger or equal to 10")
)

For loops

for(define(x, 0), <(x, 5), ++(x),
  print(x)
) # Prints "1 2 3 4 5"

Arrays and Foreach loops

def(x, arr(1, 2, 3)),
foreach(x, x, print(x)) # Prints "1 2 3"

Maps

def(y, map(a: 1, b: 2, c: 3)),
foreach(key, y.keys(), print(key.toUpperCase())), # Prints "A B C"

Everything is an object

print(4.toFixed(2)),  # Prints "4.00"

OOP

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-Catch blocks

try(
  do {  # Try
    throw(42)
  },
  do {  # Catch
    print(+("Caught error! ", __error__))
  }
),

Regex

:=(d, r/
       (?<year>  \d{4} ) -?  # year
       (?<month> \d{2} ) -?  # month
       (?<day>   \d{2} )     # day
      /x),
print(d.test("1987-07-14")),  # true

Module import/export

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"
}

AST Generation

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.

Author

David Afonso Dorta - University of La Laguna. Computer Engineering, 3rd year

About

An extended version of the Egg programming language from the "Eloquent Javascript" book.

Resources

License

Stars

Watchers

Forks

Packages

No packages published