ast: rename Let to Bind
This commit is contained in:
23
src/ast.rs
23
src/ast.rs
@@ -106,15 +106,18 @@ pub enum Ty {
|
||||
Fn(Vec<Ty>),
|
||||
}
|
||||
|
||||
/// 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<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)]
|
||||
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<A: Annotation = Span> {
|
||||
MetId(String),
|
||||
/// A literal bool, string, char, or int
|
||||
Lit(Literal),
|
||||
/// let Pat<NoTopAlt> = expr
|
||||
Let(Box<Let<A>>),
|
||||
/// (let | const | static) Pat<NoTopAlt> (= expr (else expr)?)?
|
||||
Bind(Box<Bind<A>>),
|
||||
/// (struct | enum | type) Pat<NoTopAlt>
|
||||
Struct(Box<Typedef>),
|
||||
/// `| 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 {
|
||||
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<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 {
|
||||
f.write_str(match self {
|
||||
Self::Let => "let",
|
||||
@@ -404,7 +407,7 @@ impl<A: Annotation> Display for Expr<A> {
|
||||
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),
|
||||
|
||||
@@ -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 {
|
||||
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<A: Annotation> Match<A> for Expr<A> {
|
||||
(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<A: Annotation> Match<A> for Expr<A> {
|
||||
}
|
||||
}
|
||||
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),
|
||||
|
||||
@@ -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<Self> {
|
||||
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<Expr> {
|
||||
],
|
||||
)
|
||||
.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<Expr> {
|
||||
],
|
||||
)
|
||||
.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<Expr> {
|
||||
|
||||
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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user