conlang: Add unconditional loop expression, for desugaring

This commit is contained in:
2024-04-17 00:29:09 -05:00
parent ec1a1255ad
commit 00d72b823a
8 changed files with 62 additions and 10 deletions

View File

@@ -103,6 +103,7 @@ pub enum Parsing {
Block,
Group,
Tuple,
Loop,
While,
If,
For,
@@ -204,6 +205,7 @@ impl Display for Parsing {
Parsing::Block => "a block",
Parsing::Group => "a grouped expression",
Parsing::Tuple => "a tuple",
Parsing::Loop => "an unconditional loop expression",
Parsing::While => "a while expression",
Parsing::If => "an if expression",
Parsing::For => "a for expression",

View File

@@ -809,6 +809,7 @@ impl<'t> Parser<'t> {
self.consume_peeked();
Unary { kind, tail: self.exprkind(after)?.into() }.into()
}
TokenKind::Loop => ExprKind::Loop(self.parse_loop()?),
TokenKind::While => ExprKind::While(self.parse_while()?),
TokenKind::If => ExprKind::If(self.parse_if()?),
TokenKind::For => ExprKind::For(self.parse_for()?),
@@ -1010,6 +1011,12 @@ impl<'t> Parser<'t> {
}
/// ## Control flow subexpressions
impl<'t> Parser<'t> {
/// [Loop] = `loop` [Block]
pub fn parse_loop(&mut self) -> PResult<Loop> {
self.match_type(TokenKind::Loop, Parsing::Loop)?;
Ok(Loop { body: self.block()? })
}
/// [While] = `while` [Expr] [Block] [Else]?
pub fn parse_while(&mut self) -> PResult<While> {
self.match_type(TokenKind::While, Parsing::While)?;