-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-clox.lox
166 lines (139 loc) · 2.48 KB
/
test-clox.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
print !(5 - 4 > 3 * 2 == !nil);
print "this " + "is "+ "a " + "test string";
var a = 1000;
var b = a + 300;
b = b + 37;
print b;
var a = 1;
{
print "outer scope before assigning:";
print a;
var a = 1337;
print "outer scope after assigning:";
print a;
{
print "inner scope before assigning:";
print a;
var b = a;
var a = b + 1;
print "inner scope after assigning:";
print b;
print a;
}
print "outer scope:";
print a;
}
print "global scope:";
print a;
print "before if-else";
if (1337 > 0) {
print "1337 is > 0!";
} else {
print "1337 is NOT > 0!";
}
print "after if-else";
var a = 1 or 2;
print a;
a = false or 3;
print a;
a = false or false;
print a;
var b = 1 and 2;
print b;
b = 1 and false;
print b;
b = false and false;
print b;
var l = 10;
while(l > 0) {
print l;
l = l - 1;
}
for(var i=0; i<10; i=i+2) {
print i;
}
fun test_function() {
print "woo we're inside a function!";
}
print test_function;
test_function();
fun sum(a, b, c) {
return a + b + c;
}
print sum(1, 2, 3);
fun fib(n) {
if (n < 2)
return n;
return fib(n - 2) + fib (n - 1);
}
var start = clock();
for (var i = 0; i <= 20; i = i + 1) {
// uncomment to calculate fib
// print fib(i);
}
print clock() - start;
// closures test
var x = "global";
fun outer() {
var x = "outer";
fun inner() {
print x;
}
inner();
}
outer();
fun outer() {
var x = "outside";
fun inner() {
print x;
}
return inner;
}
var closure = outer();
closure();
class Klass {}
print Klass;
print Klass();
class Pair {}
var pair = Pair();
pair.first = 1300;
pair.second = 37;
print pair.first + pair.second; // 1337
class Scone {
topping(first, second) {
print "scone with " + first + " and " + second;
}
}
var scone = Scone();
scone.topping("berries", "cream");
class CoffeeMaker {
init(coffee) {
this.coffee = coffee;
}
brew() {
print "Enjoy your cup of " + this.coffee;
// no reusing the grounds!
this.coffee = nil;
}
}
var maker = CoffeeMaker("coffee and chicory");
maker.brew();
class Doughnut {
cook() {
print "Dunk in the fryer.";
this.finish("sprinkles");
}
finish(ingredient) {
print "Finish with " + ingredient;
}
}
class Cruller < Doughnut {
finish(ingredient) {
// No sprinkles, always icing.
super.finish("icing");
}
}
Doughnut().cook();
Cruller().cook();
var nan = 0 / 0;
print nan == nan;