-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
270 lines (237 loc) · 6.06 KB
/
parser.y
File metadata and controls
270 lines (237 loc) · 6.06 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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
extern int yylex();
extern int yylineno;
void yyerror(const char* s);
BlockNode* programRoot = nullptr;
%}
%union {
long long ival;
bool bval;
char* sval;
ASTNode* node;
BlockNode* block;
std::vector<std::unique_ptr<ASTNode>>* args;
std::vector<std::string>* params;
}
%token <ival> INTEGER
%token <bval> BOOLEAN
%token <sval> STRING IDENTIFIER
%token FUNCTION END IF THEN ELSE ELSEIF WHILE DO RETURN LOCAL TYPE PRINT
%token EQ NE LT LE GT GE AND OR NOT
%type <node> statement expression primary_expr unary_expr multiplicative_expr
%type <node> additive_expr comparison_expr logical_and_expr logical_or_expr
%type <node> function_def if_stmt while_stmt assignment return_stmt function_call
%type <block> program statement_list block
%type <args> arg_list arg_list_items
%type <params> param_list param_list_items
%type <sval> type_annotation opt_type_annotation
%left OR
%left AND
%left EQ NE
%left LT LE GT GE
%left '+' '-'
%left '*' '/' '%'
%right NOT
%right UNARY
%%
program:
statement_list { programRoot = $1; $$ = $1; }
;
statement_list:
/* empty */ { $$ = new BlockNode(); }
| statement_list statement {
$$ = $1;
if ($2) $$->addStatement($2);
}
;
statement:
assignment { $$ = $1; }
| function_def { $$ = $1; }
| function_call { $$ = $1; }
| if_stmt { $$ = $1; }
| while_stmt { $$ = $1; }
| return_stmt { $$ = $1; }
| PRINT '(' arg_list ')' {
PrintNode* pn = new PrintNode();
if ($3) {
pn->args = std::move(*$3);
delete $3;
}
$$ = pn;
}
;
assignment:
LOCAL IDENTIFIER opt_type_annotation '=' expression {
$$ = new AssignmentNode($2, $5, $3 ? $3 : "");
free($2);
if ($3) free($3);
}
| IDENTIFIER '=' expression {
$$ = new AssignmentNode($1, $3);
free($1);
}
;
opt_type_annotation:
/* empty */ { $$ = nullptr; }
| ':' type_annotation { $$ = $2; }
;
type_annotation:
IDENTIFIER { $$ = $1; }
;
function_def:
FUNCTION IDENTIFIER '(' param_list ')' opt_type_annotation block END {
$$ = new FunctionDefNode($2, *$4, $7, {}, $6 ? $6 : "");
free($2);
if ($6) free($6);
delete $4;
}
;
param_list:
/* empty */ { $$ = new std::vector<std::string>(); }
| param_list_items { $$ = $1; }
;
param_list_items:
IDENTIFIER opt_type_annotation {
$$ = new std::vector<std::string>();
$$->push_back($1);
free($1);
if ($2) free($2);
}
| param_list_items ',' IDENTIFIER opt_type_annotation {
$$ = $1;
$$->push_back($3);
free($3);
if ($4) free($4);
}
;
function_call:
IDENTIFIER '(' arg_list ')' {
FunctionCallNode* fc = new FunctionCallNode($1);
if ($3) {
fc->args = std::move(*$3);
delete $3;
}
$$ = fc;
free($1);
}
;
arg_list:
/* empty */ { $$ = nullptr; }
| arg_list_items { $$ = $1; }
;
arg_list_items:
expression {
$$ = new std::vector<std::unique_ptr<ASTNode>>();
$$->push_back(std::unique_ptr<ASTNode>($1));
}
| arg_list_items ',' expression {
$$ = $1;
$$->push_back(std::unique_ptr<ASTNode>($3));
}
;
if_stmt:
IF expression THEN block END {
$$ = new IfNode($2, $4);
}
| IF expression THEN block ELSE block END {
$$ = new IfNode($2, $4, $6);
}
;
while_stmt:
WHILE expression DO block END {
$$ = new WhileNode($2, $4);
}
;
block:
statement_list { $$ = $1; }
;
return_stmt:
RETURN expression {
$$ = new ReturnNode($2);
}
| RETURN {
$$ = new ReturnNode(nullptr);
}
;
expression:
logical_or_expr { $$ = $1; }
;
logical_or_expr:
logical_and_expr { $$ = $1; }
| logical_or_expr OR logical_and_expr {
$$ = new BinaryOpNode(BinaryOpType::OR, $1, $3);
}
;
logical_and_expr:
comparison_expr { $$ = $1; }
| logical_and_expr AND comparison_expr {
$$ = new BinaryOpNode(BinaryOpType::AND, $1, $3);
}
;
comparison_expr:
additive_expr { $$ = $1; }
| comparison_expr EQ additive_expr {
$$ = new BinaryOpNode(BinaryOpType::EQ, $1, $3);
}
| comparison_expr NE additive_expr {
$$ = new BinaryOpNode(BinaryOpType::NE, $1, $3);
}
| comparison_expr LT additive_expr {
$$ = new BinaryOpNode(BinaryOpType::LT, $1, $3);
}
| comparison_expr LE additive_expr {
$$ = new BinaryOpNode(BinaryOpType::LE, $1, $3);
}
| comparison_expr GT additive_expr {
$$ = new BinaryOpNode(BinaryOpType::GT, $1, $3);
}
| comparison_expr GE additive_expr {
$$ = new BinaryOpNode(BinaryOpType::GE, $1, $3);
}
;
additive_expr:
multiplicative_expr { $$ = $1; }
| additive_expr '+' multiplicative_expr {
$$ = new BinaryOpNode(BinaryOpType::ADD, $1, $3);
}
| additive_expr '-' multiplicative_expr {
$$ = new BinaryOpNode(BinaryOpType::SUB, $1, $3);
}
;
multiplicative_expr:
unary_expr { $$ = $1; }
| multiplicative_expr '*' unary_expr {
$$ = new BinaryOpNode(BinaryOpType::MUL, $1, $3);
}
| multiplicative_expr '/' unary_expr {
$$ = new BinaryOpNode(BinaryOpType::DIV, $1, $3);
}
| multiplicative_expr '%' unary_expr {
$$ = new BinaryOpNode(BinaryOpType::MOD, $1, $3);
}
;
unary_expr:
primary_expr { $$ = $1; }
| NOT unary_expr {
$$ = new UnaryOpNode(UnaryOpType::NOT, $2);
}
| '-' unary_expr %prec UNARY {
$$ = new UnaryOpNode(UnaryOpType::NEG, $2);
}
;
primary_expr:
INTEGER { $$ = new IntegerNode($1); }
| BOOLEAN { $$ = new BooleanNode($1); }
| STRING { $$ = new StringNode($1); free($1); }
| IDENTIFIER { $$ = new VariableNode($1); free($1); }
| function_call { $$ = $1; }
| '(' expression ')' { $$ = $2; }
;
%%
void yyerror(const char* s) {
fprintf(stderr, "Parse error at line %d: %s\n", yylineno, s);
}