From e6ed97ba112da1b429c5a56c512e71e5b50bd85a Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Tue, 30 Nov 2021 15:43:13 -0500 Subject: [PATCH] more keywords --- lexer/lexer.go | 12 ++++++++++++ lexer/lexer_test.go | 21 +++++++++++++++++++++ token/token.go | 25 +++++++++++++++++++++---- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lexer/lexer.go b/lexer/lexer.go index e7e8ce8..b68ff36 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -37,6 +37,18 @@ func (l *Lexer) NextToken() token.Token { tok = newToken(token.ASSIGN, l.ch) case '+': tok = newToken(token.PLUS, l.ch) + case '-': + tok = newToken(token.MINUS, l.ch) + case '!': + tok = newToken(token.BANG, l.ch) + case '/': + tok = newToken(token.SLASH, l.ch) + case '*': + tok = newToken(token.ASTERISK, l.ch) + case '<': + tok = newToken(token.LT, l.ch) + case '>': + tok = newToken(token.GT, l.ch) case '(': tok = newToken(token.LPAREN, l.ch) case ')': diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 7e90b2b..b1436ad 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -14,6 +14,15 @@ let add = fn(x,y) { x + y; }; let result = add(five, ten); +!-/*5; +5 < 10 > 5; + +if (5 < 10) { + return true; +} else { + return false; +} +// [...] ` tests := []struct { @@ -56,6 +65,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 901e47d..a3ed311 100644 --- a/token/token.go +++ b/token/token.go @@ -16,8 +16,15 @@ const ( INT = "INT" /// 1 2 3 4 5 //OPERATORS - ASSIGN = "=" - PLUS = "+" + ASSIGN = "=" + PLUS = "+" + MINUS = "-" + BANG = "!" + ASTERISK = "*" + SLASH = "/" + + LT = "<" + GT = ">" //DELIM COMMA = "" @@ -31,11 +38,21 @@ const ( // KEYWORDS FUNCTION = "FUNCTION" LET = "LET" + TRUE = "TRUE" + FALSE = "FALSE" + IF = "IF" + ELSE = "ELSE" + RETURN = "RETURN" ) 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 {