A Go implementaion of the tree walk interpreter for the Lox language. I wrote this version while reading the book Crafting Interpreters by Robert Nystrom.
Download the latest binary from the Releases page
as per your platform. Unzip the package, you'll have the glox binary. Running only ./glox
will give you the interactive terminal or you can pass in the location of a glox script file to
run a script e.g. ./glox hello.glox
where hello.glox
contains the glox script in the same
directory as the glox binary.
// Your very first lox program.
print "Hello, world";
var a = 5;
var b = 6;
var c = a + b;
print c; // prints 11
for (var i = 0; i < 5; i = i+1) {
print i;
}
// prints
// 0
// 1
// 2
// 3
// 4
var a = 5;
if (a >= 4) {
print "a is greater than or equal to 4";
} else {
print "a is less than 4";
}
fun printSum(a, b) {
print a + b;
}
printSum(5, 6); // prints 11
// Closure
fun returnFunction() {
var outside = "outside";
fun inner() {
print outside;
}
return inner;
}
var fn = returnFunction();
fn(); // prints outside
class Breakfast {
// constructor
init(meat, bread) {
this.meat = meat;
this.bread = bread;
}
cook() {
print "Eggs a-fryin'!";
}
serve(who) {
print "Enjoy your " + this.meat + " and " +
this.bread + ", " + who + ".";
}
}
// instantiate
var breakfast = Breakfast("sausage", "toast");
breakfast.serve("Sayantan"); // prints "Enjoy your sausage and toast, Sayantan".
class Brunch < Breakfast {
init(meat, bread, drink) {
super.init(meat, bread);
this.drink = drink;
}
}