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 : type 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);
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);
167 $$ = sltNodeMakeVariableDefinition($1, $2, NULL);
169 | type IDENTIFIER '=' expression {
170 $$ = sltNodeMakeVariableDefinition($1, $2, $4);
176 $$ = $1;//sltNodeMakeTypeDefinition($1, 1);
178 | '[' type ':' INT ']' {
179 $$ = $2;//sltNodeMakeTypeDefinition($1, $2);
193 $$ = sltNodeMakeSequence(NULL, $1);
197 | statements statement {
199 $$ = sltNodeMakeSequence($1, $2);
208 $$ = NULL; /* Otherwise this will pass up the ';' character */
210 | function_definition
211 | variable_definition ';'
212 | "return" optional_expression ';' {
213 $$ = sltNodeMakeReturn($2);
216 $$ = sltNodeMakeSleep();
218 | selection_statement
219 | iteration_statement
221 $$ = sltNodeMakeBreak();
224 | '{' optional_statements '}' {
226 $$ = sltNodeMakeStatementBlock($2);
234 : "if" '(' expression ')' statement {
235 $$ = sltNodeMakeSelection($3, $5, NULL);
237 | "if" '(' expression ')' statement "else" statement {
238 $$ = sltNodeMakeSelection($3, $5, $7);
243 : "for" '(' optional_expression_or_variable_definition ';' optional_expression ';' optional_expression ')' statement {
244 $$ = sltNodeMakeForIteration($3, $5, $7, $9);
246 | "while" '(' expression ')' statement {
247 $$ = sltNodeMakeWhileIteration($3, $5);
249 | "do" statement "while" '(' expression ')' ';' {
250 $$ = sltNodeMakeDoIteration($5, $2);
254 optional_expression_or_variable_definition
259 | variable_definition
270 : assignment_expression
273 assignment_expression
275 | unary_expression assignment_operator assignment_expression {
276 $$ = sltNodeMakeAssignment($1, ($2 == NULL) ? $3 : sltNodeMakeBinaryOperation($2, $1, $3));
285 $$ = sltNodeMakeOperator(SLTOperatorTypeAdd);
288 $$ = sltNodeMakeOperator(SLTOperatorTypeSubtract);
291 $$ = sltNodeMakeOperator(SLTOperatorTypeMultiply);
294 $$ = sltNodeMakeOperator(SLTOperatorTypeDivide);
297 $$ = sltNodeMakeOperator(SLTOperatorTypeModulo);
302 : bit_logic_expression
303 | logic_expression logic_operator bit_logic_expression {
304 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
310 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalAnd);
313 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalOr);
318 : equality_expression
319 | bit_logic_expression bit_logic_operator equality_expression {
320 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
326 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseAnd);
329 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseOr);
332 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseXor);
337 : arithmetic_expression
338 | equality_expression equality_operator arithmetic_expression {
339 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
345 $$ = sltNodeMakeOperator(SLTOperatorTypeEqual);
348 $$ = sltNodeMakeOperator(SLTOperatorTypeNotEqual);
351 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThanEqual);
354 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThanEqual);
357 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThan);
360 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThan);
364 arithmetic_expression
366 | arithmetic_expression '+' arithmetic_expression {
367 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeAdd), $1, $3);
369 | arithmetic_expression '-' arithmetic_expression {
370 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeSubtract), $1, $3);
372 | arithmetic_expression '*' arithmetic_expression {
373 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeMultiply), $1, $3);
375 | arithmetic_expression '/' arithmetic_expression {
376 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeDivide), $1, $3);
378 | arithmetic_expression '%' arithmetic_expression {
379 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeModulo), $1, $3);
385 | '-' %prec U_MINUS unary_expression {
386 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeNegative), $2);
388 | '~' unary_expression {
389 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeBitwiseNot), $2);
391 | '!' unary_expression {
392 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeLogicalNot), $2);
398 $$ = sltNodeMakeOperator(SLTOperatorTypePreIncrement);
401 $$ = sltNodeMakeOperator(SLTOperatorTypePreDecrement);
404 $$ = sltNodeMakeOperator(SLTOperatorTypePositive);
406 | '-' %prec U_MINUS {
407 $$ = sltNodeMakeOperator(SLTOperatorTypeNegative);
410 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseNot);
413 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalNot);
419 | postfix_expression '[' expression ']' {
420 $$ = sltNodeMakeArrayAccess($1, $3);
422 | postfix_expression '(' optional_arguments ')' {
423 $$ = sltNodeMakeFunctionCall($1, $3);
425 | postfix_expression '.' IDENTIFIER {
426 $$ = sltNodeMakeMemberAccess($1, $3);
428 /*| postfix_expression postfix_unary_operator {
429 $$ = sltNodeMakeUnaryOperation($1, $2);
433 /*postfix_unary_operator
435 $$ = sltNodeMakeOperator(SLTOperatorTypePostIncrement);
438 $$ = sltNodeMakeOperator(SLTOperatorTypePostDecrement);
451 $$ = sltNodeMakeSequence(NULL, sltNodeMakeArgument($1));
453 | arguments ',' expression {
454 $$ = sltNodeMakeSequence($1, sltNodeMakeArgument($3));
465 | '(' expression ')' {
466 $$ = sltNodeMakeCompound($2);
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);