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
//! The primary libslide IR.

#[macro_use]
mod mem;
pub mod collectors;
mod expression_pattern;
mod statement;
mod transformer;
pub mod visit;
pub use expression_pattern::*;
pub use mem::*;
pub use statement::*;
pub use transformer::*;

use crate::emit::Emit;
use crate::scanner::types::{Token, TokenType};

use core::cmp::Ordering;
use core::convert::TryFrom;

/// Describes a top-level item in the libslide grammar.
pub trait Grammar
where
    Self: Emit,
{
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)]
pub enum BinaryOperator {
    // Discrimant values exist to describe a formal ordering, and are grouped by tens to express
    // precedence.
    Plus = 1,
    Minus = 2,
    Mult = 10,
    Div = 11,
    Mod = 12,
    Exp = 20,
}

impl BinaryOperator {
    pub(crate) fn precedence(&self) -> u8 {
        (*self as u8) / 10
    }

    pub(crate) fn is_associative(&self) -> bool {
        use BinaryOperator::*;
        matches!(self, Plus | Mult | Exp)
    }
}

impl TryFrom<&Token> for BinaryOperator {
    type Error = ();

    fn try_from(token: &Token) -> Result<Self, Self::Error> {
        use BinaryOperator::*;
        match token.ty {
            TokenType::Plus => Ok(Plus),
            TokenType::Minus => Ok(Minus),
            TokenType::Mult => Ok(Mult),
            TokenType::Div => Ok(Div),
            TokenType::Mod => Ok(Mod),
            TokenType::Exp => Ok(Exp),
            _ => Err(()),
        }
    }
}

/// A binary expression.
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub struct BinaryExpr<E: RcExpression> {
    /// The binary operator.
    pub op: BinaryOperator,
    /// The left hand side of the binary operator.
    pub lhs: E,
    /// The right hand side of the binary operator.
    pub rhs: E,
}

macro_rules! mkop {
    ($($op_name:ident: $binop:path)*) => {
    $(
        /// Creates a binary expression with the given operator.
        pub fn $op_name<T, U>(lhs: T, rhs: U) -> Self
        where
            T: Into<E>,
            U: Into<E>,
        {
            Self {
                op: $binop,
                lhs: lhs.into(),
                rhs: rhs.into(),
            }
        }
    )*
    }
}

impl<E> BinaryExpr<E>
where
    E: RcExpression,
{
    mkop! {
        sub: BinaryOperator::Minus
        mult: BinaryOperator::Mult
        div:  BinaryOperator::Div
        exp:  BinaryOperator::Exp
    }
}

impl<E> PartialOrd for BinaryExpr<E>
where
    E: RcExpression,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<E> Ord for BinaryExpr<E>
where
    E: RcExpression,
{
    fn cmp(&self, other: &Self) -> Ordering {
        if self.op != other.op {
            self.op.cmp(&other.op)
        } else if self.lhs != other.lhs {
            self.lhs.cmp(&other.lhs)
        } else {
            self.rhs.cmp(&other.rhs)
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)]
pub enum UnaryOperator {
    SignPositive = 1,
    SignNegative = 2,
}

impl TryFrom<&Token> for UnaryOperator {
    type Error = ();

    fn try_from(token: &Token) -> Result<Self, Self::Error> {
        use UnaryOperator::*;
        match token.ty {
            TokenType::Plus => Ok(SignPositive),
            TokenType::Minus => Ok(SignNegative),
            _ => Err(()),
        }
    }
}

/// A unary expression.
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub struct UnaryExpr<E: RcExpression> {
    /// The unary operator.
    pub op: UnaryOperator,
    /// The expression the unary operator applies to.
    pub rhs: E,
}

impl<E> UnaryExpr<E>
where
    E: RcExpression,
{
    /// Creates a negation of an expression.
    pub fn negate<T>(expr: T) -> Self
    where
        T: Into<E>,
    {
        Self {
            op: UnaryOperator::SignNegative,
            rhs: expr.into(),
        }
    }
}

impl<E> PartialOrd for UnaryExpr<E>
where
    E: RcExpression,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<E> Ord for UnaryExpr<E>
where
    E: RcExpression,
{
    fn cmp(&self, other: &Self) -> Ordering {
        match self.op.cmp(&other.op) {
            Ordering::Equal => self.rhs.cmp(&other.rhs),
            ord => ord,
        }
    }
}