/
token.go
102 lines (91 loc) · 2.03 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package token
// Type is a token type.
type Type string
// Token is the lexical symbol that gets returned after performing lexical analysis.
type Token struct {
Type Type
Literal string
LineNumber int
}
const (
// ILLEGAL token is created when symbols not belonging to our language are found.
ILLEGAL = "ILLEGAL"
// EOF - end of file
EOF = "EOF"
// IDENT - identifier
IDENT = "IDENT"
// INT - integer literal
INT = "INT"
// STRING - string literal
STRING = "STRING"
// BOOLEAN - boolean literal
BOOLEAN = "BOOLEAN"
// ASSIGN - assign operator
ASSIGN = "="
// PLUS - sum / concatenation
PLUS = "+"
// MINUS - subtraction / negate number
MINUS = "-"
// BANG - negate logical expression or value
BANG = "!"
// ASTERISK - multiplication
ASTERISK = "*"
// SLASH - division
SLASH = "/"
// LT - lower than
LT = "<"
// GT - greater than
GT = ">"
// LTE - lower than or equal
LTE = "<="
// GTE - greater than or equal
GTE = ">="
// EQ - equal
EQ = "=="
// NEQ - not equal
NEQ = "!="
// COMMA - values delimeter
COMMA = ","
// SEMICOLON - separates expressions
SEMICOLON = ";"
// COLON - separates key value pair in hashes
COLON = ":"
// LPAREN - function calls, binding expressions
LPAREN = "("
// RPAREN - function calls, binding expressions
RPAREN = ")"
// LBRACE - starts block statement
LBRACE = "{"
// RBRACE = ends block statements
RBRACE = "}"
// LBRACKET = starts an array statement
LBRACKET = "["
// RBRACKET = ends an array statement
RBRACKET = "]"
// FUNCTION keyword "fun"
FUNCTION = "FUNCTION"
// RETURN keyword "return"
RETURN = "RETURN"
// CONST keyword "const"
CONST = "CONST"
// IF keyword "if"
IF = "IF"
// ELSE keyword "else"
ELSE = "ELSE"
)
var keywords = map[string]Type{
"fun": FUNCTION,
"const": CONST,
"return": RETURN,
"true": BOOLEAN,
"false": BOOLEAN,
"if": IF,
"else": ELSE,
}
// LookUpIdent checks if identifier exists in the map of keywords.
func LookUpIdent(ident string) Type {
if tok, ok := keywords[ident]; ok {
return tok
}
return IDENT
}