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

use OverResult;
use arr;
use error::OverError;
use fraction::BigFraction;
use num::bigint::BigInt;
use obj;
use tup;
use types::Type;

/// Enum of possible values and their inner types.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    /// A null value.
    Null,

    // Copy values.
    /// A boolean value.
    Bool(bool),
    /// A signed integer value.
    Int(BigInt),
    /// A fractional value.
    Frac(BigFraction),
    /// A character value.
    Char(char),
    /// A string value.
    Str(String),

    // Reference values.
    /// An array value.
    Arr(arr::Arr),
    /// A tuple value.
    Tup(tup::Tup),
    /// An object value.
    Obj(obj::Obj),
}

impl Value {
    /// Returns true iff this `Value` is null.
    pub fn is_null(&self) -> bool {
        if let Value::Null = *self { true } else { false }
    }

    /// Returns the `Type` of this `Value`.
    pub fn get_type(&self) -> Type {
        use self::Value::*;

        match *self {
            Null => Type::Null,
            Bool(_) => Type::Bool,
            Int(_) => Type::Int,
            Frac(_) => Type::Frac,
            Char(_) => Type::Char,
            Str(_) => Type::Str,
            Arr(ref arr) => Type::Arr(Box::new(arr.get_type())),
            Tup(ref tup) => Type::Tup(tup.get_type()),
            Obj(_) => Type::Obj,
        }
    }

    /// Returns the `bool` contained in this `Value`.
    /// Returns an error if this `Value` is not Bool.
    pub fn get_bool(&self) -> OverResult<bool> {
        if let Value::Bool(inner) = *self {
            Ok(inner)
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `BigInt` contained in this `Value`.
    /// Returns an error if this `Value` is not Int.
    pub fn get_int(&self) -> OverResult<BigInt> {
        if let Value::Int(ref inner) = *self {
            Ok(inner.clone())
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `BigFraction` contained in this `Value`.
    /// Returns an error if this `Value` is not Frac.
    pub fn get_frac(&self) -> OverResult<BigFraction> {
        if let Value::Frac(ref inner) = *self {
            Ok(inner.clone())
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `char` contained in this `Value`.
    /// Returns an error if this `Value` is not Char.
    pub fn get_char(&self) -> OverResult<char> {
        if let Value::Char(inner) = *self {
            Ok(inner)
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `String` contained in this `Value`.
    /// Returns an error if this `Value` is not Str.
    pub fn get_str(&self) -> OverResult<String> {
        if let Value::Str(ref inner) = *self {
            Ok(inner.clone())
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `Arr` contained in this `Value`.
    /// Returns an error if this `Value` is not Arr.
    pub fn get_arr(&self) -> OverResult<arr::Arr> {
        if let Value::Arr(ref inner) = *self {
            Ok(inner.clone())
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `Tup` contained in this `Value`.
    /// Returns an error if this `Value` is not Tup.
    pub fn get_tup(&self) -> OverResult<tup::Tup> {
        if let Value::Tup(ref inner) = *self {
            Ok(inner.clone())
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }

    /// Returns the `Obj` contained in this `Value`.
    /// Returns an error if this `Value` is not Obj.
    pub fn get_obj(&self) -> OverResult<obj::Obj> {
        if let Value::Obj(ref inner) = *self {
            Ok(inner.clone())
        } else {
            Err(OverError::TypeMismatch(self.get_type()))
        }
    }
}

// impl PartialEq

macro_rules! impl_eq {
    ($valtype:ident, $type:ty) => {
        impl PartialEq<$type> for Value {
            fn eq(&self, other: &$type) -> bool {
                match *self {
                    Value::$valtype(ref value) => value == other,
                    _                         => false
                }
            }
        }

        impl PartialEq<Value> for $type {
            fn eq(&self, other: &Value) -> bool {
                match *other {
                    Value::$valtype(ref value) => value == self,
                    _ => false
                }
            }
        }
    }
}

impl_eq!(Bool, bool);
impl_eq!(Int, BigInt);
impl_eq!(Frac, BigFraction);
impl_eq!(Char, char);
impl_eq!(Str, String);
impl_eq!(Arr, arr::Arr);
impl_eq!(Tup, tup::Tup);
impl_eq!(Obj, obj::Obj);

impl<'a> PartialEq<&'a str> for Value {
    fn eq(&self, other: &&str) -> bool {
        match *self {
            Value::Str(ref value) => value == other,
            _ => false,
        }
    }
}

impl<'a> PartialEq<Value> for &'a str {
    fn eq(&self, other: &Value) -> bool {
        match *other {
            Value::Str(ref value) => value == self,
            _ => false,
        }
    }
}

// impl From

macro_rules! impl_from {
    ( $type:ty, $fn:tt ) => {
        impl From<$type> for Value {
            fn from(inner: $type) -> Self {
                Value::$fn(inner.into())
            }
        }
    };
}

impl_from!(bool, Bool);

impl_from!(usize, Int);
impl_from!(u8, Int);
impl_from!(u16, Int);
impl_from!(u32, Int);
impl_from!(u64, Int);
impl_from!(i8, Int);
impl_from!(i16, Int);
impl_from!(i32, Int);
impl_from!(i64, Int);
impl_from!(BigInt, Int);

impl_from!(f32, Frac);
impl_from!(f64, Frac);
impl_from!(BigFraction, Frac);

impl_from!(char, Char);

impl_from!(String, Str);
impl<'a> From<&'a str> for Value {
    fn from(inner: &str) -> Self {
        Value::Str(inner.into())
    }
}

impl_from!(arr::Arr, Arr);

impl_from!(tup::Tup, Tup);

impl_from!(obj::Obj, Obj);