2 #define YYSTYPE SLTNode *
4 #include <sun/compiler/compiler_internal.h>
5 #include <sun/tree/node.h>
7 #include "sun_parser.h"
10 void yyerror(const YYLTYPE *location, SLTNode **moduleOut, yyscan_t *scanner, const char *error);
15 %parse-param {SLTNode **moduleOut}
16 %parse-param {yyscan_t *scanner}
18 %lex-param {yyscan_t *scanner}
31 %token RETURN "return"
34 %token BOOL_FALSE "false"
35 %token BOOL_TRUE "true"
37 %token ADD_ASSIGN "+="
38 %token SUBTRACT_ASSIGN "-="
39 %token MULTIPLY_ASSIGN "*="
40 %token DIVIDE_ASSIGN "/="
41 %token MOD_ASSIGN "%="
43 %token LOGICAL_AND "&&"
44 %token LOGICAL_OR "||"
51 /*%token INCREMENT "++"
52 %token DECREMENT "--"*/
60 /* https://en.cppreference.com/w/c/language/operator_precedence */
62 %right '=' "+=" "-=" "*=" "/=" "%=" /* etc... */
69 %left '<' "<=" '>' ">="
73 %right U_PLUS U_MINUS '!' '~'
74 /* %right "++" "--" "" */
75 /* %left "++" "--" "" */
82 : optional_statements {
83 $$ = sltNodeMakeModule($1);
88 /*optional_statements_or_definitions
92 | statements_or_definitions
95 statements_or_definitions
96 : function_definition {
97 $$ = sltNodeMakeSequence(NULL, $1);
100 $$ = sltNodeMakeSequence(NULL, $1);
102 | statements_or_definitions function_definition {
103 $$ = sltNodeMakeSequence($1, $2);
105 | statements_or_definitions statement {
106 $$ = sltNodeMakeSequence($1, $2);
111 : function_definition
113 | variable_definition ';'
117 : IDENTIFIER IDENTIFIER '(' optional_parameter_definitions ')' '{' optional_statements '}' {
118 $$ = sltNodeMakeFunctionDefinition($1, $2, $4, $7);
122 optional_parameter_definitions
126 | parameter_definitions
129 parameter_definitions
130 : parameter_definition {
131 $$ = sltNodeMakeSequence(NULL, $1);
133 | parameter_definitions ',' parameter_definition {
134 $$ = sltNodeMakeSequence($1, $3);
139 : IDENTIFIER IDENTIFIER {
140 $$ = sltNodeMakeFunctionParameterDefinition($1, $2);
145 : "struct" IDENTIFIER '{' type_member_definitions '}' {
146 $$ = sltNodeMakeTypeDefinition($2, $4);
150 type_member_definitions
151 : type_member_definition ';' {
152 $$ = sltNodeMakeSequence(NULL, $1);
154 | type_member_definitions type_member_definition ';' {
155 $$ = sltNodeMakeSequence($1, $2);
159 type_member_definition
160 : IDENTIFIER IDENTIFIER {
161 $$ = sltNodeMakeTypeMemberDefinition($1, $2);
166 : IDENTIFIER IDENTIFIER {
167 $$ = sltNodeMakeVariableDefinition($1, $2, NULL);
169 | IDENTIFIER IDENTIFIER '=' expression {
170 $$ = sltNodeMakeVariableDefinition($1, $2, $4);
184 $$ = sltNodeMakeSequence(NULL, $1);
188 | statements statement {
190 $$ = sltNodeMakeSequence($1, $2);
199 $$ = NULL; /* Otherwise this will pass up the ';' character */
201 | function_definition
202 | variable_definition ';'
203 | "return" optional_expression ';' {
204 $$ = sltNodeMakeReturn($2);
207 $$ = sltNodeMakeSleep();
209 | selection_statement
210 | iteration_statement
212 $$ = sltNodeMakeBreak();
215 | '{' optional_statements '}' {
217 $$ = sltNodeMakeStatementBlock($2);
225 : "if" '(' expression ')' statement {
226 $$ = sltNodeMakeSelection($3, $5, NULL);
228 | "if" '(' expression ')' statement "else" statement {
229 $$ = sltNodeMakeSelection($3, $5, $7);
234 : "for" '(' optional_expression_or_variable_definition ';' optional_expression ';' optional_expression ')' statement {
235 $$ = sltNodeMakeForIteration($3, $5, $7, $9);
237 | "while" '(' expression ')' statement {
238 $$ = sltNodeMakeWhileIteration($3, $5);
240 | "do" statement "while" '(' expression ')' ';' {
241 $$ = sltNodeMakeDoIteration($5, $2);
245 optional_expression_or_variable_definition
250 | variable_definition
261 : assignment_expression
264 assignment_expression
266 | unary_expression assignment_operator assignment_expression {
267 $$ = sltNodeMakeAssignment($1, ($2 == NULL) ? $3 : sltNodeMakeBinaryOperation($2, $1, $3));
276 $$ = sltNodeMakeOperator(SLTOperatorTypeAdd);
279 $$ = sltNodeMakeOperator(SLTOperatorTypeSubtract);
282 $$ = sltNodeMakeOperator(SLTOperatorTypeMultiply);
285 $$ = sltNodeMakeOperator(SLTOperatorTypeDivide);
288 $$ = sltNodeMakeOperator(SLTOperatorTypeModulo);
293 : bit_logic_expression
294 | logic_expression logic_operator bit_logic_expression {
295 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
301 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalAnd);
304 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalOr);
309 : equality_expression
310 | bit_logic_expression bit_logic_operator equality_expression {
311 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
317 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseAnd);
320 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseOr);
323 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseXor);
328 : arithmetic_expression
329 | equality_expression equality_operator arithmetic_expression {
330 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
336 $$ = sltNodeMakeOperator(SLTOperatorTypeEqual);
339 $$ = sltNodeMakeOperator(SLTOperatorTypeNotEqual);
342 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThanEqual);
345 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThanEqual);
348 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThan);
351 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThan);
355 arithmetic_expression
357 | arithmetic_expression '+' arithmetic_expression {
358 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeAdd), $1, $3);
360 | arithmetic_expression '-' arithmetic_expression {
361 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeSubtract), $1, $3);
363 | arithmetic_expression '*' arithmetic_expression {
364 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeMultiply), $1, $3);
366 | arithmetic_expression '/' arithmetic_expression {
367 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeDivide), $1, $3);
369 | arithmetic_expression '%' arithmetic_expression {
370 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeModulo), $1, $3);
376 | '-' %prec U_MINUS unary_expression {
377 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeNegative), $2);
379 | '~' unary_expression {
380 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeBitwiseNot), $2);
382 | '!' unary_expression {
383 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeLogicalNot), $2);
389 $$ = sltNodeMakeOperator(SLTOperatorTypePreIncrement);
392 $$ = sltNodeMakeOperator(SLTOperatorTypePreDecrement);
395 $$ = sltNodeMakeOperator(SLTOperatorTypePositive);
397 | '-' %prec U_MINUS {
398 $$ = sltNodeMakeOperator(SLTOperatorTypeNegative);
401 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseNot);
404 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalNot);
410 | postfix_expression '[' expression ']' {
411 $$ = sltNodeMakeArrayAccess($1, $3);
413 | postfix_expression '(' optional_arguments ')' {
414 $$ = sltNodeMakeFunctionCall($1, $3);
416 | postfix_expression '.' IDENTIFIER {
417 $$ = sltNodeMakeTypeAccess($1, $3);
419 /*| postfix_expression postfix_unary_operator {
420 $$ = sltNodeMakeUnaryOperation($1, $2);
424 /*postfix_unary_operator
426 $$ = sltNodeMakeOperator(SLTOperatorTypePostIncrement);
429 $$ = sltNodeMakeOperator(SLTOperatorTypePostDecrement);
442 $$ = sltNodeMakeSequence(NULL, sltNodeMakeArgument($1));
444 | arguments ',' expression {
445 $$ = sltNodeMakeSequence($1, sltNodeMakeArgument($3));
456 | '(' expression ')' {
457 $$ = sltNodeMakeCompound($2);
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);