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
use crate::{
    printer::{Printer, LINE_WIDTH},
    Printable,
};
use pomelo_parse::ast;

pub fn print_dec(dec: &ast::Dec) -> String {
    let mut printer = Printer::new(LINE_WIDTH);
    printer.dec(dec);
    printer.output()
}

impl Printable for ast::Dec {
    fn print(&self, printer: &mut Printer) -> Option<()> {
        printer.dec(self)
    }
}

impl Printer {
    pub fn dec(&mut self, dec: &ast::Dec) -> Option<()> {
        match dec {
            ast::Dec::Seq(d) => self.seq_dec(d),
            ast::Dec::Val(d) => self.val(d),
            ast::Dec::Fun(d) => self.fun(d),
            ast::Dec::Type(d) => self.tydec(d),
            ast::Dec::Datatype(d) => self.datatype(d),
            ast::Dec::DatatypeRep(d) => self.datarep(d),
            ast::Dec::Abstype(d) => self.abstype(d),
            ast::Dec::Exception(d) => self.exception(d),
            ast::Dec::Local(d) => self.local(d),
            ast::Dec::Open(d) => self.open(d),
            ast::Dec::Infix(d) => self.infix(d),
            ast::Dec::Infixr(d) => self.infixr(d),
            ast::Dec::Nonfix(d) => self.nonfix(d),
        }
    }

    fn seq_dec(&mut self, d: &ast::SeqDec) -> Option<()> {
        self.igroup(0);
        for (i, d) in d.declarations().enumerate() {
            if i != 0 {
                self.linebreak();
            }
            self.dec(&d)?;
        }
        self.endgroup();

        Some(())
    }

    fn val(&mut self, d: &ast::ValDec) -> Option<()> {
        let tyvars = d.tyvarseq();
        let mut bindings = d.bindings().enumerate();
        let n_bindings = d.bindings().count();

        self.igroup(0);

        self.igroup(crate::INDENT);
        self.text("val");
        self.space();
        // TODO: factor this out
        for t in tyvars {
            self.text(t.to_string());
            self.space();
        }

        let (_, b) = bindings.next()?;
        self.pat(&b.pat()?)?;
        self.space();
        self.text("=");
        self.space();
        self.expr(&b.expr()?)?;
        self.endgroup();

        if n_bindings > 1 {
            self.space();
            self.igroup(0);
            for (i, b) in bindings {
                self.igroup(crate::INDENT);
                self.text("and");
                self.space();

                self.pat(&b.pat()?)?;
                self.space();
                self.text("=");

                self.space();
                self.expr(&b.expr()?)?;
                self.endgroup();

                if i < n_bindings - 1 {
                    self.linebreak();
                }
            }
            self.endgroup();
        }
        self.endgroup();
        Some(())
    }

    fn fun(&mut self, _d: &ast::FunDec) -> Option<()> {
        todo!()
    }

    fn tydec(&mut self, _d: &ast::TypeDec) -> Option<()> {
        todo!()
    }

    fn datatype(&mut self, _d: &ast::DatatypeDec) -> Option<()> {
        todo!()
    }

    fn datarep(&mut self, _d: &ast::DatatypeRepDec) -> Option<()> {
        todo!()
    }

    fn abstype(&mut self, _d: &ast::AbstypeDec) -> Option<()> {
        todo!()
    }

    fn exception(&mut self, _d: &ast::ExceptionDec) -> Option<()> {
        todo!()
    }

    fn local(&mut self, _d: &ast::LocalDec) -> Option<()> {
        todo!()
    }

    fn open(&mut self, _d: &ast::OpenDec) -> Option<()> {
        todo!()
    }

    fn infix(&mut self, _d: &ast::InfixDec) -> Option<()> {
        todo!()
    }

    fn infixr(&mut self, _d: &ast::InfixrDec) -> Option<()> {
        todo!()
    }

    fn nonfix(&mut self, _d: &ast::NonfixDec) -> Option<()> {
        todo!()
    }
}

#[cfg(test)]
mod tests {
    use crate::tests::check_dec;
    use expect_test::expect;

    #[test]
    fn valdec_let_expr() {
        // TODO: figure out whether this output is too weird...
        check_dec(
            "val x = let val y = 1 in y end and z = let val a = b in c end and d = 1",
            80,
            expect![[r#"
                val x =
                  let
                    val y = 1
                  in
                    y
                  end
                and z =
                  let
                    val a = b
                  in
                    c
                  end
                and d = 1"#]],
        );
    }
}