more keywords

This commit is contained in:
Tyrel Souza 2021-11-30 15:43:13 -05:00
parent 4b65ef822e
commit e6ed97ba11
3 changed files with 54 additions and 4 deletions

View File

@ -37,6 +37,18 @@ func (l *Lexer) NextToken() token.Token {
tok = newToken(token.ASSIGN, l.ch) tok = newToken(token.ASSIGN, l.ch)
case '+': case '+':
tok = newToken(token.PLUS, l.ch) 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 '(': case '(':
tok = newToken(token.LPAREN, l.ch) tok = newToken(token.LPAREN, l.ch)
case ')': case ')':

View File

@ -14,6 +14,15 @@ let add = fn(x,y) {
x + y; x + y;
}; };
let result = add(five, ten); let result = add(five, ten);
!-/*5;
5 < 10 > 5;
if (5 < 10) {
return true;
} else {
return false;
}
// [...]
` `
tests := []struct { tests := []struct {
@ -56,6 +65,18 @@ let result = add(five, ten);
{token.IDENT, "ten"}, {token.IDENT, "ten"},
{token.RPAREN, ")"}, {token.RPAREN, ")"},
{token.SEMICOLON, ";"}, {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, ""}, {token.EOF, ""},
} }

View File

@ -18,6 +18,13 @@ const (
//OPERATORS //OPERATORS
ASSIGN = "=" ASSIGN = "="
PLUS = "+" PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"
LT = "<"
GT = ">"
//DELIM //DELIM
COMMA = "" COMMA = ""
@ -31,11 +38,21 @@ const (
// KEYWORDS // KEYWORDS
FUNCTION = "FUNCTION" FUNCTION = "FUNCTION"
LET = "LET" LET = "LET"
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
) )
var keywords = map[string]TokenType{ var keywords = map[string]TokenType{
"fn": FUNCTION, "fn": FUNCTION,
"let": LET, "let": LET,
"true": TRUE,
"false": FALSE,
"if": IF,
"else": ELSE,
"return": RETURN,
} }
func LookupIdent(ident string) TokenType { func LookupIdent(ident string) TokenType {