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
306
307
308
309
310
311
//! Module for values.

use crate::arr;
use crate::error::OverError;
use crate::obj;
use crate::parse::format::Format;
use crate::tup;
use crate::types::Type;
use crate::{OverResult, INDENT_STEP};
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::ToPrimitive;
use std::fmt;

/// 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(BigRational),
    /// 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),
}

macro_rules! get_fn {
    ( $doc:expr, $name:tt, $type:ty, $variant:ident ) => {
        #[doc=$doc]
        pub fn $name(&self) -> OverResult<$type> {
            if let Value::$variant(ref inner) = *self {
                Ok(inner.clone())
            } else {
                Err(OverError::TypeMismatch(Type::$variant, self.get_type()))
            }
        }
    }
}

impl Value {
    /// Returns true if 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.inner_type())),
            Tup(ref tup) => Type::Tup(tup.inner_type_vec()),
            Obj(_) => Type::Obj,
        }
    }

    get_fn!(
        "Returns the `bool` contained in this `Value`. \
         Returns an error if this `Value` is not `Bool`.",
        get_bool,
        bool,
        Bool
    );
    get_fn!(
        "Returns the `BigInt` contained in this `Value`. \
         Returns an error if this `Value` is not `Int`.",
        get_int,
        BigInt,
        Int
    );
    /// Returns the `BigRational` contained in this `Value`.
    /// Returns an error if this `Value` is not `Frac`.
    pub fn get_frac(&self) -> OverResult<BigRational> {
        match *self {
            Value::Frac(ref inner) => Ok(inner.clone()),
            Value::Int(ref inner) => Ok(frac!(inner.clone(), 1)),
            _ => Err(OverError::TypeMismatch(Type::Frac, self.get_type())),
        }
    }
    get_fn!(
        "Returns the `char` contained in this `Value`. \
         Returns an error if this `Value` is not `Char`.",
        get_char,
        char,
        Char
    );
    get_fn!(
        "Returns the `String` contained in this `Value`. \
         Returns an error if this `Value` is not `Str`.",
        get_str,
        String,
        Str
    );
    get_fn!(
        "Returns the `Obj` contained in this `Value`. \
         Returns an error if this `Value` is not `Obj`.",
        get_obj,
        obj::Obj,
        Obj
    );

    /// 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(
                Type::Arr(Box::new(Type::Any)),
                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(Type::Tup(vec![]), self.get_type()))
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.format(true, INDENT_STEP))
    }
}

// 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, BigRational);
impl_eq!(Char, char);
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.replace("\r\n", "\n"),
            _ => false,
        }
    }
}

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

impl PartialEq<String> for Value {
    fn eq(&self, other: &String) -> bool {
        &other.as_str() == self
    }
}

impl PartialEq<Value> for String {
    fn eq(&self, other: &Value) -> bool {
        &self.as_str() == other
    }
}

// PartialEq for integers

macro_rules! impl_eq_int {
    ($type:ty, $fn:tt) => {
        impl PartialEq<$type> for Value {
            fn eq(&self, other: &$type) -> bool {
                match *self {
                    Value::Int(ref value) => match value.$fn() {
                        Some(value) => value == *other,
                        None => false,
                    },
                    _ => false,
                }
            }
        }

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

impl_eq_int!(usize, to_usize);
impl_eq_int!(u8, to_u8);
impl_eq_int!(u16, to_u16);
impl_eq_int!(u32, to_u32);
impl_eq_int!(u64, to_u64);
impl_eq_int!(i8, to_i8);
impl_eq_int!(i16, to_i16);
impl_eq_int!(i32, to_i32);
impl_eq_int!(i64, to_i64);

// 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);

// This is commented because the resultant values don't pass equality checks.
//
// impl From<f32> for Value {
//     fn from(inner: f32) -> Self {
//         Value::Frac(BigRational::from_f32(inner).unwrap())
//     }
// }
// impl From<f64> for Value {
//     fn from(inner: f64) -> Self {
//         Value::Frac(BigRational::from_f64(inner).unwrap())
//     }
// }
impl_from!(BigRational, 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);