Skip to content

genericalexacc/LaetLang

Repository files navigation

LaetLang 💻

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:

Reading from a file:

var textInput = fileR("./out.txt");
print "File content is: " + textInput;

Writting to files:

fileln("./out.txt", "Hello World.");

Making a network call:

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.

Async Network call:

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:

Defining a function:

fun fib(n) {
    if (n <= 1) return n;
    return fib(n-2) + fib(n-1);
}

FizzBuzz is messy because of no elif :(

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;
        }
      }
    }
}

About

LaetLang is an interpreted C style language. It has file reading/writting, TCP network calls and awaitable promises.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages