The lox programming language features:
- Dynamic typing
- Automatic memory management
- Datatypes including
- Boolean
- Number: double precision floating point number
- Strings
- Nil
- Expressions
- Arithmetic
- Comparison
- Logical Operator
- Precedence and grouping
- Statements
- Variables
- Control flows
- Functions
- Closures
- Classes
- Prototype
- Inheritance
The boolean type is pretty straight forward, there are two boolean values, true
and false
true; // not false
false; // not true
The type number settle for double-precision floating point, but we can still have integer and floats
100502; // A positive integer
-3090802; // A negative integer
1005.02; // A float
-3008.02; // A negative float
String behave like we expected in any languages
"I am a string hehe";
""; // Empty string
"123"; // This is a string and not a number
Nil is the value of "null"
Arithmetic operations is the same as C-like languages.
add + me;
subtract - me;
multiply * me;
divide * me;
There's also a prefix one for negative
-negativeMe;
We have the standard comparison seen in language like rust
less < than;
lessThan <= orEqual;
greater > than;
greaterThan >= orEqual;
// Boolean comparison
1 == 2; // false
1 != 2; // true
123 == "abcd"; // false
123 == "123"; // false
We have the not !
, and &&
, or ||
operator.
!false; // true
!true; // false
true && true; // true
true && false; // false
true || false; // true
false || false; // false
We can do standard grouping of expression using ()
.
let average = ( min + max ) * 2;
Statemets dont evalutate to values, instead they are used to change the state of our program. For example:
print("Hello world");
"Some expression";
// We can also group statement using `{}`
{
print("one statement");
print("Two statement")
}
You declare a variable using the let
keyword. You can either initialize it right away, or omit it. If you omit it, the variable value is default to nil.
let newVariable = "abcd"; // "abcd"
let newVariable; // nil
We have the GOAT trifecta: if-else
, while
, for
.
// If statement
if (condition) {
print("true")
} else {
print("false")
}
let a = 1;
while (a < 10) {
print(a);
a = a + 1;
}
for (let a = 0; a < 10; a = a + 1) {
print(a)
}
Function call expression is the same as in Rust or C
createNew(a, b, c);
createNew();
Define your own function is straightforward using the fun
keyword.
fun createNew(a, b) {
print(a + b);
}
fun sum(a, b) {
return a + b;
}
Function are first class. Which mean you can get a reference to it, store it in value, pass around. For example:
fun addPairs(a, b) {
return a + b;
}
fun println(a) {
print(a);
}
println(addPairs(3,4)); // 7
When you combine them together, we can have this.
fun addSome(a) {
let x = 0;
fun add() {
x = x + a;
print(x)
}
return add;
}
let threw = addSome(3); // add
three(); // 3;
three(); // 6;
Here's an example on how to use class in lox
class Cat {
name;
age;
init(name, age) {
this.name = name
this.age = age
}
meow() {
print("meow")
}
eat(x) {
print(x)
}
}
We can inherit from other class using the extends
keyword. We can also override the inherited class initialization.
class Dog extends Cat {
drink;
init(age, name, drink) {
super.init(age, name);
this.drink = drink;
}
}