Skip to content

Commit

Permalink
Add additional tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Akos authored and Akos committed Oct 6, 2024
1 parent 3e8dd1e commit 1ec5781
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@ func (l *Lexer) NextToken() token.Token {
tok = newToken(token.COMMA, l.ch)
case '+':
tok = newToken(token.PLUS, l.ch)
case '-':
tok = newToken(token.MINUS, l.ch)
case '*':
tok = newToken(token.ASTERISK, l.ch)
case '/':
tok = newToken(token.SLASH, l.ch)
case '<':
tok = newToken(token.LT, l.ch)
case '>':
tok = newToken(token.GT, l.ch)
case '{':
tok = newToken(token.LBRACE, l.ch)
case '}':
tok = newToken(token.RBRACE, l.ch)
case '!':
tok = newToken(token.BANG, l.ch)
case 0:
tok.Literal = ""
tok.Type = token.EOF
Expand Down
16 changes: 15 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ let add = fn(x, y) {
x + y;
};
let result = add(five, ten);`
let result = add(five, ten);
!-/*5;
5 < 10 > 5;`

tests := []struct {
expectedType token.TokenType
Expand Down Expand Up @@ -56,6 +58,18 @@ let result = add(five, ten);`
{token.IDENT, "ten"},
{token.RPAREN, ")"},
{token.SEMICOLON, ";"},
{token.BANG, "!"},
{token.MINUS, "-"},
{token.SLASH, "/"},
{token.ASTERISK, "*"},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.INT, "5"},
{token.LT, "<"},
{token.INT, "10"},
{token.GT, ">"},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.EOF, ""},
}

Expand Down
11 changes: 9 additions & 2 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ const (
INT = "INT" // 123456

// Operators
ASSIGN = "="
PLUS = "+"
ASSIGN = "="
PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"

LT = "<"
GT = ">"

// Delimeters
COMMA = ","
Expand Down

0 comments on commit 1ec5781

Please sign in to comment.