Skip to main content

mirascript_core/lexer/
operator.rs

1use std::fmt::Display;
2
3use strum::{EnumProperty, VariantArray};
4
5use crate::emitter::OpCode;
6
7use super::prelude::*;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, VariantArray, EnumProperty)]
10pub enum Operator {
11    /// Unknown operator
12    Unknown,
13    /// `(`
14    OpenParen,
15    /// `)`
16    CloseParen,
17    /// `[`
18    OpenBracket,
19    /// `]`
20    CloseBracket,
21    /// `:`
22    Colon,
23    /// `?`
24    Question,
25    /// `?:`
26    QuestionColon,
27    /// `::`
28    ColonColon,
29    /// `,`
30    Comma,
31    /// `.`
32    Dot,
33    /// `->`
34    Arrow,
35
36    /// `..`
37    SpreadRange,
38    /// `..<`
39    HalfOpenRange,
40
41    /// `+`
42    #[strum(props(arithmetic = true, infix = true, prefix = true))]
43    Plus,
44    /// `+=`
45    #[strum(props(arithmetic = true, compound = true))]
46    PlusAssign,
47    /// `-`
48    #[strum(props(arithmetic = true, infix = true, prefix = true))]
49    Minus,
50    /// `-=`
51    #[strum(props(arithmetic = true, compound = true))]
52    MinusAssign,
53    /// `*`
54    #[strum(props(arithmetic = true, infix = true))]
55    Asterisk,
56    /// `*=`
57    #[strum(props(arithmetic = true, compound = true))]
58    AsteriskAssign,
59    /// `/`
60    #[strum(props(arithmetic = true, infix = true))]
61    Slash,
62    /// `/=`
63    #[strum(props(arithmetic = true, compound = true))]
64    SlashAssign,
65    /// `%`
66    #[strum(props(arithmetic = true, infix = true))]
67    Percent,
68    /// `%=`
69    #[strum(props(arithmetic = true, compound = true))]
70    PercentAssign,
71    /// `^`
72    #[strum(props(arithmetic = true, infix = true))]
73    Caret,
74    /// `^=`
75    #[strum(props(arithmetic = true, compound = true))]
76    CaretAssign,
77
78    /// `!`
79    #[strum(props(logical = true, prefix = true, postfix = true))]
80    Exclamation,
81    /// `&&`
82    #[strum(props(logical = true, infix = true))]
83    LogicalAnd,
84    /// `&&=`
85    #[strum(props(logical = true, compound = true))]
86    LogicalAndAssign,
87    /// `||`
88    #[strum(props(logical = true, infix = true))]
89    LogicalOr,
90    /// `||=`
91    #[strum(props(logical = true, compound = true))]
92    LogicalOrAssign,
93    /// `??`
94    #[strum(props(null_coalescing = true, infix = true))]
95    NullCoalescing,
96    /// `??=`
97    #[strum(props(null_coalescing = true, compound = true))]
98    NullCoalescingAssign,
99
100    /// `=`
101    Assign,
102
103    /// `==`
104    #[strum(props(equality = true, infix = true))]
105    Equal,
106    /// `!=`
107    #[strum(props(equality = true, infix = true))]
108    NotEqual,
109    /// `=~`
110    #[strum(props(comparison = true, infix = true))]
111    TildeEqual,
112    /// `!~`
113    #[strum(props(comparison = true, infix = true))]
114    TildeNotEqual,
115    /// `>`
116    #[strum(props(comparison = true, infix = true))]
117    Greater,
118    /// `>=`
119    #[strum(props(comparison = true, infix = true))]
120    GreaterEqual,
121    /// `<`
122    #[strum(props(comparison = true, infix = true))]
123    Less,
124    /// `<=`
125    #[strum(props(comparison = true, infix = true))]
126    LessEqual,
127
128    /// `;`
129    Semicolon,
130    /// `{`
131    OpenBrace,
132    /// `}`
133    CloseBrace,
134}
135
136impl Operator {
137    pub fn to_infix_op(&self) -> Option<OpCode> {
138        use Operator::*;
139        match self {
140            Caret => Some(OpCode::Pow),
141
142            Asterisk => Some(OpCode::Mul),
143            Slash => Some(OpCode::Div),
144            Percent => Some(OpCode::Mod),
145
146            Plus => Some(OpCode::Add),
147            Minus => Some(OpCode::Sub),
148
149            Greater => Some(OpCode::Gt),
150            GreaterEqual => Some(OpCode::Gte),
151            Less => Some(OpCode::Lt),
152            LessEqual => Some(OpCode::Lte),
153
154            Equal => Some(OpCode::Eq),
155            NotEqual => Some(OpCode::Neq),
156            TildeEqual => Some(OpCode::Aeq),
157            TildeNotEqual => Some(OpCode::Naeq),
158
159            _ => None,
160        }
161    }
162
163    pub fn to_compound_op(&self) -> Option<OpCode> {
164        use Operator::*;
165        match self {
166            CaretAssign => Some(OpCode::Pow),
167
168            AsteriskAssign => Some(OpCode::Mul),
169            SlashAssign => Some(OpCode::Div),
170            PercentAssign => Some(OpCode::Mod),
171
172            PlusAssign => Some(OpCode::Add),
173            MinusAssign => Some(OpCode::Sub),
174
175            _ => None,
176        }
177    }
178
179    pub(crate) fn is_arithmetic(&self) -> bool {
180        self.get_bool("arithmetic").unwrap_or(false)
181    }
182    pub(crate) fn is_logical(&self) -> bool {
183        self.get_bool("logical").unwrap_or(false)
184    }
185    pub(crate) fn is_equality(&self) -> bool {
186        self.get_bool("equality").unwrap_or(false)
187    }
188    pub(crate) fn is_comparison(&self) -> bool {
189        self.get_bool("comparison").unwrap_or(false)
190    }
191    pub(crate) fn is_relation(&self) -> bool {
192        self.is_equality() || self.is_comparison()
193    }
194
195    pub(crate) fn is_infix(&self) -> bool {
196        self.get_bool("infix").unwrap_or(false)
197    }
198    pub(crate) fn is_prefix(&self) -> bool {
199        self.get_bool("prefix").unwrap_or(false)
200    }
201    pub(crate) fn is_postfix(&self) -> bool {
202        self.get_bool("postfix").unwrap_or(false)
203    }
204    pub(crate) fn is_compound(&self) -> bool {
205        self.get_bool("compound").unwrap_or(false)
206    }
207}
208
209impl From<Operator> for TokenKind<'_> {
210    fn from(op: Operator) -> Self {
211        TokenKind::Operator(op)
212    }
213}
214
215impl PartialEq<Token<'_>> for Operator {
216    fn eq(&self, other: &Token<'_>) -> bool {
217        matches!(&other.kind, TokenKind::Operator(op) if op == self)
218    }
219}
220
221impl PartialEq<Operator> for Token<'_> {
222    fn eq(&self, other: &Operator) -> bool {
223        matches!(&self.kind, TokenKind::Operator(op) if op == other)
224    }
225}
226
227impl PartialEq<TokenKind<'_>> for Operator {
228    fn eq(&self, other: &TokenKind<'_>) -> bool {
229        matches!(other, TokenKind::Operator(op) if op == self)
230    }
231}
232
233impl PartialEq<Operator> for TokenKind<'_> {
234    fn eq(&self, other: &Operator) -> bool {
235        matches!(self, TokenKind::Operator(op) if op == other)
236    }
237}
238
239impl Display for Operator {
240    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
241        use Operator::*;
242        match self {
243            Unknown => Ok(()),
244            OpenParen => f.write_str("("),
245            CloseParen => f.write_str(")"),
246            OpenBracket => f.write_str("["),
247            CloseBracket => f.write_str("]"),
248            Colon => f.write_str(":"),
249            Question => f.write_str("?"),
250            QuestionColon => f.write_str("?:"),
251            ColonColon => f.write_str("::"),
252            Comma => f.write_str(","),
253            Dot => f.write_str("."),
254            Arrow => f.write_str("->"),
255            SpreadRange => f.write_str(".."),
256            HalfOpenRange => f.write_str("..<"),
257            Plus => f.write_str("+"),
258            PlusAssign => f.write_str("+="),
259            Minus => f.write_str("-"),
260            MinusAssign => f.write_str("-="),
261            Asterisk => f.write_str("*"),
262            AsteriskAssign => f.write_str("*="),
263            Slash => f.write_str("/"),
264            SlashAssign => f.write_str("/="),
265            Percent => f.write_str("%"),
266            PercentAssign => f.write_str("%="),
267            Caret => f.write_str("^"),
268            CaretAssign => f.write_str("^="),
269            Exclamation => f.write_str("!"),
270            LogicalAnd => f.write_str("&&"),
271            LogicalAndAssign => f.write_str("&&="),
272            LogicalOr => f.write_str("||"),
273            LogicalOrAssign => f.write_str("||="),
274            NullCoalescing => f.write_str("??"),
275            NullCoalescingAssign => f.write_str("??="),
276            Assign => f.write_str("="),
277            Equal => f.write_str("=="),
278            NotEqual => f.write_str("!="),
279            TildeEqual => f.write_str("=~"),
280            TildeNotEqual => f.write_str("!~"),
281            Greater => f.write_str(">"),
282            GreaterEqual => f.write_str(">="),
283            Less => f.write_str("<"),
284            LessEqual => f.write_str("<="),
285            Semicolon => f.write_str(";"),
286            OpenBrace => f.write_str("{"),
287            CloseBrace => f.write_str("}"),
288        }
289    }
290}