From 331506af1b068194c0f543dbad61781d55a1ca13 Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 21 Jul 2022 12:32:24 +0200 Subject: [PATCH] Value fmt --- src/lexer.rs | 5 +---- src/list.rs | 15 ++++++++++++--- src/main.rs | 16 ++++++++++------ src/parser.rs | 3 +-- src/value.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index b40e459..2203b2b 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -24,10 +24,7 @@ pub enum Token<'a> { #[token("'")] Quote, - - #[token(",")] - Comma, - + // Values #[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))] String(&'a str), diff --git a/src/list.rs b/src/list.rs index 42feff5..b85c71d 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,7 +1,6 @@ +use crate::value::{write_seq, Value}; use std::fmt::Display; -use crate::value::Value; - /// Single-linked list #[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum List<'a> { @@ -16,6 +15,16 @@ impl<'a> List<'a> { .rev() .fold(Self::Nil, |list, next| Self::Cons(next, Box::new(list))) } + + pub fn iter(&self) -> Iter<'_, 'a> { + Iter(self) + } +} + +impl<'a> Display for List<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write_seq(f, self, "(", ")") + } } impl<'l, 'v> IntoIterator for &'l List<'v> { @@ -23,7 +32,7 @@ impl<'l, 'v> IntoIterator for &'l List<'v> { type IntoIter = Iter<'l, 'v>; fn into_iter(self) -> Self::IntoIter { - Iter(self) + self.iter() } } diff --git a/src/main.rs b/src/main.rs index cf5180c..45d65d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,16 @@ use web_lisp::parser; fn main() -> Result<(), Box> { - println!( - "{:?}", - parser::read(&std::fs::read_to_string( - std::env::args().nth(1).ok_or("no filename provided")?, - )?) - ); + match parser::read(&std::fs::read_to_string( + std::env::args().nth(1).ok_or("no filename provided")?, + )?) { + Ok(vals) => { + for val in vals { + println!("{val}"); + } + } + Err(e) => eprintln!("Parse error: {e:?}"), + } Ok(()) } diff --git a/src/parser.rs b/src/parser.rs index 4af04dc..626426e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -36,8 +36,7 @@ fn parser<'a>() -> impl Parser, Vec>, Error = Simple { String(Cow<'a, str>), Nil, } + +impl<'a> Display for Value<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Value::List(list) => write!(f, "{list}"), + Value::Vector(vec) => write_seq(f, vec, "[", "]"), + Value::Map(map) => write_seq( + f, + map.into_iter().map(|(k, v)| format!("{k} {v}")), + "[", + "]", + ), + Value::Symbol(sym) => write!(f, "{sym}"), + Value::Keyword(kw) => write!(f, ":{kw}"), + Value::Function { .. } => write!(f, "fn"), + Value::Bool(b) => write!(f, "{b}"), + Value::Number(n) => write!(f, "{n}"), + Value::String(s) => write!(f, "\"{s}\""), + Value::Nil => write!(f, "nil"), + } + } +} + +pub(crate) fn write_seq( + f: &mut impl Write, + iterable: impl IntoIterator, + delimiter_left: &str, + delimiter_right: &str, +) -> std::fmt::Result { + let mut iter = iterable.into_iter(); + write!(f, "{delimiter_left}")?; + if let Some(x) = iter.next() { + write!(f, "{x}")?; + } + for x in iter { + write!(f, " {x}")?; + } + write!(f, "{delimiter_right}") +}