parser: workaround for assignment by ref

This commit is contained in:
Ryan Chandler 2022-12-05 00:32:40 +00:00
parent e8b58ba52b
commit d71600ee65
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 14 additions and 3 deletions

View File

@ -833,6 +833,7 @@ pub enum InfixOp {
And,
Or,
Assign,
AssignRef,
AddAssign,
Pow,
Instanceof,

View File

@ -1353,8 +1353,15 @@ impl Parser {
}
}
_ => {
// FIXME: Hacky, should probably be refactored.
let by_ref = kind == TokenKind::Equals && state.current.kind == TokenKind::Ampersand;
if by_ref {
state.next();
}
let rhs = self.expression(state, rpred)?;
left = infix(left, kind, rhs);
left = infix(left, kind, rhs, by_ref);
}
}
@ -1853,10 +1860,13 @@ fn prefix(op: &TokenKind, rhs: Expression) -> Expression {
}
}
fn infix(lhs: Expression, op: TokenKind, rhs: Expression) -> Expression {
fn infix(lhs: Expression, op: TokenKind, rhs: Expression, by_ref: bool) -> Expression {
Expression::Infix {
lhs: Box::new(lhs),
op: op.into(),
op: match (&op, by_ref) {
(TokenKind::Equals, true) => ast::InfixOp::AssignRef,
_ => op.into()
},
rhs: Box::new(rhs),
}
}