forked from norswap/sigh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticAnalysisTests.java
569 lines (459 loc) · 21.8 KB
/
SemanticAnalysisTests.java
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
import norswap.autumn.AutumnTestFixture;
import norswap.autumn.positions.LineMapString;
import norswap.sigh.SemanticAnalysis;
import norswap.sigh.SighGrammar;
import norswap.sigh.ast.SighNode;
import norswap.uranium.Reactor;
import norswap.uranium.UraniumTestFixture;
import norswap.utils.visitors.Walker;
import org.testng.annotations.Test;
/**
* NOTE(norswap): These tests were derived from the {@link InterpreterTests} and don't test anything
* more, but show how to idiomatically test semantic analysis. using {@link UraniumTestFixture}.
*/
public final class SemanticAnalysisTests extends UraniumTestFixture
{
// ---------------------------------------------------------------------------------------------
private final SighGrammar grammar = new SighGrammar();
private final AutumnTestFixture autumnFixture = new AutumnTestFixture();
{
autumnFixture.rule = grammar.root();
autumnFixture.runTwice = false;
autumnFixture.bottomClass = this.getClass();
}
private String input;
@Override protected Object parse (String input) {
this.input = input;
return autumnFixture.success(input).topValue();
}
@Override protected String astNodeToString (Object ast) {
LineMapString map = new LineMapString("<test>", input);
return ast.toString() + " (" + ((SighNode) ast).span.startString(map) + ")";
}
// ---------------------------------------------------------------------------------------------
@Override protected void configureSemanticAnalysis (Reactor reactor, Object ast) {
Walker<SighNode> walker = SemanticAnalysis.createWalker(reactor);
walker.walk(((SighNode) ast));
}
// ---------------------------------------------------------------------------------------------
@Test public void testLiteralsAndUnary() {
successInput("return 42");
successInput("return 42.0");
successInput("return \"hello\"");
successInput("return (42)");
successInput("return [1, 2, 3]");
successInput("return true");
successInput("return false");
successInput("return null");
successInput("return !false");
successInput("return !true");
successInput("return !!true");
}
// ---------------------------------------------------------------------------------------------
@Test public void testNumericBinary() {
successInput("return 1 + 2");
successInput("return 2 - 1");
successInput("return 2 * 3");
successInput("return 2 / 3");
successInput("return 3 / 2");
successInput("return 2 % 3");
successInput("return 3 % 2");
successInput("return 1.0 + 2.0");
successInput("return 2.0 - 1.0");
successInput("return 2.0 * 3.0");
successInput("return 2.0 / 3.0");
successInput("return 3.0 / 2.0");
successInput("return 2.0 % 3.0");
successInput("return 3.0 % 2.0");
successInput("return 1 + 2.0");
successInput("return 2 - 1.0");
successInput("return 2 * 3.0");
successInput("return 2 / 3.0");
successInput("return 3 / 2.0");
successInput("return 2 % 3.0");
successInput("return 3 % 2.0");
successInput("return 1.0 + 2");
successInput("return 2.0 - 1");
successInput("return 2.0 * 3");
successInput("return 2.0 / 3");
successInput("return 3.0 / 2");
successInput("return 2.0 % 3");
successInput("return 3.0 % 2");
failureInputWith("return 2 + true", "Trying to add Int with Bool");
failureInputWith("return true + 2", "Trying to add Bool with Int");
failureInputWith("return 2 + [1]", "Trying to add Int with Int[]");
failureInputWith("return [1] + 2", "Trying to add Int[] with Int");
}
// ---------------------------------------------------------------------------------------------
@Test public void testOtherBinary() {
successInput("return true && false");
successInput("return false && true");
successInput("return true && true");
successInput("return true || false");
successInput("return false || true");
successInput("return false || false");
failureInputWith("return false || 1",
"Attempting to perform binary logic on non-boolean type: Int");
failureInputWith("return 2 || true",
"Attempting to perform binary logic on non-boolean type: Int");
successInput("return 1 + \"a\"");
successInput("return \"a\" + 1");
successInput("return \"a\" + true");
successInput("return 1 == 1");
successInput("return 1 == 2");
successInput("return 1.0 == 1.0");
successInput("return 1.0 == 2.0");
successInput("return true == true");
successInput("return false == false");
successInput("return true == false");
successInput("return 1 == 1.0");
failureInputWith("return true == 1", "Trying to compare incomparable types Bool and Int");
failureInputWith("return 2 == false", "Trying to compare incomparable types Int and Bool");
successInput("return \"hi\" == \"hi\"");
successInput("return [1] == [1]");
successInput("return 1 != 1");
successInput("return 1 != 2");
successInput("return 1.0 != 1.0");
successInput("return 1.0 != 2.0");
successInput("return true != true");
successInput("return false != false");
successInput("return true != false");
successInput("return 1 != 1.0");
failureInputWith("return true != 1", "Trying to compare incomparable types Bool and Int");
failureInputWith("return 2 != false", "Trying to compare incomparable types Int and Bool");
successInput("return \"hi\" != \"hi\"");
successInput("return [1] != [1]");
successInput("return 3 is Int");
successInput("return \"hi\" is Float");
successInput("return 0.3465 is Void");
successInput("return 8 is Int");
successInput("return true is Bool");
successInput(
"struct P { var x: Int; var y: Int }" +
"var p: P = $P(1, 2);" +
"return p is P");
failureInput("return false is false");
}
// ---------------------------------------------------------------------------------------------
@Test public void testVarDecl() {
successInput("var x: Int = 1; return x");
successInput("var x: Float = 2.0; return x");
successInput("var x: Int = 0; return x = 3");
successInput("var x: String = \"0\"; return x = \"S\"");
failureInputWith("var x: Int = true", "expected Int but got Bool");
failureInputWith("return x + 1", "Could not resolve: x");
failureInputWith("return x + 1; var x: Int = 2", "Variable used before declaration: x");
// implicit conversions
successInput("var x: Float = 1 ; x = 2");
}
// ---------------------------------------------------------------------------------------------
@Test public void testRootAndBlock () {
successInput("return");
successInput("return 1");
successInput("return 1; return 2");
successInput("print(\"a\")");
successInput("print(\"a\" + 1)");
successInput("print(\"a\"); print(\"b\")");
successInput("{ print(\"a\"); print(\"b\") }");
successInput(
"var x: Int = 1;" +
"{ print(\"\" + x); var x: Int = 2; print(\"\" + x) }" +
"print(\"\" + x)");
}
// ---------------------------------------------------------------------------------------------
@Test public void testCalls() {
successInput(
"fun add (a: Int, b: Int): Int { return a + b } " +
"return add(4, 7)");
successInput(
"struct Point { var x: Int; var y: Int }" +
"return $Point(1, 2)");
successInput("var str: String = null; return print(str + 1)");
failureInputWith("return print(1)", "argument 0: expected String but got Int");
}
// ---------------------------------------------------------------------------------------------
@Test public void testArrayStructAccess() {
successInput("return [1][0]");
successInput("return [1.0][0]");
successInput("return [1, 2][1]");
failureInputWith("return [1][true]", "Indexing an array using a non-Int-valued expression");
// TODO make this legal?
// successInput("[].length", 0L);
successInput("return [1].length");
successInput("return [1, 2].length");
successInput("var array: Int[] = null; return array[0]");
successInput("var array: Int[] = null; return array.length");
successInput("var x: Int[] = [0, 1]; x[0] = 3; return x[0]");
successInput("var x: Int[] = []; x[0] = 3; return x[0]");
successInput("var x: Int[] = null; x[0] = 3");
successInput(
"struct P { var x: Int; var y: Int }" +
"return $P(1, 2).y");
successInput(
"struct P { var x: Int; var y: Int }" +
"var p: P = null;" +
"return p.y");
successInput(
"struct P { var x: Int; var y: Int }" +
"var p: P = $P(1, 2);" +
"p.y = 42;" +
"return p.y");
successInput(
"struct P { var x: Int; var y: Int }" +
"var p: P = null;" +
"p.y = 42");
failureInputWith(
"struct P { var x: Int; var y: Int }" +
"return $P(1, true)",
"argument 1: expected Int but got Bool");
failureInputWith(
"struct P { var x: Int; var y: Int }" +
"return $P(1, 2).z",
"Trying to access missing field z on struct P");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testIfWhile () {
successInput("if (true) return 1 else return 2");
successInput("if (false) return 1 else return 2");
successInput("if (false) return 1 else if (true) return 2 else return 3 ");
successInput("if (false) return 1 else if (false) return 2 else return 3 ");
successInput("var i: Int = 0; while (i < 3) { print(\"\" + i); i = i + 1 } ");
failureInputWith("if 1 return 1",
"If statement with a non-boolean condition of type: Int");
failureInputWith("while 1 return 1",
"While statement with a non-boolean condition of type: Int");
}
// ---------------------------------------------------------------------------------------------
@Test public void testInference() {
successInput("var array: Int[] = []");
successInput("var array: String[] = []");
successInput("fun use_array (array: Int[]) {} ; use_array([])");
}
// ---------------------------------------------------------------------------------------------
@Test public void testTypeAsValues() {
successInput("struct S{} ; return \"\"+ S");
successInput("struct S{} ; var type: Type = S ; return \"\"+ type");
}
// ---------------------------------------------------------------------------------------------
@Test public void testUnconditionalReturn()
{
successInput("fun f(): Int { if (true) return 1 else return 2 } ; return f()");
// TODO: would be nice if this pinpointed the if-statement as missing the return,
// not the whole function declaration
failureInputWith("fun f(): Int { if (true) return 1 } ; return f()",
"Missing return in function");
}
// ---------------------------------------------------------------------------------------------
@Test public void testSwitches() {
successInput(
"switch(1){ case 1 { print(\"i=1\") } "+
"case 2{ print(\"i=2\") } }"
);
successInput(
"var str : String = \"Hello\""+
"switch(str){"+
"case \"Hi\"{ print(\"Hi\") } }"
);
successInput(
"var num : Int = 3"+
"switch(num){"+
"case 3 { print(\"3\") }"+
"case 3 { print(\"3\") }"+
"default { print(\"3\") }"+
"case 3 { print(\"3\") } }"
);
successInput(
"struct P { var x: Int; var y: Int }" +
"var p: P = $P(1, 2)" +
"switch(p){"+
"case (1,1) { print(\"1 and 1\") }"+
"case (_,3) { print(\"? and 3\") }"+
"case (1,_) { print(\"1 and ?\") }"+
"case (_,_) { print(\"this is default\") }"+
"default { print(\"other\") } }"
);
successInput(
"struct P { var x: Int; var y: String }" +
"var p: P = $P(1, \"b\")" +
"switch(p){"+
"case (1,\"a\") { print(\"1 and a\") }"+
"case (_,\"b\") { print(\"? and b\") }"+
"case (1,_) { print(\"1 and ?\") }"+
"case (_,_) { print(\"this is default\") }"+
"default { print(\"other\") } }"
);
failureInputWith(
"switch(1){ case 1 { print(\"i=1\") } "+
"default { print(\"else\") } "+
"default { print(\"something else\") } }",
"Too many default case, expected 0 or 1 but got 2"
);
failureInputWith(
"switch (1) { if (3 > 0) {return 3}}",
"Unexpected Statement in the switch block"
);
failureInputWith(
"switch(1){ case \"1\" { print(\"i=1\") } "+
"case 2{ print(\"i=2\") } }",
"Type of the case expression doesn't match with the type of the switch expression," +
" expected Int but got String"
);
failureInputWith(
"switch(3){"+
"case (1,1) { print(\"1 and 1\") }"+
"case (_,3) { print(\"? and 3\") }"+
"default { print(\"other\") } }",
"Type of the case expression doesn't match with the type of the switch expression," +
" expected Int but got StructMatching"
);
failureInputWith(
"struct P { var x: Int; var y: Int }" +
"var p: P = $P(1, 2)" +
"switch(p){"+
"case (_,\"3\") { print(\"? and 3\") }"+
"default { print(\"other\") } }",
"Type of the field number 1 doesn't match with the type of corresponding field of the switch structure," +
" expected Int but got String"
);
failureInputWith(
"struct P { var x: Int; var y: Int }" +
"var p: P = $P(1, 2)" +
"switch(p){"+
"case (_,3,_) { print(\"? and 3\") }"+
"default { print(\"other\") } }",
"Number of struct fields in case doesn't match with the number of fields of the switch structure," +
" expected 2 but got 3"
);
failureInputWith(
"struct P { var x: Int; var y: Int ; var z: Int}" +
"var p: P = $P(1, 2, 3)" +
"switch(p){"+
"case (_,3) { print(\"? and 3\") }"+
"default { print(\"other\") } }",
"Number of struct fields in case doesn't match with the number of fields of the switch structure," +
" expected 3 but got 2"
);
failureInputWith(
"struct P { var x: Int}" +
"var p: P = $P(1)" +
"switch(p){"+
"case () { print(\"1\") }"+
"default { print(\"other\") } }",
"Number of struct fields in case doesn't match with the number of fields of the switch structure," +
" expected 1 but got 0"
);
}
// ---------------------------------------------------------------------------------------------
@Test public void testClosures() {
successInput("fun f() : (Int, Int[]) -> Int {" +
"return { (index, array) in " +
"return array[index]}}" +
"var indexer : (Int, Int[]) -> Int = f()" +
"indexer(1,[1,2,3,4,5])");
successInput("fun f(x : Float) : (String) -> Void {" +
"return { (str) in " +
"print(str)" +
"}}" +
"var printer : (String) -> Void = f(3.2)" +
"printer(\"Hello\")");
successInput("fun f(x : Float, closure : (Float) -> Bool) : Float {" +
"if (closure(x)) " +
"return x " +
"else " +
"return 2" +
"}" +
"var result : Float = f(3,{ (x) in " +
"return x>3.6" +
"})");
successInput("fun f(str : String) : (Int) -> String {" +
"var counter : Int = 0" +
"return {(num) in " +
"counter = counter + num " +
"return str+num}}");
failureInputWith("fun add ( num : Int ) : (Int, Int) -> Int {"+
"var base : Int = 42 "+
"return { (x) in " +
"return x}}","wrong number of arguments, expected 2 but got 1",
"Incompatible return type, expected (Int,Int) -> Int but got (1 parameters) -> Int"
);
failureInputWith("fun add ( num : Int ) : (Int, String) -> Int {"+
"var base : Int = 42 "+
"return { (x,y) in " +
"return y}}","Incompatible return type, expected Int but got String");
failureInputWith("fun f(x : Float) : (String) -> Void {" +
"return { (str) in " +
"print(str)" +
"}}" +
"var printer : (String) -> Void = f(3.2)" +
"printer(3.6)","incompatible argument provided for argument 0: expected String but got Float");
failureInputWith("fun f(x : Float) : (String) -> Void {" +
"return { (str) in " +
"print(str)" +
"}}" +
"var printer : (String) -> Void = f(3.2)" +
"printer(\"Hello\",3.2)","wrong number of arguments, expected 1 but got 2");
}
// ---------------------------------------------------------------------------------------------
@Test public void testDeclOptional(){
successInput("var i:Int? = 2");
successInput("var i:Int?");
failureInputWith("var i:Int? = \"abc\"","incompatible initializer type provided for variable `i`: expected Int but got String");
}
@Test public void testAssignToOptional(){
successInput("var i:Int? = 2"+
"var sum: Int = 0"+
"i = sum + 3");
successInput("var test:String?"+
"test = \"abc\"");
failureInputWith("var test:String?"+
"test = 3","Trying to assign a value to a non-compatible lvalue (expected: String but got Int");
}
@Test public void testAssignOptionalToOptional(){
successInput("var i:Int? = 2"+
"var sum: Int? = i\n"+
"i = sum! + 3");
successInput("var test:String?"+
"var test2 : String? = test\n"+
"print(test2!)");
failureInputWith("var test:String?"+
"var i: Int? = test","incompatible initializer type provided for variable `i`: expected Optional(Int) but got Optional(String)");
}
@Test public void testUnwrapOptionalWithBang(){
successInput("var i:Int? = 2"+
"var sum: Int = 0"+
"sum = sum + i!");
failureInputWith("var test:String?"+
"test = \"zyx\""+
"var i: Int = 4"+
"i = test","Trying to assign a value to a non-compatible lvalue.");
failureInputWith("var test: String?"+
"test = \"zyx\""+
"var i:Int = 4"+
"i = test!","Trying to assign a value to a non-compatible lvalue.");
}
@Test public void testUnwrapOptionalWithIf(){
successInput("var i:Int? = 2"+
"if var iUnwrap:Int = i{}");
failureInputWith("var i:Int = 2"+
"if var iUnwrap:Int = i{}","impossible to unwrap non optional variable (got Int)","Need optional in this if statement condition to be unwrap but got Int");
successInput("var j: Int\n" +
"var i:Int? = 2"+
"if var iUnwrap:Int = i{j = iUnwrap}");
}
@Test public void testListComprehension(){
successInput("var list:Int[] = [1,2,3]\n"+"var test:Int[] = [ x for x:Int in list ]");
successInput("var list:Int[] = [1,2,3]\n"+"var test:Int[] = [ x for x:Int in list if x > 3]");
successInput("var test:Int[] = [ x for x:Int in [1,2,3] if x > 3]");
failureInputWith("var test:Int[] = [ 1 for x:String in \"A\" ]",
"Expression StringLiteral(\"A\") must be arrayType, actual : String");
failureInputWith("var list:String[] = [\"a\",\"b\",\"c\"]\n"+"var test:Int[] = [ x for x:Int in list ]",
"Incompatible type for local variable x, need String but actual Int");
failureInputWith("var list:String[] = [\"a\",\"b\",\"c\"]\n"+"var test:String[] = [ 1 for x:String in list ]",
"incompatible initializer type provided for variable `test`: expected String[] but got Int[]");
failureInputWith("var list:String = \"a\"\n"+"var test:Int[] = [ 1 for x:String in list ]",
"Expression Reference(list) must be arrayType, actual : String");
failureInputWith("var list:Int[] = [1]\n"+"var test:Int[] = [ x for x:Int in list if x+1]",
"Incompatible type for condition expression, need Bool but actual Int");
}
}