LaetLang is an interpreted C style language built by following along Robert Nystrom's book Crafting Interpreters. This is a toy language that was meant as a learning exercise, please do not use it for any projects.
A few of the things that I decided to add that were not part of the interpreter covered in the book:
var textInput = fileR("./out.txt");
print "File content is: " + textInput;
fileln("./out.txt", "Hello World.");
var networkResponse = net("localhost", 2000, "TEST");
print "Server responded with: " + networkResponse;
The network call is a TCP write, so a language level library for http calls could be made.
var promiseResponses = nil;
for(var i = 0; i < 10; i = i + 1) {
var address = asyncNet("localhost", 2000, listN(requestList, i));
promiseResponses = prepend(address, promiseResponses);
}
map(PromiseResolve, promiseResponses);
A very naive implementation of promises by using an eventDict to store threaded operation results, which can then be retrieved by awaiting for the promise to resolve. This means you can do multiple network requests at the same time, and then await for the results.
Other things in the language that are straight from the book implementation of the interpreter:
fun fib(n) {
if (n <= 1) return n;
return fib(n-2) + fib(n-1);
}
fun mod1(a, m, i) {
if (m * i == a) {
return 0;
}
if (m * i < a) {
return mod1(a, m, i+1);
}
return a - m * (i - 1);
}
fun mod(a, b) {
return mod1(a, b, 1);
}
for(var i = 0; i < 100; i = i + 1) {
if (mod(i, 15) == 0) {
print "Fizz Buzz";
} else {
if (mod(i, 3) == 0) {
print "Fizz";
} else {
if (mod(i, 5) == 0) {
print "Buzz";
} else {
print i;
}
}
}
}