From b7240fa1c1bdcb383bd375db84799e7511614261 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 26 Jul 2022 18:57:39 +0200 Subject: [PATCH] Simplified syntax --- src/syntax/ast.rs | 30 +++++++++++++----------------- src/syntax/lexer.rs | 33 +++++++++------------------------ src/syntax/parser.rs | 20 +++++--------------- 3 files changed, 27 insertions(+), 56 deletions(-) diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 43362f7..16d9425 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -43,7 +43,6 @@ impl Hash for Spanned { pub enum Expr<'a> { List(Vec>), Vector(Vec>), - Map(Vec<(Spanned, Spanned)>), Quote(Box>), Symbol(Cow<'a, str>), Keyword(Cow<'a, str>), @@ -54,9 +53,11 @@ pub enum Expr<'a> { impl<'a> Display for Expr<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::List(list) => write_seq(f, list, "(", ")"), - Self::Vector(vec) => write_seq(f, vec, "[", "]"), - Self::Map(map) => write_seq(f, map.iter().map(|(k, v)| format!("{k} {v}")), "{", "}"), + Self::List(list) => fmt_list(f, list), + Self::Vector(vec) => { + write!(f, "#")?; + fmt_list(f, vec) + } Self::Quote(expr) => write!(f, "'{expr}"), Self::Symbol(sym) => write!(f, "{sym}"), Self::Keyword(kw) => write!(f, ":{kw}"), @@ -66,19 +67,14 @@ impl<'a> Display for Expr<'a> { } } -fn write_seq( - f: &mut impl Write, - iterable: impl IntoIterator, - 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}")?; +fn fmt_list(f: &mut impl Write, iterable: &Vec>) -> std::fmt::Result { + let mut iter = iterable.iter(); + write!(f, "(")?; + if let Some(i) = iter.next() { + write!(f, "{i}")?; } - for x in iter { - write!(f, " {x}")?; + for i in iter { + write!(f, " {i}")?; } - write!(f, "{delimiter_right}") + write!(f, ")") } diff --git a/src/syntax/lexer.rs b/src/syntax/lexer.rs index 251c079..c4f96c6 100644 --- a/src/syntax/lexer.rs +++ b/src/syntax/lexer.rs @@ -11,21 +11,12 @@ pub enum Token<'a> { #[token(")")] RightParen, - #[token("[")] - LeftBracket, - - #[token("]")] - RightBracket, - - #[token("{")] - LeftCurly, - - #[token["}"]] - RightCurly, - #[token("'")] Quote, + #[token("#")] + Octothrope, + // Values #[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))] String(&'a str), @@ -60,11 +51,8 @@ impl<'a> Display for Token<'a> { match self { Token::LeftParen => write!(f, "("), Token::RightParen => write!(f, ")"), - Token::LeftBracket => write!(f, "["), - Token::RightBracket => write!(f, "]"), - Token::LeftCurly => write!(f, "{{"), - Token::RightCurly => write!(f, "}}"), Token::Quote => write!(f, "'"), + Token::Octothrope => write!(f, "#"), Token::String(s) => write!(f, "\"{s}\""), Token::Number(n) => write!(f, "{n}"), Token::Symbol(sym) => write!(f, "{sym}"), @@ -85,15 +73,12 @@ mod tests { #[test] fn gibberish() { assert_lex( - "(][)}'{\"ABLE\"corp :wisp 6.2831853071¨", + "()'#\"ABLE\"corp :wisp 6.2831853071¨", &[ LeftParen, - RightBracket, - LeftBracket, RightParen, - RightCurly, Quote, - LeftCurly, + Octothrope, String("ABLE"), Symbol("corp"), Keyword("wisp"), @@ -106,15 +91,15 @@ mod tests { #[test] fn function() { assert_lex( - "(defun print-add [x y] (println (add x y))) (print-add 42 12)", + "(defun print-add (x y) (println (add x y))) (print-add 42 12)", &[ LeftParen, Symbol("defun"), Symbol("print-add"), - LeftBracket, + LeftParen, Symbol("x"), Symbol("y"), - RightBracket, + RightParen, LeftParen, Symbol("println"), LeftParen, diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index e4dd914..07fb92e 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -24,22 +24,13 @@ fn parser<'a>() -> impl Parser, Vec>>, Error = Simple let list = expr .clone() .repeated() - .map(Expr::List) .delimited_by(just(Token::LeftParen), just(Token::RightParen)); - let vector = expr - .clone() - .repeated() - .map(Expr::Vector) - .delimited_by(just(Token::LeftBracket), just(Token::RightBracket)); - - let map = expr - .clone() - .then(expr.clone()) - .repeated() - .collect() - .map(Expr::Map) - .delimited_by(just(Token::LeftCurly), just(Token::RightCurly)); + let vector = just(Token::Octothrope) + .ignore_then(list.clone()) + .map(Expr::Vector); + + let list = list.map(Expr::List); let quote = just(Token::Quote) .ignore_then(expr) @@ -48,7 +39,6 @@ fn parser<'a>() -> impl Parser, Vec>>, Error = Simple atom.or(list) .or(vector) - .or(map) .or(quote) .map_with_span(Spanned::new) })