From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../bjc/utils/parserutils/TreeConstructor.java | 99 ++-------------------- 1 file changed, 7 insertions(+), 92 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java') 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 52299bb..efd0394 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -2,18 +2,15 @@ package bjc.utils.parserutils; import java.util.Deque; import java.util.LinkedList; -import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; -import java.util.function.UnaryOperator; -import bjc.utils.data.experimental.IHolder; -import bjc.utils.data.experimental.IPair; -import bjc.utils.data.experimental.Identity; -import bjc.utils.data.experimental.Pair; +import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; +import bjc.utils.data.Identity; +import bjc.utils.data.Pair; import bjc.utils.funcdata.IFunctionalList; import bjc.utils.funcdata.ITree; -import bjc.utils.funcdata.Tree; /** * Creates a parse tree from a postfix expression @@ -22,88 +19,6 @@ import bjc.utils.funcdata.Tree; * */ public class TreeConstructor { - private static final class TokenTransformer implements Consumer { - private final class OperatorHandler implements - UnaryOperator>, ITree>> { - private T element; - - public OperatorHandler(T element) { - this.element = element; - } - - @Override - public IPair>, ITree> apply( - IPair>, ITree> pair) { - return pair.bind((queuedASTs, currentAST) -> { - return handleOperator(queuedASTs); - }); - } - - private IPair>, ITree> handleOperator( - Deque> queuedASTs) { - ITree newAST; - - if (isSpecialOperator.test(element)) { - newAST = handleSpecialOperator.apply(queuedASTs); - } else { - if (queuedASTs.size() < 2) { - throw new IllegalStateException( - "Attempted to parse binary operator without enough operands.\n" - + "Problem operator is " + element - + "\nPossible operand is: \n\t" - + queuedASTs.peek()); - } - - ITree rightAST = queuedASTs.pop(); - ITree leftAST = queuedASTs.pop(); - - newAST = new Tree<>(element, leftAST, rightAST); - } - - queuedASTs.push(newAST); - - return new Pair<>(queuedASTs, newAST); - } - } - - 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) { - this.initialState = initialState; - this.operatorPredicate = operatorPredicate; - this.isSpecialOperator = isSpecialOperator; - this.handleSpecialOperator = handleSpecialOperator; - } - - @Override - public void accept(T element) { - if (operatorPredicate.test(element)) { - initialState.transform(new OperatorHandler(element)); - } else { - ITree newAST = new Tree<>(element); - - initialState.doWith((pair) -> { - pair.doWith((queue, currentAST) -> { - queue.push(newAST); - }); - }); - - initialState.transform((pair) -> { - return pair.bind((queue, currentAST) -> { - return new Pair<>(queue, newAST); - }); - }); - } - } - } - /** * Construct a tree from a list of tokens in postfix notation * @@ -161,15 +76,15 @@ 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, isSpecialOperator, handleSpecialOperator)); return initialState.unwrap((pair) -> { - return pair.merge((queue, currentAST) -> currentAST); + return pair.getRight(); }); } } -- cgit v1.2.3