-
Notifications
You must be signed in to change notification settings - Fork 0
/
origin.y
356 lines (351 loc) · 9.57 KB
/
origin.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
%{
#include <stdio.h>
#include "origin.h"
#define YYDEBUG 1
int yyerror(char const *str) {
extern char *yytext;
char *near_token;
if (yytext[0] == '\0') {
near_token = "EOF";
} else {
near_token = yytext;
}
//crb_compile_error(PARSE_ERR,STRING_MESSAGE_ARGUMENT, "token", near_token,MESSAGE_ARGUMENT_END);
return 0;
}
%}
%union {
char *identifier;
ParameterList *parameter_list;
ArgumentList *argument_list;
Expression *expression;
Statement *statement;
StatementList *statement_list;
Block *block;
Elseif *elseif;
IdentifierList *identifier_list;
}
%token <expression> INT_LITERAL
%token <expression> DOUBLE_LITERAL
%token <expression> STRING_LITERAL
%token <identifier> IDENTIFIER
%token FUNCTION IF ELSE ELSEIF WHILE FOR RETURN_O BREAK CONTINUE NULL_O
LP RP LC RC SEMICOLON COMMA ASSIGN LOGICAL_AND LOGICAL_OR
EQ NE GT GE LT LE ADD SUB MUL DIV MOD TRUE_O FALSE_O GLOBAL_O
BIT_LEFT BIT_RIGHT BIT_OR BIT_AND BIT_XOR
%type <parameter_list> parameter_list
%type <argument_list> argument_list
%type <expression> expression expression_opt
logical_and_expression logical_or_expression
equality_expression relational_expression
additive_expression multiplicative_expression
unary_expression primary_expression bit_expression
%type <statement> statement global_statement
if_statement while_statement for_statement
return_statement break_statement continue_statement
%type <statement_list> statement_list
%type <block> block
%type <elseif> elseif elseif_list
%type <identifier_list> identifier_list
%%
translation_unit
: definition_or_statement
| translation_unit definition_or_statement
;
definition_or_statement
: function_definition
| statement
{
ORG_Interpreter *inter = org_get_current_interpreter();
inter->statement_list = org_chain_statement_list(inter->statement_list, $1);
}
;
function_definition
: FUNCTION IDENTIFIER LP parameter_list RP block
{
org_function_define($2, $4, $6);
}
| FUNCTION IDENTIFIER LP RP block
{
org_function_define($2, NULL, $5);
}
;
parameter_list
: IDENTIFIER
{
$$ = org_create_parameter($1);
}
| parameter_list COMMA IDENTIFIER
{
$$ = org_chain_parameter($1, $3);
}
;
argument_list
: expression
{
$$ = org_create_argument_list($1);
}
| argument_list COMMA expression
{
$$ = org_chain_argument_list($1, $3);
}
;
statement_list
: statement
{
$$ = org_create_statement_list($1);
}
| statement_list statement
{
$$ = org_chain_statement_list($1, $2);
}
;
expression
: logical_or_expression
| IDENTIFIER ASSIGN expression
{
$$ = org_create_assign_expression($1, $3);
}
;
logical_or_expression
: logical_and_expression
| logical_or_expression LOGICAL_OR logical_and_expression
{
$$ = org_create_binary_expression(LOGICAL_OR_EXPRESSION, $1, $3);
}
;
logical_and_expression
: equality_expression
| logical_and_expression LOGICAL_AND equality_expression
{
$$ = org_create_binary_expression(LOGICAL_AND_EXPRESSION, $1, $3);
}
;
equality_expression
: relational_expression
| equality_expression EQ relational_expression
{
$$ = org_create_binary_expression(EQ_EXPRESSION, $1, $3);
}
| equality_expression NE relational_expression
{
$$ = org_create_binary_expression(NE_EXPRESSION, $1, $3);
}
;
relational_expression
: bit_expression
| relational_expression GT additive_expression
{
$$ = org_create_binary_expression(GT_EXPRESSION, $1, $3);
}
| relational_expression GE additive_expression
{
$$ = org_create_binary_expression(GE_EXPRESSION, $1, $3);
}
| relational_expression LT additive_expression
{
$$ = org_create_binary_expression(LT_EXPRESSION, $1, $3);
}
| relational_expression LE additive_expression
{
$$ = org_create_binary_expression(LE_EXPRESSION, $1, $3);
}
;
bit_expression
: additive_expression
| additive_expression BIT_LEFT additive_expression
{
$$ = org_create_binary_expression(BIT_LEFT_EXPRESSION, $1, $3);
}
| additive_expression BIT_RIGHT additive_expression
{
$$ = org_create_binary_expression(BIT_RIGHT_EXPRESSION, $1, $3);
}
| additive_expression BIT_OR additive_expression
{
$$ = org_create_binary_expression(BIT_OR_EXPRESSION, $1, $3);
}
| additive_expression BIT_AND additive_expression
{
$$ = org_create_binary_expression(BIT_AND_EXPRESSION, $1, $3);
}
| additive_expression BIT_XOR additive_expression
{
$$ = org_create_binary_expression(BIT_XOR_EXPRESSION, $1, $3);
}
;
additive_expression
: multiplicative_expression
| additive_expression ADD multiplicative_expression
{
$$ = org_create_binary_expression(ADD_EXPRESSION, $1, $3);
}
| additive_expression SUB multiplicative_expression
{
$$ = org_create_binary_expression(SUB_EXPRESSION, $1, $3);
}
;
multiplicative_expression
: unary_expression
| multiplicative_expression MUL unary_expression
{
$$ = org_create_binary_expression(MUL_EXPRESSION, $1, $3);
}
| multiplicative_expression DIV unary_expression
{
$$ = org_create_binary_expression(DIV_EXPRESSION, $1, $3);
}
| multiplicative_expression MOD unary_expression
{
$$ = org_create_binary_expression(MOD_EXPRESSION, $1, $3);
}
;
unary_expression
: primary_expression
| SUB unary_expression
{
$$ = org_create_minus_expression($2);
}
;
primary_expression
: IDENTIFIER LP argument_list RP
{
$$ = org_create_function_call_expression($1, $3);
}
| IDENTIFIER LP RP
{
$$ = org_create_function_call_expression($1, NULL);
}
| LP expression RP
{
$$ = $2;
}
| IDENTIFIER
{
$$ = org_create_identifier_expression($1);
}
| INT_LITERAL
| DOUBLE_LITERAL
| STRING_LITERAL
| TRUE_O
{
$$ = org_create_boolean_expression(ORG_TRUE);
}
| FALSE_O
{
$$ = org_create_boolean_expression(ORG_FALSE);
}
| NULL_O
{
$$ = org_create_null_expression();
}
;
statement
: expression SEMICOLON
{
$$ = org_create_expression_statement($1);
}
| global_statement
| if_statement
| while_statement
| for_statement
| return_statement
| break_statement
| continue_statement
;
global_statement
: GLOBAL_O identifier_list SEMICOLON
{
$$ = org_create_global_statement($2);
}
;
identifier_list
: IDENTIFIER
{
$$ = org_create_global_identifier($1);
}
| identifier_list COMMA IDENTIFIER
{
$$ = org_chain_identifier($1, $3);
}
;
if_statement
: IF LP expression RP block
{
$$ = org_create_if_statement($3, $5, NULL, NULL);
}
| IF LP expression RP block ELSE block
{
$$ = org_create_if_statement($3, $5, NULL, $7);
}
| IF LP expression RP block elseif_list
{
$$ = org_create_if_statement($3, $5, $6, NULL);
}
| IF LP expression RP block elseif_list ELSE block
{
$$ = org_create_if_statement($3, $5, $6, $8);
}
;
elseif_list
: elseif
| elseif_list elseif
{
$$ = org_chain_elseif_list($1, $2);
}
;
elseif
: ELSEIF LP expression RP block
{
$$ = org_create_elseif($3, $5);
}
;
while_statement
: WHILE LP expression RP block
{
$$ = org_create_while_statement($3, $5);
}
;
for_statement
: FOR LP expression_opt SEMICOLON expression_opt SEMICOLON
expression_opt RP block
{
$$ = org_create_for_statement($3, $5, $7, $9);
}
;
expression_opt
: /* empty */
{
$$ = NULL;
}
| expression
;
return_statement
: RETURN_O expression_opt SEMICOLON
{
$$ = org_create_return_statement($2);
}
;
break_statement
: BREAK SEMICOLON
{
$$ = org_create_break_statement();
}
;
continue_statement
: CONTINUE SEMICOLON
{
$$ = org_create_continue_statement();
}
;
block
: LC statement_list RC
{
$$ = org_create_block($2);
}
| LC RC
{
$$ = org_create_block(NULL);
}
;
%%