1
use std::borrow::Borrow;
2
use uniplate::Uniplate;
3

            
4
use crate::ast::enumerated::EnumVariant;
5

            
6
use super::{
7
    AbstractLiteral, DeclarationPtr, DomainPtr, Expression, Literal, Moo, Name,
8
    categories::{Category, CategoryOf},
9
    domains::HasDomain,
10
    records::Field,
11
};
12
use derivative::Derivative;
13
use parking_lot::MappedRwLockReadGuard;
14
use polyquine::Quine;
15
use serde::{Deserialize, Serialize};
16

            
17
/// An `Atom` is an indivisible expression, such as a literal or a reference.
18
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Uniplate, Derivative, Quine)]
19
#[derivative(Hash)]
20
#[uniplate()]
21
#[biplate(to=Literal)]
22
#[biplate(to=Expression)]
23
#[biplate(to=AbstractLiteral<Literal>)]
24
#[biplate(to=Field<Literal>)]
25
#[biplate(to=DeclarationPtr)]
26
#[biplate(to=Name)]
27
#[biplate(to=super::Reference)]
28
#[path_prefix(conjure_cp::ast)]
29
pub enum Atom {
30
    Literal(Literal),
31
    #[polyquine_skip]
32
    Reference(super::Reference),
33
}
34

            
35
impl Atom {
36
88787
    pub fn new_ref(decl: DeclarationPtr) -> Atom {
37
88787
        Atom::Reference(super::Reference::new(decl))
38
88787
    }
39

            
40
    pub fn into_declaration(self) -> DeclarationPtr {
41
        match self {
42
            Atom::Reference(reference) => reference.into_ptr(),
43
            _ => panic!("Called into_declaration on a non-reference Atom"),
44
        }
45
    }
46
}
47

            
48
impl CategoryOf for Atom {
49
15397063
    fn category_of(&self) -> Category {
50
15397063
        match self {
51
4261353
            Atom::Literal(_) => Category::Constant,
52
11135710
            Atom::Reference(reference) => reference.category_of(),
53
        }
54
15397063
    }
55
}
56

            
57
impl HasDomain for Atom {
58
324034626
    fn domain_of(&self) -> DomainPtr {
59
324034626
        match self {
60
39667334
            Atom::Literal(literal) => literal.domain_of(),
61
284367292
            Atom::Reference(reference) => reference.domain_of(),
62
        }
63
324034626
    }
64
}
65

            
66
impl std::fmt::Display for Atom {
67
44404437
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68
44404437
        match self {
69
23339206
            Atom::Literal(x) => x.fmt(f),
70
21065231
            Atom::Reference(x) => x.fmt(f),
71
        }
72
44404437
    }
73
}
74

            
75
impl From<Literal> for Atom {
76
11170929
    fn from(value: Literal) -> Self {
77
11170929
        Atom::Literal(value)
78
11170929
    }
79
}
80

            
81
impl From<DeclarationPtr> for Atom {
82
    fn from(value: DeclarationPtr) -> Self {
83
        Atom::Reference(super::Reference::new(value))
84
    }
85
}
86

            
87
impl From<super::Reference> for Atom {
88
192126116
    fn from(value: super::Reference) -> Self {
89
192126116
        Atom::Reference(value)
90
192126116
    }
91
}
92

            
93
impl From<i32> for Atom {
94
256
    fn from(value: i32) -> Self {
95
256
        Atom::Literal(value.into())
96
256
    }
97
}
98

            
99
impl From<bool> for Atom {
100
2758159
    fn from(value: bool) -> Self {
101
2758159
        Atom::Literal(value.into())
102
2758159
    }
103
}
104

            
105
impl TryFrom<Expression> for Atom {
106
    type Error = &'static str;
107

            
108
13034
    fn try_from(value: Expression) -> Result<Self, Self::Error> {
109
13034
        match value {
110
10513
            Expression::Atomic(_, atom) => Ok(atom),
111
2521
            _ => Err("Cannot convert non-atomic expression to Atom"),
112
        }
113
13034
    }
114
}
115

            
116
impl TryFrom<Box<Expression>> for Atom {
117
    type Error = &'static str;
118

            
119
    fn try_from(value: Box<Expression>) -> Result<Self, Self::Error> {
120
        TryFrom::try_from(*value)
121
    }
122
}
123

            
124
impl TryFrom<Moo<Expression>> for Atom {
125
    type Error = &'static str;
126

            
127
2387
    fn try_from(value: Moo<Expression>) -> Result<Self, Self::Error> {
128
2387
        TryFrom::try_from(Moo::unwrap_or_clone(value))
129
2387
    }
130
}
131
impl<'a> TryFrom<&'a Expression> for &'a Atom {
132
    type Error = &'static str;
133

            
134
257567892
    fn try_from(value: &'a Expression) -> Result<Self, Self::Error> {
135
257567892
        match value {
136
150502951
            Expression::Atomic(_, atom) => Ok(atom),
137
107064941
            _ => Err("Cannot convert non-atomic expression to Atom"),
138
        }
139
257567892
    }
140
}
141

            
142
impl<'a> TryFrom<&'a Box<Expression>> for &'a Atom {
143
    type Error = &'static str;
144

            
145
    fn try_from(value: &'a Box<Expression>) -> Result<Self, Self::Error> {
146
        let expr: &'a Expression = value.borrow();
147
        expr.try_into()
148
    }
149
}
150

            
151
impl<'a> TryFrom<&'a Moo<Expression>> for &'a Atom {
152
    type Error = &'static str;
153

            
154
3098044
    fn try_from(value: &'a Moo<Expression>) -> Result<Self, Self::Error> {
155
3098044
        let expr: &'a Expression = value.borrow();
156
3098044
        expr.try_into()
157
3098044
    }
158
}
159

            
160
impl TryFrom<Atom> for Literal {
161
    type Error = &'static str;
162

            
163
12989
    fn try_from(value: Atom) -> Result<Self, Self::Error> {
164
12989
        match value {
165
            Atom::Literal(l) => Ok(l),
166
12989
            _ => Err("Cannot convert non-literal atom to Literal"),
167
        }
168
12989
    }
169
}
170

            
171
impl<'a> TryFrom<&'a Atom> for &'a Literal {
172
    type Error = &'static str;
173

            
174
651
    fn try_from(value: &'a Atom) -> Result<Self, Self::Error> {
175
651
        match value {
176
            Atom::Literal(l) => Ok(l),
177
651
            _ => Err("Cannot convert non-literal atom to Literal"),
178
        }
179
651
    }
180
}
181

            
182
impl TryFrom<Atom> for Name {
183
    type Error = &'static str;
184

            
185
    fn try_from(value: Atom) -> Result<Self, Self::Error> {
186
        match value {
187
            Atom::Reference(x) => Ok(x.ptr().name().clone()),
188
            _ => Err("Cannot convert non-reference atom to Name"),
189
        }
190
    }
191
}
192

            
193
impl<'a> TryFrom<&'a Atom> for MappedRwLockReadGuard<'a, Name> {
194
    type Error = &'static str;
195

            
196
    fn try_from(value: &'a Atom) -> Result<Self, Self::Error> {
197
        match value {
198
            Atom::Reference(x) => Ok(x.ptr().name()),
199
            _ => Err("Cannot convert non-reference atom to Name"),
200
        }
201
    }
202
}
203

            
204
impl TryFrom<Atom> for i32 {
205
    type Error = &'static str;
206

            
207
12989
    fn try_from(value: Atom) -> Result<Self, Self::Error> {
208
12989
        let lit: Literal = value.try_into()?;
209
        lit.try_into()
210
12989
    }
211
}
212

            
213
impl TryFrom<&Box<Atom>> for i32 {
214
    type Error = &'static str;
215

            
216
    fn try_from(value: &Box<Atom>) -> Result<Self, Self::Error> {
217
        TryFrom::<&Atom>::try_from(value.as_ref())
218
    }
219
}
220

            
221
impl TryFrom<Box<Atom>> for i32 {
222
    type Error = &'static str;
223

            
224
    fn try_from(value: Box<Atom>) -> Result<Self, Self::Error> {
225
        let lit: Literal = (*value).try_into()?;
226
        lit.try_into()
227
    }
228
}
229

            
230
impl TryFrom<&Moo<Atom>> for i32 {
231
    type Error = &'static str;
232

            
233
1228314
    fn try_from(value: &Moo<Atom>) -> Result<Self, Self::Error> {
234
1228314
        TryFrom::<&Atom>::try_from(value.as_ref())
235
1228314
    }
236
}
237

            
238
impl TryFrom<Moo<Atom>> for i32 {
239
    type Error = &'static str;
240

            
241
    fn try_from(value: Moo<Atom>) -> Result<Self, Self::Error> {
242
        TryFrom::<&Atom>::try_from(value.as_ref())
243
    }
244
}
245

            
246
impl TryFrom<&Atom> for i32 {
247
    type Error = &'static str;
248

            
249
4389902
    fn try_from(value: &Atom) -> Result<Self, Self::Error> {
250
4389902
        match value {
251
1840464
            Atom::Literal(lit) => lit.try_into(),
252
            Atom::Reference(reference) => {
253
2549438
                let lit = reference
254
2549438
                    .resolve_constant()
255
2549438
                    .ok_or("Cannot convert non-constant reference atom to literal")?;
256
1117904
                lit.try_into()
257
                    .map_err(|_| "Cannot convert non-int reference atom to i32")
258
            }
259
        }
260
4389902
    }
261
}
262

            
263
impl TryFrom<Atom> for bool {
264
    type Error = &'static str;
265

            
266
    fn try_from(value: Atom) -> Result<Self, Self::Error> {
267
        let lit: Literal = value.try_into()?;
268
        lit.try_into()
269
    }
270
}
271

            
272
impl TryFrom<&Atom> for bool {
273
    type Error = &'static str;
274

            
275
1153
    fn try_from(value: &Atom) -> Result<Self, Self::Error> {
276
1153
        match value {
277
            Atom::Literal(lit) => lit.try_into(),
278
            Atom::Reference(reference) => {
279
1153
                let lit = reference
280
1153
                    .resolve_constant()
281
1153
                    .ok_or("Cannot convert non-constant reference atom to literal")?;
282
                lit.try_into()
283
                    .map_err(|_| "Cannot convert non-bool reference atom to bool")
284
            }
285
        }
286
1153
    }
287
}
288

            
289
impl TryFrom<Atom> for EnumVariant {
290
    type Error = &'static str;
291

            
292
    fn try_from(value: Atom) -> Result<Self, Self::Error> {
293
        let lit: Literal = value.try_into()?;
294
        lit.try_into()
295
    }
296
}
297

            
298
impl TryFrom<&Atom> for EnumVariant {
299
    type Error = &'static str;
300

            
301
    fn try_from(value: &Atom) -> Result<Self, Self::Error> {
302
        match value {
303
            Atom::Literal(lit) => lit.try_into(),
304
            Atom::Reference(reference) => {
305
                let lit = reference
306
                    .resolve_constant()
307
                    .ok_or("Cannot convert non-constant reference atom to literal")?;
308
                lit.try_into()
309
                    .map_err(|_| "Cannot convert non-bool reference atom to bool")
310
            }
311
        }
312
    }
313
}