Value fmt
This commit is contained in:
parent
09df88c80d
commit
331506af1b
|
@ -25,9 +25,6 @@ pub enum Token<'a> {
|
||||||
#[token("'")]
|
#[token("'")]
|
||||||
Quote,
|
Quote,
|
||||||
|
|
||||||
#[token(",")]
|
|
||||||
Comma,
|
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
#[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))]
|
#[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))]
|
||||||
String(&'a str),
|
String(&'a str),
|
||||||
|
|
15
src/list.rs
15
src/list.rs
|
@ -1,7 +1,6 @@
|
||||||
|
use crate::value::{write_seq, Value};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use crate::value::Value;
|
|
||||||
|
|
||||||
/// Single-linked list
|
/// Single-linked list
|
||||||
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum List<'a> {
|
pub enum List<'a> {
|
||||||
|
@ -16,6 +15,16 @@ impl<'a> List<'a> {
|
||||||
.rev()
|
.rev()
|
||||||
.fold(Self::Nil, |list, next| Self::Cons(next, Box::new(list)))
|
.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> {
|
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>;
|
type IntoIter = Iter<'l, 'v>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
Iter(self)
|
self.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -3,12 +3,16 @@
|
||||||
use web_lisp::parser;
|
use web_lisp::parser;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!(
|
match parser::read(&std::fs::read_to_string(
|
||||||
"{:?}",
|
|
||||||
parser::read(&std::fs::read_to_string(
|
|
||||||
std::env::args().nth(1).ok_or("no filename provided")?,
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,7 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Value<'a>>, Error = Simple<Token<'
|
||||||
let map = value
|
let map = value
|
||||||
.clone()
|
.clone()
|
||||||
.then(value.clone())
|
.then(value.clone())
|
||||||
.separated_by(just(Token::Comma))
|
.repeated()
|
||||||
.allow_trailing()
|
|
||||||
.collect()
|
.collect()
|
||||||
.map(Value::Map)
|
.map(Value::Map)
|
||||||
.delimited_by(just(Token::LeftCurly), just(Token::RightCurly));
|
.delimited_by(just(Token::LeftCurly), just(Token::RightCurly));
|
||||||
|
|
45
src/value.rs
45
src/value.rs
|
@ -1,6 +1,10 @@
|
||||||
use crate::list::List;
|
use crate::list::List;
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use std::{borrow::Cow, collections::BTreeMap};
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
collections::BTreeMap,
|
||||||
|
fmt::{Display, Write},
|
||||||
|
};
|
||||||
|
|
||||||
/// A Wisp value
|
/// A Wisp value
|
||||||
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
@ -19,3 +23,42 @@ pub enum Value<'a> {
|
||||||
String(Cow<'a, str>),
|
String(Cow<'a, str>),
|
||||||
Nil,
|
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}")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue