summaryrefslogtreecommitdiff
path: root/chapter12
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-09-13 09:23:19 -0700
committerXmiliaH <45106915+XmiliaH@users.noreply.github.com>2024-09-13 18:46:01 +0200
commit703f88dbb2e1e4352a0725b7e742151739b82ea1 (patch)
treea1e541797a02d79c372508a22b7ddabb0f25fdb9 /chapter12
parentce4c47a5bec87ca404fbb49a2d91f34ab6f4e545 (diff)
downloadSimple-703f88dbb2e1e4352a0725b7e742151739b82ea1.tar.gz
Simple-703f88dbb2e1e4352a0725b7e742151739b82ea1.zip
x-x==0
Move x-x==0 to compute. Add x/1 ==> x All chapters
Diffstat (limited to 'chapter12')
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java7
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java8
2 files changed, 10 insertions, 5 deletions
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
index a3845d89..e0c95019 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -32,7 +32,12 @@ public class DivNode extends Node {
}
@Override
- public Node idealize() { return null; }
+ public Node idealize() {
+ // Div of 1.
+ if( in(2)._type.isConstant() && in(2)._type instanceof TypeInteger i && i.value()==1 )
+ return in(1);
+ return null;
+ }
@Override Node copy(Node lhs, Node rhs) { return new DivNode(lhs,rhs); }
@Override Node copyF() { return new DivFNode(null,null); }
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 33b6a162..8bebe774 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -27,15 +27,15 @@ public class SubNode extends Node {
if (i0.isConstant() && i1.isConstant())
return TypeInteger.constant(i0.value()-i1.value());
}
+ // Sub of same is 0
+ if( in(1)==in(2) )
+ return TypeInteger.ZERO;
+
return in(1)._type.meet(in(2)._type);
}
@Override
public Node idealize() {
- // Sub of same is 0
- if( in(1)==in(2) )
- return Parser.ZERO;
-
// x - (-y) is x+y
if( in(2) instanceof MinusNode minus )
return new AddNode(in(1),minus.in(1));