-
Notifications
You must be signed in to change notification settings - Fork 0
/
blang.y
374 lines (291 loc) · 13.5 KB
/
blang.y
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
%{
#include <cstdio>
#include <iostream>
#include "SizeType.h"
#include "astnode.h"
using namespace std;
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE* yyin;
void yyerror(const char* err);
bool g_isBoolean = false;
Blang::ProgramAST* blangProgram;
%}
%union
{
std::string* str;
Blang::SizeType word;
Blang::AssignOp_t assign;
Blang::UnaryOp_t unary;
Blang::ProgramAST* program;
Blang::ExtrnDefAST* definition;
Blang::AutoDeclAST* adecl;
Blang::DeclAST* decl;
Blang::ConstExprAST* constant, *ival;
Blang::StatementAST* statement;
Blang::ExpressionAST* expression;
Blang::NameAST* name;
Blang::StatementASTList* statement_list;
Blang::AutoDeclASTList* auto_decl_list;
Blang::ExpressionASTList* expression_list;
Blang::NameASTList* name_list;
Blang::ConstExprASTList* ival_list;
}
%token <word> WORD
%token <str> STRING
%token <str> CHAR
%token <str> IDENT
%token AUTO EXTRN IF ELSE WHILE SWITCH CASE DEFAULT RETURN BREAK GOTO
%token ASN_ADD ASN_SUB ASN_MUL ASN_DIV ASN_MOD ASN_AND ASN_OR ASN_XOR
%token ASN_LSH ASN_RSH ASN_EQ ASN_NQ ASN_GT ASN_LT ASN_GTE ASN_LTE
%token UN_INC UN_DEC LOG_LTE LOG_GTE BIT_LSH BIT_RSH LOG_EQ LOG_NQ
%type <program> extrn_definition_list
%type <definition> extrn_definition func_decl extrn_decl
%type <decl> decl
%type <adecl> adecl
%type <name> name
%type <statement> statement local_decl if_statement while_statement switch_statement
%type <statement> expression_statement jump_statement statement_block labeled_statement selection_statement
%type <assign> assignment_operator
%type <unary> unary_operator
%type <expression> assignment_expression unary_expression if_expr_statement
%type <expression> conditional_expression bitwise_or_expression bitwise_xor_expression bitwise_and_expression postfix_expression primary_expression ival
%type <expression> equality_expression relational_expression shift_expression additive_expression multiplicative_expression expression constant
%type <ival_list> ival_list
%type <name_list> name_list func_decl_parms extrn_decl_local
%type <statement_list> local_decl_list statement_list
%type <auto_decl_list> auto_decl_list auto_decl
%type <expression_list> arg_expression_list
%token END 0 "end of file"
%define parse.error verbose
%start blang_program
%%
blang_program
: extrn_definition_list { blangProgram = $1; }
;
extrn_definition_list
: extrn_definition { $$ = new Blang::ProgramAST(); $$->m_extrnDefs.push_back($<definition>1); }
| extrn_definition_list extrn_definition { $1->m_extrnDefs.push_back($<definition>2); }
;
extrn_definition
: extrn_decl ';' { $$ = $<definition>1; }
| func_decl { $$ = $<definition>1; }
;
extrn_decl
: decl { $$ = new Blang::ExtrnDeclAST($<decl>1, NULL); $<decl>1->m_storage = Blang::kExtrn; }
| decl ival_list { $$ = new Blang::ExtrnDeclAST($<decl>1, $<ival_list>2); $<decl>1->m_storage = Blang::kExtrn; }
;
func_decl
: name func_decl_parms statement { $$ = new Blang::FuncDeclAST($<name>1, $<ival_list>2, $<statement>3); /*funcs are always extrn*/ }
;
func_decl_parms
: '(' ')' { $$ = NULL; }
| '(' ival_list ')' { $$ = new Blang::NameASTList(*$<ival_list>2); }
;
name_list
: name { $$ = new Blang::NameASTList(); $$->push_back($<name>1); }
| name_list ',' name { $$->push_back($<name>3); }
;
ival_list
: ival { $$ = new Blang::ConstExprASTList(); $$->push_back($<ival>1); }
| ival_list ',' ival { $$->push_back($<ival>3); }
;
decl
: name { $$ = new Blang::DeclAST($<name>1); }
| name '[' ']' { $$ = new Blang::VectorDeclAST($<name>1, NULL); }
| name '[' constant ']' { $$ = new Blang::VectorDeclAST($<name>1, $<constant>3); }
;
statement
: labeled_statement
| statement_block
| selection_statement
| while_statement
| jump_statement
| expression_statement
;
labeled_statement
: name ':' statement { $$ = new Blang::LabelStatementAST( $<name>1, $<statement>3 ); /*labels are always internal storage*/ }
| CASE constant ':' statement { $$ = new Blang::CaseStatementAST ( $<constant>2, $<statement>4 ); }
| DEFAULT ':' statement { $$ = new Blang::CaseStatementAST ( $<statement>3 ); }
;
statement_block
: '{' '}' { $$ = new Blang::StatementBlockAST( NULL ); }
| '{' local_decl_list '}' { $$ = new Blang::StatementBlockAST( $<statement_list>2 ); }
| '{' statement_list '}' { $$ = new Blang::StatementBlockAST( $<statement_list>2 ); }
| '{' local_decl_list statement_list '}' { $$ = new Blang::StatementBlockAST( $<statement_list>2 ); $<statement_list>2->insert( $<statement_list>2->end(),
$<statement_list>3->begin(),
$<statement_list>3->end() ); delete $3; }
;
local_decl
: auto_decl ';' { $$ = new Blang::AutoDeclStatementAST( $<auto_decl_list>1 ); }
| extrn_decl_local ';' { $$ = new Blang::ExtrnDeclLocalStatementAST( $<name_list>1 ); }
;
local_decl_list
: local_decl { $$ = new Blang::StatementASTList(); $$->push_back( $<statement>1 ); }
| local_decl_list local_decl { $1->push_back( $<statement>2 ); }
;
auto_decl
: AUTO auto_decl_list { $$ = $<auto_decl_list>2; }
;
auto_decl_list
: adecl { $$ = new Blang::AutoDeclASTList(); $$->push_back( $<adecl>1 ); }
| auto_decl_list ',' adecl { $1->push_back( $<adecl>3 ); }
;
adecl
: name { $$ = new Blang::AutoDeclAST( $<name>1, NULL ); }
| name '[' constant ']' { $$ = new Blang::AutoDeclAST( $<name>1, $<constant>3 ); }
;
extrn_decl_local
: EXTRN name_list { $$ = $<name_list>2; }
;
statement_list
: statement { $$ = new Blang::StatementASTList(); $$->push_back( $<statement>1 ); }
| statement_list statement { $1->push_back( $<statement>2 ); }
;
selection_statement
: if_statement
| switch_statement
;
if_statement
: if_expr_statement statement { $$ = new Blang::IfStatementAST($<expression>1, $<statement>2, NULL); }
| if_expr_statement statement ELSE statement { $$ = new Blang::IfStatementAST($<expression>1, $<statement>2, $<statement>4); }
;
if_expr_statement
: IF '(' { g_isBoolean = true; }
expression ')' { $$ = $<expression>4; g_isBoolean = false; }
;
switch_statement
: SWITCH '(' expression ')' statement { $$ = new Blang::SwitchStatementAST($<expression>3, $<statement>5); }
;
while_statement
: WHILE '(' { g_isBoolean = true; }
expression ')' statement { $$ = new Blang::WhileStatementAST($<expression>4, $<statement>5); g_isBoolean = false; }
;
jump_statement
: GOTO name ';' { $$ = new Blang::GotoStatementAST( $<name>2 ); }
| BREAK ';' { $$ = new Blang::BreakStatementAST(); }
| RETURN ';' { $$ = new Blang::ReturnStatementAST( NULL ); }
| RETURN '(' expression ')' ';' { $$ = new Blang::ReturnStatementAST( $<expression>3 ); }
;
expression_statement
: ';' { $$ = new Blang::ExpressionStatementAST( NULL ); }
| expression ';' { $$ = new Blang::ExpressionStatementAST($<expression>1); }
;
primary_expression
: ival
| '(' expression ')' { $$ = $<expression>2; }
;
postfix_expression
: primary_expression
| postfix_expression '[' expression ']' { $$ = new Blang::VectorExprAST( $<expression>1, $<expression>3 ); }
| postfix_expression '(' ')' { $$ = new Blang::FuncCallExprAST( $<expression>1, NULL ); }
| postfix_expression '(' arg_expression_list ')' { $$ = new Blang::FuncCallExprAST( $<expression>1, $<expression_list>2 ); }
| postfix_expression UN_INC { $$ = new Blang::UnaryExpressionAST(Blang::kIncr, $<expression>2, true); }
| postfix_expression UN_DEC { $$ = new Blang::UnaryExpressionAST(Blang::kDecr, $<expression>2, true); }
;
arg_expression_list
: assignment_expression { $$ = new Blang::ExpressionASTList(); $$->push_back( $<expression>1 ); }
| arg_expression_list ',' assignment_expression { $1->push_back( $<expression>3 ); }
;
unary_expression
: postfix_expression
| UN_INC unary_expression { $$ = new Blang::UnaryExpressionAST(Blang::kIncr, $<expression>2, false); }
| UN_DEC unary_expression { $$ = new Blang::UnaryExpressionAST(Blang::kDecr, $<expression>2, false); }
| unary_operator multiplicative_expression { $$ = new Blang::UnaryExpressionAST($<unary>1, $<expression>2, false); }
;
unary_operator
: '&' { $$ = Blang::kAddrOf; }
| '*' { $$ = Blang::kDeRef; }
| '-' { $$ = Blang::kNegate; }
| '!' { $$ = Blang::kLogNot; }
| '~' { $$ = Blang::kBitNot; }
;
multiplicative_expression
: unary_expression
| multiplicative_expression '*' unary_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kMul, $<expression>3); }
| multiplicative_expression '/' unary_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kDiv, $<expression>3); }
| multiplicative_expression '%' unary_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kMod, $<expression>3); }
;
additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kAdd, $<expression>3); }
| additive_expression '-' multiplicative_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kSub, $<expression>3); }
;
shift_expression
: additive_expression
| shift_expression BIT_LSH additive_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kLshift, $<expression>3); }
| shift_expression BIT_RSH additive_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kRshift, $<expression>3); }
;
relational_expression
: shift_expression
| relational_expression '<' shift_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kLess, $<expression>3); }
| relational_expression '>' shift_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kGreater, $<expression>3); }
| relational_expression LOG_LTE shift_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kLessEq, $<expression>3); }
| relational_expression LOG_GTE shift_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kGreaterEq, $<expression>3); }
;
equality_expression
: relational_expression
| equality_expression LOG_EQ relational_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kEqual, $<expression>3); }
| equality_expression LOG_NQ relational_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kNotEqual, $<expression>3); }
;
bitwise_and_expression
: equality_expression
| bitwise_and_expression '&' equality_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, g_isBoolean ? Blang::kLogAnd : Blang::kAnd, $<expression>3); }
;
bitwise_xor_expression
: bitwise_and_expression
| bitwise_xor_expression '^' bitwise_and_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, Blang::kXor, $<expression>3); }
;
bitwise_or_expression
: bitwise_xor_expression
| bitwise_or_expression '|' bitwise_xor_expression { $$ = new Blang::BinaryExpressionAST( $<expression>1, g_isBoolean ? Blang::kLogOr : Blang::kOr, $<expression>3); }
;
conditional_expression
: bitwise_or_expression
| bitwise_or_expression '?' expression ':' conditional_expression { $$ = new Blang::ConditionalExpressionAST( $<expression>1, $<expression>3, $<expression>5 ); }
;
assignment_expression
: conditional_expression
| unary_expression assignment_operator assignment_expression { $$ = new Blang::AssignmentAST($<expression>1, $2, $<expression>3); }
;
assignment_operator
: '=' { $$ = Blang::kAssign; }
| ASN_ADD { $$ = Blang::kAddAssign; }
| ASN_SUB { $$ = Blang::kSubAssign; }
| ASN_MUL { $$ = Blang::kMulAssign; }
| ASN_DIV { $$ = Blang::kDivAssign; }
| ASN_MOD { $$ = Blang::kModAssign; }
| ASN_AND { $$ = Blang::kAndAssign; }
| ASN_OR { $$ = Blang::kOrAssign; }
| ASN_XOR { $$ = Blang::kXorAssign; }
| ASN_LSH { $$ = Blang::kLshAssign; }
| ASN_RSH { $$ = Blang::kRshAssign; }
| ASN_EQ { $$ = Blang::kEqAssign; }
| ASN_NQ { $$ = Blang::kNqAssign; }
| ASN_GT { $$ = Blang::kGtAssign; }
| ASN_LT { $$ = Blang::kLtAssign; }
| ASN_GTE { $$ = Blang::kGteAssign; }
| ASN_LTE { $$ = Blang::kLteAssign; }
;
expression
: assignment_expression
;
ival
: name { $$ = $<expression>1; }
| constant { $$ = $<expression>1; }
;
name
: IDENT { $$ = new Blang::NameAST(*$1); delete $1; }
;
constant
: WORD { $$ = new Blang::WordConstantAST($1); printf("WORD: %ld\n", $1); }
| STRING { $$ = new Blang::StringConstantAST(*$1); delete $1; }
| CHAR { $$ = new Blang::CharConstantAST(*$1); delete $1; }
;
%%
void
yyerror(const char* err)
{
cout << "PARSE ERROR: " << err << endl;
exit(-1);
}