|
@@ -7,7 +7,7 @@ package lexer
|
|
|
|
|
|
import (
|
|
|
"fgm/waiig15/token"
|
|
|
- )
|
|
|
+)
|
|
|
|
|
|
type Lexer struct {
|
|
|
input string
|
|
@@ -40,7 +40,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.LPAREN, l.ch)
|
|
|
case ')':
|
|
@@ -50,7 +57,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.SLASH, l.ch)
|
|
|
case '*':
|
|
@@ -94,6 +108,14 @@ func newToken(tokenType token.TokenType, ch byte) token.Token {
|
|
|
return token.Token{Type: tokenType, Literal: string(ch)}
|
|
|
}
|
|
|
|
|
|
+func (l *Lexer) peekChar() byte {
|
|
|
+ if l.readPosition >= len(l.input) {
|
|
|
+ return 0
|
|
|
+ } else {
|
|
|
+ return l.input[l.readPosition]
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (l *Lexer) readIdentifier() string {
|
|
|
position := l.position
|
|
|
for isLetter(l.ch) {
|