-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
740 lines (601 loc) · 12.9 KB
/
parse.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
package bcl
import (
"io"
"strconv"
)
type parseConfig struct {
outw, logw io.Writer
}
func parse(inputs <-chan string, name string, cf parseConfig) (
*Prog,
parseStats, error,
) {
linePos := newLineCalc()
p := &parser{
linePos: linePos,
lexer: newLexer(inputs, linePos.add),
prog: newProg(name, cf.outw),
identRefs: make(map[string]int, 8),
// identRefs are for reusing block types & fields and selected consts
scope: new(scopeCompiler),
log: logger{cf.logw},
}
p.prog.initForParse()
p.advance()
for !p.matchEnd() {
decl(p)
p.match(tSEMICOLON) // no check as it is optional
}
if p.hadError {
p.finishStats()
return p.prog, p.stats, errCombined{"parse"}
}
p.end()
p.finishStats()
return p.prog, p.stats, nil
}
type parser struct {
lexer *lexer
prog *Prog
linePos *lineCalc
prev, current token
hadError bool
hadLexFail bool
panicMode bool
identRefs map[string]int // map ident names to const indices
scope *scopeCompiler
stats parseStats
log logger
}
type scopeCompiler struct {
locals [localsMaxSize]local
localCount int
depth int
}
const localsMaxSize = stackSize
type local struct {
name string
depth int
}
type parseStats struct {
tokens int
localMax int
depthMax int
constants int
opsCreated int
codeBytes int
}
func (p *parser) finishStats() {
p.stats.constants = len(p.prog.constants)
p.stats.codeBytes = p.prog.count()
}
func decl(p *parser) {
if p.match(tVAR) {
varDecl(p)
} else {
stmt(p)
}
if p.panicMode && p.scope.depth == 0 {
p.sync()
}
}
func varDecl(p *parser) {
p.consume(tIDENT, "expected variable name")
if p.panicMode {
return
}
p.declVar()
if p.match(tEQ) {
expr(p)
} else {
p.emitOp(opNIL)
}
p.defVar()
}
func stmt(p *parser) {
if p.match(tPRINT) {
printStmt(p)
} else if p.match(tEVAL) {
exprStmt(p)
} else if p.match(tDEF) {
blockStmt(p)
} else if p.scope.depth > 0 {
exprStmt(p)
} else {
p.errorAtCurrent("expected statement")
}
}
func blockStmt(p *parser) {
p.consume(tIDENT, "expected block type")
if p.panicMode {
return
}
blockType := p.prev.val
var blockName string
if p.match(tSTR) {
blockName, _ = strconv.Unquote(p.prev.val)
}
p.consume(tLCURLY, "expected '{'")
p.defBlock(p.identConst(blockType), p.makeConst(blockName))
defer p.endBlock()
p.beginScope()
defer p.endScope()
for !p.check(tRCURLY) && !p.checkEnd() {
decl(p)
if p.panicMode {
p.advance()
}
p.match(tSEMICOLON) // optional
}
if p.hadLexFail {
return
}
p.consume(tRCURLY, "expected '}'")
}
func printStmt(p *parser) {
expr(p)
p.emitOp(opPRINT)
}
func exprStmt(p *parser) {
expr(p)
p.emitOp(opPOP)
}
func expr(p *parser) {
p.parsePrecedence(precAssign)
}
type parseFn func(*parser, bool)
type parseRule struct {
prefix parseFn
infix parseFn
prec precedence
}
type precedence int
const (
precNone precedence = iota
precAssign
precOr
precAnd
precNot
precEq
precCmp
precTerm
precFactor
precUnary
precCall
precPrimary
)
var rules [tMAX]parseRule
func init() {
rules = [...]parseRule{
tLPAREN: {parens, nil, precNone},
tRPAREN: {nil, nil, precNone},
tLCURLY: {nil, nil, precNone},
tRCURLY: {nil, nil, precNone},
tEQ: {nil, nil, precNone},
tMINUS: {unary, binary, precTerm},
tPLUS: {unary, binary, precTerm},
tSLASH: {nil, binary, precFactor},
tSTAR: {nil, binary, precFactor},
tOR: {nil, boolOr, precOr},
tAND: {nil, boolAnd, precAnd},
tNOT: {boolNot, nil, precNone},
tBE: {nil, binary, precEq},
tEE: {nil, binary, precEq},
tGT: {nil, binary, precCmp},
tGE: {nil, binary, precCmp},
tLT: {nil, binary, precCmp},
tLE: {nil, binary, precCmp},
tIDENT: {identRef, nil, precNone},
tSTR: {stringLit, nil, precNone},
tINT: {intLit, nil, precNone},
tFLOAT: {floatLit, nil, precNone},
tFALSE: {boolLit, nil, precNone},
tTRUE: {boolLit, nil, precNone},
tNIL: {nilLit, nil, precNone},
tVAR: {nil, nil, precNone},
tSEMICOLON: {nil, nil, precNone},
tERR: {nil, nil, precNone},
tEOF: {nil, nil, precNone},
tFAIL: {nil, nil, precNone},
}
}
func getRule(t tokenType) parseRule { return rules[t] }
func identRef(p *parser, canAssign bool) {
p.resolveIdent(p.prev.val, canAssign)
}
func parens(p *parser, _ bool) {
expr(p)
p.consume(tRPAREN, "expected ')' after expression")
}
func binary(p *parser, _ bool) {
opType := p.prev.typ
rule := getRule(opType)
p.parsePrecedence(rule.prec + 1)
switch opType {
case tEE:
p.emitOp(opEQ)
case tBE:
p.emitOps(opEQ, opNOT)
case tLT:
p.emitOp(opLT)
case tLE:
p.emitOps(opGT, opNOT)
case tGT:
p.emitOp(opGT)
case tGE:
p.emitOps(opLT, opNOT)
case tPLUS:
p.emitOp(opADD)
case tMINUS:
p.emitOp(opSUB)
case tSTAR:
p.emitOp(opMUL)
case tSLASH:
p.emitOp(opDIV)
}
}
func boolAnd(p *parser, _ bool) {
endJump := p.emitJump(opJFALSE)
p.emitOp(opPOP)
p.parsePrecedence(precAnd)
// NOTE: *not incrementing* prececence means that this op is right-assoc.
// It doesn't seem to hurt in bool algebra; the bytecode is even cleaner.
// In a chain of ANDs, first JFALSE jumps to the end of chain, making it
// faster. Left assoc version would jump to the middle of chain, only to
// test & jump again with another JFALSE.
//
// Increment if ever going back to typical left-associativity.
p.patchJump(endJump)
}
func boolOr(p *parser, _ bool) {
midJump := p.emitJump(opJFALSE)
endJump := p.emitJump(opJUMP)
p.patchJump(midJump)
p.emitOp(opPOP)
p.parsePrecedence(precOr)
// See NOTE for boolAnd and not incrementing the precedence.
// Idea is the same, it's just the JUMP that goes to the end vs to the middle.
p.patchJump(endJump)
}
func boolNot(p *parser, _ bool) {
opType := p.prev.typ
p.parsePrecedence(precNot)
switch opType {
case tNOT:
p.emitOp(opNOT)
}
}
func unary(p *parser, _ bool) {
opType := p.prev.typ
p.parsePrecedence(precUnary)
switch opType {
case tMINUS:
p.emitOp(opNEG)
case tPLUS:
p.emitOp(opUNPLUS)
}
}
func intLit(p *parser, _ bool) {
v, err := strconv.ParseInt(p.prev.val, 0, 0)
if err != nil {
panic(err)
}
switch v {
case 0:
p.emitOp(opZERO)
case 1:
p.emitOp(opONE)
default:
p.emitConst(int(v))
}
}
func floatLit(p *parser, _ bool) {
v, err := strconv.ParseFloat(p.prev.val, 64)
if err != nil {
panic(err)
}
p.emitConst(v)
}
func stringLit(p *parser, _ bool) {
s, err := strconv.Unquote(p.prev.val)
if err != nil {
panic(err)
}
p.emitConst(s)
}
func boolLit(p *parser, _ bool) {
switch p.prev.typ {
case tTRUE:
p.emitOp(opTRUE)
case tFALSE:
p.emitOp(opFALSE)
}
}
func nilLit(p *parser, _ bool) {
switch p.prev.typ {
case tNIL:
p.emitOp(opNIL)
}
}
func (p *parser) advance() {
p.prev = p.current
for {
current, ok := p.lexer.nextToken()
if !ok {
return
}
p.current = current
p.stats.tokens++
if p.current.typ == tFAIL {
p.hadLexFail = true
}
if p.current.typ != tERR {
break
}
p.errorAtCurrent(p.current.err.Error())
}
}
func (p *parser) consume(typ tokenType, errmsg string) {
if p.current.typ == typ {
p.advance()
return
}
p.errorAtCurrent(errmsg)
}
func (p *parser) match(typ tokenType) bool {
if !p.check(typ) {
return false
}
p.advance()
return true
}
func (p *parser) matchEnd() bool {
if !p.checkEnd() {
return false
}
p.advance()
return true
}
func (p *parser) check(typ tokenType) bool {
return p.current.typ == typ
}
func (p *parser) checkEnd() bool {
return p.current.typ <= tEOF
}
func (p *parser) sync() {
p.panicMode = false
for !p.checkEnd() {
switch p.current.typ {
case tVAR, tDEF, tPRINT, tEVAL: // tokens delimiting a statement
return
}
p.advance()
}
}
func (p *parser) parsePrecedence(prec precedence) {
p.advance()
prefixRule := getRule(p.prev.typ).prefix
if prefixRule == nil {
p.error("expected expression")
return
}
canAssign := prec <= precAssign
prefixRule(p, canAssign)
for prec <= getRule(p.current.typ).prec {
p.advance()
infixRule := getRule(p.prev.typ).infix
infixRule(p, canAssign)
}
if canAssign && p.match(tEQ) {
p.error("invalid assignment target")
}
}
func (p *parser) end() {
p.popN(p.scope.localCount)
p.emitOp(opRET)
p.prog.linePos = p.linePos
}
func (p *parser) beginScope() {
p.scope.depth++
p.stats.depthMax = max(p.stats.depthMax, p.scope.depth)
}
func (p *parser) endScope() {
p.scope.depth--
var popCount int
for p.scope.localCount > 0 &&
p.scope.locals[p.scope.localCount-1].depth > p.scope.depth {
popCount++
p.scope.localCount--
}
p.popN(popCount)
}
func (p *parser) popN(count int) {
switch count {
case 0:
case 1:
p.emitOp(opPOP)
default:
p.emitOp(opPOPN)
p.emitUvarint(count)
}
}
func (p *parser) declVar() {
name := p.prev.val
for i := p.scope.localCount - 1; i >= 0; i-- {
local := &p.scope.locals[i]
if local.depth != -1 && local.depth < p.scope.depth {
break
}
if name == local.name {
p.error("variable with this name already present in this scope")
}
}
p.addLocal(name)
}
func (p *parser) addLocal(name string) {
if p.scope.localCount == localsMaxSize {
p.error("too many local variables")
return
}
local := &p.scope.locals[p.scope.localCount]
local.name = name
local.depth = -1
p.scope.localCount++
p.stats.localMax = max(p.stats.localMax, p.scope.localCount)
}
func (p *parser) markInitialized() {
p.scope.locals[p.scope.localCount-1].depth = p.scope.depth
}
func (p *parser) defVar() {
p.markInitialized()
}
func (p *parser) defBlock(typeIdx, nameIdx int) {
p.emitOp(opDEFBLOCK)
p.emitUvarint(typeIdx)
p.emitUvarint(nameIdx)
}
func (p *parser) endBlock() {
p.emitOp(opENDBLOCK)
}
func (p *parser) resolveIdent(name string, canAssign bool) {
var setOp, getOp opcode
var idx int
idx = p.resolveLocal(p.scope, name)
if idx >= 0 {
setOp, getOp = opSETLOCAL, opGETLOCAL
} else {
if p.scope.depth == 0 {
p.error("undefined variable")
return
}
// when in block, there can be field used at runtime,
// or "ident not resolved" runtime error
idx = p.identConst(name)
setOp, getOp = opSETFIELD, opGETFIELD
}
if canAssign && p.match(tEQ) {
expr(p)
p.emitOp(setOp)
p.emitUvarint(idx)
} else {
p.emitOp(getOp)
p.emitUvarint(idx)
}
}
func (p *parser) resolveLocal(scope *scopeCompiler, name string) int {
for i := scope.localCount - 1; i >= 0; i-- {
local := &scope.locals[i]
if name == local.name {
if local.depth == -1 {
// `var x=x` will reach x from outer scope
continue
}
return i
}
}
return -1
}
func (p *parser) emitByte(b byte) {
p.currentProg().write(b, p.prev.pos)
}
func (p *parser) emitBytes(bb ...byte) {
prog := p.currentProg()
for _, b := range bb {
prog.write(b, p.prev.pos)
}
}
func (p *parser) emitUvarint(x int) {
// To be on the safe side, this buffer should be 9B for sqlite4 uvarint.
// Other sensible varint implementations
// (binary/encoding aka protobuf encoding, leb128, vlq) also need max 9B
// when encoding non-neg int as uint (max value is 1<<63-1).
var b [9]byte
n := uvarintToBytes(b[:], uint64(x))
p.emitBytes(b[:n]...)
}
func (p *parser) emitOp(op opcode) {
prog := p.currentProg()
prog.write(byte(op), p.prev.pos)
p.stats.opsCreated++
}
func (p *parser) emitOps(oo ...opcode) {
for _, o := range oo {
p.emitOp(o)
}
}
func (p *parser) emitConst(v value) {
idx := p.makeConst(v)
p.emitOp(opCONST)
p.emitUvarint(idx)
}
const jumpByteLength = 2
func (p *parser) emitJump(op opcode) int {
p.emitOp(op)
p.emitBytes(0xff, 0xff)
// note: can't use varuint for jumps, no way to know its size before patch
return p.currentProg().count() - jumpByteLength
}
func (p *parser) patchJump(offset int) {
prog := p.currentProg()
jump := prog.count() - offset - jumpByteLength
if jump > 1<<(8*jumpByteLength)-1 {
p.error("jump too long")
return
}
if jump < 0 {
p.error("negative jump")
return
}
u16ToBytes(prog.code[offset:], uint16(jump))
}
func (p *parser) identConst(name string) int {
idx, ok := p.identRefs[name]
if !ok {
idx = p.makeConst(name)
p.identRefs[name] = idx
}
return idx
}
func (p *parser) makeConst(v value) int {
// in case of some values like empty str, cache like identConst:
var cache bool
if v == "" {
idx, ok := p.identRefs[""]
if ok {
return idx
}
cache = true
}
idx := p.currentProg().addConst(v)
if cache {
p.identRefs[v.(string)] = idx
}
return idx
}
func (p *parser) currentProg() *Prog {
// this will be extended if there are more code objects, like functions
return p.prog
}
func (p *parser) errorAtCurrent(msg string) {
p.errorAt(&p.current, msg)
}
func (p *parser) error(msg string) {
p.errorAt(&p.prev, msg)
}
func (p *parser) errorAt(t *token, msg string) {
p.panicMode = true
p.log.Printf("line %s: error", p.linePos.format(t.pos))
switch t.typ {
case tEOF:
p.log.Print(" at end")
case tERR, tFAIL: // nop
default:
p.log.Printf(" at '%s'", t.val)
}
p.log.Printf(": %s\n", msg)
p.hadError = true
}
type errCombined struct {
source string
}
func (e errCombined) Error() string { return "combined errors from " + e.source }