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> {
List(Vec<Spanned<Self>>),
Vector(Vec<Spanned<Self>>),
Map(Vec<(Spanned<Self>, Spanned<Self>)>),
Quote(Box<Spanned<Self>>),
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<Item = impl Display>,
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<Spanned<Expr>>) -> 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, ")")
}

View file

@ -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,

View file

@ -24,22 +24,13 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Spanned<Expr<'a>>>, 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<Token<'a>, Vec<Spanned<Expr<'a>>>, Error = Simple
atom.or(list)
.or(vector)
.or(map)
.or(quote)
.map_with_span(Spanned::new)
})