This is an interpreter created to learn how programming languages such as Python, Javascript, and others are created. It is much simpler than the languages already mentioned. This Interpreter is based on the book Writing An Interpreter in Go.
- Always tripped up with off-by-one errors
- Small functions often cause jumping around when reading the code. (but sometimes it helps)
- Recursion is sometimes difficult to explain. This interpreter use a lot of recursion.
- Extending the feature of programming language is challenging.
This interpreter can be tried on Playground, which is available online here,along with several examples here
Several feature currently available in the language.
let x = 10 + 5 * 2;
x = 50;
puts(x);
Limitation: variable name only alphabetical
let x = 10;
let y = false;
let arr = [1, 2, 3, "abc", false];
let hash = {"a": 12, 5: "a", false: 12};
puts(x, y, arr[1], hash[false]);
Limitation:
float
currently not supported
let fib = fn(n, cache) {
if (n <= 0) {
return 0;
}
if (n <= 2) {
return 1;
}
if (cache[n]) {
return cache[n];
}
let res = fib(n-1, cache) + fib(n-2, cache);
cache[n] = res;
return res;
}
let cache = {}
let x = fib(100, cache);
puts(x);
if (2 > 3) {
puts("hurray");
} else {
puts("ok")
}
Limitation:
else if
currently not supported.
let arr = [1,2,3,4,5];
let i = 0
while (i < len(arr)) {
puts(arr[i] * arr[i]);
i = i + 1;
}
Loop also possible using recursion.
let map = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
return accumulated;
}
return iter(
rest(arr),
push(accumulated, f(first(arr)))
);
};
return iter(arr, []);
};
let filter = fn(arr, f) {
let iter = fn(arr, accumulated) {
if (len(arr) == 0) {
return accumulated;
}
let x = first(arr);
if (f(x)) {
accumulated = push(accumulated, x);
}
return iter(rest(arr), accumulated);
};
return iter(arr, []);
};
let arr = [1,2,2,3,4];
puts("original", arr);
let filtered = filter(arr, fn(x) { x == 3 });
puts("filtered", filtered);
let squared = map(arr, fn(x) { x * x });
puts("squared", squared);
Limitation:
for-loop
currently not supported.
See more here. You can check the _test file if you are even more curious.
If you want to develop this interpreter even more follow this step
-
Install Go Programming language
-
Install python to develop playground
-
Install Python
-
Install pip
-
move to playground directory
-
Init virtual environment
$ python3 -m venv venv $ source ./venv/bin/activate
-
Install all required packages
$ pip install -r requirements.txt
-
Run playground
$ streamlit run main.py
-
-
Install pre-commit
-
Read installation here
-
Install pre-commit hook to project
$ pre-commit install
-
Thorsten Ball, author of Writing An Interpreter in Go.