parser: support unary plus op

This commit is contained in:
Ryan Chandler 2022-09-16 14:43:44 +01:00
parent 37029e4e41
commit b6f61bb344
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 12 additions and 0 deletions

View File

@ -562,6 +562,9 @@ pub enum Expression {
Negate {
value: Box<Expression>,
},
UnaryPlus {
value: Box<Expression>,
},
Cast {
kind: CastKind,
value: Box<Self>,

View File

@ -2179,6 +2179,7 @@ fn is_prefix(op: &TokenKind) -> bool {
op,
TokenKind::Bang
| TokenKind::Minus
| TokenKind::Plus
| TokenKind::StringCast
| TokenKind::BinaryCast
| TokenKind::ObjectCast
@ -2203,6 +2204,7 @@ fn prefix(op: &TokenKind, rhs: Expression) -> Expression {
TokenKind::Minus => Expression::Negate {
value: Box::new(rhs),
},
TokenKind::Plus => Expression::UnaryPlus { value: Box::new(rhs) },
TokenKind::StringCast
| TokenKind::BinaryCast
| TokenKind::ObjectCast
@ -4286,6 +4288,13 @@ mod tests {
);
}
#[test]
fn unary_plus() {
assert_ast("<?php +1;", &[
expr!(Expression::UnaryPlus { value: Box::new(Expression::Int { i: 1 }) })
]);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();