|
@@ -7,7 +7,7 @@ package lexer
|
|
|
|
|
|
import (
|
|
import (
|
|
"fgm/waiig15/token"
|
|
"fgm/waiig15/token"
|
|
- )
|
|
|
|
|
|
+)
|
|
|
|
|
|
type Lexer struct {
|
|
type Lexer struct {
|
|
input string
|
|
input string
|
|
@@ -36,6 +36,8 @@ func (l *Lexer) readChar() {
|
|
func (l *Lexer) NextToken() token.Token {
|
|
func (l *Lexer) NextToken() token.Token {
|
|
var tok token.Token
|
|
var tok token.Token
|
|
|
|
|
|
|
|
+ l.skipWhitespace()
|
|
|
|
+
|
|
switch l.ch {
|
|
switch l.ch {
|
|
case '=':
|
|
case '=':
|
|
tok = newToken(token.ASSIGN, l.ch)
|
|
tok = newToken(token.ASSIGN, l.ch)
|
|
@@ -56,6 +58,20 @@ func (l *Lexer) NextToken() token.Token {
|
|
case 0:
|
|
case 0:
|
|
tok.Literal = ""
|
|
tok.Literal = ""
|
|
tok.Type = token.EOF
|
|
tok.Type = token.EOF
|
|
|
|
+ default:
|
|
|
|
+ if isLetter(l.ch) {
|
|
|
|
+ tok.Literal = l.readIdentifier()
|
|
|
|
+ tok.Type = token.LookupIdent(tok.Literal)
|
|
|
|
+ // We already read the next char, so avoid the final readChar().
|
|
|
|
+ return tok
|
|
|
|
+ } else if isDigit(l.ch) {
|
|
|
|
+ tok.Type = token.INT
|
|
|
|
+ tok.Literal = l.readNumber()
|
|
|
|
+ // Ditto.
|
|
|
|
+ return tok
|
|
|
|
+ } else {
|
|
|
|
+ tok = newToken(token.ILLEGAL, l.ch)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
l.readChar()
|
|
l.readChar()
|
|
@@ -63,5 +79,35 @@ func (l *Lexer) NextToken() token.Token {
|
|
}
|
|
}
|
|
|
|
|
|
func newToken(tokenType token.TokenType, ch byte) token.Token {
|
|
func newToken(tokenType token.TokenType, ch byte) token.Token {
|
|
- return token.Token{ Type: tokenType, Literal: string(ch) }
|
|
|
|
|
|
+ return token.Token{Type: tokenType, Literal: string(ch)}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (l *Lexer) readIdentifier() string {
|
|
|
|
+ position := l.position
|
|
|
|
+ for isLetter(l.ch) {
|
|
|
|
+ l.readChar()
|
|
|
|
+ }
|
|
|
|
+ return l.input[position:l.position]
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (l *Lexer) readNumber() string {
|
|
|
|
+ position := l.position
|
|
|
|
+ for isDigit(l.ch) {
|
|
|
|
+ l.readChar()
|
|
|
|
+ }
|
|
|
|
+ return l.input[position:l.position]
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func isDigit(ch byte) bool {
|
|
|
|
+ return '0' <= ch && ch <= '9'
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func isLetter(ch byte) bool {
|
|
|
|
+ return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (l *Lexer) skipWhitespace() {
|
|
|
|
+ for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
|
|
|
|
+ l.readChar()
|
|
|
|
+ }
|
|
}
|
|
}
|