A little language interpreter written in Go
The littlelang programming language is a little language (funny that) designed by Ben Hoyt for fun and (his own) learning. It's kind of a cross between Python, JavaScript, and Go. It's a dynamically but strongly-typed language with the usual data types, first-class functions, closures, and a bit more.
The code includes a tokenizer and parser and a (slowish but simple) tree-walk interpreter written in Go. There's also an interpreter written in littlelang itself, just to prove the language is powerful enough to write somewhat real programs in.
Below are a couple of examples of the language, the full language "spec", and the littlelang grammar. However, you might be better off if you read my introduction first.
…Littlelang's syntax is a cross between Go and Python. Like Go, it uses func to define functions (named or anonymous), requires { and } for blocks, and doesn't need semicolons. But like Python, it uses keywords for and and or and in. Like both those languages, it distinguishes expressions and statements.
It's dynamically typed and garbage collected, with the usual data types: nil, bool, int, str, list, map, and func. There are also several builtin functions.
Calling this a "spec" is probably a bit grandiose, but it's the best you'll get.
A littlelang program is simply zero or more statements. Statements don't actually have to be separated by newlines, only by whitespace. The following is a valid program (but you'd probably use newlines in the if block in real life):
s = "world"
print("Hello, " + s)
if s != "" { t = "The end" print(t) }
// Hello, world
// The endBetween tokens, whitespace and comments (// through to the end of a line) are ignored.
Littlelang has the following data types: nil, bool, int, str, list, map, and func. The int type is a signed 64-bit integer, strings are immutable arrays of bytes, lists are growable arrays (use the append() builtin), and maps are unordered hash tables. Trailing commas are allowed after the last element in a list or map:
| Type | Syntax | Comments |
|---|---|---|
| nil | nil |
|
| bool | true false |
|
| int | 0 42 1234 -5 |
-5 is actually 5 with unary - |
| str | "" "foo" "\"quotes\" and a\nline break" |
Escapes: \" \\ \t \r \n |
| list | [] [1, 2,] [1, 2, 3] |
|
| map | {} {"a": 1,} {"a": 1, "b": 2} |
Littlelang supports if, else if, and else. You must use { ... } braces around the blocks:
a = 10
if a > 5 {
print("large")
} else if a 0 {
print(i)
i = i - 1
}
// 3
// 2
// 1Littlelang does not have break or continue, but you can return value as one way of breaking out of a loop early.
For loops are similar to Python's for loops and Go's for range loops. You can iterate through the (Unicode) characters in a string, elements in a list (the range() builtin returns a list), and keys in a map.
Note that iteration order of a map is undefined -- create a list of keys and sort() if you need that.
for c in "foo" {
print(c)
}
// f
// o
// o
for x in [nil, 3, "z"] {
print(x)
}
// nil
// 3
// z
for i in range(5) {
print(i, i*i)
}
// 0 0
// 1 1
// 2 4
// 3 9
// 4 16
map = {"a": 1, "b": 2}
for k in map {
print(k, map[k])
}
// a 1
// b 2You can define named or anonymous functions, including functions inside functions that reference outer variables (closures). Vararg functions are supported with ... syntax like in Go.
func add(a, b) {
return a + b
}
print(add(3, 4))
// 7
func make_adder(n) {
func adder(x) {
return x + n
}
return adder
}
add5 = make_adder(5)
print(add5(7))
// 12
// Anonymous function, equivalent to "func plus(nums...)"
plus = func(nums...) {
sum = 0
for n in nums {
sum = sum + n
}
return sum
}
print(plus(1, 2, 3))
lst = [4, 5, 6]
print(plus(lst...))
// 6
// 15A grammar note: you can't have a "bare return" -- it requires a return value. So if you don't want to return anything (functions always return at least nil anyway), just say return nil.
Assignment can assign to a name, a list element by index, or a map value by key. When assigning to a name (variable), it always assigns to the local function scope (like Python). You can't assign to an outer scope without using a mutable list or map (there's no global or nonlocal keyword).
To help with object-oriented programming, obj.foo = bar is syntactic sugar for obj["foo"] = bar. They're exactly equivalent.
i = 1
func nochange() {
i = 2
print(i)
}
print(i)
nochange()
print(i)
// 1
// 2
// 1
map = {"a": 1}
func change() {
map.a = 2
print(map.a)
}
print(map.a)
change()
print(map.a)
// 1
// 2
// 2
lst = [0, 1, 2]
lst[1] = "one"
print(lst)
// [0, "one", 2]
map = {"a": 1, "b": 2}
map["a"] = 3
map.c = 4
print(map)
// {"a": 3, "b": 2, "c": 4}Littlelang supports pretty standard binary and unary operators. Here they are with their precedence, from highest to lowest (operators of the same precedence evaluate left to right):
| Operators | Description |
|---|---|
[] |
Subscript |
- |
Unary minus |
* / % |
Multiplication |
+ - |
Addition |
>= in |
Comparison |
== != |
Equality |
not |
Logical not |
and |
Logical and (short-circuit) |
or |
Logical or (short-circuit) |
Several of the operators are overloaded. Here are the types they can operate on:
| Operator | Types | Action |
|---|---|---|
[] |
str[int] |
fetch nth byte of str (0-based) |
[] |
list[int] |
fetch nth element of list (0-based) |
[] |
map[str] |
fetch map value by key str |
- |
int |
negate int |
* |
int * int |
multiply ints |
* |
str * int |
repeat str n times |
* |
int * str |
repeat str n times |
* |
list * int |
repeat list n times, give new list |
* |
int * list |
repeat list n times, give new list |
/ |
int / int |
divide ints, truncated |
% |
int % int |
divide ints, give remainder |
+ |
int + int |
add ints |
+ |
str + str |
concatenate strs, give new string |
+ |
list + list |
concatenate lists, give new list |
+ |
map + map |
merge maps into new map, keys in right map win |
- |
int - int |
subtract ints |
>= |
same as `` for func. |
type(value) returns a str denoting the type of value: nil, bool, int, str, list, map, or func.
upper(str) returns an uppercased version of str.
Below is the full littlelang grammar in pseudo-BNF format. Rules are in lowercase letters like "statement", and single tokens are in allcaps like "COMMA" and "NAME" (see tokenizer/tokenizer.go for the full list of tokens).
…To build, install Go, then fetch and build like so:
cd ~/go/src # or wherever your Go code lives
go get github.com/benhoyt/littlelang
cd github.com/benhoyt/littlelang/
go buildYou can then run one of the examples using the Go interpreter binary:
./littlelang examples/readme.llIf you want to get really meta, run the README example using the littlelang interpreter running under the Go interpreter:
./littlelang littlelang.ll examples/readme.ll
./littlelang littlelang.ll littlelang.ll examples/readme.llHow deep does the rabbit hole go?
Many thanks to Bob Nystrom for his free book Crafting Interpreters, which is a great read and helped me understand how to implement closures.
No open issues yet, or sync has not completed.