-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
585 lines (452 loc) · 9.42 KB
/
test.lua
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
local pt = require("pt")
local parser = require("kiara.frontend.parser")
local compiler = require("kiara.backend.compiler")
local backend_build = require("kiara.backend.build")
local function run_code(input, debug)
compiler:clean()
if debug == true then
print("input:", input)
print("")
end
-- print(input)
local ast = parser.parse(input)
if debug == true then
print("ast:")
print(pt.pt(ast))
end
local code = compiler:compile(ast)
if debug == true then
print("\ncode:")
print(pt.pt(code))
end
local stack = {}
local mem = { k0 = 0, k1 = 1, k11 = 10 }
backend_build.run(code, mem, stack, 0)
if debug == true then
print("\nstack:")
print(stack[1])
end
return stack[1]
end
local function assert_code(input, expected, debug)
local result = run_code(input, debug)
assert(result == expected)
end
local function assert_code_error(input, expected, debug)
local _, err = pcall(run_code, input, debug)
assert(string.match(err, expected) == expected)
end
local function assert_stat(input, expected, debug)
local code = "function main() {\n return " .. input .. "\n}"
assert_code(code, expected, debug)
end
-- Assert basic operations
assert_stat("1 + 1", 2)
assert_stat("1 - 1", 0)
assert_stat("2 * 2", 4)
assert_stat("2 / 2", 1)
assert_stat("1 + -1", 0)
-- FINAL PROJECT: Adding more operators - part 1
assert_stat("4 % 2", 0)
assert_stat("4 % 3", 1)
assert_stat("3 ^ 3", 27)
assert_stat("3 ^ 3 ^3", 7625597484987)
-- hexadecimal numbers
assert_stat("0x1", 1)
assert_stat("0xc0ffee", 12648430)
assert_stat("0Xc0ffee", 12648430)
-- FINAL PROJECT: Adding more operators - part 2
assert_stat("-1", -1)
assert_stat("- -1", 1)
assert_stat("-(1+2)", -3)
assert_stat("(1 < 2)", 1)
assert_stat("(1 > 2)", 0)
assert_stat("(1 >= 1)", 1)
assert_stat("(1 >= 0)", 1)
assert_stat("(1 >= 3)", 0)
assert_stat("(1 <= 2)", 1)
assert_stat("(2 <= 2)", 1)
assert_stat("(5 <= 2)", 0)
assert_stat("(1 == 1)", 1)
assert_stat("(1 == 2)", 0)
assert_stat("(1 != 2)", 1)
assert_stat("(1 != 1)", 0)
-- Floating-point numbers
assert_stat("1.5", 1.5)
assert_stat("-1.5", -1.5)
assert_stat("0.1", 0.1)
assert_stat(".1", 0.1)
assert_stat(".5", 0.5)
-- scientific notation
assert_stat("0.5E-3", 0.0005)
assert_stat("0.5e-3", 0.0005)
assert_stat(".5e-3", 0.0005)
assert_stat("10e3", 10000.0)
assert_stat("10E3", 10000.0)
-- Rules for Identifiers
assert_code([[
function main() {
var a_b = 2;
return a_b
}
]], 2)
assert_code([[
function main() {
var two_gt_one? = (2 > 1);
return two_gt_one?
}
]], 1)
-- FINAL PROJECT: Empty Statement
assert_code([[
function main() {
{};
;;;;
return 0
}
]], 0)
-- FINAL PROJECT: Print Statement
assert_code([[
function main() {
@ (2);
return 0
}
]], 0)
assert_code_error([[
function main() {
var a = b + 1;
}
]], "variable b is not defined")
-- FINAL PROJECT: Error messages with line numbers
assert_code_error([[
function main() {
@ (2)
return 0
}
]], "syntax error on line: 5 col: 33")
-- FINAL PROJECT: Block comments
assert_code([[
function main() {
#{
this is a
block comment
#}
var a = 1;
return a
}
]], 1)
-- FINAL PROJECT: ‘not’ operator
assert_stat("!(1 > 2)", 1)
assert_stat("!!(1 > 2)", 0)
-- FINAL PROJECT: ‘elseif’
assert_code([[
function main() {
if 0 {return 1} else {return 2}
}
]], 2)
assert_code([[
function main() {
var a = 3;
var b = 0;
if (a > 4) { b = 5 }
elseif (a == 2) {b = 200000000}
elseif (a == 3) {b = 300000000}
else { b = 10 };
return b
}
]], 300000000)
assert_code([[
function main() {
var a = 2;
var b = 0;
if (a > 4) { b = 5 }
elseif (a == 2) {b = 200000000}
elseif (a == 3) {b = 300000000}
else { b = 10 };
return b
}
]], 200000000)
assert_code([[
function main() {
var a = 1;
var b = 0;
if (a > 4) { b = 5 }
elseif (a == 2) {b = 200000000}
elseif (a == 3) {b = 300000000}
else { b = 10 };
return b
}
]], 10)
-- arrays
assert_code([[
function main() {
var a = new[10];
a[5] = 50;
return a[5]
}
]], 50)
-- FINAL PROJECT: print arrays
assert_code([[
function main() {
var a = new[10];
a[5] = 50;
a[2] = 1;
@ (a);
return a[5] + a[2]
}
]], 51)
-- Name Conflicts Among Functions
assert_code_error([[
function foo() {
return 1
}
function foo() {
return 2
}
function main() {
return foo()
}
]], "function 'foo' already declared")
-- Optional initialization for local variables
assert_code([[
function main () {
var foo;
return foo
}
]], 0)
assert_code([[
function main () {
var foo;
foo = 42;
return foo
}
]], 42)
-- Checking redeclaration of variables
assert_code([[
function foo() {
var bar = 11;
return bar;
}
function main () {
return foo()
}
]], 11)
assert_code_error([[
function foo() {
var bar = 10;
var bar = 11;
}
function main () {
return foo()
}
]], "local variable: bar already declared locally")
-- Checking redeclaration of variables and parameters
assert_code([[
function foo(bar) {
return bar;
}
function main () {
return foo(15)
}
]], 15)
assert_code_error([[
function foo(bar, bar) {
return bar;
}
function main () {
return foo(15, 16)
}
]], "param bar already declared")
-- Check for Main
assert_code([[
function main () {
var foo = 42;
return foo
}
]], 42)
assert_code_error([[
function main (bar) {
var foo = 42;
return foo
}
]], "main function must have no parameters")
-- Adding checks to the interpreter
assert_code([[
function main () {
var a = new[10];
a[10] = 9;
return a[10]
}
]], 9)
assert_code_error([[
function main () {
var a = new[10];
a[11] = 9;
return a[11]
}
]], "index out of range. max array size: 10")
-- FINAL PROJECT: Logical operators
-- and
assert_stat("1 and 0", 0)
assert_stat("0 and 1", 1)
assert_stat("5 and 10", 10)
assert_stat("10 and 5", 5)
assert_stat("10 and 10", 10)
-- or
assert_stat("1 or 0", 1)
assert_stat("0 or 1", 1)
assert_stat("5 or 10", 5)
assert_stat("10 or 5", 10)
assert_stat("10 or 10", 10)
-- while test
assert_code([[
function main() {
var n = 0;
var i = 0;
while (i < 10) {
n = n + 1;
i = i + 1;
};
return n;
}
]], 10)
-- wrong number of arguments
assert_code([[
function foo(bar) {
return bar + 1;
}
function main () {
return foo(5);
}
]], 6)
assert_code_error([[
function foo(bar) {
return bar + 1;
}
function main () {
return foo(5, 6);
}
]], "wrong number of arguments to foo")
-- FINAL PROJECT: Forward Declarations
assert_code([[
function odd();
function odd() {
return 2;
}
function even () {
var n = 5;
if n {
return odd()
} else {
return 1
}
}
function main () {
var n = 10;
return even()
}
]], 2)
assert_code_error([[
function odd();
function even () {
var n = 5;
if n {
return odd()
} else {
return 1
}
}
function main () {
var n = 10;
return even()
}
]], "function: odd not declared")
assert_code_error([[
function odd();
function odd() {
return 2;
}
function odd() {
return 5;
}
function even () {
var n = 5;
if n {
return odd()
} else {
return 1
}
}
function main () {
var n = 10;
return even()
}
]], "function 'odd' already declared")
-- test recursion
assert_code([[
function fact(n) {
if n {
return n * fact(n - 1)
} else {
return 1
}
}
function main () {
return fact(6)
}
]], 720)
assert_code([[
function foo(a, b, c=20) {
return c
}
function main () {
return foo(1, 0)
}
]], 20)
-- FINAL PROJECT: Default Arguments
assert_code([[
function fa(b, a=2) {
return a;
}
function ab(a, b) {
var res = fa(0) + 5;
return res;
}
function main () {
return ab(0, 0);
}
]], 7)
assert_code([[
function fa(b, a=2) {
return a;
}
function ab(a, b) {
var res = fa(0, b) + 5;
return res;
}
function main () {
return ab(0, 5);
}
]], 10)
assert_code([[
function foo(a) {
return a + 1;
}
function bar(b) {
return b + 1;
}
function main () {
return foo(bar(1));
}
]], 3)
-- assert_stat("1 and 0", 0)
-- FINAL PROJECT: Multidimensional new
-- assert_code([[
-- function main() {
-- var d = new[3][5][2];
-- d[2][1][1] = 6000;
-- d[3][1][1] = 4000;
-- @ (d[2][1][1]);
-- @ (d[3][1][1]);
-- return d[2][1][1]
-- }
-- ]], 6000, true)