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
374
375
376
377
378
379
380
381
382
use std::io::{Result, Write};

use arret_runtime::boxed;
use arret_runtime::boxed::prelude::*;
use arret_runtime::boxed::refs::Gc;
use arret_runtime::intern::InternedSym;

macro_rules! process_escaped_chars {
    ($w:ident, $source:ident, $( $pattern:pat => $escape:expr ),*) => {
        // Try to write sequential unescaped characters in chunks
        // This is especially important if $w isn't buffered
        let mut last_escape_end = 0;
        for (index, c) in $source.char_indices() {
            match c {
                $(
                    $pattern => {
                        $w.write_all(&$source.as_bytes()[last_escape_end..index])?;
                        last_escape_end = index + c.len_utf8();
                        ($escape)?;
                    }
                ),* ,
                _ => {}
            };
        }

        $w.write_all(&$source.as_bytes()[last_escape_end..])?;
    }
}

fn write_escaped_str(w: &mut dyn Write, source: &str) -> Result<()> {
    process_escaped_chars!(w, source,
        '\t' => write!(w, "\\t"),
        '\r' => write!(w, "\\r"),
        '\n' => write!(w, "\\n"),
        '\\' => write!(w, "\\\\"),
        '"' => write!(w, "\\\""),
        c @ '\u{0}'..='\u{19}' => write!(w, "\\x{:X};", c as u32)
    );

    Ok(())
}

fn write_boxed_seq(
    w: &mut dyn Write,
    heap: &impl AsHeap,
    elems: impl Iterator<Item = Gc<boxed::Any>>,
) -> Result<()> {
    let mut has_prev = false;
    for elem in elems {
        if has_prev {
            write!(w, " ")?;
        } else {
            has_prev = true;
        }

        write_boxed(w, heap, elem)?;
    }

    Ok(())
}

fn write_boxed_map(
    w: &mut dyn Write,
    heap: &impl AsHeap,
    elems: impl Iterator<Item = (Gc<boxed::Any>, Gc<boxed::Any>)>,
) -> Result<()> {
    write!(w, "{{")?;

    let mut has_prev = false;
    for (key, value) in elems {
        if has_prev {
            write!(w, ", ")?;
        } else {
            has_prev = true;
        }

        write_boxed(w, heap, key)?;
        write!(w, " ")?;
        write_boxed(w, heap, value)?;
    }

    write!(w, "}}")?;
    Ok(())
}

fn write_char(w: &mut dyn Write, c: char) -> Result<()> {
    match c {
        '\n' => write!(w, "\\newline"),
        '\r' => write!(w, "\\return"),
        ' ' => write!(w, "\\space"),
        '\t' => write!(w, "\\tab"),
        '\u{21}'..='\u{126}' => write!(w, "\\{}", c),
        other => write!(w, "\\u{:04X}", other as u32),
    }
}

#[allow(clippy::float_cmp)]
fn write_float(w: &mut dyn Write, f: f64) -> Result<()> {
    if f.is_nan() {
        write!(w, "##NaN")
    } else if f.is_infinite() {
        if f.is_sign_positive() {
            write!(w, "##Inf")
        } else {
            write!(w, "##-Inf")
        }
    } else if f == 0.0 && f.is_sign_negative() {
        write!(w, "-0.0")
    } else if (f as i64 as f64) == f {
        // This is has no fractional part; force a .0 to mark it as a float
        write!(w, "{:.1}", f)
    } else {
        write!(w, "{:.}", f)
    }
}

fn write_interned_sym(
    w: &mut dyn Write,
    heap: &impl AsHeap,
    interned_sym: InternedSym,
) -> Result<()> {
    // TODO: We don't support quoted/raw symbols as EDN doesn't
    // This assumes the symbol is a valid identifier
    write!(
        w,
        "{}",
        heap.as_heap()
            .type_info()
            .interner()
            .unintern(&interned_sym)
    )
}

fn write_record(w: &mut dyn Write, heap: &impl AsHeap, record: &boxed::Record) -> Result<()> {
    use boxed::FieldValue;

    // TODO: Print our source name
    write!(w, "#record(")?;

    let mut has_prev = false;
    for field in record.field_values(heap.as_heap()) {
        if has_prev {
            write!(w, " ")?;
        } else {
            has_prev = true;
        }

        match field {
            FieldValue::Bool(true) => write!(w, "true")?,
            FieldValue::Bool(false) => write!(w, "false")?,
            FieldValue::Char(c) => write_char(w, c)?,
            FieldValue::Float(f) => write_float(w, f)?,
            FieldValue::Int(i) => write!(w, "{}", i)?,
            FieldValue::InternedSym(interned_sym) => write_interned_sym(w, heap, interned_sym)?,
            FieldValue::Boxed(boxed) => write_boxed(w, heap, boxed)?,
        }
    }

    write!(w, ")")
}

/// Writes a representation of the passed box to the writer
pub fn write_boxed(w: &mut dyn Write, heap: &impl AsHeap, any_ref: Gc<boxed::Any>) -> Result<()> {
    use arret_runtime::boxed::AnySubtype;

    match any_ref.as_subtype() {
        AnySubtype::True(_) => write!(w, "true"),
        AnySubtype::False(_) => write!(w, "false"),
        AnySubtype::Nil(_) => write!(w, "()"),
        AnySubtype::Int(int_ref) => write!(w, "{}", int_ref.value()),
        AnySubtype::Sym(sym) => write_interned_sym(w, heap, sym.interned()),
        AnySubtype::Float(float_ref) => write_float(w, float_ref.value()),
        AnySubtype::Pair(list) => {
            write!(w, "(")?;
            write_boxed_seq(w, heap, list.as_list_ref().iter())?;
            write!(w, ")")
        }
        AnySubtype::Vector(vec) => {
            write!(w, "[")?;
            write_boxed_seq(w, heap, vec.iter())?;
            write!(w, "]")
        }
        AnySubtype::Set(set) => {
            write!(w, "#{{")?;
            write_boxed_seq(w, heap, set.iter())?;
            write!(w, "}}")
        }
        AnySubtype::Char(char_ref) => write_char(w, char_ref.value()),
        AnySubtype::Str(s) => {
            write!(w, "\"")?;
            write_escaped_str(w, s.as_str())?;
            write!(w, "\"")
        }
        AnySubtype::FunThunk(_) => write!(w, "#fn"),
        AnySubtype::Record(record) => write_record(w, heap, record),
        AnySubtype::Map(map) => write_boxed_map(w, heap, map.iter()),
    }
}

/// Writes a pretty-printed representation of the passed box to the writer
pub fn pretty_print_boxed(write: &mut dyn Write, heap: &impl AsHeap, any_ref: Gc<boxed::Any>) {
    match any_ref.as_subtype() {
        boxed::AnySubtype::Str(string) => {
            write.write_all(string.as_str().as_bytes()).unwrap();
        }
        boxed::AnySubtype::Char(c) => {
            let mut buffer = [0; 4];
            write
                .write_all(c.value().encode_utf8(&mut buffer).as_bytes())
                .unwrap();
        }
        boxed::AnySubtype::Sym(sym) => {
            write
                .write_all(sym.name(heap.as_heap()).as_bytes())
                .unwrap();
        }
        _ => {
            write_boxed(write, heap.as_heap(), any_ref).unwrap();
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn string_for_boxed(heap: &boxed::Heap, any_ref: Gc<boxed::Any>) -> String {
        use std::str;

        let mut output_buf: Vec<u8> = vec![];
        write_boxed(&mut output_buf, heap, any_ref).unwrap();
        str::from_utf8(output_buf.as_slice()).unwrap().to_owned()
    }

    fn assert_write(heap: &mut boxed::Heap, expected: &'static str, any_ref: Gc<boxed::Any>) {
        use crate::reader;
        use arret_syntax::parser::datum_from_str;

        let first_output = string_for_boxed(heap, any_ref);
        assert_eq!(expected, first_output);

        // Try to round trip this to make sure our output and tests are sane
        let reparsed_syntax = datum_from_str(None, &first_output).unwrap();
        let reboxed_ref = reader::box_syntax_datum(heap, &reparsed_syntax);

        let second_output = string_for_boxed(heap, reboxed_ref);
        assert_eq!(expected, second_output);
    }

    #[test]
    fn bools() {
        let mut heap = boxed::Heap::empty();
        assert_write(&mut heap, "false", boxed::FALSE_INSTANCE.as_any_ref());
        assert_write(&mut heap, "true", boxed::TRUE_INSTANCE.as_any_ref());
    }

    #[test]
    fn ints() {
        let mut heap = boxed::Heap::empty();

        let boxed_zero = boxed::Int::new(&mut heap, 0);
        assert_write(&mut heap, "0", boxed_zero.as_any_ref());

        let boxed_positive = boxed::Int::new(&mut heap, 120);
        assert_write(&mut heap, "120", boxed_positive.as_any_ref());

        let boxed_negative = boxed::Int::new(&mut heap, -120);
        assert_write(&mut heap, "-120", boxed_negative.as_any_ref());
    }

    #[test]
    fn floats() {
        let mut heap = boxed::Heap::empty();

        let test_floats = [
            ("0.0", 0.0),
            ("-0.0", -0.0),
            ("120.0", 120.0),
            ("0.25", 0.25),
            ("-120.0", -120.0),
            ("9007199254740992.0", 9_007_199_254_740_992.0),
            ("##NaN", std::f64::NAN),
            ("##Inf", std::f64::INFINITY),
            ("##-Inf", std::f64::NEG_INFINITY),
        ];

        for (expected, f) in &test_floats {
            let boxed_float = boxed::Float::new(&mut heap, *f);
            assert_write(&mut heap, expected, boxed_float.as_any_ref());
        }
    }

    #[test]
    fn sym() {
        let mut heap = boxed::Heap::empty();

        let boxed_foo = boxed::Sym::new(&mut heap, "foo");
        assert_write(&mut heap, "foo", boxed_foo.as_any_ref());

        let boxed_bar = boxed::Sym::new(&mut heap, "bar");
        assert_write(&mut heap, "bar", boxed_bar.as_any_ref());
    }

    #[test]
    fn lists() {
        let mut heap = boxed::Heap::empty();

        let empty_list = boxed::List::from_values(&mut heap, [].iter().cloned(), boxed::Int::new);
        assert_write(&mut heap, "()", empty_list.as_any_ref());

        let one_list = boxed::List::from_values(&mut heap, [1].iter().cloned(), boxed::Int::new);
        assert_write(&mut heap, "(1)", one_list.as_any_ref());

        let three_list =
            boxed::List::from_values(&mut heap, [1, 2, 3].iter().cloned(), boxed::Int::new);
        assert_write(&mut heap, "(1 2 3)", three_list.as_any_ref());
    }

    #[test]
    fn vectors() {
        let mut heap = boxed::Heap::empty();

        let empty_vector =
            boxed::Vector::from_values(&mut heap, [].iter().cloned(), boxed::Int::new);
        assert_write(&mut heap, "[]", empty_vector.as_any_ref());

        let one_vector =
            boxed::Vector::from_values(&mut heap, [1].iter().cloned(), boxed::Int::new);
        assert_write(&mut heap, "[1]", one_vector.as_any_ref());

        let three_vector =
            boxed::Vector::from_values(&mut heap, [1, 2, 3].iter().cloned(), boxed::Int::new);
        assert_write(&mut heap, "[1 2 3]", three_vector.as_any_ref());
    }

    #[test]
    fn chars() {
        let mut heap = boxed::Heap::empty();

        let test_chars = [
            ("\\newline", '\n'),
            ("\\return", '\r'),
            ("\\space", ' '),
            ("\\tab", '\t'),
            ("\\a", 'a'),
            ("\\A", 'A'),
            ("\\(", '('),
            ("\\u03BB", '\u{03bb}'),
        ];

        for (expected, c) in &test_chars {
            let boxed_char = boxed::Char::new(&mut heap, *c);
            assert_write(&mut heap, expected, boxed_char.as_any_ref());
        }
    }

    #[test]
    fn strings() {
        let mut heap = boxed::Heap::empty();

        let test_strings = [
            (r#""""#, ""),
            (r#""Hello, world!""#, "Hello, world!"),
            (r#""Hello\"World""#, "Hello\"World"),
            (r#""Hello\\World""#, "Hello\\World"),
            (r#""Tab\t""#, "Tab\t"),
            (r#""\n\nnewline""#, "\n\nnewline"),
            (r#""carriage: \r""#, "carriage: \r"),
            (r#""lλ""#, "lλ"),
            (r#""\x0;null!""#, "\u{0}null!"),
            (
                r#""The word \"recursion\" has many meanings.""#,
                r#"The word "recursion" has many meanings."#,
            ),
        ];

        for (expected, s) in &test_strings {
            let boxed_char = boxed::Str::new(&mut heap, *s);
            assert_write(&mut heap, expected, boxed_char.as_any_ref());
        }
    }
}