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_or_definitions {
83 $$ = sltNodeMakeModule($1);
88 optional_statements_or_definitions
92 | statements_or_definitions
95 statements_or_definitions
97 $$ = sltNodeMakeSequence(NULL, $1);
100 $$ = sltNodeMakeSequence(NULL, $1);
102 | statements_or_definitions definition {
103 $$ = sltNodeMakeSequence($1, $2);
105 | statements_or_definitions statement {
106 $$ = sltNodeMakeSequence($1, $2);
111 : function_definition
112 /*| type_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 | variable_definition ';'
202 | "return" optional_expression ';' {
203 $$ = sltNodeMakeReturn($2);
206 $$ = sltNodeMakeSleep();
208 | selection_statement
209 | iteration_statement
211 $$ = sltNodeMakeBreak();
214 | '{' optional_statements '}' {
216 $$ = sltNodeMakeStatementBlock($2);
224 : "if" '(' expression ')' statement {
225 $$ = sltNodeMakeSelection($3, $5, NULL);
227 | "if" '(' expression ')' statement "else" statement {
228 $$ = sltNodeMakeSelection($3, $5, $7);
233 : "for" '(' optional_expression_or_variable_definition ';' optional_expression ';' optional_expression ')' statement {
234 $$ = sltNodeMakeForIteration($3, $5, $7, $9);
236 | "while" '(' expression ')' statement {
237 $$ = sltNodeMakeWhileIteration($3, $5);
239 | "do" statement "while" '(' expression ')' ';' {
240 $$ = sltNodeMakeDoIteration($5, $2);
244 optional_expression_or_variable_definition
249 | variable_definition
260 : assignment_expression
263 assignment_expression
265 | unary_expression assignment_operator assignment_expression {
266 $$ = sltNodeMakeAssignment($1, ($2 == NULL) ? $3 : sltNodeMakeBinaryOperation($2, $1, $3));
275 $$ = sltNodeMakeOperator(SLTOperatorTypeAdd);
278 $$ = sltNodeMakeOperator(SLTOperatorTypeSubtract);
281 $$ = sltNodeMakeOperator(SLTOperatorTypeMultiply);
284 $$ = sltNodeMakeOperator(SLTOperatorTypeDivide);
287 $$ = sltNodeMakeOperator(SLTOperatorTypeModulo);
292 : bit_logic_expression
293 | logic_expression logic_operator bit_logic_expression {
294 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
300 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalAnd);
303 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalOr);
308 : equality_expression
309 | bit_logic_expression bit_logic_operator equality_expression {
310 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
316 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseAnd);
319 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseOr);
322 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseXor);
327 : arithmetic_expression
328 | equality_expression equality_operator arithmetic_expression {
329 $$ = sltNodeMakeBinaryOperation($2, $1, $3);
335 $$ = sltNodeMakeOperator(SLTOperatorTypeEqual);
338 $$ = sltNodeMakeOperator(SLTOperatorTypeNotEqual);
341 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThanEqual);
344 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThanEqual);
347 $$ = sltNodeMakeOperator(SLTOperatorTypeLessThan);
350 $$ = sltNodeMakeOperator(SLTOperatorTypeGreaterThan);
354 arithmetic_expression
356 | arithmetic_expression '+' arithmetic_expression {
357 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeAdd), $1, $3);
359 | arithmetic_expression '-' arithmetic_expression {
360 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeSubtract), $1, $3);
362 | arithmetic_expression '*' arithmetic_expression {
363 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeMultiply), $1, $3);
365 | arithmetic_expression '/' arithmetic_expression {
366 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeDivide), $1, $3);
368 | arithmetic_expression '%' arithmetic_expression {
369 $$ = sltNodeMakeBinaryOperation(sltNodeMakeOperator(SLTOperatorTypeModulo), $1, $3);
375 | '-' %prec U_MINUS unary_expression {
376 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeNegative), $2);
378 | '~' unary_expression {
379 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeBitwiseNot), $2);
381 | '!' unary_expression {
382 $$ = sltNodeMakeUnaryOperation(sltNodeMakeOperator(SLTOperatorTypeLogicalNot), $2);
388 $$ = sltNodeMakeOperator(SLTOperatorTypePreIncrement);
391 $$ = sltNodeMakeOperator(SLTOperatorTypePreDecrement);
394 $$ = sltNodeMakeOperator(SLTOperatorTypePositive);
396 | '-' %prec U_MINUS {
397 $$ = sltNodeMakeOperator(SLTOperatorTypeNegative);
400 $$ = sltNodeMakeOperator(SLTOperatorTypeBitwiseNot);
403 $$ = sltNodeMakeOperator(SLTOperatorTypeLogicalNot);
409 | postfix_expression '[' expression ']' {
410 $$ = sltNodeMakeArrayAccess($1, $3);
412 | postfix_expression '(' optional_arguments ')' {
413 $$ = sltNodeMakeFunctionCall($1, $3);
415 | postfix_expression '.' IDENTIFIER {
416 $$ = sltNodeMakeTypeAccess($1, $3);
418 /*| postfix_expression postfix_unary_operator {
419 $$ = sltNodeMakeUnaryOperation($1, $2);
423 /*postfix_unary_operator
425 $$ = sltNodeMakeOperator(SLTOperatorTypePostIncrement);
428 $$ = sltNodeMakeOperator(SLTOperatorTypePostDecrement);
441 $$ = sltNodeMakeSequence(NULL, sltNodeMakeArgument($1));
443 | arguments ',' expression {
444 $$ = sltNodeMakeSequence($1, sltNodeMakeArgument($3));
455 | '(' expression ')' {
456 $$ = sltNodeMakeCompound($2);
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);