improve do & recur errs

This commit is contained in:
Scott Richmond 2024-12-10 17:39:02 -05:00
parent 4c9659271b
commit e9b52e1488
2 changed files with 18 additions and 6 deletions

View File

@ -167,10 +167,7 @@ pub fn run(src: &'static str) {
pub fn main() { pub fn main() {
let src = " let src = "
match foo with { do 42 > recur ()
:a -> :b
:c -> :d
}
"; ";
run(src); run(src);
// struct_scalpel::print_dissection_info::<value::Value>() // struct_scalpel::print_dissection_info::<value::Value>()

View File

@ -282,6 +282,8 @@ impl<'a> Validator<'a> {
self.ast = expr; self.ast = expr;
self.span = *span; self.span = *span;
self.validate(); self.validate();
//TODO: check arity of call
} }
_ => unreachable!(), _ => unreachable!(),
} }
@ -389,13 +391,25 @@ impl<'a> Validator<'a> {
self.validate(); self.validate();
self.status.tail_position = tailpos; self.status.tail_position = tailpos;
} }
// TODO: fix the tail call here?
Ast::Do(terms) => { Ast::Do(terms) => {
for term in terms { if terms.len() < 2 {
return self.err("do expressions must have at least two terms".to_string());
}
for term in terms.iter().take(terms.len() - 1) {
let (expr, span) = term; let (expr, span) = term;
self.ast = expr; self.ast = expr;
self.span = *span; self.span = *span;
self.validate(); self.validate();
} }
let (expr, span) = terms.last().unwrap();
self.ast = expr;
self.span = *span;
if matches!(expr, Ast::Recur(_)) {
self.err("`recur` may not be used in `do` forms".to_string());
}
self.validate();
} }
Ast::Repeat(times, body) => { Ast::Repeat(times, body) => {
self.status.tail_position = false; self.status.tail_position = false;
@ -427,7 +441,8 @@ impl<'a> Validator<'a> {
} }
Ast::Recur(args) => { Ast::Recur(args) => {
if !self.status.in_loop { if !self.status.in_loop {
self.err("you may only use `recur` in a loop form".to_string()); self.err("you may only use `recur` in a `loop` form".to_string());
return;
} }
if !self.status.tail_position { if !self.status.tail_position {
self.err("you may only use `recur` in tail position".to_string()); self.err("you may only use `recur` in tail position".to_string());