cl-ast: Cleanup

- Function bind is now one Pattern
- TyRef now allows &Ty (i.e. &[i32], &(char, bool)
- Range patterns (they cannot bind, only check whether a value is in range
- ArrayRep repeat has been reverted to usize, for now, until early consteval is implemented.
This commit is contained in:
2025-04-21 04:17:45 -04:00
parent ef92d8b798
commit 7ba808594c
15 changed files with 252 additions and 134 deletions

View File

@@ -100,7 +100,7 @@ pub trait Fold {
Function {
name: self.fold_sym(name),
sign: self.fold_ty_fn(sign),
bind: bind.into_iter().map(|p| self.fold_pattern(p)).collect(),
bind: self.fold_pattern(bind),
body: body.map(|b| self.fold_expr(b)),
}
}
@@ -184,7 +184,7 @@ pub trait Fold {
}
fn fold_ty_ref(&mut self, t: TyRef) -> TyRef {
let TyRef { mutable, count, to } = t;
TyRef { mutable: self.fold_mutability(mutable), count, to: self.fold_path(to) }
TyRef { mutable: self.fold_mutability(mutable), count, to: Box::new(self.fold_ty(*to)) }
}
fn fold_ty_fn(&mut self, t: TyFn) -> TyFn {
let TyFn { args, rety } = t;
@@ -246,6 +246,14 @@ pub trait Fold {
self.fold_mutability(mutability),
Box::new(self.fold_pattern(*pattern)),
),
Pattern::RangeExc(head, tail) => Pattern::RangeInc(
Box::new(self.fold_pattern(*head)),
Box::new(self.fold_pattern(*tail)),
),
Pattern::RangeInc(head, tail) => Pattern::RangeInc(
Box::new(self.fold_pattern(*head)),
Box::new(self.fold_pattern(*tail)),
),
Pattern::Tuple(patterns) => {
Pattern::Tuple(patterns.into_iter().map(|p| self.fold_pattern(p)).collect())
}
@@ -356,10 +364,7 @@ pub trait Fold {
}
fn fold_array_rep(&mut self, a: ArrayRep) -> ArrayRep {
let ArrayRep { value, repeat } = a;
ArrayRep {
value: Box::new(self.fold_expr(*value)),
repeat: Box::new(self.fold_expr(*repeat)),
}
ArrayRep { value: Box::new(self.fold_expr(*value)), repeat }
}
fn fold_addrof(&mut self, a: AddrOf) -> AddrOf {
let AddrOf { mutable, expr } = a;

View File

@@ -82,7 +82,7 @@ pub trait Visit<'a>: Sized {
let Function { name, sign, bind, body } = f;
self.visit_sym(name);
self.visit_ty_fn(sign);
bind.iter().for_each(|p| self.visit_pattern(p));
self.visit_pattern(bind);
if let Some(b) = body {
self.visit_expr(b)
}
@@ -154,7 +154,7 @@ pub trait Visit<'a>: Sized {
fn visit_ty_ref(&mut self, t: &'a TyRef) {
let TyRef { mutable, count: _, to } = t;
self.visit_mutability(mutable);
self.visit_path(to);
self.visit_ty(to);
}
fn visit_ty_fn(&mut self, t: &'a TyFn) {
let TyFn { args, rety } = t;
@@ -215,6 +215,14 @@ pub trait Visit<'a>: Sized {
self.visit_mutability(mutability);
self.visit_pattern(pattern);
}
Pattern::RangeExc(head, tail) => {
self.visit_pattern(head);
self.visit_pattern(tail);
}
Pattern::RangeInc(head, tail) => {
self.visit_pattern(head);
self.visit_pattern(tail);
}
Pattern::Tuple(patterns) => {
patterns.iter().for_each(|p| self.visit_pattern(p));
}
@@ -311,9 +319,8 @@ pub trait Visit<'a>: Sized {
values.iter().for_each(|e| self.visit_expr(e))
}
fn visit_array_rep(&mut self, a: &'a ArrayRep) {
let ArrayRep { value, repeat } = a;
let ArrayRep { value, repeat: _ } = a;
self.visit_expr(value);
self.visit_expr(repeat);
}
fn visit_addrof(&mut self, a: &'a AddrOf) {
let AddrOf { mutable, expr } = a;