From 8a8b457c98e207d809a7616e73eb59bfe197a7a5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 31 Mar 2016 11:43:21 -0400 Subject: More code maintenance --- .../bjc/utils/parserutils/TreeConstructor.java | 71 ++++++++++++---------- 1 file changed, 38 insertions(+), 33 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 6339d8c..42d5a9d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -23,9 +23,9 @@ public class TreeConstructor { * * @param * The elements of the parse tree - * @param toks + * @param tokens * The list of tokens to build a tree from - * @param opPredicate + * @param operatorPredicate * The predicate to use to determine if something is a * operator * @return A AST from the expression @@ -34,9 +34,10 @@ public class TreeConstructor { * {@link TreeConstructor#constructTree(FunctionalList, Predicate, Predicate, Function)} * instead */ - public static AST constructTree(FunctionalList toks, - Predicate opPredicate) { - return constructTree(toks, opPredicate, (op) -> false, null); + public static AST constructTree(FunctionalList tokens, + Predicate operatorPredicate) { + return constructTree(tokens, operatorPredicate, (op) -> false, + null); } /** @@ -46,15 +47,15 @@ public class TreeConstructor { * * @param * The elements of the parse tree - * @param toks + * @param tokens * The list of tokens to build a tree from - * @param opPredicate + * @param operatorPredicate * The predicate to use to determine if something is a * operator - * @param isSpecialOp + * @param isSpecialOperator * The predicate to use to determine if an operator needs * special handling - * @param handleSpecialOp + * @param handleSpecialOperator * The function to use to handle special case operators * @return A AST from the expression * @@ -62,51 +63,55 @@ public class TreeConstructor { * interface. Maybe there's a better way to express how that * works */ - public static AST constructTree(FunctionalList toks, - Predicate opPredicate, Predicate isSpecialOp, - Function>, AST> handleSpecialOp) { - GenHolder>, AST>> initState = + public static AST constructTree(FunctionalList tokens, + Predicate operatorPredicate, Predicate isSpecialOperator, + Function>, AST> handleSpecialOperator) { + GenHolder>, AST>> initialState = new GenHolder<>(new Pair<>(new LinkedList<>(), null)); - toks.forEach((ele) -> { - if (opPredicate.test(ele)) { - initState.transform((par) -> { - Deque> lft = par.merge((deq, ast) -> deq); + tokens.forEach((element) -> { + if (operatorPredicate.test(element)) { + initialState.transform((pair) -> { + Deque> queuedASTs = + pair.merge((queue, currentAST) -> queue); - AST mergedAST = par.merge((deq, ast) -> { + AST mergedAST = pair.merge((queue, currentAST) -> { AST newAST; - if (isSpecialOp.test(ele)) { - newAST = handleSpecialOp.apply(deq); + if (isSpecialOperator.test(element)) { + newAST = handleSpecialOperator.apply(queue); } else { - AST right = deq.pop(); - AST left = deq.pop(); - newAST = new AST<>(ele, left, right); + AST rightAST = queue.pop(); + AST leftAST = queue.pop(); + + newAST = new AST<>(element, leftAST, rightAST); } - deq.push(newAST); + queue.push(newAST); return newAST; }); Pair>, AST> newPair = - new Pair<>(lft, mergedAST); + new Pair<>(queuedASTs, mergedAST); return newPair; }); } else { - AST newAST = new AST<>(ele); + AST newAST = new AST<>(element); - initState.doWith((par) -> par.doWith((deq, ast) -> { - deq.push(newAST); - })); + initialState.doWith( + (pair) -> pair.doWith((queue, currentAST) -> { + queue.push(newAST); + })); - initState.transform((par) -> { - return (Pair>, AST>) par - .apply((d) -> d, (a) -> newAST); + initialState.transform((pair) -> { + return (Pair>, AST>) pair.apply( + (queue) -> queue, (currentAST) -> newAST); }); } }); - return initState.unwrap((par) -> par.merge((deq, ast) -> ast)); + return initialState.unwrap( + (pair) -> pair.merge((queue, currentAST) -> currentAST)); } } -- cgit v1.2.3