conlang: Struct, tuple member accesses, member function call syntax

Currently uses UFCS in the interpreter. This may change after type checking?
This commit is contained in:
2024-05-19 13:55:28 -05:00
parent a033e9f33b
commit 8d8928b8a8
8 changed files with 140 additions and 8 deletions

View File

@@ -862,6 +862,7 @@ impl<'t> Parser<'t> {
Some(match op {
Punct::LBrack => Precedence::Index,
Punct::LParen => Precedence::Call,
Punct::Dot => Precedence::Member,
_ => None?,
})
}
@@ -889,6 +890,10 @@ impl<'t> Parser<'t> {
}
.into()
}
Punct::Dot => {
let kind = self.access()?;
Member { head: Box::new(head), kind }.into()
}
_ => Err(self.error(Unexpected(TokenKind::Punct(op)), parsing))?,
};
continue;
@@ -922,6 +927,28 @@ impl<'t> Parser<'t> {
Ok(head)
}
pub fn access(&mut self) -> PResult<MemberKind> {
const PARSING: Parsing = Parsing::Member;
const DEL: (Punct, Punct) = PARENS; // delimiter
match self.peek_kind(PARSING)? {
TokenKind::Identifier => {
let name = self.identifier()?;
if self.match_op(DEL.0, PARSING).is_err() {
Ok(MemberKind::Struct(name))
} else {
let exprs = sep(Self::expr, Punct::Comma, DEL.1, PARSING)(self)?;
self.match_op(DEL.1, PARSING)?; // should succeed
Ok(MemberKind::Call(name, Tuple { exprs }))
}
}
TokenKind::Literal => {
let name = self.literal()?; // TODO: Maybe restrict this to just
Ok(MemberKind::Tuple(name))
}
t => Err(self.error(Unexpected(t), PARSING)),
}
}
/// Parses an expression beginning with a [Path] (i.e. [Path] or [Structor])
pub fn exprkind_pathlike(&mut self) -> PResult<ExprKind> {
let head = self.path()?;
@@ -1174,7 +1201,7 @@ impl Precedence {
}
pub fn postfix(self) -> Option<(u8, ())> {
match self {
Self::Index | Self::Call => Some((self.level(), ())),
Self::Index | Self::Call | Self::Member => Some((self.level(), ())),
_ => None,
}
}
@@ -1190,7 +1217,6 @@ impl From<BinaryKind> for Precedence {
use BinaryKind as Op;
match value {
Op::Call => Precedence::Call,
Op::Dot => Precedence::Member,
Op::Mul | Op::Div | Op::Rem => Precedence::Term,
Op::Add | Op::Sub => Precedence::Factor,
Op::Shl | Op::Shr => Precedence::Shift,
@@ -1264,6 +1290,5 @@ operator! {
Star => Mul,
Slash => Div,
Rem => Rem,
Dot => Dot,
};
}