-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.go
138 lines (118 loc) · 2.46 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package token
import (
"fmt"
)
// token
type TokenType int
const (
TOKEN_ILLEGAL TokenType = (iota - 1) // Illegal token
TOKEN_EOF //End Of File
TOKEN_PLUS // +
TOKEN_MINUS // -
TOKEN_MULTIPLY // *
TOKEN_DIVIDE // '/'
TOKEN_MOD // '%'
TOKEN_POWER // **
TOKEN_LPAREN // (
TOKEN_RPAREN // )
TOKEN_ASSIGN // =
TOKEN_SEMICOLON //;
TOKEN_NUMBER //10 or 10.1
TOKEN_IDENTIFIER //identifier
//reserved keywords
TOKEN_TRUE //true
TOKEN_FALSE //false
TOKEN_NIL // nil
TOKEN_LET //let
)
//for debug & testing
func (tt TokenType) String() string {
switch tt {
case TOKEN_ILLEGAL:
return "ILLEGAL"
case TOKEN_EOF:
return "EOF"
case TOKEN_PLUS:
return "+"
case TOKEN_MINUS:
return "-"
case TOKEN_MULTIPLY:
return "*"
case TOKEN_DIVIDE:
return "/"
case TOKEN_MOD:
return "%"
case TOKEN_POWER:
return "**"
case TOKEN_LPAREN:
return "("
case TOKEN_RPAREN:
return ")"
case TOKEN_ASSIGN:
return "="
case TOKEN_SEMICOLON:
return ";"
case TOKEN_NUMBER:
return "NUMBER"
case TOKEN_IDENTIFIER:
return "IDENTIFIER"
case TOKEN_TRUE:
return "TRUE"
case TOKEN_FALSE:
return "FALSE"
case TOKEN_NIL:
return "NIL"
case TOKEN_LET:
return "LET"
default:
return "UNKNOWN"
}
}
var keywords = map[string]TokenType{
"true": TOKEN_TRUE,
"false": TOKEN_FALSE,
"nil": TOKEN_NIL,
"let": TOKEN_LET,
}
type Token struct {
Pos Position
Type TokenType
Literal string
}
//Stringer method for Token
func (t Token) String() string {
return fmt.Sprintf("Position: %s, Type: %s, Literal: %s", t.Pos, t.Type, t.Literal)
}
//Position is the location of a code point in the source
type Position struct {
Filename string
Offset int //offset relative to entire file
Line int
Col int //offset relative to each line
}
//Stringer method for Position
func (p Position) String() string {
var msg string
if p.Filename == "" {
msg = fmt.Sprint(" <", p.Line, ":", p.Col, "> ")
} else {
msg = fmt.Sprint(" <", p.Filename, ":", p.Line, ":", p.Col, "> ")
}
return msg
}
//We could not use `Line()` as function name, because `Line` is the struct's field
func (p Position) Sline() string { //String line
var msg string
if p.Filename == "" {
msg = fmt.Sprint(p.Line)
} else {
msg = fmt.Sprint(" <", p.Filename, ":", p.Line, "> ")
}
return msg
}
func LookupIdent(ident string) TokenType {
if tok, ok := keywords[ident]; ok {
return tok
}
return TOKEN_IDENTIFIER
}