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.
Egg is a JavaScript based programming language. Want to write your own implementation? See Eloquent Javascript for instructions and starter code.
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.
A sequence of digits.
A string is simply a sequence of characters that are not double quotes, wrapped in double quotes.
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 ( ) , "
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.
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.
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 is an application with one argument, the string "Hello World!".
print is an application with one argument, the number 10.
print is an application with one argument, the application
define(color,"green") which evaluates to the value assigned to the binding,
"green".
define() is an application that accepts two arguments, separated by commas and returns the
second argument
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.
A string is simply a sequence of characters that are not double quotes, wrapped in double quotes.
Define returns the value that the second argument evaluates to.
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".
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.
A numeric expression (or a string with the + operator).
A numeric expression (or a string with the + operator).
Math operators (applications) return a calculated numeric value (or a string with the +
operator, when one or more arguments is a string).
-(9,4) calculates and returns the difference of its two arguments (9 - 4), which is 5.
+(5,3) calculates and returns the sum of its two arguments (5 + 3), which is 8.
+("Hello ","world") returns the concatonatation of its two arguments "Hello " and
"world", or "Hello world".
Comparisons are of type Apply. Comparisons such as
>, <, and == are applications that
each accept two arguments, separated by commas
and return true or false
An expression of type Value, Word, or Apply that is compared based on evaluated value
An expression of type Value, Word, or Apply that is compared based on evaluated value
Comparison operators (applications) return true or false.
>(5,3) determines if 5 is greater than 3 (like the 5>3) and returns true as 5 is greater than 3.
==(5,5) determines if 5 is equal to 5 and returns true as 5 is equal to 5.
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.
do() returns the value of its last expression.
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.
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.
The first argument must evaluate to true or false.
The second argument is an expression that is evaluated if the first argument is not false.
The third argument is an expression that is evaluated if the first argument evaluates to false.
if() returns the value of the argument executed (either the second argument or the third
argument).
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.
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.
The first argument must evaluate to true or false and is the condition for which
the while loop repeats.
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.
while() returns false.
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.
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.
The first arguments must be of type word.
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.
fun() returns the result of evaluating the body, the last argument.
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.
Comments in Egg are in-line. # stats an in-line comment and the comment ends at the next
new-line.
#store 3 + 5 and #display result are ignored as comments.
Check out the documentation for Egg.js.