diff options
| -rw-r--r-- | BJC-Utils2/pom.xml | 51 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/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 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <groupId>bjc</groupId> - <artifactId>BJC-Utils2</artifactId> - <version>0.1.0-SNAPSHOT</version> - <packaging>jar</packaging> + <groupId>bjc</groupId> + <artifactId>BJC-Utils2</artifactId> + <version>0.1.0-SNAPSHOT</version> + <packaging>jar</packaging> - <name>BJC-Utils2</name> - <url>http://maven.apache.org</url> + <name>BJC-Utils2</name> + <url>http://maven.apache.org</url> - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - </properties> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.12</version> - </dependency> - </dependencies> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + </dependency> + <dependency> + <groupId>org.junit.contrib</groupId> + <artifactId>junit-theories</artifactId> + <version>4.12</version> + </dependency> + <dependency> + <groupId>com.pholser</groupId> + <artifactId>junit-quickcheck-core</artifactId> + <version>0.5</version> + </dependency> + <dependency> + <groupId>com.pholser</groupId> + <artifactId>junit-quickcheck-generators</artifactId> + <version>0.5</version> + </dependency> + </dependencies> </project> 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 |
