code monkey make espresso
This commit is contained in:
commit
231be571da
1
lexer/lexer.go
Normal file
1
lexer/lexer.go
Normal file
@ -0,0 +1 @@
|
||||
package lexer
|
38
lexer/lexer_test.go
Normal file
38
lexer/lexer_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package lexer
|
||||
import (
|
||||
"testing"
|
||||
"monkey/token"
|
||||
)
|
||||
|
||||
func TestNextToken(t *testing.T) {
|
||||
input := `=+(){},;`
|
||||
|
||||
tests := []struct{
|
||||
expectedType token.TokenType
|
||||
expectedLiteral string
|
||||
}{
|
||||
{token.ASSIGN, "="},
|
||||
{token.PLUS, "+"},
|
||||
{token.LPAREN, "("},
|
||||
{token.RPAREN, ")"},
|
||||
{token.LBRACE, "{"},
|
||||
{token.RBRACE, "}"},
|
||||
{token.COMMA, ","},
|
||||
{token.SEMICOLON, ";"},
|
||||
{token.EOF, ""},
|
||||
}
|
||||
|
||||
l := New(input)
|
||||
for i, tt := range tests {
|
||||
tok := l.NextToken()
|
||||
|
||||
if tok.Type != tt.expectedType {
|
||||
t.Fatalf("tests[%d] - tokentype wrong. expected=%q, got %q",
|
||||
i, tt.expectedType, tok.Type)
|
||||
}
|
||||
if tok.Literal != tt.expectedLiteral {
|
||||
t.Fatalf("tests[%d] - literal wrong. expected=%q, got %q",
|
||||
i, tt.expectedLiteral, tok.Literal)
|
||||
}
|
||||
}
|
||||
}
|
34
token/token.go
Normal file
34
token/token.go
Normal file
@ -0,0 +1,34 @@
|
||||
package token
|
||||
|
||||
type TokenType string
|
||||
|
||||
type Token struct {
|
||||
Type TokenType
|
||||
Literal string
|
||||
}
|
||||
|
||||
const (
|
||||
ILLEGAL = "ILLEGAL"
|
||||
EOF = "EOF"
|
||||
|
||||
//Identifiers and literals
|
||||
IDENT = "IDENT" //add, foo, bar, a, b, c...
|
||||
INT = "INT" /// 1 2 3 4 5
|
||||
|
||||
//OPERATORS
|
||||
ASSIGN = "="
|
||||
PLUS = "+"
|
||||
|
||||
//DELIM
|
||||
COMMA = ""
|
||||
SEMICOLON = ";"
|
||||
|
||||
LPAREN = "("
|
||||
RPAREN = ")"
|
||||
LBRACE = "{"
|
||||
RBRACE = "}"
|
||||
|
||||
// KEYWORDS
|
||||
FUNCTION = "FUNCTION"
|
||||
LET = "LET"
|
||||
)
|
Loading…
Reference in New Issue
Block a user