From 0015ac5f1b924b30c7d1e551b2222abae5907f72 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 20 Oct 2025 00:39:56 -0400 Subject: [PATCH] ast: rename `Let` to `Bind` --- src/ast.rs | 23 +++++++++++++---------- src/ast/macro_matcher.rs | 8 ++++---- src/parser.rs | 28 ++++++++++++++-------------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 4b57644..d2201c4 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -106,15 +106,18 @@ pub enum Ty { Fn(Vec), } -/// A `let` binding +/// A pattern binding /// ```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)] -pub struct Let(pub LetKind, pub Pat, pub Vec, A>>); +pub struct Bind(pub BindKind, pub Pat, pub Vec, A>>); #[derive(Clone, Debug, PartialEq, Eq)] -pub enum LetKind { +pub enum BindKind { /// A `let Pat (= Expr (else Expr)?)?` binding Let, /// A `const Pat = Expr` binding @@ -174,8 +177,8 @@ pub enum Expr { MetId(String), /// A literal bool, string, char, or int Lit(Literal), - /// let Pat = expr - Let(Box>), + /// (let | const | static) Pat (= expr (else expr)?)? + Bind(Box>), /// (struct | enum | type) Pat Struct(Box), /// `| Pat | Expr` | `|| Expr` | `fn Ident? (Pat,*) Expr` @@ -336,11 +339,11 @@ impl Display for Fn { } } -impl Display for Let { +impl Display for Bind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let Self(op, pat, exprs) = self; - if let LetKind::Match = op { + if let BindKind::Match = op { return f.delimit(fmt!("{pat} => "), "").list(exprs, ",!? "); } @@ -353,7 +356,7 @@ impl Display for Let { } } -impl Display for LetKind { +impl Display for BindKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Self::Let => "let", @@ -404,7 +407,7 @@ impl Display for Expr { Self::Id(id) => id.fmt(f), Self::MetId(id) => write!(f, "`{id}"), 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::Make(v) => v.fmt(f), Self::Mod(v) => v.fmt(f), diff --git a/src/ast/macro_matcher.rs b/src/ast/macro_matcher.rs index 0d4a3d7..25e85b7 100644 --- a/src/ast/macro_matcher.rs +++ b/src/ast/macro_matcher.rs @@ -96,7 +96,7 @@ impl Match for Fn { } } -impl Match for Let { +impl Match for Bind { fn recurse(sub: &mut Subst, pat: &Self, expr: &Self) -> bool { let (Self(pat_kind, pat_pat, pat_expr), Self(expr_kind, expr_pat, expr_expr)) = (pat, expr); pat_kind == expr_kind @@ -159,8 +159,8 @@ impl Match for Expr { (Expr::Id(_), _) => false, (Expr::Lit(pat), Expr::Lit(expr)) => pat == expr, (Expr::Lit(_), _) => false, - (Expr::Let(pat), Expr::Let(expr)) => Match::recurse(sub, pat, expr), - (Expr::Let(..), _) => false, + (Expr::Bind(pat), Expr::Bind(expr)) => Match::recurse(sub, pat, expr), + (Expr::Bind(..), _) => false, (Expr::Struct(pat), Expr::Struct(expr)) => Match::recurse(sub, pat, expr), (Expr::Struct(_), _) => false, (Expr::Make(pat), Expr::Make(expr)) => Match::recurse(sub, pat, expr), @@ -184,7 +184,7 @@ impl Match for Expr { } } Expr::Id(_) | Expr::Lit(_) => {} - Expr::Let(expr) => expr.apply(sub), + Expr::Bind(expr) => expr.apply(sub), Expr::Struct(expr) => expr.apply(sub), Expr::Make(expr) => expr.apply(sub), Expr::Mod(expr) => expr.apply(sub), diff --git a/src/parser.rs b/src/parser.rs index dfcf0d2..f68f427 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -705,11 +705,11 @@ impl<'t> Parse<'t> for Fn { } } -impl<'t> Parse<'t> for Let { - type Prec = LetKind; +impl<'t> Parse<'t> for Bind { + type Prec = BindKind; fn parse(p: &mut Parser<'t>, level: Self::Prec) -> PResult { - if let LetKind::Match = level { + if let BindKind::Match = level { // |? Pat => Expr p.next_if(TKind::Bar)?.ok(); // and discard return Ok(Self( @@ -800,8 +800,8 @@ fn parse_for<'t>(p: &mut Parser<'t>, _level: ()) -> PResult { ], ) .anno(cspan), - Expr::Let(Box::new(Let( - LetKind::Match, + Expr::Bind(Box::new(Bind( + BindKind::Match, Pat::Name("#iter".into()), vec![ Expr::Op( @@ -822,14 +822,14 @@ fn parse_for<'t>(p: &mut Parser<'t>, _level: ()) -> PResult { ], ) .anno(cspan), - Expr::Let(Box::new(Let( - LetKind::Match, + Expr::Bind(Box::new(Bind( + BindKind::Match, Pat::Name("None".into()), vec![Expr::Op(Op::Break, vec![fail]).anno(fspan)], ))) .anno(fspan), - Expr::Let(Box::new(Let( - LetKind::Match, + Expr::Bind(Box::new(Bind( + BindKind::Match, Pat::TupStruct( "Some".into(), 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::Mid => Expr::MetId(p.consume().next()?.lexeme.to_string()), Ps::Lit => Expr::Lit(p.parse(())?), - Ps::Let => Expr::Let(p.parse(LetKind::Let)?), - Ps::Const => Expr::Let(p.parse(LetKind::Const)?), - Ps::Static => Expr::Let(p.parse(LetKind::Static)?), + Ps::Let => Expr::Bind(p.parse(BindKind::Let)?), + Ps::Const => Expr::Bind(p.parse(BindKind::Const)?), + Ps::Static => Expr::Bind(p.parse(BindKind::Static)?), Ps::For => parse_for(p, ())?, Ps::Typedef => Expr::Struct(p.parse(())?), Ps::Mod => Expr::Mod(p.parse(())?), @@ -1024,9 +1024,9 @@ fn parse_match<'t>(p: &mut Parser<'t>) -> PResult { let arms = p .expect(TKind::LCurly)? - .list(vec![], LetKind::Match, TKind::Comma, TKind::RCurly)? + .list(vec![], BindKind::Match, TKind::Comma, TKind::RCurly)? .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());