Skip to content

Commit b265698

Browse files
[examples] Add struct support to Toy DSL examples. (buddy-compiler#65)
1 parent eb83e88 commit b265698

File tree

4 files changed

+295
-27
lines changed

4 files changed

+295
-27
lines changed

examples/ToyDSL/Toy.g4

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ grammar Toy;
55
}
66

77
module
8-
: funDefine+
8+
: structDefine* funDefine+
99
;
1010

1111
expression
@@ -17,6 +17,8 @@ expression
1717
| identifierExpr
1818
| expression Mul expression
1919
| expression Add expression
20+
| expression Dot expression
21+
| structLiteral
2022
;
2123

2224
identifierExpr
@@ -57,6 +59,14 @@ varDecl returns [std::string idName]
5759
if ($Equal)
5860
tensorDataBuffer.clear();
5961
}
62+
| Identifier Identifier (Equal expression)?
63+
{
64+
$idName = $Identifier.text;
65+
}
66+
| Identifier
67+
{
68+
$idName = $Identifier.text;
69+
}
6070
;
6171

6272
type
@@ -75,8 +85,8 @@ prototype returns [std::string idName]
7585
;
7686

7787
declList
78-
: Identifier
79-
| Identifier Comma declList
88+
: varDecl
89+
| varDecl Comma declList
8090
;
8191

8292
block
@@ -87,6 +97,23 @@ blockExpr
8797
: varDecl | returnExpr | expression
8898
;
8999

100+
literalList
101+
: tensorLiteral
102+
{
103+
tensorDataBuffer.clear();
104+
}
105+
| tensorLiteral Comma literalList
106+
;
107+
108+
structLiteral
109+
:
110+
| BracketOpen (structLiteral | literalList) (Comma (structLiteral| literalList))* BracketClose
111+
;
112+
113+
structDefine
114+
: Struct Identifier BracketOpen (varDecl Semicolon)* BracketClose
115+
;
116+
90117
ParentheseOpen
91118
: '('
92119
;
@@ -127,6 +154,10 @@ Def
127154
: 'def'
128155
;
129156

157+
Struct
158+
: 'struct'
159+
;
160+
130161
Identifier
131162
: [a-zA-Z][a-zA-Z0-9_]*
132163
;
@@ -159,6 +190,10 @@ Mul
159190
: '*'
160191
;
161192

193+
Dot
194+
: '.'
195+
;
196+
162197
WS
163198
: [ \r\n\t] -> skip
164199
;

0 commit comments

Comments
 (0)