-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lox
74 lines (61 loc) · 1.07 KB
/
test.lox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Variable declarations
var a = 10;
var b = 20;
var c = a + b;
print c; // 30
// Arithmetic operations
print a + b; // 30
print a - b; // -10
print a * b; // 200
print b / a; // 2
// Conditional statements
if (a < b) {
print "a is less than b";
} else {
print "a is not less than b";
}
// Loops
var i = 0;
while (i < 5) {
print i;
i = i + 1;
}
// Functions
fun greet(name) {
print "Hello, " + name + "!";
}
greet("Lox");
// Return values
fun add(x, y) {
return x + y;
}
var result = add(3, 4);
print result; // 7
// Closure
fun makeCounter() {
var count = 0;
fun increment() {
count = count + 1;
return count;
}
return increment;
}
var counter = makeCounter();
print counter(); // 1
print counter(); // 2
print counter(); // 3
// Classes and inheritance
class Animal {
speak() {
print "The animal makes a sound.";
}
}
class Dog < Animal {
speak() {
print "The dog barks.";
}
}
var animal = Animal();
animal.speak(); // The animal makes a sound.
var dog = Dog();
dog.speak(); // The dog barks.