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]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user