sloth is a programming language designed to learn interpreters.
$ git clone https://github.com/sean-d/sloth.git
$ go install .
OR
$ go run main.goThe functionality is minimal as this was an exercise in learning how programming languages work.
- C-like syntax
- variable bindings
- integers and booleans
- a string data structure
- an array data structure
- a hash data structure
- arithmetic expressions
- built-in functions
- first-class and higher-order functions • closures
An example of fib.
let fibonacci = fn(x) {
if (x == 0) {
0;
} else {
if (x == 1) {
1;
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
};
fibonacci(10);
It supports if. else, but not else if.
if (true) {
10;
} else {
5;
}
1 + 2 + (3 * 4) - (10 / 5);
!true;
!false;
+10;
-5;
"Hello" + " " + "World";
if (true) {
return;
}
let identity = fn(x) {
return x;
};
identity("sloth");
Format:
let <identifier> = <expression>;
Example:
let x = 0;
let y = 10;
let foobar = add(5, 5);
let alias = foobar;
let identity = fn(x) { x };
Integer represents an integer value. where are floats? indeed, where are they.
Format:
[-+]?[1-9][0-9]*;
Example:
10;
1234;
Boolean represents a general boolean types.
Format:
true | false;
Example:
true;
false;
let truthy = !false;
let falsy = !true;
String represents a string. Only double quotes can be used.
Format:
"<value>";
Example:
"sloth programming language";
"hello" + " " + "world";
Array represents an ordered contiguous element. Each element can contain different data types.
Format:
[<expression>, <expression>, ...];
Example:
[1, 2, 3 + 3, fn(x) { x }, add(2, 2), true];
let arr = [1, true, fn(x) { x }];
arr[0];
arr[1];
arr[2](10);
arr[1 + 1](10);
Hash expresses data associating keys with values.
Format:
{ <expression>: <expression>, <expression>: <expression>, ... };
Example:
let hash = {
"name": "person",
"age": 50,
true: "a boolean",
99: "an integer"
};
hash["name"];
hash["a" + "ge"];
hash[true];
hash[99];
hash[100 - 1];
Function supports functions like those supported by other programming languages.
Format:
fn (<parameter one>, <parameter two>, ...) { <block statement> };
Example:
let add = fn(x, y) {
return x + y;
};
add(10, 20);
let add = fn(x, y) {
x + y;
};
add(10, 20);
If return does not exist, it returns the result of the last evaluated expression.
let addThree = fn(x) { x + 3 };
let callTwoTimes = fn(x, f) { f(f(x)) };
callTwoTimes(3, addThree);
Passing around functions, higher-order functions and closures will also work.
You can use 6 built-in functions 🚀
It outputs the specified value to stdout.
puts("Hello");
puts("World!");
For String, it returns the number of characters. If it's Array, it returns the number of elements.
len("sloth");
len([0, 1, 2]);
Returns the element at the beginning of Array.
first([0, 1, 2]);
Returns the element at the last of Array.
last([0, 1, 2]);
Returns a new Array with the first element removed.
rest([0, 1, 2]);
Returns a new Array with the element specified at the end added.
push([0, 1], 2);

