LAIN Programming Language

LAIN

In order to learn Go, I decided to try writing an interpreter for my own programming language. LAIN is a small C like language heavily inspired by Thorsten Ball’s language Monkey from Writing An Interpreter In Go. Thorsten’s book builds a programming language from scratch and walks you through the creation of a lexer, a parser, the AST, the object system, and the evaluator portions of an interpreter. His example code was very readable and helped me figure out how to structure tests in Go which has made me feel very comfortable when I have had to heavily refactor sections of code. His book can be purchased here and I highly recommend it to anyone interested in either programming language design or Go.

Language Details

Below is an overview of some of the features of LAIN. A more detailed look into the various parts of the language can be found in the repo’s README.

Types

Lain has the following data types: null, bool, int, str, array, hash, and fn

Builtin Functions

Lain currently has the following builtin functions:

len: Return the number of characters in a string

puts: Takes an unlimited number of args, prints each on a separate line, returns NULL

push(array,value): returns a new array with value pushed onto the end of array

rest(array): returns a new array with the first element of array removed

first(array): Returns the first element of the array

last(array): returns the last element of the array

Script files

When LAIN is run without args, the user enters a REPL. If a file is provided as an arg when invoking the interpreter, the script file will be read and executed.

Below is an example script that calculates the 34th fibonacci number:

let fib = fn(x) {
  if (x == 0) {
    return 0
  }
  if (x == 1) {
    return 1
  }
  return fib(x-1) + fib(x-2)
}

puts(fib(34))

script-running-on-lain

Future Work

I plan on continuing to update LAIN while working through Thorsten’s other book Writing A Compiler In Go. My main goal is to speed up the language implementation. Currently, the script above takes 18 seconds on my desktop to calculate the 34th fibonacci number so there is plenty of room for improvement in terms of speed. For anyone interested in exploring the code or downloading LAIN, the repo can be found on my gitlab here.