summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Tews <git@tews.dev>2024-06-19 06:07:54 +0200
committerMichael Tews <michael@tews.dev>2026-04-12 11:11:02 +0200
commit734e8e7a7159dd933c12e97b53984d6686c7cb22 (patch)
treef12e28749ddba9516fd4f4edf3a042500e1dedbc
parent0e3db367f467a2fd9b1ce0266699cce31c3c60eb (diff)
feat(lexer): added new tokens
added == and !=
-rw-r--r--lexer/lexer.go26
-rw-r--r--token/token.go3
2 files changed, 27 insertions, 2 deletions
diff --git a/lexer/lexer.go b/lexer/lexer.go
index cdd2901..807d3f0 100644
--- a/lexer/lexer.go
+++ b/lexer/lexer.go
@@ -22,7 +22,14 @@ func (l *Lexer) NextToken() token.Token {
switch l.ch {
case '=':
- tok = newToken(token.ASSIGN, l.ch)
+ if l.peekChar() == '=' {
+ ch := l.ch
+ l.readChar()
+ literal := string(ch) + string(l.ch)
+ tok = token.Token{Type: token.EQ, Literal: literal}
+ } else {
+ tok = newToken(token.ASSIGN, l.ch)
+ }
case ';':
tok = newToken(token.SEMICOLON, l.ch)
case '(':
@@ -40,7 +47,14 @@ func (l *Lexer) NextToken() token.Token {
case '-':
tok = newToken(token.MINUS, l.ch)
case '!':
- tok = newToken(token.BANG, l.ch)
+ if l.peekChar() == '=' {
+ ch := l.ch
+ l.readChar()
+ literal := string(ch) + string(l.ch)
+ tok = token.Token{Type: token.NOT_EQ, Literal: literal}
+ } else {
+ tok = newToken(token.BANG, l.ch)
+ }
case '*':
tok = newToken(token.ASTERISK, l.ch)
case '/':
@@ -83,6 +97,14 @@ func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
+func (l *Lexer) peekChar() byte {
+ if l.readPosition >= len(l.input) {
+ return 0
+ } else {
+ return l.input[l.readPosition]
+ }
+}
+
func (l *Lexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
diff --git a/token/token.go b/token/token.go
index a168b8f..63d5f41 100644
--- a/token/token.go
+++ b/token/token.go
@@ -19,6 +19,9 @@ const (
LT = "<"
GT = ">"
+ EQ = "=="
+ NOT_EQ = "!="
+
// Delimiters
COMMA = ","
SEMICOLON = ";"