From 4e1a13330028b57818ece6741e029ebdbc9c7572 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 7 Apr 2018 15:20:50 -0400 Subject: Documentation --- .../bjc/utils/parserutils/TreeConstructor.java | 56 ++++++++++------------ 1 file changed, 26 insertions(+), 30 deletions(-) (limited to 'base/src/main/java/bjc/utils/parserutils/TreeConstructor.java') diff --git a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java index 7bff43e..960e61a 100644 --- a/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/base/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -13,7 +13,7 @@ import bjc.utils.data.Pair; import bjc.utils.funcdata.IList; /** - * Creates a parse tree from a postfix expression + * Creates a parse tree from a postfix expression. * * @author ben * @@ -23,15 +23,13 @@ public class TreeConstructor { * Alias interface for special operator types. * * @param - * The token type of the tree. + * The token type of the tree. */ public interface QueueFlattener extends Function>, ITree> { } - /* - * Alias for constructor state. - */ + /* Alias for constructor state. */ static final class ConstructorState extends Pair>, ITree> { public ConstructorState(final Deque> left, final ITree right) { super(left, right); @@ -43,23 +41,25 @@ public class TreeConstructor { } /** - * 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. * * @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 - * @return A AST from the expression + * The predicate to use to determine if something is a + * operator. + * + * @return A AST from the expression. */ public static ITree constructTree(final IList tokens, final Predicate isOperator) { - /* - * Construct a tree with no special operators - */ + /* Construct a tree with no special operators */ return constructTree(tokens, isOperator, op -> false, null); } @@ -85,7 +85,7 @@ public class TreeConstructor { * @param handleSpecialOperator * The function to use to handle special case operators. * - * @return A AST from the expression + * @return A AST from the expression. * */ public static ITree constructTree(final IList tokens, @@ -101,23 +101,19 @@ public class TreeConstructor { else if(isSpecialOperator == null) throw new NullPointerException("Special operator determiner must not be null"); - /* - * Here is the state for the tree construction - */ - final IHolder> initialState = new Identity<>( - new ConstructorState<>(new LinkedList<>(), null)); + final ConstructorState cstate = new ConstructorState<>( + new LinkedList<>(), null); - /* - * Transform each of the tokens - */ - tokens.forEach(new TokenTransformer<>(initialState, isOperator, isSpecialOperator, - handleSpecialOperator)); + /* Here is the state for the tree construction */ + final IHolder> initialState = new Identity<>(cstate); - /* - * Grab the tree from the state - */ - return initialState.unwrap(pair -> { - return pair.getRight(); - }); + /* Transform each of the tokens */ + final TokenTransformer trans = new TokenTransformer<>(initialState, + isOperator, isSpecialOperator, handleSpecialOperator); + + tokens.forEach(trans); + + /* Grab the tree from the state */ + return initialState.unwrap(pair -> pair.getRight()); } } -- cgit v1.2.3