68 lines
900 B
Go
68 lines
900 B
Go
package token
|
|
|
|
type TokenType string
|
|
|
|
type Token struct {
|
|
Type TokenType
|
|
Literal string
|
|
}
|
|
|
|
const (
|
|
ILLEGAL = "ILLEGAL"
|
|
EOF = "EOF"
|
|
|
|
// identifiers + literals
|
|
IDENT = "IDENT"
|
|
INT = "INT"
|
|
|
|
// operators
|
|
PLUS = "+"
|
|
MINUS = "-"
|
|
ASSIGN = "="
|
|
BANG = "!"
|
|
ASTERISK = "*"
|
|
SLASH = "/"
|
|
|
|
EQ = "=="
|
|
NOT_EQ = "!="
|
|
|
|
// delimiters
|
|
COMMA = ","
|
|
SEMICOLON = ";"
|
|
|
|
LPAREN = "("
|
|
RPAREN = ")"
|
|
LBRACE = "{"
|
|
RBRACE = "}"
|
|
|
|
LT = "<"
|
|
GT = ">"
|
|
|
|
// keywords
|
|
FUNCTION = "function"
|
|
LET = "let"
|
|
IF = "if"
|
|
ELSE = "else"
|
|
TRUE = "true"
|
|
FALSE = "false"
|
|
RETURN = "return"
|
|
)
|
|
|
|
var keywords = map[string]TokenType{
|
|
"fn": FUNCTION,
|
|
"let": LET,
|
|
"return": RETURN,
|
|
"if": IF,
|
|
"else": ELSE,
|
|
"true": TRUE,
|
|
"false": FALSE,
|
|
}
|
|
|
|
func LookupIdent(ident string) TokenType {
|
|
if tt, ok := keywords[ident]; ok {
|
|
return tt
|
|
}
|
|
|
|
return IDENT
|
|
}
|