comments
This commit is contained in:
@@ -7,6 +7,7 @@ type Lexer struct {
|
||||
position int // current position in input (point to current char)
|
||||
readPosition int // current reading position in input (after current char)
|
||||
ch byte // current char under exclamation
|
||||
prevCh byte // previous char read
|
||||
}
|
||||
|
||||
func New(input string) *Lexer {
|
||||
@@ -16,6 +17,7 @@ func New(input string) *Lexer {
|
||||
}
|
||||
|
||||
func (l *Lexer) readChar() {
|
||||
l.prevCh = l.ch
|
||||
if l.readPosition >= len(l.input) {
|
||||
l.ch = 0
|
||||
} else {
|
||||
@@ -39,6 +41,9 @@ func (l *Lexer) NextToken() token.Token {
|
||||
l.skipWhitespace()
|
||||
|
||||
switch l.ch {
|
||||
case '#':
|
||||
tok.Type = token.COMMENT
|
||||
tok.Literal = l.readLine()
|
||||
case '=':
|
||||
if l.peekChar() == '=' {
|
||||
ch := l.ch
|
||||
@@ -69,7 +74,13 @@ func (l *Lexer) NextToken() token.Token {
|
||||
}
|
||||
|
||||
case '/':
|
||||
tok = newToken(token.SLASH, l.ch)
|
||||
if l.peekChar() == '/' {
|
||||
l.readChar()
|
||||
tok.Type = token.COMMENT
|
||||
tok.Literal = l.readLine()
|
||||
} else {
|
||||
tok = newToken(token.SLASH, l.ch)
|
||||
}
|
||||
case '*':
|
||||
tok = newToken(token.ASTERISK, l.ch)
|
||||
case '<':
|
||||
@@ -165,3 +176,14 @@ func (l *Lexer) readString() string {
|
||||
}
|
||||
return l.input[position:l.position]
|
||||
}
|
||||
|
||||
func (l *Lexer) readLine() string {
|
||||
position := l.position + 1
|
||||
for {
|
||||
l.readChar()
|
||||
if l.ch == '\r' || l.ch == '\n' || l.ch == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return l.input[position:l.position]
|
||||
}
|
||||
|
||||
@@ -6,13 +6,15 @@ import (
|
||||
)
|
||||
|
||||
func TestNextToken(t *testing.T) {
|
||||
input := `let five = 5;
|
||||
input := `#!./monkey-lang
|
||||
let five = 5;
|
||||
let ten = 10;
|
||||
|
||||
let add = fn(x, y) {
|
||||
x + y;
|
||||
};
|
||||
|
||||
# this is a comment
|
||||
let result = add(five, ten);
|
||||
!-/*5;
|
||||
5 < 10 > 5;
|
||||
@@ -23,6 +25,7 @@ func TestNextToken(t *testing.T) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// this is another comment
|
||||
10 == 10;
|
||||
10 != 9;
|
||||
"foobar"
|
||||
@@ -35,6 +38,7 @@ func TestNextToken(t *testing.T) {
|
||||
expectedType token.TokenType
|
||||
expectedLiteral string
|
||||
}{
|
||||
{token.COMMENT, "!./monkey-lang"},
|
||||
{token.LET, "let"},
|
||||
{token.IDENT, "five"},
|
||||
{token.ASSIGN, "="},
|
||||
@@ -61,6 +65,7 @@ func TestNextToken(t *testing.T) {
|
||||
{token.SEMICOLON, ";"},
|
||||
{token.RBRACE, "}"},
|
||||
{token.SEMICOLON, ";"},
|
||||
{token.COMMENT, " this is a comment"},
|
||||
{token.LET, "let"},
|
||||
{token.IDENT, "result"},
|
||||
{token.ASSIGN, "="},
|
||||
@@ -106,7 +111,7 @@ func TestNextToken(t *testing.T) {
|
||||
{token.SEMICOLON, ";"},
|
||||
|
||||
{token.RBRACE, "}"},
|
||||
|
||||
{token.COMMENT, " this is another comment"},
|
||||
{token.INT, "10"},
|
||||
{token.EQ, "=="},
|
||||
{token.INT, "10"},
|
||||
|
||||
Reference in New Issue
Block a user