parser: make Assign an InfixOp instead of expression type

Closes #14
This commit is contained in:
Ryan Chandler 2022-08-03 20:50:07 +01:00
parent 5eca385a9a
commit d602902d7b
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 4 additions and 5 deletions

View File

@ -320,6 +320,7 @@ pub enum InfixOp {
NotIdentical,
And,
Or,
Assign,
AddAssign,
}
@ -341,6 +342,7 @@ impl From<TokenKind> for InfixOp {
TokenKind::BangDoubleEquals => Self::NotIdentical,
TokenKind::BooleanAnd => Self::And,
TokenKind::BooleanOr => Self::Or,
TokenKind::Equals => Self::Assign,
TokenKind::PlusEquals => Self::AddAssign,
_ => unreachable!()
}

View File

@ -1202,10 +1202,6 @@ fn prefix(op: &TokenKind, rhs: Expression) -> Expression {
}
fn infix(lhs: Expression, op: TokenKind, rhs: Expression) -> Expression {
if op == TokenKind::Equals {
return Expression::Assign(Box::new(lhs), Box::new(rhs));
}
Expression::Infix(Box::new(lhs), op.into(), Box::new(rhs))
}
@ -1350,11 +1346,12 @@ mod tests {
#[test]
fn array_index_assign() {
assert_ast("<?php $foo['bar'] = 'baz';", &[
expr!(Expression::Assign(
expr!(Expression::Infix(
Box::new(Expression::ArrayIndex(
Box::new(Expression::Variable("foo".into())),
Some(Box::new(Expression::ConstantString("bar".into())))
)),
InfixOp::Assign,
Box::new(Expression::ConstantString("baz".into()))
))
]);