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)
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 ')':

View File

@ -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, ""},
}

View File

@ -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 {