No longer mooo

This commit is contained in:
Erin 2022-07-28 15:18:00 +02:00 committed by ondra05
parent 00d9483dc8
commit 9469011643
2 changed files with 18 additions and 19 deletions

View file

@ -1,6 +1,5 @@
use ordered_float::OrderedFloat;
use std::{
borrow::Cow,
fmt::{Display, Write},
hash::Hash,
};
@ -40,18 +39,18 @@ impl<T: Hash> Hash for Spanned<T> {
/// A Wisp AST
#[derive(Debug, Clone, Hash, PartialEq)]
pub enum Expr<'a> {
pub enum Expr<'s> {
List(Vec<Spanned<Self>>),
Vector(Vec<Spanned<Self>>),
Pair((Box<Spanned<Self>>, Box<Spanned<Self>>)),
Quote(Box<Spanned<Self>>),
Symbol(Cow<'a, str>),
Keyword(Cow<'a, str>),
Symbol(&'s str),
Keyword(&'s str),
Number(OrderedFloat<f64>),
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<Spanned<Expr>>) -> std::fmt::Result {
fn fmt_list(f: &mut impl Write, iterable: &[Spanned<Expr>]) -> std::fmt::Result {
let mut iter = iterable.iter();
write!(f, "(")?;
if let Some(i) = iter.next() {

View file

@ -12,12 +12,12 @@ pub fn parse(src: &str) -> Result<Vec<Spanned<Expr>>, Vec<Simple<Token<'_>>>> {
parser().parse(Stream::from_iter(len..len + 1, lexer.spanned()))
}
fn parser<'a>() -> impl Parser<Token<'a>, Vec<Spanned<Expr<'a>>>, Error = Simple<Token<'a>>> {
fn parser<'s>() -> impl Parser<Token<'s>, Vec<Spanned<Expr<'s>>>, Error = Simple<Token<'s>>> {
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
)