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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) 2016-2020 Fabian Schuiki

//! Floating-point types.

use std::fmt::{self, Display};
use std::ops::Deref;

use crate::ty2::prelude::*;
use crate::ty2::ScalarSubtype;

/// A real type.
///
/// This can either be an `FloatingBasetype` or an `FloatingSubtype`.
pub trait FloatingType: Type {
    /// Convert to a type.
    fn as_type(&self) -> &Type;

    /// The range of values this real can assume.
    ///
    /// Universal integers return `None` here, as they do not have a value range
    /// associated with them.
    fn range(&self) -> Option<&Range<f64>>;

    /// The base type of this real.
    fn base_type(&self) -> &Type;

    /// The resolution function associated with this type.
    fn resolution_func(&self) -> Option<usize> {
        None
    }

    /// Returns `Some` if self is an `FloatingBasetype`, `None` otherwise.
    fn as_basetype(&self) -> Option<&FloatingBasetype> {
        None
    }

    /// Returns `Some` if self is an `FloatingSubtype`, `None` otherwise.
    fn as_subtype(&self) -> Option<&FloatingSubtype> {
        None
    }

    /// Checks whether this is a universal real type.
    fn is_universal(&self) -> bool {
        false
    }

    /// Returns an `&FloatingBasetype` or panics if the type is not a basetype.
    fn unwrap_basetype(&self) -> &FloatingBasetype {
        self.as_basetype().expect("real type is not a basetype")
    }

    /// Returns an `&FloatingSubtype` or panics if the type is not a subtype.
    fn unwrap_subtype(&self) -> &FloatingSubtype {
        self.as_subtype().expect("real type is not a subtype")
    }

    /// Check if two real types are equal.
    fn is_equal(&self, other: &FloatingType) -> bool;
}

impl<'t> PartialEq for FloatingType + 't {
    fn eq(&self, other: &FloatingType) -> bool {
        FloatingType::is_equal(self, other)
    }
}

impl<'t> Eq for FloatingType + 't {}

macro_rules! common_type_impl {
    () => {
        fn is_scalar(&self) -> bool {
            true
        }

        fn is_discrete(&self) -> bool {
            false
        }

        fn is_numeric(&self) -> bool {
            true
        }

        fn is_composite(&self) -> bool {
            false
        }

        fn as_any(&self) -> AnyType {
            AnyType::Floating(self)
        }
    };
}

/// A real base type.
#[derive(Debug, Clone, PartialEq)]
pub struct FloatingBasetype {
    /// The range of values.
    range: Range<f64>,
}

impl Eq for FloatingBasetype {}

impl FloatingBasetype {
    /// Create a new real type.
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate moore_vhdl;
    /// # extern crate num;
    /// # fn main() {
    /// use moore_vhdl::ty2::{Type, FloatingBasetype, Range, RangeDir};
    ///
    /// let a = FloatingBasetype::new(Range::ascending(0, 42));
    /// let b = FloatingBasetype::new(Range::descending(42, 0));
    ///
    /// assert_eq!(format!("{}", a), "0 to 42");
    /// assert_eq!(format!("{}", b), "42 downto 0");
    /// assert_eq!(a.dir(), RangeDir::To);
    /// assert_eq!(b.dir(), RangeDir::Downto);
    /// assert_eq!(a.len(), 43 as f64);
    /// assert_eq!(b.len(), 43 as f64);
    /// # }
    /// ```
    pub fn new(range: Range<f64>) -> FloatingBasetype {
        FloatingBasetype { range: range }
    }
}

impl Type for FloatingBasetype {
    common_type_impl!();

    fn into_owned<'a>(self) -> OwnedType<'a>
    where
        Self: 'a,
    {
        OwnedType::FloatingBasetype(self)
    }

    fn to_owned<'a>(&self) -> OwnedType<'a>
    where
        Self: 'a,
    {
        OwnedType::FloatingBasetype(self.clone())
    }
}

impl FloatingType for FloatingBasetype {
    fn as_type(&self) -> &Type {
        self
    }

    fn range(&self) -> Option<&Range<f64>> {
        Some(&self.range)
    }

    fn base_type(&self) -> &Type {
        self
    }

    fn as_basetype(&self) -> Option<&FloatingBasetype> {
        Some(self)
    }

    fn is_equal(&self, other: &FloatingType) -> bool {
        other.as_basetype().map(|t| self == t).unwrap_or(false)
    }
}

impl Deref for FloatingBasetype {
    type Target = Range<f64>;
    fn deref(&self) -> &Range<f64> {
        &self.range
    }
}

impl Display for FloatingBasetype {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.range)
    }
}

/// A subtype of an real type.
pub type FloatingSubtype<'t> = ScalarSubtype<'t, FloatingType, f64>;

impl<'t> FloatingSubtype<'t> {
    /// Create a new real subtype.
    ///
    /// Returns `Some(...)` if `range` is a subrange of the real, or `None`
    /// otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use moore_common::name::get_name_table;
    /// use moore_vhdl::ty2::{Type, TypeMark, FloatingBasetype, FloatingSubtype, Range};
    ///
    /// let ty = FloatingBasetype::new(Range::ascending(0.0, 1.0));
    /// let tm = TypeMark::new(
    ///     get_name_table().intern("UNIT", false),
    ///     &ty,
    /// );
    /// let a = FloatingSubtype::new(&tm, Range::ascending(0.0, 0.5)).unwrap();
    /// let b = FloatingSubtype::new(&tm, Range::descending(0.5, 0.0)).unwrap();
    ///
    /// assert_eq!(format!("{}", a), "UNIT range 0 to 0.5");
    /// assert_eq!(format!("{}", b), "UNIT range 0.5 downto 0");
    /// ```
    pub fn new(mark: &'t TypeMark<'t>, range: Range<f64>) -> Option<FloatingSubtype<'t>> {
        let base = mark.as_any().unwrap_floating();
        let base_range = base.range()?;
        if base_range.has_subrange(&range) {
            Some(FloatingSubtype {
                resfn: base.resolution_func(),
                mark: mark,
                base: base,
                con: range,
            })
        } else {
            None
        }
    }
}

impl<'t> Type for FloatingSubtype<'t> {
    common_type_impl!();

    fn into_owned<'a>(self) -> OwnedType<'a>
    where
        Self: 'a,
    {
        OwnedType::FloatingSubtype(self)
    }

    fn to_owned<'a>(&self) -> OwnedType<'a>
    where
        Self: 'a,
    {
        OwnedType::FloatingSubtype(self.clone())
    }
}

impl<'t> FloatingType for FloatingSubtype<'t> {
    fn as_type(&self) -> &Type {
        self
    }

    fn range(&self) -> Option<&Range<f64>> {
        Some(&self.con)
    }

    fn base_type(&self) -> &Type {
        self.base.as_type()
    }

    fn as_subtype(&self) -> Option<&FloatingSubtype> {
        Some(self)
    }

    fn is_equal(&self, other: &FloatingType) -> bool {
        other.as_subtype().map(|t| self == t).unwrap_or(false)
    }
}

impl<'t> Display for FloatingSubtype<'t> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} range {}", self.mark, self.con)
    }
}

/// A universal real.
///
/// This is not strictly a separate type, but rather defined by the standard as
/// the real type with the largest range. However since we can represent
/// arbitrary numbers as `f64`, we use this special marker type.
///
/// # Example
///
/// ```
/// use moore_vhdl::ty2::{Type, UniversalRealType};
///
/// let ty = UniversalRealType;
///
/// assert_eq!(format!("{}", ty), "{universal real}");
/// assert_eq!(ty.is_scalar(), true);
/// assert_eq!(ty.is_discrete(), false);
/// assert_eq!(ty.is_numeric(), true);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct UniversalRealType;

impl Type for UniversalRealType {
    common_type_impl!();

    fn into_owned<'a>(self) -> OwnedType<'a>
    where
        Self: 'a,
    {
        OwnedType::UniversalReal
    }

    fn to_owned<'a>(&self) -> OwnedType<'a>
    where
        Self: 'a,
    {
        OwnedType::UniversalReal
    }
}

impl FloatingType for UniversalRealType {
    fn as_type(&self) -> &Type {
        self
    }

    fn range(&self) -> Option<&Range<f64>> {
        None
    }

    fn base_type(&self) -> &Type {
        self
    }

    fn is_universal(&self) -> bool {
        true
    }

    fn is_equal(&self, other: &FloatingType) -> bool {
        other.is_universal()
    }
}

impl Display for UniversalRealType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{{universal real}}")
    }
}