optimizations
This commit is contained in:
@@ -182,9 +182,7 @@ func (l *Lexer) NextToken() token.Token {
|
||||
tok.Type = token.LookupIdent(tok.Literal)
|
||||
return tok
|
||||
} else if isDigit(l.ch) {
|
||||
tok.Type = token.INT
|
||||
tok.Literal = l.readNumber()
|
||||
return tok
|
||||
return l.readNumber()
|
||||
} else {
|
||||
tok = newToken(token.ILLEGAL, l.ch)
|
||||
}
|
||||
@@ -209,12 +207,22 @@ func (l *Lexer) readIdentifier() string {
|
||||
return l.input[position:l.position]
|
||||
}
|
||||
|
||||
func (l *Lexer) readNumber() string {
|
||||
func (l *Lexer) readNumber() token.Token {
|
||||
position := l.position
|
||||
for isDigit(l.ch) {
|
||||
l.readChar()
|
||||
}
|
||||
return l.input[position:l.position]
|
||||
intPart := l.input[position:l.position]
|
||||
if l.ch == '.' {
|
||||
l.readChar()
|
||||
position := l.position
|
||||
for isDigit(l.ch) {
|
||||
l.readChar()
|
||||
}
|
||||
fracPart := l.input[position:l.position]
|
||||
return token.Token{Type: token.FLOAT, Literal: intPart + "." + fracPart}
|
||||
}
|
||||
return token.Token{Type: token.INT, Literal: intPart}
|
||||
}
|
||||
|
||||
func isLetter(ch byte) bool {
|
||||
|
||||
@@ -210,3 +210,35 @@ c := "\r\n\t"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFloats(t *testing.T) {
|
||||
input := `2.5
|
||||
3.14
|
||||
`
|
||||
|
||||
tests := []struct {
|
||||
expectedType token.TokenType
|
||||
expectedLiteral string
|
||||
}{
|
||||
{token.FLOAT, "2.5"},
|
||||
{token.FLOAT, "3.14"},
|
||||
{token.EOF, ""},
|
||||
}
|
||||
|
||||
lexer := New(input)
|
||||
|
||||
for i, test := range tests {
|
||||
token := lexer.NextToken()
|
||||
|
||||
if token.Type != test.expectedType {
|
||||
t.Fatalf("tests[%d] - token type wrong. expected=%q, got=%q",
|
||||
i, test.expectedType, token.Type)
|
||||
}
|
||||
|
||||
if token.Literal != test.expectedLiteral {
|
||||
t.Fatalf("tests[%d] - literal wrong. expected=%q, got=%q",
|
||||
i, test.expectedLiteral, token.Literal)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user