conlang: Patterns...2!

- Deny arbitrary paths in patterns (only one non-keyword identifier allowed!)
- Allow patterns in for-loop binders (literally useless atm, but it's a step toward making patterns the only way to bind names.)

Next: Functions, Tuple Struct Patterns... And solving the stupid syntactic ambiguity of structors.
This commit is contained in:
2025-02-22 01:00:29 -06:00
parent b115fea71b
commit 5d2c714bc1
8 changed files with 85 additions and 113 deletions

View File

@@ -418,12 +418,12 @@ pub struct Let {
/// A [Pattern] meta-expression (any [`ExprKind`] that fits pattern rules)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Pattern {
Path(Path),
Name(Sym),
Literal(Literal),
Ref(Mutability, Box<Pattern>),
Tuple(Vec<Pattern>),
Array(Vec<Pattern>),
Struct(Path, Vec<(Path, Option<Pattern>)>),
Struct(Path, Vec<(Sym, Option<Pattern>)>),
}
/// A `match` expression: `match` `{` ([MatchArm] `,`)* [MatchArm]? `}`
@@ -620,7 +620,7 @@ pub struct If {
/// A [For] expression: `for` Pattern `in` [`Expr`] [`Block`] [`Else`]?
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct For {
pub bind: Sym, // TODO: Patterns?
pub bind: Pattern,
pub cond: Box<Expr>,
pub pass: Box<Block>,
pub fail: Else,

View File

@@ -468,7 +468,7 @@ mod display {
impl Display for Pattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Pattern::Path(path) => path.fmt(f),
Pattern::Name(sym) => sym.fmt(f),
Pattern::Literal(literal) => literal.fmt(f),
Pattern::Ref(mutability, pattern) => write!(f, "&{mutability}{pattern}"),
Pattern::Tuple(patterns) => separate(patterns, ", ")(f.delimit(INLINE_PARENS)),
@@ -863,7 +863,10 @@ mod convert {
fn try_from(value: ExprKind) -> Result<Self, Self::Error> {
Ok(match value {
ExprKind::Literal(literal) => Pattern::Literal(literal),
ExprKind::Path(path) => Pattern::Path(path),
ExprKind::Path(Path { absolute: false, ref parts }) => match parts.as_slice() {
[PathPart::Ident(name)] => Pattern::Name(*name),
_ => Err(value)?,
},
ExprKind::Empty => Pattern::Tuple(vec![]),
ExprKind::Group(Group { expr }) => Pattern::Tuple(vec![Pattern::try_from(*expr)?]),
ExprKind::Tuple(Tuple { exprs }) => Pattern::Tuple(
@@ -887,10 +890,7 @@ mod convert {
let fields = init
.into_iter()
.map(|Fielder { name, init }| {
Ok((
name.into(),
init.map(|i| Pattern::try_from(i.kind)).transpose()?,
))
Ok((name, init.map(|i| Pattern::try_from(i.kind)).transpose()?))
})
.collect::<Result<_, Self::Error>>()?;
Pattern::Struct(to, fields)

View File

@@ -248,7 +248,7 @@ pub trait Fold {
fn fold_pattern(&mut self, p: Pattern) -> Pattern {
match p {
Pattern::Path(path) => Pattern::Path(self.fold_path(path)),
Pattern::Name(sym) => Pattern::Name(self.fold_sym(sym)),
Pattern::Literal(literal) => Pattern::Literal(self.fold_literal(literal)),
Pattern::Ref(mutability, pattern) => Pattern::Ref(
self.fold_mutability(mutability),
@@ -285,7 +285,7 @@ pub trait Fold {
let MatchArm(pat, expr) = a;
MatchArm(self.fold_pattern(pat), self.fold_expr(expr))
}
fn fold_assign(&mut self, a: Assign) -> Assign {
let Assign { parts } = a;
let (head, tail) = *parts;
@@ -400,7 +400,7 @@ pub trait Fold {
fn fold_for(&mut self, f: For) -> For {
let For { bind, cond, pass, fail } = f;
For {
bind: self.fold_sym(bind),
bind: self.fold_pattern(bind),
cond: Box::new(self.fold_expr(*cond)),
pass: Box::new(self.fold_block(*pass)),
fail: self.fold_else(fail),

View File

@@ -214,7 +214,7 @@ pub trait Visit<'a>: Sized {
fn visit_pattern(&mut self, p: &'a Pattern) {
match p {
Pattern::Path(path) => self.visit_path(path),
Pattern::Name(name) => self.visit_sym(name),
Pattern::Literal(literal) => self.visit_literal(literal),
Pattern::Ref(mutability, pattern) => {
self.visit_mutability(mutability);
@@ -347,7 +347,7 @@ pub trait Visit<'a>: Sized {
}
fn visit_for(&mut self, f: &'a For) {
let For { bind, cond, pass, fail } = f;
self.visit_sym(bind);
self.visit_pattern(bind);
self.visit_expr(cond);
self.visit_block(pass);
self.visit_else(fail);