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
// Copyright (c) 2019 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use std::any::Any;
use std::rc::Rc;

pub mod builtin;
mod eval;
mod parse;

pub use eval::{eval_stmt, Env, Fun, FunMut, Pair, Ref, State};

/// Stringify (), bool, i64, String, Ref or Pair.  () is represented by the
/// empty string.  String will be quoted.  None is returned if the type is not
/// supported.
pub fn stringify(x: Rc<dyn Any>) -> Option<String> {
    if let Some(_) = x.downcast_ref::<()>() {
        return Some("".to_string());
    }

    if let Some(b) = x.downcast_ref::<bool>() {
        return Some(b.to_string());
    }

    if let Some(n) = x.downcast_ref::<i64>() {
        return Some(n.to_string());
    }

    if let Some(s) = x.downcast_ref::<String>() {
        let escaped = s.escape_debug().to_string();
        let mut quoted = String::with_capacity(escaped.len() + 2);
        quoted.push('"');
        quoted.push_str(&escaped);
        quoted.push('"');
        return Some(quoted);
    }

    if let Some(r) = x.downcast_ref::<Ref>() {
        return Some(r.to_string());
    }

    if let Some(p) = x.downcast_ref::<Pair>() {
        let mut s = String::new();
        s.push('(');
        stringify_explicit(&mut s, &p.0);
        s.push_str(" . ");
        stringify_explicit(&mut s, &p.1);
        s.push(')');
        return Some(s);
    }

    None
}

fn stringify_explicit(dest: &mut String, x: &Rc<dyn Any>) {
    if let Some(_) = x.downcast_ref::<()>() {
        dest.push_str("()");
    }

    if let Some(s) = stringify(x.clone()) {
        dest.push_str(&s);
    } else {
        dest.push('?');
    }
}