summaryrefslogtreecommitdiff
path: root/chapter10
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-05-29 12:39:56 -0700
committerGitHub <noreply@github.com>2024-05-29 20:39:56 +0100
commitd91dc657cce857d90389eefd9dd62d7c00ee0710 (patch)
tree6ef8427aa0535b1b568bf2e357d2909d38fbd334 /chapter10
parent492786481ed69edb847f5ebfa9a24a95d08aedb8 (diff)
downloadSimple-d91dc657cce857d90389eefd9dd62d7c00ee0710.tar.gz
Simple-d91dc657cce857d90389eefd9dd62d7c00ee0710.zip
Add a minor peep to settle a CCC discussion (#82)
* Add a minor peep to settle a CCC discussion * remove junk test
Diffstat (limited to 'chapter10')
-rw-r--r--chapter10/src/main/java/com/seaofnodes/simple/node/SubNode.java4
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java24
2 files changed, 19 insertions, 9 deletions
diff --git a/chapter10/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter10/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 760177d7..7be53b68 100644
--- a/chapter10/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter10/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -39,6 +39,10 @@ public class SubNode extends Node {
if( in(2) instanceof MinusNode minus )
return new AddNode(in(1),minus.in(1));
+ // (-x) - y is -(x+y)
+ if( in(1) instanceof MinusNode minus )
+ return new MinusNode(new AddNode(minus.in(1),in(2)).peephole());
+
return null;
}
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index 4a4ef8be..f7388ff9 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -17,16 +17,22 @@ public class Chapter10Test {
public void testFuzzer() {
Parser parser = new Parser(
"""
-struct s0 {
- int v0;
+int a = arg/3;
+int b = arg*5;
+int x = arg*7;
+int y = arg/11;
+int p; int g; int h;
+if( (arg/13)==0 ) {
+ p = x + y;
+ g = x;
+ h = y;
+} else {
+ p = a + b;
+ g = a;
+ h = b;
}
-s0 v5 = new s0;
-int v6 = new s0.v0;
-s0? arg=v5;
-if( arg ) v5 = new s0;
-else v6 = 0;
-s0 v7=v5;
-return 0;
+int r = g+h;
+return p-r;
""");
StopNode stop = parser.parse(false).iterate(false);
assertEquals("return 0;", stop.toString());