use super::Arity;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LogicalOperator {
And,
Or,
Not,
}
impl LogicalOperator {
pub const ALL: [Self; 3] = [Self::And, Self::Or, Self::Not];
#[must_use]
pub const fn symbol(self) -> &'static str {
match self {
Self::And => "&&",
Self::Or => "||",
Self::Not => "!",
}
}
#[must_use]
pub const fn arity(self) -> Arity {
match self {
Self::Not => Arity::Unary,
_ => Arity::Binary,
}
}
#[must_use]
pub const fn precedence(self) -> u8 {
match self {
Self::Not => 1,
Self::And => 9,
Self::Or => 10,
}
}
}
#[cfg(test)]
mod tests {
use super::LogicalOperator;
#[test]
fn and_binds_tighter_than_or() {
assert!(LogicalOperator::And.precedence() < LogicalOperator::Or.precedence());
}
#[test]
fn logical_operators_bind_looser_than_every_bitwise_one() {
use crate::expression::BitwiseOperator;
assert!(BitwiseOperator::Or.precedence() < LogicalOperator::And.precedence());
}
#[test]
fn the_symbols_are_doubled_to_distinguish_them_from_bitwise() {
assert_eq!(LogicalOperator::And.symbol(), "&&");
assert_eq!(LogicalOperator::Or.symbol(), "||");
}
}