diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 19:56:32 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 19:56:32 -0400 |
| commit | b4f5f98c0aa7fc892e96771ff2df729e61c21f74 (patch) | |
| tree | 09820cc267577c295be7bf33dc5deabf662cb37c /BJC-Utils2/src/main/java/bjc/utils/parserutils | |
| parent | 7c12fd8fe169944152ca73f0da4e8fe8e280f648 (diff) | |
Minor code changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils')
3 files changed, 20 insertions, 26 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 9c7618b..0e34b97 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -87,7 +87,9 @@ public class ShuntingYard<E> { stack.push(token); } else if (StringUtils.containsOnly(token, "\\)")) { // Handle groups of parenthesis for multiple nesting levels - while (stack.peek().equals(token.replace(')', '('))) { + String swappedToken = token.replace(')', '('); + + while (!stack.peek().equals(swappedToken)) { output.add(transform.apply(stack.pop())); } @@ -98,21 +100,6 @@ public class ShuntingYard<E> { } } - private static boolean shouldConfigureBasicOperators = true; - - /** - * Set whether the shunter should configure the four basic math - * operators - * - * @param configureBasicOperators - * Whether or not the four basic math operators should be - * configured - */ - public static void setBasicOperatorConfiguration( - boolean configureBasicOperators) { - shouldConfigureBasicOperators = configureBasicOperators; - } - /** * Holds all the shuntable operations */ @@ -120,11 +107,12 @@ public class ShuntingYard<E> { /** * Create a new shunting yard with a default set of operators + * @param configureBasics Whether or not basic math operators should be provided */ - public ShuntingYard() { + public ShuntingYard(boolean configureBasics) { operators = new FunctionalMap<>(); - if (shouldConfigureBasicOperators) { + if (configureBasics) { operators.put("+", Operator.ADD); operators.put("-", Operator.SUBTRACT); operators.put("*", Operator.MULTIPLY); @@ -165,11 +153,15 @@ public class ShuntingYard<E> { String rightOperator) { boolean operatorExists = operators.containsKey(rightOperator); + if (!operatorExists) { + return false; + } + boolean hasHigherPrecedence = operators.get(rightOperator).getPrecedence() >= operators .get(leftOperator).getPrecedence(); - return (operatorExists && hasHigherPrecedence); + return hasHigherPrecedence; } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java index 4db5e76..c88763f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java @@ -11,6 +11,7 @@ import bjc.utils.data.IPair; import bjc.utils.data.Pair; import bjc.utils.funcdata.ITree; import bjc.utils.funcdata.Tree; +import bjc.utils.funcutils.StringUtils; final class TokenTransformer<T> implements Consumer<T> { private final class OperatorHandler @@ -34,7 +35,8 @@ final class TokenTransformer<T> implements Consumer<T> { ITree<T> newAST; if (isSpecialOperator.test(element)) { - newAST = handleSpecialOperator.apply(queuedASTs); + newAST = handleSpecialOperator.apply(element) + .apply(queuedASTs); } else { if (queuedASTs.size() < 2) { throw new IllegalStateException( @@ -56,15 +58,15 @@ final class TokenTransformer<T> implements Consumer<T> { } } - private IHolder<IPair<Deque<ITree<T>>, ITree<T>>> initialState; - private Predicate<T> operatorPredicate; - private Predicate<T> isSpecialOperator; - private Function<Deque<ITree<T>>, ITree<T>> handleSpecialOperator; + private IHolder<IPair<Deque<ITree<T>>, ITree<T>>> initialState; + private Predicate<T> operatorPredicate; + private Predicate<T> isSpecialOperator; + private Function<T, Function<Deque<ITree<T>>, ITree<T>>> handleSpecialOperator; public TokenTransformer( IHolder<IPair<Deque<ITree<T>>, ITree<T>>> initialState, Predicate<T> operatorPredicate, Predicate<T> isSpecialOperator, - Function<Deque<ITree<T>>, ITree<T>> handleSpecialOperator) { + Function<T, Function<Deque<ITree<T>>, ITree<T>>> handleSpecialOperator) { this.initialState = initialState; this.operatorPredicate = operatorPredicate; this.isSpecialOperator = isSpecialOperator; diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java index efd0394..152d4d9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -65,7 +65,7 @@ public class TreeConstructor { */ public static <T> ITree<T> constructTree(IFunctionalList<T> tokens, Predicate<T> operatorPredicate, Predicate<T> isSpecialOperator, - Function<Deque<ITree<T>>, ITree<T>> handleSpecialOperator) { + Function<T, Function<Deque<ITree<T>>, ITree<T>>> handleSpecialOperator) { if (tokens == null) { throw new NullPointerException("Tokens must not be null"); } else if (operatorPredicate == null) { |
