parse loop and recur

This commit is contained in:
Scott Richmond 2024-11-21 16:41:46 -05:00
parent 56e6712154
commit dbff31be40
3 changed files with 143 additions and 60 deletions

View File

@ -13,7 +13,9 @@
// - [x] do this to extract/simplify/DRY things like tuple patterns, fn clauses, etc. // - [x] do this to extract/simplify/DRY things like tuple patterns, fn clauses, etc.
// * [x] Work around chumsky::Stream::from_iter().spanned disappearing in most recent version // * [x] Work around chumsky::Stream::from_iter().spanned disappearing in most recent version
// * [x] investigate using labels (which is behind a compiler flag, somehow) // * [x] investigate using labels (which is behind a compiler flag, somehow)
// * [ ] write parsing errors
// * [ ] wire up Ariadne parsing errors // * [ ] wire up Ariadne parsing errors
// * [ ] add stack traces and code locations to panics
// * [ ] validation // * [ ] validation
// * [x] break this out into multiple files // * [x] break this out into multiple files
// * [x] write a tree-walk VM // * [x] write a tree-walk VM
@ -24,7 +26,7 @@
// * [x] guards in match clauses // * [x] guards in match clauses
// * [x] `as` patterns // * [x] `as` patterns
// * [x] splat patterns in tuples, lists, dicts // * [x] splat patterns in tuples, lists, dicts
// * [ ] splats in list and dict literals // * [x] splats in list and dict literals
// * [ ] `loop` and `recur` // * [ ] `loop` and `recur`
// * [ ] string patterns // * [ ] string patterns
// * [ ] string interpolation // * [ ] string interpolation
@ -55,14 +57,13 @@ use crate::base::*;
pub fn main() { pub fn main() {
let src = " let src = "
fn t () -> true let foo = :foo
fn f () -> false loop (foo) with {
(:foo) -> :done
fn id { (x as :bool) -> recur (:bar)
(x) if f () -> x (x as :number) -> recur (:foo)
(x) -> :whoops (x as :keyword) -> recur (:foo)
} }
id (:foo)
"; ";
let (tokens, lex_errs) = lexer().parse(src).into_output_errors(); let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
if lex_errs.len() > 0 { if lex_errs.len() > 0 {

View File

@ -32,17 +32,17 @@ impl<'src> fmt::Display for MatchClause<'src> {
} }
} }
#[derive(Clone, Debug, PartialEq)] // #[derive(Clone, Debug, PartialEq)]
pub struct Pair<'src> { // pub struct Pair<'src> {
pub key: &'src str, // pub key: &'src str,
pub value: Spanned<Ast<'src>>, // pub value: Spanned<Ast<'src>>,
} // }
impl<'src> fmt::Display for Pair<'src> { // impl<'src> fmt::Display for Pair<'src> {
fn fmt(self: &Pair<'src>, f: &mut fmt::Formatter) -> fmt::Result { // fn fmt(self: &Pair<'src>, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "pair: {}: {}", self.key, self.value.0) // write!(f, "pair: {}: {}", self.key, self.value.0)
} // }
} // }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Ast<'src> { pub enum Ast<'src> {
@ -59,7 +59,7 @@ pub enum Ast<'src> {
Tuple(Vec<Spanned<Self>>), Tuple(Vec<Spanned<Self>>),
Arguments(Vec<Spanned<Self>>), Arguments(Vec<Spanned<Self>>),
List(Vec<Spanned<Self>>), List(Vec<Spanned<Self>>),
Dict(Vec<Pair<'src>>), Dict(Vec<Spanned<Self>>),
Let(Box<Spanned<Pattern<'src>>>, Box<Spanned<Self>>), Let(Box<Spanned<Pattern<'src>>>, Box<Spanned<Self>>),
Box(&'src str, Box<Spanned<Self>>), Box(&'src str, Box<Spanned<Self>>),
Synthetic(Box<Spanned<Self>>, Box<Spanned<Self>>, Vec<Spanned<Self>>), Synthetic(Box<Spanned<Self>>, Box<Spanned<Self>>, Vec<Spanned<Self>>),
@ -70,8 +70,10 @@ pub enum Ast<'src> {
Panic(Box<Spanned<Self>>), Panic(Box<Spanned<Self>>),
Do(Vec<Spanned<Self>>), Do(Vec<Spanned<Self>>),
Repeat(Box<Spanned<Self>>, Box<Spanned<Self>>), Repeat(Box<Spanned<Self>>, Box<Spanned<Self>>),
// Loop(Box<Spanned<Self>>, Vec<Spanned<Self>>), Splat(&'src str),
// Recur(Vec<Spanned<Self>>), Pair(&'src str, Box<Spanned<Self>>),
Loop(Box<Spanned<Self>>, Vec<MatchClause<'src>>),
Recur(Vec<Spanned<Self>>),
} }
impl<'src> fmt::Display for Ast<'src> { impl<'src> fmt::Display for Ast<'src> {
@ -105,7 +107,7 @@ impl<'src> fmt::Display for Ast<'src> {
"#{{{}}}", "#{{{}}}",
entries entries
.iter() .iter()
.map(|pair| pair.to_string()) .map(|pair| (*pair).0.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", ") .join(", ")
), ),
@ -184,8 +186,33 @@ impl<'src> fmt::Display for Ast<'src> {
) )
} }
Ast::Repeat(_times, _body) => todo!(), Ast::Repeat(_times, _body) => todo!(),
// Ast::Loop(init, body) => todo!(), Ast::Splat(word) => {
// Ast::Recur(args) => todo!(), write!(f, "splat: {}", word)
}
Ast::Pair(k, v) => {
write!(f, "pair: {} {}", k, (*v).0.to_string())
}
Ast::Loop(init, body) => {
write!(
f,
"loop: {} with {}",
(*init).0.to_string(),
body.iter()
.map(|clause| clause.to_string())
.collect::<Vec<_>>()
.join("\n")
)
}
Ast::Recur(args) => {
write!(
f,
"recur: {}",
args.iter()
.map(|(arg, _)| arg.to_string())
.collect::<Vec<_>>()
.join(", ")
)
}
} }
} }
} }
@ -428,8 +455,22 @@ where
) )
}); });
let splat = just(Token::Punctuation("..."))
.ignore_then(word.clone())
.map_with(|(w, _), e| {
(
Ast::Splat(if let Ast::Word(w) = w {
w
} else {
unreachable!()
}),
e.span(),
)
});
let list = simple let list = simple
.clone() .clone()
.or(splat.clone())
.separated_by(separators.clone()) .separated_by(separators.clone())
.allow_leading() .allow_leading()
.allow_trailing() .allow_trailing()
@ -439,15 +480,14 @@ where
let pair = select! {Token::Keyword(k) => k} let pair = select! {Token::Keyword(k) => k}
.then(simple.clone()) .then(simple.clone())
.map_with(|(key, value), e| Pair { key, value }); .map_with(|(key, value), e| (Ast::Pair(key, Box::new(value)), e.span()));
let shorthand = select! {Token::Word(w) => w}.map_with(|w, e| Pair { let shorthand = select! {Token::Word(w) => w}
key: w, .map_with(|w, e| (Ast::Pair(w, Box::new((Ast::Word(w), e.span()))), e.span()));
value: ((Ast::Word(w), e.span())),
});
let dict = pair let dict = pair
.or(shorthand) .or(shorthand)
.or(splat.clone())
.separated_by(separators.clone()) .separated_by(separators.clone())
.allow_leading() .allow_leading()
.allow_trailing() .allow_trailing()
@ -458,12 +498,22 @@ where
) )
.map_with(|dict, e| (Ast::Dict(dict), e.span())); .map_with(|dict, e| (Ast::Dict(dict), e.span()));
let recur = just(Token::Reserved("recur"))
.ignore_then(tuple.clone())
.map_with(|args, e| {
let (Ast::Tuple(args), _) = args else {
unreachable!()
};
(Ast::Recur(args), e.span())
});
simple.define( simple.define(
synthetic synthetic
.or(recur)
.or(word) .or(word)
.or(keyword) .or(keyword)
.or(value) .or(value)
.or(tuple) .or(tuple.clone())
.or(list) .or(list)
.or(dict) .or(dict)
.labelled("simple expression"), .labelled("simple expression"),
@ -599,7 +649,7 @@ where
}) })
.labelled("function clause"); .labelled("function clause");
let fn_clause = tuple_pattern let fn_unguarded = tuple_pattern
.clone() .clone()
.then_ignore(just(Token::Punctuation("->"))) .then_ignore(just(Token::Punctuation("->")))
.then(nonbinding.clone()) .then(nonbinding.clone())
@ -610,10 +660,28 @@ where
}) })
.labelled("function clause"); .labelled("function clause");
let fn_clause = fn_guarded.clone().or(fn_unguarded.clone());
let lambda = just(Token::Reserved("fn")) let lambda = just(Token::Reserved("fn"))
.ignore_then(fn_clause.clone()) .ignore_then(fn_unguarded.clone())
.map_with(|clause, e| (Ast::Fn("anonymous", vec![clause]), e.span())); .map_with(|clause, e| (Ast::Fn("anonymous", vec![clause]), e.span()));
let fn_multiclause = fn_clause
.clone()
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}")));
let fn_single_clause = fn_clause.clone().map_with(|c, e| vec![c]);
let r#loop = just(Token::Reserved("loop"))
.ignore_then(tuple.clone())
.then_ignore(just(Token::Reserved("with")))
.then(fn_multiclause.clone().or(fn_single_clause.clone()))
.map_with(|(init, body), e| (Ast::Loop(Box::new(init), body), e.span()));
nonbinding.define( nonbinding.define(
simple simple
.clone() .clone()
@ -623,6 +691,7 @@ where
.or(panic) .or(panic)
.or(do_) .or(do_)
.or(repeat) .or(repeat)
.or(r#loop)
.labelled("nonbinding expression"), .labelled("nonbinding expression"),
); );
@ -658,18 +727,9 @@ where
(Ast::FnDeclaration(name), e.span()) (Ast::FnDeclaration(name), e.span())
}); });
// let tuple_pattern = pattern
// .clone()
// .separated_by(separators.clone())
// .allow_leading()
// .allow_trailing()
// .collect()
// .delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
// .map_with(|tuple, e| (Pattern::Tuple(tuple), e.span()));
let fn_named = just(Token::Reserved("fn")) let fn_named = just(Token::Reserved("fn"))
.ignore_then(word.clone()) .ignore_then(word.clone())
.then(fn_clause.clone().or(fn_guarded.clone())) .then(fn_unguarded.clone())
.map_with(|(word, clause), e| { .map_with(|(word, clause), e| {
let name = if let Ast::Word(word) = word.0 { let name = if let Ast::Word(word) = word.0 {
word word
@ -681,16 +741,7 @@ where
let fn_compound = just(Token::Reserved("fn")) let fn_compound = just(Token::Reserved("fn"))
.ignore_then(word.clone()) .ignore_then(word.clone())
.then( .then(fn_multiclause.clone())
fn_clause
.clone()
.or(fn_guarded.clone())
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}"))),
)
.map_with(|(word, clauses), e| { .map_with(|(word, clauses), e| {
let name = if let Ast::Word(word) = word.0 { let name = if let Ast::Word(word) = word.0 {
word word

View File

@ -322,7 +322,19 @@ pub fn eval<'src, 'a>(
Ast::List(members) => { Ast::List(members) => {
let mut vect = Vector::new(); let mut vect = Vector::new();
for member in members { for member in members {
vect.push_back(eval(&member.0, ctx)?) if let Ast::Splat(_) = member.0 {
let to_splat = eval(&member.0, ctx)?;
match to_splat {
Value::List(list) => vect.append(list),
_ => {
return Err(LudusError {
msg: "only lists may be splatted into lists".to_string(),
})
}
}
} else {
vect.push_back(eval(&member.0, ctx)?)
}
} }
Ok(Value::List(vect)) Ok(Value::List(vect))
} }
@ -333,7 +345,7 @@ pub fn eval<'src, 'a>(
} }
Ok(Value::Tuple(Rc::new(vect))) Ok(Value::Tuple(Rc::new(vect)))
} }
Ast::Word(w) => { Ast::Word(w) | Ast::Splat(w) => {
let val = if let Some((_, value)) = ctx.iter().rev().find(|(name, _)| w == name) { let val = if let Some((_, value)) = ctx.iter().rev().find(|(name, _)| w == name) {
value.clone() value.clone()
} else { } else {
@ -372,11 +384,26 @@ pub fn eval<'src, 'a>(
Ok(Value::Tuple(Rc::new(args))) Ok(Value::Tuple(Rc::new(args)))
} }
} }
Ast::Dict(pairs) => { Ast::Dict(terms) => {
let mut dict = HashMap::new(); let mut dict = HashMap::new();
for Pair { key, value } in pairs { for term in terms {
let value = eval(&value.0, ctx)?; let (term, _) = term;
dict.insert(*key, value); match term {
Ast::Pair(key, value) => {
let value = eval(&value.0, ctx)?;
dict.insert(*key, value);
}
Ast::Splat(_) => {
let resolved = eval(term, ctx)?;
let Value::Dict(to_splat) = resolved else {
return Err(LudusError {
msg: "cannot splat non-dict into dict".to_string(),
});
};
dict = to_splat.union(dict);
}
_ => unreachable!(),
}
} }
Ok(Value::Dict(dict)) Ok(Value::Dict(dict))
} }
@ -451,7 +478,11 @@ pub fn eval<'src, 'a>(
result = apply(next, arg, ctx)?; result = apply(next, arg, ctx)?;
} }
Ok(result) Ok(result)
} // Ast::Loop(_, _) => todo!(), }
// Ast::Recur(_) => todo!(), Ast::Pair(..) => {
unreachable!()
}
Ast::Loop(_, _) => todo!(),
Ast::Recur(_) => todo!(),
} }
} }