From 72a0d0703a5bfff365da541c190bac4a7099e92f Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 21 Feb 2016 15:42:39 -0500 Subject: Some refactoring and commenting --- BJC-Utils2/pom.xml | 51 ++++++++----- .../java/bjc/utils/parserutils/ShuntingYard.java | 87 ++++++++++++++++++++-- 2 files changed, 112 insertions(+), 26 deletions(-) diff --git a/BJC-Utils2/pom.xml b/BJC-Utils2/pom.xml index 7eb6f43..9323a0f 100644 --- a/BJC-Utils2/pom.xml +++ b/BJC-Utils2/pom.xml @@ -1,24 +1,39 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - bjc - BJC-Utils2 - 0.1.0-SNAPSHOT - jar + bjc + BJC-Utils2 + 0.1.0-SNAPSHOT + jar - BJC-Utils2 - http://maven.apache.org + BJC-Utils2 + http://maven.apache.org - - UTF-8 - + + UTF-8 + - - - junit - junit - 4.12 - - + + + junit + junit + 4.12 + + + org.junit.contrib + junit-theories + 4.12 + + + com.pholser + junit-quickcheck-core + 0.5 + + + com.pholser + junit-quickcheck-generators + 0.5 + + 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 + * The type of tokens being shunted + */ public class ShuntingYard { - 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 ops = new HashMap(); + /* + * (non-Javadoc) + * + * @see bjc.utils.parserutils.IPrecedent#getPrecedence() + */ + @Override + public int getPrecedence() { + return precedence; + } + } static { + } + + /** + * Holds all the shuntable operations + */ + private Map 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 postfix(FunctionalList inp, Function transform) { FunctionalList outp = new FunctionalList<>(); @@ -66,4 +118,23 @@ public class ShuntingYard { 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 -- cgit v1.2.3