]> git.bts.cx Git - sun.git/blob - runtime/src/sun/compiler/sun.y
b71f4de442f7da6a17e8c808f04a6a2ab6897349
[sun.git] / runtime / src / sun / compiler / sun.y
1 %{
2 #define YYSTYPE SLTNode *
3
4 #include <sun/compiler/compiler_internal.h>
5 #include <sun/tree/node.h>
6
7 #include "sun_parser.h"
8 #include "sun_lexer.h"
9
10 void yyerror(const YYLTYPE *location, SLTNode **moduleOut, yyscan_t *scanner, const char *error);
11 %}
12
13 %pure-parser
14
15 %parse-param {SLTNode **moduleOut}
16 %parse-param {yyscan_t *scanner}
17
18 %lex-param {yyscan_t *scanner}
19
20 %error-verbose
21 %locations
22
23 %token IF "if"
24 %token ELSE "else"
25 %token FOR "for"
26 %token DO "do"
27 %token WHILE "while"
28 %token BREAK "break"
29 %token TYPE "struct"
30
31 %token RETURN "return"
32 %token SLEEP "sleep"
33
34 %token BOOL_FALSE "false"
35 %token BOOL_TRUE "true"
36
37 %token ADD_ASSIGN "+="
38 %token SUBTRACT_ASSIGN "-="
39 %token MULTIPLY_ASSIGN "*="
40 %token DIVIDE_ASSIGN "/="
41 %token MOD_ASSIGN "%="
42
43 %token LOGICAL_AND "&&"
44 %token LOGICAL_OR "||"
45
46 %token EQUAL "=="
47 %token NOT_EQUAL "!="
48 %token LT_EQUAL "<="
49 %token GT_EQUAL ">="
50
51 /*%token INCREMENT "++"
52 %token DECREMENT "--"*/
53
54 %token IDENTIFIER
55
56 %token INT
57 %token FLOAT
58 %token STRING
59
60 /* https://en.cppreference.com/w/c/language/operator_precedence */
61 %left ','
62 %right '=' "+=" "-=" "*=" "/=" "%=" /* etc... */
63 %left "||"
64 %left "&&"
65 %left '|'
66 %left '^'
67 %left '&'
68 %left "==" "!="
69 %left '<' "<=" '>' ">="
70 /* %left "<<" ">>" */
71 %left '+' '-'
72 %left '*' '/' '%'
73 %right U_PLUS U_MINUS '!' '~'
74 /* %right "++" "--" "" */
75 /* %left "++" "--" "" */
76
77 %start module
78
79 %%
80
81 module
82 : optional_statements_or_definitions {
83 $$ = sltNodeMakeModule($1);
84 *moduleOut = $$;
85 }
86 ;
87
88 optional_statements_or_definitions
89 : /* Nothing */ {
90 $$ = NULL;
91 }
92 | statements_or_definitions
93 ;
94
95 statements_or_definitions
96 : definition {
97 $$ = sltNodeMakeSequence(NULL, $1);
98 }
99 | statement {
100 $$ = sltNodeMakeSequence(NULL, $1);
101 }
102 | statements_or_definitions definition {
103 $$ = sltNodeMakeSequence($1, $2);
104 }
105 | statements_or_definitions statement {
106 $$ = sltNodeMakeSequence($1, $2);
107 }
108 ;
109
110 definition
111 : function_definition
112 /*| type_definition*/
113 | variable_definition ';'
114 ;
115
116 function_definition
117 : IDENTIFIER IDENTIFIER '(' optional_parameter_definitions ')' '{' optional_statements '}' {
118 $$ = sltNodeMakeFunctionDefinition($1, $2, $4, $7);
119 }
120 ;
121
122 optional_parameter_definitions
123 : /* Nothing */ {
124 $$ = NULL;
125 }
126 | parameter_definitions
127 ;
128
129 parameter_definitions
130 : parameter_definition {
131 $$ = sltNodeMakeSequence(NULL, $1);
132 }
133 | parameter_definitions ',' parameter_definition {
134 $$ = sltNodeMakeSequence($1, $3);
135 }
136 ;
137
138 parameter_definition
139 : IDENTIFIER IDENTIFIER {
140 $$ = sltNodeMakeFunctionParameterDefinition($1, $2);
141 }
142 ;
143
144 /*type_definition
145 : "struct" IDENTIFIER '{' type_member_definitions '}' {
146 $$ = sltNodeMakeTypeDefinition($2, $4);
147 }
148 ;
149
150 type_member_definitions
151 : type_member_definition ';' {
152 $$ = sltNodeMakeSequence(NULL, $1);
153 }
154 | type_member_definitions type_member_definition ';' {
155 $$ = sltNodeMakeSequence($1, $2);
156 }
157 ;
158
159 type_member_definition
160 : IDENTIFIER IDENTIFIER {
161 $$ = sltNodeMakeTypeMemberDefinition($1, $2);
162 }
163 ;*/
164
165 variable_definition
166 : IDENTIFIER IDENTIFIER {
167 $$ = sltNodeMakeVariableDefinition($1, $2, NULL);
168 }
169 | IDENTIFIER IDENTIFIER '=' expression {
170 $$ = sltNodeMakeVariableDefinition($1, $2, $4);
171 }
172 ;
173
174 optional_statements
175 : /* nothing */ {
176 $$ = NULL;
177 }
178 | statements
179 ;
180
181 statements
182 : statement {
183 if ($1 != NULL) {
184 $$ = sltNodeMakeSequence(NULL, $1);
185 }
186
187 }
188 | statements statement {
189 if ($2 != NULL) {
190 $$ = sltNodeMakeSequence($1, $2);
191 } else {
192 $$ = $1;
193 }
194 }
195 ;
196
197 statement
198 : ';' {
199 $$ = NULL; /* Otherwise this will pass up the ';' character */
200 }
201 | variable_definition ';'
202 | "return" optional_expression ';' {
203 $$ = sltNodeMakeReturn($2);
204 }
205 | "sleep" ';' {
206 $$ = sltNodeMakeSleep();
207 }
208 | selection_statement
209 | iteration_statement
210 | "break" ';' {
211 $$ = sltNodeMakeBreak();
212 }
213 | expression ';'
214 | '{' optional_statements '}' {
215 if ($2 != NULL) {
216 $$ = sltNodeMakeStatementBlock($2);
217 } else {
218 $$ = NULL;
219 }
220 }
221 ;
222
223 selection_statement
224 : "if" '(' expression ')' statement {
225 $$ = sltNodeMakeSelection($3, $5, NULL);
226 }
227 | "if" '(' expression ')' statement "else" statement {
228 $$ = sltNodeMakeSelection($3, $5, $7);
229 }
230 ;
231
232 iteration_statement
233 : "for" '(' optional_expression_or_variable_definition ';' optional_expression ';' optional_expression ')' statement {
234 $$ = sltNodeMakeForIteration($3, $5, $7, $9);
235 }
236 | "while" '(' expression ')' statement {
237 $$ = sltNodeMakeWhileIteration($3, $5);
238 }
239 | "do" statement "while" '(' expression ')' ';' {
240 $$ = sltNodeMakeDoIteration($5, $2);
241 }
242 ;
243
244 optional_expression_or_variable_definition
245 : /* nothing */ {
246 $$ = NULL;
247 }
248 | expression
249 | variable_definition
250 ;
251
252 optional_expression
253 : /* nothing */ {
254 $$ = NULL;
255 }
256 | expression
257 ;
258
259 expression
260 : assignment_expression
261 ;
262
263 assignment_expression
264 : logic_expression
265 | unary_expression assignment_operator assignment_expression {
266 $$ = sltNodeMakeAssignment($1, ($2 == NULL) ? $3 : sltNodeMakeBinaryOperation($2, $1, $3));
267 }
268 ;
269
270 assignment_operator
271 : '=' {
272 $$ = NULL;
273 }
274 | "+=" {
275 $$ = sltNodeMakeOperator(SLTOperatorTypeAdd);
276 }
277 | "-=" {
278 $$ = sltNodeMakeOperator(SLTOperatorTypeSubtract);
279 }
280 | "*=" {
281 $$ = sltNodeMakeOperator(SLTOperatorTypeMultiply);
282 }
283 | "/=" {
284 $$ = sltNodeMakeOperator(SLTOperatorTypeDivide);
285 }
286 | "%=" {
287 $$ = sltNodeMakeOperator(SLTOperatorTypeModulo);
288 }
289 ;
290
291 logic_expression
292 : bit_logic_expression
293 | logic_expression logic_operator bit_logic_expression {
294 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
295 }
296 ;
297
298 logic_operator
299 : "&&" {
300 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalAnd);
301 }
302 | "||" {
303 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalOr);
304 }
305 ;
306
307 bit_logic_expression
308 : equality_expression
309 | bit_logic_expression bit_logic_operator equality_expression {
310 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
311 }
312 ;
313
314 bit_logic_operator
315 : '&' {
316 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseAnd);
317 }
318 | '|' {
319 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseOr);
320 }
321 | '^' {
322 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseXor);
323 }
324 ;
325
326 equality_expression
327 : arithmetic_expression
328 | equality_expression equality_operator arithmetic_expression {
329 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
330 }
331 ;
332
333 equality_operator
334 : "==" {
335 $$ = sltNodeMakeOperator(SLTOperatorTypeEqual);
336 }
337 | "!=" {
338 $$ = sltNodeMakeOperator(SLTOperatorTypeNotEqual);
339 }
340 | "<=" {
341 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThanEqual);
342 }
343 | ">=" {
344 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThanEqual);
345 }
346 | '<' {
347 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThan);
348 }
349 | '>' {
350 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThan);
351 }
352 ;
353
354 arithmetic_expression
355 : unary_expression
356 | arithmetic_expression '+' arithmetic_expression {
357 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeAdd), $1, $3);
358 }
359 | arithmetic_expression '-' arithmetic_expression {
360 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeSubtract), $1, $3);
361 }
362 | arithmetic_expression '*' arithmetic_expression {
363 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeMultiply), $1, $3);
364 }
365 | arithmetic_expression '/' arithmetic_expression {
366 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeDivide), $1, $3);
367 }
368 | arithmetic_expression '%' arithmetic_expression {
369 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeModulo), $1, $3);
370 }
371 ;
372
373 unary_expression
374 : postfix_expression
375 | '-' %prec U_MINUS unary_expression {
376 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeNegative), $2);
377 }
378 | '~' unary_expression {
379 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeBitwiseNot), $2);
380 }
381 | '!' unary_expression {
382 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeLogicalNot), $2);
383 }
384 ;
385
386 /*unary_operator
387 : "++" {
388 $$ = sltNodeMakeOperator(SLTOperatorTypePreIncrement);
389 }
390 | "--" {
391 $$ = sltNodeMakeOperator(SLTOperatorTypePreDecrement);
392 }
393 |'+' {
394 $$ = sltNodeMakeOperator(SLTOperatorTypePositive);
395 }
396 | '-' %prec U_MINUS {
397 $$ = sltNodeMakeOperator(SLTOperatorTypeNegative);
398 }
399 | '~' {
400 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseNot);
401 }
402 | '!' {
403 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalNot);
404 }
405 ;*/
406
407 postfix_expression
408 : primary_expression
409 | postfix_expression '[' expression ']' {
410 $$ = sltNodeMakeArrayAccess($1, $3);
411 }
412 | postfix_expression '(' optional_arguments ')' {
413 $$ = sltNodeMakeFunctionCall($1, $3);
414 }
415 | postfix_expression '.' IDENTIFIER {
416 $$ = sltNodeMakeTypeAccess($1, $3);
417 }
418 /*| postfix_expression postfix_unary_operator {
419 $$ = sltNodeMakeUnaryOperation($1, $2);
420 }*/
421 ;
422
423 /*postfix_unary_operator
424 : "++" {
425 $$ = sltNodeMakeOperator(SLTOperatorTypePostIncrement);
426 }
427 | "--" {
428 $$ = sltNodeMakeOperator(SLTOperatorTypePostDecrement);
429 }
430 ;*/
431
432 optional_arguments
433 : /* nothing */ {
434 $$ = NULL;
435 }
436 | arguments
437 ;
438
439 arguments
440 : expression {
441 $$ = sltNodeMakeSequence(NULL, sltNodeMakeArgument($1));
442 }
443 | arguments ',' expression {
444 $$ = sltNodeMakeSequence($1, sltNodeMakeArgument($3));
445 }
446 ;
447
448 primary_expression
449 : IDENTIFIER
450 | BOOL_FALSE
451 | BOOL_TRUE
452 | INT
453 | FLOAT
454 | STRING
455 | '(' expression ')' {
456 $$ = sltNodeMakeCompound($2);
457 }
458 ;
459
460 %%
461
462 void yyerror(const YYLTYPE *location, SLTNode **moduleOut, yyscan_t *scanner, const char *error) {
463 //void yyerror(const YYLTYPE *location, yyscan_t scanner, const char *error) {
464 printf("[ERROR] Line #%u, Characters %u: %s\n", /*yyget_lineno(scanner)*/location->first_line, /*yyget_column(scanner)*/location->first_column, error);
465 }