This commit is contained in:
nora 2022-04-09 11:57:40 +02:00
parent 535e95069d
commit 7087c9be0f
5 changed files with 155 additions and 11 deletions

View file

@ -136,6 +136,7 @@ fn statement_parser<'src>() -> impl Parser<Token<'src>, Stmt, Error = Error<'src
.then(ident_parser())
.then_ignore(just(Token::Eq))
.then(expr_parser())
.then_ignore(just(Token::Semi))
.map(|((ty, name), rhs)| {
Stmt::VarDecl(VarDecl {
name,
@ -176,16 +177,17 @@ fn statement_parser<'src>() -> impl Parser<Token<'src>, Stmt, Error = Error<'src
.delimited_by(just(Token::BraceO), just(Token::BraceC)),
)
.then(
just(Token::Else).ignore_then(
if_stmt
.map(|if_stmt| ElsePart::ElseIf(Box::new(if_stmt)))
.or(stmt
.clone()
.repeated()
.delimited_by(just(Token::BraceO), just(Token::BraceC))
.map_with_span(ElsePart::Else))
.or_not(),
),
just(Token::Else)
.ignore_then(
if_stmt
.map(|if_stmt| ElsePart::ElseIf(Box::new(if_stmt)))
.or(stmt
.clone()
.repeated()
.delimited_by(just(Token::BraceO), just(Token::BraceC))
.map_with_span(ElsePart::Else)),
)
.or_not(),
)
.map_with_span(|((cond, body), else_part), span| IfStmt {
cond,
@ -325,6 +327,24 @@ mod tests {
insta::assert_debug_snapshot!(r);
}
#[test]
fn if_no_else() {
let r = parse("fn foo() -> u64 { if false {} }");
insta::assert_debug_snapshot!(r);
}
#[test]
fn if_else() {
let r = parse("fn foo() -> u64 { if false {} else {} }");
insta::assert_debug_snapshot!(r);
}
#[test]
fn while_loop() {
let r = parse("fn foo() -> u64 { while false {} }");
insta::assert_debug_snapshot!(r);
}
#[test]
fn var_decl() {
let r = parse("fn foo() -> u64 { u64 hello = 5; }");