egg
is a toy Programming Language from the book Eloquest Javascript by Marijn Haverbeke, Chapter 12. The Book was pivotal to my early Programming journey. I moved to Rust some time back and in a fit of nostalgia I decided to rewrite egg
in Rust.
✨ Features
- Extensive and Modular standard library;
Core
,Objects
,StringTools
,Console
andFunctions
- Effective Scope Chain: Local Variables and Global Variables work as expected.
- User-Defined Functions: Create functions in Egg using the
fn
keyword. - Higher Order Functions: Pass functions as values to other functions or to built-in
Operators
. - Extensible: Create your own builtin functions by implementing the
Operator
trait. - no_std: Only depends on
alloc
. Enabling thestd
feature adds thePrint
,PrintLine
,ReadLine
andSleep
builtins.
🏋️♂️ Examples
To start executing a script, we need to first parse
it, create a Scope
and assemble a map of builtin functions it can access:
use *;
// Create the default Scope, with necessary constants set
let mut scope = default;
// Create a minimal set of operators
let mut operators = empty;
minimal;
// Parse a Script into a list of expressions
let script = "sum(12.5, 12.5, 25)";
let expressions = parse.unwrap;
// Evaluate the expression
let expression = &expressions; // the call to `sum`
let result = evaluate.unwrap;
assert_eq!;
We can also define custom built-in functions by implementing Operator
on a type:
use *;
use BTreeMap;
// Create the default Scope, with necessary constants set
let mut scope = default;
// Insert base operators, and add console functions; Adds println
let mut operators = empty;
minimal;
console;
// Define a `random(...)` builtin
;
// Insert `random(...)` into Operators map
operators.insert;
// Parse a Script into a list of expressions
let script = r#"
define(iterations, random(0, 120))
repeat(iterations, println("oi oi oi oi oi"))
"#;
let expressions = parse.unwrap;
// Evaluate the expressions; define -> repeat -> ...
for expression in expressions
Example Egg scripts can be found in the
scripts
directory;