diff options
| author | Cliff Click <cliffc@acm.org> | 2024-10-14 16:47:15 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-10-21 08:52:01 -0700 |
| commit | 3b2e5240eae6a0b7f29a1eb491591dcbd0e9c2d5 (patch) | |
| tree | 2448b1012a6276dd2efcfd630395a96a9a5d1407 /chapter16/src/test/java/com/seaofnodes | |
| parent | 94e557c9de68e7b3f5595d75e7330b63df76c323 (diff) | |
| download | Simple-3b2e5240eae6a0b7f29a1eb491591dcbd0e9c2d5.tar.gz Simple-3b2e5240eae6a0b7f29a1eb491591dcbd0e9c2d5.zip | |
Constructors
Missing arrays yet.
Allow constructor syntax in 2 parts; the type description and at the New.
must-init fields (final and not-null) have to be initialized in at least one of the two places;
finals must be initialized exactly once.
Any code is allowed in the constructors.
Diffstat (limited to 'chapter16/src/test/java/com/seaofnodes')
6 files changed, 64 insertions, 38 deletions
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java index ec6a0bb4..0b3e4110 100644 --- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java +++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java @@ -103,7 +103,7 @@ while (arg) { return bar.a; """); StopNode stop = parser.parse(false).iterate(); - assertEquals("return Phi(Loop14,0,(Phi_a+2));", stop.toString()); + assertEquals("return Phi(Loop15,0,(Phi_a+2));", stop.toString()); } @Test @@ -155,7 +155,7 @@ if( bar ) bar.a = 1; return bar; """); StopNode stop = parser.parse(false).iterate(); - assertEquals("return Phi(Region34,(*void)Phi(Region21,null,Bar),null);", stop.toString()); + assertEquals("return Phi(Region35,(*void)Phi(Region22,null,Bar),null);", stop.toString()); } @Test @@ -171,7 +171,7 @@ else bar.a = 1; return rez; """); StopNode stop = parser.parse(false).iterate(); - assertEquals("return Phi(Region40,4,3);", stop.toString()); + assertEquals("return Phi(Region41,4,3);", stop.toString()); } @Test @@ -225,12 +225,10 @@ while( i.x < i.len ) { return sum; """); StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Loop20,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString()); + assertEquals("return Phi(Loop21,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString()); } - - @Test public void test1() { Parser parser = new Parser(""" @@ -246,7 +244,7 @@ while(arg) { return ret; """); StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Loop14,s0,Phi(Region38,s0,Phi_ret));", stop.toString()); + assertEquals("return Phi(Loop15,s0,Phi(Region39,s0,Phi_ret));", stop.toString()); } @Test @@ -264,7 +262,7 @@ while(arg) { return ret; """); StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Loop18,s0,Phi(Region39,s0,Phi_ret));", stop.toString()); + assertEquals("return Phi(Loop19,s0,Phi(Region40,s0,Phi_ret));", stop.toString()); } @@ -281,7 +279,7 @@ while(arg < 10) { return ret; """); StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Loop14,s0,Phi(Region36,s0,Phi_ret));", stop.toString()); + assertEquals("return Phi(Loop15,s0,Phi(Region37,s0,Phi_ret));", stop.toString()); } @Test diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java index 4998d78f..90198098 100644 --- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java +++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java @@ -369,7 +369,7 @@ if (arg) { return i; """); StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Region41,.f,0);", stop.toString()); + assertEquals("return Phi(Region42,.f,0);", stop.toString()); } @Test @@ -476,7 +476,7 @@ while(arg) { return arg; """); StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Loop18,arg,Phi(Region43,.f,0));", stop.toString()); + assertEquals("return Phi(Loop19,arg,Phi(Region44,.f,0));", stop.toString()); } @Test diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java index a95a6155..750fe3d3 100644 --- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java +++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java @@ -95,33 +95,33 @@ return n.next; public void testNullRef1() { Parser parser = new Parser( """ -struct N { N next; int i; }; -N n = new N; -n.next = new N; +struct M { int m; }; +struct N { M next; int i; }; +N n = new N { next = new M; }; return n.next; """); StopNode stop = parser.parse().iterate(); - assertEquals("return N;", stop.toString()); + assertEquals("return M;", stop.toString()); } @Test public void testNullRef2() { Parser parser = new Parser( """ -struct N { N next; int i; }; -N n = new N; -n.next = null; +struct M { int m; }; +struct N { M next; int i; }; +N n = new N { next = null; } return n.next; """); try { parser.parse().iterate(); fail(); } - catch( Exception e ) { assertEquals("Cannot store null into field *N next",e.getMessage()); } + catch( Exception e ) { assertEquals("Type null is not of declared type *M",e.getMessage()); } } @Test public void testNullRef3() { Parser parser = new Parser( """ -struct N { N next; int i; }; +struct N { N? next; int i; }; N n = new N; n.i = 3.14; return n.i; @@ -169,7 +169,7 @@ return new S1.s=new S2; Parser parser = new Parser( """ struct I {int i;}; -struct P {I pi;}; +struct P { I? pi; }; P p1 = new P; P p2 = new P; p2.pi = new I; diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter14Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter14Test.java index 048cb93a..674196ee 100644 --- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter14Test.java +++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter14Test.java @@ -93,7 +93,7 @@ return b; public void testRefLoad() { Parser parser = new Parser( """ -struct Foo { u1 b; } +struct Foo { u1 b; }; Foo f = new Foo; f.b = 123; return f.b; diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java index 7adc5b10..9a56bc94 100644 --- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java +++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java @@ -5,6 +5,7 @@ import com.seaofnodes.simple.node.StopNode; import org.junit.Test; import org.junit.Ignore; +import java.awt.*; import java.nio.charset.StandardCharsets; import static org.junit.Assert.assertEquals; @@ -83,17 +84,53 @@ return y; assertEquals(9L, Evaluator.evaluate(stop, 1)); } + @Test + public void testConstruct0() { + Parser parser = new Parser(""" +struct X { int x=3; }; +X z = new X; +return z.x; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return 3;", stop.toString()); + assertEquals(3L, Evaluator.evaluate(stop, 0)); + } + + @Test + public void testConstruct1() { + Parser parser = new Parser(""" +struct X { int !x; }; +X z = new X { x=3; }; +return z.x; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return 3;", stop.toString()); + assertEquals(3L, Evaluator.evaluate(stop, 0)); + } + + @Test + public void testConstruct2() { + Parser parser = new Parser(""" +struct X { int x=3; }; +X z = new X { x = 4; }; +return z.x; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return 4;", stop.toString()); + assertEquals(4L, Evaluator.evaluate(stop, 0)); + } + @Test public void testStructFinal() { Parser parser = new Parser(""" -struct Point { int !x, !y; } -Point p = new Point { x=3; y=4; } +struct Point { int !x, !y; }; +Point p = new Point { x=3; y=4; }; return p; """); StopNode stop = parser.parse().iterate(); - assertEquals("return P;", stop.toString()); - assertEquals(3.14, Evaluator.evaluate(stop, 0)); + assertEquals("return Point;", stop.toString()); + assertEquals("Obj<Point>{x=3,y=4}", Evaluator.evaluate(stop, 0).toString()); } } diff --git a/chapter16/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter16/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java index 34921b94..fccec969 100644 --- a/chapter16/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java +++ b/chapter16/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java @@ -202,19 +202,10 @@ public class Evaluator { } num = type._fields.length-1; } else { - body = new Object[type._fields.length]; - num = type._fields.length; - } - for (int i=0; i<num; i++) { - var c = type._fields[i]._type; - if (c instanceof TypeInteger) { - body[i] = 0L; - } else if (c instanceof TypeFloat) { - body[i] = 0D; - } else { - assert c instanceof TypeMemPtr; - } + body = new Object[num = type._fields.length]; } + for (int i=0; i<num; i++) + body[i] = alloc.in(2+i+num)._type; Object[] mems = new Object[type._fields.length+2]; // mems[0] is control mems[1] = new Obj(type,body); // the ref |
