From 848dc739becfa41193aff9a07c918aed91e5ef79 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 7 Apr 2017 08:56:27 -0400 Subject: Cleanup --- .../bjc/utils/parserutils/TreeConstructor.java | 57 ++++++++++++++++------ 1 file changed, 41 insertions(+), 16 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 82ded42..bd0ab97 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -19,6 +19,29 @@ import java.util.function.Predicate; * */ public class TreeConstructor { + /** + * Alias interface for special operator types. + * + * @param + * The token type of the tree. + */ + public interface QueueFlattener extends Function>, ITree> { + + } + + /* + * Alias for constructor state. + */ + static final class ConstructorState extends Pair>, ITree> { + public ConstructorState(Deque> left, ITree right) { + super(left, right); + } + + public ConstructorState(IPair>, ITree> par) { + super(par.getLeft(), par.getRight()); + } + } + /** * Construct a tree from a list of tokens in postfix notation * @@ -36,36 +59,38 @@ public class TreeConstructor { public static ITree constructTree(IList tokens, Predicate isOperator) { // Construct a tree with no special operators - return constructTree(tokens, isOperator, (op) -> false, null); + return constructTree(tokens, isOperator, op -> false, null); } /** - * Construct a tree from a list of tokens in postfix notation + * Construct a tree from a list of tokens in postfix notation. * * Only binary operators are accepted by default. Use the last two - * parameters to handle non-binary operators + * parameters to handle non-binary operators. * * @param - * The elements of the parse tree + * The elements of the parse tree. + * * @param tokens - * The list of tokens to build a tree from + * The list of tokens to build a tree from. + * * @param isOperator * The predicate to use to determine if something is a - * operator + * operator. + * * @param isSpecialOperator * The predicate to use to determine if an operator needs - * special handling + * special handling. + * * @param handleSpecialOperator - * The function to use to handle special case operators + * The function to use to handle special case operators. + * * @return A AST from the expression * - * FIXME The handleSpecialOp function seems like an ugly - * interface. Maybe there's a better way to express how that - * works. */ public static ITree constructTree(IList tokens, Predicate isOperator, Predicate isSpecialOperator, - Function>, ITree>> handleSpecialOperator) { + Function> handleSpecialOperator) { // Make sure our parameters are valid if(tokens == null) throw new NullPointerException("Tokens must not be null"); @@ -75,16 +100,16 @@ public class TreeConstructor { throw new NullPointerException("Special operator determiner must not be null"); // Here is the state for the tree construction - IHolder>, ITree>> initialState = new Identity<>( - new Pair<>(new LinkedList<>(), null)); + IHolder> initialState = new Identity<>( + new ConstructorState<>(new LinkedList<>(), null)); // Transform each of the tokens tokens.forEach(new TokenTransformer<>(initialState, isOperator, isSpecialOperator, handleSpecialOperator)); // Grab the tree from the state - return initialState.unwrap((pair) -> { + return initialState.unwrap(pair -> { return pair.getRight(); }); } -} +} \ No newline at end of file -- cgit v1.2.3