From d91dc657cce857d90389eefd9dd62d7c00ee0710 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 29 May 2024 12:39:56 -0700 Subject: Add a minor peep to settle a CCC discussion (#82) * Add a minor peep to settle a CCC discussion * remove junk test --- .../java/com/seaofnodes/simple/node/SubNode.java | 4 ++++ .../java/com/seaofnodes/simple/Chapter10Test.java | 24 ++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) (limited to 'chapter10/src') 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()); -- cgit v1.2.3