-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_parser.go
More file actions
534 lines (478 loc) · 11.2 KB
/
Copy path2_parser.go
File metadata and controls
534 lines (478 loc) · 11.2 KB
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
// Parsing, or syntax analyzing is the process of converting tokens into corresponding
// Intermediate Representation(IR) using the programming language grammar.
// Top-down parser, and bottom-up parser are two main categories of parsers.
// Top-down parser is classified into two types: Recursive decent parser, and LL(1).
// Bottom-up parser is classified into two types: LR, and Operator precedence parser.
// LR parser is classified into four types: LR(0), SLR(1), LALR(1) and CLR(1).
//
// Parser
// ├── Top-down parser
// │ ├── Recursive decent parser
// │ └── LL(1)
// └── Bottom-up parser
// ├── LR
// │ ├── LR(0)
// │ ├── SLR(1)
// │ ├── LALR(1)
// │ └── CLR(1)
// └── Operator precedence parser
package main
import (
"fmt"
"log"
"strconv"
)
const (
LOWEST = iota + 1
EQUALS // == !=
BOOLOP // or and
GREATER // < > <= >=
SUM // + -
PRODUCT // * /
PREFIX // +x -x !x
)
// ************
// ** Parser **
// ************
type Parser struct {
tokens chan Token
currentToken Token
peekToken Token
errors []error
}
func NewParser(lexer *Lexer) *Parser {
return &Parser{
tokens: lexer.Lex(),
errors: make([]error, 0),
}
}
func (p *Parser) Parse() chan Statement {
p.next() // initialize peek token
p.next() // initialize current token
statements := make(chan Statement)
go func() {
for p.currentToken.Type != EOF {
if statement := p.parseStatement(); statement != nil {
statements <- statement
}
}
close(statements)
}()
return statements
}
// ****************
// ** Statements **
// ****************
type Statement interface{}
func (p *Parser) parseStatement() Statement {
switch p.currentToken.Type {
case VAR:
return p.parseVariable()
case IF:
return p.parseIf()
case WHILE:
return p.parseWhile()
case FOR:
return p.parseFor()
case FN:
return p.parseFunction()
case RETURN:
return p.parseReturn()
case LCURLY:
return p.parseBlock()
case IDENT:
if p.peekToken.Type == ASSIGN {
return p.parseVariable()
}
fallthrough
default:
return p.parseExpression(LOWEST)
}
}
type Variable struct {
Name Identifier
Value Expression
IsNew bool
}
func (p *Parser) parseVariable() Statement {
v := Variable{}
if p.currentToken.Type == VAR {
p.next() // skip var keyword
v.IsNew = true
}
v.Name = p.parseIdentifier().(Identifier)
if !p.expectCurrent(ASSIGN) {
return nil
}
p.next() // skip = symbol
v.Value = p.parseExpression(LOWEST)
return v
}
type If struct {
Condition Expression
Consequence Block
Alternative *Block
}
func (p *Parser) parseIf() Statement {
p.next() // skip if keyword
i := If{}
i.Condition = p.parseExpression(LOWEST)
i.Consequence = p.parseBlock().(Block)
if p.currentToken.Type == ELSE {
p.next() // skip else keyword
alternative := p.parseBlock().(Block)
i.Alternative = &alternative
}
return i
}
type While struct {
Condition Expression
Consequence Block
}
func (p *Parser) parseWhile() Statement {
p.next() // skip while keyword
w := While{Condition: p.parseExpression(LOWEST)}
if !p.expectCurrent(LCURLY) {
return nil
}
w.Consequence = p.parseBlock().(Block)
return w
}
type For struct {
Key Identifier
Value Identifier
Condition Expression
Consequence Block
}
func (p *Parser) parseFor() Statement {
p.next() // skip for keyword
f := For{Key: p.parseIdentifier().(Identifier)}
if p.currentToken.Type == COMMA {
p.next() // skip , symbol
f.Value = p.parseIdentifier().(Identifier)
}
if !p.expectCurrent(IN) {
return nil
}
p.next() // read in keyword
f.Condition = p.parseExpression(LOWEST)
if !p.expectCurrent(LCURLY) {
return nil
}
f.Consequence = p.parseBlock().(Block)
return f
}
type Function struct {
Name Identifier
Parameters []Identifier
Body Block
}
func (p *Parser) parseFunction() Statement {
p.next() // skip fn keyword
f := Function{Name: p.parseIdentifier().(Identifier)}
if !p.expectCurrent(LPAREN) {
return nil
}
p.next() // skip ( symbol
for p.currentToken.Type != RPAREN {
f.Parameters = append(f.Parameters, p.parseIdentifier().(Identifier))
if p.currentToken.Type == COMMA {
p.next() // skip , symbol
}
}
p.next() // skip ) symbol
if !p.expectCurrent(LCURLY) {
return nil
}
f.Body = p.parseBlock().(Block)
return f
}
type Return struct {
Value Expression
}
func (p *Parser) parseReturn() Statement {
p.next() // skip return keyword
r := Return{Value: p.parseExpression(LOWEST)}
return r
}
type Block struct {
Statements []Statement
}
func (p *Parser) parseBlock() Statement {
p.next() // skip { symbol
b := Block{}
for p.currentToken.Type != RCURLY {
b.Statements = append(b.Statements, p.parseStatement())
}
p.next() // skip } symbol
return b
}
// *****************
// ** Expressions **
// *****************
type Expression interface{}
func (p *Parser) parseExpression(precedence int) Expression {
var left Expression
switch p.currentToken.Type {
case TRUE, FALSE:
left = p.parseBoolean()
case INT:
left = p.parseInteger()
case FLOAT:
left = p.parseFloat()
case STRING:
left = p.parseString()
case PLUS, MINUS, NOT:
left = p.parseUnaryOperation()
case IDENT:
left = p.parseIdentifier()
switch p.currentToken.Type {
case LBRACKET:
left = p.parseIndex(left)
case LPAREN:
left = p.parseCall(left)
}
case LPAREN:
p.next() // skip ( symbol
left = p.parseExpression(LOWEST)
p.next() // skip ) symbol
case LBRACKET:
left = p.parseArray()
case LCURLY:
left = p.parseMap()
case LEN:
left = p.parseLen()
case PRINT, PRINTLN:
left = p.parsePrint()
default:
p.errors = append(p.errors, fmt.Errorf("unary parse function for %s not found", p.currentToken.Type))
return nil
}
for precedence < getPrecedence(p.currentToken.Type) {
switch p.currentToken.Type {
case OR, AND, PLUS, MINUS, ASTERISK, SLASH, EQ, NEQ, LT, GT, LEQ, GEQ:
left = p.parseBinaryOperation(left)
default:
p.errors = append(p.errors, fmt.Errorf("binary parse function for %s not found", p.currentToken.Type))
return nil
}
}
return left
}
type Boolean struct {
Value bool
}
func (p *Parser) parseBoolean() Expression {
if !p.expectCurrent(TRUE, FALSE) {
return nil
}
b := Boolean{}
b.Value, _ = strconv.ParseBool(p.currentToken.Value)
p.next() // skip boolean literal
return b
}
type Integer struct {
Value int64
}
func (p *Parser) parseInteger() Expression {
if !p.expectCurrent(INT) {
return nil
}
i := Integer{}
i.Value, _ = strconv.ParseInt(p.currentToken.Value, 10, 64)
p.next() // skip integer literal
return i
}
type Float struct {
Value float64
}
func (p *Parser) parseFloat() Expression {
if !p.expectCurrent(FLOAT) {
return nil
}
f := Float{}
f.Value, _ = strconv.ParseFloat(p.currentToken.Value, 64)
p.next() // skip float literal
return f
}
type String struct {
Value string
}
func (p *Parser) parseString() Expression {
if !p.expectCurrent(STRING) {
return nil
}
s := String{Value: p.currentToken.Value}
p.next() // skip string literal
return s
}
type Array struct {
Items []Expression
}
func (p *Parser) parseArray() Expression {
p.next() // skip [ symbol
a := Array{Items: make([]Expression, 0)}
for p.currentToken.Type != RBRACKET {
a.Items = append(a.Items, p.parseExpression(LOWEST))
if p.currentToken.Type == COMMA {
p.next() // skip , symbol
}
}
p.next() // skip ] symbol
return a
}
type Map struct {
Items map[Expression]Expression
}
func (p *Parser) parseMap() Expression {
p.next() // skip { symbol
m := Map{Items: make(map[Expression]Expression)}
for p.currentToken.Type != RCURLY {
key := p.parseExpression(LOWEST)
if !p.expectCurrent(COLON) {
return nil
}
p.next() // skip : symbol
m.Items[key] = p.parseExpression(LOWEST)
if p.currentToken.Type == COMMA {
p.next() // skip , symbol
}
}
p.next() // skip } symbol
return m
}
type Index struct {
Index Expression
Subject Expression
}
func (p *Parser) parseIndex(left Expression) Expression {
p.next() // skip [ symbol
i := Index{
Index: p.parseExpression(LOWEST),
Subject: left,
}
p.next() // skip ] symbol
return i
}
type Call struct {
Identifier Identifier
Arguments []Expression
}
func (p *Parser) parseCall(left Expression) Expression {
p.next() // skip ( symbol
c := Call{Identifier: left.(Identifier), Arguments: make([]Expression, 0)}
for p.currentToken.Type != RPAREN {
c.Arguments = append(c.Arguments, p.parseExpression(LOWEST))
if p.currentToken.Type == COMMA {
p.next() // skip , symbol
}
}
p.next() // skip ) symbol
return c
}
type Identifier struct {
Token Token
IsFunctionCall bool
}
func (p *Parser) parseIdentifier() Expression {
if !p.expectCurrent(IDENT) {
return nil
}
i := Identifier{Token: p.currentToken, IsFunctionCall: p.peekToken.Type == LPAREN}
p.next() // skip identifier
return i
}
type UnaryOperation struct {
Token Token
Expression Expression
}
func (p *Parser) parseUnaryOperation() Expression {
if !p.expectCurrent(PLUS, MINUS, NOT) {
return nil
}
uo := UnaryOperation{Token: p.currentToken}
p.next() // skip +, -, or !
uo.Expression = p.parseExpression(PREFIX)
return uo
}
type BinaryOperation struct {
Token Token
Left Expression
Right Expression
}
func (p *Parser) parseBinaryOperation(left Expression) Expression {
bo := BinaryOperation{Token: p.currentToken, Left: left}
precedence := getPrecedence(p.currentToken.Type)
p.next() // skip operator(+, -, ...)
bo.Right = p.parseExpression(precedence)
return bo
}
type Len struct {
Subject Expression
}
func (p *Parser) parseLen() Expression {
p.next() // skip len keyword
p.next() // skip ( symbol
l := Len{Subject: p.parseExpression(LOWEST)}
p.next() // skip ) symbol
return l
}
type Print struct {
Args []Expression
IsNewLine bool
}
func (p *Parser) parsePrint() Expression {
print := Print{IsNewLine: p.currentToken.Type == PRINTLN}
p.next() // skip print, or println keyword
if !p.expectCurrent(LPAREN) {
return nil
}
p.next() // skip ( symbol
for p.currentToken.Type != RPAREN {
print.Args = append(print.Args, p.parseExpression(LOWEST))
if p.currentToken.Type == COMMA {
p.next() // skip , symbol
}
}
p.next() // skip ) symbol
return print
}
func (p *Parser) expectCurrent(tokenTypes ...TokenType) bool {
for _, tokenType := range tokenTypes {
if p.currentToken.Type == tokenType {
return true
}
}
if len(tokenTypes) == 1 {
log.Fatalf("expected %s, got %s instead", tokenTypes[0], p.currentToken.Type)
p.errors = append(p.errors, fmt.Errorf("expected %s, got %s instead", tokenTypes[0], p.currentToken.Type))
} else {
log.Fatalf("expected one of %s, got %s instead", tokenTypes, p.currentToken.Type)
p.errors = append(p.errors, fmt.Errorf("expected one of %s, got %s instead", tokenTypes, p.currentToken.Type))
}
return false
}
func getPrecedence(in TokenType) int {
precedences := map[TokenType]int{
EQ: EQUALS,
NEQ: EQUALS,
OR: BOOLOP,
AND: BOOLOP,
LT: GREATER,
GT: GREATER,
LEQ: GREATER,
GEQ: GREATER,
PLUS: SUM,
MINUS: SUM,
ASTERISK: PRODUCT,
SLASH: PRODUCT,
}
if precedence, ok := precedences[in]; ok {
return precedence
}
return LOWEST
}
func (p *Parser) next() {
p.currentToken = p.peekToken
p.peekToken = <-p.tokens
}