From 94690116431856b77fa944582a7070ece6f8553e Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 28 Jul 2022 15:18:00 +0200 Subject: [PATCH] No longer mooo --- src/syntax/ast.rs | 13 ++++++------- src/syntax/parser.rs | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 6bfc236..e61d08f 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -1,6 +1,5 @@ use ordered_float::OrderedFloat; use std::{ - borrow::Cow, fmt::{Display, Write}, hash::Hash, }; @@ -40,18 +39,18 @@ impl Hash for Spanned { /// A Wisp AST #[derive(Debug, Clone, Hash, PartialEq)] -pub enum Expr<'a> { +pub enum Expr<'s> { List(Vec>), Vector(Vec>), Pair((Box>, Box>)), Quote(Box>), - Symbol(Cow<'a, str>), - Keyword(Cow<'a, str>), + Symbol(&'s str), + Keyword(&'s str), Number(OrderedFloat), - String(Cow<'a, str>), + String(&'s str), } -impl<'a> Display for Expr<'a> { +impl<'s> Display for Expr<'s> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::List(list) => fmt_list(f, list), @@ -69,7 +68,7 @@ impl<'a> Display for Expr<'a> { } } -fn fmt_list(f: &mut impl Write, iterable: &Vec>) -> std::fmt::Result { +fn fmt_list(f: &mut impl Write, iterable: &[Spanned]) -> std::fmt::Result { let mut iter = iterable.iter(); write!(f, "(")?; if let Some(i) = iter.next() { diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index bc16f55..7d1acf1 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -12,12 +12,12 @@ pub fn parse(src: &str) -> Result>, Vec>>> { parser().parse(Stream::from_iter(len..len + 1, lexer.spanned())) } -fn parser<'a>() -> impl Parser, Vec>>, Error = Simple>> { +fn parser<'s>() -> impl Parser, Vec>>, Error = Simple>> { recursive(|expr| { let atom = select! { - Token::Symbol(s) => Expr::Symbol(s.into()), - Token::Keyword(k) => Expr::Keyword(k.into()), - Token::String(s) => Expr::String(s.into()), + Token::Symbol(s) => Expr::Symbol(s), + Token::Keyword(k) => Expr::Keyword(k), + Token::String(s) => Expr::String(s), Token::Number(n) => Expr::Number(n), }; @@ -66,21 +66,21 @@ mod tests { parse(r#"(defun hello (name) (log (concat "Hello, " name "!")))"#).unwrap(), &[Spanned::new( Expr::List(vec![ - Spanned::new(Expr::Symbol("defun".into()), 1..6), - Spanned::new(Expr::Symbol("hello".into()), 7..12), + Spanned::new(Expr::Symbol("defun"), 1..6), + Spanned::new(Expr::Symbol("hello"), 7..12), Spanned::new( - Expr::List(vec![Spanned::new(Expr::Symbol("name".into()), 14..18),]), + Expr::List(vec![Spanned::new(Expr::Symbol("name"), 14..18),]), 13..19, ), Spanned::new( Expr::List(vec![ - Spanned::new(Expr::Symbol("log".into()), 21..24), + Spanned::new(Expr::Symbol("log"), 21..24), Spanned::new( Expr::List(vec![ - Spanned::new(Expr::Symbol("concat".into()), 26..32), - Spanned::new(Expr::String("Hello, ".into()), 33..42), - Spanned::new(Expr::Symbol("name".into()), 43..47), - Spanned::new(Expr::String("!".into()), 48..51), + Spanned::new(Expr::Symbol("concat"), 26..32), + Spanned::new(Expr::String("Hello, "), 33..42), + Spanned::new(Expr::Symbol("name"), 43..47), + Spanned::new(Expr::String("!"), 48..51), ]), 48..51 )