Skip to content

Commit

Permalink
Add more keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Akos authored and Akos committed Oct 6, 2024
1 parent 1ec5781 commit b591493
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
25 changes: 24 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ x + y;
let result = add(five, ten);
!-/*5;
5 < 10 > 5;`
5 < 10 > 5;
if (5 > 10) {
return false;
} else {
return true;
}`

tests := []struct {
expectedType token.TokenType
Expand Down Expand Up @@ -70,6 +76,23 @@ let result = add(five, ten);
{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
14 changes: 12 additions & 2 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 Down Expand Up @@ -50,4 +55,9 @@ const (
// Keywords
FUNCTION = "FUNCTION"
LET = "LET"
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
)

0 comments on commit b591493

Please sign in to comment.