comments
Some checks failed
Build / build (push) Failing after 1m10s
Test / build (push) Failing after 11m40s

This commit is contained in:
Chuck Smith
2024-03-16 19:31:23 -04:00
parent 7463b3af39
commit 83ad72a7fc
7 changed files with 106 additions and 6 deletions

View File

@@ -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]
}