Simplified syntax

This commit is contained in:
Erin 2022-07-26 18:57:39 +02:00 committed by ondra05
parent cca325033c
commit b7240fa1c1
3 changed files with 27 additions and 56 deletions

View file

@ -43,7 +43,6 @@ impl<T: Hash> Hash for Spanned<T> {
pub enum Expr<'a> { pub enum Expr<'a> {
List(Vec<Spanned<Self>>), List(Vec<Spanned<Self>>),
Vector(Vec<Spanned<Self>>), Vector(Vec<Spanned<Self>>),
Map(Vec<(Spanned<Self>, Spanned<Self>)>),
Quote(Box<Spanned<Self>>), Quote(Box<Spanned<Self>>),
Symbol(Cow<'a, str>), Symbol(Cow<'a, str>),
Keyword(Cow<'a, str>), Keyword(Cow<'a, str>),
@ -54,9 +53,11 @@ pub enum Expr<'a> {
impl<'a> Display for Expr<'a> { impl<'a> Display for Expr<'a> {
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) => write_seq(f, list, "(", ")"), Self::List(list) => fmt_list(f, list),
Self::Vector(vec) => write_seq(f, vec, "[", "]"), Self::Vector(vec) => {
Self::Map(map) => write_seq(f, map.iter().map(|(k, v)| format!("{k} {v}")), "{", "}"), write!(f, "#")?;
fmt_list(f, vec)
}
Self::Quote(expr) => write!(f, "'{expr}"), Self::Quote(expr) => write!(f, "'{expr}"),
Self::Symbol(sym) => write!(f, "{sym}"), Self::Symbol(sym) => write!(f, "{sym}"),
Self::Keyword(kw) => write!(f, ":{kw}"), Self::Keyword(kw) => write!(f, ":{kw}"),
@ -66,19 +67,14 @@ impl<'a> Display for Expr<'a> {
} }
} }
fn write_seq( fn fmt_list(f: &mut impl Write, iterable: &Vec<Spanned<Expr>>) -> std::fmt::Result {
f: &mut impl Write, let mut iter = iterable.iter();
iterable: impl IntoIterator<Item = impl Display>, write!(f, "(")?;
delimiter_left: &str, if let Some(i) = iter.next() {
delimiter_right: &str, write!(f, "{i}")?;
) -> std::fmt::Result {
let mut iter = iterable.into_iter();
write!(f, "{delimiter_left}")?;
if let Some(x) = iter.next() {
write!(f, "{x}")?;
} }
for x in iter { for i in iter {
write!(f, " {x}")?; write!(f, " {i}")?;
} }
write!(f, "{delimiter_right}") write!(f, ")")
} }

View file

@ -11,21 +11,12 @@ pub enum Token<'a> {
#[token(")")] #[token(")")]
RightParen, RightParen,
#[token("[")]
LeftBracket,
#[token("]")]
RightBracket,
#[token("{")]
LeftCurly,
#[token["}"]]
RightCurly,
#[token("'")] #[token("'")]
Quote, Quote,
#[token("#")]
Octothrope,
// Values // Values
#[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))] #[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))]
String(&'a str), String(&'a str),
@ -60,11 +51,8 @@ impl<'a> Display for Token<'a> {
match self { match self {
Token::LeftParen => write!(f, "("), Token::LeftParen => write!(f, "("),
Token::RightParen => 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::Quote => write!(f, "'"),
Token::Octothrope => write!(f, "#"),
Token::String(s) => write!(f, "\"{s}\""), Token::String(s) => write!(f, "\"{s}\""),
Token::Number(n) => write!(f, "{n}"), Token::Number(n) => write!(f, "{n}"),
Token::Symbol(sym) => write!(f, "{sym}"), Token::Symbol(sym) => write!(f, "{sym}"),
@ -85,15 +73,12 @@ mod tests {
#[test] #[test]
fn gibberish() { fn gibberish() {
assert_lex( assert_lex(
"(][)}'{\"ABLE\"corp :wisp 6.2831853071¨", "()'#\"ABLE\"corp :wisp 6.2831853071¨",
&[ &[
LeftParen, LeftParen,
RightBracket,
LeftBracket,
RightParen, RightParen,
RightCurly,
Quote, Quote,
LeftCurly, Octothrope,
String("ABLE"), String("ABLE"),
Symbol("corp"), Symbol("corp"),
Keyword("wisp"), Keyword("wisp"),
@ -106,15 +91,15 @@ mod tests {
#[test] #[test]
fn function() { fn function() {
assert_lex( 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, LeftParen,
Symbol("defun"), Symbol("defun"),
Symbol("print-add"), Symbol("print-add"),
LeftBracket, LeftParen,
Symbol("x"), Symbol("x"),
Symbol("y"), Symbol("y"),
RightBracket, RightParen,
LeftParen, LeftParen,
Symbol("println"), Symbol("println"),
LeftParen, LeftParen,

View file

@ -24,22 +24,13 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Spanned<Expr<'a>>>, Error = Simple
let list = expr let list = expr
.clone() .clone()
.repeated() .repeated()
.map(Expr::List)
.delimited_by(just(Token::LeftParen), just(Token::RightParen)); .delimited_by(just(Token::LeftParen), just(Token::RightParen));
let vector = expr let vector = just(Token::Octothrope)
.clone() .ignore_then(list.clone())
.repeated() .map(Expr::Vector);
.map(Expr::Vector)
.delimited_by(just(Token::LeftBracket), just(Token::RightBracket));
let map = expr let list = list.map(Expr::List);
.clone()
.then(expr.clone())
.repeated()
.collect()
.map(Expr::Map)
.delimited_by(just(Token::LeftCurly), just(Token::RightCurly));
let quote = just(Token::Quote) let quote = just(Token::Quote)
.ignore_then(expr) .ignore_then(expr)
@ -48,7 +39,6 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Spanned<Expr<'a>>>, Error = Simple
atom.or(list) atom.or(list)
.or(vector) .or(vector)
.or(map)
.or(quote) .or(quote)
.map_with_span(Spanned::new) .map_with_span(Spanned::new)
}) })