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