Merge pull request #1 from Dohxis/feat/one-liner-if-statement

This commit is contained in:
Ryan Chandler 2022-07-26 10:45:22 +01:00 committed by GitHub
commit 61c76ad49a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -337,16 +337,22 @@ impl Parser {
expect!(self, TokenKind::RightParen, "expected )"); expect!(self, TokenKind::RightParen, "expected )");
// TODO: Support one-liner if statements. let body_end_token = if self.current.kind == TokenKind::LeftBrace {
expect!(self, TokenKind::LeftBrace, "expected {"); self.next();
TokenKind::RightBrace
} else {
TokenKind::SemiColon
};
let mut then = Block::new(); let mut then = Block::new();
while ! self.is_eof() && self.current.kind != TokenKind::RightBrace { while ! self.is_eof() && self.current.kind != body_end_token {
then.push(self.statement()?); then.push(self.statement()?);
} }
// TODO: Support one-liner if statements. if body_end_token == TokenKind::RightBrace {
expect!(self, TokenKind::RightBrace, "expected }"); expect!(self, TokenKind::RightBrace, "expected }");
}
Statement::If { condition, then } Statement::If { condition, then }
}, },
@ -1141,6 +1147,22 @@ mod tests {
]); ]);
} }
#[test]
fn one_liner_if_statement() {
assert_ast("\
<?php
if($name)
return $name;", &[
Statement::If {
condition: Expression::Variable("name".into()),
then: vec![
Statement::Return { value: Some(Expression::Variable("name".into())) }
],
},
]);
}
#[test] #[test]
fn echo() { fn echo() {
assert_ast("<?php echo 1;", &[ assert_ast("<?php echo 1;", &[