No longer mooo
This commit is contained in:
parent
00d9483dc8
commit
9469011643
|
@ -1,6 +1,5 @@
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
|
||||||
fmt::{Display, Write},
|
fmt::{Display, Write},
|
||||||
hash::Hash,
|
hash::Hash,
|
||||||
};
|
};
|
||||||
|
@ -40,18 +39,18 @@ impl<T: Hash> Hash for Spanned<T> {
|
||||||
|
|
||||||
/// A Wisp AST
|
/// A Wisp AST
|
||||||
#[derive(Debug, Clone, Hash, PartialEq)]
|
#[derive(Debug, Clone, Hash, PartialEq)]
|
||||||
pub enum Expr<'a> {
|
pub enum Expr<'s> {
|
||||||
List(Vec<Spanned<Self>>),
|
List(Vec<Spanned<Self>>),
|
||||||
Vector(Vec<Spanned<Self>>),
|
Vector(Vec<Spanned<Self>>),
|
||||||
Pair((Box<Spanned<Self>>, Box<Spanned<Self>>)),
|
Pair((Box<Spanned<Self>>, Box<Spanned<Self>>)),
|
||||||
Quote(Box<Spanned<Self>>),
|
Quote(Box<Spanned<Self>>),
|
||||||
Symbol(Cow<'a, str>),
|
Symbol(&'s str),
|
||||||
Keyword(Cow<'a, str>),
|
Keyword(&'s str),
|
||||||
Number(OrderedFloat<f64>),
|
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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::List(list) => fmt_list(f, list),
|
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();
|
let mut iter = iterable.iter();
|
||||||
write!(f, "(")?;
|
write!(f, "(")?;
|
||||||
if let Some(i) = iter.next() {
|
if let Some(i) = iter.next() {
|
||||||
|
|
|
@ -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()))
|
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| {
|
recursive(|expr| {
|
||||||
let atom = select! {
|
let atom = select! {
|
||||||
Token::Symbol(s) => Expr::Symbol(s.into()),
|
Token::Symbol(s) => Expr::Symbol(s),
|
||||||
Token::Keyword(k) => Expr::Keyword(k.into()),
|
Token::Keyword(k) => Expr::Keyword(k),
|
||||||
Token::String(s) => Expr::String(s.into()),
|
Token::String(s) => Expr::String(s),
|
||||||
Token::Number(n) => Expr::Number(n),
|
Token::Number(n) => Expr::Number(n),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,21 +66,21 @@ mod tests {
|
||||||
parse(r#"(defun hello (name) (log (concat "Hello, " name "!")))"#).unwrap(),
|
parse(r#"(defun hello (name) (log (concat "Hello, " name "!")))"#).unwrap(),
|
||||||
&[Spanned::new(
|
&[Spanned::new(
|
||||||
Expr::List(vec![
|
Expr::List(vec![
|
||||||
Spanned::new(Expr::Symbol("defun".into()), 1..6),
|
Spanned::new(Expr::Symbol("defun"), 1..6),
|
||||||
Spanned::new(Expr::Symbol("hello".into()), 7..12),
|
Spanned::new(Expr::Symbol("hello"), 7..12),
|
||||||
Spanned::new(
|
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,
|
13..19,
|
||||||
),
|
),
|
||||||
Spanned::new(
|
Spanned::new(
|
||||||
Expr::List(vec![
|
Expr::List(vec![
|
||||||
Spanned::new(Expr::Symbol("log".into()), 21..24),
|
Spanned::new(Expr::Symbol("log"), 21..24),
|
||||||
Spanned::new(
|
Spanned::new(
|
||||||
Expr::List(vec![
|
Expr::List(vec![
|
||||||
Spanned::new(Expr::Symbol("concat".into()), 26..32),
|
Spanned::new(Expr::Symbol("concat"), 26..32),
|
||||||
Spanned::new(Expr::String("Hello, ".into()), 33..42),
|
Spanned::new(Expr::String("Hello, "), 33..42),
|
||||||
Spanned::new(Expr::Symbol("name".into()), 43..47),
|
Spanned::new(Expr::Symbol("name"), 43..47),
|
||||||
Spanned::new(Expr::String("!".into()), 48..51),
|
Spanned::new(Expr::String("!"), 48..51),
|
||||||
]),
|
]),
|
||||||
48..51
|
48..51
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue