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
#[macro_export]
macro_rules! library {
    ($(extern crate $n:ident { $($tt:tt)* })*) => {
        #[allow(non_snake_case)]
        mod RUNTIME {
            #[allow(dead_code, non_snake_case)]
            pub fn MODULE() -> $crate::Module {
                $crate::Module::root()
            }

            $(
                library!(@mod $n { $($tt)* });
            )*
        }
    };

    (@mod $n:ident { $($tt:tt)* }) => {
        pub mod $n {
            #[allow(unused_imports)]
            use $crate::runtime::prelude::*;

            #[allow(dead_code, non_snake_case)]
            pub fn MODULE() -> $crate::Module {
                super::MODULE().get_module(stringify!($n))
            }

            struct __Indirect<T>(T);

            library!(@parse $($tt)*);
        }
    };

    (@type $n:ident) => {
        #[derive(Copy, Clone)]
        pub struct $n;

        impl $crate::runtime::RuntimeType for $n {
            fn SELF(self) -> $crate::Type {
                MODULE().get_type(stringify!($n))
            }
        }
    };

    (@impl $n:ident { fn $f:ident($($args:tt)*) $($rest:tt)* }) => {
        library!(@fn_args $n { fn () $f($($args)*) $($rest)*});
    };

    (@impl $n:ident {}) => {};

    (@fn_args $n:ident { fn ($(($($arg:tt)*))*) $f:ident() -> $($rest:tt)* }) => {
        library!(@parse_type (@fn_ret $n { fn ($(($($arg)*))*) $f }) $($rest)*);
    };

    (@fn_args $n:ident { fn ($(($($arg:tt)*))*) $f:ident() ; $($rest:tt)* }) => {
        library!(@fn_args $n { fn ($(($($arg)*))*) $f() -> (); $($rest)* });
    };

    (@fn_args $n:ident { fn ($(($($arg:tt)*))*) $f:ident($($args:tt)*) $($rest:tt)* }) => {
        library!(@parse_type (@fn_arg $n { fn ($(($($arg)*))*) $f $($rest)* }) $($args)*);
    };

    (@fn_arg ($($ty:tt)*) (, $($more:tt)*) $n:ident { fn ($(($($arg:tt)*))*) $f:ident $($rest:tt)* }) => {
        library!(@fn_args $n { fn ($(($($arg)*))* (v $($ty)*)) $f($($more)*) $($rest)* });
    };

    (@fn_arg ($($ty:tt)*) () $n:ident { fn ($(($($arg:tt)*))*) $f:ident $($rest:tt)* }) => {
        library!(@fn_args $n { fn ($(($($arg)*))* (v $($ty)*)) $f() $($rest)* });
    };

    (@fn_ret ($($ty:tt)*) (; $($rest:tt)*) $n:ident { fn ($(($($arg:tt)*))*) $f:ident }) => {
        library!(@fn_recv $n $f ($(($($arg)*))*) ($($ty)*));
        library!(@impl $n { $($rest)* });
    };

    (@fn_recv $n:ident $f:ident ((v self) $(($($arg:tt)*))*) ($($ret:tt)*)) => {
        library!(@fn $n $f (v set_self_by_value) ($(($($arg)*))*) ($($ret)*));
    };

    (@fn_recv $n:ident $f:ident ((v &self) $(($($arg:tt)*))*) ($($ret:tt)*)) => {
        library!(@fn $n $f (v set_self_by_reference) ($(($($arg)*))*) ($($ret)*));
    };

    (@fn_recv $n:ident $f:ident ((v &mut self) $(($($arg:tt)*))*) ($($ret:tt)*)) => {
        library!(@fn $n $f (v set_self_by_reference_mut) ($(($($arg)*))*) ($($ret)*));
    };

    (@fn_recv $n:ident $f:ident ($(($($arg:tt)*))*) ($($ret:tt)*)) => {
        library!(@fn $n $f () ($(($($arg)*))*) ($($ret)*));
    };

    (@fn $n:ident $f:ident ($($v:ident $recv:ident)*) ($(($w:ident $($arg:tt)*))*) ($($ret:tt)*)) => {
        impl __Indirect<$n> {
            #[allow(dead_code)]
            fn $f() {
                #[allow(non_camel_case_types)]
                #[derive(Copy, Clone)]
                pub struct $f;

                impl $crate::runtime::RuntimeFunction for $f {
                    fn SELF(self) -> $crate::Function {
                        let mut sig = $crate::Signature::new();
                        $(
                            sig.$recv();
                        )*
                        $(
                            sig.add_input(library!(@to_runtime_type $($arg)*));
                        )*
                        sig.set_output(library!(@to_runtime_type $($ret)*));
                        $crate::runtime::RuntimeType::SELF($n).get_function(stringify!($f), sig)
                    }
                }

                impl $f {
                    pub fn INVOKE<'a>(
                        self,
                        $(
                            $v: $crate::Value<'a>,
                        )*
                        $(
                            $w: $crate::Value<'a>,
                        )*
                    ) -> $crate::Value<'a> {
                        $crate::runtime::RuntimeFunction::SELF(self).invoke([$($v,)* $($w,)*])
                    }
                }

                impl $n {
                    #[allow(non_upper_case_globals)]
                    pub const $f: $f = $f;
                }
            }
        }
    };

    (@to_runtime_type $n:ident) => {
        $crate::runtime::RuntimeType::SELF($n)
    };

    (@to_runtime_type &mut $($rest:tt)*) => {
        library!(@to_runtime_type $($rest)*).reference_mut()
    };

    (@to_runtime_type & $($rest:tt)*) => {
        library!(@to_runtime_type $($rest)*).reference()
    };

    (@to_runtime_type ()) => {
        $crate::Type::unit()
    };

    (@to_runtime_type $($rest:tt)*) => {
        compile_error!(stringify!($($rest)*))
    };

    (@parse_type (@$tag:ident $($then:tt)*) () $($rest:tt)*) => {
        library!(@$tag (()) ($($rest)*) $($then)*);
    };

    (@parse_type (@$tag:ident $($then:tt)*) $t:ident $($rest:tt)*) => {
        library!(@$tag ($t) ($($rest)*) $($then)*);
    };

    (@parse_type (@$tag:ident $($then:tt)*) &mut $($rest:tt)*) => {
        library!(@parse_type (@parse_type_mut @$tag $($then)*) $($rest)*);
    };

    (@parse_type_mut ($($ty:tt)*) ($($rest:tt)*) @$tag:ident $($then:tt)*) => {
        library!(@$tag (&mut $($ty)*) ($($rest)*) $($then)*);
    };

    (@parse_type (@$tag:ident $($then:tt)*) & $($rest:tt)*) => {
        library!(@parse_type (@parse_type_ref @$tag $($then)*) $($rest)*);
    };

    (@parse_type_ref ($($ty:tt)*) ($($rest:tt)*) @$tag:ident $($then:tt)*) => {
        library!(@$tag (& $($ty)*) ($($rest)*) $($then)*);
    };

    (@parse_type (@$tag:ident $($then:tt)*) $other:tt $($input:tt)*) => {
        error_token!($other);
    };

    (@parse mod $n:ident { $($tt:tt)* } $($rest:tt)*) => {
        library!(@mod $n { $($tt)* });
        library!(@parse $($rest)*);
    };

    (@parse type $n:ident; $($rest:tt)*) => {
        library!(@type $n);
        library!(@parse $($rest)*);
    };

    (@parse impl $n:ident { $($tt:tt)* } $($rest:tt)*) => {
        library!(@impl $n { $($tt)* });
        library!(@parse $($rest)*);
    };

    (@parse trait $n:ident { $($tt:tt)* } $($rest:tt)*) => {
        library!(@type $n);
        library!(@impl $n { $($tt)* });
        library!(@parse $($rest)*);
    };

    (@parse) => {};
}

#[doc(hidden)]
#[macro_export]
macro_rules! error_token {
    () => {};
}