-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
332 lines (296 loc) · 8.71 KB
/
parser.y
File metadata and controls
332 lines (296 loc) · 8.71 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
%{
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include "ast.hpp"
#include "tokens.hpp"
extern int yylex(void);
extern int yylineno;
void yyerror(const char* s);
AST::Program* g_program = nullptr;
static AST::ClassDecl* currentClass = nullptr;
%}
%code requires {
#include <vector>
namespace AST {
struct Node;
struct Program;
struct ClassDecl;
struct VarDecl;
struct Expr;
struct Stmt;
struct MethodDecl;
struct CtorDecl;
struct Param;
struct Block;
}
}
%defines "parser.hpp"
%define parse.error verbose
%union {
long long ival;
double rval;
char* cstr;
AST::Program* program;
AST::ClassDecl* classdecl;
AST::VarDecl* vardecl;
AST::Expr* expr;
AST::Stmt* stmt;
AST::MethodDecl* methoddecl;
AST::CtorDecl* ctordecl;
AST::Param* param;
std::vector<AST::ClassDecl*>* classlist;
std::vector<AST::Node*>* memberlist;
std::vector<AST::VarDecl*>* varlist;
std::vector<AST::Param*>* paramlist;
std::vector<AST::Stmt*>* stmtlist;
std::vector<AST::Expr*>* exprlist;
}
%token CLASS EXTENDS VAR IS END
%token METHOD RETURN IF THEN ELSE
%token WHILE LOOP
%token TRUE FALSE
%token THIS
%token COLON COMMA
%token LPAREN RPAREN LBRACKET RBRACKET
%token ASSIGN ARROW
%token DOT
%token <cstr> IDENTIFIER
%token <ival> INT_LITERAL
%token <rval> REAL_LITERAL
%token <cstr> STRING_LITERAL
%type <program> program
%type <classlist> class_list
%type <classdecl> class_decl
%type <memberlist> class_body member_list
%type <vardecl> field_decl
%type <methoddecl> method_decl
%type <ctordecl> ctor_decl
%type <paramlist> opt_params param_list
%type <param> param
%type <stmt> stmt simple_stmt if_stmt while_stmt block_stmt method_body opt_else
%type <stmtlist> stmt_list
%type <expr> expr primary postfix
%type <exprlist> arg_list opt_args
%type <cstr> opt_extends opt_ret_type
%%
program
: class_list
{
g_program = new AST::Program();
for (auto* c : *$1) g_program->classes.push_back(c);
delete $1;
}
;
class_list
: class_list class_decl { $$ = $1; $1->push_back($2); }
| class_decl { $$ = new std::vector<AST::ClassDecl*>(); $$->push_back($1); }
;
class_decl
: CLASS IDENTIFIER opt_extends IS class_body END
{
AST::ClassDecl* c = new AST::ClassDecl($2);
if ($3) c->baseName = $3;
for (auto* n : *$5) {
if (auto* v = dynamic_cast<AST::VarDecl*>(n)) c->fields.push_back(v);
else if (auto* ct = dynamic_cast<AST::CtorDecl*>(n)) c->ctors.push_back(ct);
else if (auto* m = dynamic_cast<AST::MethodDecl*>(n)) c->methods.push_back(m);
else delete n;
}
free($2);
if ($3) free($3);
delete $5;
$$ = c;
}
;
opt_extends
: EXTENDS IDENTIFIER { $$ = $2; }
| { $$ = nullptr; }
;
class_body
: member_list { $$ = $1; }
| { $$ = new std::vector<AST::Node*>(); }
;
member_list
: member_list field_decl { $$ = $1; $1->push_back($2); }
| member_list method_decl { $$ = $1; $1->push_back($2); }
| member_list ctor_decl { $$ = $1; $1->push_back($2); }
| field_decl { $$ = new std::vector<AST::Node*>(); $$->push_back($1); }
| method_decl { $$ = new std::vector<AST::Node*>(); $$->push_back($1); }
| ctor_decl { $$ = new std::vector<AST::Node*>(); $$->push_back($1); }
;
field_decl
: VAR IDENTIFIER COLON expr
{
AST::VarDecl* v = new AST::VarDecl($2, "", $4);
free($2);
$$ = v;
}
;
ctor_decl
: THIS opt_params IS block_stmt END
{
AST::CtorDecl* c = new AST::CtorDecl();
for (auto* p : *$2) c->params.push_back(p);
delete $2;
c->body = $4;
$$ = c;
}
;
method_decl
: METHOD IDENTIFIER opt_params opt_ret_type method_body
{
AST::MethodDecl* m = new AST::MethodDecl($2, $4 ? $4 : std::string(""), $5);
for (auto* p : *$3) m->params.push_back(p);
delete $3;
if ($4) free($4);
free($2);
$$ = m;
}
;
opt_params
: LPAREN param_list RPAREN { $$ = $2; }
| LPAREN RPAREN { $$ = new std::vector<AST::Param*>(); }
| { $$ = new std::vector<AST::Param*>(); }
;
param_list
: param_list COMMA param { $$ = $1; $1->push_back($3); }
| param { $$ = new std::vector<AST::Param*>(); $$->push_back($1); }
;
param
: IDENTIFIER COLON IDENTIFIER
{
AST::Param* p = new AST::Param($1, $3);
free($1);
free($3);
$$ = p;
}
;
opt_ret_type
: COLON IDENTIFIER { $$ = $2; }
| { $$ = nullptr; }
;
method_body
: IS block_stmt END
{
$$ = $2;
}
| ARROW expr
{
AST::Block* b = new AST::Block();
b->stmts.push_back(new AST::ReturnStmt($2));
$$ = b;
}
;
block_stmt
: stmt_list
{
AST::Block* b = new AST::Block();
for (auto* s : *$1) b->stmts.push_back(s);
delete $1;
$$ = b;
}
|
{
AST::Block* b = new AST::Block();
$$ = b;
}
;
stmt_list
: stmt_list stmt { $$ = $1; $1->push_back($2); }
| stmt { $$ = new std::vector<AST::Stmt*>(); $$->push_back($1); }
;
stmt
: simple_stmt { $$ = $1; }
| if_stmt { $$ = $1; }
| while_stmt { $$ = $1; }
;
simple_stmt
: RETURN expr { $$ = new AST::ReturnStmt($2); }
| RETURN { $$ = new AST::ReturnStmt(nullptr); }
| VAR IDENTIFIER COLON expr
{
AST::VarDecl* v = new AST::VarDecl($2, "", $4);
free($2);
$$ = new AST::VarDeclStmt(v);
}
| IDENTIFIER ASSIGN expr
{
AST::Expr* lhs = new AST::Identifier($1);
free($1);
AST::Expr* rhs = $3;
AST::Binary* bin = new AST::Binary(AST::BinOp::Assign, lhs, rhs);
$$ = new AST::ExprStmt(bin);
}
| expr
{
$$ = new AST::ExprStmt($1);
}
;
if_stmt
: IF expr THEN block_stmt opt_else END
{
$$ = new AST::IfStmt($2, $4, $5);
}
;
opt_else
: ELSE block_stmt { $$ = $2; }
| { $$ = new AST::Block(); }
;
while_stmt
: WHILE expr LOOP block_stmt END
{
$$ = new AST::WhileStmt($2, $4);
}
;
expr
: postfix { $$ = $1; }
;
postfix
: primary { $$ = $1; }
| postfix DOT IDENTIFIER
{
$$ = new AST::MemberAccess($1, $3);
free($3);
}
| postfix LPAREN opt_args RPAREN
{
AST::Call* call = new AST::Call($1);
for (auto* e : *$3) call->args.push_back(e);
delete $3;
$$ = call;
}
| IDENTIFIER LPAREN opt_args RPAREN
{
AST::Identifier* id = new AST::Identifier($1);
free($1);
AST::Call* call = new AST::Call(id);
for (auto* e : *$3) call->args.push_back(e);
delete $3;
$$ = call;
}
;
opt_args
: arg_list { $$ = $1; }
| { $$ = new std::vector<AST::Expr*>(); }
;
arg_list
: arg_list COMMA expr { $$ = $1; $1->push_back($3); }
| expr { $$ = new std::vector<AST::Expr*>(); $$->push_back($1); }
;
primary
: INT_LITERAL { $$ = new AST::IntLiteral($1); }
| REAL_LITERAL { $$ = new AST::RealLiteral($1); }
| STRING_LITERAL { $$ = new AST::StringLiteral($1); free($1); }
| TRUE { $$ = new AST::BoolLiteral(true); }
| FALSE { $$ = new AST::BoolLiteral(false); }
| THIS { $$ = new AST::ThisExpr(); }
| IDENTIFIER { $$ = new AST::Identifier($1); free($1); }
| LPAREN expr RPAREN { $$ = $2; }
;
%%
void yyerror(const char* s) {
std::fprintf(stderr, "Parse error at line %d: %s\n", yylineno, s);
}