From f52fce892fc277f5dcb8d75a6525fb7922323aed Mon Sep 17 00:00:00 2001 From: Flipez Date: Wed, 29 Sep 2021 22:09:59 +0200 Subject: [PATCH] add single line comments This fixes #7 Signed-off-by: Flipez --- lexer/lexer.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lexer/lexer.go b/lexer/lexer.go index 2e54ce5..360012a 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -54,7 +54,12 @@ func (l *Lexer) NextToken() token.Token { tok = newToken(token.BANG, l.ch) } case '/': - tok = newToken(token.SLASH, l.ch) + if l.peekChar() == '/' { + l.skipComment() + tok = l.NextToken() + } else { + tok = newToken(token.SLASH, l.ch) + } case '*': tok = newToken(token.ASTERISK, l.ch) case '<': @@ -146,6 +151,13 @@ func (l *Lexer) skipWhitespace() { } } +func (l *Lexer) skipComment() { + for l.ch != '\n' && l.ch != 0 { + l.readChar() + } + l.skipWhitespace() +} + func isLetter(ch byte) bool { return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' }