summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:42:39 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:42:39 -0500
commit72a0d0703a5bfff365da541c190bac4a7099e92f (patch)
treed668ea54d22faabfb7a473db3d2232faed2bc27f /BJC-Utils2/src/main
parentc2089e5a3e604424c7cd00bbddcb03731ff5a0ea (diff)
Some refactoring and commenting
Diffstat (limited to 'BJC-Utils2/src/main')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java87
1 files changed, 79 insertions, 8 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
index 4b9cb79..ace636e 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java
@@ -6,33 +6,85 @@ import java.util.LinkedList;
import java.util.Map;
import java.util.function.Function;
+import bjc.utils.data.IPrecedent;
import bjc.utils.funcdata.FunctionalList;
+/**
+ * Utility to run the shunting yard algorithm on a bunch of tokens
+ *
+ * @author ben
+ *
+ * @param <E>
+ * The type of tokens being shunted
+ */
public class ShuntingYard<E> {
- private enum Operator {
- ADD(1), SUBTRACT(2), MULTIPLY(3), DIVIDE(4);
- final int precedence;
+ private static enum Operator implements IPrecedent {
+ ADD(1), DIVIDE(4), MULTIPLY(3), SUBTRACT(2);
- Operator(int p) {
+ private final int precedence;
+
+ private Operator(int p) {
precedence = p;
}
- }
- private static Map<String, Operator> ops = new HashMap<String, Operator>();
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.parserutils.IPrecedent#getPrecedence()
+ */
+ @Override
+ public int getPrecedence() {
+ return precedence;
+ }
+ }
static {
+ }
+
+ /**
+ * Holds all the shuntable operations
+ */
+ private Map<String, IPrecedent> ops;
+
+ /**
+ * Create a new shunting yard with a default set of operators
+ */
+ public ShuntingYard() {
+ ops = new HashMap<>();
+
ops.put("+", Operator.ADD);
ops.put("-", Operator.SUBTRACT);
ops.put("*", Operator.MULTIPLY);
ops.put("/", Operator.DIVIDE);
}
+ /**
+ * Add an operator to the list of shuntable operators
+ *
+ * @param tok
+ * The token representing the operator
+ * @param prec
+ * The precedence of the operator
+ */
+ public void addOp(String tok, IPrecedent prec) {
+ ops.put(tok, prec);
+ }
+
private boolean isHigherPrec(String op, String sub) {
- return (ops.containsKey(sub)
- && ops.get(sub).precedence >= ops.get(op).precedence);
+ return (ops.containsKey(sub) && ops.get(sub).getPrecedence() >= ops
+ .get(op).getPrecedence());
}
+ /**
+ * Transform a string of tokens from infix notation to postfix
+ *
+ * @param inp
+ * The string to transform
+ * @param transform
+ * The function to use to transform strings to tokens
+ * @return A list of tokens in postfix notation
+ */
public FunctionalList<E> postfix(FunctionalList<String> inp,
Function<String, E> transform) {
FunctionalList<E> outp = new FunctionalList<>();
@@ -66,4 +118,23 @@ public class ShuntingYard<E> {
return outp;
}
+ /**
+ * Remove an operator from the list of shuntable operators
+ *
+ * @param tok
+ * The token representing the operator
+ */
+ public void removeOp(String tok) {
+ ops.remove(tok);
+ }
+
+ /**
+ * Add an operator to the list of shuntable operators
+ *
+ * @param tok
+ * The token representing the operator
+ */
+ public void addOp(String tok, int i) {
+ this.addOp(tok, IPrecedent.newSimplePrecedent(i));
+ }
} \ No newline at end of file