conlang: Unify binding operations!

This breaks out the pattern matching/unification algorithm from cl-interpret/interpret.rs to cl-interpret/pattern.rs

TODO: pattern destructuring in const, static :^)
This commit is contained in:
2025-02-22 05:16:37 -06:00
parent 7a8da33de9
commit e3d94d8949
14 changed files with 202 additions and 171 deletions

View File

@@ -1,5 +1,6 @@
use super::*;
use cl_ast::ExprKind;
use cl_lexer::error::{Error as LexError, Reason};
use std::fmt::Display;
pub type PResult<T> = Result<T, Error>;
@@ -29,6 +30,7 @@ pub enum ErrorKind {
ExpectedParsing {
want: Parsing,
},
InvalidPattern(Box<ExprKind>),
/// Indicates unfinished code
Todo(&'static str),
}
@@ -148,6 +150,7 @@ impl Display for ErrorKind {
ErrorKind::Unexpected(t) => write!(f, "Encountered unexpected token `{t}`"),
ErrorKind::ExpectedToken { want: e, got: g } => write!(f, "Expected `{e}`, got `{g}`"),
ErrorKind::ExpectedParsing { want } => write!(f, "Expected {want}"),
ErrorKind::InvalidPattern(got) => write!(f, "Got invalid `{got}`"),
ErrorKind::Todo(unfinished) => write!(f, "TODO: {unfinished}"),
}
}

View File

@@ -494,7 +494,7 @@ impl Parse<'_> for TypedParam {
/// Parses a single function [parameter](Param)
fn parse(p: &mut Parser) -> PResult<(Param, TyKind)> {
Ok((
Param { mutability: Mutability::parse(p)?, name: Sym::parse(p)? },
Param { mutability: Mutability::parse(p)?, bind: Pattern::parse(p)? },
{
p.match_type(TokenKind::Colon, Parsing::Param)?;
TyKind::parse(p)?
@@ -1006,13 +1006,20 @@ impl Parse<'_> for Block {
}
}
/// Conditions (which precede curly-braced blocks) get special treatment
fn condition(p: &mut Parser) -> PResult<Expr> {
let start = p.loc();
let kind = prec::exprkind(p, prec::Precedence::Condition.level())?;
Ok(Expr { kind, extents: Span(start, p.loc()) })
}
impl Parse<'_> for While {
/// [While] = `while` [Expr] [Block] [Else]?
#[rustfmt::skip]
fn parse(p: &mut Parser) -> PResult<While> {
p.match_type(TokenKind::While, Parsing::While)?;
Ok(While {
cond: Expr::parse(p)?.into(),
cond: condition(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?
})
@@ -1025,7 +1032,7 @@ impl Parse<'_> for If {
fn parse(p: &mut Parser) -> PResult<If> {
p.match_type(TokenKind::If, Parsing::If)?;
Ok(If {
cond: Expr::parse(p)?.into(),
cond: condition(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?,
})
@@ -1033,7 +1040,7 @@ impl Parse<'_> for If {
}
impl Parse<'_> for For {
/// [For]: `for` Pattern (TODO) `in` [Expr] [Block] [Else]?
/// [For]: `for` [Pattern] `in` [Expr] [Block] [Else]?
#[rustfmt::skip]
fn parse(p: &mut Parser) -> PResult<For> {
p.match_type(TokenKind::For, Parsing::For)?;
@@ -1041,7 +1048,7 @@ impl Parse<'_> for For {
p.match_type(TokenKind::In, Parsing::For)?;
Ok(For {
bind,
cond: Expr::parse(p)?.into(),
cond: condition(p)?.into(),
pass: Block::parse(p)?.into(),
fail: Else::parse(p)?,
})
@@ -1081,8 +1088,7 @@ impl Parse<'_> for Return {
impl Parse<'_> for Pattern {
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
let value = prec::exprkind(p, prec::Precedence::Pattern.level())?;
Pattern::try_from(value)
.map_err(|_| p.error(ExpectedParsing { want: Parsing::Pattern }, Parsing::Pattern))
Pattern::try_from(value).map_err(|e| p.error(InvalidPattern(e.into()), Parsing::Pattern))
}
}

View File

@@ -44,7 +44,9 @@ pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
Some(match op {
TokenKind::LBrack => Precedence::Index,
TokenKind::LParen => Precedence::Call,
TokenKind::LCurly => Precedence::Structor,
TokenKind::Dot => Precedence::Member,
TokenKind::As => Precedence::Cast,
_ => None?,
})
}
@@ -55,25 +57,36 @@ pub fn exprkind(p: &mut Parser, power: u8) -> PResult<ExprKind> {
if before < power {
break;
}
p.consume_peeked();
head = match op {
TokenKind::LBrack => {
p.consume_peeked();
let indices =
sep(Expr::parse, TokenKind::Comma, TokenKind::RBrack, parsing)(p)?;
p.match_type(TokenKind::RBrack, parsing)?;
ExprKind::Index(Index { head: head.into(), indices })
}
TokenKind::LParen => {
p.consume_peeked();
let exprs = sep(Expr::parse, TokenKind::Comma, TokenKind::RParen, parsing)(p)?;
p.match_type(TokenKind::RParen, parsing)?;
Binary { kind: BinaryKind::Call, parts: (head, Tuple { exprs }.into()).into() }
.into()
}
TokenKind::LCurly => match head {
ExprKind::Path(path) => ExprKind::Structor(structor_body(p, path)?),
_ => break,
},
TokenKind::Dot => {
p.consume_peeked();
let kind = MemberKind::parse(p)?;
Member { head: Box::new(head), kind }.into()
}
TokenKind::As => {
p.consume_peeked();
let ty = Ty::parse(p)?;
Cast { head: head.into(), ty }.into()
}
_ => Err(p.error(Unexpected(op), parsing))?,
};
continue;
@@ -217,11 +230,7 @@ fn exprkind_group(p: &mut Parser) -> PResult<ExprKind> {
/// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor])
fn exprkind_pathlike(p: &mut Parser) -> PResult<ExprKind> {
let head = Path::parse(p)?;
Ok(match p.match_type(TokenKind::Colon, Parsing::Path) {
Ok(_) => ExprKind::Structor(structor_body(p, head)?),
Err(_) => ExprKind::Path(head),
})
Path::parse(p).map(Into::into)
}
/// [Structor]Body = `{` ([Fielder] `,`)* [Fielder]? `}`
@@ -244,6 +253,9 @@ fn structor_body(p: &mut Parser, to: Path) -> PResult<Structor> {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Precedence {
Assign,
Pattern, // A pattern can contain a structor
Structor, // A structor is never a valid conditional
Condition, // Anything that syntactically needs a block following it
Logic,
Compare,
Range,
@@ -255,7 +267,6 @@ pub enum Precedence {
Index,
Cast,
Member, // left-associative
Pattern,
Call,
}
@@ -284,7 +295,9 @@ impl Precedence {
pub fn postfix(self) -> Option<(u8, ())> {
match self {
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
Self::Structor | Self::Index | Self::Call | Self::Member | Self::Cast => {
Some((self.level(), ()))
}
_ => None,
}
}