diff options
| -rw-r--r-- | lexer/lexer.go | 26 | ||||
| -rw-r--r-- | token/token.go | 3 |
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 = ";" |
