diff --git a/lexer/lexer.go b/lexer/lexer.go index dd64781..ee395f1 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -21,13 +21,17 @@ func New(input string) *Lexer { } func (l *Lexer) NextToken() token.Token { - var tok token.Token - l.skipWhitespace() + if l.ch == '/' && l.peekCharIs('/') { + l.skipComment() + } + + var tok token.Token + switch l.ch { case '=': - if l.peekChar() == '=' { + if l.peekCharIs('=') { ch := l.ch l.readChar() literal := string(ch) + string(l.ch) @@ -40,7 +44,7 @@ func (l *Lexer) NextToken() token.Token { case '-': tok = newToken(token.MINUS, l.ch) case '!': - if l.peekChar() == '=' { + if l.peekCharIs('=') { ch := l.ch l.readChar() literal := string(ch) + string(l.ch) @@ -114,6 +118,10 @@ func (l *Lexer) peekChar() byte { return l.input[l.readPosition] } +func (l *Lexer) peekCharIs(c byte) bool { + return l.peekChar() == c +} + func (l *Lexer) readIdentifier() string { position := l.position for isLetter(l.ch) { @@ -141,6 +149,13 @@ func (l *Lexer) readString() string { return l.input[position:l.position] } +func (l *Lexer) skipComment() { + for l.ch != '\n' && l.ch != '\r' { + l.readChar() + } + l.skipWhitespace() +} + func (l *Lexer) skipWhitespace() { for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' { l.readChar() diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index dfb5fa1..905680c 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -24,8 +24,10 @@ func TestNextToken(t *testing.T) { return false; } + //comment + 10 == 10; - 10 != 9; + 10 != 9; // Inline comment "foobar" "foo bar" `