]> git.bts.cx Git - sun.git/blob - runtime/src/sun/compiler/sun.y
Groundwork for arrays
[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 {
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 : function_definition {
97 $$ = sltNodeMakeSequence(NULL, $1);
98 }
99 | statement {
100 $$ = sltNodeMakeSequence(NULL, $1);
101 }
102 | statements_or_definitions function_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 : type 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 : type 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 : type IDENTIFIER {
167 $$ = sltNodeMakeVariableDefinition($1, $2, NULL);
168 }
169 | type IDENTIFIER '=' expression {
170 $$ = sltNodeMakeVariableDefinition($1, $2, $4);
171 }
172 ;
173
174 type
175 : IDENTIFIER {
176 $$ = $1;//sltNodeMakeTypeDefinition($1, 1);
177 }
178 | '[' type ':' INT ']' {
179 $$ = $2;//sltNodeMakeTypeDefinition($1, $2);
180 }
181 ;
182
183 optional_statements
184 : /* nothing */ {
185 $$ = NULL;
186 }
187 | statements
188 ;
189
190 statements
191 : statement {
192 if ($1 != NULL) {
193 $$ = sltNodeMakeSequence(NULL, $1);
194 }
195
196 }
197 | statements statement {
198 if ($2 != NULL) {
199 $$ = sltNodeMakeSequence($1, $2);
200 } else {
201 $$ = $1;
202 }
203 }
204 ;
205
206 statement
207 : ';' {
208 $$ = NULL; /* Otherwise this will pass up the ';' character */
209 }
210 | function_definition
211 | variable_definition ';'
212 | "return" optional_expression ';' {
213 $$ = sltNodeMakeReturn($2);
214 }
215 | "sleep" ';' {
216 $$ = sltNodeMakeSleep();
217 }
218 | selection_statement
219 | iteration_statement
220 | "break" ';' {
221 $$ = sltNodeMakeBreak();
222 }
223 | expression ';'
224 | '{' optional_statements '}' {
225 if ($2 != NULL) {
226 $$ = sltNodeMakeStatementBlock($2);
227 } else {
228 $$ = NULL;
229 }
230 }
231 ;
232
233 selection_statement
234 : "if" '(' expression ')' statement {
235 $$ = sltNodeMakeSelection($3, $5, NULL);
236 }
237 | "if" '(' expression ')' statement "else" statement {
238 $$ = sltNodeMakeSelection($3, $5, $7);
239 }
240 ;
241
242 iteration_statement
243 : "for" '(' optional_expression_or_variable_definition ';' optional_expression ';' optional_expression ')' statement {
244 $$ = sltNodeMakeForIteration($3, $5, $7, $9);
245 }
246 | "while" '(' expression ')' statement {
247 $$ = sltNodeMakeWhileIteration($3, $5);
248 }
249 | "do" statement "while" '(' expression ')' ';' {
250 $$ = sltNodeMakeDoIteration($5, $2);
251 }
252 ;
253
254 optional_expression_or_variable_definition
255 : /* nothing */ {
256 $$ = NULL;
257 }
258 | expression
259 | variable_definition
260 ;
261
262 optional_expression
263 : /* nothing */ {
264 $$ = NULL;
265 }
266 | expression
267 ;
268
269 expression
270 : assignment_expression
271 ;
272
273 assignment_expression
274 : logic_expression
275 | unary_expression assignment_operator assignment_expression {
276 $$ = sltNodeMakeAssignment($1, ($2 == NULL) ? $3 : sltNodeMakeBinaryOperation($2, $1, $3));
277 }
278 ;
279
280 assignment_operator
281 : '=' {
282 $$ = NULL;
283 }
284 | "+=" {
285 $$ = sltNodeMakeOperator(SLTOperatorTypeAdd);
286 }
287 | "-=" {
288 $$ = sltNodeMakeOperator(SLTOperatorTypeSubtract);
289 }
290 | "*=" {
291 $$ = sltNodeMakeOperator(SLTOperatorTypeMultiply);
292 }
293 | "/=" {
294 $$ = sltNodeMakeOperator(SLTOperatorTypeDivide);
295 }
296 | "%=" {
297 $$ = sltNodeMakeOperator(SLTOperatorTypeModulo);
298 }
299 ;
300
301 logic_expression
302 : bit_logic_expression
303 | logic_expression logic_operator bit_logic_expression {
304 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
305 }
306 ;
307
308 logic_operator
309 : "&&" {
310 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalAnd);
311 }
312 | "||" {
313 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalOr);
314 }
315 ;
316
317 bit_logic_expression
318 : equality_expression
319 | bit_logic_expression bit_logic_operator equality_expression {
320 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
321 }
322 ;
323
324 bit_logic_operator
325 : '&' {
326 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseAnd);
327 }
328 | '|' {
329 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseOr);
330 }
331 | '^' {
332 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseXor);
333 }
334 ;
335
336 equality_expression
337 : arithmetic_expression
338 | equality_expression equality_operator arithmetic_expression {
339 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
340 }
341 ;
342
343 equality_operator
344 : "==" {
345 $$ = sltNodeMakeOperator(SLTOperatorTypeEqual);
346 }
347 | "!=" {
348 $$ = sltNodeMakeOperator(SLTOperatorTypeNotEqual);
349 }
350 | "<=" {
351 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThanEqual);
352 }
353 | ">=" {
354 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThanEqual);
355 }
356 | '<' {
357 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThan);
358 }
359 | '>' {
360 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThan);
361 }
362 ;
363
364 arithmetic_expression
365 : unary_expression
366 | arithmetic_expression '+' arithmetic_expression {
367 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeAdd), $1, $3);
368 }
369 | arithmetic_expression '-' arithmetic_expression {
370 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeSubtract), $1, $3);
371 }
372 | arithmetic_expression '*' arithmetic_expression {
373 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeMultiply), $1, $3);
374 }
375 | arithmetic_expression '/' arithmetic_expression {
376 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeDivide), $1, $3);
377 }
378 | arithmetic_expression '%' arithmetic_expression {
379 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeModulo), $1, $3);
380 }
381 ;
382
383 unary_expression
384 : postfix_expression
385 | '-' %prec U_MINUS unary_expression {
386 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeNegative), $2);
387 }
388 | '~' unary_expression {
389 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeBitwiseNot), $2);
390 }
391 | '!' unary_expression {
392 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeLogicalNot), $2);
393 }
394 ;
395
396 /*unary_operator
397 : "++" {
398 $$ = sltNodeMakeOperator(SLTOperatorTypePreIncrement);
399 }
400 | "--" {
401 $$ = sltNodeMakeOperator(SLTOperatorTypePreDecrement);
402 }
403 |'+' {
404 $$ = sltNodeMakeOperator(SLTOperatorTypePositive);
405 }
406 | '-' %prec U_MINUS {
407 $$ = sltNodeMakeOperator(SLTOperatorTypeNegative);
408 }
409 | '~' {
410 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseNot);
411 }
412 | '!' {
413 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalNot);
414 }
415 ;*/
416
417 postfix_expression
418 : primary_expression
419 | postfix_expression '[' expression ']' {
420 $$ = sltNodeMakeArrayAccess($1, $3);
421 }
422 | postfix_expression '(' optional_arguments ')' {
423 $$ = sltNodeMakeFunctionCall($1, $3);
424 }
425 | postfix_expression '.' IDENTIFIER {
426 $$ = sltNodeMakeMemberAccess($1, $3);
427 }
428 /*| postfix_expression postfix_unary_operator {
429 $$ = sltNodeMakeUnaryOperation($1, $2);
430 }*/
431 ;
432
433 /*postfix_unary_operator
434 : "++" {
435 $$ = sltNodeMakeOperator(SLTOperatorTypePostIncrement);
436 }
437 | "--" {
438 $$ = sltNodeMakeOperator(SLTOperatorTypePostDecrement);
439 }
440 ;*/
441
442 optional_arguments
443 : /* nothing */ {
444 $$ = NULL;
445 }
446 | arguments
447 ;
448
449 arguments
450 : expression {
451 $$ = sltNodeMakeSequence(NULL, sltNodeMakeArgument($1));
452 }
453 | arguments ',' expression {
454 $$ = sltNodeMakeSequence($1, sltNodeMakeArgument($3));
455 }
456 ;
457
458 primary_expression
459 : IDENTIFIER
460 | BOOL_FALSE
461 | BOOL_TRUE
462 | INT
463 | FLOAT
464 | STRING
465 | '(' expression ')' {
466 $$ = sltNodeMakeCompound($2);
467 }
468 ;
469
470 %%
471
472 void yyerror(const YYLTYPE *location, SLTNode **moduleOut, yyscan_t *scanner, const char *error) {
473 //void yyerror(const YYLTYPE *location, yyscan_t scanner, const char *error) {
474 printf("[ERROR] Line #%u, Characters %u: %s\n", /*yyget_lineno(scanner)*/location->first_line, /*yyget_column(scanner)*/location->first_column, error);
475 }