2 use types::MalVal::{Nil,Bool,Int,Str,Sym,List,Vector,Hash,Func,MalFunc,Atom};
4 fn escape_str(s: &str) -> String {
7 '"' => "\\\"".to_string(),
8 '\n' => "\\n".to_string(),
9 '\\' => "\\\\".to_string(),
12 }).collect::<Vec<String>>().join("")
16 pub fn pr_str(&self, print_readably: bool) -> String {
18 Nil => String::from("nil"),
19 Bool(true) => String::from("true"),
20 Bool(false) => String::from("false"),
21 Int(i) => format!("{}", i),
22 //Float(f) => format!("{}", f),
24 if s.starts_with("\u{29e}") {
25 format!(":{}", &s[2..])
26 } else if print_readably {
27 format!("\"{}\"", escape_str(s))
33 List(l,_) => pr_seq(&**l, print_readably, "(", ")", " "),
34 Vector(l,_) => pr_seq(&**l, print_readably, "[", "]", " "),
36 let l: Vec<MalVal> = hm
38 .flat_map(|(k, v)| { vec![Str(k.to_string()), v.clone()] })
40 pr_seq(&l, print_readably, "{", "}", " ")
42 Func(f,_) => format!("#<fn {:?}>", f),
43 MalFunc{ast: a, params: p, ..} => {
44 format!("(fn* {} {})", p.pr_str(true), a.pr_str(true))
46 Atom(a) => format!("(atom {})", a.borrow().pr_str(true)),
51 pub fn pr_seq(seq: &Vec<MalVal>, print_readably: bool,
52 start: &str, end: &str, join: &str) -> String {
53 let strs: Vec<String> = seq
55 .map(|x| x.pr_str(print_readably))
57 format!("{}{}{}", start, strs.join(join), end)
60 // vim: ts=2:sw=2:expandtab