Skip to content

Commit

Permalink
Flatten chains of InfixExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Jun 12, 2024
1 parent 6c73f3c commit 4eff632
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1288,14 +1288,6 @@ private Expression convertExpressionImpl(JCExpression javac) {
if (javac instanceof JCBinary binary) {
InfixExpression res = this.ast.newInfixExpression();
commonSettings(res, javac);
Expression left = convertExpression(binary.getLeftOperand());
if (left != null) {
res.setLeftOperand(left);
}
Expression right = convertExpression(binary.getRightOperand());
if (right != null) {
res.setRightOperand(right);
}
res.setOperator(switch (binary.getTag()) {
case OR -> InfixExpression.Operator.CONDITIONAL_OR;
case AND -> InfixExpression.Operator.CONDITIONAL_AND;
Expand All @@ -1318,6 +1310,12 @@ private Expression convertExpressionImpl(JCExpression javac) {
case MOD -> InfixExpression.Operator.REMAINDER;
default -> null;
});
Expression left = convertExpression(binary.getLeftOperand());
Expression right = convertExpression(binary.getRightOperand());
List<Expression> operands = collectOperands(res.getOperator(), left, right);
res.setLeftOperand(operands.removeFirst());
res.setRightOperand(operands.removeFirst());
res.extendedOperands().addAll(operands);
return res;
}
if (javac instanceof JCUnary unary) {
Expand Down Expand Up @@ -2914,5 +2912,21 @@ private static List<ASTNode> childrenOf(ASTNode node) {
.toList();
}


private List<Expression> collectOperands(org.eclipse.jdt.core.dom.InfixExpression.Operator operator, Expression... leftToRight) {
List<Expression> res = new ArrayList<>();
for (Expression exp : leftToRight) {
if (exp instanceof InfixExpression infix && Objects.equals(infix.getOperator(), operator)) {
infix.delete();
List<Expression> operands = new ArrayList<>(2 + infix.extendedOperands().size());
operands.add(infix.getLeftOperand());
operands.add(infix.getRightOperand());
operands.addAll(infix.extendedOperands());
res.addAll(collectOperands(operator, operands.toArray(Expression[]::new)));
} else {
exp.setParent(null, null);
res.add(exp);
}
}
return res;
}
}

0 comments on commit 4eff632

Please sign in to comment.