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
use std::fmt;
use std::str::FromStr;

use crate::*;
use fmt::{Debug, Display, Formatter};
use thiserror::Error;

/// A variable for use in [`Pattern`]s or [`Subst`]s.
///
/// This implements [`FromStr`], and will only parse if it has a
/// leading `?`.
///
/// [`FromStr`]: std::str::FromStr
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Var(VarInner);

impl Var {
    /// Create a new variable from a u32.
    ///
    /// You can also use special syntax `?#3`, `?#42` to denote a numeric variable.
    /// These avoid some symbol interning, and can also be created manually from
    /// using this function or the `From` impl.
    ///
    /// ```rust
    /// # use egg::*;
    /// assert_eq!(Var::from(12), "?#12".parse().unwrap());
    /// assert_eq!(Var::from_u32(12), "?#12".parse().unwrap());
    /// ```
    pub fn from_u32(num: u32) -> Self {
        Var(VarInner::Num(num))
    }

    /// If this variable was created from a u32, get it back out.
    pub fn as_u32(&self) -> Option<u32> {
        match self.0 {
            VarInner::Num(num) => Some(num),
            _ => None,
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum VarInner {
    Sym(Symbol),
    Num(u32),
}

#[derive(Debug, Error)]
pub enum VarParseError {
    #[error("pattern variable {0:?} should have a leading question mark")]
    MissingQuestionMark(String),
    #[error("number pattern variable {0:?} was malformed")]
    BadNumber(String),
}

impl FromStr for Var {
    type Err = VarParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use VarParseError::*;

        match s.as_bytes() {
            [b'?', b'#', ..] => s[2..]
                .parse()
                .map(|num| Var(VarInner::Num(num)))
                .map_err(|_| BadNumber(s.to_owned())),
            [b'?', ..] if s.len() > 1 => Ok(Var(VarInner::Sym(Symbol::from(s)))),
            _ => Err(MissingQuestionMark(s.to_owned())),
        }
    }
}

impl Display for Var {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self.0 {
            VarInner::Sym(sym) => write!(f, "{}", sym),
            VarInner::Num(num) => write!(f, "?#{}", num),
        }
    }
}

impl Debug for Var {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self.0 {
            VarInner::Sym(sym) => write!(f, "{:?}", sym),
            VarInner::Num(num) => write!(f, "?#{}", num),
        }
    }
}

impl From<u32> for Var {
    fn from(num: u32) -> Self {
        Var(VarInner::Num(num))
    }
}

/// A substitution mapping [`Var`]s to eclass [`Id`]s.
///
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Subst {
    pub(crate) vec: smallvec::SmallVec<[(Var, Id); 3]>,
}

impl Subst {
    /// Create a `Subst` with the given initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            vec: smallvec::SmallVec::with_capacity(capacity),
        }
    }

    /// Insert something, returning the old `Id` if present.
    pub fn insert(&mut self, var: Var, id: Id) -> Option<Id> {
        for pair in &mut self.vec {
            if pair.0 == var {
                return Some(std::mem::replace(&mut pair.1, id));
            }
        }
        self.vec.push((var, id));
        None
    }

    /// Retrieve a `Var`, returning `None` if not present.
    #[inline(never)]
    pub fn get(&self, var: Var) -> Option<&Id> {
        self.vec
            .iter()
            .find_map(|(v, id)| if *v == var { Some(id) } else { None })
    }
}

impl std::ops::Index<Var> for Subst {
    type Output = Id;

    fn index(&self, var: Var) -> &Self::Output {
        match self.get(var) {
            Some(id) => id,
            None => panic!("Var '{}={}' not found in {:?}", var, var, self),
        }
    }
}

impl Debug for Subst {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let len = self.vec.len();
        write!(f, "{{")?;
        for i in 0..len {
            let (var, id) = &self.vec[i];
            write!(f, "{}: {}", var, id)?;
            if i < len - 1 {
                write!(f, ", ")?;
            }
        }
        write!(f, "}}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn var_parse() {
        assert_eq!(Var::from_str("?a").unwrap().to_string(), "?a");
        assert_eq!(Var::from_str("?abc 123").unwrap().to_string(), "?abc 123");
        assert!(Var::from_str("a").is_err());
        assert!(Var::from_str("a?").is_err());
        assert!(Var::from_str("?").is_err());
        assert!(Var::from_str("?#").is_err());
        assert!(Var::from_str("?#foo").is_err());

        // numeric vars
        assert_eq!(Var::from_str("?#0").unwrap(), Var(VarInner::Num(0)));
        assert_eq!(Var::from_str("?#010").unwrap(), Var(VarInner::Num(10)));
        assert_eq!(
            Var::from_str("?#10").unwrap(),
            Var::from_str("?#0010").unwrap()
        );
        assert_eq!(Var::from_str("?#010").unwrap(), Var(VarInner::Num(10)));
    }
}