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    #[allow(dead_code)]
196    pub(crate) fn is_infix(&self) -> bool {
197        self.get_bool("infix").unwrap_or(false)
198    }
199    #[allow(dead_code)]
200    pub(crate) fn is_prefix(&self) -> bool {
201        self.get_bool("prefix").unwrap_or(false)
202    }
203    #[allow(dead_code)]
204    pub(crate) fn is_postfix(&self) -> bool {
205        self.get_bool("postfix").unwrap_or(false)
206    }
207    pub(crate) fn is_compound(&self) -> bool {
208        self.get_bool("compound").unwrap_or(false)
209    }
210}
211
212impl From<Operator> for TokenKind<'_> {
213    fn from(op: Operator) -> Self {
214        TokenKind::Operator(op)
215    }
216}
217
218impl PartialEq<Token<'_>> for Operator {
219    fn eq(&self, other: &Token<'_>) -> bool {
220        matches!(&other.kind, TokenKind::Operator(op) if op == self)
221    }
222}
223
224impl PartialEq<Operator> for Token<'_> {
225    fn eq(&self, other: &Operator) -> bool {
226        matches!(&self.kind, TokenKind::Operator(op) if op == other)
227    }
228}
229
230impl PartialEq<TokenKind<'_>> for Operator {
231    fn eq(&self, other: &TokenKind<'_>) -> bool {
232        matches!(other, TokenKind::Operator(op) if op == self)
233    }
234}
235
236impl PartialEq<Operator> for TokenKind<'_> {
237    fn eq(&self, other: &Operator) -> bool {
238        matches!(self, TokenKind::Operator(op) if op == other)
239    }
240}
241
242impl Display for Operator {
243    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244        use Operator::*;
245        match self {
246            Unknown => Ok(()),
247            OpenParen => f.write_str("("),
248            CloseParen => f.write_str(")"),
249            OpenBracket => f.write_str("["),
250            CloseBracket => f.write_str("]"),
251            Colon => f.write_str(":"),
252            Question => f.write_str("?"),
253            QuestionColon => f.write_str("?:"),
254            ColonColon => f.write_str("::"),
255            Comma => f.write_str(","),
256            Dot => f.write_str("."),
257            Arrow => f.write_str("->"),
258            SpreadRange => f.write_str(".."),
259            HalfOpenRange => f.write_str("..<"),
260            Plus => f.write_str("+"),
261            PlusAssign => f.write_str("+="),
262            Minus => f.write_str("-"),
263            MinusAssign => f.write_str("-="),
264            Asterisk => f.write_str("*"),
265            AsteriskAssign => f.write_str("*="),
266            Slash => f.write_str("/"),
267            SlashAssign => f.write_str("/="),
268            Percent => f.write_str("%"),
269            PercentAssign => f.write_str("%="),
270            Caret => f.write_str("^"),
271            CaretAssign => f.write_str("^="),
272            Exclamation => f.write_str("!"),
273            LogicalAnd => f.write_str("&&"),
274            LogicalAndAssign => f.write_str("&&="),
275            LogicalOr => f.write_str("||"),
276            LogicalOrAssign => f.write_str("||="),
277            NullCoalescing => f.write_str("??"),
278            NullCoalescingAssign => f.write_str("??="),
279            Assign => f.write_str("="),
280            Equal => f.write_str("=="),
281            NotEqual => f.write_str("!="),
282            TildeEqual => f.write_str("=~"),
283            TildeNotEqual => f.write_str("!~"),
284            Greater => f.write_str(">"),
285            GreaterEqual => f.write_str(">="),
286            Less => f.write_str("<"),
287            LessEqual => f.write_str("<="),
288            Semicolon => f.write_str(";"),
289            OpenBrace => f.write_str("{"),
290            CloseBrace => f.write_str("}"),
291        }
292    }
293}