public override ICodeNode VisitBinaryExpression(BinaryExpression node) { bool shouldAddLeftParentheses = false; if (node.Left.CodeNodeType == CodeNodeType.BinaryExpression) { BinaryExpression leftAsBinary = node.Left as BinaryExpression; int compared = leftAsBinary.CompareOperators(node); // (a + b) * c if (compared > 0) { shouldAddLeftParentheses = true; } else if (leftAsBinary.IsOverridenOperation) { shouldAddLeftParentheses = true; } } if (shouldAddLeftParentheses) { node.Left = new ParenthesesExpression(node.Left); } bool shouldAddRightParentheses = false; if (node.Right.CodeNodeType == CodeNodeType.BinaryExpression) { BinaryExpression rightAsBinary = node.Right as BinaryExpression; int compared = rightAsBinary.CompareOperators(node); // a * (b + c) if (compared > 0) { shouldAddRightParentheses = true; } else if (compared == 0) { // a * (b / c) if (node.Operator != rightAsBinary.Operator) { shouldAddRightParentheses = true; } // a - (b - c) else if (!IsCommutative(node.Operator)) { shouldAddRightParentheses = true; } // a + (b + c) else if (rightAsBinary.ExpressionType == rightAsBinary.ExpressionType.Module.TypeSystem.Single || rightAsBinary.ExpressionType == rightAsBinary.ExpressionType.Module.TypeSystem.Double) { shouldAddRightParentheses = true; } } else if (!node.IsAssignmentExpression && rightAsBinary.IsOverridenOperation) { shouldAddRightParentheses = true; } } if (shouldAddRightParentheses) { node.Right = new ParenthesesExpression(node.Right); } return(base.VisitBinaryExpression(node)); }