-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
264 lines (249 loc) · 5.2 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"bufio"
"io"
"log"
"strings"
"unicode"
)
type TokKind string
type Lexeme struct {
Kind TokKind
Text string
}
type scanner struct {
lexemes chan Lexeme
rdr *bufio.Reader
}
const (
END_OF_FILE TokKind = "EOF"
OPEN_PAREN TokKind = "("
CLOSE_PAREN TokKind = ")"
OPEN_CURLY TokKind = "{"
CLOSE_CURLY TokKind = "}"
OPEN_BRACKET TokKind = "["
CLOSE_BRACKET TokKind = "]"
COMMA_SYM TokKind = ","
DOT_SYM TokKind = "."
PLUS_SYM TokKind = "+"
MINUS_SYM TokKind = "-"
MULTIPLY_SYM TokKind = "*"
DIVIDE_SYM TokKind = "/"
COLON_SYM TokKind = ":"
SEMICOLON_SYM TokKind = ";"
EXCLAMATION TokKind = "!"
QUESTION_MARK TokKind = "?"
ASSIGN TokKind = "="
EQ_OP TokKind = "=="
NEQ_OP TokKind = "!="
GREATER_THAN TokKind = ">"
LESS_THAN TokKind = "<"
GREATER_EQ TokKind = ">="
LESS_EQ TokKind = "<="
STRING_T TokKind = "STRING"
INTEGER_T TokKind = "INT"
FLOAT_T TokKind = "FLOAT"
IDENTIFIER TokKind = "IDENT"
ERROR_T TokKind = "ERROR"
TRUE_T TokKind = "TRUE"
FALSE_T TokKind = "FALSE"
IF_T TokKind = "IF"
ELSE_T TokKind = "ELSE"
FUNCTION_T TokKind = "FN"
PRINT_T TokKind = "PRINT"
PRINTLN_T TokKind = "PRINTLN"
RETURN_T TokKind = "RETURN"
FOR_T TokKind = "FOR"
DOTDOT_SYM TokKind = ".."
SWAP_T TokKind = "SWAP"
INPUT_T TokKind = "INPUT"
LENGTH_T TokKind = "LEN"
IMPORT_T TokKind = "IMPORT"
OR_T TokKind = "OR"
AND_T TokKind = "AND"
)
var reservedWords = map[string]TokKind{
"fn": FUNCTION_T,
"print": PRINT_T,
"println": PRINTLN_T,
"return": RETURN_T,
"true": TRUE_T,
"false": FALSE_T,
"if": IF_T,
"else": ELSE_T,
"for": FOR_T,
"swap": SWAP_T,
"input": INPUT_T,
"len": LENGTH_T,
"import": IMPORT_T,
"or": OR_T,
"and": AND_T,
}
var symbolMap = map[string]TokKind{
"(": OPEN_PAREN,
")": CLOSE_PAREN,
"{": OPEN_CURLY,
"}": CLOSE_CURLY,
"[": OPEN_BRACKET,
"]": CLOSE_BRACKET,
",": COMMA_SYM,
"..": DOTDOT_SYM,
".": DOT_SYM,
"+": PLUS_SYM,
"-": MINUS_SYM,
"*": MULTIPLY_SYM,
"/": DIVIDE_SYM,
":": COLON_SYM,
"!": EXCLAMATION,
"?": QUESTION_MARK,
"=": ASSIGN,
"==": EQ_OP,
"!=": NEQ_OP,
">": GREATER_THAN,
">=": GREATER_EQ,
"<": LESS_THAN,
"<=": LESS_EQ,
}
func CreateScanner(src string) *scanner {
src = strings.NewReplacer(`\n`, "\n", `\t`, "\t", `\r`, "\r").Replace(src)
rdr := bufio.NewReader(strings.NewReader(src))
s := &scanner{
rdr: rdr,
lexemes: make(chan Lexeme, 256),
}
go s.scanTokens()
return s
}
func (s *scanner) scanTokens() {
for {
r, _, err := s.rdr.ReadRune()
if err == io.EOF {
s.sendToken(END_OF_FILE, "")
close(s.lexemes)
break
}
switch {
case r == '"':
s.scanString()
case unicode.IsDigit(r):
s.scanNumber(r)
case unicode.IsLetter(r):
s.scanIdentifier(r)
default:
s.scanSymbol(r)
}
}
}
func (s *scanner) scanMultiLineComment() {
for {
_, err := s.rdr.ReadString('*')
if err != nil {
log.Fatal(err)
}
nextRune, _, err := s.rdr.ReadRune()
if err != nil {
log.Fatal(err)
}
if nextRune == '/' {
break
}
s.rdr.UnreadRune()
}
}
func (s *scanner) sendToken(kind TokKind, txt string) {
s.lexemes <- Lexeme{Kind: kind, Text: txt}
}
func (s *scanner) scanString() {
var builder strings.Builder
for {
r, _, err := s.rdr.ReadRune()
if err == io.EOF {
break
}
if r == '"' {
current := builder.String()
if len(current) > 0 && current[len(current)-1] == '\\' {
builder.WriteRune(r)
continue
}
break
}
builder.WriteRune(r)
}
result := strings.ReplaceAll(builder.String(), `\"`, `"`)
s.sendToken(STRING_T, result)
}
func (s *scanner) scanIdentifier(initial rune) {
var builder strings.Builder
builder.WriteRune(initial)
for {
r, _, err := s.rdr.ReadRune()
if err != nil {
break
}
if unicode.IsLetter(r) || unicode.IsDigit(r) {
builder.WriteRune(r)
} else {
s.rdr.UnreadRune()
break
}
}
text := builder.String()
if kw, ok := reservedWords[text]; ok {
s.sendToken(kw, text)
} else {
s.sendToken(IDENTIFIER, text)
}
}
func (s *scanner) scanNumber(initial rune) {
tokType := INTEGER_T
var builder strings.Builder
builder.WriteRune(initial)
for {
r, _, err := s.rdr.ReadRune()
if err != nil {
break
}
if r == '.' {
nextRune, _, err := s.rdr.ReadRune()
if err == nil && nextRune == '.' {
s.sendToken(tokType, builder.String())
s.sendToken(DOTDOT_SYM, "..")
return
} else if err == nil {
s.rdr.UnreadRune()
}
tokType = FLOAT_T
builder.WriteRune(r)
} else if unicode.IsDigit(r) {
builder.WriteRune(r)
} else {
s.rdr.UnreadRune()
break
}
}
s.sendToken(tokType, builder.String())
}
func (s *scanner) scanSymbol(r rune) {
single := string(r)
if peek, err := s.rdr.Peek(1); err == nil {
double := single + string(peek)
if t, ok := symbolMap[double]; ok {
s.rdr.ReadRune()
s.sendToken(t, double)
return
}
if double == "//" {
s.rdr.ReadString('\n')
return
}
if double == "/*" {
s.rdr.ReadRune()
s.scanMultiLineComment()
return
}
}
if t, ok := symbolMap[single]; ok {
s.sendToken(t, single)
}
}