A tree-walk interpreter for the Lox programming language, written in C#, based on Crafting Interpreters.
Lox is a small, dynamically-typed language with C-like syntax, first-class functions, and object-oriented features. This particular implementation extends the base language with a built-in list class.
class Fib {
// returns a function that generates Fibonacci numbers
generator() {
var seq = list();
fun next() {
var n = seq.length();
if (n < 2) {
seq.add(n);
} else {
seq.add(seq.get(n - 1) + seq.get(n - 2));
}
return seq.get(n);
}
return next;
}
}
var next = Fib().generator();
for (var i = 0; i < 7; i = i + 1) {
print next();
}
// 0, 1, 1, 2, 3, 5, 8
Requires .NET 9.
Build with make publish or equivalent, then:
./cslox # start the REPL
./cslox script.lox # run a script
./cslox -p script.lox # print the AST (works in REPL mode too)