Written in Java
Lox is a object oriented, dynamically typed language. Jlox is an interpreter written for the Lox language in Java. It uses the JVM as the backbone to the interpreter and implements it with a "tree-walk" interpeter. The interpreter works by first scanning the code for tokens, which is fed into the parser to be turned into a list of statements, and then is given to the resolver to run the code.
java -jar Lox.jar [filename]Lines are teriminated with semicolons.
The var keyword is used to initialize variables.
Example:
var a = 4;
print a; // 4Loops should look familar.
Example:
fun fib(n){ // gets up to n of the fibonacci sequence
var a = 0;
var b = 1;
var c = 0;
while (c < n){
c = a + b;
print c;
a = b;
b = c;
}
}The fun keyword is used to define functions.
Example:
fun factorial(n){
if (n < 1){
return 1;
}
return n * factorial(n-1);
}The class keywords is used to define classes and < is used for inheritance of a superclass.
Example:
class Animal {
init(age, name){
this.age = age;
this.name = name;
}
getSound(){
return "generic sound";
}
}
class Lion < Animal {
init(age, name, weight){
super.init(age, name);
this.weight = weight;
}
getSound(){
return "roar";
}
eat(){
return "ate";
}
}
var gabe = Lion(22, "Gabe", 140);
print gabe.weight;
print gabe.getSound();
print gabe.eat();