@Test public void balanceUpAfterRtLeft() { PlusExpr target; ExprContainer container = new ExprContainer(); container.expr.set(mul(target = plus(id(), id()), id())); new ExprBinOpTransformer().balanceUp(target); assertEquals("(id '+' (id '*' id))", container.expr.get().toString()); }
public BinExprT balanceUp(BinExprT expr) { ExprT parent = parent(expr); if (!isBinOp(parent)) return expr; BinExprT parentNode = asBinOp(parent); int parentPriority = getPriority(parentNode); int exprPriority = getPriority(expr); Associativity assoc = getAssociativity(parentNode); if (isLeftChild(expr) && ( (assoc == Associativity.LEFT && parentPriority > exprPriority) || (assoc == Associativity.RIGHT && parentPriority >= exprPriority))) { return balanceUp(rotateRight(parentNode)); } if (isRightChild(expr) && ( (assoc == Associativity.LEFT && parentPriority >= exprPriority) || (assoc == Associativity.RIGHT && parentPriority > exprPriority))) { return balanceUp(rotateLeft(parentNode)); } return expr; }
BinOpTransformer { public BinExprT balanceUp(BinExprT expr) { ExprT parent = parent(expr); if (!isBinOp(parent)) return expr; BinExprT parentNode = asBinOp(parent); int parentPriority = getPriority(parentNode); int exprPriority = getPriority(expr); Associativity assoc = getAssociativity(parentNode); if (isLeftChild(expr) && ( (assoc == Associativity.LEFT && parentPriority > exprPriority) || (assoc == Associativity.RIGHT && parentPriority >= exprPriority))) { return balanceUp(rotateRight(parentNode)); } if (isRightChild(expr) && ( (assoc == Associativity.LEFT && parentPriority >= exprPriority) || (assoc == Associativity.RIGHT && parentPriority > exprPriority))) { return balanceUp(rotateLeft(parentNode)); } return expr; } }
BinOpTransformer { public BinExprT balanceUp(BinExprT expr) { ExprT parent = parent(expr); if (!isBinOp(parent)) return expr; BinExprT parentNode = asBinOp(parent); int parentPriority = getPriority(parentNode); int exprPriority = getPriority(expr); Associativity assoc = getAssociativity(parentNode); if (isLeftChild(expr) && ( (assoc == Associativity.LEFT && parentPriority > exprPriority) || (assoc == Associativity.RIGHT && parentPriority >= exprPriority))) { return balanceUp(rotateRight(parentNode)); } if (isRightChild(expr) && ( (assoc == Associativity.LEFT && parentPriority >= exprPriority) || (assoc == Associativity.RIGHT && parentPriority > exprPriority))) { return balanceUp(rotateLeft(parentNode)); } return expr; }  }
BinOpTransformer { public BinExprT balanceUp(BinExprT expr) { ExprT parent = parent(expr); if (!isBinOp(parent)) return expr; BinExprT parentNode = asBinOp(parent); int parentPriority = getPriority(parentNode); int exprPriority = getPriority(expr); Associativity assoc = getAssociativity(parentNode); if (isLeftChild(expr) && ( (assoc == Associativity.LEFT && parentPriority > exprPriority) || (assoc == Associativity.RIGHT && parentPriority >= exprPriority))) { return balanceUp(rotateRight(parentNode)); } if (isRightChild(expr) && ( (assoc == Associativity.LEFT && parentPriority >= exprPriority) || (assoc == Associativity.RIGHT && parentPriority > exprPriority))) { return balanceUp(rotateLeft(parentNode)); } return expr; }  BinExprT balanceUp(BinExprT expr); ExprT balanceOnOperationChange(BinExprT expr); BinExprT balanceDown(BinExprT expr); }
BinOpTransformer { public BinExprT balanceUp(BinExprT expr) { ExprT parent = parent(expr); if (!isBinOp(parent)) return expr; BinExprT parentNode = asBinOp(parent); int parentPriority = getPriority(parentNode); int exprPriority = getPriority(expr); Associativity assoc = getAssociativity(parentNode); if (isLeftChild(expr) && ( (assoc == Associativity.LEFT && parentPriority > exprPriority) || (assoc == Associativity.RIGHT && parentPriority >= exprPriority))) { return balanceUp(rotateRight(parentNode)); } if (isRightChild(expr) && ( (assoc == Associativity.LEFT && parentPriority >= exprPriority) || (assoc == Associativity.RIGHT && parentPriority > exprPriority))) { return balanceUp(rotateLeft(parentNode)); } return expr; }  BinExprT balanceUp(BinExprT expr); ExprT balanceOnOperationChange(BinExprT expr); BinExprT balanceDown(BinExprT expr);  }