The goal of this project was to create a simple programming language and learn about bytecode-compiler/virtual-machine. It is heavily inspired by Robert Nystrom's book "crafting interpreters" and implements features from chapter 14 to 24. But I've made a few changes, which are listed below.
- there are two type of numbers float64 and int64
- few keywords are different
varisletandfunisfn - new variable can't be declared without an initial value
let num = 5;
- Assign(=) is not an operator, it's a statement
- operator precedence order is copied from Golang
- supports all the prefix(~, +, -) and infix(+, -, *, /, %, &, |, ^, <<, >>, ==, !=, <, <=, >, >=, ||, &&) operators
- supports all the
Op=type statements like +=, -=, *=, /= etc. - {} after
if condandwhile condis must, similar to Golang and parenthesis aroundcondis not necessary
if 1<2 {
print "true";
} else {
print "false";
}
printstatement can take multiple argumentsprint "hello", "world";- no support for
forloop becausewhilecan do it all - added support for
breakstatement - doesn't follow the exact same implementation details from the book
- no support for string interning
- no jump in logical expressions
fn fact(n) {
if n <= 1 {
return 1;
}
return n * fact(n-1);
}
let n = 6;
print "factorial of", n, "is", fact(n);
let i = 0;
let sm = 0;
while i <= 1_000_000 {
i += 1;
sm += i;
}
print sm;
$ git clone https://github.com/2asm/glox
$ cd glox
$ go build .
$ ./glox test.gloxhttps://craftinginterpreters.com/
https://interpreterbook.com/
https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
https://en.wikipedia.org/wiki/Operator-precedence_parser
https://github.com/golang/go/tree/master/src/go