Egg! Tutorial

Learn More

Egg is a JavaScript based programming language. Want to write your own implementation? See Eloquent Javascript for instructions and starter code.

Hello World

Example

Egg! Basics

Expressions and Types

Everything in Egg is an expression. An expression can be a number, a string, the name of a binding, or an application. All expressions are one of three types: value, word, or apply.

Value Type: Number

A sequence of digits.

Value Type: String

A string is simply a sequence of characters that are not double quotes, wrapped in double quotes.

Word Type: Binding

Binding names can consist of any character that is not whitespace and that does not have a special meaning in the syntax. Syntax characters include ( ) , "

Apply Type: Application

Applications are written the way they are in JavaScript, by putting parentheses after an expression and having any number of arguments between those parentheses, separated by commas.

Working with Expressions

Example

Example Explained

define is an application with two arguments color and "blue".

color is a name of a binding being defined.

"blue" is a string value assigned to the binding color.

Egg! Print

Print

This implementation of Egg is configured to print to a designated HTML Textarea. Print is an application that accepts one argument, an expression (number, string, binding, or application).

Print a String (Value)

Example

Example Explained

print is an application with one argument, the string "Hello World!".

Print a Number (Value)

Example

Example Explained

print is an application with one argument, the number 10.

Print the result of an Application (Apply)

Example

Example Explained

print is an application with one argument, the application define(color,"green") which evaluates to the value assigned to the binding, "green".

Egg! Define

Define

define() is an application that accepts two arguments, separated by commas and returns the second argument

Arg 1: Binding

The name of the binding, without quotes. Binding names can consist of any character that is not whitespace and that does not have a special meaning in the syntax.

Arg 2: Value

A string is simply a sequence of characters that are not double quotes, wrapped in double quotes.

Returns: Arg 2

Define returns the value that the second argument evaluates to.

Working with Expressions

Example

Example Explained

define is an application with two arguments color and "blue".

color is a name of a binding being defined.

"blue" is a string value assigned to the binding color.

Return value: "blue".

Egg! Math.

Math Operations (Applications)

Operations are of type Apply. Operations such as +, -, *, and / are applications that each accept two arguments, separated by commas. -, *, and / only accept numeric arguments. However, +, like in JavaScript will add numeric values or if given a string as an argument, will concatonate its arguments.

Arg 1: Expression

A numeric expression (or a string with the + operator).

Arg 2: Expression

A numeric expression (or a string with the + operator).

Returns: numeric

Math operators (applications) return a calculated numeric value (or a string with the + operator, when one or more arguments is a string).

What's the Difference

Example -

Example Explained

-(9,4) calculates and returns the difference of its two arguments (9 - 4), which is 5.

Find a Sum

Example +

Example Explained

+(5,3) calculates and returns the sum of its two arguments (5 + 3), which is 8.

Concatonate

Example + (with strings)

Example Explained

+("Hello ","world") returns the concatonatation of its two arguments "Hello " and "world", or "Hello world".

Egg! Comparisons.

Comparison Operators (Applications)

Comparisons are of type Apply. Comparisons such as >, <, and == are applications that each accept two arguments, separated by commas and return true or false

Arg 1: Expression

An expression of type Value, Word, or Apply that is compared based on evaluated value

Arg 2: Expression

An expression of type Value, Word, or Apply that is compared based on evaluated value

Returns: true or false

Comparison operators (applications) return true or false.

Greater Than

Example >

Example Explained

>(5,3) determines if 5 is greater than 3 (like the 5>3) and returns true as 5 is greater than 3.

Testing Equality

Example ==

Example Explained

==(5,5) determines if 5 is equal to 5 and returns true as 5 is equal to 5.

Egg! Do.

Do Block

do() is an application that accepts any number of arguments, separated by commas. Each argument is an expression that will be evaluated in sequence and within the same scope. do() returns the value produced by its last argument.

Returns:

do() returns the value of its last expression.

Do Block

Example

Example Explained

do has two arguments, define(sum,+(3,5)) and print(sum). Each argument is evaluated in sequence. First define creates the binding sum with the value 8. Then, print displays the value of sum.

Egg! If.

If...then...else...

if() is a special form that accepts three arguments, separated by commas. Different than an application, it only evaluates the second or third argument after evaluating the first argument. It behaves similar to the JavaScript Ternary Operator. If the first argument evaluates to not false, then the second argument is evaluated and returned. Otherwise, the third argument is evaluated and returned.

Arg 1: Condition

The first argument must evaluate to true or false.

Arg 2: Expression

The second argument is an expression that is evaluated if the first argument is not false.

Arg 3: Expression

The third argument is an expression that is evaluated if the first argument evaluates to false.

Returns:

if() returns the value of the argument executed (either the second argument or the third argument).

If

Example

Example Explained

if has three arguments, >(age,18), "Old enough to vote", and "Still a minor". age is greater than 18, so the first argument evaluates to true. As a result, the second argument is evaluated and returned.

Egg! While.

While

while() is a special form that accepts two arguments, separated by commas. Different than an application, it only evaluates the second argument after evaluating the first argument. If the first argument evaluates to not false, then the second argument is evaluated repeatedly until the first argument evaluates to false. Each time the second argument is evaluated, it runs in a new scope.

Arg 1: condition

The first argument must evaluate to true or false and is the condition for which the while loop repeats.

Arg 2: Expression

The second argument is an expression that is evaluated if the first argument is not false. This argument is evaluated again and again until the first argument is false. Multiple expressions can be executed if wrapped in a do() statement.

Returns:

while() returns false.

Sum the numbers 1 to 10

Example

Example Explained

define creates a variable sum and sets it equal to 0.
define creates a variable x and sets it equal to 1.
while has two arguments <(x,11) and do(define(sum,+(sum,x)),define(x,+(x,1)),).
The first argument evaluates to false as x is zero and less than 11.
Therefore, the second argument is executed setting sum equal to iteself plus x. Then x is incremented by one. This repeats until x is no longer less than 11.
The while loop terminates. sum, the sum of all integers from 1 to 10, is printed, 55.

Egg! Fun.

Functions

fun() is a special form for defining functions. It accepts one or more arguments, separated by commas. The last argument is the body of the function and is executed when the function is called. The first 0 to n-1 arguments are parameters for the function being defined. Different than an application, fun() only evaluates its last argument when the function it defines is called. At the time of a function call, a new scope is created.

Args 1 through n-1: Function Parameters

The first arguments must be of type word.

Arg n: Function Body

The last argument is an expression that is evaluated when the function is called. All of the first arguments appear in the local scope with values assigned based on the function call.

Returns:

fun() returns the result of evaluating the body, the last argument.

Fun plusOne

Example

Example Explained

define creates a variable plusOne and assigns to it a function created with fun().
fun(num,+(num,1)) has two arguments, a parameter num and the body of the function +(num,1).
The function plusOne() is called with with the argument 10. The function runs, adding one to 10 and returns the result 11, which is printed.

Egg! Comments.

Comments

Comments in Egg are in-line. # stats an in-line comment and the comment ends at the next new-line.

Comments

Example

Example Explained

#store 3 + 5 and #display result are ignored as comments.

Egg! Mocha & Chai Tests

Egg! Documentation


Check out the documentation for Egg.js.

Try it out!