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:
@@ -129,7 +129,7 @@ pub struct Static {
|
||||
pub struct Function {
|
||||
pub name: Sym,
|
||||
pub sign: TyFn,
|
||||
pub bind: Vec<Pattern>,
|
||||
pub bind: Pattern,
|
||||
pub body: Option<Expr>,
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ pub struct TyTuple {
|
||||
pub struct TyRef {
|
||||
pub mutable: Mutability,
|
||||
pub count: u16,
|
||||
pub to: Path,
|
||||
pub to: Box<Ty>,
|
||||
}
|
||||
|
||||
/// The args and return value for a function pointer [Ty]pe
|
||||
@@ -360,7 +360,7 @@ pub enum ExprKind {
|
||||
While(While),
|
||||
/// An [If] expression: `if` [`Expr`] [`Block`] [`Else`]?
|
||||
If(If),
|
||||
/// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]?
|
||||
/// A [For] expression: `for` [`Pattern`] `in` [`Expr`] [`Block`] [`Else`]?
|
||||
For(For),
|
||||
/// A [Break] expression: `break` [`Expr`]?
|
||||
Break(Break),
|
||||
@@ -392,6 +392,8 @@ pub enum Pattern {
|
||||
Literal(Literal),
|
||||
Rest(Option<Box<Pattern>>),
|
||||
Ref(Mutability, Box<Pattern>),
|
||||
RangeExc(Box<Pattern>, Box<Pattern>),
|
||||
RangeInc(Box<Pattern>, Box<Pattern>),
|
||||
Tuple(Vec<Pattern>),
|
||||
Array(Vec<Pattern>),
|
||||
Struct(Path, Vec<(Sym, Option<Pattern>)>),
|
||||
@@ -557,7 +559,7 @@ pub struct Array {
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ArrayRep {
|
||||
pub value: Box<Expr>,
|
||||
pub repeat: Box<Expr>,
|
||||
pub repeat: usize,
|
||||
}
|
||||
|
||||
/// An address-of expression: `&` `mut`? [`Expr`]
|
||||
|
||||
@@ -140,6 +140,14 @@ impl TryFrom<Expr> for Pattern {
|
||||
};
|
||||
Pattern::TupleStruct(path, args)
|
||||
}
|
||||
ExprKind::Binary(Binary { kind: BinaryKind::RangeExc, parts }) => {
|
||||
let (head, tail) = (Pattern::try_from(parts.0)?, Pattern::try_from(parts.1)?);
|
||||
Pattern::RangeExc(head.into(), tail.into())
|
||||
}
|
||||
ExprKind::Binary(Binary { kind: BinaryKind::RangeInc, parts }) => {
|
||||
let (head, tail) = (Pattern::try_from(parts.0)?, Pattern::try_from(parts.1)?);
|
||||
Pattern::RangeInc(head.into(), tail.into())
|
||||
}
|
||||
ExprKind::Unary(Unary { kind: UnaryKind::RangeExc, tail }) => {
|
||||
Pattern::Rest(Some(Pattern::try_from(*tail)?.into()))
|
||||
}
|
||||
|
||||
@@ -161,11 +161,19 @@ impl Display for Function {
|
||||
Default::default()
|
||||
}
|
||||
};
|
||||
let bind = match bind {
|
||||
Pattern::Tuple(patterns) => patterns.as_slice(),
|
||||
_ => {
|
||||
write!(f, "Invalid argument binder: {bind}")?;
|
||||
Default::default()
|
||||
}
|
||||
};
|
||||
|
||||
debug_assert_eq!(bind.len(), types.len());
|
||||
write!(f, "fn {name} ")?;
|
||||
{
|
||||
let mut f = f.delimit(INLINE_PARENS);
|
||||
|
||||
for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() {
|
||||
if idx != 0 {
|
||||
f.write_str(", ")?;
|
||||
@@ -453,6 +461,8 @@ impl Display for Pattern {
|
||||
Pattern::Rest(Some(name)) => write!(f, "..{name}"),
|
||||
Pattern::Rest(None) => "..".fmt(f),
|
||||
Pattern::Ref(mutability, pattern) => write!(f, "&{mutability}{pattern}"),
|
||||
Pattern::RangeExc(head, tail) => write!(f, "{head}..{tail}"),
|
||||
Pattern::RangeInc(head, tail) => write!(f, "{head}..={tail}"),
|
||||
Pattern::Tuple(patterns) => separate(patterns, ", ")(f.delimit(INLINE_PARENS)),
|
||||
Pattern::Array(patterns) => separate(patterns, ", ")(f.delimit(INLINE_SQUARE)),
|
||||
Pattern::Struct(path, items) => {
|
||||
|
||||
@@ -342,6 +342,8 @@ impl WeightOf for Pattern {
|
||||
Pattern::Rest(Some(pattern)) => pattern.weight_of(),
|
||||
Pattern::Rest(None) => 0,
|
||||
Pattern::Ref(mutability, pattern) => mutability.weight_of() + pattern.weight_of(),
|
||||
Pattern::RangeExc(head, tail) => head.weight_of() + tail.weight_of(),
|
||||
Pattern::RangeInc(head, tail) => head.weight_of() + tail.weight_of(),
|
||||
Pattern::Tuple(patterns) | Pattern::Array(patterns) => patterns.weight_of(),
|
||||
Pattern::Struct(path, items) => {
|
||||
let sitems: usize = items
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user