~jojo/microcrisp

2e0cf656b25ff9edb2c2019c4c77c5f72f1659d9 — JoJo 1 year, 4 months ago c984afe
impl Display for Lisp
1 files changed, 28 insertions(+), 1 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +28 -1
@@ 3,7 3,7 @@ mod matching;
use std::{
    borrow::Cow,
    cmp::{max, min},
    str,
    fmt, str,
};

const SYMBOL_CHARS: &str = "\0abcdefghijklmnopqrstuvxyz+-*?0123456789";


@@ 245,6 245,33 @@ impl PartialEq<i64> for Lisp {
    }
}

impl fmt::Display for Lisp {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self {
            Lisp::Int(x) => x.fmt(f),
            Lisp::Float(x) => x.fmt(f),
            Lisp::Symbol(x) => x.fmt(f),
            Lisp::String(x) => fmt::Debug::fmt(x, f),
            Lisp::List(xs) => {
                write!(f, "(")?;
                if let Some((xn, xs)) = xs.split_last() {
                    for x in xs {
                        write!(f, "{x} ")?
                    }
                    write!(f, "{xn}")?
                }
                write!(f, ")")
            }
        }
    }
}

impl fmt::Display for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.unpack_str().fmt(f)
    }
}

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