diff options
| author | Bobby <[email protected]> | 2023-11-02 22:22:54 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-11-02 22:22:54 -0400 |
| commit | 3c98969220c0f3c6372aef3207a98e4cbcc9a135 (patch) | |
| tree | af35d2ac4deb882d5d23026dccbd8f57a11548d9 /lexer | |
| parent | 195c72b225c6f171011dfdde550f63cb409b7249 (diff) | |
| download | mana-3c98969220c0f3c6372aef3207a98e4cbcc9a135.tar.xz mana-3c98969220c0f3c6372aef3207a98e4cbcc9a135.zip | |
parser:let|ident
Diffstat (limited to 'lexer')
| -rw-r--r-- | lexer/lexer.go | 17 | ||||
| -rw-r--r-- | lexer/lexer_test.go | 9 |
2 files changed, 14 insertions, 12 deletions
diff --git a/lexer/lexer.go b/lexer/lexer.go index b65d00b..589ed60 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -1,4 +1,5 @@ package lexer + import "mana/tokens" type Lexer struct { @@ -10,7 +11,7 @@ type Lexer struct { // New returns a new Lexer instance. func New(input string) *Lexer { - l := &Lexer{input: input} + var l *Lexer = &Lexer{input: input} l.readChar() return l } @@ -24,9 +25,9 @@ func (l *Lexer) NextToken() tokens.Token { switch l.ch { case '=': if l.peekChar() == '=' { - ch := l.ch + var ch byte = l.ch l.readChar() - literal := string(ch) + string(l.ch) + var literal string = string(ch) + string(l.ch) tok = tokens.Token{Type: tokens.EQ, Literal: literal} } else { tok = newToken(tokens.ASSIGN, l.ch) @@ -45,9 +46,9 @@ func (l *Lexer) NextToken() tokens.Token { tok = newToken(tokens.GT, l.ch) case '!': if l.peekChar() == '=' { - ch := l.ch + var ch byte = l.ch l.readChar() - literal := string(ch) + string(l.ch) + var literal string = string(ch) + string(l.ch) tok = tokens.Token{Type: tokens.NOT_EQ, Literal: literal} } else { tok = newToken(tokens.BANG, l.ch) @@ -80,7 +81,7 @@ func (l *Lexer) NextToken() tokens.Token { tok = newToken(tokens.ILLEGAL, l.ch) } } - + l.readChar() return tok } @@ -129,7 +130,7 @@ func (l *Lexer) peekChar() byte { // readIdentifier reads an identifier and advances the position in the input string until it encounters a non-letter character. func (l *Lexer) readIdentifier() string { - position := l.position + var position int = l.position for isLetter(l.ch) { l.readChar() } @@ -138,7 +139,7 @@ func (l *Lexer) readIdentifier() string { // readNumber reads a number and advances the position in the input string until it encounters a non-digit character. func (l *Lexer) readNumber() string { - position := l.position + var position int = l.position for isDigit(l.ch) { l.readChar() } diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index f8e3eae..5cce79d 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -6,7 +6,7 @@ import ( ) func TestNextToken(t *testing.T) { - input := ` + const input string = ` let five = 5; let ten = 10; @@ -29,7 +29,7 @@ func TestNextToken(t *testing.T) { 10 != 9; ` - tests := []struct { + var tests = []struct { expectedType tokens.TokenType expectedLiteral string }{ @@ -109,10 +109,11 @@ func TestNextToken(t *testing.T) { {tokens.EOF, ""}, } - l := New(input) + + var l *Lexer = New(input) for i, tt := range tests { - tok := l.NextToken() + var tok tokens.Token = l.NextToken() if tok.Type != tt.expectedType { t.Fatalf("tests[%d] - tokentype wrong. expected=%q, got=%q", |
