1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) 2016-2020 Fabian Schuiki

//! Operators

#![deny(missing_docs)]

use std::collections::HashMap;
use std::fmt;

use crate::common::errors::*;
use crate::common::name::*;
use crate::common::score::Result;
use crate::common::source::Spanned;

use crate::score::ResolvableName;
use crate::syntax::ast;
pub use crate::syntax::ast::LogicalOp;
pub use crate::syntax::ast::RelationalOp;
pub use crate::syntax::ast::ShiftOp;

/// An operator.
///
/// See IEEE 1076-2008 section 9.2.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Operator {
    /// A logical operator.
    Logical(LogicalOp),
    /// A relational operator.
    Rel(RelationalOp),
    /// A matching relational operator (i.e. with `?` prefix).
    Match(RelationalOp),
    /// A shift operator.
    Shift(ShiftOp),
    /// Addition or positive sign `+`.
    Add,
    /// Subtraction or negative sign `-`.
    Sub,
    /// Concatenation `&`.
    Concat,
    /// Multiplication `*`.
    Mul,
    /// Division `/`.
    Div,
    /// Modulus `mod`.
    Mod,
    /// Remainder `rem`.
    Rem,
    /// Power `**`.
    Pow,
    /// Absolute value `abs`.
    Abs,
    /// Boolean negation `not.
    Not,
    /// Condition operator `??`.
    Cond,
}

impl fmt::Display for Operator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Operator::Logical(op) => write!(f, "{}", op),
            Operator::Rel(op) => write!(f, "{}", op),
            Operator::Match(op) => write!(f, "?{}", op),
            Operator::Shift(op) => write!(f, "{}", op),
            Operator::Add => write!(f, "+"),
            Operator::Sub => write!(f, "-"),
            Operator::Concat => write!(f, "&"),
            Operator::Mul => write!(f, "*"),
            Operator::Div => write!(f, "/"),
            Operator::Mod => write!(f, "mod"),
            Operator::Rem => write!(f, "rem"),
            Operator::Pow => write!(f, "**"),
            Operator::Abs => write!(f, "abs"),
            Operator::Not => write!(f, "not"),
            Operator::Cond => write!(f, "??"),
        }
    }
}

impl Operator {
    /// Map a name to an operator.
    ///
    /// Returns `None` if no such operator exists.
    pub fn from_name(name: Name) -> Option<Operator> {
        TBL.get(&name).map(|&o| o)
    }
}

// A static table that maps operator symbols to the actual operator.
lazy_static! {
    static ref TBL: HashMap<Name, Operator> = {
        let mut tbl = HashMap::new();
        let nt = get_name_table();
        tbl.insert(nt.intern("and", false), Operator::Logical(LogicalOp::And));
        tbl.insert(nt.intern("or", false), Operator::Logical(LogicalOp::Or));
        tbl.insert(nt.intern("nand", false), Operator::Logical(LogicalOp::Nand));
        tbl.insert(nt.intern("nor", false), Operator::Logical(LogicalOp::Nor));
        tbl.insert(nt.intern("xor", false), Operator::Logical(LogicalOp::Xor));
        tbl.insert(nt.intern("xnor", false), Operator::Logical(LogicalOp::Xnor));
        tbl.insert(nt.intern("=", false), Operator::Rel(RelationalOp::Eq));
        tbl.insert(nt.intern("/=", false), Operator::Rel(RelationalOp::Neq));
        tbl.insert(nt.intern("<", false), Operator::Rel(RelationalOp::Lt));
        tbl.insert(nt.intern("<=", false), Operator::Rel(RelationalOp::Leq));
        tbl.insert(nt.intern(">", false), Operator::Rel(RelationalOp::Gt));
        tbl.insert(nt.intern(">=", false), Operator::Rel(RelationalOp::Geq));
        tbl.insert(nt.intern("?=", false), Operator::Match(RelationalOp::Eq));
        tbl.insert(nt.intern("?/=", false), Operator::Match(RelationalOp::Neq));
        tbl.insert(nt.intern("?<", false), Operator::Match(RelationalOp::Lt));
        tbl.insert(nt.intern("?<=", false), Operator::Match(RelationalOp::Leq));
        tbl.insert(nt.intern("?>", false), Operator::Match(RelationalOp::Gt));
        tbl.insert(nt.intern("?>=", false), Operator::Match(RelationalOp::Geq));
        tbl.insert(nt.intern("sll", false), Operator::Shift(ShiftOp::Sll));
        tbl.insert(nt.intern("srl", false), Operator::Shift(ShiftOp::Srl));
        tbl.insert(nt.intern("sla", false), Operator::Shift(ShiftOp::Sla));
        tbl.insert(nt.intern("sra", false), Operator::Shift(ShiftOp::Sra));
        tbl.insert(nt.intern("rol", false), Operator::Shift(ShiftOp::Rol));
        tbl.insert(nt.intern("ror", false), Operator::Shift(ShiftOp::Ror));
        tbl.insert(nt.intern("+", false), Operator::Add);
        tbl.insert(nt.intern("-", false), Operator::Sub);
        tbl.insert(nt.intern("&", false), Operator::Concat);
        tbl.insert(nt.intern("*", false), Operator::Mul);
        tbl.insert(nt.intern("/", false), Operator::Div);
        tbl.insert(nt.intern("mod", false), Operator::Mod);
        tbl.insert(nt.intern("rem", false), Operator::Rem);
        tbl.insert(nt.intern("**", false), Operator::Pow);
        tbl.insert(nt.intern("abs", false), Operator::Abs);
        tbl.insert(nt.intern("not", false), Operator::Not);
        tbl
    };
}

/// A unary operator.
///
/// See IEEE 1076-2008 section 9.2.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum UnaryOp {
    /// The `not` operator.
    Not,
    /// The `abs` operator.
    Abs,
    /// The `+` sign operator.
    Pos,
    /// The `-` sign operator.
    Neg,
    /// A logical operator.
    Logical(LogicalOp),
    /// The `??` operator.
    Cond,
}

impl UnaryOp {
    /// Map an AST unary operator to a HIR unary operator.
    ///
    /// Emits an error if the operator is not a valid unary operator.
    pub fn from<C: DiagEmitter>(ast: Spanned<ast::UnaryOp>, ctx: C) -> Result<Spanned<UnaryOp>> {
        let op = match ast.value {
            ast::UnaryOp::Not => UnaryOp::Not,
            ast::UnaryOp::Abs => UnaryOp::Abs,
            ast::UnaryOp::Sign(ast::Sign::Pos) => UnaryOp::Pos,
            ast::UnaryOp::Sign(ast::Sign::Neg) => UnaryOp::Neg,
            ast::UnaryOp::Logical(op) => UnaryOp::Logical(op),
            ast::UnaryOp::Condition => UnaryOp::Cond,
            _ => {
                ctx.emit(DiagBuilder2::error("invalid unary operator").span(ast.span));
                return Err(());
            }
        };
        Ok(Spanned::new(op, ast.span))
    }
}

impl fmt::Display for UnaryOp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            UnaryOp::Not => write!(f, "not"),
            UnaryOp::Abs => write!(f, "abs"),
            UnaryOp::Pos => write!(f, "+"),
            UnaryOp::Neg => write!(f, "-"),
            UnaryOp::Logical(op) => write!(f, "{}", op),
            UnaryOp::Cond => write!(f, "??"),
        }
    }
}

impl From<UnaryOp> for Operator {
    fn from(op: UnaryOp) -> Operator {
        match op {
            UnaryOp::Not => Operator::Not,
            UnaryOp::Abs => Operator::Abs,
            UnaryOp::Pos => Operator::Add,
            UnaryOp::Neg => Operator::Sub,
            UnaryOp::Logical(op) => Operator::Logical(op),
            UnaryOp::Cond => Operator::Cond,
        }
    }
}

impl From<UnaryOp> for ResolvableName {
    fn from(op: UnaryOp) -> ResolvableName {
        ResolvableName::Operator(op.into())
    }
}

/// A binary operator.
///
/// See IEEE 1076-2008 section 9.2.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum BinaryOp {
    /// A logical operator.
    Logical(LogicalOp),
    /// A relational operator.
    Rel(RelationalOp),
    /// A matching relational operator. These are the relational operators
    /// prefixed with a `?`.
    Match(RelationalOp),
    /// A shift operator.
    Shift(ShiftOp),
    /// The `+` operator.
    Add,
    /// The `-` operator.
    Sub,
    /// The `&` operator.
    Concat,
    /// The `*` operator.
    Mul,
    /// The `/` operator.
    Div,
    /// The `mod` operator.
    Mod,
    /// The `rem` operator.
    Rem,
    /// The `**` operator.
    Pow,
}

impl BinaryOp {
    /// Map an AST binary operator to a HIR binary operator.
    ///
    /// Emits an error if the operator is not a valid binary operator.
    pub fn from<C: DiagEmitter>(ast: Spanned<ast::BinaryOp>, ctx: C) -> Result<Spanned<BinaryOp>> {
        let op = match ast.value {
            ast::BinaryOp::Logical(op) => BinaryOp::Logical(op),
            ast::BinaryOp::Rel(op) => BinaryOp::Rel(op),
            ast::BinaryOp::Match(op) => BinaryOp::Match(op),
            ast::BinaryOp::Shift(op) => BinaryOp::Shift(op),
            ast::BinaryOp::Add => BinaryOp::Add,
            ast::BinaryOp::Sub => BinaryOp::Sub,
            ast::BinaryOp::Concat => BinaryOp::Concat,
            ast::BinaryOp::Mul => BinaryOp::Mul,
            ast::BinaryOp::Div => BinaryOp::Div,
            ast::BinaryOp::Mod => BinaryOp::Mod,
            ast::BinaryOp::Rem => BinaryOp::Rem,
            ast::BinaryOp::Pow => BinaryOp::Pow,
            _ => {
                ctx.emit(DiagBuilder2::error("invalid binary operator").span(ast.span));
                return Err(());
            }
        };
        Ok(Spanned::new(op, ast.span))
    }
}

impl fmt::Display for BinaryOp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            BinaryOp::Logical(op) => write!(f, "{}", op),
            BinaryOp::Rel(op) => write!(f, "{}", op),
            BinaryOp::Match(op) => write!(f, "?{}", op),
            BinaryOp::Shift(op) => write!(f, "{}", op),
            BinaryOp::Add => write!(f, "+"),
            BinaryOp::Sub => write!(f, "-"),
            BinaryOp::Concat => write!(f, "&"),
            BinaryOp::Mul => write!(f, "*"),
            BinaryOp::Div => write!(f, "/"),
            BinaryOp::Mod => write!(f, "mod"),
            BinaryOp::Rem => write!(f, "rem"),
            BinaryOp::Pow => write!(f, "**"),
        }
    }
}

impl From<BinaryOp> for Operator {
    fn from(op: BinaryOp) -> Operator {
        match op {
            BinaryOp::Logical(op) => Operator::Logical(op),
            BinaryOp::Rel(op) => Operator::Rel(op),
            BinaryOp::Match(op) => Operator::Match(op),
            BinaryOp::Shift(op) => Operator::Shift(op),
            BinaryOp::Add => Operator::Add,
            BinaryOp::Sub => Operator::Sub,
            BinaryOp::Concat => Operator::Concat,
            BinaryOp::Mul => Operator::Mul,
            BinaryOp::Div => Operator::Div,
            BinaryOp::Mod => Operator::Mod,
            BinaryOp::Rem => Operator::Rem,
            BinaryOp::Pow => Operator::Pow,
        }
    }
}

impl From<BinaryOp> for ResolvableName {
    fn from(op: BinaryOp) -> ResolvableName {
        ResolvableName::Operator(op.into())
    }
}