This is a simple programming language implementation based on the guide from Eloquent JavaScript Chapter 12. The book provides a basic level implementation. I've managed to a add on a few more features.
- Simple Syntax: Everything is an expression
- Functions: First-class functions with closures
- Control Flow:
ifconditionals andwhileloops - Variables: Variable definition and assignment
- Arrays: Built-in array support
- Comments: Line comments starting with
# - REPL: Interactive shell for testing
The language implementation consists of several modules:
- parser.js - Converts source code into Abstract Syntax Tree (AST)
- evaluator.js - Executes the AST
- special-forms.js - Implements special syntax (if, while, define, etc.)
- environment.js - Defines built-in functions and global scope
- egg.js - Main module with CLI and REPL
- Node.js (version 12 or higher)
- Create the project directory structure:
egg-language/
├── src/
├── examples/
└── tests/
- Copy all the provided files into their respective directories
- Navigate to the project directory:
cd egg-language
On Unix/Linux/macOS:
# Make setup script executable and run it
chmod +x setup.sh
./setup.shOn Windows:
# Run the Windows setup script
setup.batgit clone https://github.com/othaime-en/egg-lang
cd egg-lang
npm installAll Platforms: Run an Egg program from a file:
# Using npm script
npm start examples/basic.egg
# Using Node directly
node src/egg.js examples/basic.eggWindows Command Prompt:
node src\egg.js examples\basic.eggPowerShell (Windows):
node src/egg.js examples/basic.eggStart the interactive shell:
npm run repl
# or
node src/egg.js --replExecute the test suite:
npm test42 # Numbers
"hello" # Strings
true # Boolean true
false # Boolean false
define(x, 10) # Define a variable
set(x, 20) # Update existing variable
# Simple function
define(square, fun(x, *(x, x)))
square(5) # Returns 25
# Multiple parameters
define(add, fun(a, b, +(a, b)))
# Recursive functions
define(factorial, fun(n,
if(==(n, 0),
1,
*(n, factorial(-(n, 1)))
)
))
# Conditional
if(>(x, 5), print("big"), print("small"))
# Loop
while(<(i, 10),
do(
print(i),
define(i, +(i, 1))
)
)
# Sequential execution
do(
define(x, 5),
define(y, 10),
+(x, y)
)
define(arr, array(1, 2, 3, 4)) # Create array
length(arr) # Get length
element(arr, 0) # Get element at index
# This is a comment
define(x, 5) # Comments can be at end of line
# Multiple lines of comments
# are supported
+(a, b)- Addition-(a, b)- Subtraction*(a, b)- Multiplication/(a, b)- Division
==(a, b)- Equality!=(a, b)- Inequality<(a, b)- Less than>(a, b)- Greater than<=(a, b)- Less than or equal>=(a, b)- Greater than or equal
&&(a, b)- Logical AND||(a, b)- Logical OR!(a)- Logical NOT
array(...values)- Create arraylength(array)- Get array lengthelement(array, index)- Get element at index
print(value)- Print value to consoletype(value)- Get type of value
abs(n)- Absolute valuemin(a, b)- Minimummax(a, b)- Maximumfloor(n)- Floorceil(n)- Ceilinground(n)- Roundrandom()- Random number 0-1
do(
define(total, 0),
define(count, 1),
while(<(count, 11),
do(
define(total, +(total, count)),
define(count, +(count, 1))
)
),
print(total) # Prints 55
)
do(
define(fib, fun(n,
if(<(n, 2),
n,
+(fib(-(n, 1)), fib(-(n, 2)))
)
)),
print(fib(10)) # Prints 55
)
do(
define(numbers, array(5, 2, 8, 1, 9)),
define(sum, fun(arr,
do(
define(i, 0),
define(total, 0),
while(<(i, length(arr)),
do(
define(total, +(total, element(arr, i))),
define(i, +(i, 1))
)
),
total
)
)),
print(sum(numbers)) # Prints 25
)