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
use std::{cell::RefCell, rc::{Rc, Weak}};
use std::fmt::Debug;

pub mod util;

pub use smodel_proc::smodel;

pub struct Arena<T> {
    data: RefCell<Vec<Rc<T>>>,
}

impl<T> Arena<T> {
    pub fn new() -> Self {
        Self {
            data: RefCell::new(vec![]),
        }
    }

    pub fn allocate(&self, value: T) -> Weak<T> {
        let obj = Rc::new(value);
        self.data.borrow_mut().push(obj.clone());
        Rc::downgrade(&obj)
    }
}

#[derive(Debug)]
pub enum SModelError {
    Contravariant,
}

#[cfg(test)]
mod test {
    #[test]
    fn test() {
        use crate::smodel;

        smodel! {
            mod smodel = crate;

            type Arena = Arena;
        
            /// My unified data type.
            struct Thingy {
                pub fn Thingy() {
                    super();
                }
        
                /// Empty, Foo, FooBar or FooQux
                pub fn name(&self) -> String {
                    "".into()
                }

                pub fn base_example(&self) -> String {
                    "from base".into()
                }

                pub fn x(&self) -> f64 {
                    0.0
                }
            }
        
            struct Foo: Thingy {
                pub fn Foo() {
                    super();
                }

                #[inheritdoc]
                pub override fn name(&self) -> String {
                    "Foo".into()
                }
            }
        
            struct FooBar: Foo {
                pub fn FooBar() {
                    super();
                }

                #[inheritdoc]
                pub override fn name(&self) -> String {
                    "FooBar".into()
                }

                pub override fn base_example(&self) -> String {
                    format!("from bar; {}", super.base_example())
                }
            }
            
            struct FooBarBar: FooBar {
                let m_x: f64 = 0.0;
                let ref m_y: String = "".into();

                pub fn FooBarBar(x: f64, y: &str) {
                    super();
                    self.set_m_x(x);
                    self.set_m_y(y.into());
                }

                #[inheritdoc]
                pub override fn name(&self) -> String {
                    "FooBarBar".into()
                }

                pub override fn x(&self) -> f64 {
                    self.m_x()
                }

                pub override fn base_example(&self) -> String {
                    format!("from {}; {}", self.m_y(), super.base_example())
                }
            }
        
            struct FooQux: Foo {
                pub fn FooQux() {
                    super();
                }

                #[inheritdoc]
                pub override fn name(&self) -> String {
                    "FooQux".into()
                }
            }
        }


        let arena = Arena::new();

        let symbol = Foo::new(&arena);
        let base_symbol: Thingy = symbol.into();
        assert_eq!("Foo", base_symbol.name());
        assert_eq!(true, base_symbol.is::<Foo>());
        assert_eq!(false, base_symbol.is::<FooBar>());
        assert_eq!(false, base_symbol.is::<FooQux>());
        assert_eq!("from base", base_symbol.base_example());
        assert_eq!(0.0, base_symbol.x());

        let symbol = FooBar::new(&arena);
        let base_symbol: Thingy = symbol.into();
        assert_eq!("FooBar", base_symbol.name());
        assert_eq!(true, base_symbol.is::<Foo>());
        assert_eq!(true, base_symbol.is::<FooBar>());
        assert_eq!(false, base_symbol.is::<FooBarBar>());
        assert_eq!(false, base_symbol.is::<FooQux>());
        assert_eq!("from bar; from base", base_symbol.base_example());
        assert_eq!(0.0, base_symbol.x());

        let symbol = FooBarBar::new(&arena, 10.0, "bar bar");
        let base_symbol: Thingy = symbol.into();
        assert_eq!("FooBarBar", base_symbol.name());
        assert_eq!(true, base_symbol.is::<Foo>());
        assert_eq!(true, base_symbol.is::<FooBar>());
        assert_eq!(true, base_symbol.is::<FooBarBar>());
        assert_eq!(false, base_symbol.is::<FooQux>());
        assert_eq!("from bar bar; from bar; from base", base_symbol.base_example());
        assert_eq!(10.0, base_symbol.x());

        let symbol = FooQux::new(&arena);
        let base_symbol: Thingy = symbol.into();
        assert_eq!("FooQux", base_symbol.name());
        assert_eq!(true, base_symbol.is::<Foo>());
        assert_eq!(false, base_symbol.is::<FooBar>());
        assert_eq!(true, base_symbol.is::<FooQux>());
        assert_eq!(0.0, base_symbol.x());
    }
}