-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
2873 lines (2723 loc) · 59.9 KB
/
test.ts
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from 'fs'
import * as child_process from 'child_process'
import { scanTokens } from './src/scanner'
import { parse } from './src/parser'
import { resolve } from './src/resolver'
import { emit } from './src/backend'
import { ReportError, UTF8Codec } from './src/util'
import * as ast from './src/nodes'
import { TokenType } from './src/tokens'
enum Passes {
SCAN = 1 << 0,
PARSE = 1 << 1,
RESOLVE = 1 << 2,
THROUGH_PARSE = SCAN | PARSE,
THROUGH_RESOLVE = SCAN | PARSE | RESOLVE
}
function expectAST(source: string, expected: string) {
source = source.trim()
const errors: string[] = []
const reportError: ReportError = (line, msg) => {
errors.push(`${line}: ${msg}`)
}
const tokens = scanTokens(source, reportError)
const output = parse(tokens, reportError)
let sexpr = "("
output.topLevelStatements.forEach((stmt, i) => {
if (i > 0) sexpr += " "
sexpr += ast.astToSExpr(stmt)
})
sexpr += ")"
expect(sexpr).toEqual(expected)
expect(errors).toEqual([])
}
function expectErrors(source: string, expectedErrors: string[], passes: Passes): ast.Context | null {
source = source.trim()
const errors: string[] = []
const reportError: ReportError = (line, msg) => {
errors.push(`${line}: ${msg}`)
}
const tokens = scanTokens(source, reportError)
let context: ast.Context | null = null
if (errors.length == 0 && (passes & Passes.PARSE)) {
context = parse(tokens, reportError)
if (errors.length == 0 && (passes & Passes.RESOLVE)) {
resolve(context, reportError)
}
}
expect(errors).toEqual(expectedErrors)
return context
}
async function expectOutput(source: string, expectedOutput: string) {
const context = expectErrors(source, [], Passes.THROUGH_RESOLVE)
if (context) {
const code = emit(context)
fs.writeFileSync("test/tmp.wat", code)
child_process.execSync(`npx -p wabt wat2wasm test/tmp.wat -o test/tmp.wasm`)
const codec = new UTF8Codec()
let ioBuffer = ""
let output = ""
const instance = await WebAssembly.instantiate(fs.readFileSync("test/tmp.wasm"), {
io: {
log: (x: any) => {
output += x + "\n"
},
putchar: (x: number) => {
ioBuffer += codec.decodeASCIIChar(x)
},
putf: (x: number) => {
ioBuffer += x
},
puti: (x: number) => {
ioBuffer += x
},
flush: () => {
output += ioBuffer + "\n"
ioBuffer = ""
}
}
});
const exports = instance.instance.exports as any
exports.__init_globals__()
exports.main()
expect(output).toBe(expectedOutput)
}
}
describe("parser", () => {
const expectParseErrors = (source: string, expectedErrors: string[]) => expectErrors(source, expectedErrors, Passes.THROUGH_PARSE)
test("top-level statements", () => {
expectAST(`
var a = 1;
var b = 2;
def foo(x int, y byte) int {}
var c = true;
def bar(z bool) {}
var d = 5.0;
def main() {}
var e = -4.0;
`,
"(" +
"(var a 1) " +
"(var b 2) " +
"(def foo ((param x int) (param y byte)) ()) " +
"(var c true) " +
"(def bar ((param z bool)) ()) " +
"(var d 5.0) " +
"(def main () ()) " +
"(var e (- 4.0))" +
")")
})
test("operator precedence", () => {
expectAST(`
var a = 1 + 2 * -(3.5 - -7.0) / (float(1) + 5.) == int(5.0) > -4 || x && !!foo(bar(x, y), foo() && bar());
`,
"(" +
"(var a " +
"(|| " +
"(== " +
"(+ " +
"1 " +
"(/ " +
"(* " +
"2 " +
"(- ((- 3.5 (- 7.0))))" +
") " +
"((+ (float 1) 5.0))" +
")" +
") " +
"(> (int 5.0) (- 4))" +
") " +
"(&& " +
"x " +
"(! (! " +
"(call " +
"foo " +
"(" +
"(call bar (x y)) " +
"(&& " +
"(call foo ()) " +
"(call bar ())" +
")" +
")" +
")" +
"))" +
")" +
")" +
")" +
")")
})
test("operator associativity", () => {
expectAST(`
var a = 1 + 2 - 3 + 4 - 5;
`,
"(" +
"(var a " +
"(- " +
"(+ " +
"(- (+ 1 2) 3) " +
"4" +
") " +
"5"+
")" +
")" +
")")
expectAST(`
var a = 1 * 2 / 3 * 4 / 5;
`,
"(" +
"(var a " +
"(/ " +
"(* " +
"(/ (* 1 2) 3) " +
"4" +
") " +
"5"+
")" +
")" +
")")
})
test("Function definitions", () => {
expectParseErrors(`
def foo {}
def foo() void {}
def foo(a, b) {}
def foo(a int, b int) int;
def foo(a int, b int) int {}
def foo2(a int, b int) int {
print 3.14159265358979626;
`,
[
"1: Expect '(' after function name.",
"2: Invalid type specifier starting at 'void'.",
"3: Invalid type specifier starting at ','.",
"4: Expect '{' before function body.",
"7: Expect '}' after block."
])
// If we ever add nested functions, can remove this test
expectParseErrors(`
def foo() {
def bar() {}
print 123;
}
`,
[
"2: Expect expression."
])
})
test("Variable declaration outside block", () => {
expectParseErrors(`
var allowed1 = 1;
def foo() {
var allowed2 = 2;
if (true) {
var allowed3 = 3;
} else var notAllowed1 = 1;
if (true) var notAllowed2 = 2;
}
`,
[
"6: Expect expression.",
"7: Expect expression."
])
})
test("Function call syntax", () => {
expectParseErrors(`
def main() {
foo(x 1);
foo(x,1;
foo(x,1);
foo(bar(x), foobar(1 + 2) * 3);
// if we add first-class functions, this is not an error
foo(x,1)();
}
`,
[
"2: Expect ',' between arguments.",
"3: Expect ',' between arguments.",
"7: Expect ';' after expression statement."
])
})
test("Loop syntax", () => {
expectParseErrors(`
def main() {
while (foo() == bar()) loop(); // ok
while (foo() == bar()) { // ok
loop();
}
while (var i = 0) loop(); // error
while () loop(); // error
for (;;) loop(); // ok
for (;;) { // ok
loop();
}
for (var p = n; p != end; p = next(n)) loop(); // ok
for (var p = n;;) loop(); // ok
for (; p != end;) loop(); // ok
for (;; p = next(n)) loop(); // ok
for (var p = n; var q = n; p = next(n)) loop(); // error
}
`,
[
"6: Expect expression.",
"7: Expect expression.",
"16: Expect expression.",
// TODO: silence extra error, e.g. with a for loop error production
"16: Expect ';' after expression statement."
])
})
test("Duplicate symbol declaration", () => {
expectParseErrors(`
var a = 1;
var b = 2;
var a = b;
`,
[
"3: 'a' is already declared in this scope."
])
expectParseErrors(`
var a = 1;
var b = 2;
def foo() {
var a = b;
var b = 3.14;
{
var a = b;
var b = 42;
var b = 42;
}
}
def bar() {
var a = -1;
var b = 1337;
var a = 0;
}
`,
[
"9: 'b' is already declared in this scope.",
"15: 'a' is already declared in this scope."
])
expectParseErrors(`
def foo(a int, b int, a int) {}
`,
[
"1: 'a' is already declared in this scope."
])
})
test("Invalid assignment target", () => {
expectParseErrors(`
def foo() int {
return 1;
}
def main() {
var a = 0;
var y = true;
var z = [1, 2, 3];
var p = &a;
2 = a; // error
foo() = a; // error
&a = p; // error
false = y; // error
[4, 5, 6] = z; // error
}
`,
[
"9: Invalid assignment target.",
"10: Invalid assignment target.",
"11: Invalid assignment target.",
"12: Invalid assignment target.",
"13: Invalid assignment target."
])
})
test("Invalid variable declaration", () => {
expectParseErrors(`
def main() {
var a += 0;
var 0 = 0;
var a int;
var a 1 = 0;
var a int = 0; // ok
var b = 0; // ok
}
`,
[
"2: Invalid type specifier starting at '+='.",
"3: Expect identifier after 'var'.",
"4: Expect '=' after variable declaration.",
"5: Invalid type specifier starting at '1'.",
])
})
test("Hex literals", () => {
expectParseErrors(`
var x = 0xAABBCCDD; // ok
var y = 0xAAABBCCDD; // error
`,
[
"2: Hex literal does not fit in any numeric type.",
])
})
test("Character literals", () => {
expectParseErrors(`
var x = 'h'; // ok
var y = 'hello'; // error
var z = 'é'; // error
`,
[
"2: Invalid character literal (use double quotes for strings).",
"3: Invalid character literal (only ASCII characters allowed)."
])
})
test("Struct statements", () => {
expectParseErrors(`
struct Point { x float, y float }
struct Point { x int, y int } // error!
struct Point2 { x int, y int }
struct Semicolons { x int; } // error!
struct NoBody; // error!
struct DuplicateMember { x int, y int, y int } // error!
struct BadBody {
def foo() {} // error!
}
def FunctionStructCollision() {}
struct FunctionStructCollision { x int, y int } // error!
struct Empty {} // ok
def main() {
struct Vector { x float, y float } // error!
print "synchronize";
}
`,
[
"2: 'Point' is already declared in this scope.",
"5: Missing comma after member.",
"5: Only variable declarations and function definitions allowed at the top-level.",
"6: Expect '{' after struct name.",
"7: 'y' is already declared in member list.",
"9: Expect identifier.",
"13: 'FunctionStructCollision' is already declared in this scope.",
"18: Expect expression.",
])
})
})
describe("type checking", () => {
const expectResolveErrors = (source: string, expectedErrors: string[]) => expectErrors(source, expectedErrors, Passes.THROUGH_RESOLVE)
test("Operators", () => {
expectResolveErrors(`
var u = 1.0 == false;
var v = -true;
var w = !1.5; // err
var x = 1 + true;
var y = true / false;
var z = true > false;
// ok
var a = 1.0 == 0.5;
var b = -1;
var c = !false;
var d = 1 + 2;
var e = 1.0 / 2.0;
var f = 1.0 > 2.0;
var g = 5 % 2;
var h = 5 % -2;
var i = 5.0 % -2.0; // err
var j1 = [1.0] == [2.0]; // err
var j2 = [1.0] != [2.0]; // err
struct Point{ x float, y float }
var k1 = Point{1.0, 2.0} == Point{2.0, 3.0}; // err
var k2 = Point{1.0, 2.0} != Point{2.0, 3.0}; // err
`,
[
"1: Cannot compare float to bool.",
"2: Invalid operand type for unary operator '-'.",
"3: Cannot implicitly convert operand to 'bool'.",
"4: Invalid operand types for binary operator '+'.",
"5: Invalid operand types for binary operator '/'.",
"6: Invalid operand types for binary operator '>'.",
"17: Invalid operand types for binary operator '%'.",
"19: Cannot compare [float; 1] to [float; 1].",
"20: Cannot compare [float; 1] to [float; 1].",
"22: Cannot compare Point to Point.",
"23: Cannot compare Point to Point.",
])
})
test("Operators 2", () => {
expectResolveErrors(`
def main() {
var i = 1;
var f = 1.5;
var bt = 'c';
i += 1; // ok
i += 1.5; // error
i += true; // error
i -= 1; // ok
i -= 1.5; // error
i -= true; // error
i *= 1; // ok
i *= 1.5; // error
i *= true; // error
i /= 1; // ok
i /= 1.5; // error
i /= true; // error
i %= 1; // ok
i %= 1.5; // error
i %= true; // error
f += 1; // ok
f += 1.5; // ok
f += true; // error
f -= 1; // ok
f -= 1.5; // ok
f -= true; // error
f *= 1; // ok
f *= 1.5; // ok
f *= true; // error
f /= 1; // ok
f /= 1.5; // ok
f /= true; // error
f %= 1; // error
f %= 1.5; // error
f %= true; // error
bt += byte(1); // ok
bt += 1.5; // error
bt += true; // error
bt -= byte(1); // ok
bt -= 1.5; // error
bt -= true; // error
bt *= byte(1); // ok
bt *= 1.5; // error
bt *= true; // error
bt /= byte(1); // ok
bt /= 1.5; // error
bt /= true; // error
bt %= byte(1); // ok
bt %= 1.5; // error
bt %= true; // error
}
`,
[
"6: Cannot implicitly convert operand to 'int'.",
"7: Invalid operand types for binary operator '+'.",
"9: Cannot implicitly convert operand to 'int'.",
"10: Invalid operand types for binary operator '-'.",
"12: Cannot implicitly convert operand to 'int'.",
"13: Invalid operand types for binary operator '*'.",
"15: Cannot implicitly convert operand to 'int'.",
"16: Invalid operand types for binary operator '/'.",
"18: Invalid operand types for binary operator '%'.",
"19: Invalid operand types for binary operator '%'.",
"22: Invalid operand types for binary operator '+'.",
"25: Invalid operand types for binary operator '-'.",
"28: Invalid operand types for binary operator '*'.",
"31: Invalid operand types for binary operator '/'.",
"32: Invalid operand types for binary operator '%'.",
"33: Invalid operand types for binary operator '%'.",
"34: Invalid operand types for binary operator '%'.",
"36: Cannot implicitly convert operand to 'byte'.",
"37: Invalid operand types for binary operator '+'.",
"39: Cannot implicitly convert operand to 'byte'.",
"40: Invalid operand types for binary operator '-'.",
"42: Cannot implicitly convert operand to 'byte'.",
"43: Invalid operand types for binary operator '*'.",
"45: Cannot implicitly convert operand to 'byte'.",
"46: Invalid operand types for binary operator '/'.",
"48: Invalid operand types for binary operator '%'.",
"49: Invalid operand types for binary operator '%'.",
])
})
test("Variable type annotation", () => {
expectResolveErrors(`
def returnsInt() int {
return 42;
}
var x int = true;
var y bool = 5.0;
var z bool = returnsInt();
`,
[
"4: Cannot assign value of type 'bool' to variable of type 'int'.",
"5: Cannot assign value of type 'float' to variable of type 'bool'.",
"6: Cannot assign value of type 'int' to variable of type 'bool'."
])
})
test("Type inference from initializer", () => {
expectResolveErrors(`
var x = 5;
var y int = x;
def foo() int {
return 1;
}
var z = foo();
var p = z;
def bar(x int) {
print x;
}
def main() {
bar(z);
bar(p);
}
`,
[/* no errors*/])
})
test("Parameter mismatch", () => {
expectResolveErrors(`
def foo(x int, y int) {}
def main() {
foo(1, 2); // ok
foo(1);
foo(true, false);
}
`,
[
"4: Expected 2 arguments but got 1 in call to foo.",
"5: Cannot implicitly convert operand to 'int'.",
"5: Cannot implicitly convert operand to 'int'.",
])
})
test("Non-callable symbol", () => {
expectResolveErrors(`
var foo = 1;
def main() {
foo();
bar();
}
`,
[
"3: Cannot call this type.",
"4: Undefined symbol 'bar'."
])
})
test("Return type mismatch", () => {
expectResolveErrors(`
def foo() {
return 1;
}
def bar(x bool) int {
if (x) {
return 1;
}
return false;
}
def returnsFloat() float { return 5.0; }
def foobar(x bool) bool {
if (x) {
return;
}
return returnsFloat();
}
`,
[
"2: Expected a value of type 'void'.",
"8: Expected a value of type 'int'.",
"13: Expected a value of type 'bool'.",
"15: Expected a value of type 'bool'."
])
})
test("Missing return", () => {
expectResolveErrors(`
def foo() {
print 1337;
// ok to not return
}
def foo2() int {
print 1337;
// missing return
}
def bar(x bool) int {
if (x) {
return 1;
} else {
return 0;
}
// all paths satisfied
}
def bar2(x bool, y bool) int {
if (x && y) {
return 1;
} else if (!x && !y) {
return 2;
}
// missing return
}
def bar3(x bool, y bool) int {
if (x && y) {
return 1;
}
print x;
if (x) {
return 2;
} else {
return 3;
}
// all paths satisfied
}
`,
[
"5: All control paths for foo2 must return a value of type 'int'.",
"17: All control paths for bar2 must return a value of type 'int'."
])
})
test("Return from void function", () => {
expectResolveErrors(`
def foobar() {
return 1; // error
}
def foo() {
print 1234;
return;
}
def bar(x int) {
if (x) {
return;
}
foo();
}
`,
[
"2: Expected a value of type 'void'."
])
})
test("Global scope", () => {
expectResolveErrors(`
// ok for dependent globals to be declared out-of-order
var x = y;
var y = 1;
// undeclared symbol is still an error
var z1 = missing;
var z2 = x * (y - missing);
// missing global is reported in function body
def foo() int {
missing = 5; // implicit declaration not allowed
return missing;
}
var z3 = foo();
`,
[
"5: Undefined symbol 'missing'.",
"6: Undefined symbol 'missing'.",
"6: Invalid operand types for binary operator '-'.",
"9: Undefined symbol 'missing'.",
"10: Undefined symbol 'missing'.",
"10: Expected a value of type 'int'.",
])
})
test("Function scope", () => {
expectResolveErrors(`
def inner(outerAndInnerParam bool, innerParam int) {
var inner1 = 1;
var outerAndInner = true;
// cannot refer to vars declared only inside outer
print outerBeforeInner;
print outerParam;
// ok
print inner1;
// wrong type
print outerAndInner == 1.5;
print outerAndInnerParam == 0.99;
// ok
print outerAndInner == true;
print outerAndInnerParam == true;
}
def outer(outerAndInnerParam float, outerParam int) {
var outerAndInner = 1.5;
var outerBeforeInner = 1;
inner(true, 2);
// cannot refer to vars declared only inside inner
print inner1;
print innerParam;
// ok
print outerBeforeInner;
// ok
var outerAfterInner = 2;
print outerAfterInner;
// ok
print outerAndInner == 1.5;
print outerAndInnerParam == 0.99;
// wrong type
print outerAndInner == true;
print outerAndInnerParam == true;
}
`,
[
"5: Undefined symbol 'outerBeforeInner'.",
"6: Undefined symbol 'outerParam'.",
"10: Cannot compare bool to float.",
"11: Cannot compare bool to float.",
"21: Undefined symbol 'inner1'.",
"22: Undefined symbol 'innerParam'.",
"32: Cannot compare float to bool.",
"33: Cannot compare float to bool."
])
})
test("Block scope", () => {
expectResolveErrors(`
def main() {
var a = 5.0;
var b = 2.0;
print a == true; // wrong type
print a == 5.0; // ok
print x; // undefined symbol
{
var a = true;
var x = b;
print a == true; // ok
print a == 5.0; // wrong type
print x; // ok
print x == b; // ok
print x == 5.0; // ok
print x == a; // wrong type
}
print a == true; // wrong type
print a == 5.0; // ok
print x; // undefined symbol
}
`,
[
"4: Cannot compare float to bool.",
"6: Undefined symbol 'x'.",
"11: Cannot compare bool to float.",
"15: Cannot compare float to bool.",
"17: Cannot compare float to bool.",
"19: Undefined symbol 'x'.",
])
})
test("Lexical scope", () => {
expectResolveErrors(`
def main() {
var a = 1;
var b = c; // error to reference locals out-of-order
var c = a; // ok
{
var x = a; // ok
var y = d; // error
var z = x; // ok
}
var d = c; // ok
{
var p = x; // error
var q = d; // ok
var r = c; // ok
}
}
`,
[
"3: Undefined symbol 'c'.",
"7: Undefined symbol 'd'.",
"12: Undefined symbol 'x'."
])
})
test("Lexical scope 2", () => {
expectResolveErrors(`
def main() {
var a = 1.5;
{
print a == true; // error
var a = true;
print a == true; // ok
}
}
`,
[
"4: Cannot compare float to bool."
])
})
test("For loop scope", () => {
expectResolveErrors(`
def main() {
{
var i = 1337;
for (var i = 0; i < 3; i = i + 1) {
print i;
}
}
for (var i = 0; i < 3; i = i + 1) {
print i;
}
print i; // error
}
`,
[
"11: Undefined symbol 'i'."
])
})
test("Scope change due to out-of-order resolution", () => {
expectResolveErrors(`
def foo(y int) int {
var localAndGlobal = true;
var local = 1;
var x = globalVar1 + y;
var z = globalVar2;
if (globalVar3) {
return x + y;
}
return x + z;
}
var globalVar1 = int(y);
var globalVar2 = int(local);
var globalVar3 = localAndGlobal == 3.14;
var localAndGlobal = 3.14;
`,
[
"11: Undefined symbol 'y'.",
"12: Undefined symbol 'local'."
])
})
test("Cyclic variable declaration", () => {
expectResolveErrors(`
var x = x;
var y = z;
var z = y;
var a = foo();
def foo() int {
return bar();
}
def bar() int {
return int(b);
}
var b = int(a);
var p = foobar();
def foobar() int {
return int(q);
}
var q = foobar();
def getGlobalG() int {
return g;
}
var g = 1337;
def main() {
var g = getGlobalG(); // ok
}
`,
[
"1: Declaration of 'x' is cyclic. Defined here:\n" +
"var x = x;\n" +
" ^",
"2: Declaration of 'y' is cyclic. Defined here:\n" +
" var y = z;\n" +
" ^",
"4: Declaration of 'a' is cyclic. Defined here:\n" +
" var a = foo();\n" +
" ^",
"16: Declaration of 'q' is cyclic. Defined here:\n" +
" var q = foobar();\n" +
" ^"
])
})
test("Cyclic variable declaration inside block", () => {
expectResolveErrors(`
def main() {
var a = 1;
{
var a = a; // --> error!
}
}
`,
[
"4: Declaration of 'a' is cyclic. Defined here:\n" +
" var a = a; // --> error!\n" +
" ^"
])
})
test("Recursive functions", () => {
expectResolveErrors(`
def fib(n int) int {
if (n <= 1) {
return 1;
}
return fib(n-1) + fib(n-2);
}
def isEven(n int) bool {
if (n == 0) {
return true;
}
return !isOdd(n-1);
}
def isOdd(n int) bool {
if (n == 1) {
return true;
}
return !isEven(n-1);
}
`,
[/* no errors */])
})
test("Cyclic variable declaration with mutually recursive functions", () => {
expectResolveErrors(`
def foo(p int) int {
if (p == 0) {
return x;
}
return bar(p-1);
}
var x = int(bar(1));
def bar(p int) int {