Refactor parenthesis requirement check

This commit is contained in:
Patrick Lühne 2020-04-06 21:45:55 +02:00
parent 80d7460ec1
commit e0dbb8b75f
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -1,12 +1,52 @@
use super::*; use super::*;
fn requires_parentheses<'formula>(formula: &'formula crate::Formula, impl std::fmt::Debug for crate::ImplicationDirection
parent_formula: &'formula crate::Formula) {
-> bool fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
match &self
{
Self::LeftToRight => write!(format, "left to right"),
Self::RightToLeft => write!(format, "right to left"),
}
}
}
impl std::fmt::Debug for crate::PredicateDeclaration
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(format, "{}/{}", &self.name, self.arity)
}
}
impl std::fmt::Display for crate::PredicateDeclaration
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(format, "{:?}", &self)
}
}
struct FormulaDisplay<'formula>
{
formula: &'formula crate::Formula,
parent_formula: Option<&'formula crate::Formula>,
}
impl<'formula> FormulaDisplay<'formula>
{
fn requires_parentheses(&self) -> bool
{ {
use crate::Formula; use crate::Formula;
match formula let parent_formula = match self.parent_formula
{
Some(parent_formula) => parent_formula,
None => return false,
};
match self.formula
{ {
Formula::Predicate(_) Formula::Predicate(_)
| Formula::Boolean(_) | Formula::Boolean(_)
@ -44,13 +84,13 @@ fn requires_parentheses<'formula>(formula: &'formula crate::Formula,
| Formula::And(_) | Formula::And(_)
| Formula::Or(_) | Formula::Or(_)
=> true, => true,
Formula::Implies( Formula::Implies(crate::Implies{direction: parent_direction,
crate::Implies{direction: parent_direction, antecedent: parent_antecedent, ..}) => antecedent: parent_antecedent, ..}) =>
if direction == parent_direction if direction == parent_direction
{ {
// Implications with the same direction nested on the antecedent side require // Implications with the same direction nested on the antecedent side require
// parentheses because implication is considered right-associative // parentheses because implication is considered right-associative
std::ptr::eq(formula, &**parent_antecedent) std::ptr::eq(self.formula, &**parent_antecedent)
} }
else else
{ {
@ -71,39 +111,6 @@ fn requires_parentheses<'formula>(formula: &'formula crate::Formula,
}, },
} }
} }
impl std::fmt::Debug for crate::ImplicationDirection
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
match &self
{
Self::LeftToRight => write!(format, "left to right"),
Self::RightToLeft => write!(format, "right to left"),
}
}
}
impl std::fmt::Debug for crate::PredicateDeclaration
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(format, "{}/{}", &self.name, self.arity)
}
}
impl std::fmt::Display for crate::PredicateDeclaration
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(format, "{:?}", &self)
}
}
struct FormulaDisplay<'formula>
{
formula: &'formula crate::Formula,
parent_formula: Option<&'formula crate::Formula>,
} }
fn display_formula<'formula>(formula: &'formula crate::Formula, fn display_formula<'formula>(formula: &'formula crate::Formula,
@ -121,11 +128,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
{ {
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{ {
let requires_parentheses = match self.parent_formula let requires_parentheses = self.requires_parentheses();
{
Some(ref parent_formula) => requires_parentheses(self.formula, parent_formula),
None => false,
};
if requires_parentheses if requires_parentheses
{ {