From 231be571dacb84faf2d174e9a26255f906e8c1ad Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Tue, 30 Nov 2021 14:02:01 -0500 Subject: [PATCH] code monkey make espresso --- go.mod | 3 +++ lexer/lexer.go | 1 + lexer/lexer_test.go | 38 ++++++++++++++++++++++++++++++++++++++ main.go | 1 + token/token.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 go.mod create mode 100644 lexer/lexer.go create mode 100644 lexer/lexer_test.go create mode 100644 main.go create mode 100644 token/token.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1187b62 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module monkey + +go 1.17 diff --git a/lexer/lexer.go b/lexer/lexer.go new file mode 100644 index 0000000..9beaa38 --- /dev/null +++ b/lexer/lexer.go @@ -0,0 +1 @@ +package lexer diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go new file mode 100644 index 0000000..cf1b91d --- /dev/null +++ b/lexer/lexer_test.go @@ -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) + } + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/main.go @@ -0,0 +1 @@ +package main diff --git a/token/token.go b/token/token.go new file mode 100644 index 0000000..f3d6779 --- /dev/null +++ b/token/token.go @@ -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" +)