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
use super::*;

impl Debug for ClassTerm {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Macro(v) => Debug::fmt(v, f),
            Self::Field(v) => Debug::fmt(v, f),
            Self::Method(v) => Debug::fmt(v, f),
            Self::Domain(v) => Debug::fmt(v, f),
        }
    }
}
impl ClassKind {
    /// The keyword of class declaration
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Class => "class",
            Self::Structure => "structure",
        }
    }
}
#[cfg(feature = "pretty-print")]
impl PrettyPrint for ClassDeclaration {
    fn pretty(&self, theme: &PrettyProvider) -> PrettyTree {
        let mut terms = PrettySequence::new(4);
        terms += theme.keyword("class");
        terms += " ";
        terms += self.name.pretty(theme);
        if let Some(gen) = &self.generic {
            terms += gen.pretty(theme);
        }
        terms += " ";
        let block = SoftBlock::curly_braces().with_joint(PrettyTree::text(";").append(PrettyTree::Hardline));
        terms += block.join_slice(&self.terms, theme);
        terms += block.join_slice(&self.methods, theme);
        terms.into()
    }
}
#[cfg(feature = "lispify")]
impl Lispify for ClassDeclaration {
    type Output = Lisp;

    fn lispify(&self) -> Self::Output {
        let mut lisp = Lisp::new(4);
        lisp += Lisp::keyword("define/class");
        lisp += self.name.lispify();
        lisp += self.modifiers.lispify();
        for item in &self.terms {
            lisp += item.lispify();
        }
        for item in &self.methods {
            lisp += item.lispify();
        }
        lisp
    }
}

impl Debug for FieldDeclaration {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let w = &mut f.debug_struct("Field");
        if !self.annotations.is_empty() {
            w.field("annotations", &self.annotations);
        }
        w.field("name", &WrapDisplay::new(&self.name));
        if let Some(typing) = &self.typing {
            w.field("type", typing);
        }
        if let Some(default) = &self.default {
            w.field("default", default);
        }
        w.finish()
    }
}
#[cfg(feature = "pretty-print")]
impl PrettyPrint for FieldDeclaration {
    fn pretty(&self, theme: &PrettyProvider) -> PrettyTree {
        let mut terms = PrettySequence::new(4);
        terms += self.annotations.pretty(theme);
        terms += theme.argument(self.name.name.to_string(), false);
        if let Some(typing) = &self.r#type {
            terms += theme.operator(":");
            terms += " ";
            terms += typing.pretty(theme);
        }
        if let Some(value) = &self.default {
            terms += theme.operator("=");
            terms += " ";
            terms += value.pretty(theme);
        }

        terms.into()
    }
}
#[cfg(feature = "lispify")]
impl Lispify for FieldDeclaration {
    type Output = Lisp;

    fn lispify(&self) -> Self::Output {
        let mut lisp = Lisp::new(10);
        lisp += Lisp::keyword("class/field");
        lisp += self.name.lispify();
        lisp += self.annotations.lispify();
        if let Some(typing) = &self.r#type {
            lisp += Lisp::keyword(":");
            lisp += typing.lispify();
        }
        if let Some(value) = &self.default {
            lisp += Lisp::keyword("=");
            lisp += value.lispify();
        }
        lisp
    }
}
#[cfg(feature = "pretty-print")]
impl PrettyPrint for MethodDeclaration {
    fn pretty(&self, theme: &PrettyProvider) -> PrettyTree {
        let mut terms = PrettySequence::new(10);
        terms += self.annotations.pretty(theme);
        terms += theme.operator(self.name.to_string());
        if let Some(typing) = &self.generics {
            if !typing.terms.is_empty() {
                terms += typing.pretty(theme);
            }
        }
        terms += self.parameters.pretty(theme);
        if let Some(typing) = &self.returns {
            terms += typing.pretty(theme);
        }
        if let Some(value) = &self.effect_type {
            terms += " ";
            terms += value.pretty(theme);
        }
        if let Some(value) = &self.body {
            terms += " ";
            terms += value.pretty(theme);
        }

        terms.into()
    }
}
#[cfg(feature = "lispify")]
impl Lispify for MethodDeclaration {
    type Output = Lisp;

    fn lispify(&self) -> Self::Output {
        let mut lisp = Lisp::new(4);
        lisp += Lisp::keyword("class/method");
        lisp += self.name.lispify();
        lisp += self.annotations.lispify();
        if let Some(generic) = &self.generics {
            if !generic.terms.is_empty() {
                lisp += generic.lispify();
            }
        }
        lisp += self.parameters.lispify();
        match &self.returns {
            Some(ty) => {
                lisp += Lisp::keyword("return/type") + ty.lispify();
            }
            None => {
                lisp += Lisp::keyword("return/type") + Lisp::symbol("Unit");
            }
        }
        match &self.effect_type {
            Some(ty) => lisp += ty.lispify(),
            None => {
                lisp += Lisp::keyword("return/type") + Lisp::symbol("Pure");
            }
        }
        if let Some(body) = &self.body {
            for item in &body.terms {
                lisp += item.lispify();
            }
        }
        lisp
    }
}