-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lox
241 lines (211 loc) · 6.44 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Comments (//, /* */)
// This is a comment
/* This is a block comment */
// Print Statements
print("---- Testing print statements ----"); // Expect "Testing print statements"
print(5); // Expect 5
print("Hello, World!"); // Expect "Hello, World!"
// Arithmetic operations (+, -, *, /, ^)
print("---- Testing arithmetic operations ----");
print((1 + 2) * 3 ^ 2 / 4 - 5); // Expect 1.75
// Unary operations (-, !)
print("---- Testing unary operations ----");
print(-(-5)); // Expect 5
print(!true); // Expect false
// Comparison operations (>, >=, <, <=, ==, !=)
print("---- Testing comparison operations ----");
print(5 > 10); // Expect false
print(5 >= 5); // Expect true
print(5 < 10); // Expect true
print(5 <= 5); // Expect true
print(5 == 5); // Expect true
print(5 != 5); // Expect false
// Logical operations (and, or)
print("---- Testing logical operations ----");
print(true and false or true); // Expect true
// Variable Declarations, Assignments, and Access
print("---- Testing variable declarations, assignments, and access ----");
var x = 5;
x = 10;
print(x); // Expect 10
// Augmented Assignments (+=, -=, *=, /=)
print("---- Testing augmented assignments ----");
var x = 10;
x += 5;
print(x); // Expect 15
x -= 5;
print(x); // Expect 10
x *= 2;
print(x); // Expect 20
x /= 4;
print(x); // Expect 5
// Increment and Decrement (++, --)
print("---- Testing increment and decrement ----");
var x = 5;
++x;
print(x); // Expect 6
--x;
print(x); // Expect 5
// Strings and String Concatenation
print("---- Testing strings and string concatenation ----");
var str = "Hello";
print(str); // Expect "Hello"
print(str + ", World!"); // Expect "Hello, World!"
// Variable Scope (Lexical Scoping) and Block Statements
print("---- Testing variable scope ----");
var x = 5;
{
var x = 10;
print(x); // Expect 10
}
print(x); // Expect 5
// Lists, List Indexing, List Assignment, List Length
print("---- Testing lists, list indexing, list assignment, and list length ----");
var list = [1, 2, 3];
print(list[0]); // Expect 1
list[0] = 5;
print(list[0]); // Expect 5
print(length(list)); // Expect 3
// Control Flow (if//else, while, for)
print("---- Testing control flow ----");
x = 0;
if (x < 5) {
print("x is less than 5"); // Expect "x is less than 5"
} else {
print("x is greater than or equal to 5"); //Expect nothing (as x is less than 5)
}
var list = [];
while (x < 5) {
list += x;
++x;
}
print(list); // Expect [0, 1, 2, 3, 4]
for (var i = 0; i < 5; i = i + 1) {
print(i); // Expect 0, 1, 2, 3, 4
}
// Native Functions
print("---- Testing native functions ----");
// print("Enter a number: (time to sleep in seconds (so a small number, unless you want to wait a long time!))";
// var userInput = input();
// print("Your input: " + userInput); // Expect "Your input: <user input>"
var userInput = 2; // Comment this line and uncomment the above code to test input
var startTime = clock();
var sleepTime = convert(userInput, "number");
print(isType(sleepTime, "number")); // Expect "true"
sleepTime = sqrt(sleepTime + randInt(2, 4)) - ln(randFloat(3, 6));
if (sleepTime <= 0) {
print("Non-positive sleep time, skipping.");
} else {
sleep(sleepTime);
print("Slept for: " + (clock() - startTime) + " seconds."); // Expect "Slept for: <roughly user input>"
}
print(length("slept")); // Expect 5
// Global Math Constants
print("---- Testing global math constants ----");
print(PI); // Expect 3.141592653589793
print(E); // Expect 2.718281828459045
// Function declarations and Function Calls
print("---- Testing function declarations and function calls ----");
fun add(a, b) {
return a + b;
}
print(add(5, 10)); // Expect 15
// Closures
print("---- Testing closures ----");
fun makeCounter() {
var i = 0;
fun count() {
i = i + 1;
return i;
}
return count;
}
var counter = makeCounter();
print(counter()); // Expect 1
print(counter()); // Expect 2
print(counter()); // Expect 3
// Closures with captured variables
print("---- Testing closures with captured variables ----");
fun makeAdder(a) {
fun add(b) {
return a + b;
}
return add;
}
var add5 = makeAdder(5);
print(add5(10)); // Expect 15
// Recursion
print("---- Testing recursion ----");
fun factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
print(factorial(5)); // Expect 120
// Classes
print("---- Testing classes ----");
class Counter {
init(start) {
this.i = start;
}
count() { // note that method declarations do not use the fun keyword
this.i = this.i + 1;
return this.i;
}
}
var counter = Counter(3);
print(counter); // Expect "<class Counter instance>"
print(counter.count()); // Expect 4
print(counter.count()); // Expect 5
print(counter.count()); // Expect 6
// Inheritance
print("---- Testing inheritance ----");
class Cat {
init(name) {
this.name = name;
}
speak() {
return this.name + " says 'Meow!'"; // Expect "<name> says 'Meow!'"
}
toString() {
return this.name;
}
}
class Lion < Cat {
speak() {
return this.name + " roars loudly!"; // Expect "<name> roars loudly!"
}
}
var cat = Cat("Whiskers");
var lion = Lion("Simba");
print(cat); // Expect "<class Cat instance>"
print(lion); // Expect "<class Lion instance>"
print(cat.speak()); // Expect "Whiskers says 'Meow!'"
print(lion.speak()); // Expect "Simba roars loudly!"
print(cat.toString()); // Expect "Whiskers"
print(lion.toString()); // Expect "Simba"
// Superclass methods
print("---- Testing superclass methods ----");
class Animal {
speak() {
return "Animal speaks";
}
}
class Dog < Animal {
speak() {
return "Dog barks";
}
speakLikeAnimal() {
return super.speak();
}
}
var dog = Dog();
print(dog); // Expect "<class Dog instance>"
print(dog.speak()); // Expect "Dog barks"
print(dog.speakLikeAnimal()); // Expect "Animal speaks"
// Parse Errors (uncomment to test)
// print("---- Testing parse errors ----");
// print(5 +); // Expect "ParseError: Expected expression after '+'"
// class {} // Expect "ParseError: Expected expression after '+'"
// Interpreter Runtime Errors (uncomment to test)
// print("---- Testing interpreter runtime errors ----");
//print(5 / 0); // Expect "RuntimeError: Cannot divide by zero"