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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use std::collections::BTreeMap;
use std::fmt::{Formatter, Display};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Import {
    pub type_module: &'static str,
}

impl ::std::cmp::PartialOrd for Import {
    fn partial_cmp(&self, other: &Import) -> Option<::std::cmp::Ordering> {
        self.type_module.partial_cmp(other.type_module)
    }
}

impl ::std::cmp::Ord for Import {
    fn cmp(&self, other: &Import) -> ::std::cmp::Ordering {
        self.type_module.cmp(other.type_module)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct RecordConstructor {
    pub import: Option<Import>,
    pub name: String,
    pub arguments: Vec<(String, PursType)>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct SeqConstructor {
    pub import: Option<Import>,
    pub name: String,
    pub arguments: Vec<PursType>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Constructor {
    Seq(SeqConstructor),
    Record(RecordConstructor),
}

impl Constructor {
    fn get_constructor_name(&self) -> String {
        match *self {
            Constructor::Seq(ref c) => c.name.clone(),
            Constructor::Record(ref c) => c.name.clone(),
        }
    }

    fn get_import(&self) -> &Option<Import> {
        match *self {
            Constructor::Seq(ref c) => &c.import,
            Constructor::Record(ref c) => &c.import,
        }
    }

    fn get_name(&self) -> String {
        match *self {
            Constructor::Seq(ref c) => {
                let mut s = String::new();

                if !c.arguments.is_empty() {
                    s.push_str("(");
                }

                s.push_str(&c.name);

                for ref arg in c.arguments.iter() {

                    s.push(' ');
                    s.push_str(&arg.get_name());
                }

                if !c.arguments.is_empty() {
                    s.push_str(")");
                }

                s
            },
            Constructor::Record(ref c) => format!("{}", c.name),
        }
    }

    fn get_children(&self) -> Vec<PursType> {
        match *self {
            Constructor::Seq(ref c) => c.arguments.clone(),
            Constructor::Record(ref c) => c.arguments.clone().into_iter().map(|(_, arg)| arg).collect(),
        }
    }
}

impl Display for Constructor {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        match *self {
            Constructor::Seq(ref c) => {
                write!(f, "{}", c)
            },
            Constructor::Record(ref c) => {
                write!(f, "{}", c)
            },
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum PursType {
    Struct(Constructor),
    Enum(String, Vec<Constructor>),
    Leaf(Import, String),
}

impl PursType {
    fn get_name(&self) -> String {
        use PursType::*;
        match *self {
            Struct(ref constructor) => constructor.get_name(),
            Enum(ref name, _) => format!("{}", name),
            Leaf(ref _import, ref name) => format!("{}", name),
        }
    }
}

impl Display for SeqConstructor {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        write!(f, "{}", self.name)?;
        for ref arg in self.arguments.iter() {
            write!(f, " {}", arg.get_name())?;
        }
        Ok(())
    }
}

impl Display for RecordConstructor {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        write!(f, "{} {{ ", self.name)?;
        for (idx, &(ref name, ref type_)) in self.arguments.iter().enumerate() {
            write!(f, "{} :: {}", name, type_.get_name())?;
            if idx < (self.arguments.len() - 1) {
                write!(f, ",")?;
            }
            write!(f, " ")?;
        }
        write!(f, "}}")
    }
}

impl Display for PursType {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        use PursType::*;
        use Constructor::*;

        match *self {
            Struct(Record(ref constructor)) => write!(f, "{}", constructor),
            Enum(ref _name, ref constructors) => {
                for (idx, ref constructor) in constructors.iter().enumerate() {
                    write!(f, "{}", constructor)?;
                    if idx < constructors.len() - 1 {
                        write!(f, " | ")?;
                    }
                }
                Ok(())
            }
            Leaf(_, ref ty) => {
                write!(f, "{}", ty)?;
                Ok(())
            },
            _ => unimplemented!(),
        }
    }
}

pub trait ToPursType {
    fn to_purs_type() -> PursType;
}


impl<T> ToPursType for Vec<T>
where T: ToPursType
{
    fn to_purs_type() -> PursType {
        PursType::Struct(Constructor::Seq(SeqConstructor {
            import: None,
            name: "Array".to_string(),
            arguments: vec![<T as ToPursType>::to_purs_type()]
        }))
    }
}

impl<T> ToPursType for Option<T>
where T: ToPursType
{
    fn to_purs_type() -> PursType {
        PursType::Struct(Constructor::Seq(SeqConstructor {
            import: Some(Import { type_module: "Data.Maybe" }),
            name: "Maybe".to_string(),
            arguments: vec![<T as ToPursType>::to_purs_type()]
        }))
    }
}

impl<T, U> ToPursType for (T, U)
where T: ToPursType,
      U: ToPursType
{
    fn to_purs_type() -> PursType {
        PursType::Struct(Constructor::Seq(SeqConstructor {
            import: Some(Import { type_module: "Data.Tuple" }),
            name: "Tuple".to_string(),
            arguments: vec![
                <T as ToPursType>::to_purs_type(),
                <U as ToPursType>::to_purs_type()
            ]
        }))
    }
}

impl ToPursType for ()
{
    fn to_purs_type() -> PursType {
        PursType::Struct(Constructor::Seq(SeqConstructor {
            import: Some(Import { type_module: "Prelude" }),
            name: "Tuple".to_string(),
            arguments: vec![]
        }))
    }
}

// Make that a feature so people can decide on their impls
// enabled by default
macro_rules! purs_primitive_impl {
    ($rust_type:ty, $purs_type:expr, $import:expr) => {
        impl ToPursType for $rust_type {
            fn to_purs_type() -> PursType {
                PursType::Leaf($import, $purs_type.to_string())
            }
        }
    }
}

const PRIM: Import = Import { type_module: "PRIM" };

purs_primitive_impl!(bool, "Boolean", PRIM);

purs_primitive_impl!(i8, "Int", PRIM);
purs_primitive_impl!(i16, "Int", PRIM);
purs_primitive_impl!(i32, "Int", PRIM);
purs_primitive_impl!(i64, "Int", PRIM);
purs_primitive_impl!(isize, "Int", PRIM);

purs_primitive_impl!(u8, "Int", PRIM);
purs_primitive_impl!(u16, "Int", PRIM);
purs_primitive_impl!(u32, "Int", PRIM);
purs_primitive_impl!(u64, "Int", PRIM);
purs_primitive_impl!(usize, "Int", PRIM);

purs_primitive_impl!(f32, "Number", PRIM);
purs_primitive_impl!(f64, "Number", PRIM);

purs_primitive_impl!(String, "String", PRIM);

#[derive(Debug)]
pub struct PursModule {
    name: String,
    imports: BTreeMap<Import, Vec<String>>,
    types: Vec<PursType>,
}

#[macro_export]
macro_rules! purs_module {
    ( $name:expr ; $( $p:path ),* ) => {
        {
            let purs_types = vec![
                $( <$p>::to_purs_type() ),*
            ];
            PursModule::new($name, purs_types)
        }
    }
}

impl PursModule {
    /// The `purs_module!` macro is slightly more convenient because it calls `to_purs_type` for
    /// you.
    pub fn new(name: String, types: Vec<PursType>) -> Self {
        let mut imports = BTreeMap::new();
        imports.insert(Import { type_module: "Data.Generic" }, vec!["class Generic".to_string()]);

        for type_ in types.iter() {
            Self::accumulate_imports(&mut imports, type_)
        }
        PursModule {
            name,
            imports,
            types,
        }
    }

    pub fn accumulate_imports(imports: &mut BTreeMap<Import, Vec<String>>, type_: &PursType) {
        use PursType::*;

        match *type_ {
            Struct(ref constructor) => {
                if let Some(ref import) = *constructor.get_import() {
                    {
                        let mut value = imports.entry(import.clone()).or_insert_with(|| Vec::new());
                        let name = constructor.get_constructor_name();
                        if let None = value.iter().find(|i| **i == name) {
                            value.push(name.clone());
                        }
                    }
                }
                for ref inner in constructor.get_children().iter() {
                    Self::accumulate_imports(imports, inner);
                }
            },
            Enum(_, ref constructors) => {
                for ref constructor in constructors.iter() {
                    if let &Some(ref import) = constructor.get_import() {
                        {
                            let mut value = imports.entry(import.clone()).or_insert_with(|| Vec::new());
                            value.push(constructor.get_constructor_name().clone());
                            let name = constructor.get_constructor_name();
                            if let None = value.iter().find(|i| **i == name) {
                                value.push(name.clone());
                            }
                        }
                    }
                    for ref inner in constructor.get_children().iter() {
                        Self::accumulate_imports(imports, inner);
                    }
                }
            },
            Leaf(ref import, ref name) => {
                let mut value = imports.entry(import.clone()).or_insert_with(|| Vec::new());
                if let None = value.iter().find(|i| *i == name) {
                    value.push(name.clone());
                }
            }
        }
    }
}

impl Display for PursModule {
    fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
        write!(f, "module {} where\n\n", self.name)?;

        for (key, value) in self.imports.iter() {
            if key.type_module == "PRIM" {
                continue
            }
            write!(f, "import {} (", key.type_module)?;
            for v in value.iter() {
                write!(f, "\n{}", v)?;
            }
            write!(f, "\n)\n")?;
        }
        write!(f, "\n")?;

        for ref type_ in self.types.iter() {
            match *type_ {
                &PursType::Leaf(_, _) => panic!("Leaf types cannot be at the module top-level"),
                &PursType::Struct(ref constructor) => {
                    let name = constructor.get_name();
                    write!(f, "data {} = {}\n\n", name, constructor)?;
                    write!(f, "derive instance generic{} :: Generic {}\n\n", name, name)?;
                },
                &PursType::Enum(ref name, ref _constructors) => {
                    write!(f, "data {} = {}\n\n", name, type_)?;
                    write!(f, "derive instance generic{} :: Generic {}\n\n", name, name)?;
                }
            }
        }
        Ok(())
    }
}