code monkey make espresso

This commit is contained in:
Tyrel Souza 2021-11-30 14:02:01 -05:00
commit 231be571da
5 changed files with 77 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module monkey
go 1.17

1
lexer/lexer.go Normal file
View File

@ -0,0 +1 @@
package lexer

38
lexer/lexer_test.go Normal file
View 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)
}
}
}

1
main.go Normal file
View File

@ -0,0 +1 @@
package main

34
token/token.go Normal file
View 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"
)