pr47 0.1.4-CHARLIE

A semi-experimental programming language. Still working in progress.
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use std::any::TypeId;
use std::collections::HashSet;
use std::cmp::{Eq, PartialEq};
use std::hash::{Hash, Hasher};
use std::hint::unreachable_unchecked;
use std::mem::{discriminant, forget};
use std::ptr::NonNull;

use xjbutil::korobka::Korobka;
use xjbutil::std_ext::{BoxedExt, VecExt};

use crate::builtins::object::Object;

pub struct ContainerTyckInfo {
    pub type_id: TypeId,
    pub params: NonNull<[NonNull<TyckInfo>]>
}

pub struct FunctionTyckInfo {
    pub params: NonNull<[NonNull<TyckInfo>]>,
    pub rets: NonNull<[NonNull<TyckInfo>]>,
    pub exceptions: NonNull<[NonNull<TyckInfo>]>
}

pub enum TyckInfo {
    AnyType,
    Plain(TypeId),
    Nullable(NonNull<TyckInfo>),
    Container(ContainerTyckInfo),
    Function(FunctionTyckInfo)
}

impl TyckInfo {
    pub fn is_any(&self) -> bool {
        if let TyckInfo::AnyType = self {
            true
        } else {
            false
        }
    }

    pub unsafe fn get_container_tyck_info_unchecked(&self) -> NonNull<ContainerTyckInfo> {
        if let TyckInfo::Container(container_tyck_info) = self {
            NonNull::new_unchecked(container_tyck_info as *const _ as *mut _)
        } else {
            unreachable_unchecked()
        }
    }

    pub unsafe fn get_function_tyck_info_unchecked(&self) -> NonNull<FunctionTyckInfo> {
        if let TyckInfo::Function(function_tyck_info) = self {
            NonNull::new_unchecked(function_tyck_info as *const _ as *mut _)
        } else {
            unreachable_unchecked()
        }
    }
}

impl Drop for TyckInfo {
    fn drop(&mut self) {
        match self {
            TyckInfo::Container(ContainerTyckInfo { params, .. }) => {
                let boxed: Box<[NonNull<TyckInfo>]> = unsafe { Box::reclaim(*params) };
                drop(boxed);
            },
            TyckInfo::Function(FunctionTyckInfo { params, rets, exceptions }) => {
                let boxed: Box<[NonNull<TyckInfo>]> = unsafe { Box::reclaim(*params) };
                drop(boxed);
                let boxed: Box<[NonNull<TyckInfo>]> = unsafe { Box::reclaim(*rets) };
                drop(boxed);
                let boxed: Box<[NonNull<TyckInfo>]> = unsafe { Box::reclaim(*exceptions) };
                drop(boxed);
            },
            _ => {}
        }
    }
}

impl Hash for TyckInfo {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            TyckInfo::AnyType => {
                discriminant(self).hash(state);
            },
            TyckInfo::Plain(plain_type_id) => {
                discriminant(self).hash(state);
                plain_type_id.hash(state);
            },
            TyckInfo::Nullable(underlying) => {
                discriminant(self).hash(state);
                underlying.as_ptr().hash(state);
            },
            TyckInfo::Container(ContainerTyckInfo { type_id, params }) => {
                discriminant(self).hash(state);
                type_id.hash(state);
                let params: &[NonNull<TyckInfo>] = unsafe { params.as_ref() };
                for param /*: &NonNull<TyckInfo>*/ in params.iter() {
                    state.write_usize(param.as_ptr() as usize);
                }
            },
            TyckInfo::Function(FunctionTyckInfo { params, rets, exceptions }) => {
                discriminant(self).hash(state);
                let params: &[NonNull<TyckInfo>] = unsafe { params.as_ref() };
                for param /*: &NonNull<TyckInfo>*/ in params.iter() {
                    state.write_usize(param.as_ptr() as usize);
                }

                let rets: &[NonNull<TyckInfo>] = unsafe { rets.as_ref() };
                for ret /*: &NonNull<TyckInfo>*/ in rets.iter() {
                    state.write_usize(ret.as_ptr() as usize);
                }

                let exceptions: &[NonNull<TyckInfo>] = unsafe { exceptions.as_ref() };
                for exception /*: &NonNull<TyckInfo>*/ in exceptions.iter() {
                    state.write_usize(exception.as_ptr() as usize);
                }
            }
        }
    }
}

impl PartialEq for TyckInfo {
    fn eq(&self, other: &Self) -> bool {
        match self {
            TyckInfo::AnyType => {
                if let TyckInfo::AnyType = other { true } else { false }
            },
            TyckInfo::Plain(plain_type_id) => {
                if let TyckInfo::Plain(other_plain_type_id) = other {
                    plain_type_id == other_plain_type_id
                } else {
                    false
                }
            },
            TyckInfo::Nullable(underlying) => {
                if let TyckInfo::Nullable(other_underlying) = other {
                    underlying.as_ptr() == other_underlying.as_ptr()
                } else {
                    false
                }
            },
            TyckInfo::Container(ContainerTyckInfo { type_id, params }) => {
                let self_params: &[NonNull<TyckInfo>] = unsafe { params.as_ref() };
                if let TyckInfo::Container(other_container_tyck_info) = other {
                    let other_params: &[NonNull<TyckInfo>] = unsafe {
                        other_container_tyck_info.params.as_ref()
                    };
                    *type_id == other_container_tyck_info.type_id
                    && self_params.iter().zip(other_params.iter()).all(
                        |(p1, p2): (&NonNull<TyckInfo>, &NonNull<TyckInfo>)| {
                            p1.as_ptr() == p2.as_ptr()
                        }
                    )
                } else {
                    false
                }
            },
            TyckInfo::Function(FunctionTyckInfo { params, rets, exceptions }) => {
                let self_params: &[NonNull<TyckInfo>] = unsafe { params.as_ref() };
                let self_rets: &[NonNull<TyckInfo>] = unsafe { rets.as_ref() };
                let self_exceptions: &[NonNull<TyckInfo>] = unsafe { exceptions.as_ref() };
                if let TyckInfo::Function(other_function_tyck_info) = other {
                    let other_params: &[NonNull<TyckInfo>] = unsafe {
                        other_function_tyck_info.params.as_ref()
                    };
                    let other_rets: &[NonNull<TyckInfo>] = unsafe {
                        other_function_tyck_info.rets.as_ref()
                    };
                    let other_exceptions: &[NonNull<TyckInfo>] = unsafe {
                        other_function_tyck_info.exceptions.as_ref()
                    };
                    self_params.iter().zip(other_params.iter()).all(
                        |(p1, p2): (&NonNull<TyckInfo>, &NonNull<TyckInfo>)| {
                            p1.as_ptr() == p2.as_ptr()
                        }
                    )
                    && self_rets.iter().zip(other_rets.iter()).all(
                        |(r1, r2): (&NonNull<TyckInfo>, &NonNull<TyckInfo>)| {
                            r1.as_ptr() == r2.as_ptr()
                        }
                    )
                    && self_exceptions.iter().zip(other_exceptions.iter()).all(
                        |(e1, e2): (&NonNull<TyckInfo>, &NonNull<TyckInfo>)| {
                            e1.as_ptr() == e2.as_ptr()
                        }
                    )
                } else {
                    false
                }
            }
        }
    }
}

impl Eq for TyckInfo {}

pub struct TyckInfoCommons {
    tyck_info_int: TyckInfo,
    tyck_info_float: TyckInfo,
    tyck_info_char: TyckInfo,
    tyck_info_bool: TyckInfo,
    tyck_info_string: TyckInfo,
    tyck_info_object: TyckInfo,
    tyck_info_any: TyckInfo
}

impl TyckInfoCommons {
    pub fn new() -> Self {
        Self {
            tyck_info_int: TyckInfo::Plain(TypeId::of::<i64>()),
            tyck_info_float: TyckInfo::Plain(TypeId::of::<f64>()),
            tyck_info_char: TyckInfo::Plain(TypeId::of::<char>()),
            tyck_info_bool: TyckInfo::Plain(TypeId::of::<bool>()),
            tyck_info_string: TyckInfo::Plain(TypeId::of::<String>()),
            tyck_info_object: TyckInfo::Plain(TypeId::of::<Object>()),
            tyck_info_any: TyckInfo::AnyType
        }
    }
}

pub struct TyckInfoPool {
    pool: HashSet<Korobka<TyckInfo>>,
    commons: Korobka<TyckInfoCommons>
}

impl TyckInfoPool {
    pub fn new() -> Self {
        Self {
            pool: HashSet::new(),
            commons: Korobka::new(TyckInfoCommons::new())
        }
    }

    #[inline(always)]
    pub fn get_int_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_int as *const _ as *mut _) }
    }

    #[inline(always)]
    pub fn get_float_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_float as *const _ as *mut _) }
    }

    #[inline(always)]
    pub fn get_char_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_char as *const _ as *mut _) }
    }

    #[inline(always)]
    pub fn get_bool_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_bool as *const _ as *mut _) }
    }

    #[inline(always)]
    pub fn get_string_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_string as *const _ as *mut _) }
    }

    #[inline(always)]
    pub fn get_object_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_object as *const _ as *mut _) }
    }

    #[inline(always)]
    pub fn get_any_type(&self) -> NonNull<TyckInfo> {
        unsafe { NonNull::new_unchecked(&self.commons.tyck_info_any as *const _ as *mut _) }
    }

    pub fn create_plain_type(&mut self, type_id: TypeId) -> NonNull<TyckInfo> {
        let tyck_info: TyckInfo = TyckInfo::Plain(type_id);
        if let Some(tyck_info /*: &Korobka<TyckInfo>*/) = self.pool.get(&tyck_info) {
            tyck_info.as_nonnull()
        } else {
            let tyck_info: Korobka<TyckInfo> = Korobka::new(tyck_info);
            let ret: NonNull<TyckInfo> = tyck_info.as_nonnull();
            self.pool.insert(tyck_info);
            ret
        }
    }

    pub fn create_nullable_type(&mut self, base: NonNull<TyckInfo>) -> NonNull<TyckInfo> {
        let tyck_info: TyckInfo = TyckInfo::Nullable(base);
        if let Some(tyck_info /*: &Korobka<TyckInfo>*/) = self.pool.get(&tyck_info) {
            tyck_info.as_nonnull()
        } else {
            let tyck_info: Korobka<TyckInfo> = Korobka::new(tyck_info);
            let ret: NonNull<TyckInfo> = tyck_info.as_nonnull();
            self.pool.insert(tyck_info);
            ret
        }
    }

    pub fn create_container_type(
        &mut self,
        container_type_id: TypeId,
        params: &[NonNull<TyckInfo>]
    ) -> NonNull<TyckInfo> {
        let query_tyck_info: TyckInfo = TyckInfo::Container(ContainerTyckInfo {
            type_id: container_type_id,
            params: unsafe { NonNull::new_unchecked(params as *const _ as *mut _) }
        });

        let ret: NonNull<TyckInfo> =
            if let Some(tyck_info /*: &Korobka<TyckInfo>*/) = self.pool.get(&query_tyck_info) {
                tyck_info.as_nonnull()
            } else {
                let tyck_info: TyckInfo = TyckInfo::Container(ContainerTyckInfo {
                    type_id: container_type_id,
                    params: Vec::from(params).into_slice_ptr()
                });
                let tyck_info: Korobka<TyckInfo> = Korobka::new(tyck_info);
                let ret: NonNull<TyckInfo> = tyck_info.as_nonnull();
                self.pool.insert(tyck_info);
                ret
            };

        forget(query_tyck_info);
        ret
    }

    pub fn create_function_type(
        &mut self,
        params: &[NonNull<TyckInfo>],
        rets: &[NonNull<TyckInfo>],
        exceptions: &[NonNull<TyckInfo>]
    ) -> NonNull<TyckInfo> {
        let query_tyck_info: TyckInfo = TyckInfo::Function(FunctionTyckInfo {
            params: unsafe { NonNull::new_unchecked(params as *const _ as *mut _) },
            rets: unsafe { NonNull::new_unchecked(rets as *const _ as *mut _) },
            exceptions: unsafe { NonNull::new_unchecked(exceptions as *const _ as *mut _) }
        });

        let ret: NonNull<TyckInfo> =
            if let Some(tyck_info /*: &Korobka<TyckInfo>*/) = self.pool.get(&query_tyck_info) {
                tyck_info.as_nonnull()
            } else {
                let tyck_info: TyckInfo = TyckInfo::Function(FunctionTyckInfo {
                    params: Vec::from(params).into_slice_ptr(),
                    rets: Vec::from(rets).into_slice_ptr(),
                    exceptions: Vec::from(exceptions).into_slice_ptr()
                });
                let tyck_info: Korobka<TyckInfo> = Korobka::new(tyck_info);
                let ret: NonNull<TyckInfo> = tyck_info.as_nonnull();
                self.pool.insert(tyck_info);
                ret
            };

        forget(query_tyck_info);
        ret
    }
}

#[cfg(test)]
mod test_tyck_info_pool {
    use std::any::TypeId;
    use std::ptr::NonNull;

    use crate::data::tyck::{TyckInfo, TyckInfoPool};

    struct TestType1();
    struct TestType2();
    struct TestType3();

    #[test]
    fn test_tyck_info_pool() {
        let type1_id: TypeId = TypeId::of::<TestType1>();
        let type2_id: TypeId = TypeId::of::<TestType2>();
        let type3_id: TypeId = TypeId::of::<TestType3>();

        let mut tyck_info_pool: TyckInfoPool = TyckInfoPool::new();

        let tyck_info1: NonNull<TyckInfo> = tyck_info_pool.create_plain_type(type1_id);
        let tyck_info2: NonNull<TyckInfo> = tyck_info_pool.create_plain_type(type2_id);
        let tyck_info3: NonNull<TyckInfo> = tyck_info_pool.create_plain_type(type3_id);

        let tyck_info1_1: NonNull<TyckInfo> = tyck_info_pool.create_plain_type(type1_id);
        let tyck_info2_1: NonNull<TyckInfo> = tyck_info_pool.create_plain_type(type2_id);
        let tyck_info3_1: NonNull<TyckInfo> = tyck_info_pool.create_plain_type(type3_id);

        assert_eq!(tyck_info1, tyck_info1_1);
        assert_eq!(tyck_info2, tyck_info2_1);
        assert_eq!(tyck_info3, tyck_info3_1);

        assert_ne!(tyck_info1, tyck_info2);
        assert_ne!(tyck_info1, tyck_info3);
        assert_ne!(tyck_info2, tyck_info3);

        let type4_params: [NonNull<TyckInfo>; 2] = [tyck_info1, tyck_info2];
        let type5_params: [NonNull<TyckInfo>; 2] = [tyck_info2, tyck_info3];
        let type6_params: [NonNull<TyckInfo>; 2] = [tyck_info1, tyck_info3];

        let tyck_info4: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type3_id, &type4_params);
        let tyck_info5: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type3_id, &type5_params);
        let tyck_info6: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type3_id, &type6_params);

        let tyck_info4_1: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type3_id, &type4_params);
        let tyck_info5_1: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type3_id, &type5_params);
        let tyck_info6_1: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type3_id, &type6_params);

        assert_eq!(tyck_info4, tyck_info4_1);
        assert_eq!(tyck_info5, tyck_info5_1);
        assert_eq!(tyck_info6, tyck_info6_1);

        assert_ne!(tyck_info4, tyck_info5);
        assert_ne!(tyck_info5, tyck_info6);
        assert_ne!(tyck_info4, tyck_info6);

        let type7_params: [NonNull<TyckInfo>; 3] = [tyck_info4, tyck_info5, tyck_info6];
        let type9_params: [NonNull<TyckInfo>; 3] = [tyck_info6, tyck_info4, tyck_info5];

        let tyck_info7: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type1_id, &type7_params);
        let tyck_info8: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type2_id, &type7_params);
        let tyck_info9: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type1_id, &type9_params);

        let tyck_info7_1: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type1_id, &type7_params);
        let tyck_info8_1: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type2_id, &type7_params);
        let tyck_info9_1: NonNull<TyckInfo> =
            tyck_info_pool.create_container_type(type1_id, &type9_params);

        assert_eq!(tyck_info7, tyck_info7_1);
        assert_eq!(tyck_info8, tyck_info8_1);
        assert_eq!(tyck_info9, tyck_info9_1);

        assert_ne!(tyck_info7, tyck_info8);
        assert_ne!(tyck_info7, tyck_info9);
        assert_ne!(tyck_info8, tyck_info9);
    }
}