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::*;
fn requires_parentheses<'formula>(formula: &'formula crate::Formula,
parent_formula: &'formula crate::Formula)
-> bool
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>,
}
impl<'formula> FormulaDisplay<'formula>
{
fn requires_parentheses(&self) -> bool
{
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::Boolean(_)
@ -44,13 +84,13 @@ fn requires_parentheses<'formula>(formula: &'formula crate::Formula,
| Formula::And(_)
| Formula::Or(_)
=> true,
Formula::Implies(
crate::Implies{direction: parent_direction, antecedent: parent_antecedent, ..}) =>
Formula::Implies(crate::Implies{direction: parent_direction,
antecedent: parent_antecedent, ..}) =>
if direction == parent_direction
{
// Implications with the same direction nested on the antecedent side require
// parentheses because implication is considered right-associative
std::ptr::eq(formula, &**parent_antecedent)
std::ptr::eq(self.formula, &**parent_antecedent)
}
else
{
@ -70,40 +110,7 @@ fn requires_parentheses<'formula>(formula: &'formula crate::Formula,
_ => false,
},
}
}
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,
@ -121,11 +128,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
{
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
{
let requires_parentheses = match self.parent_formula
{
Some(ref parent_formula) => requires_parentheses(self.formula, parent_formula),
None => false,
};
let requires_parentheses = self.requires_parentheses();
if requires_parentheses
{