Skip to main content

t_ree/
operator.rs

1/// Arithmetic binary operators.
2#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3pub enum ArithmeticOperator {
4    /// Addition (`+`).
5    Add,
6    /// Subtraction (`-`).
7    Subtract,
8    /// Multiplication (`*`).
9    Multiply,
10    /// Division (`/`).
11    Divide,
12    /// Remainder (`%`).
13    Remainder,
14}
15
16/// Comparison binary operators.
17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
18pub enum ComparisonOperator {
19    /// Equal (`==`).
20    Equal,
21    /// Not equal (`!=`).
22    NotEqual,
23    /// Less than (`<`).
24    Less,
25    /// Less than or equal (`<=`).
26    LessEqual,
27    /// Greater than (`>`).
28    Greater,
29    /// Greater than or equal (`>=`).
30    GreaterEqual,
31}
32
33/// Logical binary operators.
34#[derive(Copy, Clone, Debug, PartialEq, Eq)]
35pub enum LogicalOperator {
36    /// Logical AND (`&&`).
37    And,
38    /// Logical OR (`||`).
39    Or,
40}
41
42/// Bitwise binary operators.
43#[derive(Copy, Clone, Debug, PartialEq, Eq)]
44pub enum BitwiseOperator {
45    /// Bitwise AND (`&`).
46    And,
47    /// Bitwise OR (`|`).
48    Or,
49    /// Bitwise XOR (`^`).
50    Xor,
51    /// Left shift (`<<`).
52    ShiftLeft,
53    /// Right shift (`>>`).
54    ShiftRight,
55}
56
57/// Binary operator categories.
58#[derive(Copy, Clone, Debug, PartialEq, Eq)]
59pub enum BinaryOperator {
60    /// Arithmetic operator.
61    Arithmetic(ArithmeticOperator),
62    /// Comparison operator.
63    Comparison(ComparisonOperator),
64    /// Logical operator.
65    Logical(LogicalOperator),
66    /// Bitwise operator.
67    Bitwise(BitwiseOperator),
68}
69
70impl std::fmt::Display for BinaryOperator {
71    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        match self {
73            Self::Arithmetic(ArithmeticOperator::Add) => write!(formatter, "+"),
74            Self::Arithmetic(ArithmeticOperator::Subtract) => write!(formatter, "-"),
75            Self::Arithmetic(ArithmeticOperator::Multiply) => write!(formatter, "*"),
76            Self::Arithmetic(ArithmeticOperator::Divide) => write!(formatter, "/"),
77            Self::Arithmetic(ArithmeticOperator::Remainder) => write!(formatter, "%"),
78            Self::Comparison(ComparisonOperator::Equal) => write!(formatter, "=="),
79            Self::Comparison(ComparisonOperator::NotEqual) => write!(formatter, "!="),
80            Self::Comparison(ComparisonOperator::Less) => write!(formatter, "<"),
81            Self::Comparison(ComparisonOperator::LessEqual) => write!(formatter, "<="),
82            Self::Comparison(ComparisonOperator::Greater) => write!(formatter, ">"),
83            Self::Comparison(ComparisonOperator::GreaterEqual) => write!(formatter, ">="),
84            Self::Logical(LogicalOperator::And) => write!(formatter, "&&"),
85            Self::Logical(LogicalOperator::Or) => write!(formatter, "||"),
86            Self::Bitwise(BitwiseOperator::And) => write!(formatter, "&"),
87            Self::Bitwise(BitwiseOperator::Or) => write!(formatter, "|"),
88            Self::Bitwise(BitwiseOperator::Xor) => write!(formatter, "^"),
89            Self::Bitwise(BitwiseOperator::ShiftLeft) => write!(formatter, "<<"),
90            Self::Bitwise(BitwiseOperator::ShiftRight) => write!(formatter, ">>"),
91        }
92    }
93}
94
95/// Unary operators.
96#[derive(Copy, Clone, Debug, PartialEq, Eq)]
97pub enum UnaryOperator {
98    /// Arithmetic negation (`-x`).
99    Negate,
100    /// Logical NOT (`!x`).
101    LogicalNot,
102    /// Bitwise NOT (`~x`).
103    BitwiseNot,
104}