-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
248 lines (209 loc) · 5.22 KB
/
lexer.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
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
242
243
244
245
246
247
248
package main
import (
"container/list"
"fmt"
. "github.com/pokerG/Compiler/common"
"reflect"
"strings"
)
var fun map[string]bool
type Lexer struct {
input string
input_length int
pos int
line_number int
char_number int
start_pos int
current_char byte
running bool
buffer string
token_stream list.List
}
type Token struct {
token_type int
content string
line_number int
char_number int
}
func (self *Lexer) flushBuffer() string {
result := self.buffer
self.buffer = ""
return result
}
func (self *Lexer) createToken(token_type int, content string) {
token := &Token{}
token.token_type = token_type
token.content = content
token.line_number = self.line_number
token.char_number = self.char_number
fmt.Printf("(type %d , content %s)\n", token.token_type, token.content)
self.token_stream.PushBack(token)
}
func (self *Lexer) peek(ahead int) byte {
return self.input[self.pos+ahead]
}
func (self *Lexer) createLexer(input string) {
self.input = input
self.pos = 0
self.input_length = len(self.input)
self.line_number = 1
self.char_number = 1
self.start_pos = 0
self.current_char = self.input[self.pos]
self.running = true
}
func isDigit(c byte) bool {
return '0' <= c && c <= '9'
}
func isLayout(c byte) bool {
return c <= 32
}
func isLetter(c byte) bool {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
}
func isLetterOrDigit(c byte) bool {
return isDigit(c) || isLetter(c)
}
func isOperator(c byte) bool {
return strings.Contains("+-*/=", string(c))
}
func isSeparator(c byte) bool {
return strings.Contains(";,{}()[]", string(c))
}
func (self *Lexer) consumeCharacter() {
if self.pos == self.input_length-1 {
self.running = false
return
}
if !isLayout(self.current_char) {
self.buffer = self.buffer + string(self.input[self.pos])
} else if self.current_char == '\n' {
self.line_number += 1
}
self.pos++
self.current_char = self.input[self.pos]
self.char_number = self.char_number + 1
}
func (self *Lexer) recognizeNumberToken() {
self.consumeCharacter()
real := false
if self.current_char == '.' {
real = true
self.consumeCharacter()
for isDigit(self.current_char) {
self.consumeCharacter()
}
}
for isDigit(self.current_char) {
if self.peek(1) == '.' {
real = true
self.consumeCharacter()
for isDigit(self.current_char) {
self.consumeCharacter()
}
}
self.consumeCharacter()
}
if real {
self.createToken(REAL, self.flushBuffer())
} else {
self.createToken(DIGITS, self.flushBuffer())
}
}
func (self *Lexer) recognizeIdentifierToken() {
self.consumeCharacter()
for isLetterOrDigit(self.current_char) {
self.consumeCharacter()
}
for self.current_char == '_' && isLetterOrDigit(self.peek(1)) {
self.consumeCharacter()
for isLetterOrDigit(self.current_char) {
self.consumeCharacter()
}
}
codes := NewCodes()
_, ok := codes[self.buffer]
if ok {
self.createToken(RESERVED, self.flushBuffer())
} else {
_, exist := fun[self.buffer]
if reflect.ValueOf(self.token_stream.Back().Value).Elem().Field(1).String() == "fn" {
fun[self.buffer] = true
self.createToken(FID, self.flushBuffer())
} else if exist {
self.createToken(FID, self.flushBuffer())
} else {
self.createToken(IDENTIFIER, self.flushBuffer())
}
}
}
func (self *Lexer) recognizeSeparatorToken() {
self.consumeCharacter()
self.createToken(SEPARATOR, self.flushBuffer())
}
func (self *Lexer) recognizeStringToken() {
self.consumeCharacter() // eat "
for self.current_char != '"' {
self.consumeCharacter()
}
self.consumeCharacter() // eat "
self.createToken(STRING, self.flushBuffer())
}
func (self *Lexer) recognizeCharacterToken() {
self.consumeCharacter()
if isLetterOrDigit(self.current_char) {
self.consumeCharacter()
}
self.consumeCharacter()
self.createToken(CHARACTER, self.flushBuffer())
}
func (self *Lexer) recognizeLogicalopToken() {
first := self.current_char
self.consumeCharacter()
if first == self.current_char {
self.consumeCharacter()
} else if self.current_char == '=' {
self.consumeCharacter()
}
self.createToken(LOGICALOP, self.flushBuffer())
}
func (self *Lexer) recognizeOperatorToken() {
self.consumeCharacter()
self.createToken(OPERATOR, self.flushBuffer())
}
func (self *Lexer) getNextToken() {
self.start_pos = 0
for isLayout(self.current_char) {
self.consumeCharacter()
if !self.running {
return
}
}
self.start_pos = self.pos
if isDigit(self.current_char) {
self.recognizeNumberToken()
} else if isLetterOrDigit(self.current_char) || self.current_char == '_' {
self.recognizeIdentifierToken()
} else if self.current_char == '"' {
self.recognizeStringToken()
} else if self.current_char == '\'' {
self.recognizeCharacterToken()
} else if strings.Contains("!=|&<>", string(self.current_char)) {
self.recognizeLogicalopToken()
} else if isOperator(self.current_char) {
self.recognizeOperatorToken()
} else if isSeparator(self.current_char) {
self.recognizeSeparatorToken()
} else {
fmt.Printf("unknown token type %d, aka %c at %d\n", self.current_char, self.current_char, self.line_number)
self.running = false
return
}
}
func (self *Lexer) startLexing() {
fun = make(map[string]bool)
for self.running {
self.getNextToken()
}
self.createToken(END_OF_FILE, "<EOF>")
}