-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
242 lines (209 loc) · 4.55 KB
/
scanner.go
File metadata and controls
242 lines (209 loc) · 4.55 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package glox
import (
"fmt"
"strconv"
)
// Keywords are the keywords defined in the language
var Keywords = map[string]int{
"and": AND,
"class": CLASS,
"else": ELSE,
"false": FALSE,
"for": FOR,
"fun": FUN,
"if": IF,
"nil": NIL,
"or": OR,
"print": PRINT,
"return": RETURN,
"super": SUPER,
"this": THIS,
"true": TRUE,
"var": VAR,
"while": WHILE,
}
// Scanner is used to generate tokens from a source text
type Scanner struct {
Source string
Tokens []Token
start, current int
line int
}
func (sc *Scanner) atEnd() bool {
return sc.current >= len([]rune(sc.Source))
}
func (sc *Scanner) advance() rune {
sc.current++
return []rune(sc.Source)[sc.current-1]
}
func (sc *Scanner) peek() rune {
if sc.atEnd() {
return '\x00'
}
return []rune(sc.Source)[sc.current]
}
func (sc *Scanner) peekNext() rune {
if sc.current+1 >= len([]rune(sc.Source)) {
return '\x00'
}
return []rune(sc.Source)[sc.current+1]
}
func (sc *Scanner) match(c rune) bool {
if sc.atEnd() {
return false
}
if []rune(sc.Source)[sc.current] != c {
return false
}
sc.current++
return true
}
func (sc *Scanner) addToken(token int) {
sc.addLiteralToken(token, nil)
}
func (sc *Scanner) addLiteralToken(token int, literal interface{}) {
text := string([]rune(sc.Source)[sc.start:sc.current])
sc.Tokens = append(sc.Tokens, Token{
TokenType: token,
Lexeme: text,
Literal: literal,
Line: sc.line,
})
}
func (sc *Scanner) scanStr() {
for sc.peek() != '"' && !sc.atEnd() { // Keep reading characters until a " is found or the end is reached
if sc.peek() == '\n' { // If we encounter a newline
sc.line++ // increase the line count
}
sc.advance() // Advance the scanner to the next character
}
if sc.atEnd() { // If we're at the end of the source then there was never a closing "
fmt.Printf("Unterminated string")
return
}
sc.advance() // Advance last the final "
value := []rune(sc.Source)[sc.start+1 : sc.current-1] // Get the value of the string by reading from the source
sc.addLiteralToken(STRING, string(value)) // Add the string token to the token list
}
func isDigit(c rune) bool {
return c >= '0' && c <= '9'
}
func isAlpha(c rune) bool {
return (c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z' ||
c == '_')
}
func isAlphaNumeric(c rune) bool {
return isAlpha(c) || isDigit(c)
}
func (sc *Scanner) scanNum() {
for isDigit(sc.peek()) {
sc.advance()
}
if sc.peek() == '.' && isDigit(sc.peekNext()) {
sc.advance()
for isDigit(sc.peek()) {
sc.advance()
}
}
num, _ := strconv.ParseFloat(string([]rune(sc.Source)[sc.start:sc.current]), 64)
sc.addLiteralToken(NUMBER, num)
}
func (sc *Scanner) scanIdentifier() {
for isAlphaNumeric(sc.peek()) {
sc.advance()
}
text := string([]rune(sc.Source[sc.start:sc.current]))
tokenType := Keywords[text]
if tokenType == 0 {
tokenType = IDENTIFIER
}
sc.addToken(tokenType)
}
func (sc *Scanner) scanToken() {
c := sc.advance()
switch c {
case '(':
sc.addToken(LEFT_PAREN)
case ')':
sc.addToken(RIGHT_PAREN)
case '{':
sc.addToken(LEFT_BRACE)
case '}':
sc.addToken(RIGHT_BRACE)
case ',':
sc.addToken(COMMA)
case '.':
sc.addToken(DOT)
case '-':
sc.addToken(MINUS)
case '+':
sc.addToken(PLUS)
case ';':
sc.addToken(SEMICOLON)
case '*':
if sc.match('*') {
sc.addToken(STARSTAR)
} else {
sc.addToken(STAR)
}
case '!':
if sc.match('=') {
sc.addToken(BANG_EQUAL)
} else {
sc.addToken(BANG)
}
case '=':
if sc.match('=') {
sc.addToken(EQUAL_EQUAL)
} else {
sc.addToken(EQUAL)
}
case '<':
if sc.match('=') {
sc.addToken(LESS_EQUAL)
} else {
sc.addToken(LESS)
}
case '>':
if sc.match('=') {
sc.addToken(GREATER_EQUAL)
} else {
sc.addToken(GREATER)
}
case '/':
if sc.match('/') {
for sc.peek() != '\n' && !sc.atEnd() {
sc.advance()
}
} else {
sc.addToken(SLASH)
}
case ' ':
fallthrough
case '\r':
fallthrough
case '\t':
case '\n':
sc.line++
case '"':
sc.scanStr()
default:
if isDigit(c) {
sc.scanNum()
} else if isAlphaNumeric(c) {
sc.scanIdentifier()
} else {
fmt.Printf("Unexpected character on line %v\n", sc.line)
}
}
}
// ScanTokens will scan the soruce code for tokens
func (sc *Scanner) ScanTokens() []Token {
for !sc.atEnd() { // Keep reading every character until at the end of the file
sc.start = sc.current // The token starts at the current position
sc.scanToken() // Scan a token from the source
}
sc.addToken(EOF) // Add en end of file token to the end
return sc.Tokens // Return the list of tokens
}