diff --git a/src/list.rs b/src/list.rs index d3c05e5..dec4527 100644 --- a/src/list.rs +++ b/src/list.rs @@ -2,14 +2,14 @@ use crate::value::Value; /// Single-linked list #[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub enum List { - Cons(Value, Box), +pub enum List<'a> { + Cons(Value<'a>, Box), #[default] Nil, } -impl List { - pub fn from_vec(vec: Vec) -> Self { +impl<'a> List<'a> { + pub fn from_vec(vec: Vec>) -> Self { vec.into_iter() .rev() .fold(Self::Nil, |list, next| Self::Cons(next, Box::new(list))) diff --git a/src/parser.rs b/src/parser.rs index 6a25819..4af04dc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -7,15 +7,15 @@ pub fn read(src: &str) -> Result, Vec>>> { parser().parse(stream_of_lexer(Token::lexer(src))) } -fn parser<'a>() -> impl Parser, Vec, Error = Simple>> { +fn parser<'a>() -> impl Parser, Vec>, Error = Simple>> { recursive(|value| { let atom = select! { Token::Symbol("true") => Value::Bool(true), Token::Symbol("false") => Value::Bool(false), Token::Symbol("nil") => Value::Nil, - Token::Symbol(s) => Value::Symbol(s.to_owned()), - Token::Keyword(k) => Value::Keyword(k.to_owned()), - Token::String(s) => Value::String(s.to_owned()), + Token::Symbol(s) => Value::Symbol(s.into()), + Token::Keyword(k) => Value::Keyword(k.into()), + Token::String(s) => Value::String(s.into()), Token::Number(n) => Value::Number(n), }; @@ -44,7 +44,7 @@ fn parser<'a>() -> impl Parser, Vec, Error = Simple>> let quote = just(Token::Quote).ignore_then(value).map(|value| { Value::List(Box::new(List::Cons( - Value::Symbol("quote".to_owned()), + Value::Symbol("quote".into()), Box::new(List::Cons(value, Box::new(List::Nil))), ))) }); diff --git a/src/value.rs b/src/value.rs index 79f66f1..d44c1a6 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,18 +1,21 @@ use crate::list::List; use ordered_float::OrderedFloat; -use std::collections::BTreeMap; +use std::{borrow::Cow, collections::BTreeMap}; /// A Wisp value #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub enum Value { - List(Box), +pub enum Value<'a> { + List(Box>), Vector(Vec), Map(BTreeMap), - Symbol(String), - Keyword(String), - Function { args: Vec, body: Box }, + Symbol(Cow<'a, str>), + Keyword(Cow<'a, str>), + Function { + args: Vec>, + body: Box>, + }, Bool(bool), Number(OrderedFloat), - String(String), + String(Cow<'a, str>), Nil, }