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
//! Parser for PostScript fonts.

/// An error.
pub type Error = std::io::Error;

/// A result.
pub type Result<T> = std::result::Result<T, Error>;

macro_rules! deref {
    ($name:ident::$field:tt => $target:ty) => (itemize! {
        impl ::std::ops::Deref for $name {
            type Target = $target;

            #[inline]
            fn deref(&self) -> &Self::Target {
                &self.$field
            }
        }

        impl ::std::ops::DerefMut for $name {
            #[inline]
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.$field
            }
        }
    });
    ($name:ident<$life:tt>::$field:tt => $target:ty) => (itemize! {
        impl<$life> ::std::ops::Deref for $name<$life> {
            type Target = $target;

            #[inline]
            fn deref(&self) -> &Self::Target {
                &self.$field
            }
        }

        impl<$life> ::std::ops::DerefMut for $name<$life> {
            #[inline]
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.$field
            }
        }
    });
}

macro_rules! itemize(($($chunk:item)*) => ($($chunk)*));

macro_rules! number {
    ($name:ident) => (
        /// A number.
        #[derive(Clone, Copy, Debug, PartialEq)]
        pub enum $name {
            /// An integer number.
            Integer(i32),
            /// A real number.
            Real(f32),
        }

        impl From<$name> for i32 {
            #[inline]
            fn from(number: $name) -> i32 {
                use self::$name::*;
                match number {
                    Integer(value) => value,
                    Real(value) => value as i32,
                }
            }
        }

        impl From<$name> for f32 {
            #[inline]
            fn from(number: $name) -> f32 {
                use self::$name::*;
                match number {
                    Integer(value) => value as f32,
                    Real(value) => value,
                }
            }
        }

        impl ::std::cmp::PartialOrd for $name {
            #[inline]
            fn partial_cmp(&self, that: &Self) -> Option<::std::cmp::Ordering> {
                use self::$name::*;
                match (self, that) {
                    (&Integer(this), &Integer(that)) => this.partial_cmp(&that),
                    (&Real(this), &Real(that)) => this.partial_cmp(&that),
                    (&Integer(this), &Real(that)) => (this as f32).partial_cmp(&that),
                    (&Real(this), &Integer(that)) => this.partial_cmp(&(that as f32)),
                }
            }
        }

        impl ::std::ops::Add for $name {
            type Output = Self;

            #[inline]
            fn add(self, that: Self) -> Self::Output {
                use self::$name::*;
                match (self, that) {
                    (Integer(this), Integer(that)) => Integer(this + that),
                    (Real(this), Real(that)) => Real(this + that),
                    (Integer(this), Real(that)) => Real(this as f32 + that),
                    (Real(this), Integer(that)) => Real(this + that as f32),
                }
            }
        }

        impl ::std::ops::Div for $name {
            type Output = Self;

            #[inline]
            fn div(self, that: Self) -> Self::Output {
                use self::$name::*;
                match (self, that) {
                    (Integer(this), Integer(that)) => Integer(this / that),
                    (Real(this), Real(that)) => Real(this / that),
                    (Integer(this), Real(that)) => Real(this as f32 / that),
                    (Real(this), Integer(that)) => Real(this / that as f32),
                }
            }
        }

        impl ::std::ops::Mul for $name {
            type Output = Self;

            #[inline]
            fn mul(self, that: Self) -> Self::Output {
                use self::$name::*;
                match (self, that) {
                    (Integer(this), Integer(that)) => Integer(this * that),
                    (Real(this), Real(that)) => Real(this * that),
                    (Integer(this), Real(that)) => Real(this as f32 * that),
                    (Real(this), Integer(that)) => Real(this * that as f32),
                }
            }
        }

        impl ::std::ops::Neg for $name {
            type Output = Self;

            #[inline]
            fn neg(self) -> Self::Output {
                use self::$name::*;
                match self {
                    Integer(value) => Integer(-value),
                    Real(value) => Real(-value),
                }
            }
        }

        impl ::std::ops::Sub for $name {
            type Output = Self;

            #[inline(always)]
            fn sub(self, that: Self) -> Self::Output {
                self + (-that)
            }
        }
    );
}

macro_rules! raise(
    ($message:expr) => (return Err(::Error::new(::std::io::ErrorKind::Other, $message)));
    ($($argument:tt)+) => (raise!(format!($($argument)+)));
);

mod tape;

pub mod compact;
pub mod type2;

pub use tape::{Tape, Value, ValueExt};