summaryrefslogtreecommitdiff
path: root/chapter17/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-11-09 13:47:37 -0800
committerCliff Click <cliffc@acm.org>2024-11-23 09:03:20 -0800
commite386e6323e34f2a3d4e5b5779cdefd12c9552bd6 (patch)
tree3ed7fa23321abfc8b988862799f5bfdc7aaea7e5 /chapter17/src/test/java/com/seaofnodes
parentbaa84f5eb99cd21f6704e71515b6ec7bbd78624f (diff)
downloadSimple-e386e6323e34f2a3d4e5b5779cdefd12c9552bd6.tar.gz
Simple-e386e6323e34f2a3d4e5b5779cdefd12c9552bd6.zip
Change field mutability
Fields & locals with an initializer are immutable by default; those without are mutable. Make mutable with `!`. Fields are always mutable in a constructor. Add a trinary `pred ? then : else` syntax. SubNode retains as much precision as AddNode. Sharpen some asserts and error cases. Limit Load node cloning if the ptr is not obvious.
Diffstat (limited to 'chapter17/src/test/java/com/seaofnodes')
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java2
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter03Test.java4
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java4
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java2
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java91
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java66
6 files changed, 148 insertions, 21 deletions
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
index abb8b834..de6852c7 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -24,7 +24,7 @@ public class BrainFuckTest {
Parser parser = new Parser(
encoded + """
var pc = 0, d = 0;
-u8[] output = new u8[0];
+u8[] !output = new u8[0];
val data = new u8[100];
while (pc < program#) {
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter03Test.java
index c76df903..4525918c 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter03Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -31,7 +31,7 @@ public class Chapter03Test {
@Test
public void testVarScopeNoPeephole() {
- Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c; ");
+ Parser parser = new Parser("int a=1; int b=2; int !c=0; { int b=3; c=a+b; } return c; ");
Node._disablePeephole = true;
StopNode ret = parser.parse();
Node._disablePeephole = false;
@@ -58,7 +58,7 @@ public class Chapter03Test {
@Test
public void testBad1() {
try {
- new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse();
+ new Parser("int a=1; int b=2; int !c=0; { int b=3; c=a+b;").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error, expected }: ",e.getMessage());
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 70f38dd6..24612fee 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -96,14 +96,14 @@ public class Chapter04Test {
@Test
public void testBug1() {
- Parser parser = new Parser("int a=arg+1; int b=a; b=1; return a+2; ");
+ Parser parser = new Parser("int a=arg+1; int !b=a; b=1; return a+2; ");
StopNode ret = parser.parse();
assertEquals("return (arg+3);", ret.print());
}
@Test
public void testBug2() {
- Parser parser = new Parser("int a=arg+1; a=a; return a; ");
+ Parser parser = new Parser("int !a=arg+1; a=a; return a; ");
StopNode ret = parser.parse();
assertEquals("return (arg+1);", ret.print());
}
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index 165d23e5..aa659147 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -144,7 +144,7 @@ struct S{};
public void testForwardRef0() {
Parser parser = new Parser(
"""
-struct S1 { S2 s; };
+struct S1 { S2? s; };
return new S2;
""");
try { parser.parse().iterate(); fail(); }
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java
index ad287e82..be48ec55 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter16Test.java
@@ -43,23 +43,23 @@ return x+y;
assertEquals(5L, Evaluator.evaluate(stop, 0));
}
- @Test
- public void testFinal0() {
- Parser parser = new Parser(
-"""
-int !x=2;
-x=3;
-return x;
-""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
- }
+// @Test
+// public void testFinal0() {
+// Parser parser = new Parser(
+//"""
+//int !x=2;
+//x=3;
+//return x;
+//""");
+// try { parser.parse().iterate(); fail(); }
+// catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+// }
@Test
public void testFinal1() {
Parser parser = new Parser(
"""
-int !x=2, y=3;
+int x=2, y=3;
if( arg ) { int x = y; x = x*x; y=x; } // Shadow final x
return y;
""");
@@ -107,7 +107,7 @@ return z.x;
@Test
- public void testStructFinal() {
+ public void testStructFinal0() {
Parser parser = new Parser("""
struct Point { int !x, !y; };
Point p = new Point { x=3; y=4; };
@@ -118,6 +118,67 @@ return p;
assertEquals("Obj<Point>{x=3,y=4}", Evaluator.evaluate(stop, 0).toString());
}
+ @Test
+ public void testStructFinal1() {
+ Parser parser = new Parser("""
+struct Point { int x=3, y=4; };
+Point p = new Point { x=5; y=6; };
+p.x++;
+return p;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+ }
+
+ @Test
+ public void testStructFinal2() {
+ Parser parser = new Parser("""
+struct Point { int x=3, y=4; };
+Point p = new Point;
+p.x++;
+return p;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+ }
+
+ @Test
+ public void testStructFinal3() {
+ Parser parser = new Parser("""
+struct Point { var x; var y; };
+Point p = new Point;
+p.x++;
+return p;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Syntax error, expected expression: ;",e.getMessage()); }
+ }
+
+ @Test
+ public void testStructFinal4() {
+ Parser parser = new Parser("""
+struct Point { val x=3; val y=4; };
+Point p = new Point;
+p.x++;
+return p;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+ }
+
+ @Test
+ public void testStructFinal5() {
+ Parser parser = new Parser("""
+struct Point { var x=3; var y=4; };
+Point p = new Point;
+p.x++;
+return p;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Point;", stop.toString());
+ assertEquals("Obj<Point>{x=4,y=4}", Evaluator.evaluate(stop, 0).toString());
+ }
+
// Same as the Chapter13 test with the same name, but using the new
// constructor syntax
@Test
@@ -152,7 +213,7 @@ while( arg ) {
head = new LLI {
next=head;
// Any old code in the constructor
- int tmp=arg;
+ int !tmp=arg;
while( arg > 10 ) {
tmp = tmp + arg;
arg = arg - 1;
@@ -182,7 +243,7 @@ struct Square {
// Newtons approximation to the square root, computed in a constructor.
// The actual allocation will copy in this result as the initial
// value for 'diag'.
- flt diag = arg*arg/2;
+ flt !diag = arg*arg/2;
while( 1 ) {
flt next = (side/diag + diag)/2;
if( next == diag ) break;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
index 96f12480..5db9592e 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java
@@ -2,6 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
+import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -97,6 +98,71 @@ return d;
catch( Exception e ) { assertEquals("Syntax error, expected expression: ;",e.getMessage()); }
}
+
+ @Test
+ public void testTrinary0() {
+ Parser parser = new Parser("""
+return arg ? 1 : 2;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region19,1,2);", stop.toString());
+ assertEquals(2L, Evaluator.evaluate(stop, 0));
+ assertEquals(1L, Evaluator.evaluate(stop, 1));
+ }
+
+ @Test
+ public void testTrinary1() {
+ Parser parser = new Parser("""
+return arg ? 0 : arg;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 0;", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test
+ public void testTrinary2() {
+ Parser parser = new Parser("""
+struct Bar { int x; };
+var b = arg ? new Bar : null;
+return b ? b.x++ + b.x++ : -1;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region54,1,-1);", stop.toString());
+ assertEquals(-1L, Evaluator.evaluate(stop, 0));
+ assertEquals( 1L, Evaluator.evaluate(stop, 1));
+ }
+
+ @Test
+ public void testTrinary3() {
+ Parser parser = new Parser("""
+struct Bar { int x; };
+var b = arg ? new Bar;
+return b ? b.x++ + b.x++ : -1;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region54,1,-1);", stop.toString());
+ assertEquals(-1L, Evaluator.evaluate(stop, 0));
+ assertEquals( 1L, Evaluator.evaluate(stop, 1));
+ }
+
+ @Ignore
+ @Test
+ public void testTrinary4() {
+ Parser parser = new Parser("""
+struct Bar { Bar? next; int x; };
+var b = arg ? new Bar { next = (arg==2) ? new Bar{x=2;}; x=1; };
+return b ? b.next ? b.next.x : b.x; // parses "b ? (b.next ? b.next.x : b.x) : 0"
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region51,1,-1);", stop.toString());
+ assertEquals( 0L, Evaluator.evaluate(stop, 0));
+ assertEquals( 1L, Evaluator.evaluate(stop, 1));
+ assertEquals( 2L, Evaluator.evaluate(stop, 2));
+ }
+
+
+
@Test
public void testLinkedList2() {
Parser parser = new Parser("""