Skip to content

Commit

Permalink
ref(lexer): move peak method to below readChar for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismael Matsinhe committed Jun 13, 2023
1 parent f4f0f37 commit 574339e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (l *Lexer) NextToken() token.Token {
tok = newToken(token.COLON, l.ch)

default:
if isLetter(l.ch) { // isLetter is a helper function
if isLetter(l.ch) {
tok.Literal = l.readIdentifier()
tok.Type = token.LookupIdent(tok.Literal)
return tok
Expand All @@ -112,16 +112,18 @@ func (l *Lexer) NextToken() token.Token {
return tok
}

func (l *Lexer) readIdentifier() string { // readIdentifier is a helper function
func isLetter(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}

func (l *Lexer) readIdentifier() string {
position := l.position
for isLetter(l.ch) { // isLetter is a helper function
for isLetter(l.ch) {
l.readChar()
}
return l.input[position:l.position]
}
func isLetter(ch byte) bool { // isLetter is a helper function
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}

func newToken(tokenType token.TokenType, ch byte) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
}
Expand Down

0 comments on commit 574339e

Please sign in to comment.