From 04460639bc75ffcb9ebf90efe6feccc444525b28 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 27 Mar 2016 16:29:12 -0400 Subject: Modified tree constructor to support "special" operators The use case this was created for was to support unary operators, but it should support operators of any arities, as well some basic meta operations. I can't think of any obvious ones off the top of my head but the potential is there. --- .../bjc/utils/parserutils/TreeConstructor.java | 46 ++++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc') 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 30c147e..6339d8c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -2,6 +2,7 @@ package bjc.utils.parserutils; import java.util.Deque; import java.util.LinkedList; +import java.util.function.Function; import java.util.function.Predicate; import bjc.utils.data.GenHolder; @@ -28,9 +29,42 @@ public class TreeConstructor { * The predicate to use to determine if something is a * operator * @return A AST from the expression + * + * @deprecated Use + * {@link TreeConstructor#constructTree(FunctionalList, Predicate, Predicate, Function)} + * instead */ public static AST constructTree(FunctionalList toks, Predicate opPredicate) { + return constructTree(toks, opPredicate, (op) -> false, null); + } + + /** + * Construct a tree from a list of tokens in postfix notation + * + * Only binary operators are accepted. + * + * @param + * The elements of the parse tree + * @param toks + * The list of tokens to build a tree from + * @param opPredicate + * The predicate to use to determine if something is a + * operator + * @param isSpecialOp + * The predicate to use to determine if an operator needs + * special handling + * @param handleSpecialOp + * 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 AST constructTree(FunctionalList toks, + Predicate opPredicate, Predicate isSpecialOp, + Function>, AST> handleSpecialOp) { GenHolder>, AST>> initState = new GenHolder<>(new Pair<>(new LinkedList<>(), null)); @@ -40,13 +74,17 @@ public class TreeConstructor { Deque> lft = par.merge((deq, ast) -> deq); AST mergedAST = par.merge((deq, ast) -> { - AST right = deq.pop(); - AST left = deq.pop(); + AST newAST; - AST newAST = new AST<>(ele, left, right); + if (isSpecialOp.test(ele)) { + newAST = handleSpecialOp.apply(deq); + } else { + AST right = deq.pop(); + AST left = deq.pop(); + newAST = new AST<>(ele, left, right); + } deq.push(newAST); - return newAST; }); -- cgit v1.2.3