Skip to main content

xur_ast/
op.rs

1use std::fmt;
2
3use xur_span::Span;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct BinOp {
7    pub kind: BinOpKind,
8    pub span: Span,
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum BinOpKind {
13    Add,
14    Sub,
15    Mul,
16    Div,
17    Rem,
18    BitAnd,
19    BitOr,
20    BitXor,
21    Shl,
22    Shr,
23    And,
24    Or,
25    Eq,
26    Ne,
27    Lt,
28    Le,
29    Gt,
30    Ge,
31}
32
33impl BinOpKind {
34    pub fn as_str(self) -> &'static str {
35        match self {
36            Self::Add => "+",
37            Self::Sub => "-",
38            Self::Mul => "*",
39            Self::Div => "/",
40            Self::Rem => "%",
41            Self::BitAnd => "&",
42            Self::BitOr => "|",
43            Self::BitXor => "^",
44            Self::Shl => "<<",
45            Self::Shr => ">>",
46            Self::And => "&&",
47            Self::Or => "||",
48            Self::Eq => "==",
49            Self::Ne => "!=",
50            Self::Lt => "<",
51            Self::Le => "<=",
52            Self::Gt => ">",
53            Self::Ge => ">=",
54        }
55    }
56
57    pub fn precedence(self) -> Precedence {
58        match self {
59            Self::Mul | Self::Div | Self::Rem => Precedence::Product,
60            Self::Add | Self::Sub => Precedence::Sum,
61            Self::Shl | Self::Shr => Precedence::Shift,
62            Self::BitAnd => Precedence::BitAnd,
63            Self::BitXor => Precedence::BitXor,
64            Self::BitOr => Precedence::BitOr,
65            Self::Eq | Self::Ne | Self::Lt | Self::Le | Self::Gt | Self::Ge => Precedence::Compare,
66            Self::And => Precedence::And,
67            Self::Or => Precedence::Or,
68        }
69    }
70
71    pub fn fixity(self) -> Fixity {
72        match self.precedence() {
73            // `a == b == c` is rejected rather than silently grouped.
74            Precedence::Compare => Fixity::None,
75            _ => Fixity::Left,
76        }
77    }
78}
79
80impl fmt::Display for BinOpKind {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        f.write_str(self.as_str())
83    }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum UnOp {
88    /// `-x`
89    Neg,
90    /// `!x`
91    Not,
92    /// `~x`
93    BitNot,
94}
95
96impl UnOp {
97    pub fn as_str(self) -> &'static str {
98        match self {
99            Self::Neg => "-",
100            Self::Not => "!",
101            Self::BitNot => "~",
102        }
103    }
104}
105
106/// How tightly an expression binds, weakest first; follows rustc's
107/// `ExprPrecedence`.
108#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
109pub enum Precedence {
110    /// `return`, `break`, closures: extend as far right as possible.
111    Jump,
112    /// `a = b`
113    Assign,
114    /// `a..b`
115    Range,
116    /// `a || b`
117    Or,
118    /// `a && b`
119    And,
120    /// `a == b`, `a < b`, ...
121    Compare,
122    /// `a | b`
123    BitOr,
124    /// `a ^ b`
125    BitXor,
126    /// `a & b`
127    BitAnd,
128    /// `a << b`, `a >> b`
129    Shift,
130    /// `a + b`, `a - b`
131    Sum,
132    /// `a * b`, `a / b`, `a % b`
133    Product,
134    /// `-a`, `!a`, `~a`
135    Prefix,
136    /// Calls, field access, `?`, literals, paths, blocks, ...
137    Unambiguous,
138}
139
140/// Which way a chain of same-precedence operators groups.
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub enum Fixity {
143    /// `a - b - c` is `(a - b) - c`.
144    Left,
145    /// `a = b = c` is `a = (b = c)`.
146    Right,
147    /// `a == b == c` is an error.
148    None,
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154
155    #[test]
156    fn product_binds_tighter_than_sum_than_compare() {
157        assert!(BinOpKind::Mul.precedence() > BinOpKind::Add.precedence());
158        assert!(BinOpKind::Add.precedence() > BinOpKind::Shl.precedence());
159        assert!(BinOpKind::Shl.precedence() > BinOpKind::BitAnd.precedence());
160        assert!(BinOpKind::BitAnd.precedence() > BinOpKind::BitXor.precedence());
161        assert!(BinOpKind::BitXor.precedence() > BinOpKind::BitOr.precedence());
162        assert!(BinOpKind::BitOr.precedence() > BinOpKind::Lt.precedence());
163        assert!(BinOpKind::Lt.precedence() > BinOpKind::And.precedence());
164        assert!(BinOpKind::And.precedence() > BinOpKind::Or.precedence());
165        assert!(BinOpKind::Or.precedence() > Precedence::Range);
166        assert!(Precedence::Range > Precedence::Assign);
167    }
168
169    #[test]
170    fn comparisons_do_not_associate() {
171        assert_eq!(BinOpKind::Eq.fixity(), Fixity::None);
172        assert_eq!(BinOpKind::Sub.fixity(), Fixity::Left);
173    }
174}