Skip to content

Commit

Permalink
Merge pull request #5 from akos011221/feat/further-extend
Browse files Browse the repository at this point in the history
Add additional tokens
  • Loading branch information
akos011221 authored Oct 6, 2024
2 parents 6a319ec + b591493 commit 006587a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 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
39 changes: 38 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ let add = fn(x, y) {
x + y;
};
let result = add(five, ten);`
let result = add(five, ten);
!-/*5;
5 < 10 > 5;
if (5 > 10) {
return false;
} else {
return true;
}`

tests := []struct {
expectedType token.TokenType
Expand Down Expand Up @@ -56,6 +64,35 @@ 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.IF, "if"},
{token.LPAREN, "("},
{token.INT, "5"},
{token.GT, ">"},
{token.INT, "10"},
{token.RPAREN, ")"},
{token.LBRACE, "{"},
{token.RETURN, "return"},
{token.FALSE, "false"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.ELSE, "else"},
{token.LBRACE, "{"},
{token.RETURN, "return"},
{token.TRUE, "true"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.EOF, ""},
}

Expand Down
25 changes: 21 additions & 4 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ type Token struct {
}

var keywords = map[string]TokenType{
"fn": FUNCTION,
"let": LET,
"fn": FUNCTION,
"let": LET,
"true": TRUE,
"false": FALSE,
"if": IF,
"else": ELSE,
"return": RETURN,
}

func LookupIdent(ident string) TokenType {
Expand All @@ -28,8 +33,15 @@ const (
INT = "INT" // 123456

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

LT = "<"
GT = ">"

// Delimeters
COMMA = ","
Expand All @@ -43,4 +55,9 @@ const (
// Keywords
FUNCTION = "FUNCTION"
LET = "LET"
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
)

0 comments on commit 006587a

Please sign in to comment.