Value fmt

main
ondra05 2022-07-21 12:32:24 +02:00
parent 7982c8ca4f
commit 00a4e770e0
5 changed files with 68 additions and 16 deletions

View File

@ -24,10 +24,7 @@ pub enum Token<'a> {
#[token("'")]
Quote,
#[token(",")]
Comma,
// Values
#[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))]
String(&'a str),

View File

@ -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()
}
}

View File

@ -3,12 +3,16 @@
use web_lisp::parser;
fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

View File

@ -36,8 +36,7 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value<'a>>, Error = Simple<Token<'
let map = value
.clone()
.then(value.clone())
.separated_by(just(Token::Comma))
.allow_trailing()
.repeated()
.collect()
.map(Value::Map)
.delimited_by(just(Token::LeftCurly), just(Token::RightCurly));

View File

@ -1,6 +1,10 @@
use crate::list::List;
use ordered_float::OrderedFloat;
use std::{borrow::Cow, collections::BTreeMap};
use std::{
borrow::Cow,
collections::BTreeMap,
fmt::{Display, Write},
};
/// A Wisp value
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
@ -19,3 +23,42 @@ pub enum Value<'a> {
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<Item = impl Display>,
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}")
}