Tachibana General Labs
Welcome to LAIN picture
2021-04-24 — Welcome to LAIN

LAIN is a small C like programming language.

LAIN is small C like language with a bytecode compiler written in Go. LAIN is based on the Monkey programming language from Thorsten Ball's Writing An Interpreter In Go textbook.

The original version of LAIN used a tree walking interpreter and the current version uses a single pass bytecode compiler with a companion virtual machine. LAIN code, after being interpreted or compiled with the bytecode compiler / VM combo is eventually executed in Go.

Getting started

$ go get gitlab.com/thatzopoulos/lain
$ ./lain -h
Usage: lain [options] [<filename>]
  -d    enable debug mode
  -e string
        engine to use (eval or vm) (default "vm")
  -i    enable interactive mode
  -v    display version information

To execute a lain script simply pass the name to the interpreter:

$ ./lain ./scripts/hello.lain

Scripts can be made executable by adding a suitable shebang line:

$ cat hello.lain
#!/usr/bin/env lain
puts( "Hello, Lain!" );

Execution then works as you would expect:

$ chmod 755 hello.lain
$ ./hello.lain
Hello, Lain!

If a script is not passed into the interpreter, the user will be dropped into a REPL.

Language Details


s1 = "hello LAIN"                     // strings
i = 1000                              // int
b = true                              // bool
a = [1, "2"]                          // array
h = {"name": "Lain", "OS": "Copland"} // hash
n = null                              // null
let add = fn(a, b) { a + b };         // function
let newAdder = fn(x) { fn(y) { x + y } }; // closure


Type      | Syntax                                    | Comments
--------- | ----------------------------------------- | -----------------------
null      | `null`                                    |
bool      | `true false`                              |
int       | 123                                       | a signed 64-bit integer
str       | `"" "foo" "foo"+"bar"`                    |
array     | `[] [1,2]`                                |
hashes    | `{"name": "Lain", "OS": "Copland"}`       |

Infix Operators


5 + 5;
5 - 5;
5 * 5;
5 / 5;


5 > 5;
5 < 5;
5 == 5;
5 != 5;

Prefix operators



len() // Return the number of characters in a string
puts() // Takes an unlimited number of args, prints each on a separate line and 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

Other Notes

Parameters in functions are evaluated from left to right. LAIN cannot make assertions about the order of argument evaluation.

Compiling an identifier is done with a 16-bit opcode instruction, so LAIN is limited to a maximum of 65536 global bindings.

Similarly, due to the 2 byte size of the OpArray opcode, the max number of elements that an array can hold is capped at 655355.

Syntax Highlighting Plugins For Various Text Editors

Outbound Links:

incoming(3): dorcas compiler serial experiments lain

Last update on 2023-07-02, edited 4 times. +0/0fh