diff options
| author | Cliff Click <cliffc@acm.org> | 2024-09-13 09:23:19 -0700 |
|---|---|---|
| committer | XmiliaH <45106915+XmiliaH@users.noreply.github.com> | 2024-09-13 18:46:01 +0200 |
| commit | 703f88dbb2e1e4352a0725b7e742151739b82ea1 (patch) | |
| tree | a1e541797a02d79c372508a22b7ddabb0f25fdb9 /chapter13/src | |
| parent | ce4c47a5bec87ca404fbb49a2d91f34ab6f4e545 (diff) | |
| download | Simple-703f88dbb2e1e4352a0725b7e742151739b82ea1.tar.gz Simple-703f88dbb2e1e4352a0725b7e742151739b82ea1.zip | |
x-x==0
Move x-x==0 to compute.
Add x/1 ==> x
All chapters
Diffstat (limited to 'chapter13/src')
| -rw-r--r-- | chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java | 7 | ||||
| -rw-r--r-- | chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java | 7 |
2 files changed, 10 insertions, 4 deletions
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java index a3845d89..e0c95019 100644 --- a/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java +++ b/chapter13/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/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java index 33b6a162..0f2deb33 100644 --- a/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java +++ b/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java @@ -27,14 +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 ) |
