parser: support add assign operator

This commit is contained in:
Ryan Chandler 2022-07-28 20:07:40 +01:00
parent 67a26ae78f
commit 22d8cc332c
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
4 changed files with 15 additions and 4 deletions

View File

@ -597,7 +597,16 @@ impl Lexer {
},
'+' => {
self.col += 1;
TokenKind::Plus
if let Some('=') = it.peek() {
self.col += 1;
it.next();
TokenKind::PlusEquals
} else {
TokenKind::Plus
}
},
'-' => {
self.col += 1;

View File

@ -102,6 +102,7 @@ pub enum TokenKind {
Percent,
Pipe,
Plus,
PlusEquals,
Pow,
Private,
Protected,
@ -232,6 +233,7 @@ impl Display for TokenKind {
Self::Percent => "%",
Self::Pipe => "|",
Self::Plus => "+",
Self::PlusEquals => "+=",
Self::Pow => "**",
Self::Private => "private",
Self::Protected => "protected",

View File

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

View File

@ -723,8 +723,6 @@ impl Parser {
let target = self.expression(0)?;
dbg!(&self.current.kind);
Expression::Clone(Box::new(target))
},
TokenKind::Variable(v) => {
@ -1217,7 +1215,7 @@ fn infix_binding_power(t: &TokenKind) -> Option<(u8, u8)> {
TokenKind::DoubleEquals | TokenKind::TripleEquals | TokenKind::BangEquals | TokenKind::BangDoubleEquals => (7, 8),
TokenKind::BooleanAnd => (5, 6),
TokenKind::BooleanOr => (3, 4),
TokenKind::Equals => (2, 1),
TokenKind::Equals | TokenKind::PlusEquals => (2, 1),
_ => return None,
})
}