Ella lang
Welcome to Ella lang! Ella lang is a computer programming language implemented in Rust.
Checkout the interactive online playground here.
Language features
Basics
Here is the most basic hello world program:
println("Hello World!");
Yep! That's it. Don't forget to add the semicolon (;
) character at the end.
Variables
One can also store values inside variables:
let name = "Ella";
println("Hello " + name + "!");
This should print Hello Ella!
to the console.
Of course, other types exist as well.
let number = 1;
let float = 1.5;
let boolean = true; // or false
Expressions can also be assigned to variables
// same as let computed = 2;
let computed = 10 / 5;
or
let x = 10;
let y = x / 5; // should evaluate to 2
Functions
Like almost every other programming language, Ella supports defining and calling functions (aka methods in some languages):
fn greet() {
println("Hello World!");
}
greet(); // prints Hello World!
Functions can also take parameters:
fn greet(name) {
println("Hello " + name + "!");
}
greet("Luke"); // prints Hello Luke!
Functions can return results:
fn double(x) {
return x * 2;
}
Results are returned using a return
statement.
Expressions
As seen earlier, Ella includes expressions.
Expressions can include arithmetic operators with the appropriate precedence...
1 + 2 * 3 // parsed as 1 + (2 * 3)
(Note that addition operator, +
, works both on numbers and on strings. On numbers, +
is simply math addition; with strings, +
performs string concatenation.)
reference variables...
foo + 10
include function calls (with arguments)...
double(10) + 2
and even reference functions (higher order functions)...
fn my_function() { ... }
my_function // reference to a function (not a function call)
Higher order functions and closures
In Ella, functions are also variables. This means we can pass functions to other functions as arguments. Example:
// This function applies the function f on g
fn apply(f, x) {
return f(x);
}
fn double(x) {
return x * 2;
}
apply(double, 2); // should evaluate to 4
Closures are also supported. Example:
/// This function combines f and g into a single function
fn compose(f, g) {
fn inner(x) {
return f(g(x));
}
return inner;
}
fn add_one(x) { return x + 1; }
fn double(x) { return x * 2; }
let func = compose(add_one, double); // func adds one and than doubles the result
func(3); // evaluates to (3 + 1) * 2 = 8
Functions can also be expressed using lambda syntax. Example:
let f = fn() { return 2; }
f(); // evaluates to 2
Lambda allows inline function creation and thus improves ergonomics.
Control flow
Ella supports structured control flow via if
/else
and while
(for
is still being implemented).
Branching is achieved via if
and else
. The else
block is optional.
if condition {
// do something
} else {
// do something else
}
Note that unlike other languages, the if
and else
blocks must be surrounded with {
and }
brackets. This prevents the infamous "goto fail" incident as well as not requiring parenthesis around the condition. It is more aesthetic and easier to differentiate between control flow statements and function calls.
Looping is achieved via the while
statement.
while condition {
// repeat something
}
If condition
is false since the very beginning, the loop will never execute.
Built-in functions
Ella includes some built-in functions that are defined in Rust:
print(x)
- Prints a valuex
to the console. Not available in playground.println(x)
- Prints a valuex
to the console followed by a new line (\n
character).readln()
- Reads a new line from stdin and returns a string. Not available in playground.assert(value)
- Asserts a certain condition istrue
. Uses Rust'sassert!
macro under the hood and will panic if fail. Not available in playground.assert_eq(value)
- Asserts two values are equal. Uses Rust'sassert_eq!
macro under the hood and will panic if fail. Not available in playground.is_nan(num)
- Returnstrue
if the number isNaN
. Returnsfalse
otherwise. Note that this is the only way to check if a number isNaN
.parse_number(str)
- Parses a string into a floating point number orNaN
if invalid.clock()
- Returns a floating point number representing the number of seconds since the Unix epoch. Useful for simple benchmarks.str(value)
- Converts any value into a string.
This list of features is non exhaustive. More features are currently being implemented. Thanks for checking out this project!