ast: rename Let to Bind

This commit is contained in:
2025-10-20 00:39:56 -04:00
parent 458e95a8ed
commit 0015ac5f1b
3 changed files with 31 additions and 28 deletions

View File

@@ -106,15 +106,18 @@ pub enum Ty {
Fn(Vec<Ty>), Fn(Vec<Ty>),
} }
/// A `let` binding /// A pattern binding
/// ```ignore /// ```ignore
/// let Pat (= Expr)? /// let Pat (= Expr (else Expr)?)?
/// const Pat (= Expr (else Expr)?)?
/// static Pat (= Expr (else Expr)?)?
/// Pat => Expr // in match
/// `````` /// ``````
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Let<A: Annotation = Span>(pub LetKind, pub Pat, pub Vec<Anno<Expr<A>, A>>); pub struct Bind<A: Annotation = Span>(pub BindKind, pub Pat, pub Vec<Anno<Expr<A>, A>>);
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum LetKind { pub enum BindKind {
/// A `let Pat (= Expr (else Expr)?)?` binding /// A `let Pat (= Expr (else Expr)?)?` binding
Let, Let,
/// A `const Pat = Expr` binding /// A `const Pat = Expr` binding
@@ -174,8 +177,8 @@ pub enum Expr<A: Annotation = Span> {
MetId(String), MetId(String),
/// A literal bool, string, char, or int /// A literal bool, string, char, or int
Lit(Literal), Lit(Literal),
/// let Pat<NoTopAlt> = expr /// (let | const | static) Pat<NoTopAlt> (= expr (else expr)?)?
Let(Box<Let<A>>), Bind(Box<Bind<A>>),
/// (struct | enum | type) Pat<NoTopAlt> /// (struct | enum | type) Pat<NoTopAlt>
Struct(Box<Typedef>), Struct(Box<Typedef>),
/// `| Pat<Tuple> | Expr` | `|| Expr` | `fn Ident? (Pat,*) Expr` /// `| Pat<Tuple> | Expr` | `|| Expr` | `fn Ident? (Pat,*) Expr`
@@ -336,11 +339,11 @@ impl<A: Annotation> Display for Fn<A> {
} }
} }
impl<A: Annotation> Display for Let<A> { impl<A: Annotation> Display for Bind<A> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self(op, pat, exprs) = self; let Self(op, pat, exprs) = self;
if let LetKind::Match = op { if let BindKind::Match = op {
return f.delimit(fmt!("{pat} => "), "").list(exprs, ",!? "); return f.delimit(fmt!("{pat} => "), "").list(exprs, ",!? ");
} }
@@ -353,7 +356,7 @@ impl<A: Annotation> Display for Let<A> {
} }
} }
impl Display for LetKind { impl Display for BindKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self { f.write_str(match self {
Self::Let => "let", Self::Let => "let",
@@ -404,7 +407,7 @@ impl<A: Annotation> Display for Expr<A> {
Self::Id(id) => id.fmt(f), Self::Id(id) => id.fmt(f),
Self::MetId(id) => write!(f, "`{id}"), Self::MetId(id) => write!(f, "`{id}"),
Self::Lit(literal) => literal.fmt(f), Self::Lit(literal) => literal.fmt(f),
Self::Let(v) => v.fmt(f), Self::Bind(v) => v.fmt(f),
Self::Struct(v) => v.fmt(f), Self::Struct(v) => v.fmt(f),
Self::Make(v) => v.fmt(f), Self::Make(v) => v.fmt(f),
Self::Mod(v) => v.fmt(f), Self::Mod(v) => v.fmt(f),

View File

@@ -96,7 +96,7 @@ impl<A: Annotation> Match<A> for Fn<A> {
} }
} }
impl<A: Annotation> Match<A> for Let<A> { impl<A: Annotation> Match<A> for Bind<A> {
fn recurse(sub: &mut Subst<A>, pat: &Self, expr: &Self) -> bool { fn recurse(sub: &mut Subst<A>, pat: &Self, expr: &Self) -> bool {
let (Self(pat_kind, pat_pat, pat_expr), Self(expr_kind, expr_pat, expr_expr)) = (pat, expr); let (Self(pat_kind, pat_pat, pat_expr), Self(expr_kind, expr_pat, expr_expr)) = (pat, expr);
pat_kind == expr_kind pat_kind == expr_kind
@@ -159,8 +159,8 @@ impl<A: Annotation> Match<A> for Expr<A> {
(Expr::Id(_), _) => false, (Expr::Id(_), _) => false,
(Expr::Lit(pat), Expr::Lit(expr)) => pat == expr, (Expr::Lit(pat), Expr::Lit(expr)) => pat == expr,
(Expr::Lit(_), _) => false, (Expr::Lit(_), _) => false,
(Expr::Let(pat), Expr::Let(expr)) => Match::recurse(sub, pat, expr), (Expr::Bind(pat), Expr::Bind(expr)) => Match::recurse(sub, pat, expr),
(Expr::Let(..), _) => false, (Expr::Bind(..), _) => false,
(Expr::Struct(pat), Expr::Struct(expr)) => Match::recurse(sub, pat, expr), (Expr::Struct(pat), Expr::Struct(expr)) => Match::recurse(sub, pat, expr),
(Expr::Struct(_), _) => false, (Expr::Struct(_), _) => false,
(Expr::Make(pat), Expr::Make(expr)) => Match::recurse(sub, pat, expr), (Expr::Make(pat), Expr::Make(expr)) => Match::recurse(sub, pat, expr),
@@ -184,7 +184,7 @@ impl<A: Annotation> Match<A> for Expr<A> {
} }
} }
Expr::Id(_) | Expr::Lit(_) => {} Expr::Id(_) | Expr::Lit(_) => {}
Expr::Let(expr) => expr.apply(sub), Expr::Bind(expr) => expr.apply(sub),
Expr::Struct(expr) => expr.apply(sub), Expr::Struct(expr) => expr.apply(sub),
Expr::Make(expr) => expr.apply(sub), Expr::Make(expr) => expr.apply(sub),
Expr::Mod(expr) => expr.apply(sub), Expr::Mod(expr) => expr.apply(sub),

View File

@@ -705,11 +705,11 @@ impl<'t> Parse<'t> for Fn {
} }
} }
impl<'t> Parse<'t> for Let { impl<'t> Parse<'t> for Bind {
type Prec = LetKind; type Prec = BindKind;
fn parse(p: &mut Parser<'t>, level: Self::Prec) -> PResult<Self> { fn parse(p: &mut Parser<'t>, level: Self::Prec) -> PResult<Self> {
if let LetKind::Match = level { if let BindKind::Match = level {
// |? Pat => Expr // |? Pat => Expr
p.next_if(TKind::Bar)?.ok(); // and discard p.next_if(TKind::Bar)?.ok(); // and discard
return Ok(Self( return Ok(Self(
@@ -800,8 +800,8 @@ fn parse_for<'t>(p: &mut Parser<'t>, _level: ()) -> PResult<Expr> {
], ],
) )
.anno(cspan), .anno(cspan),
Expr::Let(Box::new(Let( Expr::Bind(Box::new(Bind(
LetKind::Match, BindKind::Match,
Pat::Name("#iter".into()), Pat::Name("#iter".into()),
vec![ vec![
Expr::Op( Expr::Op(
@@ -822,14 +822,14 @@ fn parse_for<'t>(p: &mut Parser<'t>, _level: ()) -> PResult<Expr> {
], ],
) )
.anno(cspan), .anno(cspan),
Expr::Let(Box::new(Let( Expr::Bind(Box::new(Bind(
LetKind::Match, BindKind::Match,
Pat::Name("None".into()), Pat::Name("None".into()),
vec![Expr::Op(Op::Break, vec![fail]).anno(fspan)], vec![Expr::Op(Op::Break, vec![fail]).anno(fspan)],
))) )))
.anno(fspan), .anno(fspan),
Expr::Let(Box::new(Let( Expr::Bind(Box::new(Bind(
LetKind::Match, BindKind::Match,
Pat::TupStruct( Pat::TupStruct(
"Some".into(), "Some".into(),
Box::new(Pat::Op(PatOp::Tuple, vec![pat])), Box::new(Pat::Op(PatOp::Tuple, vec![pat])),
@@ -876,9 +876,9 @@ impl<'t> Parse<'t> for Expr {
Ps::Id => Expr::Id(p.parse(())?), Ps::Id => Expr::Id(p.parse(())?),
Ps::Mid => Expr::MetId(p.consume().next()?.lexeme.to_string()), Ps::Mid => Expr::MetId(p.consume().next()?.lexeme.to_string()),
Ps::Lit => Expr::Lit(p.parse(())?), Ps::Lit => Expr::Lit(p.parse(())?),
Ps::Let => Expr::Let(p.parse(LetKind::Let)?), Ps::Let => Expr::Bind(p.parse(BindKind::Let)?),
Ps::Const => Expr::Let(p.parse(LetKind::Const)?), Ps::Const => Expr::Bind(p.parse(BindKind::Const)?),
Ps::Static => Expr::Let(p.parse(LetKind::Static)?), Ps::Static => Expr::Bind(p.parse(BindKind::Static)?),
Ps::For => parse_for(p, ())?, Ps::For => parse_for(p, ())?,
Ps::Typedef => Expr::Struct(p.parse(())?), Ps::Typedef => Expr::Struct(p.parse(())?),
Ps::Mod => Expr::Mod(p.parse(())?), Ps::Mod => Expr::Mod(p.parse(())?),
@@ -1024,9 +1024,9 @@ fn parse_match<'t>(p: &mut Parser<'t>) -> PResult<Expr> {
let arms = p let arms = p
.expect(TKind::LCurly)? .expect(TKind::LCurly)?
.list(vec![], LetKind::Match, TKind::Comma, TKind::RCurly)? .list(vec![], BindKind::Match, TKind::Comma, TKind::RCurly)?
.into_iter() .into_iter()
.map(|Anno(arm, span)| Anno(Expr::Let(Box::new(arm)), span)); .map(|Anno(arm, span)| Anno(Expr::Bind(Box::new(arm)), span));
let expr = Expr::Op(Op::Match, iter::once(scrutinee).chain(arms).collect()); let expr = Expr::Op(Op::Match, iter::once(scrutinee).chain(arms).collect());