Respect operator precedence when printing
This commit is contained in:
408
src/format.rs
408
src/format.rs
@@ -1,3 +1,34 @@
|
||||
struct TermDisplay<'term>
|
||||
{
|
||||
parent_precedence: u64,
|
||||
term: &'term crate::Term,
|
||||
}
|
||||
|
||||
struct FormulaDisplay<'formula>
|
||||
{
|
||||
parent_precedence: u64,
|
||||
formula: &'formula crate::Formula,
|
||||
}
|
||||
|
||||
fn display_term<'term>(term: &'term crate::Term, parent_precedence: u64) -> TermDisplay<'term>
|
||||
{
|
||||
TermDisplay
|
||||
{
|
||||
parent_precedence,
|
||||
term,
|
||||
}
|
||||
}
|
||||
|
||||
fn display_formula<'formula>(formula: &'formula crate::Formula, parent_precedence: u64)
|
||||
-> FormulaDisplay<'formula>
|
||||
{
|
||||
FormulaDisplay
|
||||
{
|
||||
parent_precedence,
|
||||
formula,
|
||||
}
|
||||
}
|
||||
|
||||
fn term_precedence(term: &crate::Term) -> u64
|
||||
{
|
||||
match term
|
||||
@@ -51,11 +82,19 @@ impl std::fmt::Display for crate::VariableDeclaration
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for crate::Term
|
||||
impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
match *self
|
||||
let precedence = term_precedence(self.term);
|
||||
let requires_parentheses = precedence > self.parent_precedence;
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
write!(format, "(")?;
|
||||
}
|
||||
|
||||
match self.term
|
||||
{
|
||||
crate::Term::Infimum => write!(format, "#inf"),
|
||||
crate::Term::Supremum => write!(format, "#sup"),
|
||||
@@ -63,31 +102,146 @@ impl std::fmt::Debug for crate::Term
|
||||
crate::Term::Symbolic(ref value) => write!(format, "{}", value),
|
||||
crate::Term::String(ref value) => write!(format, "\"{}\"", value),
|
||||
crate::Term::Variable(ref declaration) => write!(format, "{:?}", declaration),
|
||||
crate::Term::Add(ref left, ref right) => write!(format, "({:?} + {:?})", left, right),
|
||||
crate::Term::Subtract(ref left, ref right) => write!(format, "({:?} - {:?})", left, right),
|
||||
crate::Term::Multiply(ref left, ref right) => write!(format, "({:?} * {:?})", left, right),
|
||||
crate::Term::Negative(ref argument) => write!(format, "-({:?})", argument),
|
||||
crate::Term::Add(ref left, ref right) => write!(format, "{:?} + {:?}", display_term(left, precedence), display_term(right, precedence)),
|
||||
crate::Term::Subtract(ref left, ref right) => write!(format, "{:?} - {:?}", display_term(left, precedence), display_term(right, precedence)),
|
||||
crate::Term::Multiply(ref left, ref right) => write!(format, "{:?} * {:?}", display_term(left, precedence), display_term(right, precedence)),
|
||||
crate::Term::Negative(ref argument) => write!(format, "-{:?}", display_term(argument, precedence)),
|
||||
}?;
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
write!(format, ")")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for crate::Term
|
||||
impl<'term> std::fmt::Display for TermDisplay<'term>
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
match *self
|
||||
write!(format, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
let precedence = formula_precedence(self.formula);
|
||||
let requires_parentheses = precedence > self.parent_precedence;
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
crate::Term::Infimum => write!(format, "#inf"),
|
||||
crate::Term::Supremum => write!(format, "#sup"),
|
||||
crate::Term::Integer(value) => write!(format, "{}", value),
|
||||
crate::Term::Symbolic(ref value) => write!(format, "{}", value),
|
||||
crate::Term::String(ref value) => write!(format, "\"{}\"", value),
|
||||
crate::Term::Variable(ref declaration) => write!(format, "{}", declaration),
|
||||
crate::Term::Add(ref left, ref right) => write!(format, "({} + {})", left, right),
|
||||
crate::Term::Subtract(ref left, ref right) => write!(format, "({} - {})", left, right),
|
||||
crate::Term::Multiply(ref left, ref right) => write!(format, "({} * {})", left, right),
|
||||
crate::Term::Negative(ref argument) => write!(format, "-({})", argument),
|
||||
write!(format, "(")?;
|
||||
}
|
||||
|
||||
match self.formula
|
||||
{
|
||||
crate::Formula::Exists(ref exists) =>
|
||||
{
|
||||
write!(format, "exists")?;
|
||||
|
||||
let mut separator = " ";
|
||||
|
||||
for parameter in &exists.parameters
|
||||
{
|
||||
write!(format, "{}{:?}", separator, parameter)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " {:?}", display_formula(&exists.argument, precedence))?;
|
||||
},
|
||||
crate::Formula::ForAll(ref for_all) =>
|
||||
{
|
||||
write!(format, "forall")?;
|
||||
|
||||
let mut separator = " ";
|
||||
|
||||
for parameter in &for_all.parameters
|
||||
{
|
||||
write!(format, "{}{:?}", separator, parameter)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " {:?}", display_formula(&for_all.argument, precedence))?;
|
||||
},
|
||||
crate::Formula::Not(ref argument) => write!(format, "not {:?}", display_formula(argument, precedence))?,
|
||||
crate::Formula::And(ref arguments) =>
|
||||
{
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_formula(argument, precedence))?;
|
||||
|
||||
separator = " and "
|
||||
}
|
||||
},
|
||||
crate::Formula::Or(ref arguments) =>
|
||||
{
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_formula(argument, precedence))?;
|
||||
|
||||
separator = " or "
|
||||
}
|
||||
},
|
||||
crate::Formula::Implies(ref left, ref right) => write!(format, "{:?} -> {:?}", display_formula(left, precedence), display_formula(right, precedence))?,
|
||||
crate::Formula::Biconditional(ref left, ref right) => write!(format, "{:?} <-> {:?}", display_formula(left, precedence), display_formula(right, precedence))?,
|
||||
crate::Formula::Less(ref left, ref right) => write!(format, "{:?} < {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::LessOrEqual(ref left, ref right) => write!(format, "{:?} <= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::Greater(ref left, ref right) => write!(format, "{:?} > {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::GreaterOrEqual(ref left, ref right) => write!(format, "{:?} >= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::Equal(ref left, ref right) => write!(format, "{:?} = {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::NotEqual(ref left, ref right) => write!(format, "{:?} != {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::Boolean(value) =>
|
||||
match value
|
||||
{
|
||||
true => write!(format, "#true")?,
|
||||
false => write!(format, "#false")?,
|
||||
},
|
||||
crate::Formula::Predicate(ref predicate) =>
|
||||
{
|
||||
write!(format, "{}", predicate.declaration.name)?;
|
||||
|
||||
if !predicate.arguments.is_empty()
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in &predicate.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_term(argument, 1000))?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, ")")?;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
write!(format, ")")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'formula> std::fmt::Display for FormulaDisplay<'formula>
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,106 +249,7 @@ impl std::fmt::Debug for crate::Formula
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
match *self
|
||||
{
|
||||
crate::Formula::Exists(ref exists) =>
|
||||
{
|
||||
write!(format, "exists")?;
|
||||
|
||||
let mut separator = " ";
|
||||
|
||||
for parameter in &exists.parameters
|
||||
{
|
||||
write!(format, "{}{:?}", separator, parameter)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " ({:?})", exists.argument)
|
||||
},
|
||||
crate::Formula::ForAll(ref for_all) =>
|
||||
{
|
||||
write!(format, "forall")?;
|
||||
|
||||
let mut separator = " ";
|
||||
|
||||
for parameter in &for_all.parameters
|
||||
{
|
||||
write!(format, "{}{:?}", separator, parameter)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " ({:?})", for_all.argument)
|
||||
},
|
||||
crate::Formula::Not(ref argument) => write!(format, "not {:?}", argument),
|
||||
crate::Formula::And(ref arguments) =>
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, argument)?;
|
||||
|
||||
separator = " and "
|
||||
}
|
||||
|
||||
write!(format, ")")
|
||||
},
|
||||
crate::Formula::Or(ref arguments) =>
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, argument)?;
|
||||
|
||||
separator = " or "
|
||||
}
|
||||
|
||||
write!(format, ")")
|
||||
},
|
||||
crate::Formula::Implies(ref left, ref right) => write!(format, "({:?} -> {:?})", left, right),
|
||||
crate::Formula::Biconditional(ref left, ref right) => write!(format, "({:?} <-> {:?})", left, right),
|
||||
crate::Formula::Less(ref left, ref right) => write!(format, "({:?} < {:?})", left, right),
|
||||
crate::Formula::LessOrEqual(ref left, ref right) => write!(format, "({:?} <= {:?})", left, right),
|
||||
crate::Formula::Greater(ref left, ref right) => write!(format, "({:?} > {:?})", left, right),
|
||||
crate::Formula::GreaterOrEqual(ref left, ref right) => write!(format, "({:?} >= {:?})", left, right),
|
||||
crate::Formula::Equal(ref left, ref right) => write!(format, "({:?} = {:?})", left, right),
|
||||
crate::Formula::NotEqual(ref left, ref right) => write!(format, "({:?} != {:?})", left, right),
|
||||
crate::Formula::Boolean(value) =>
|
||||
match value
|
||||
{
|
||||
true => write!(format, "#true"),
|
||||
false => write!(format, "#false"),
|
||||
},
|
||||
crate::Formula::Predicate(ref predicate) =>
|
||||
{
|
||||
write!(format, "{}", predicate.declaration.name)?;
|
||||
|
||||
if !predicate.arguments.is_empty()
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in &predicate.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, argument)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, ")")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
write!(format, "{:?}", display_formula(&self, 1000))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,105 +257,22 @@ impl std::fmt::Display for crate::Formula
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
match *self
|
||||
{
|
||||
crate::Formula::Exists(ref exists) =>
|
||||
{
|
||||
write!(format, "exists")?;
|
||||
|
||||
let mut separator = " ";
|
||||
|
||||
for parameter in &exists.parameters
|
||||
{
|
||||
write!(format, "{}{}", separator, parameter)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " ({})", exists.argument)
|
||||
},
|
||||
crate::Formula::ForAll(ref for_all) =>
|
||||
{
|
||||
write!(format, "forall")?;
|
||||
|
||||
let mut separator = " ";
|
||||
|
||||
for parameter in &for_all.parameters
|
||||
{
|
||||
write!(format, "{}{}", separator, parameter)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " ({})", for_all.argument)
|
||||
},
|
||||
crate::Formula::Not(ref argument) => write!(format, "not {}", argument),
|
||||
crate::Formula::And(ref arguments) =>
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{}", separator, argument)?;
|
||||
|
||||
separator = " and "
|
||||
}
|
||||
|
||||
write!(format, ")")
|
||||
},
|
||||
crate::Formula::Or(ref arguments) =>
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{}", separator, argument)?;
|
||||
|
||||
separator = " or "
|
||||
}
|
||||
|
||||
write!(format, ")")
|
||||
},
|
||||
crate::Formula::Implies(ref left, ref right) => write!(format, "({} -> {})", left, right),
|
||||
crate::Formula::Biconditional(ref left, ref right) => write!(format, "({} <-> {})", left, right),
|
||||
crate::Formula::Less(ref left, ref right) => write!(format, "({} < {})", left, right),
|
||||
crate::Formula::LessOrEqual(ref left, ref right) => write!(format, "({} <= {})", left, right),
|
||||
crate::Formula::Greater(ref left, ref right) => write!(format, "({} > {})", left, right),
|
||||
crate::Formula::GreaterOrEqual(ref left, ref right) => write!(format, "({} >= {})", left, right),
|
||||
crate::Formula::Equal(ref left, ref right) => write!(format, "({} = {})", left, right),
|
||||
crate::Formula::NotEqual(ref left, ref right) => write!(format, "({} != {})", left, right),
|
||||
crate::Formula::Boolean(value) =>
|
||||
match value
|
||||
{
|
||||
true => write!(format, "#true"),
|
||||
false => write!(format, "#false"),
|
||||
},
|
||||
crate::Formula::Predicate(ref predicate) =>
|
||||
{
|
||||
write!(format, "{}", predicate.declaration.name)?;
|
||||
|
||||
if !predicate.arguments.is_empty()
|
||||
{
|
||||
write!(format, "(")?;
|
||||
|
||||
let mut separator = "";
|
||||
|
||||
for argument in &predicate.arguments
|
||||
{
|
||||
write!(format, "{}{}", separator, argument)?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, ")")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
write!(format, "{}", display_formula(&self, 1000))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", display_term(&self, 1000))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{}", display_term(&self, 1000))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user