Skip to content

Commit

Permalink
Working with two character tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Akos authored and Akos committed Oct 6, 2024
1 parent b591493 commit 15929e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
26 changes: 24 additions & 2 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 '(':
Expand All @@ -35,7 +42,14 @@ func (l *Lexer) NextToken() token.Token {
case '}':
tok = newToken(token.RBRACE, 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 0:
tok.Literal = ""
tok.Type = token.EOF
Expand Down Expand Up @@ -113,3 +127,11 @@ func (l *Lexer) readNumber() string {
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]
}
}
14 changes: 13 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ if (5 > 10) {
return false;
} else {
return true;
}`
}
10 == 10;
10 != 5;
`

tests := []struct {
expectedType token.TokenType
Expand Down Expand Up @@ -93,6 +97,14 @@ if (5 > 10) {
{token.TRUE, "true"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.INT, "10"},
{token.EQ, "=="},
{token.INT, "10"},
{token.SEMICOLON, ";"},
{token.INT, "10"},
{token.NOT_EQ, "!="},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.EOF, ""},
}

Expand Down
2 changes: 2 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ const (
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
EQ = "=="
NOT_EQ = "!="
)

0 comments on commit 15929e5

Please sign in to comment.