summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/Chapter17Test.java
blob: 24daa1645e9a407f108f01962b6270a4212ddea3 (plain)
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
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class Chapter17Test {

    @Test
    public void testJig() {
        CodeGen code = new CodeGen(
"""
return 1;
""");
        code.parse().opto().typeCheck();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code,  0));
    }


    // ---------------------------------------------------------------
    @Test
    public void testInc0() {
        CodeGen code = new CodeGen("""
return arg++;
""");
        code.parse().opto();
        assertEquals("return arg;", code.print());
        assertEquals("0", Eval2.eval(code, 0));
    }

    @Test
    public void testInc1() {
        CodeGen code = new CodeGen("""
return arg+++arg++;
""");
        code.parse().opto();
        assertEquals("return ((arg<<1)+1);", code.print());
        assertEquals("1", Eval2.eval(code, 0));
    }

    @Test
    public void testInc2() {
        CodeGen code = new CodeGen("""
//   -(arg--)-(arg--)
return -arg---arg--;
""");
        code.parse().opto();
        assertEquals("return (-((arg<<1)+-1));", code.print());
        assertEquals("1", Eval2.eval(code, 0));
    }

    @Test
    public void testInc3() {
        CodeGen code = new CodeGen("""
int[] !xs = new int[arg];
xs[0]++;
xs[1]++;
return xs[0];
""");
        code.parse().opto();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code, 2));
    }

    @Test
    public void testInc4() {
        CodeGen code = new CodeGen("""
u8[] !xs = new u8[1];
xs[0]--;
return xs[0];
""");
        code.parse().opto();
        assertEquals("return 255;", code.print());
        assertEquals("255", Eval2.eval(code, 2));
    }

    @Test
    public void testInc5() {
        CodeGen code = new CodeGen("""
struct _S { u16 x; };
_S !s = new _S;
s.x--;
return s.x;
""");
        code.parse().opto();
        assertEquals("return 65535;", code.print());
        assertEquals("65535", Eval2.eval(code, 2));
    }

    @Test
    public void testInc6() {
        CodeGen code = new CodeGen("return --arg;");
        code.parse().opto();
        assertEquals("return (arg+-1);", code.print());
        assertEquals("-1", Eval2.eval(code, 0));
    }

    @Test
    public void testInc7() {
        CodeGen code = new CodeGen("u8 x=0; return --x;");
        code.parse().opto();
        assertEquals("return 255;", code.print());
        assertEquals("255", Eval2.eval(code, 0));
    }

    @Test
    public void testInc8() {
        CodeGen code = new CodeGen("int x; x+=2; return x+=3;");
        code.parse().opto();
        assertEquals("return 5;", code.print());
        assertEquals("5", Eval2.eval(code, 0));
    }

    @Test
    public void testInc9() {
        CodeGen code = new CodeGen("flt x = 1.5; x++; return x;");
        code.parse().opto();
        assertEquals("return 2.5f;", code.print());
        assertEquals("2.5", Eval2.eval(code, 0));
    }

    @Test public void testInt10() {
        CodeGen code = new CodeGen("flt x = 1.5; x++; int i = x; return i;");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Type 2.5f is not of declared type i64",e.getMessage()); }
    }


    // ---------------------------------------------------------------
    @Test public void testVar0() {
        CodeGen code = new CodeGen("var d; return d;");
        try { code.parse().opto(); fail(); }
        catch( Exception e ) { assertEquals("Syntax error, expected `=expression` but found `;`",e.getMessage()); }
    }

    @Test public void testVar1() {
        CodeGen code = new CodeGen("val d; return d;");
        try { code.parse().opto(); fail(); }
        catch( Exception e ) { assertEquals("Syntax error, expected `=expression` but found `;`",e.getMessage()); }
    }

    @Test public void testVar2() {
        CodeGen code = new CodeGen("int x; x=3; x++; return x; // Ok, no initializer so x is mutable ");
        code.parse().opto();
        assertEquals("return 4;", code.print());
        assertEquals("4", Eval2.eval(code, 0));
    }

    @Test public void testVar3() {
        CodeGen code = new CodeGen("int x=3; x++; return x; // Ok, primitive so x is mutable despite initializer ");
        code.parse().opto();
        assertEquals("return 4;", code.print());
        assertEquals("4", Eval2.eval(code, 0));
    }

    @Test public void testVar4() {
        CodeGen code = new CodeGen("struct _S{int x;}; _S? s; s=new _S; s.x++; return s.x; // Ok, no initializer so x is mutable ");
        code.parse().opto();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code, 0));
    }

    @Test public void testVar5() {
        CodeGen code = new CodeGen("struct _S{int x; new _S={ int xx -> x=xx; };}; _S? s; s=new _S(3); s.x++; return s.x; // Ok, no initializer so x is mutable ");
        code.parse().opto();
        assertEquals("return 4;", code.print());
        assertEquals("4", Eval2.eval(code, 0));
    }

    @Test public void testVar6() {
        CodeGen code = new CodeGen("struct S{int x;}; val s = new S; s.x++; return s.x; // Error initializer so x is immutable ");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
    }

    @Test public void testVar7() {
        CodeGen code = new CodeGen("struct S{int x; new S={ int xx -> x=xx; };}; val s = new S(3); s.x++; return s.x; // Error initializer so x is immutable ");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
    }

    @Test public void testVar8() {
        CodeGen code = new CodeGen("struct _S{int x;}; _S !s=new _S; s.x++; return s.x; // Ok, has '!' so s.x is mutable ");
        code.parse().opto();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code, 0));
    }

    @Test public void testVar9() {
        CodeGen code = new CodeGen("struct _S{int x;}; var s=new _S; s.x++; return s.x; // Ok, has var so s.x is mutable ");
        code.parse().opto();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code, 0));
    }

    @Test public void testVar10() {
        CodeGen code = new CodeGen("struct S{int x;}; val s=new S; s.x++; return s.x; // Error, has val so x is immutable ");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
    }

    @Test public void testVar11() {
        CodeGen code = new CodeGen("""
struct Bar { int x; };
Bar !bar = new Bar;
bar.x = 3; // Ok, bar is mutable

struct Foo { Bar? !bar; int y; new Foo = { Bar? b -> bar = b; }; };
Foo !foo = new Foo(bar);
foo.bar = bar; // Ok foo is mutable
foo.bar.x++;   // Ok foo and foo.bar and foo.bar.x are all mutable

val xfoo = foo; // Throw away mutability
xfoo.bar.x++;   // Error, cannot mutate through xfoo
return xfoo.bar.x;
 """);
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
    }

    @Test public void testVar12() {
        CodeGen code = new CodeGen("""
struct _Bar { int x; };
_Bar !bar = new _Bar;
bar.x = 3; // Ok, bar is mutable

struct _Foo { _Bar? !bar; int y; };
_Foo !foo = new _Foo;
foo.bar = bar; // Ok bar is mutable
foo.bar.x++;   // Ok foo and foo.bar and foo.bar.x are all mutable

val xfoo = foo;        // Throw away mutability
int x4 = xfoo.bar.x;   // Ok to read through xfoo, gets 4
foo.bar.x++;           // Bumps to 5
int x5 = xfoo.bar.x;   // Ok to read through xfoo, gets 5
return x4*10+x5;
""");
        code.parse().opto();
        assertEquals("return 45;", code.print());
        assertEquals("45", Eval2.eval(code, 0));
    }

    @Test
    public void testVar13() {
        CodeGen code = new CodeGen("""
int i,i++;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Redefining name 'i'",e.getMessage()); }
    }


    @Test
    public void testVar14() {
        CodeGen code = new CodeGen("""
struct B {};
struct A { B b; new A = { ->
    return b; // read before init
    b = new B;
}; };
A x = new A;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Field 'b' might not be initialized",e.getMessage()); }
    }

    @Test
    public void testVar15() {
        CodeGen code = new CodeGen("""
struct B {};
struct A { B b; new A = { ->
    if (arg) b = new B; // Constructor ends with partial init of b
}; };
return (new A).b;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("'Test.A' is not fully initialized, field 'b' is only partially set in the constructor",e.getMessage()); }
    }

    @Test
    public void testVar16() {
        CodeGen code = new CodeGen(
"""
struct S{ new S = { -> x = 2; }; };
val x = 1;
val s = new S;
return x;
"""
);
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Undefined name 'x'",e.getMessage()); }
    }

    @Test
    public void testVar17() {
        CodeGen code = new CodeGen(
"""
struct Person {
    bool focused;
};
val focus = { Person p ->
    p.focused = true;
};
Person me; // Person !me = new Person;
focus(me);
return me;
"""
);
        try { code.parse(); fail(); }
        catch( Exception e ) { assertEquals("Syntax error, expected `=expression` but found `;`",e.getMessage()); }
    }

    // ---------------------------------------------------------------
    @Test
    public void testTrinary0() {
        CodeGen code = new CodeGen("""
return arg ? 1 : 2;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,1,2);", code.print());
        assertEquals("2", Eval2.eval(code, 0));
        assertEquals("1", Eval2.eval(code, 1));
    }

    @Test
    public void testTrinary1() {
        CodeGen code = new CodeGen("""
return arg ? 0 : arg;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
        assertEquals("0", Eval2.eval(code, 0));
    }

    @Test
    public void testTrinary2() {
        CodeGen code = new CodeGen("""
struct _Bar { int x; };
var b = arg ? new _Bar : null;
return b ? b.x++ + b.x++ : -1;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,((.x<<1)+1),-1);", code.print());
        assertEquals("-1", Eval2.eval(code, 0));

                                   assertEquals("1", Eval2.eval(code, 1));
    }

    @Test
    public void testTrinary3() {
        CodeGen code = new CodeGen("""
struct _Bar { int x; };
var b = arg ? new _Bar;
return b ? b.x++ + b.x++ : -1;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,((.x<<1)+1),-1);", code.print());
        assertEquals("-1", Eval2.eval(code, 0));
        assertEquals("1", Eval2.eval(code, 1));
    }

    @Test
    public void testTrinary4() {
        // This test case will benefit from an unzipping transformation
        CodeGen code = new CodeGen("""
struct _Bar { _Bar? next; int x; new _Bar = { _Bar? n, int xx -> next=n; x=xx; }; };
var b = arg ? new _Bar((arg==2) ? new _Bar(null,2), 1);
return b ? b.next ? b.next.x : b.x; // parses "b ? (b.next ? b.next.x : b.x) : 0"
""");
        code.parse().opto();
        assertEquals("return Phi(Region,.x,0,.x);", code.print());
        assertEquals("0", Eval2.eval(code, 0));
        assertEquals("1", Eval2.eval(code, 1));
        assertEquals("2", Eval2.eval(code, 2));
    }

    @Test
    public void testTrinary5() {
        CodeGen code = new CodeGen("""
flt f=arg?1:1.2;
return f;   // missing widening
""");
        code.parse().opto();
        assertEquals("return Phi(Region,1.0f,1.2);", code.print());
        assertEquals("1.2", Eval2.eval(code,  0));
    }


    @Test
    public void testTrinary6() {
        CodeGen code = new CodeGen("""
struct S{};
return arg ? 7 : new S;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("No common type amongst 7 and *Test.S {}",e.getMessage()); }
    }

    // ---------------------------------------------------------------
    @Test
    public void testFor0() {
        CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; i<arg; i++ )
    sum += i;
return sum;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code.print());
        assertEquals("3", Eval2.eval(code, 3));
        assertEquals("45", Eval2.eval(code, 10));
    }
    @Test
    public void testFor1() {
        CodeGen code = new CodeGen("""
int sum=0, i=0;
for( ; i<arg; i++ )
    sum += i;
return sum;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code.print());
        assertEquals("3", Eval2.eval(code, 3));
        assertEquals("45", Eval2.eval(code, 10));
    }
    @Test
    public void testFor2() {
        CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; ; i++ ) {
    if( i>=arg ) break;
    sum += i;
}
return sum;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code.print());
        assertEquals("3", Eval2.eval(code, 3));
        assertEquals("45", Eval2.eval(code, 10));
    }
    @Test
    public void testFor3() {
        CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; i<arg; )
    sum += i++;
return sum;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code.print());
        assertEquals("3", Eval2.eval(code, 3));
        assertEquals("45", Eval2.eval(code, 10));
    }
    @Test
    public void testFor4() {
        CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; i<arg; i++ )
    sum += i;
return i;
""");
        try { code.parse().opto(); fail(); }
        catch( Exception e ) { assertEquals("Undefined name 'i'",e.getMessage()); }
    }
    @Test
    public void testFor5() {
        CodeGen code = new CodeGen("""
for(;;arg++;) {}
""");
        try { code.parse().opto(); fail(); }
        catch( Exception e ) { assertEquals("Syntax error, expected `Unexpected code after expression` but found `;`",e.getMessage()); }
    }



    // ---------------------------------------------------------------
    @Test
    public void testForward0() {
        CodeGen code = new CodeGen("""
struct _A {
    B? f1;
    B? f2;
};
return new _A;
""");
        code.parse().opto();
        assertEquals("return Test._A;", code.print());
        assertEquals("Test._A{f1=null,f2=null}", Eval2.eval(code,0));
    }

    @Test
    public void testForward1() {
        CodeGen code = new CodeGen("""
struct _A {
    B?[]? nil_array_of_b;
    B?[]      array_of_b;
    new _A = { B?[] bs -> array_of_b = bs; };
};
return new _A(new B?[0]).array_of_b;
""");
        code.parse().opto();
        assertEquals("return []*B?;", code.print());
        assertEquals("*B {}?[]", Eval2.eval(code,0));
    }

    // ---------------------------------------------------------------
    @Test
    public void testLinkedList2() {
        CodeGen code = new CodeGen("""
struct _LLI { _LLI? next; int i; new _LLI = { _LLI? n, int ii -> next=n; i=ii; }; };
_LLI? !head = null;
while( arg-- )
    head = new _LLI(head,arg);
int sum=0;
var ptr = head; // A read-only ptr, to be assigned from read-only next fields
for( ; ptr; ptr = ptr.next )
    sum += ptr.i;
return sum;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,0,(Phi_sum+.i));", code.print());
        assertEquals("45", Eval2.eval(code, 10));
    }

    @Test
    public void sieveOfEratosthenes() {
        CodeGen code = new CodeGen(
"""
var ary = new bool[arg], primes = new int[arg];
var nprimes=0, p=0;
// Find primes while p^2 < arg
for( p=2; p*p < arg; p++ ) {
    // skip marked non-primes
    while( ary[p] ) p++;
    // p is now a prime
    primes[nprimes++] = p;
    // Mark out the rest non-primes
    for( int i = p + p; i < ary#; i += p )
        ary[i] = true;
}
// Now just collect the remaining primes, no more marking
for( ; p < arg; p++ )
    if( !ary[p] )
        primes[nprimes++] = p;
// Copy/shrink the result array
var !rez = new int[nprimes];
for( int j=0; j<nprimes; j++ )
    rez[j] = primes[j];
return rez;
""");
        code.parse().opto().typeCheck().GCM();
        assertEquals("return []i64;", code.print());
        assertEquals("i64[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20));
    }

}