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
//! Module for types.

use std::fmt;

/// Enum of possible types for `Value`.
#[derive(Clone, Debug)]
pub enum Type {
    /// A type used to indicate an empty Arr.
    Empty,
    /// Null value.
    Null,

    /// A boolean type.
    Bool,
    /// A signed integer type.
    Int,
    /// A fractional type.
    Frac,
    /// A character type.
    Char,
    /// A string type.
    Str,

    /// An array type, containing the type of its sub-elements.
    Arr(Box<Type>),
    /// A tuple type, containing the types of its sub-elements.
    Tup(Vec<Type>),
    /// An object type.
    Obj,
}

impl Type {
    /// Returns true if this type is strictly the same as `other`.
    pub fn is(&self, other: &Type) -> bool {
        use self::Type::*;

        match *self {
            Empty => if let Empty = *other { true } else { false },
            Null => if let Null = *other { true } else { false },
            Bool => if let Bool = *other { true } else { false },
            Int => if let Int = *other { true } else { false },
            Frac => if let Frac = *other { true } else { false },
            Char => if let Char = *other { true } else { false },
            Str => if let Str = *other { true } else { false },
            Arr(ref box1) => {
                if let Arr(ref box2) = *other {
                    box1.is(box2)
                } else {
                    false
                }
            }
            Tup(ref tvec1) => {
                if let Tup(ref tvec2) = *other {
                    if tvec1.len() != tvec2.len() {
                        return false;
                    }
                    tvec1.iter().zip(tvec2.iter()).all(|(t1, t2)| t1.is(t2))
                } else {
                    false
                }
            }
            Obj => if let Obj = *other { true } else { false },
        }
    }
}

/// Two types are considered equal if one of them is Empty or they have the same variant.
// TODO: tests for this
impl PartialEq for Type {
    fn eq(&self, other: &Self) -> bool {
        use self::Type::*;

        // If either is Empty, always return `true`.
        if let Empty = *other {
            return true;
        }

        match *self {
            Empty => true,
            Arr(ref box1) => {
                if let Arr(ref box2) = *other {
                    box1 == box2
                } else {
                    false
                }
            }
            Tup(ref tvec1) => {
                if let Tup(ref tvec2) = *other {
                    tvec1 == tvec2
                } else {
                    false
                }
            }
            _ => self.is(other),
        }
    }
}
impl Eq for Type {}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::Type::*;

        match *self {
            Empty => write!(f, "Empty"),
            Null => write!(f, "Null"),
            Bool => write!(f, "Bool"),
            Int => write!(f, "Int"),
            Frac => write!(f, "Frac"),
            Char => write!(f, "Char"),
            Str => write!(f, "Str"),
            Arr(ref boxxy) => write!(f, "Arr({})", boxxy),
            Tup(ref tvec) => {
                write!(
                    f,
                    "Tup({})",
                    match tvec.get(0) {
                        Some(t1) => {
                            tvec.iter().skip(1).fold(format!("{}", t1), |s, t| {
                                format!("{}, {}", s, t)
                            })
                        }
                        None => String::from(""),
                    }
                )
            }
            Obj => write!(f, "Obj"),
        }
    }
}