Remove unneeded parentheses enum
This commit is contained in:
parent
6c326ddb70
commit
7b2b292727
@ -2,16 +2,3 @@ mod formulas;
|
||||
mod terms;
|
||||
|
||||
pub(crate) use terms::*;
|
||||
|
||||
trait Precedence
|
||||
{
|
||||
fn precedence_level(&self) -> i32;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
pub(crate) enum Parentheses
|
||||
{
|
||||
None,
|
||||
PrecedenceBased(i32),
|
||||
Required,
|
||||
}
|
||||
|
@ -293,30 +293,37 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
}
|
||||
}
|
||||
},
|
||||
// TODO: refactor
|
||||
crate::Formula::Compare(
|
||||
crate::Compare{operator: crate::ComparisonOperator::Less, left, right})
|
||||
=> write!(format, "{:?} < {:?}", display_term(left, Parentheses::None, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?,
|
||||
=> write!(format, "{:?} < {:?}",
|
||||
display_term(left, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, None, crate::format::terms::ChildPosition::Any))?,
|
||||
crate::Formula::Compare(
|
||||
crate::Compare{operator: crate::ComparisonOperator::LessOrEqual, left, right})
|
||||
=> write!(format, "{:?} <= {:?}", display_term(left, Parentheses::None, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?,
|
||||
=> write!(format, "{:?} <= {:?}",
|
||||
display_term(left, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, None, crate::format::terms::ChildPosition::Any))?,
|
||||
crate::Formula::Compare(
|
||||
crate::Compare{operator: crate::ComparisonOperator::Greater, left, right})
|
||||
=> write!(format, "{:?} > {:?}", display_term(left, Parentheses::None, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?,
|
||||
=> write!(format, "{:?} > {:?}",
|
||||
display_term(left, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, None, crate::format::terms::ChildPosition::Any))?,
|
||||
crate::Formula::Compare(
|
||||
crate::Compare{operator: crate::ComparisonOperator::GreaterOrEqual, left, right})
|
||||
=> write!(format, "{:?} >= {:?}", display_term(left, Parentheses::None, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?,
|
||||
=> write!(format, "{:?} >= {:?}",
|
||||
display_term(left, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, None, crate::format::terms::ChildPosition::Any))?,
|
||||
crate::Formula::Compare(
|
||||
crate::Compare{operator: crate::ComparisonOperator::Equal, left, right})
|
||||
=> write!(format, "{:?} = {:?}", display_term(left, Parentheses::None, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?,
|
||||
=> write!(format, "{:?} = {:?}",
|
||||
display_term(left, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, None, crate::format::terms::ChildPosition::Any))?,
|
||||
crate::Formula::Compare(
|
||||
crate::Compare{operator: crate::ComparisonOperator::NotEqual, left, right})
|
||||
=> write!(format, "{:?} != {:?}", display_term(left, Parentheses::None, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?,
|
||||
=> write!(format, "{:?} != {:?}",
|
||||
display_term(left, None, crate::format::terms::ChildPosition::Any),
|
||||
display_term(right, None, crate::format::terms::ChildPosition::Any))?,
|
||||
crate::Formula::Boolean(true) => write!(format, "true")?,
|
||||
crate::Formula::Boolean(false) => write!(format, "false")?,
|
||||
crate::Formula::Predicate(predicate) =>
|
||||
@ -331,8 +338,8 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
|
||||
for argument in &predicate.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator,
|
||||
display_term(argument, Parentheses::None, None, crate::format::terms::ChildPosition::Any))?;
|
||||
write!(format, "{}{:?}", separator, display_term(argument, None,
|
||||
crate::format::terms::ChildPosition::Any))?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
pub(crate) enum ChildPosition
|
||||
{
|
||||
@ -8,42 +6,6 @@ pub(crate) enum ChildPosition
|
||||
Right,
|
||||
}
|
||||
|
||||
impl super::Precedence for crate::Term
|
||||
{
|
||||
fn precedence_level(&self) -> i32
|
||||
{
|
||||
match &self
|
||||
{
|
||||
Self::Boolean(_)
|
||||
| Self::SpecialInteger(_)
|
||||
| Self::Integer(_)
|
||||
| Self::String(_)
|
||||
| Self::Variable(_)
|
||||
| Self::Function(_)
|
||||
| Self::UnaryOperation(
|
||||
crate::UnaryOperation{operator: crate::UnaryOperator::AbsoluteValue, ..})
|
||||
=> 0,
|
||||
Self::UnaryOperation(
|
||||
crate::UnaryOperation{operator: crate::UnaryOperator::Negative, ..})
|
||||
=> 1,
|
||||
Self::BinaryOperation(
|
||||
crate::BinaryOperation{operator: crate::BinaryOperator::Exponentiate, ..})
|
||||
=> 2,
|
||||
Self::BinaryOperation(
|
||||
crate::BinaryOperation{operator: crate::BinaryOperator::Multiply, ..})
|
||||
| Self::BinaryOperation(
|
||||
crate::BinaryOperation{operator: crate::BinaryOperator::Divide, ..})
|
||||
| Self::BinaryOperation(
|
||||
crate::BinaryOperation{operator: crate::BinaryOperator::Modulo, ..})
|
||||
=> 3,
|
||||
Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Add, ..})
|
||||
| Self::BinaryOperation(
|
||||
crate::BinaryOperation{operator: crate::BinaryOperator::Subtract, ..})
|
||||
=> 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for crate::SpecialInteger
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
@ -98,7 +60,6 @@ impl std::fmt::Display for crate::VariableDeclaration
|
||||
|
||||
pub(crate) struct TermDisplay<'term>
|
||||
{
|
||||
parentheses: Parentheses,
|
||||
term: &'term crate::Term,
|
||||
parent_term: Option<&'term crate::Term>,
|
||||
position: ChildPosition,
|
||||
@ -188,12 +149,12 @@ impl<'term> TermDisplay<'term>
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn display_term<'term>(term: &'term crate::Term, parentheses: Parentheses,
|
||||
parent_term: Option<&'term crate::Term>, position: ChildPosition) -> TermDisplay<'term>
|
||||
pub(crate) fn display_term<'term>(term: &'term crate::Term, parent_term: Option<&'term crate::Term>,
|
||||
position: ChildPosition)
|
||||
-> TermDisplay<'term>
|
||||
{
|
||||
TermDisplay
|
||||
{
|
||||
parentheses,
|
||||
term,
|
||||
parent_term,
|
||||
position,
|
||||
@ -237,7 +198,7 @@ impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
for argument in &function.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator,
|
||||
display_term(&argument, Parentheses::None, Some(self.term), ChildPosition::Any))?;
|
||||
display_term(&argument, Some(self.term), ChildPosition::Any))?;
|
||||
|
||||
separator = ", ";
|
||||
}
|
||||
@ -260,16 +221,18 @@ impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
};
|
||||
|
||||
write!(format, "{:?} {} {:?}",
|
||||
display_term(&binary_operation.left, Parentheses::None, Some(self.term), ChildPosition::Left),
|
||||
display_term(&binary_operation.left, Some(self.term), ChildPosition::Left),
|
||||
operator_string,
|
||||
display_term(&binary_operation.right, Parentheses::None, Some(self.term), ChildPosition::Right))
|
||||
display_term(&binary_operation.right, Some(self.term), ChildPosition::Right))
|
||||
},
|
||||
crate::Term::UnaryOperation(
|
||||
crate::UnaryOperation{operator: crate::UnaryOperator::Negative, argument})
|
||||
=> write!(format, "-{:?}", display_term(argument, Parentheses::None, Some(self.term), ChildPosition::Any)),
|
||||
=> write!(format, "-{:?}",
|
||||
display_term(argument, Some(self.term), ChildPosition::Any)),
|
||||
crate::Term::UnaryOperation(
|
||||
crate::UnaryOperation{operator: crate::UnaryOperator::AbsoluteValue, argument})
|
||||
=> write!(format, "|{:?}|", display_term(argument, Parentheses::None, Some(self.term), ChildPosition::Any)),
|
||||
=> write!(format, "|{:?}|",
|
||||
display_term(argument, Some(self.term), ChildPosition::Any)),
|
||||
}?;
|
||||
|
||||
if requires_parentheses
|
||||
@ -293,7 +256,7 @@ impl std::fmt::Debug for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", display_term(&self, Parentheses::None, None, ChildPosition::Any))
|
||||
write!(format, "{:?}", display_term(&self, None, ChildPosition::Any))
|
||||
}
|
||||
}
|
||||
|
||||
@ -301,7 +264,7 @@ impl std::fmt::Display for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{}", display_term(&self, Parentheses::None, None, ChildPosition::Any))
|
||||
write!(format, "{}", display_term(&self, None, ChildPosition::Any))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user