ast: struct

This commit is contained in:
2025-10-07 05:00:51 -04:00
parent bcabbcd39d
commit 222b2d7b98
6 changed files with 89 additions and 17 deletions

View File

@@ -32,6 +32,10 @@ pub enum Pat {
MetId(String),
/// Matches anything, and binds it to a name
Name(String),
/// Matches a Struct Expression `Ident { Pat }`
Struct(String, Box<Pat>),
/// Matches a Tuple Struct Expression `Ident ( Pat )`
TupStruct(String, Box<Pat>),
/// Matches a partial decomposition (`..rest`) or upper-bounded range (`..100`).
Rest(Option<Box<Pat>>),
/// Matches a literal value by equality comparison
@@ -119,6 +123,11 @@ pub struct MakeArm<A: Annotation = Span>(pub String, pub Option<Anno<Expr<A>, A>
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mod<A: Annotation = Span>(pub Ty, pub Anno<Expr<A>, A>);
/// A record-type definition
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Struct(pub Pat);
/// Expressions: The beating heart of Dough
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expr<A: Annotation = Span> {
@@ -132,6 +141,8 @@ pub enum Expr<A: Annotation = Span> {
Let(Box<Let<A>>),
/// `const Pat<NoTopAlt> (= Expr)?` (Basically let rec)
Const(Box<Const<A>>),
/// struct Pat<NoTopAlt>
Struct(Box<Struct>),
/// `| Pat<Tuple> | Expr` | `|| Expr` | `fn Ident? (Pat,*) Expr`
Fn(Box<Fn<A>>),
/// Expr { (Ident (: Expr)?),* }
@@ -298,6 +309,13 @@ impl<A: Annotation> Display for Mod<A> {
}
}
impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self(pat) = self;
write!(f, "struct {pat}")
}
}
impl<A: Annotation> Display for Expr<A> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
@@ -306,6 +324,7 @@ impl<A: Annotation> Display for Expr<A> {
Self::Lit(literal) => literal.fmt(f),
Self::Let(v) => v.fmt(f),
Self::Const(v) => v.fmt(f),
Self::Struct(v) => v.fmt(f),
Self::Make(v) => v.fmt(f),
Self::Match(v) => v.fmt(f),
Self::Mod(v) => v.fmt(f),
@@ -419,6 +438,8 @@ impl Display for Pat {
Self::Lit(literal) => literal.fmt(f),
Self::MetId(name) => write!(f, "`{name}"),
Self::Name(name) => name.fmt(f),
Self::Struct(name, bind) => write!(f, "{name} {{{bind}}}"),
Self::TupStruct(name, bind) => write!(f, "{name} {bind}"),
Self::Rest(Some(rest)) => write!(f, "..{rest}"),
Self::Rest(None) => write!(f, ".."),
Self::Tuple(pats) => f.delimit("(", ")").list(pats, ", "),