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 )");
// TODO: Support one-liner if statements.
expect!(self, TokenKind::LeftBrace, "expected {");
let body_end_token = if self.current.kind == TokenKind::LeftBrace {
self.next();
TokenKind::RightBrace
} else {
TokenKind::SemiColon
};
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()?);
}
// TODO: Support one-liner if statements.
expect!(self, TokenKind::RightBrace, "expected }");
if body_end_token == TokenKind::RightBrace {
expect!(self, TokenKind::RightBrace, "expected }");
}
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]
fn echo() {
assert_ast("<?php echo 1;", &[