Remove unnecessary parentheses
The unary modulus operation does not require extra parentheses to be printed in cases like “|X + Y|”. This adds a new option to the printing routine to omit parentheses in cases where the parent expression already defines a parenthesis-like scope (currently only with unary operations).
This commit is contained in:
parent
8c250f5c59
commit
04094eee23
@ -41,38 +41,39 @@ struct PrintContext
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
output::ColorStream &print(output::ColorStream &stream, const BinaryOperation::Operator operator_, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const BinaryOperation &binaryOperation, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Boolean &boolean, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Comparison &comparison, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Constant &constant, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Function &function, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const In &in, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Integer &integer, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Interval &interval, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Predicate &predicate, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const SpecialInteger &specialInteger, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const String &string, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const UnaryOperation &unaryOperation, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Variable &variable, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const BinaryOperation::Operator operator_, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const BinaryOperation &binaryOperation, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Boolean &boolean, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Comparison &comparison, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, Comparison::Operator operator_, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Constant &constant, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Function &function, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const In &in, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Integer &integer, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Interval &interval, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Predicate &predicate, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const SpecialInteger &specialInteger, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const String &string, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const UnaryOperation &unaryOperation, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Variable &variable, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext, bool omitParentheses = false);
|
||||
|
||||
output::ColorStream &print(output::ColorStream &stream, const And &and_, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Biconditional &biconditional, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Exists &exists, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const ForAll &forAll, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Implies &implies, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Not ¬_, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Or &or_, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const And &and_, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Biconditional &biconditional, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Exists &exists, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const ForAll &forAll, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Implies &implies, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Not ¬_, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Or &or_, PrintContext &printContext, bool omitParentheses = false);
|
||||
|
||||
output::ColorStream &print(output::ColorStream &stream, const Formula &formula, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Term &term, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Formula &formula, PrintContext &printContext, bool omitParentheses = false);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Term &term, PrintContext &printContext, bool omitParentheses = false);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Primitives
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, BinaryOperation::Operator operator_, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, BinaryOperation::Operator operator_, PrintContext &, bool)
|
||||
{
|
||||
switch (operator_)
|
||||
{
|
||||
@ -95,14 +96,18 @@ inline output::ColorStream &print(output::ColorStream &stream, BinaryOperation::
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const BinaryOperation &binaryOperation, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const BinaryOperation &binaryOperation, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
|
||||
print(stream, binaryOperation.left, printContext);
|
||||
stream << " ";
|
||||
print(stream, binaryOperation.operator_, printContext);
|
||||
stream << " ";
|
||||
print(stream, binaryOperation.right, printContext);
|
||||
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << ")";
|
||||
|
||||
return stream;
|
||||
@ -110,7 +115,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const BinaryOpera
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Boolean &boolean, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Boolean &boolean, PrintContext &, bool)
|
||||
{
|
||||
if (boolean.value)
|
||||
return (stream << output::Boolean("#true"));
|
||||
@ -120,7 +125,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Boolean &bo
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, Comparison::Operator operator_, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, Comparison::Operator operator_, PrintContext &, bool)
|
||||
{
|
||||
switch (operator_)
|
||||
{
|
||||
@ -143,7 +148,7 @@ inline output::ColorStream &print(output::ColorStream &stream, Comparison::Opera
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Comparison &comparison, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Comparison &comparison, PrintContext &printContext, bool)
|
||||
{
|
||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
@ -162,14 +167,14 @@ inline output::ColorStream &print(output::ColorStream &stream, const Comparison
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Constant &constant, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Constant &constant, PrintContext &, bool)
|
||||
{
|
||||
return (stream << constant.name);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Function &function, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Function &function, PrintContext &printContext, bool)
|
||||
{
|
||||
stream << function.name;
|
||||
|
||||
@ -196,7 +201,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Function &f
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const In &in, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const In &in, PrintContext &printContext, bool)
|
||||
{
|
||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
@ -213,14 +218,14 @@ inline output::ColorStream &print(output::ColorStream &stream, const In &in, Pri
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Integer &integer, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Integer &integer, PrintContext &, bool)
|
||||
{
|
||||
return (stream << output::Number<int>(integer.value));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Interval &interval, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Interval &interval, PrintContext &printContext, bool)
|
||||
{
|
||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
@ -237,7 +242,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Interval &i
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Predicate &predicate, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Predicate &predicate, PrintContext &printContext, bool)
|
||||
{
|
||||
stream << predicate.name;
|
||||
|
||||
@ -261,7 +266,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Predicate &
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const SpecialInteger &specialInteger, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const SpecialInteger &specialInteger, PrintContext &, bool)
|
||||
{
|
||||
switch (specialInteger.type)
|
||||
{
|
||||
@ -276,14 +281,14 @@ inline output::ColorStream &print(output::ColorStream &stream, const SpecialInte
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const String &string, PrintContext &)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const String &string, PrintContext &, bool)
|
||||
{
|
||||
return (stream << output::String(string.text.c_str()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const UnaryOperation &unaryOperation, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const UnaryOperation &unaryOperation, PrintContext &printContext, bool)
|
||||
{
|
||||
switch (unaryOperation.operator_)
|
||||
{
|
||||
@ -292,7 +297,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const UnaryOperat
|
||||
break;
|
||||
}
|
||||
|
||||
print(stream, unaryOperation.argument, printContext);
|
||||
print(stream, unaryOperation.argument, printContext, true);
|
||||
|
||||
switch (unaryOperation.operator_)
|
||||
{
|
||||
@ -306,7 +311,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const UnaryOperat
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Variable &variable, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Variable &variable, PrintContext &printContext, bool)
|
||||
{
|
||||
assert(variable.declaration != nullptr);
|
||||
|
||||
@ -315,7 +320,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Variable &v
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext, bool)
|
||||
{
|
||||
const auto printVariableDeclaration =
|
||||
[&stream, &variableDeclaration](const auto *prefix, auto &variableIDs) -> output::ColorStream &
|
||||
@ -351,8 +356,9 @@ inline output::ColorStream &print(output::ColorStream &stream, const VariableDec
|
||||
// Expressions
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const And &and_, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const And &and_, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
|
||||
for (auto i = and_.arguments.cbegin(); i != and_.arguments.cend(); i++)
|
||||
@ -363,6 +369,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const And &and_,
|
||||
print(stream, *i, printContext);
|
||||
}
|
||||
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << ")";
|
||||
|
||||
return stream;
|
||||
@ -370,12 +377,16 @@ inline output::ColorStream &print(output::ColorStream &stream, const And &and_,
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Biconditional &biconditional, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Biconditional &biconditional, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
|
||||
print(stream, biconditional.left, printContext);
|
||||
stream << " <-> ";
|
||||
print(stream, biconditional.right, printContext);
|
||||
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << ")";
|
||||
|
||||
return stream;
|
||||
@ -383,7 +394,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Bicondition
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Exists &exists, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Exists &exists, PrintContext &printContext, bool)
|
||||
{
|
||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
@ -409,7 +420,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Exists &exi
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const ForAll &forAll, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const ForAll &forAll, PrintContext &printContext, bool)
|
||||
{
|
||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
@ -435,12 +446,16 @@ inline output::ColorStream &print(output::ColorStream &stream, const ForAll &for
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Implies &implies, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Implies &implies, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
|
||||
print(stream, implies.antecedent, printContext);
|
||||
stream << " -> ";
|
||||
print(stream, implies.consequent, printContext);
|
||||
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << ")";
|
||||
|
||||
return stream;
|
||||
@ -448,7 +463,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Implies &im
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Not ¬_, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Not ¬_, PrintContext &printContext, bool)
|
||||
{
|
||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
@ -464,8 +479,9 @@ inline output::ColorStream &print(output::ColorStream &stream, const Not ¬_,
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Or &or_, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Or &or_, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << "(";
|
||||
|
||||
for (auto i = or_.arguments.cbegin(); i != or_.arguments.cend(); i++)
|
||||
@ -476,6 +492,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Or &or_, Pr
|
||||
print(stream, *i, printContext);
|
||||
}
|
||||
|
||||
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||
stream << ")";
|
||||
|
||||
return stream;
|
||||
@ -489,24 +506,24 @@ template<class Variant>
|
||||
struct VariantPrintVisitor
|
||||
{
|
||||
template<class T>
|
||||
output::ColorStream &visit(const T &x, output::ColorStream &stream, PrintContext &printContext)
|
||||
output::ColorStream &visit(const T &x, output::ColorStream &stream, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
return print(stream, x, printContext);
|
||||
return print(stream, x, printContext, omitParentheses);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Formula &formula, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Formula &formula, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
return formula.accept(VariantPrintVisitor<Formula>(), stream, printContext);
|
||||
return formula.accept(VariantPrintVisitor<Formula>(), stream, printContext, omitParentheses);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Term &term, PrintContext &printContext)
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Term &term, PrintContext &printContext, bool omitParentheses)
|
||||
{
|
||||
return term.accept(VariantPrintVisitor<Term>(), stream, printContext);
|
||||
return term.accept(VariantPrintVisitor<Term>(), stream, printContext, omitParentheses);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -190,6 +190,6 @@ TEST_CASE("[completion] Rules are completed", "[completion]")
|
||||
input << "adj(X, Y) :- X = 1..n, Y = 1..n, |X - Y| = 1.";
|
||||
anthem::translate("input", input, context);
|
||||
|
||||
CHECK(output.str() == "forall V1, V2 (adj(V1, V2) <-> (V1 in 1..n and V2 in 1..n and |(V1 - V2)| = 1))\n");
|
||||
CHECK(output.str() == "forall V1, V2 (adj(V1, V2) <-> (V1 in 1..n and V2 in 1..n and |V1 - V2| = 1))\n");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user