Skip to content

Commit

Permalink
feat: add lexing for *=
Browse files Browse the repository at this point in the history
  • Loading branch information
vighnesh153 committed Feb 9, 2024
1 parent 087ed46 commit b924fc5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,20 @@ fun Lexer.nextToken(): Token {
}
}

'*' -> t = Token(tokenType = TokenType.ASTERISK, tokenLiteral = TokenType.ASTERISK.value)
'*' -> {
val peek = peekCharacter()
t = when (peek) {
'=' -> {
readNextCharacter()
Token(tokenType = TokenType.ASTERISK_EQUALS, tokenLiteral = TokenType.ASTERISK_EQUALS.value)
}

else -> {
Token(tokenType = TokenType.ASTERISK, tokenLiteral = TokenType.ASTERISK.value)
}
}
}

'/' -> t = Token(tokenType = TokenType.FORWARD_SLASH, tokenLiteral = TokenType.FORWARD_SLASH.value)
'\\' -> t = Token(tokenType = TokenType.BACK_SLASH, tokenLiteral = TokenType.BACK_SLASH.value)
'%' -> t = Token(tokenType = TokenType.MODULUS, tokenLiteral = TokenType.MODULUS.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ b += c + ++d
x --
y-= z++ - --a
a *=b*= c = d
""".trimIndent()

val expectedTokens = listOf(
Expand Down Expand Up @@ -133,6 +135,15 @@ y-= z++ - --a
ExpectedToken(id = 53, tokenType = TokenType.DECREMENT, tokenLiteral = "--"),
ExpectedToken(id = 54, tokenType = TokenType.IDENTIFIER, tokenLiteral = "a"),

// a *=b*= c = d
ExpectedToken(id = 55, tokenType = TokenType.IDENTIFIER, tokenLiteral = "a"),
ExpectedToken(id = 56, tokenType = TokenType.ASTERISK_EQUALS, tokenLiteral = "*="),
ExpectedToken(id = 57, tokenType = TokenType.IDENTIFIER, tokenLiteral = "b"),
ExpectedToken(id = 58, tokenType = TokenType.ASTERISK_EQUALS, tokenLiteral = "*="),
ExpectedToken(id = 59, tokenType = TokenType.IDENTIFIER, tokenLiteral = "c"),
ExpectedToken(id = 60, tokenType = TokenType.EQUALS, tokenLiteral = "="),
ExpectedToken(id = 61, tokenType = TokenType.IDENTIFIER, tokenLiteral = "d"),

// eof
ExpectedToken(id = -1, tokenType = Token.EOF.tokenType, tokenLiteral = Token.EOF.tokenLiteral),
)
Expand Down

0 comments on commit b924fc5

Please sign in to comment.