These are just quick snippets to quickly go through all the main features. Loxscript works mostly similar to other OOP-based languages like python, java.
print "Hello World";
Note that LoxScript requires explicit ;
to end the statement.
"this is a string"; // string
true;false; // booleans
12;12.24; // numbers
nil; // None
I would be adding lists, sets and dicts in the future.
a-b;
a+b;
a/b;
c*b;
(a+b)*c; // grouping
-negate;
Note: There is no implicit conversion
a==b;
a!=b;
a>b;
a<b;
a>=b;
a<=b;
!true; // false
true and false; // false.
true and true; // true.
false or false; // false.
true or false; // true.
Truthiness: Aside from false
and nil
everything else is truthy
var x = "New variable";
print x;
x = "New value"; // modifying existing variable
Lox follows block scope
{
var x = 10;
}
print x; // error: Undefined variable x
if (a==b) {
print "a is equal to b";
} else if (a==c) {
print "a is equal to c";
} else {
print "a is not equal to b";
}
Lox supports while
and for
loop
var a = 1;
while (a < 10) {
print a;
a = a + 1;
}
Equivalent for
loop:
for (var a = 1; a < 10; a = a + 1) {
print a;
}
fun add(a,b) {
return a+b;
}
print add(1, 2);
Functions are first-class citizens in Lox.
fun returnFunction() {
var outside = "outside";
fun inner() {
print outside;
}
return inner;
}
var fn = returnFunction();
fn();
class Foo {
init(x) {
this.x = x;
}
test() {
print ("x is " + this.x);
}
}
var foo = Foo('VALUE OF XXXXX');
foo.test(); // x is VALUE OF XXXXX
class BaseClass {
init() {
this.x = 'XX';
}
method() {
print "this is a method of base class!";
print "And the value of x is " + this.x;
}
}
class SubClass < BaseClass {
init() {
this.x = 'SUBCLASS';
}
method() {
super.method();
}
}
var subclass = SubClass();
subclass.method();
// This prints
// this is a method of base class!
// And the value of x is SUBCLASS