From fb7d03388e298258563c22abda1bd46cdaf991b7 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 25 Apr 2016 22:11:28 -0400 Subject: General code cleanup, and some more GUI controls --- .../java/bjc/utils/parserutils/ShuntingYard.java | 46 +++++++++--------- .../bjc/utils/parserutils/TokenTransformer.java | 55 +++++++++++----------- .../bjc/utils/parserutils/TreeConstructor.java | 21 +++++---- 3 files changed, 63 insertions(+), 59 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils') 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 636bf31..2ea23c6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -16,10 +16,10 @@ import bjc.utils.funcutils.StringUtils; * * @author ben * - * @param + * @param * The type of tokens being shunted */ -public class ShuntingYard { +public class ShuntingYard { /** * A enum representing the fundamental operator types * @@ -32,17 +32,18 @@ public class ShuntingYard { */ ADD(1), /** - * Represents division + * Represents subtraction */ - DIVIDE(4), + SUBTRACT(2), + /** * Represents multiplication */ MULTIPLY(3), /** - * Represents subtraction + * Represents division */ - SUBTRACT(2); + DIVIDE(4); private final int precedence; @@ -62,12 +63,13 @@ public class ShuntingYard { } private final class TokenShunter implements Consumer { - private IFunctionalList output; - private Deque stack; - private Function transform; + private IFunctionalList output; + private Deque stack; + private Function transform; - public TokenShunter(IFunctionalList outpt, Deque stack, - Function transform) { + public TokenShunter(IFunctionalList outpt, + Deque stack, + Function transform) { this.output = outpt; this.stack = stack; this.transform = transform; @@ -159,11 +161,10 @@ public class ShuntingYard { return false; } - boolean hasHigherPrecedence = operators.get(rightOperator) - .getPrecedence() >= operators.get(leftOperator) - .getPrecedence(); + int rightPrecedence = operators.get(rightOperator).getPrecedence(); + int leftPrecedence = operators.get(leftOperator).getPrecedence(); - return hasHigherPrecedence; + return rightPrecedence >= leftPrecedence; } /** @@ -175,15 +176,16 @@ public class ShuntingYard { * The function to use to transform strings to tokens * @return A list of tokens in postfix notation */ - public IFunctionalList postfix(IFunctionalList input, - Function tokenTransformer) { + public IFunctionalList postfix( + IFunctionalList input, + Function tokenTransformer) { if (input == null) { throw new NullPointerException("Input must not be null"); } else if (tokenTransformer == null) { throw new NullPointerException("Transformer must not be null"); } - IFunctionalList output = new FunctionalList<>(); + IFunctionalList output = new FunctionalList<>(); Deque stack = new LinkedList<>(); @@ -199,14 +201,14 @@ public class ShuntingYard { /** * Remove an operator from the list of shuntable operators * - * @param tok + * @param token * The token representing the operator */ - public void removeOp(String tok) { - if (tok == null) { + public void removeOp(String token) { + if (token == null) { throw new NullPointerException("Token must not be null"); } - operators.remove(tok); + operators.remove(token); } } \ No newline at end of file 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 4727124..9fb64cf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java @@ -12,26 +12,26 @@ import bjc.utils.data.Pair; import bjc.utils.funcdata.ITree; import bjc.utils.funcdata.Tree; -final class TokenTransformer implements Consumer { - private final class OperatorHandler - implements UnaryOperator>, ITree>> { - private T element; +final class TokenTransformer implements Consumer { + private final class OperatorHandler implements + UnaryOperator>, ITree>> { + private TokenType element; - public OperatorHandler(T element) { + public OperatorHandler(TokenType element) { this.element = element; } @Override - public IPair>, ITree> apply( - IPair>, ITree> pair) { - return pair.bind((queuedASTs, currentAST) -> { + public IPair>, ITree> apply( + IPair>, ITree> pair) { + return pair.bindLeft((queuedASTs) -> { return handleOperator(queuedASTs); }); } - private IPair>, ITree> handleOperator( - Deque> queuedASTs) { - ITree newAST; + private IPair>, ITree> + handleOperator(Deque> queuedASTs) { + ITree newAST; if (isSpecialOperator.test(element)) { newAST = handleSpecialOperator.apply(element) @@ -45,8 +45,8 @@ final class TokenTransformer implements Consumer { + queuedASTs.peek()); } - ITree rightAST = queuedASTs.pop(); - ITree leftAST = queuedASTs.pop(); + ITree rightAST = queuedASTs.pop(); + ITree leftAST = queuedASTs.pop(); newAST = new Tree<>(element, leftAST, rightAST); } @@ -57,15 +57,18 @@ final class TokenTransformer implements Consumer { } } - private IHolder>, ITree>> initialState; - private Predicate operatorPredicate; - private Predicate isSpecialOperator; - private Function>, ITree>> handleSpecialOperator; + private IHolder>, ITree>> initialState; + + private Predicate operatorPredicate; + + private Predicate isSpecialOperator; + private Function>, ITree>> handleSpecialOperator; public TokenTransformer( - IHolder>, ITree>> initialState, - Predicate operatorPredicate, Predicate isSpecialOperator, - Function>, ITree>> handleSpecialOperator) { + IHolder>, ITree>> initialState, + Predicate operatorPredicate, + Predicate isSpecialOperator, + Function>, ITree>> handleSpecialOperator) { this.initialState = initialState; this.operatorPredicate = operatorPredicate; this.isSpecialOperator = isSpecialOperator; @@ -73,20 +76,16 @@ final class TokenTransformer implements Consumer { } @Override - public void accept(T element) { + public void accept(TokenType element) { if (operatorPredicate.test(element)) { initialState.transform(new OperatorHandler(element)); } else { - ITree newAST = new Tree<>(element); + ITree newAST = new Tree<>(element); - initialState.doWith((pair) -> { - pair.doWith((queue, currentAST) -> { + initialState.transform((pair) -> { + return pair.bindLeft((queue) -> { queue.push(newAST); - }); - }); - initialState.transform((pair) -> { - return pair.bind((queue, currentAST) -> { return new Pair<>(queue, newAST); }); }); 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 2d2a69e..283d16e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -24,7 +24,7 @@ public class TreeConstructor { * * Only binary operators are accepted. * - * @param + * @param * The elements of the parse tree * @param tokens * The list of tokens to build a tree from @@ -33,8 +33,9 @@ public class TreeConstructor { * operator * @return A AST from the expression */ - public static ITree constructTree(IFunctionalList tokens, - Predicate operatorPredicate) { + public static ITree constructTree( + IFunctionalList tokens, + Predicate operatorPredicate) { return constructTree(tokens, operatorPredicate, (op) -> false, null); } @@ -45,7 +46,7 @@ public class TreeConstructor { * Only binary operators are accepted by default. Use the last two * parameters to handle non-binary operators * - * @param + * @param * The elements of the parse tree * @param tokens * The list of tokens to build a tree from @@ -63,9 +64,11 @@ public class TreeConstructor { * interface. Maybe there's a better way to express how that * works */ - public static ITree constructTree(IFunctionalList tokens, - Predicate operatorPredicate, Predicate isSpecialOperator, - Function>, ITree>> handleSpecialOperator) { + public static ITree constructTree( + IFunctionalList tokens, + Predicate operatorPredicate, + Predicate isSpecialOperator, + Function>, ITree>> handleSpecialOperator) { if (tokens == null) { throw new NullPointerException("Tokens must not be null"); } else if (operatorPredicate == null) { @@ -76,8 +79,8 @@ public class TreeConstructor { "Special operator determiner must not be null"); } - IHolder>, ITree>> initialState = new Identity<>( - new Pair<>(new LinkedList<>(), null)); + IHolder>, ITree>> initialState = + new Identity<>(new Pair<>(new LinkedList<>(), null)); tokens.forEach( new TokenTransformer<>(initialState, operatorPredicate, -- cgit v1.2.3