summaryrefslogtreecommitdiff
path: root/chapter17/src/test/java/com
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-11-13 13:41:18 -0800
committerCliff Click <cliffc@acm.org>2024-11-23 09:03:20 -0800
commit68cd11f31dd4ba7f089d1a15de5a36ec67043da6 (patch)
tree19adb6cee02232eba346c6aed2d56081556302f2 /chapter17/src/test/java/com
parent82fb275502f84e17228fc587385eeb76f195a09e (diff)
downloadSimple-68cd11f31dd4ba7f089d1a15de5a36ec67043da6.tar.gz
Simple-68cd11f31dd4ba7f089d1a15de5a36ec67043da6.zip
Final? pass of finals
Add a least-upper-bound to set Phis to their respective Top while preserving final fields. Requires a ReadOnly node to "cast-away-writable". Series of isFinal/makeRO in all Types. Changes tests to match r/w intent.
Diffstat (limited to 'chapter17/src/test/java/com')
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java4
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java32
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java22
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java18
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java2
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java36
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java12
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java160
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/SchedulerTest.java6
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/TypeTest.java4
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java1
11 files changed, 209 insertions, 88 deletions
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
index 4833483b..88d79215 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -15,7 +15,7 @@ public class BrainFuckTest {
@Test
public void testBrainfuck() {
var program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.".getBytes(StandardCharsets.UTF_8);
- var encoded = new StringBuilder("u8[] program = new u8[").append(program.length).append("];");
+ var encoded = new StringBuilder("u8[] !program = new u8[").append(program.length).append("];");
for (int i = 0; i < program.length; i++) {
int value = program[i] & 0xFF;
encoded.append("program[").append(i).append("] = ").append(value).append(";");
@@ -25,7 +25,7 @@ public class BrainFuckTest {
encoded + """
var d = 0;
u8[] !output = new u8[0];
-val data = new u8[100];
+u8[] !data = new u8[100];
for( int pc = 0; pc < program#; pc++ ) {
var command = program[pc];
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index 44d71b1e..d41c83d8 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -44,7 +44,7 @@ struct Foo {
int x;
};
Foo? foo = null;
-Bar bar = new Bar;
+Bar !bar = new Bar;
bar.a = 1;
bar.a = 2;
return bar.a;
@@ -57,7 +57,7 @@ return bar.a;
public void testExample() {
Parser parser = new Parser("""
struct Vector2D { int x; int y; };
-Vector2D v = new Vector2D;
+Vector2D !v = new Vector2D;
v.x = 1;
if (arg)
v.y = 2;
@@ -96,7 +96,7 @@ arg=0+new s0.0;
public void testLoop() {
Parser parser = new Parser("""
struct Bar { int a; };
-Bar bar = new Bar;
+Bar !bar = new Bar;
while (arg) {
bar.a = bar.a + 2;
arg = arg + 1;
@@ -111,7 +111,7 @@ return bar.a;
public void testIf() {
Parser parser = new Parser("""
struct Bar { int a; };
-Bar bar = new Bar;
+Bar !bar = new Bar;
if (arg) bar = null;
bar.a = 1;
return bar.a;
@@ -124,7 +124,7 @@ return bar.a;
public void testIf2() {
Parser parser = new Parser("""
struct Bar { int a; };
-Bar? bar = null;
+Bar? !bar = null;
if (arg) bar = new Bar;
bar.a = 1;
return bar.a;
@@ -150,7 +150,7 @@ return bar.a;
public void testIfOrNull() {
Parser parser = new Parser("""
struct Bar { int a; };
-Bar? bar = new Bar;
+Bar? !bar = new Bar;
if (arg) bar = null;
if( bar ) bar.a = 1;
return bar;
@@ -164,7 +164,7 @@ return bar;
Parser parser = new Parser(
"""
struct Bar { int a; };
-Bar? bar = new Bar;
+Bar? !bar = new Bar;
if (arg) bar = null;
int rez = 3;
if( !bar ) rez=4;
@@ -179,7 +179,7 @@ return rez;
public void testWhileWithNullInside() {
Parser parser = new Parser("""
struct s0 {int v0;};
-s0? v0 = new s0;
+s0? !v0 = new s0;
int ret = 0;
while(arg) {
ret = v0.v0;
@@ -216,7 +216,7 @@ struct Iter {
int x;
int len;
};
-Iter i = new Iter;
+Iter !i = new Iter;
i.len = arg;
int sum=0;
while( i.x < i.len ) {
@@ -234,9 +234,9 @@ return sum;
public void test1() {
Parser parser = new Parser("""
struct s0 {int v0;};
-s0 ret = new s0;
+s0 !ret = new s0;
while(arg) {
- s0 v0 = new s0;
+ s0 !v0 = new s0;
v0.v0 = arg;
arg = arg-1;
if (arg==5) ret=v0;
@@ -252,8 +252,8 @@ return ret;
public void test2() {
Parser parser = new Parser("""
struct s0 {int v0;};
-s0 ret = new s0;
-s0 v0 = new s0;
+s0 !ret = new s0;
+s0 !v0 = new s0;
while(arg) {
v0.v0 = arg;
arg = arg-1;
@@ -271,9 +271,9 @@ return ret;
public void test3() {
Parser parser = new Parser("""
struct s0 {int v0;};
-s0 ret = new s0;
+s0 !ret = new s0;
while(arg < 10) {
- s0 v0 = new s0;
+ s0 !v0 = new s0;
if (arg == 5) ret=v0;
arg = arg + 1;
}
@@ -350,7 +350,7 @@ s0 v1 = v0;
return v1;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return s0;", stop.toString());
+ assertEquals("return (const)s0;", stop.toString());
}
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index ddcc9e63..79c0622c 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -338,7 +338,7 @@ return primeCount;
Parser parser = new Parser(
"""
struct S { int f; };
-S v=new S;
+S !v=new S;
v.f = 2;
int i=new S.f;
i=v.f;
@@ -356,9 +356,9 @@ return i;
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
v.f = arg;
-S t = new S;
+S !t = new S;
int i = 0;
if (arg) {
if (arg+1) v = t;
@@ -377,7 +377,7 @@ return i;
Parser parser = new Parser(
"""
struct S { int f; };
-S v0 = new S;
+S !v0 = new S;
S? v1;
if (arg) v1 = new S;
if (v1) {
@@ -397,7 +397,7 @@ return v0;
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
v.f = arg;
S t = new S;
int i = v.f;
@@ -414,7 +414,7 @@ return i;
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
while(1) {
while(arg+1) { arg=arg-1; }
if (arg) break;
@@ -431,7 +431,7 @@ return v;
Parser parser = new Parser(
"""
struct s { int v; };
-s ptr=new s;
+s !ptr=new s;
while( -arg )
ptr = new s;
while(1)
@@ -446,7 +446,7 @@ while(1)
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
S t = new S;
int i = v.f;
while (arg) {
@@ -464,7 +464,7 @@ return i;
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
S t = new S;
while(arg) {
arg=arg-1;
@@ -484,7 +484,7 @@ return arg;
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
S t = new S;
if (arg) {
v.f=2;
@@ -502,7 +502,7 @@ return v;
Parser parser = new Parser(
"""
struct S { int f; };
-S v = new S;
+S !v = new S;
int i = arg;
while (arg > 0) {
int j = i/3;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index aa659147..46eac241 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -23,9 +23,9 @@ return 3.14;
Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
-LLI? head = null;
+LLI? !head = null;
while( arg ) {
- LLI x = new LLI;
+ LLI !x = new LLI;
x.next = head;
x.i = arg;
head = x;
@@ -42,9 +42,9 @@ return head.next.i;
Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
-LLI? head = null;
+LLI? !head = null;
while( arg ) {
- LLI x = new LLI;
+ LLI !x = new LLI;
x.next = head;
x.i = arg;
head = x;
@@ -66,9 +66,9 @@ return next.i;
"""
struct int0 { int i; flt0? f; };
struct flt0 { flt f; int0? i; };
-int0 i0 = new int0;
+int0 !i0 = new int0;
i0.i = 17;
-flt0 f0 = new flt0;
+flt0 !f0 = new flt0;
f0.f = 3.14;
i0.f = f0;
f0.i = i0;
@@ -100,7 +100,7 @@ N n = new N { next = new M; };
return n.next;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return M;", stop.toString());
+ assertEquals("return (const)M;", stop.toString());
}
@Test
@@ -121,7 +121,7 @@ return n.next;
Parser parser = new Parser(
"""
struct N { N? next; int i; };
-N n = new N;
+N !n = new N;
n.i = 3.14;
return n.i;
""");
@@ -169,7 +169,7 @@ return new S1.s=new S2;
"""
struct I {int i;};
struct P { I? pi; };
-P p1 = new P;
+P !p1 = new P;
P p2 = new P;
p2.pi = new I;
p2.pi.i = 2;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java
index 01cecebb..23c82358 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java
@@ -94,7 +94,7 @@ return b;
Parser parser = new Parser(
"""
struct Foo { u1 b; };
-Foo f = new Foo;
+Foo !f = new Foo;
f.b = 123;
return f.b;
""");
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index 04958819..4e2682dc 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java
@@ -25,7 +25,7 @@ return 3.14;
Parser parser = new Parser(
"""
struct C { C? l; };
-C c = new C;
+C !c = new C;
c.l = c;
return c;
""");
@@ -39,7 +39,7 @@ return c;
Parser parser = new Parser(
"""
u8[] old = new u8[0];
-u8[] output = new u8[1];
+u8[] !output = new u8[1];
int i = 0;
while (i < old#) {
output[i] = old[i];
@@ -82,7 +82,7 @@ return is[1];
public void testBasic3() {
Parser parser = new Parser(
"""
-int[] a = new int[2];
+int[] !a = new int[2];
a[0] = 1;
a[1] = 2;
return a[0];
@@ -97,7 +97,7 @@ return a[0];
Parser parser = new Parser(
"""
struct A { int i; };
-A?[] a = new A?[2];
+A?[] !a = new A?[2];
return a;
""");
StopNode stop = parser.parse().iterate();
@@ -111,10 +111,10 @@ return a;
"""
struct S { int x; flt y; };
// A new S
-S s = new S; s.x=99; s.y = 3.14;
+S !s = new S; s.x=99; s.y = 3.14;
// Double-d array of Ss. Fill in one row.
-S?[]?[] iss = new S?[]?[2];
+S?[]?[] !iss = new S?[]?[2];
iss[0] = new S?[7];
iss[0][2] = s;
@@ -135,14 +135,13 @@ return rez;
assertEquals(1.2 , Evaluator.evaluate(stop, 1));
}
- @Ignore
@Test
public void testBasic6() {
Parser parser = new Parser(
"""
struct S { int x; flt y; };
// A new S
-S s = new S; s.x=99; s.y = 3.14;
+S !s = new S; s.x=99; s.y = 3.14;
// Double-d array of Ss. Fill in one row.
S?[]?[] iss = new S?[]?[2];
@@ -156,9 +155,8 @@ if( iss[arg] )
rez = iss[arg][2].y;
return rez;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Region,.y,1.2),1.2);", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'y'",e.getMessage()); }
}
@Test
@@ -167,8 +165,8 @@ return rez;
"""
// Can we define a forward-reference array?
struct Tree { Tree?[]? _kids; };
-Tree root = new Tree;
-root._kids = new Tree?[2];
+Tree !root = new Tree;
+root._kids = new Tree?[2]; // NO BANG SO ARRAY IS OF IMMUTABLE TREES????
root._kids[0] = new Tree;
return root;
""");
@@ -192,7 +190,7 @@ return 0;
public void testRollingSum() {
Parser parser = new Parser(
"""
-int[] ary = new int[arg];
+int[] !ary = new int[arg];
// Fill [0,1,2,3,4,...]
int i=0;
while( i < ary# ) {
@@ -216,7 +214,7 @@ return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
public void sieveOEratosthenes() {
Parser parser = new Parser(
"""
-int[] ary = new int[arg], primes = new int[arg];
+int[] !ary = new int[arg], !primes = new int[arg];
int nprimes = 0, p=2;
// Find primes while p^2 < arg
while( p*p < arg ) {
@@ -240,7 +238,7 @@ while( p < arg ) {
p = p + 1;
}
// Copy/shrink the result array
-int[] rez = new int[nprimes];
+int[] !rez = new int[nprimes];
int j = 0;
while( j < nprimes ) {
rez[j] = primes[j];
@@ -251,7 +249,7 @@ return rez;
StopNode stop = parser.parse().iterate();
assertEquals("return [int];", stop.toString());
Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20);
- assertEquals("[int] { # :int; [] :int; }",obj.struct().toString());
+ assertEquals("[int] {int #; int ![]; }",obj.struct().toString());
long nprimes = (Long)obj.fields()[0];
long[] primes = new long[]{2,3,5,7,11,13,17,19};
for( int i=0; i<nprimes; i++ )
@@ -263,8 +261,8 @@ return rez;
Parser parser = new Parser(
"""
struct S {int i; flt f;};
-S s1 = new S;
-S s2 = new S;
+S !s1 = new S;
+S !s2 = new S;
s2.i = 3;
s2.f = 2.0;
if (arg) s1 = new S;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java
index d0e30a7c..533241a9 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java
@@ -114,7 +114,7 @@ Point p = new Point { x=3; y=4; };
return p;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Point;", stop.toString());
+ assertEquals("return (const)Point;", stop.toString());
assertEquals("Obj<Point>{x=3,y=4}", Evaluator.evaluate(stop, 0).toString());
}
@@ -127,7 +127,7 @@ p.x++;
return p;
""");
try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+ catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test
@@ -139,7 +139,7 @@ p.x++;
return p;
""");
try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+ catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test
@@ -170,7 +170,7 @@ return p;
public void testStructFinal5() {
Parser parser = new Parser("""
struct Point { var x=3; var y=4; };
-Point p = new Point;
+Point !p = new Point;
p.x++;
return p;
""");
@@ -186,7 +186,7 @@ return p;
Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
-LLI? head = null;
+LLI? !head = null;
while( arg ) {
head = new LLI { next=head; i=arg; };
arg = arg-1;
@@ -208,7 +208,7 @@ return next.i;
Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
-LLI? head = null;
+LLI? !head = null;
while( arg ) {
head = new LLI {
next=head;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
index 3773f87f..7de7d514 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
@@ -12,13 +12,24 @@ public class Chapter17Test {
@Test
public void testJig() {
Parser parser = new Parser("""
-return 3.14;
+i32 v0=0;
+if(1<<arg) return 0&0;
+ int v1=0;
+ while(0<<0!=1) {
+ v1=v0;
+ while(0<--v0)
+ if(1&v0) continue;
+ else arg=0;
+ if(v1) continue;
+ arg=0;
+ }
""");
StopNode stop = parser.parse().iterate();
assertEquals("return 3.14;", stop.toString());
assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
+ // ---------------------------------------------------------------
@Test
public void testInc0() {
Parser parser = new Parser("""
@@ -53,7 +64,7 @@ return -arg---arg--;
@Test
public void testInc3() {
Parser parser = new Parser("""
-int[] xs = new int[arg];
+int[] !xs = new int[arg];
xs[0]++;
xs[1]++;
return xs[0];
@@ -66,7 +77,7 @@ return xs[0];
@Test
public void testInc4() {
Parser parser = new Parser("""
-u8[] xs = new u8[1];
+u8[] !xs = new u8[1];
xs[0]--;
return xs[0];
""");
@@ -79,7 +90,7 @@ return xs[0];
public void testInc5() {
Parser parser = new Parser("""
struct S { u16 x; };
-S s = new S;
+S !s = new S;
s.x--;
return s.x;
""");
@@ -90,26 +101,134 @@ return s.x;
@Test
public void testInc6() {
- Parser parser = new Parser("""
-return --arg;
-""");
+ Parser parser = new Parser("return --arg;");
StopNode stop = parser.parse().iterate();
assertEquals("return (arg+-1);", stop.toString());
assertEquals(-1L, Evaluator.evaluate(stop, 0));
}
-
@Test
- public void testVar0() {
- Parser parser = new Parser("""
-var d;
-return d;
-""");
+ public void testInc7() {
+ Parser parser = new Parser("u8 x=0; return --x;");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 255;", stop.toString());
+ assertEquals(255L, Evaluator.evaluate(stop, 0));
+ }
+
+ // ---------------------------------------------------------------
+ @Test public void testVar0() {
+ Parser parser = new Parser("var d; return d;");
try { parser.parse().iterate(); fail(); }
catch( Exception e ) { assertEquals("Syntax error, expected expression: ;",e.getMessage()); }
}
+ @Test public void testVar1() {
+ Parser parser = new Parser("val d; return d;");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Syntax error, expected expression: ;",e.getMessage()); }
+ }
+
+ @Test public void testVar2() {
+ Parser parser = new Parser("int x; x=3; x++; return x; // Ok, no initializer so x is mutable ");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 4;", stop.toString());
+ assertEquals(4L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test public void testVar3() {
+ Parser parser = new Parser("int x=3; x++; return x; // Ok, primitive so x is mutable despite initializer ");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 4;", stop.toString());
+ assertEquals(4L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test public void testVar4() {
+ Parser parser = new Parser("struct S{int x;}; S s; s=new S; s.x++; return s.x; // Ok, no initializer so x is mutable ");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 1;", stop.toString());
+ assertEquals(1L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test public void testVar5() {
+ Parser parser = new Parser("struct S{int x;}; S s; s=new S{x=3;}; s.x++; return s.x; // Ok, no initializer so x is mutable ");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 4;", stop.toString());
+ assertEquals(4L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test public void testVar6() {
+ Parser parser = new Parser("struct S{int x;}; S s=new S; s.x++; return s.x; // Error initializer so x is immutable ");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
+ }
+
+ @Test public void testVar7() {
+ Parser parser = new Parser("struct S{int x;}; S s=new S{x=3;}; s.x++; return s.x; // Error initializer so x is immutable ");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
+ }
+
+ @Test public void testVar8() {
+ Parser parser = new Parser("struct S{int x;}; S !s=new S; s.x++; return s.x; // Ok, has '!' so s.x is mutable ");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 1;", stop.toString());
+ assertEquals(1L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test public void testVar9() {
+ Parser parser = new Parser("struct S{int x;}; var s=new S; s.x++; return s.x; // Ok, has var so s.x is mutable ");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 1;", stop.toString());
+ assertEquals(1L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test public void testVar10() {
+ Parser parser = new Parser("struct S{int x;}; val s=new S; s.x++; return s.x; // Error, has val so x is immutable ");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
+ }
+
+ @Test public void testVar11() {
+ Parser parser = new Parser("""
+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 { bar = 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
+ """);
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
+ }
+
+ @Test public void testVar12() {
+ Parser parser = new Parser("""
+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;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 45;", stop.toString());
+ assertEquals(45L, Evaluator.evaluate(stop, 0));
+ }
+ // ---------------------------------------------------------------
@Test
public void testTrinary0() {
Parser parser = new Parser("""
@@ -173,6 +292,7 @@ return b ? b.next ? b.next.x : b.x; // parses "b ? (b.next ? b.next.x : b.x) : 0
}
+ // ---------------------------------------------------------------
@Test
public void testFor0() {
Parser parser = new Parser("""
@@ -240,16 +360,18 @@ return i;
}
+ // ---------------------------------------------------------------
@Test
public void testLinkedList2() {
Parser parser = new Parser("""
struct LLI { LLI? next; int i; };
-LLI? head = null;
+LLI? !head = null;
while( arg-- )
head = new LLI { next=head; i=arg; };
int sum=0;
-for( ; head; head = head.next )
- sum = sum + head.i;
+var ptr = head; // A read-only ptr, to be assigned from read-only next fields
+for( ; ptr; ptr = ptr.next )
+ sum = sum + ptr.i;
return sum;
""");
StopNode stop = parser.parse().iterate();
@@ -261,7 +383,7 @@ return sum;
public void sieveOfEratosthenes() {
Parser parser = new Parser(
"""
-val ary = new bool[arg], primes = new int[arg];
+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++ ) {
@@ -278,7 +400,7 @@ for( ; p < arg; p++ )
if( !ary[p] )
primes[nprimes++] = p;
// Copy/shrink the result array
-var rez = new int[nprimes];
+var !rez = new int[nprimes];
for( int j=0; j<nprimes; j++ )
rez[j] = primes[j];
return rez;
@@ -286,7 +408,7 @@ return rez;
StopNode stop = parser.parse().iterate();
assertEquals("return [int];", stop.toString());
Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20);
- assertEquals("[int] { # :int; [] :int; }",obj.struct().toString());
+ assertEquals("[int] {int #; int ![]; }",obj.struct().toString());
long nprimes = (Long)obj.fields()[0];
long[] primes = new long[]{2,3,5,7,11,13,17,19};
for( int i=0; i<nprimes; i++ )
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/SchedulerTest.java b/chapter17/src/test/java/com/seaofnodes/simple/SchedulerTest.java
index 168e0ed7..c67217af 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/SchedulerTest.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/SchedulerTest.java
@@ -24,7 +24,7 @@ public class SchedulerTest {
struct S {
int f;
}
-S v=new S;
+S !v=new S;
v.f = 2;
int i=v.f;
if (arg) v.f=1;
@@ -42,7 +42,7 @@ return i;
struct S {
int f;
};
-S v0=new S;
+S !v0=new S;
if(arg) v0.f=1;
return v0;
""");
@@ -58,7 +58,7 @@ return v0;
struct S {
int f;
};
-S v=new S;
+S !v=new S;
v.f = 2;
int i=new S.f;
i=v.f;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter17/src/test/java/com/seaofnodes/simple/TypeTest.java
index 03b0e31d..1cb97cf6 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/TypeTest.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/TypeTest.java
@@ -20,7 +20,7 @@ public class TypeTest {
Field.make("b", TypeInteger.BOT,-4, false) });
Assert.assertEquals(s1, s1.glb());
Assert.assertNotEquals(s1, s1.dual());
- Assert.assertEquals(s1, s1.dual().glb());
+ Assert.assertEquals(s1.makeRO(), s1.dual().glb());
TypeMem m1 = TypeMem.make(1,TypeInteger.ZERO);
TypeMem m2 = TypeMem.make(2,TypeInteger.U16);
@@ -57,7 +57,7 @@ public class TypeTest {
Assert.assertEquals(ptr1nil, ptr1.glb());
Assert.assertEquals(ptr1, ptr1.dual().dual());
- Assert.assertEquals(ptr1.glb(), ptr1.dual().glb());
+ Assert.assertEquals(ptr1.glb().makeRO(), ptr1.dual().glb());
Assert.assertEquals(TypeMemPtr.make(TypeStruct.BOT,true), ptr1.meet(ptr2nil));
Assert.assertEquals(ptr1.glb(), ptr1.meet(TypeMemPtr.NULLPTR));
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter17/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
index 37d2a9b1..72f84fa2 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -308,6 +308,7 @@ public class Evaluator {
case CProjNode cproj -> ((Object[])val(cproj.ctrl()))[cproj._idx];
case ProjNode proj -> ((Object[])val( proj.in(0) ))[ proj._idx];
case ScopeMinNode mem -> null;
+ case ReadOnlyNode ro -> val(ro.in(1));
default -> throw new AssertionError("Unexpected node " + node);
};
}