From 1ec5781848e24ec3757c23c80fa324ae378b63d9 Mon Sep 17 00:00:00 2001 From: Akos Date: Sun, 6 Oct 2024 11:00:08 +0200 Subject: [PATCH 1/2] Add additional tokens --- lexer/lexer.go | 12 ++++++++++++ lexer/lexer_test.go | 16 +++++++++++++++- token/token.go | 11 +++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lexer/lexer.go b/lexer/lexer.go index 1342ac9..5f31b05 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -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 diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 209ce1e..23d3774 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -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 @@ -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, ""}, } diff --git a/token/token.go b/token/token.go index 218eee2..36e5de1 100644 --- a/token/token.go +++ b/token/token.go @@ -28,8 +28,15 @@ const ( INT = "INT" // 123456 // Operators - ASSIGN = "=" - PLUS = "+" + ASSIGN = "=" + PLUS = "+" + MINUS = "-" + BANG = "!" + ASTERISK = "*" + SLASH = "/" + + LT = "<" + GT = ">" // Delimeters COMMA = "," From b591493ea042db63fa64a4baa706522f267cda7a Mon Sep 17 00:00:00 2001 From: Akos Date: Sun, 6 Oct 2024 11:12:13 +0200 Subject: [PATCH 2/2] Add more keywords --- lexer/lexer_test.go | 25 ++++++++++++++++++++++++- token/token.go | 14 ++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 23d3774..73783d6 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -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 @@ -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, ""}, } diff --git a/token/token.go b/token/token.go index 36e5de1..be34320 100644 --- a/token/token.go +++ b/token/token.go @@ -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 { @@ -50,4 +55,9 @@ const ( // Keywords FUNCTION = "FUNCTION" LET = "LET" + TRUE = "TRUE" + FALSE = "FALSE" + IF = "IF" + ELSE = "ELSE" + RETURN = "RETURN" )