From e4e9e2ce2115391b616d383706b27a35d3a5c719 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 25 Jul 2017 14:21:00 -0700 Subject: [PATCH] Implement parsing a postfix expression into a tree. --- src/org/nwapw/abacus/tree/NumberNode.java | 25 +++++++++++++++ src/org/nwapw/abacus/tree/OpNode.java | 38 +++++++++++++++++++++++ src/org/nwapw/abacus/tree/TreeNode.java | 20 +++++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/org/nwapw/abacus/tree/NumberNode.java create mode 100644 src/org/nwapw/abacus/tree/OpNode.java diff --git a/src/org/nwapw/abacus/tree/NumberNode.java b/src/org/nwapw/abacus/tree/NumberNode.java new file mode 100644 index 0000000..bc034b0 --- /dev/null +++ b/src/org/nwapw/abacus/tree/NumberNode.java @@ -0,0 +1,25 @@ +package org.nwapw.abacus.tree; + +import org.nwapw.abacus.number.NaiveNumber; +import org.nwapw.abacus.number.Number; + +public class NumberNode extends TreeNode { + + private Number number; + + public NumberNode(){ + number = null; + } + + public NumberNode(double value){ + number = new NaiveNumber(value); + } + + public NumberNode(String value){ + this(Double.parseDouble(value)); + } + + public Number getNumber() { + return number; + } +} diff --git a/src/org/nwapw/abacus/tree/OpNode.java b/src/org/nwapw/abacus/tree/OpNode.java new file mode 100644 index 0000000..0fcdaed --- /dev/null +++ b/src/org/nwapw/abacus/tree/OpNode.java @@ -0,0 +1,38 @@ +package org.nwapw.abacus.tree; + +public class OpNode extends TreeNode { + + private String operation; + private TreeNode left; + private TreeNode right; + + public OpNode(String operation){ + this(operation, null, null); + } + + public OpNode(String operation, TreeNode left, TreeNode right){ + this.operation = operation; + this.left = left; + this.right = right; + } + + public String getOperation() { + return operation; + } + + public TreeNode getLeft() { + return left; + } + + public void setLeft(TreeNode left) { + this.left = left; + } + + public TreeNode getRight() { + return right; + } + + public void setRight(TreeNode right) { + this.right = right; + } +} diff --git a/src/org/nwapw/abacus/tree/TreeNode.java b/src/org/nwapw/abacus/tree/TreeNode.java index f34d48d..6581826 100644 --- a/src/org/nwapw/abacus/tree/TreeNode.java +++ b/src/org/nwapw/abacus/tree/TreeNode.java @@ -78,8 +78,26 @@ public abstract class TreeNode { return output; } - public static TreeNode fromString(String string){ + public static TreeNode fromStringRecursive(String source, ArrayList> matches){ + if(matches.size() == 0) return null; + Match match = matches.remove(0); + if(match.getType() == TokenType.OP){ + TreeNode right = fromStringRecursive(source, matches); + TreeNode left = fromStringRecursive(source, matches); + if(left == null || right == null) return null; + else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right); + } else if(match.getType() == TokenType.NUM){ + return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo()))); + } return null; } + public static TreeNode fromString(String string){ + ArrayList> matches = intoPostfix(string, tokenize(string)); + if(matches == null) return null; + + Collections.reverse(matches); + return fromStringRecursive(string, matches); + } + }