-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlang.bnf
More file actions
755 lines (634 loc) · 26.8 KB
/
lang.bnf
File metadata and controls
755 lines (634 loc) · 26.8 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
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
// === [ Lexical part] =========================================================
terminator : ';' '\n' | ';' | '\n' ;
// --- [ Keywords ] ------------------------------------------------------------
kwdBreak : 'b' 'r' 'e' 'a' 'k' ;
kwdCase : 'c' 'a' 's' 'e' ;
kwdConst : 'c' 'o' 'n' 's' 't' ;
kwdContinue : 'c' 'o' 'n' 't' 'i' 'n' 'u' 'e' ;
kwdDefault : 'd' 'e' 'f' 'a' 'u' 'l' 't' ;
kwdElse : 'e' 'l' 's' 'e' ;
kwdFunc : 'f' 'u' 'n' 'c' ;
kwdFor : 'f' 'o' 'r' ;
kwdGoto : 'g' 'o' 't' 'o' ;
kwdIf : 'i' 'f' ;
kwdImport : 'i' 'm' 'p' 'o' 'r' 't' ;
kwdPackage : 'p' 'a' 'c' 'k' 'a' 'g' 'e' ;
kwdRange : 'r' 'a' 'n' 'g' 'e' ;
kwdRet : 'r' 'e' 't' 'u' 'r' 'n' ;
kwdStruct : 's' 't' 'r' 'u' 'c' 't' ;
kwdSwitch : 's' 'w' 'i' 't' 'c' 'h' ;
kwdType : 't' 'y' 'p' 'e' ;
kwdVar : 'v' 'a' 'r' ;
// --- [ Types ] ---------------------------------------------------------------
type
: 'b' 'o' 'o' 'l'
| 'i' 'n' 't'
| 'f' 'l' 'o' 'a' 't' '3' '2'
| 'f' 'l' 'o' 'a' 't' '6' '4'
| 'b' 'y' 't' 'e'
| 's' 't' 'r' 'i' 'n' 'g'
;
// --- [ Predeclared constants ] -----------------------------------------------
boolLit : 't' 'r' 'u' 'e' | 'f' 'a' 'l' 's' 'e' ;
// --- [ Operators ] -----------------------------------------------------------
_relOp : '=' '=' | '!' '=' | '<' | '<' '=' | '>' | '>' '=' ;
_addOp : '+' | '-' | '|' | '^' ;
_mulOp : '*' | '/' | '%' | '<' '<' | '>' '>' | '&' | '&' '^' ;
assignOp : [ _addOp | _mulOp ] '=' ;
shortAssign : ':' '=' ;
// --- [ Whitespaces (suppressed) ] --------------------------------------------
!whitespace : ' ' | '\t' | '\r' ;
// --- [ Comments (suppressed) ] -----------------------------------------------
!comment : _lineComment | _blockComment ;
_lineComment : '/' '/' { . } '\n' ;
_blockComment : '/' '*' { . | '*' } '*' '/' ;
// --- [ Letters and digits ] --------------------------------------------------
_letter : _unicodeLetter | '_' ;
_unicodeLetter : 'a' - 'z' | 'A' - 'Z' ;
_decimalDigit : '0' - '9' ;
_octalDigit : '0' - '7' ;
_hexDigit : _decimalDigit | 'a' - 'f' | 'A' - 'F' ;
// --- [ Identifiers ] ---------------------------------------------------------
// TODO: Check this
// blankIdent : '_' ;
identifier : _letter { _letter | _decimalDigit } ;
// --- [ Integer literals ] ----------------------------------------------------
intLit : _decimalLit | _octalLit | _hexLit ;
_decimalLit : '1' - '9' { _decimalDigit } ;
_octalLit : '0' { _octalDigit } ;
_hexLit : '0' ( 'x' | 'X' ) _hexDigit { _hexDigit } ;
// --- [ Floating-point literals ] ---------------------------------------------
floatLit : _decimals '.' [ _decimals ] [ _exponent ]
| _decimals _exponent
| '.' _decimals [ _exponent ]
;
_decimals : _decimalDigit { _decimalDigit } ;
_exponent : ( 'e' | 'E' ) [ '+' | '-' ] _decimals ;
// --- [ String literals ] -----------------------------------------------------
stringLit : _rawStrLit | _interpretedStrLit ;
_rawStrLit : '`' { . } '`' ;
_interpretedStrLit : '"' { . | '\\' '"' | _escapeChar } '"' ;
// --- [ Single-character escapes ] --------------------------------------------
_escapeChar : '\\' 'n' | '\\' 'r' | '\\' 't' ;
// --- [ Rune literals ] -------------------------------------------------------
runeLit : '\'' [ '\\' ] [ . ] '\'' ;
// === [ Syntax part] ==========================================================
<<
import (
"fmt"
"github.com/shivansh/gogo/src/ast"
"github.com/shivansh/gogo/src/tac"
"github.com/shivansh/gogo/goccgen/token"
)
>>
// NOTE
// - https://github.com/goccmack/gocc/issues/59
// - Before each production rule, the corresponding rules from the go
// language specification are written in comments.
Start
: SourceFile << ast.PrintIR($0.(*ast.Node)) >>
;
SourceFile
: RepeatTerminator PackageClause terminator RepeatTerminator RepeatTopLevelDecl << $4, nil >>
;
// TODO: Package parsing logic will be complete when support for imports has
// been integrated.
PackageClause
: kwdPackage PackageName << ast.NewPkgDecl($1.(*token.Token).Lit) >>
;
PackageName
: identifier
;
// --- [ Top level declarations ] ----------------------------------------------
// TODO: MethodDecl is not supported at the moment.
// TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
RepeatTopLevelDecl
: TopLevelDecl RepeatTopLevelDecl << ast.NewTopLevelDecl($0.(*ast.Node), $1.(*ast.Node)) >>
| empty << ast.InitNode("", []string{}) >>
;
TopLevelDecl
: Declaration RepeatTerminator << $0, nil >>
| FunctionDecl RepeatTerminator << ast.InitNode("", $0.(*ast.FuncType).Code) >>
;
// Declaration = ConstDecl | TypeDecl | VarDecl .
Declaration
: ConstDecl
| TypeDecl
| VarDecl
;
// --- [ Variable declarations ] -----------------------------------------------
// VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
// VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
VarDecl
: kwdVar VarSpec << $1, nil >>
;
VarSpec
: IdentifierList Type << ast.NewVarSpec(0, $0.(*ast.Node), $1.(*ast.Node)) >>
| IdentifierList Type "=" ExpressionList << ast.NewVarSpec(1, $0.(*ast.Node), $1.(*ast.Node), $3.(*ast.Node)) >>
| IdentifierList "=" ExpressionList << ast.NewVarSpec(2, $0.(*ast.Node), $2.(*ast.Node)) >>
| empty << ast.InitNode("", []string{}) >>
;
// --- [ Type declarations ] ---------------------------------------------------
// TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
// TypeSpec = AliasDecl | TypeDef .
// AliasDecl = identifier "=" Type
// TypeDef = identifier Type
TypeDecl
: kwdType TypeSpec << ast.NewTypeDecl($1.(ast.AstNode)) >>
;
TypeSpec
: TypeDef
;
TypeDef
: identifier Type << ast.NewTypeDef(string($0.(*token.Token).Lit), $1.(ast.AstNode)) >>
;
// --- [ Constant declarations ] -----------------------------------------------
// ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
// ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
// IdentifierList = identifier { "," identifier } .
// ExpressionList = Expression { "," Expression } .
ConstDecl
: kwdConst ConstSpec << $1, nil >>
;
ConstSpec
: IdentifierList << ast.NewConstSpec(0, $0.(*ast.Node)) >>
| IdentifierList "=" ExpressionList << ast.NewConstSpec(1, $0.(*ast.Node), $2.(*ast.Node)) >>
;
// --- [ Expressions ] ---------------------------------------------------------
ExpressionList
: Expression
| Expression "," ExpressionList << ast.AppendExpr($0.(*ast.Node), $2.(*ast.Node)) >>
;
// Expression = UnaryExpr | Expression binary_op Expression .
// NOTE: The original BNF corresponding to Expression is modified to take into
// account operator precedence. The operators used are in the order of
// increasing precedence starting from top.
Expression
: Expression "||" Term1 << ast.NewBoolExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term1
;
Term1
: Term1 "&&" Term2 << ast.NewBoolExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term2
;
Term2
: Term2 RelOp Term3 << ast.NewRelExpr($1.(*ast.Node), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term3
;
Term3
: Term3 "+" Term4 << ast.NewArithExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term4
;
Term4
: Term4 "-" Term5 << ast.NewArithExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term5
;
Term5
: Term5 "*" Term6 << ast.NewArithExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term6
;
Term6
: Term6 "/" Term7 << ast.NewArithExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term7
;
Term7
: Term7 "%" Term8 << ast.NewArithExpr(string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| Term8
;
Term8
: "(" Expression ")" << $1, nil >>
| UnaryExpr
;
// TODO: Add support for booleans in IR.
BinaryOp
: RelOp << ast.InitNode($0.(*ast.Node).Place, []string{}) >>
| "||" << ast.InitNode("or", []string{}) >>
| "&&" << ast.InitNode("and", []string{}) >>
;
// unaryOp will be used for defining UnaryExpr in parser.
// unaryOp : '+' | '-' | '!' | '^' | '*' | '&' | '<' '-' ;
// binaryOp : '|' '|' | '&' '&' | _relOp | _addOp | _mulOp ;
RelOp
: "==" << ast.InitNode("==", []string{}) >>
| "!=" << ast.InitNode("!=", []string{}) >>
| "<=" << ast.InitNode("<=", []string{}) >>
| "<" << ast.InitNode("<", []string{}) >>
| ">=" << ast.InitNode(">=", []string{}) >>
| ">" << ast.InitNode(">", []string{}) >>
;
// UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
UnaryExpr
: PrimaryExpr
| UnaryOp UnaryExpr << ast.NewUnaryExpr($0.(*ast.Node), $1.(*ast.Node)) >>
;
UnaryOp
: "+" << ast.InitNode("+", []string{}) >>
| "-" << ast.InitNode("-", []string{}) >>
| "!" << ast.InitNode("!", []string{}) >>
| "*" << ast.InitNode("*", []string{}) >>
| "&" << ast.InitNode("&", []string{}) >>
;
// PrimaryExpr =
// Operand |
// Conversion |
// MethodExpr |
// PrimaryExpr Selector |
// PrimaryExpr Index |
// PrimaryExpr Slice |
// PrimaryExpr TypeAssertion |
// PrimaryExpr Arguments .
PrimaryExpr
: Operand
| PrimaryExpr Selector << ast.NewPrimaryExprSel($0.(*ast.Node), $1.(*ast.Node)) >>
| PrimaryExpr Index << ast.NewPrimaryExprIndex($0.(*ast.Node), $1.(*ast.Node)) >>
| PrimaryExpr Arguments << ast.NewPrimaryExprArgs($0.(*ast.Node), $1.(*ast.Node)) >>
;
// Operand = Literal | OperandName | "(" Expression ")" .
// Literal = BasicLit | CompositeLit | FunctionLit .
// BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
// OperandName = identifier | QualifiedIdent.
Operand
: Literal
| OperandName
;
Literal
: BasicLit
| CompositeLit
;
BasicLit
: intLit << ast.InitNode(string($0.(*token.Token).Lit), []string{}) >>
| stringLit << ast.InitNode(fmt.Sprintf("string:%s", $0.(*token.Token).Lit), []string{}) >>
;
// CompositeLit = LiteralType LiteralValue .
// LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
// SliceType | MapType | TypeName .
// LiteralValue = "{" [ ElementList [ "," ] ] "}" .
// ElementList = KeyedElement { "," KeyedElement } .
// KeyedElement = [ Key ":" ] Element .
// Key = FieldName | Expression | LiteralValue .
// FieldName = identifier .
// Element = Expression | LiteralValue .
CompositeLit
: LiteralType LiteralValue << ast.NewCompositeLit($0.(*ast.Node), $1.(*ast.Node)) >>
;
LiteralType
: ArrayType
// When initializing structs, TypeName will be used as the name of struct.
| TypeName
;
// TypeName = identifier | QualifiedIdent .
// QualifiedIdent = PackageName "." identifier .
TypeName
: identifier << ast.InitNode(string($0.(*token.Token).Lit), []string{}) >>
;
LiteralValue
: "{" RepeatTerminator "}" << ast.InitNode("", []string{}) >>
// NOTE: We don't support adding a ',' after the last KeyedElement
// as opposed to the official go language.
// TODO: Support for struct initialization.
| "{" RepeatTerminator ElementList "}" << $2, nil >>
;
ElementList
: KeyedElement RepeatKeyedElement << ast.NewElementList($0.(*ast.Node), $1.(*ast.Node)) >>
;
RepeatKeyedElement
: "," RepeatTerminator KeyedElement RepeatKeyedElement
<< ast.AppendKeyedElement($2.(*ast.Node), $3.(*ast.Node)) >>
| RepeatTerminator << ast.InitNode("", []string{}) >>
;
KeyedElement
: Element
;
Element
: Expression
;
OperandName
: identifier << ast.NewIdentifier(string($0.(*token.Token).Lit)) >>
;
// Selector = "." identifier .
// Index = "[" Expression "]" .
// Slice = "[" [ Expression ] ":" [ Expression ] "]" |
// "[" [ Expression ] ":" Expression ":" Expression "]" .
// TypeAssertion = "." "(" Type ")" .
// Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
// Conversion = Type "(" Expression [ "," ] ")"
Selector
: "." identifier << ast.InitNode(string($1.(*token.Token).Lit), []string{}) >>
;
Index
: "[" Expression "]" << $1, nil >>
;
// Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
// NOTE: Since compilation of local variables has not yet been done, only empty
// arguments are supported for the time being.
Arguments
: "(" ")" << ast.InitNode("", []string{}) >>
| "(" ExpressionList ")" << $1, nil >>
;
// FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .
// FunctionName = identifier .
// FunctionBody = Block .
// NOTE: To support recursion, we need to add function's symbol table entry before
// the production rule for block of its body is reached. This is to avoid errors
// of the form "funcion not declared". A marker is introduced for this purpose.
FunctionDecl
: FunctionMarker FunctionBody << ast.NewFuncDecl($0.(*ast.Node), $1.(*ast.Node)) >>
;
FunctionMarker
: kwdFunc FunctionName Signature << ast.NewFuncMarker($1.(*ast.Node), $2.(*ast.Node)) >>
;
// Signature = Parameters [ Result ] .
// Result = Parameters | Type .
// Parameters = "(" [ ParameterList [ "," ] ] ")" .
// ParameterList = ParameterDecl { "," ParameterDecl } .
// ParameterDecl = [ IdentifierList ] [ "..." ] Type .
Signature
: Parameters << ast.NewSignature(0, $0.(*ast.Node)) >>
| Parameters Result << ast.NewSignature(1, $0.(*ast.Node), $1.(*ast.Node)) >>
;
Result
: Parameters << ast.NewResult($0.(*ast.Node)) >>
| Type << ast.InitNode("1", []string{}) >>
// FIXME: The place value "1" above is required to make the testcase
// `test/codegen/recursion.go` pass. It is basically a hardcoded return
// value to make this specific instance of recursion work. This will be
// fixed when recursion is implemented correctly.
;
// TODO - ignore terminator
// - parameters cannot end in ','
Parameters
: "(" RepeatTerminator ")" << ast.InitNode("", []string{}) >>
| "(" ParameterList ")" << $1, nil >>
;
ParameterList
: ParameterDecl RepeatParameterDecl << ast.NewParamList($0.(*ast.Node), $1.(*ast.Node)) >>
;
RepeatParameterDecl
: "," ParameterDecl RepeatParameterDecl << ast.AppendParam($1.(*ast.Node), $2.(*ast.Node)) >>
| empty << ast.InitNode("", []string{}) >>
;
// TODO: It is currently assumed that the return types will always be int. Make
// this generic across all types.
ParameterDecl
: IdentifierList Type << $0, nil >>
| Type << ast.InitNode("", []string{$0.(*ast.Node).Place}) >>
// The final generate node of parameters (in NewParamList) contains
// the type info in the Code attribute, hence the type here too is placed
// in the code attribute.
;
// Type = TypeName | TypeLit | "(" Type ")" .
// TypeName = identifier | QualifiedIdent .
// TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
// SliceType | MapType | ChannelType .
// TODO: Make type to be more generic to handle different type of objects.
Type
: type << ast.InitNode(string($0.(*token.Token).Lit), []string{}) >>
| TypeLit
;
TypeLit
: StructType
;
// ArrayType = "[" ArrayLength "]" ElementType .
// ArrayLength = Expression .
// ElementType = Type .
// TODO: ElementType is taken to be only int for now.
ArrayType
: "[" ArrayLength "]" ElementType
<< ast.NewArrayType($1.(*ast.Node).Place, string($3.(*ast.Node).Place)) >>
;
// NOTE: ArrayLength is modified to be only an integer, unlike in Go where it is
// an expression.
ArrayLength
: intLit << ast.InitNode(string($0.(*token.Token).Lit), []string{}) >>
;
ElementType
: Type
;
// StructType = kwdStruct "{" { FieldDecl ";" } "}" .
// FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] .
// EmbeddedField = [ "*" ] TypeName .
// Tag = string_lit .
StructType
: kwdStruct RepeatTerminator "{" RepeatTerminator RepeatFieldDecl "}"
<< ast.NewStruct($4.(*ast.Node)) >>
;
RepeatFieldDecl
: FieldDecl terminator RepeatTerminator RepeatFieldDecl
<< ast.AppendFieldDecl($0.(*ast.Node), $3.(*ast.Node)) >>
| FieldDecl RepeatTerminator << $0, nil >>
;
FieldDecl
: IdentifierList Type << ast.NewFieldDecl($0.(*ast.Node), $1.(*ast.Node)) >>
| empty << ast.InitNode("", []string{}) >>
;
// IdentifierList = identifier { "," identifier } .
IdentifierList
: identifier << ast.InitNode("", []string{string($0.(*token.Token).Lit)}) >>
| identifier "," IdentifierList << ast.AppendIdent(string($0.(*token.Token).Lit), $2.(*ast.Node)) >>
;
// The symbol table entry for a function is of the form -
// functionName : ["func", (rest of the values are yet to be decided)]
FunctionName
: identifier << ast.InitNode(string($0.(*token.Token).Lit), []string{}) >>
;
FunctionBody
: Block
;
RepeatTerminator
: terminator RepeatTerminator
| empty
;
// --- [ Statements ] ----------------------------------------------------------
StatementList
: Statement terminator RepeatTerminator StatementList
<< ast.NewStmtList($0.(*ast.Node), $3.(*ast.Node)) >>
| Statement RepeatTerminator << $0, nil >>
;
// Statement = Declaration | LabeledStmt | SimpleStmt |
// GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
// FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
// DeferStmt .
// NOTE: The following statements have been additionally introduced in the BNF -
// * PrintIntStmt
// * ScanStmt
Statement
: Declaration
| LabeledStmt << ast.InitNode("", $0.(*ast.LabeledStmt).Code) >>
| SimpleStmt
| ReturnStmt << ast.InitNode("", $0.(*ast.ReturnStmt).Code) >>
| BreakStmt
| ContinueStmt
| GotoStmt
| Block
| IfStmt
| SwitchStmt << ast.InitNode("", $0.(*ast.SwitchStmt).Code) >>
| ForStmt << ast.InitNode("", $0.(*ast.ForStmt).Code) >>
| DeferStmt << ast.InitNode("", $0.(*ast.DeferStmt).Code) >>
| PrintStmt
| ScanStmt
;
// LabeledStmt = Label ":" Statement .
// Label = identifier .
LabeledStmt
: Label ":" RepeatTerminator Statement << ast.NewLabelStmt($0.(*ast.Node), $3.(*ast.Node)) >>
;
// SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
// EmptyStmt = .
// ExpressionStmt = Expression .
// IncDecStmt = Expression ( "++" | "--" ) .
// Assignment = ExpressionList assign_op ExpressionList .
// assign_op = [ add_op | mul_op ] "=" .
// ShortVarDecl = IdentifierList ":=" ExpressionList .
SimpleStmt
: EmptyStmt
| Assignment
| ShortVarDecl
| IncDecStmt
| ExpressionStmt
;
EmptyStmt
: empty << ast.InitNode("", []string{}) >>
;
ReturnStmt
: kwdRet << ast.NewReturnStmt() >>
| kwdRet ExpressionList << ast.NewReturnStmt($1.(*ast.Node)) >>
;
// BreakStmt = "break" [ Label ] .
BreakStmt
: kwdBreak << ast.InitNode("", []string{"break"}) >>
| kwdBreak Label << ast.InitNode("", []string{fmt.Sprintf("%s, %s", tac.JMP, $1.(*ast.Node).Place)}) >>
;
// ContinueStmt = "continue" [ Label ] .
ContinueStmt
: kwdContinue << ast.InitNode("", []string{"continue"}) >>
// TODO: `continue label` vs `break label` ??
// | kwdContinue Label
;
// GotoStmt = "goto" Label .
GotoStmt
: kwdGoto Label << ast.InitNode("", []string{fmt.Sprintf("%s, %s", tac.JMP, $1.(*ast.Node).Place)}) >>
;
Block
: "{" Marker RepeatTerminator StatementList "}" << ast.NewBlock($3.(*ast.Node)) >>
;
// NOTE: Marker demarcates the beginning of a block and the corresponding symbol
// table is instantiated here.
Marker
: empty << ast.NewBlockMarker() >>
;
// IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
IfStmt
: kwdIf Expression Block << ast.NewIfStmt(0, $1.(*ast.Node), $2.(*ast.Node)) >>
| kwdIf Expression Block kwdElse Block << ast.NewIfStmt(1, $1.(*ast.Node), $2.(*ast.Node), $4.(*ast.Node)) >>
| kwdIf Expression Block kwdElse IfStmt << ast.NewIfStmt(2, $1.(*ast.Node), $2.(*ast.Node), $4.(*ast.Node)) >>
| kwdIf SimpleStmt terminator Expression Block << ast.NewIfStmt(3, $1.(*ast.Node), $3.(*ast.Node), $4.(*ast.Node)) >>
| kwdIf SimpleStmt terminator Expression Block kwdElse IfStmt
<< ast.NewIfStmt(4, $1.(*ast.Node), $3.(*ast.Node), $4.(*ast.Node), $6.(*ast.Node)) >>
| kwdIf SimpleStmt terminator Expression Block kwdElse Block
<< ast.NewIfStmt(5, $1.(*ast.Node), $3.(*ast.Node), $4.(*ast.Node), $6.(*ast.Node)) >>
;
// SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
SwitchStmt
: ExprSwitchStmt
;
// ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
// ExprCaseClause = ExprSwitchCase ":" StatementList .
// ExprSwitchCase = "case" ExpressionList | "default" .
ExprSwitchStmt
: kwdSwitch Expression "{" RepeatTerminator RepeatExprCaseClause "}"
<< ast.NewSwitchStmt($1.(*ast.Node), $4.(*ast.Node)) >>
;
RepeatExprCaseClause
: ExprCaseClause RepeatExprCaseClause << ast.AppendExprCaseClause($0.(*ast.Node), $1.(*ast.Node)) >>
| empty << ast.InitNode("", []string{}) >>
;
// ExprCaseClause = ExprSwitchCase ":" StatementList .
// ExprSwitchCase = "case" ExpressionList | "default" .
ExprCaseClause
: ExprSwitchCase ":" RepeatTerminator StatementList
<< ast.NewExprCaseClause($0.(*ast.Node), $3.(*ast.Node)) >>
;
// NOTE: The grammar is modified to support only a single expression in a
// switch statement.
// ExprSwitchCase : kwdCase ExpressionList
ExprSwitchCase
: kwdCase Expression << $1, nil >>
| kwdDefault << ast.InitNode("default", []string{}) >>
;
// ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
// Condition = Expression .
ForStmt
: kwdFor Block << ast.NewForStmt(0, $1.(*ast.Node)) >>
| kwdFor Condition Block << ast.NewForStmt(1, $1.(*ast.Node), $2.(*ast.Node)) >>
| kwdFor ForClause Block << ast.NewForStmt(2, $1.(*ast.Node), $2.(*ast.Node)) >>
;
// ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
// InitStmt = SimpleStmt .
// PostStmt = SimpleStmt .
// NOTE: The place value of ForClause contains the place value of the condition
// statement as that will be required in ForStmt to determine when to break. The
// place values of InitStmt and PostStmt will not be required.
// The code value of ForClause contains newline separated codes of InitStmt,
// Condition and PostStmt.
// When Condition is not specified, do an endless loop by initializing the place
// value to 1.
ForClause
: terminator terminator << ast.InitNode("", []string{}) >>
| InitStmt terminator terminator << ast.NewForClause(0, $0.(*ast.Node)) >>
| terminator Condition terminator << ast.NewForClause(1, $1.(*ast.Node)) >>
| terminator terminator PostStmt << ast.NewForClause(2, $2.(*ast.Node)) >>
| InitStmt terminator Condition terminator << ast.NewForClause(3, $0.(*ast.Node), $2.(*ast.Node)) >>
| InitStmt terminator terminator PostStmt << ast.NewForClause(4, $0.(*ast.Node), $3.(*ast.Node)) >>
| terminator Condition terminator PostStmt << ast.NewForClause(5, $1.(*ast.Node), $3.(*ast.Node)) >>
| InitStmt terminator Condition terminator PostStmt
<< ast.NewForClause(6, $0.(*ast.Node), $2.(*ast.Node), $4.(*ast.Node)) >>
;
InitStmt
: SimpleStmt
;
PostStmt
: SimpleStmt
;
// FIXME: Condition can only be a binary expression.
Condition
: Expression
;
// DeferStmt = "defer" Expression .
// NOTE: Currently defer only handles function calls and not method calls.
DeferStmt
: "defer" PrimaryExpr Arguments << ast.NewDeferStmt($1.(*ast.Node), $2.(*ast.Node)) >>
;
PrintStmt
: PrintIntStmt
| PrintStrStmt
;
ScanStmt
: ScanIntStmt
;
PrintIntStmt
: "printInt" Expression << ast.NewIOStmt(string($0.(*token.Token).Lit), $1.(*ast.Node)) >>
;
PrintStrStmt
: "printStr" Expression << ast.NewIOStmt(string($0.(*token.Token).Lit), $1.(*ast.Node)) >>
;
ScanIntStmt
: "scanInt" Expression << ast.NewIOStmt(string($0.(*token.Token).Lit), $1.(*ast.Node)) >>
;
ExpressionStmt
: Expression
;
IncDecStmt
: Expression "++" << ast.NewIncDecStmt(string($1.(*token.Token).Lit), $0.(*ast.Node)) >>
| Expression "--" << ast.NewIncDecStmt(string($1.(*token.Token).Lit), $0.(*ast.Node)) >>
;
Assignment
: ExpressionList assignOp ExpressionList << ast.NewAssignStmt(0, string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| ExpressionList "=" ExpressionList << ast.NewAssignStmt(1, string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
| IdentifierList "=" ExpressionList << ast.NewAssignStmt(2, string($1.(*token.Token).Lit), $0.(*ast.Node), $2.(*ast.Node)) >>
;
ShortVarDecl
: IdentifierList shortAssign ExpressionList << ast.NewShortDecl($0.(*ast.Node), $2.(ast.AstNode)) >>
;
Label
: identifier << ast.InitNode(string($0.(*token.Token).Lit), []string{}) >>
;