monkey/token/token.go

64 lines
907 B
Go
Raw Normal View History

2021-11-30 19:02:01 +00:00
package token
type TokenType string
type Token struct {
2021-11-30 20:27:22 +00:00
Type TokenType
Literal string
2021-11-30 19:02:01 +00:00
}
const (
2021-11-30 20:27:22 +00:00
ILLEGAL = "ILLEGAL"
EOF = "EOF"
2021-11-30 19:02:01 +00:00
2021-11-30 20:27:22 +00:00
//Identifiers and literals
IDENT = "IDENT" //add, foo, bar, a, b, c...
INT = "INT" /// 1 2 3 4 5
2021-11-30 19:02:01 +00:00
2021-11-30 20:27:22 +00:00
//OPERATORS
2021-11-30 20:43:13 +00:00
ASSIGN = "="
PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"
LT = "<"
GT = ">"
2021-11-30 19:02:01 +00:00
2021-11-30 20:27:22 +00:00
//DELIM
COMMA = ""
SEMICOLON = ";"
2021-11-30 19:02:01 +00:00
2021-11-30 20:27:22 +00:00
LPAREN = "("
RPAREN = ")"
LBRACE = "{"
RBRACE = "}"
2021-11-30 19:02:01 +00:00
2021-11-30 20:27:22 +00:00
// KEYWORDS
FUNCTION = "FUNCTION"
LET = "LET"
2021-11-30 20:43:13 +00:00
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
2021-11-30 19:02:01 +00:00
)
2021-11-30 20:27:22 +00:00
var keywords = map[string]TokenType{
2021-11-30 20:43:13 +00:00
"fn": FUNCTION,
"let": LET,
"true": TRUE,
"false": FALSE,
"if": IF,
"else": ELSE,
"return": RETURN,
2021-11-30 20:27:22 +00:00
}
func LookupIdent(ident string) TokenType {
if tok, ok := keywords[ident]; ok {
return tok
}
return IDENT
}