-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.go
437 lines (385 loc) · 7.41 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
import (
"bytes"
"fmt"
"io"
"log"
)
type Tag int
const (
AND Tag = 256
BASIC Tag = 257
BREAK Tag = 258
DO Tag = 259
ELSE Tag = 260
EQ Tag = 261
FALSE Tag = 262
GE Tag = 263
ID Tag = 264
IF Tag = 265
INDEX Tag = 266
LE Tag = 267
MINUS Tag = 268
NE Tag = 269
NUM Tag = 270
OR Tag = 271
REAL Tag = 272
TEMP Tag = 273
TRUE Tag = 274
WHILE Tag = 275
)
func (t Tag) Tag() Tag {
return t
}
func (t Tag) String() string {
switch t {
case AND:
return "and"
case BASIC:
return "basic"
case BREAK:
return "break"
case DO:
return "do"
case ELSE:
return "else"
case EQ:
return "eq"
case FALSE:
return "false"
case GE:
return "ge"
case ID:
return "id"
case IF:
return "if"
case INDEX:
return "index"
case LE:
return "le"
case MINUS:
return "minus"
case NE:
return "ne"
case NUM:
return "num"
case OR:
return "or"
case REAL:
return "real"
case TEMP:
return "temp"
case TRUE:
return "true"
case WHILE:
return "while"
// case INT:
// return "int"
// case FLOAT:
// return "float"
// case CHAR:
// return "char"
// case BOOL:
// return "bool"
}
return string(t)
}
type Token interface {
Tag() Tag
String() string
}
type Lexer struct {
_peek byte
// line int
tokens map[string]Token
// *scanner.Scanner
r io.Reader
err error
}
var lexerLine = 1
func NewLexer(r io.Reader) *Lexer {
l := Lexer{tokens: map[string]Token{}, r: r}
l.reserve(&Word{lexeme: "if", tag: IF})
l.reserve(&Word{lexeme: "else", tag: ELSE})
l.reserve(&Word{lexeme: "while", tag: WHILE})
l.reserve(&Word{lexeme: "do", tag: DO})
l.reserve(&Word{lexeme: "break", tag: BREAK})
l.reserve(True)
l.reserve(False)
l.reserve(Int)
l.reserve(Char)
l.reserve(Bool)
l.reserve(Float)
return &l
}
func (l *Lexer) reserve(n Token) {
l.tokens[n.String()] = n
}
func (l *Lexer) read() byte {
if l.err != nil {
return 0
}
if l._peek != 0 {
b := l._peek
l._peek = 0
return b
}
buf := make([]byte, 1)
_, l.err = l.r.Read(buf)
return buf[0]
}
func (l *Lexer) readch(c byte) bool {
l.read()
if l.peek() != c {
return false
}
l._peek = ' '
return true
}
func (l *Lexer) peek() byte {
if l._peek != 0 {
return l._peek
} else if l.err != nil {
return 0
}
l._peek = l.read()
return l._peek
}
func (l *Lexer) Scan() (Token, error) {
read:
for ; ; l.read() {
if option.lr {
log.Printf("%q\n", string(l.peek()))
}
switch l.peek() {
case ' ', '\t':
case '\n':
lexerLine++
case 0:
if l.err != nil {
return nil, l.getErr()
}
default:
break read
}
}
// log.Println(string(l.peek()))
switch l.peek() {
case '&':
if l.readch('&') {
return And, l.getErr()
} else {
return Tag('&'), l.getErr()
// return NewWord("&", '&'), l.getErr()
}
case '|':
if l.readch('|') {
return Or, l.getErr()
} else {
return Tag('|'), l.getErr()
// return NewWord("|", '|'), l.getErr()
}
case '=':
if l.readch('=') {
return Eq, l.getErr()
} else {
return Tag('='), l.getErr()
// return NewWord("=", '='), l.getErr()
}
case '!':
if l.readch('=') {
return Ne, l.getErr()
} else {
return Tag('!'), l.getErr()
// return NewWord("!", '!'), l.getErr()
}
case '<':
if l.readch('=') {
return Le, l.getErr()
} else {
return Tag('<'), l.getErr()
// return NewWord("<", '<'), l.getErr()
}
case '>':
if l.readch('=') {
return Ge, l.getErr()
} else {
return Tag('>'), l.getErr()
// return NewWord(">", '>'), l.getErr()
}
}
if isDigit(l.peek()) {
v := 0
for {
v = 10*v + int(l.peek()-'0')
l.read()
if !isDigit(l.peek()) {
break
}
}
if l.peek() != '.' {
return NewNum(v), nil
}
x := float64(v)
d := float64(10)
for {
l.read()
if !isDigit(l.peek()) {
break
}
x = x + float64(l.peek()-'0')/d
d *= 10
}
return NewReal(x), nil
}
if isLetter(l.peek()) {
var b bytes.Buffer
for {
b.WriteByte(l.peek())
l.read()
if !isLetter(l.peek()) && !isDigit(l.peek()) {
break
}
}
s := b.String()
w, ok := l.tokens[s]
if !ok {
w = NewWord(s, ID)
l.tokens[s] = w
}
return w, nil
}
tok := Tag(l.peek())
// log.Printf("--> %+v\n", string(l.peek()))
l._peek = ' '
return tok, nil
}
func isDigit(c byte) bool {
return '0' <= c && c <= '9'
}
func isLetter(c byte) bool {
return c == '_' || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')
}
func (l *Lexer) getErr() error {
if l.err == nil {
return nil
} else if l.err == io.EOF {
return l.err
}
// return fmt.Errorf("line %d: %s", l.line, l.err)
return fmt.Errorf("line %d: %s", lexerLine, l.err)
}
type Word struct {
lexeme string
tag Tag
}
func NewWord(lexeme string, tag Tag) Word {
return Word{lexeme: lexeme, tag: tag}
}
func (w Word) Tag() Tag { return w.tag }
func (w Word) String() string {
// return fmt.Sprintf("%s:%s", w.tag, w.lexeme)
return w.lexeme
}
// type Token struct {
// tag Tag
// }
// func (t Token) String() string {
// return t.tag.String()
// }
var (
And = Word{lexeme: "&&", tag: AND}
Or = Word{lexeme: "||", tag: OR}
Eq = Word{lexeme: "==", tag: EQ}
Ne = Word{lexeme: "!=", tag: NE}
Le = Word{lexeme: "<=", tag: LE}
Ge = Word{lexeme: ">=", tag: GE}
Minus = Word{lexeme: "minus", tag: MINUS}
True = Word{lexeme: "true", tag: TRUE}
False = Word{lexeme: "false", tag: FALSE}
temp = Word{lexeme: "t", tag: TEMP}
)
type Typer interface {
Lexeme() string
Width() int
}
type Type struct {
lexeme string
tag Tag
width int
}
func (t Type) Lexeme() string { return t.lexeme }
func (t Type) String() string { return t.lexeme }
func (t Type) Tag() Tag { return t.tag }
func (t Type) Width() int { return t.width }
var (
Int = Type{lexeme: "int", tag: BASIC, width: 4}
Float = Type{lexeme: "float", tag: BASIC, width: 8}
Char = Type{lexeme: "char", tag: BASIC, width: 1}
Bool = Type{lexeme: "bool", tag: BASIC, width: 1}
)
func IsNumbericType(t Typer) bool {
if t == nil {
return false
}
return t.Lexeme() == "int" || t.Lexeme() == "float" || t.Lexeme() == "char"
}
func MaxType(t1, t2 Typer) Typer {
if !IsNumbericType(t1) || !IsNumbericType(t2) {
return nil
}
if t1.Lexeme() == "float" || t2.Lexeme() == "float" {
return Float
}
if t1.Lexeme() == "int" || t2.Lexeme() == "int" {
return Int
}
return Char
}
type Array struct {
size int
elem Typer
tag Tag
lexeme string
width int
}
func NewArray(sz int, p Typer) Array {
return Array{tag: INDEX, lexeme: "[]", size: sz, elem: p, width: sz * p.Width()}
}
func (a Array) Tag() Tag { return a.tag }
func (a Array) Lexeme() string { return a.lexeme }
func (a Array) Width() int { return a.width }
func (a Array) String() string {
return fmt.Sprintf("[%d]%s", a.size, a.elem)
}
type Env struct {
prev *Env
table map[Token]Id
}
func NewEnv(prev *Env) *Env { return &Env{table: map[Token]Id{}, prev: prev} }
func (e *Env) put(k Token, v Id) { e.table[k] = v }
func (e *Env) get(k Token) (Id, bool) {
for env := e; env != nil; env = env.prev {
if id, ok := env.table[k]; ok {
return id, true
}
}
return Id{}, false
}
type Num struct {
value int
// tag Tag
}
func NewNum(v int) Num {
return Num{value: v}
}
func (n Num) Tag() Tag { return NUM }
func (n Num) String() string { return fmt.Sprint(n.value) }
type Real struct {
value float64
}
func NewReal(v float64) Real {
return Real{value: v}
}
func (r Real) Tag() Tag { return REAL }
func (r Real) String() string { return fmt.Sprint(r.value) }