Skip to content

Commit

Permalink
Added // comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kscarlett committed Jan 1, 2018
1 parent bf32f30 commit c646c08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 19 additions & 4 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func TestNextToken(t *testing.T) {
return false;
}
//comment
10 == 10;
10 != 9;
10 != 9; // Inline comment
"foobar"
"foo bar"
`
Expand Down

0 comments on commit c646c08

Please sign in to comment.