summaryrefslogtreecommitdiff
path: root/dhall/src/simple.rs
blob: 9d3846e9683cab1272a23dfcbafd6508216a2f7e (plain)
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
use std::collections::BTreeMap;

use crate::syntax::{Builtin, LitKind};
use crate::Normalized;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleValue {
    kind: Box<SValKind>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SValKind {
    Lit(LitKind),
    Optional(Option<SimpleValue>),
    List(Vec<SimpleValue>),
    Record(BTreeMap<String, SimpleValue>),
    Union(String, Option<SimpleValue>),
    Text(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleType {
    kind: Box<STyKind>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum STyKind {
    Bool,
    Natural,
    Integer,
    Double,
    Text,
    Optional(SimpleType),
    List(SimpleType),
    Record(BTreeMap<String, SimpleType>),
    Union(BTreeMap<String, Option<SimpleType>>),
}

impl SimpleValue {
    pub(crate) fn new(kind: SValKind) -> Self {
        SimpleValue {
            kind: Box::new(kind),
        }
    }
    pub fn kind(&self) -> &SValKind {
        self.kind.as_ref()
    }
}

impl SimpleType {
    pub fn new(kind: STyKind) -> Self {
        SimpleType {
            kind: Box::new(kind),
        }
    }
    pub fn kind(&self) -> &STyKind {
        self.kind.as_ref()
    }
    pub fn into_normalized(self) -> Normalized {
        match *self.kind {
            STyKind::Bool => Normalized::make_builtin_type(Builtin::Bool),
            STyKind::Natural => Normalized::make_builtin_type(Builtin::Natural),
            STyKind::Integer => Normalized::make_builtin_type(Builtin::Integer),
            STyKind::Double => Normalized::make_builtin_type(Builtin::Double),
            STyKind::Text => Normalized::make_builtin_type(Builtin::Text),
            STyKind::Optional(t) => {
                Normalized::make_optional_type(t.into_normalized())
            }
            STyKind::List(t) => Normalized::make_list_type(t.into_normalized()),
            STyKind::Record(kts) => Normalized::make_record_type(
                kts.into_iter().map(|(k, t)| (k, t.into_normalized())),
            ),
            STyKind::Union(kts) => Normalized::make_union_type(
                kts.into_iter()
                    .map(|(k, t)| (k, t.map(|t| t.into_normalized()))),
            ),
        }
    }
}