Represent parent precedence with optional type
This commit is contained in:
parent
5e96c3f85e
commit
88274e4396
@ -1,10 +1,11 @@
|
||||
struct TermDisplay<'term>
|
||||
{
|
||||
parent_precedence: i32,
|
||||
parent_precedence: Option<i32>,
|
||||
term: &'term crate::Term,
|
||||
}
|
||||
|
||||
fn display_term<'term>(term: &'term crate::Term, parent_precedence: i32) -> TermDisplay<'term>
|
||||
fn display_term<'term>(term: &'term crate::Term, parent_precedence: Option<i32>)
|
||||
-> TermDisplay<'term>
|
||||
{
|
||||
TermDisplay
|
||||
{
|
||||
@ -15,11 +16,11 @@ fn display_term<'term>(term: &'term crate::Term, parent_precedence: i32) -> Term
|
||||
|
||||
struct FormulaDisplay<'formula>
|
||||
{
|
||||
parent_precedence: i32,
|
||||
parent_precedence: Option<i32>,
|
||||
formula: &'formula crate::Formula,
|
||||
}
|
||||
|
||||
fn display_formula<'formula>(formula: &'formula crate::Formula, parent_precedence: i32)
|
||||
fn display_formula<'formula>(formula: &'formula crate::Formula, parent_precedence: Option<i32>)
|
||||
-> FormulaDisplay<'formula>
|
||||
{
|
||||
FormulaDisplay
|
||||
@ -112,7 +113,12 @@ impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
let precedence = self.term.precedence();
|
||||
let requires_parentheses = precedence > self.parent_precedence;
|
||||
let requires_parentheses = match self.parent_precedence
|
||||
{
|
||||
Some(parent_precedence) => precedence > parent_precedence,
|
||||
None => false,
|
||||
};
|
||||
let precedence = Some(precedence);
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
@ -144,7 +150,7 @@ impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
|
||||
for argument in &function.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_term(&argument, 1000))?;
|
||||
write!(format, "{}{:?}", separator, display_term(&argument, None))?;
|
||||
|
||||
separator = ", ";
|
||||
}
|
||||
@ -194,7 +200,12 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
let precedence = self.formula.precedence();
|
||||
let requires_parentheses = precedence > self.parent_precedence;
|
||||
let requires_parentheses = match self.parent_precedence
|
||||
{
|
||||
Some(parent_precedence) => precedence > parent_precedence,
|
||||
None => false,
|
||||
};
|
||||
let precedence = Some(precedence);
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
@ -269,17 +280,17 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
crate::Formula::IfAndOnlyIf(crate::IfAndOnlyIf{left, right})
|
||||
=> write!(format, "{:?} <-> {:?}", display_formula(left, precedence), display_formula(right, precedence))?,
|
||||
crate::Formula::Compare(crate::Compare{operator: crate::ComparisonOperator::Less, left, right})
|
||||
=> write!(format, "{:?} < {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
=> write!(format, "{:?} < {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Compare(crate::Compare{operator: crate::ComparisonOperator::LessOrEqual, left, right})
|
||||
=> write!(format, "{:?} <= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
=> write!(format, "{:?} <= {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Compare(crate::Compare{operator: crate::ComparisonOperator::Greater, left, right})
|
||||
=> write!(format, "{:?} > {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
=> write!(format, "{:?} > {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Compare(crate::Compare{operator: crate::ComparisonOperator::GreaterOrEqual, left, right})
|
||||
=> write!(format, "{:?} >= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
=> write!(format, "{:?} >= {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Compare(crate::Compare{operator: crate::ComparisonOperator::Equal, left, right})
|
||||
=> write!(format, "{:?} = {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
=> write!(format, "{:?} = {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Compare(crate::Compare{operator: crate::ComparisonOperator::NotEqual, left, right})
|
||||
=> write!(format, "{:?} != {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
=> write!(format, "{:?} != {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Boolean(true) => write!(format, "#true")?,
|
||||
crate::Formula::Boolean(false) => write!(format, "#false")?,
|
||||
crate::Formula::Predicate(predicate) =>
|
||||
@ -294,7 +305,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
|
||||
for argument in &predicate.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_term(argument, 1000))?;
|
||||
write!(format, "{}{:?}", separator, display_term(argument, None))?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
@ -325,7 +336,7 @@ impl std::fmt::Debug for crate::Formula
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", display_formula(&self, 1000))
|
||||
write!(format, "{:?}", display_formula(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,7 +344,7 @@ impl std::fmt::Display for crate::Formula
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{}", display_formula(&self, 1000))
|
||||
write!(format, "{}", display_formula(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
@ -341,7 +352,7 @@ impl std::fmt::Debug for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", display_term(&self, 1000))
|
||||
write!(format, "{:?}", display_term(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
@ -349,6 +360,6 @@ impl std::fmt::Display for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{}", display_term(&self, 1000))
|
||||
write!(format, "{}", display_term(&self, None))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user