EggJs Playground is a web based editor for a primitive programming language Egg built using Javascript. Egg is featured as an example project in the book Eloquent Javascript by Marjin Haverbeke.
- Everything is an expression in Egg. Even '+' or '-' which are operators in Javascript are treated as expressions.
- Each expression in Egg would be an object. Thus, it would contain a type property, which would describe the type of the expression.
- Type:- 'value' means strings or numbers.
- Type:- 'word' means names of variables
- Type:- 'apply' means applications, eg. if else blocks, while loops, etc.
- All apply expressions have additional operator and args property.
- Comment lines start with a '#' character
-
print
- Description :- Prints the given value onto the console
- Example :- print('Hello World')
-
define
- Description :- Defines a variable with the given name and assigns it a value
- Example :- define(x,10)
-
set
- Description :- Changes the value of a previously declared variable
- Example :- set(x,11)
-
if
- Description :- Conditional statement. Takes 3 arguments - condition, expression to execute if true, expression to execute if false
- Example :- if(<(x,5),print('small'), print('large'))
-
array
- Description :- Creates an array with the specified elements
- Example :- define(arr, array(1,2,3))
-
length
- Description :- Returns the length of an array
- Example :- length(arr)
-
element
- Description :- Returns the element at specified index in array. Throws an error if index out of bounds
- Example :- element(arr, 0)
-
do
- Description :- Defines a block of code.
- Example :- do(define(arr, array(1,2,3)), print(element(arr, 0)))
-
while
- Description :- Defines a while loop with condition and a loop body
- Example :- while(>(x,0), do(print(x), set(x, -(x,1))))
-
func
- Description :- Defines a function with arguments and a body. Arguments are separated by commas and the last expression is body
- Example :- func(a,b,print(+(a,b)))