-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
756 lines (636 loc) · 12.7 KB
/
ast.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
package vm
import (
"fmt"
"math"
)
var mainCursor = 0
var maxCursor = math.MaxInt - 1
var tokens []Token
// @todo need to move this to the Stack generic
type astStack []astNode
func (a *astStack) push(value astNode) {
*a = append(*a, value)
}
func (a *astStack) pop() astNode {
ret := (*a)[len(*a)-1]
*a = (*a)[:len(*a)-1]
return ret
}
func (a *astStack) tail() astNode {
return (*a)[len(*a)-1]
}
func (a *astStack) atIndex(index int) astNode {
return (*a)[index]
}
func (a *astStack) len() int {
return len(*a)
}
// all astNodes must be of type astNode
type astNode interface {
Kind()
}
type astCapturedGroupNode astStack
func (astCapturedGroupNode) Kind() {}
type astArrayDecl struct{}
func (astArrayDecl) Kind() {}
type astVarArrayIndex struct {
index astExpression
value astVarDec
}
func (astVarArrayIndex) Kind() {}
type astFuncDecl struct {
Type int
Name string
Args astFuncDeclArgs
Body astBody
Return astExpression
}
func (astFuncDecl) Kind() {}
type astAssign struct {
Left astNode
Right astNode
IsLet bool
}
func (astAssign) Kind() {}
type astLiteral struct {
Unary *astUnary
Value astNode
}
func (astLiteral) Kind() {}
type astReturn struct {
Stmt *astExpression
}
func (astReturn) Kind() {}
type astUnary string
func (astUnary) Kind() {}
type astExpression []astNode
func (astExpression) Kind() {}
type astBody []astNode
func (astBody) Kind() {}
type astFuncCall struct {
Name string
Args astFunCallArgs
}
func (astFuncCall) Kind() {}
type astIf struct {
Cond astNode
Body astBody
ElseIfs []astElseIf
Else astElse
}
func (astIf) Kind() {}
type astElseIf struct {
Cond astNode
Body astBody
}
func (astElseIf) Kind() {}
type astElse struct {
Body astBody
}
func (astElse) Kind() {}
type astFor struct {
Pre astNode
Cond astNode
Post astNode
Body astBody
}
func (astFor) Kind() {}
type astVarDec struct {
Token
}
func (astVarDec) Kind() {}
type astFuncArgValue string
func (astFuncArgValue) Kind() {}
type astOp struct {
Token Token
}
func (astOp) Kind() {}
func (op astOp) Prec() int {
switch op.Token.Kind {
case T_OR:
return 1
case T_AND:
return 2
case T_EQ, T_LT, T_LTE, T_GT, T_GTE:
return 3
case T_ADD, T_SUB:
return 4
case T_MUL, T_QUO, T_MOD:
return 5
}
return 0
}
type astLitGroup struct {
Unary *astUnary
Value astNode
}
func (astLitGroup) Kind() {}
type astValue struct {
Value Token
}
func (astValue) Kind() {}
type astFunCallArgs []astExpression
func (astFunCallArgs) Kind() {}
type astFuncDeclArgs []astNode
func (astFuncDeclArgs) Kind() {}
// get the next token from the lexer and make progress
func ntk() string {
if mainCursor >= maxCursor {
return ""
}
ret := tokens[mainCursor]
mainCursor++
return ret.Kind
}
// peek the next token, look-ahead
func peek() string {
if mainCursor >= maxCursor {
return ""
}
ret := tokens[mainCursor]
return ret.Kind
}
// get the current token
func current() string {
if mainCursor-1 < 0 {
return ""
}
ret := tokens[mainCursor-1]
return ret.Kind
}
var errors []string
func errorExpect(expected string) {
current := tokens[mainCursor]
errors = append(errors, fmt.Sprintf(`Expected %s at %d:%d got %s`, expected, current.Line, current.Col, current.Kind))
}
func printAllErrors() {
for _, err := range errors {
fmt.Println(err)
}
}
func parseScript(tt []Token) (bool, []astNode) {
tokens = tt
mainCursor = 0
maxCursor = len(tt)
found := false
var tree []astNode
for mainCursor < maxCursor {
if node := parseFuncDecl(); node != nil {
tree = append(tree, node)
found = true
} else if node := parseAssign(); node != nil {
tree = append(tree, node)
found = true
} else {
return false, nil
}
}
return found, tree
}
func parseFuncDecl() astNode {
cursor := mainCursor
node := astFuncDecl{} // the expected `astNode` type to be returned
// as we make progress parsing, we store parse errors here
var parseErr string
if ntk() == T_FUNC_DEC { // once we have the `func` declaration keyword, we should expect a valid function decl
if peek() != T_FUNC_CALL {
parseErr = T_FUNC_CALL
goto fail
}
ntk() // read next token to make progress
node.Name = tokens[mainCursor-1].Value
if peek() != T_LPAREN {
parseErr = T_LPAREN
goto fail
}
ntk() // make progress
if n := parseFuncDeclArgs(); n != nil {
if peek() != T_RPAREN {
parseErr = T_RPAREN
goto fail
}
ntk()
if peek() != T_LBRACE {
parseErr = T_LBRACE
goto fail
}
ntk()
node.Args = n.(astFuncDeclArgs)
if nBody := parseBody(); nBody != nil {
node.Body = nBody.(astBody)
} else {
goto fail
}
if peek() != T_RBRACE {
parseErr = T_RBRACE
goto fail
}
ntk()
return node
}
}
fail:
errorExpect(parseErr)
mainCursor = cursor
return nil
}
func parseAssign() astNode {
node := parseAssignOnly()
if node != nil {
if peek() == T_SEMICOLON {
_ = ntk()
}
}
return node
}
func parseReturnStmt() astNode {
cursor := mainCursor
node := astReturn{}
if ntk() == T_RETURN {
if peek() == T_SEMICOLON {
_ = ntk()
return node
}
if stmt := parseExpression(); stmt != nil {
s := stmt.(astExpression)
node.Stmt = &s
if peek() == T_SEMICOLON {
_ = ntk()
}
}
return node
}
mainCursor = cursor
return nil
}
func parseAssignOnly() astNode {
c := mainCursor
node := astAssign{}
if peek() == T_LET {
node.IsLet = true
_ = ntk()
} else {
if t := parseVarArrayIndex(); t != nil {
if ntk() == T_ASSIGN {
node.Left = t
if t := parseExpression(); t != nil {
node.Right = t
return node
}
}
}
}
if t := parseVar(); t != nil {
if ntk() == T_ASSIGN {
node.Left = t
if t := parseArrayDecl(); t != nil {
node.Right = t
return node
}
if t := parseExpression(); t != nil {
node.Right = t
return node
}
}
}
mainCursor = c
return nil
}
func parseVar() astNode {
if peek() == T_VAR {
_ = ntk()
return astVarDec{
tokens[mainCursor-1],
}
}
return nil
}
func parseVarArrayIndex() astNode {
c := mainCursor
if peek() == T_VAR {
_ = ntk()
varDecl := astVarDec{
tokens[mainCursor-1],
}
if ntk() == T_LBRACK {
if node := parseExpression(); node != nil && ntk() == T_RBRACK {
return astVarArrayIndex{
index: node.(astExpression),
value: varDecl,
}
}
}
}
mainCursor = c
return nil
}
func parseBody() astNode {
node := astBody{}
for mainCursor < maxCursor {
//TODO this might be wrong if ";" is missing
if e := parseAssign(); e != nil {
node = append(node, e)
} else if e := parseFor(); e != nil {
node = append(node, e)
} else if e := parseIf(); e != nil {
node = append(node, e)
} else if e := parseExpression(); e != nil {
node = append(node, e)
} else if e := parseReturnStmt(); e != nil {
node = append(node, e)
} else {
break
}
}
return node
}
func parseExpression() astNode {
c := mainCursor
node := astExpression{}
for mainCursor < maxCursor {
cc := mainCursor
nLitNode := parseLit()
if (nLitNode != nil && peek() == T_SEMICOLON) || nLitNode != nil {
if nOpNode := parseOp(); nOpNode != nil {
node = append(node, nLitNode)
node = append(node, nOpNode)
continue //
}
}
// rollback
mainCursor = cc
if nLit := parseLit(); nLit != nil {
node = append(node, nLit)
return node
}
break
}
mainCursor = c
return nil
}
func isUnary(tokenType string) bool {
switch tokenType {
case T_SUB, T_NOT:
return true
}
return false
}
// cleanup this, not very clean
// this should be supported in the lexer
func parseOp() astNode {
t := tokens[mainCursor]
if t.IsOp() {
node := astOp{}
node.Token = tokens[mainCursor]
_ = ntk()
return node
}
return nil
}
func parseArrayDecl() astNode {
if peek() == T_ARRAY {
_ = ntk()
return astArrayDecl{}
}
return nil
}
// rename to basicLiteral
func parseBasicLiteral() astNode {
c := mainCursor
node := astLiteral{}
if arrayIndexNode := parseVarArrayIndex(); arrayIndexNode != nil {
return arrayIndexNode
}
tok := peek()
if tok == T_NUMBER || tok == T_STRING || tok == T_VAR {
node.Value = astValue{Value: tokens[mainCursor]}
_ = ntk()
return node
}
if gNode := parseGroup(); gNode != nil {
return gNode
}
if fcNode := parseFuncCall(); fcNode != nil {
node.Value = fcNode
return node
}
mainCursor = c
return nil
}
func parseLit() astNode {
c := mainCursor
var u *astUnary
// keep parsing Unary ops
for {
if isUnary(peek()) {
if u != nil {
u = nil
_ = ntk()
// the problem here is that next token can be a unary as well
// the funny thing is that golang does not support successive unary operators
// but javascript does support it, ex: ----+-5 is a valid expression in javascript but not in golang
// since this a dynamic language, it's better to align with javascript
} else {
t := astUnary(ntk())
u = &t
}
} else {
break
}
}
lit0 := parseBasicLiteral()
if lit0 == nil {
goto fail
}
switch lit0.(type) {
case astLiteral:
lit := lit0.(astLiteral)
if lit.Unary != nil && u != nil {
lit.Unary = nil
} else if u != nil {
lit.Unary = u
}
return lit
case astLitGroup:
lit := lit0.(astLitGroup)
if lit.Unary != nil && u != nil {
lit.Unary = nil
} else if u != nil {
lit.Unary = u
}
return lit
case astVarArrayIndex:
return lit0.(astVarArrayIndex)
default:
return nil
}
fail:
mainCursor = c
return nil
}
func parseGroup() astNode {
c := mainCursor
if ntk() == T_LPAREN {
nGroup := astLitGroup{}
if n := parseExpression(); n != nil && ntk() == T_RPAREN {
nGroup.Value = n
return nGroup
}
}
mainCursor = c
return nil
}
func parseFuncCall() astNode {
c := mainCursor
node := astFuncCall{}
tok := tokens[mainCursor]
if ntk() == T_FUNC_CALL && ntk() == T_LPAREN {
if n := parseFuncCallArgs(); n != nil && ntk() == T_RPAREN {
node.Args = n.(astFunCallArgs)
node.Name = tok.Value
return node
}
}
mainCursor = c
return nil
}
func parseFuncCallArgs() astNode {
c := mainCursor
node := astFunCallArgs{}
if peek() == T_RPAREN {
return node
}
found := false
for mainCursor < maxCursor {
if n := parseExpression(); n != nil {
node = append(node, n.(astExpression))
found = true
if peek() == T_COMMA {
_ = ntk()
continue
}
}
break
}
if !found {
mainCursor = c
return nil
}
return node
}
func parseElseIf() astNode {
c := mainCursor
node := astElseIf{}
if ntk() == T_ELSE && ntk() == T_IF && ntk() == T_LPAREN {
if cond := parseExpression(); cond != nil {
node.Cond = cond
if ntk() == T_RPAREN && ntk() == T_LBRACE {
if body := parseBody(); body != nil && ntk() == T_RBRACE {
node.Body = body.(astBody)
return node
}
}
}
}
mainCursor = c
return nil
}
func parseElse() astNode {
c := mainCursor
node := astElse{}
if ntk() == T_ELSE && ntk() == T_LBRACE {
if body := parseBody(); body != nil && ntk() == T_RBRACE {
node.Body = body.(astBody)
return node
}
}
mainCursor = c
return nil
}
func parseIf() astNode {
c := mainCursor
node := astIf{}
if ntk() == T_IF && ntk() == T_LPAREN {
if cond := parseExpression(); cond != nil {
node.Cond = cond
if ntk() == T_RPAREN && ntk() == T_LBRACE {
if body := parseBody(); body != nil && ntk() == T_RBRACE {
node.Body = body.(astBody)
// check for elseif's
for mainCursor < maxCursor {
if nElseIf := parseElseIf(); nElseIf != nil {
node.ElseIfs = append(node.ElseIfs, nElseIf.(astElseIf))
} else {
break
}
}
if nElse := parseElse(); nElse != nil {
node.Else = nElse.(astElse)
}
return node
}
}
}
}
mainCursor = c
return nil
}
func parseFor() astNode {
c := mainCursor
node := astFor{}
if ntk() == T_FOR && ntk() == T_LPAREN {
if nAssign := parseAssignOnly(); nAssign != nil && parseComa() {
node.Pre = nAssign
if nExpress := parseExpression(); nExpress != nil {
if parseComa() {
node.Cond = nExpress
if nAssign := parseAssignOnly(); nAssign != nil && ntk() == T_RPAREN && ntk() == T_LBRACE {
node.Post = nAssign
if nBody := parseBody(); nBody != nil && ntk() == T_RBRACE {
node.Body = nBody.(astBody)
return node
}
}
}
}
}
}
mainCursor = c
return nil
}
func parseComa() bool {
c := mainCursor
if peek() == T_SEMICOLON {
_ = ntk()
return true
}
mainCursor = c
return false
}
func parseFuncDeclArgs() astNode {
c := mainCursor
node := astFuncDeclArgs{}
if current() == T_LPAREN && peek() == T_RPAREN {
return node
}
for mainCursor < maxCursor {
if ntk() == T_VAR {
node = append(node, astVarDec{
tokens[mainCursor-1],
})
if peek() == T_RPAREN {
return node
}
if peek() != T_COMMA && ntk() == T_LPAREN {
return node
}
if peek() == T_COMMA {
_ = ntk()
continue
}
}
break
}
mainCursor = c
return nil
}