/
token.go
71 lines (58 loc) · 1.28 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// token/token.go
package token
// TokenType allows us to use many different values as `TokenTypes`,
// which in turn allows us to distinguish between different types of tokens.
type TokenType string
// Token defines the language's token data structure.
type Token struct {
Type TokenType
Literal string
}
const (
ILLEGAL = "ILLEGAL" // `ILLEGAL` signifies a token/character we don't know about
EOF = "EOF" // `EOF` stands for "end of file", tells our parser later on, for stopping parsing
// Identifiers + literals.
IDENT = "IDENT" // add, foobar, x, y, ...
INT = "INT" // 12321412
// Operators.
ASSIGN = "="
PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"
LT = "<"
GT = ">"
EQ = "=="
NOT_EQ = "!="
// Delimiters.
COMMA = ","
SEMICOLON = ";"
LPAREN = "("
RPAREN = ")"
LBRACE = "{"
RBRACE = "}"
// keywords.
FUNCTION = "fn"
LET = "let"
TRUE = "true"
FALSE = "false"
IF = "if"
ELSE = "else"
RETURN = "return"
)
var keywords = map[string]TokenType{
"fn": FUNCTION,
"let": LET,
"true": TRUE,
"false": FALSE,
"if": IF,
"else": ELSE,
"return": RETURN,
}
func LookupIdent(ident string) TokenType {
if tok, ok := keywords[ident]; ok {
return tok
}
return IDENT
}