1 #include "c_generator.h"
3 #include "node_internal.h"
8 static int c_generator_output_node(FILE *output_file
, Node
*node
);
9 static int c_generator_output_sequence_node(FILE *output_file
, Node
*node
, const char *separator
);
10 static bool c_generator_is_prefix_unary_operator(OperatorType operator_type
);
11 static char *c_generator_operator_string(OperatorType operator_type
);
13 int c_generator_generate_module(FILE *output_file
, Node
*module
) {
14 return c_generator_output_node(output_file
, module
);
17 static int c_generator_output_node(FILE *output_file
, Node
*node
) {
23 case NodeTypeIdentifier
:
24 fprintf(output_file
, "%s", node
->string_value
);
28 fprintf(output_file
, "%d", node
->integer_value
);
32 fprintf(output_file
, "%f", node
->float_value
);
36 fprintf(output_file
, "%s", node
->string_value
); // NB: Quotes are left in by lexer
39 case NodeTypeCompound
:
40 fprintf(output_file
, "(");
41 c_generator_output_node(output_file
, node
->compound_subexpression
);
42 fprintf(output_file
, ")");
45 case NodeTypeArgument
:
46 c_generator_output_node(output_file
, node
->argument_expression
);
49 case NodeTypeArrayAccess
:
50 c_generator_output_node(output_file
, node
->array_access_array
);
51 fprintf(output_file
, "[");
52 c_generator_output_node(output_file
, node
->array_access_access
);
53 fprintf(output_file
, "]");
56 case NodeTypeTypeAccess
:
57 c_generator_output_node(output_file
, node
->type_access_type
);
58 fprintf(output_file
, ".");
59 c_generator_output_node(output_file
, node
->type_access_access
);
62 case NodeTypeFunctionCall
:
63 c_generator_output_node(output_file
, node
->function_call_function
);
64 fprintf(output_file
, "(");
65 c_generator_output_sequence_node(output_file
, node
->function_call_parameters
, ",");
66 fprintf(output_file
, ")");
70 case NodeTypeUnaryOperation
:
71 if (c_generator_is_prefix_unary_operator(node
->unary_operation_operator
->operator_type
)) {
72 c_generator_output_node(output_file
, node
->unary_operation_operator
);
73 c_generator_output_node(output_file
, node
->unary_operation_operand
);
75 c_generator_output_node(output_file
, node
->unary_operation_operand
);
76 c_generator_output_node(output_file
, node
->unary_operation_operator
);
80 case NodeTypeBinaryOperation
:
81 c_generator_output_node(output_file
, node
->binary_operation_lhs
);
82 c_generator_output_node(output_file
, node
->binary_operation_operator
);
83 c_generator_output_node(output_file
, node
->binary_operation_rhs
);
86 case NodeTypeOperator
:
87 fprintf(output_file
, "%s", c_generator_operator_string(node
->operator_type
));
90 case NodeTypeAssignment
:
91 c_generator_output_node(output_file
, node
->assignment_target
);
92 fprintf(output_file
, "=");
93 c_generator_output_node(output_file
, node
->assignment_source
);
96 case NodeTypeForIteration
:
97 fprintf(output_file
, "for (");
98 c_generator_output_node(output_file
, node
->iteration_initial_expression
);
99 fprintf(output_file
, ";");
100 c_generator_output_node(output_file
, node
->iteration_pre_valid_expression
);
101 fprintf(output_file
, ";");
102 c_generator_output_node(output_file
, node
->iteration_loop_expression
);
103 fprintf(output_file
, ") ");
104 c_generator_output_node(output_file
, node
->iteration_statement
);
107 case NodeTypeDoIteration
:
108 fprintf(output_file
, "do ");
109 c_generator_output_node(output_file
, node
->iteration_statement
);
110 fprintf(output_file
, " while (");
111 c_generator_output_node(output_file
, node
->iteration_post_valid_expression
);
112 fprintf(output_file
, ")");
115 case NodeTypeWhileIteration
:
116 fprintf(output_file
, "while (");
117 c_generator_output_node(output_file
, node
->iteration_pre_valid_expression
);
118 fprintf(output_file
, ") ");
119 c_generator_output_node(output_file
, node
->iteration_statement
);
122 case NodeTypeSelection
:
123 fprintf(output_file
, "if (");
124 c_generator_output_node(output_file
, node
->selection_valid_expression
);
125 fprintf(output_file
, ") ");
126 c_generator_output_node(output_file
, node
->selection_valid_statement
);
127 if (node
->selection_invalid_statement
!= NULL
) {
128 fprintf(output_file
, " else ");
129 c_generator_output_node(output_file
, node
->selection_invalid_statement
);
133 case NodeTypeStatement
:
134 c_generator_output_node(output_file
, node
->statement_contents
);
135 fprintf(output_file
, ";\n");
138 case NodeTypeVariableDefinition
:
139 c_generator_output_node(output_file
, node
->variable_definition_type
);
140 fprintf(output_file
, " ");
141 c_generator_output_node(output_file
, node
->variable_definition_identifier
);
142 if (node
->variable_definition_default_expression
) {
143 fprintf(output_file
, "=");
144 c_generator_output_node(output_file
, node
->variable_definition_default_expression
);
148 case NodeTypeTypeDefinition
:
149 fprintf(output_file
, "typedef struct ");
150 c_generator_output_node(output_file
, node
->type_definition_identifier
);
151 fprintf(output_file
, " {\n");
152 c_generator_output_sequence_node(output_file
, node
->type_definition_members
, NULL
);
153 fprintf(output_file
, "} ");
154 c_generator_output_node(output_file
, node
->type_definition_identifier
);
155 fprintf(output_file
, ";\n");
158 case NodeTypeTypeMemberDefinition
:
159 c_generator_output_node(output_file
, node
->type_member_definition_type
);
160 fprintf(output_file
, " ");
161 c_generator_output_node(output_file
, node
->type_member_definition_identifier
);
162 fprintf(output_file
, ";\n");
165 case NodeTypeFunctionDefinition
:
166 c_generator_output_node(output_file
, node
->function_definition_type
);
167 fprintf(output_file
, " ");
168 c_generator_output_node(output_file
, node
->function_definition_identifier
);
169 fprintf(output_file
, "(");
170 c_generator_output_sequence_node(output_file
, node
->function_definition_parameters
, ",");
171 fprintf(output_file
, ")");
172 c_generator_output_node(output_file
, node
->function_definition_body
);
175 case NodeTypeFunctionParameterDefinition
:
176 c_generator_output_node(output_file
, node
->function_parameter_definition_type
);
177 fprintf(output_file
, " ");
178 c_generator_output_node(output_file
, node
->function_parameter_definition_identifier
);
182 c_generator_output_sequence_node(output_file
, node
->module_definitions
, NULL
);
185 case NodeTypeSequence
:
186 fprintf(output_file
, "{\n");
187 c_generator_output_sequence_node(output_file
, node
, NULL
);
188 fprintf(output_file
, "}");
196 static int c_generator_output_sequence_node(FILE *output_file
, Node
*node
, const char *separator
) {
198 if (node
->type
!= NodeTypeSequence
) {
199 c_generator_output_node(output_file
, node
);
201 while (node
!= NULL
) {
202 c_generator_output_node(output_file
, node
->sequence_node
);
203 if (separator
!= NULL
&& node
->sequence_next
!= NULL
) fprintf(output_file
, "%s", separator
);
204 node
= node
->sequence_next
;
212 static bool c_generator_is_prefix_unary_operator(OperatorType operator_type
) {
213 switch (operator_type
) {
214 case OperatorTypeUnknown
:
215 // FIXME, should never reach here
217 case OperatorTypeAdd
:
218 case OperatorTypeSubtract
:
219 case OperatorTypeMultiply
:
220 case OperatorTypeDivide
:
221 case OperatorTypeModulo
:
222 case OperatorTypePositive
:
223 case OperatorTypeNegative
:
224 case OperatorTypeBitwiseAnd
:
225 case OperatorTypeBitwiseOr
:
226 case OperatorTypeBitwiseXor
:
227 case OperatorTypeBitwiseNot
:
228 case OperatorTypeLogicalAnd
:
229 case OperatorTypeLogicalOr
:
230 case OperatorTypeLogicalNot
:
231 case OperatorTypeEqual
:
232 case OperatorTypeNotEqual
:
233 case OperatorTypeLessThan
:
234 case OperatorTypeLessThanEqual
:
235 case OperatorTypeGreaterThan
:
236 case OperatorTypeGreaterThanEqual
:
237 case OperatorTypePreIncrement
:
238 case OperatorTypePreDecrement
:
241 case OperatorTypePostIncrement
:
242 case OperatorTypePostDecrement
:
247 char *c_generator_operator_string(OperatorType operator_type
) {
248 switch (operator_type
) {
250 case OperatorTypeUnknown
:
251 return "<unknown>"; // FIXME, should never reach here
253 case OperatorTypeAdd
:
256 case OperatorTypeSubtract
:
259 case OperatorTypeMultiply
:
262 case OperatorTypeDivide
:
265 case OperatorTypeModulo
:
268 case OperatorTypePositive
:
271 case OperatorTypeNegative
:
274 case OperatorTypeBitwiseAnd
:
277 case OperatorTypeBitwiseOr
:
280 case OperatorTypeBitwiseXor
:
283 case OperatorTypeBitwiseNot
:
286 case OperatorTypeLogicalAnd
:
289 case OperatorTypeLogicalOr
:
292 case OperatorTypeLogicalNot
:
295 case OperatorTypeEqual
:
298 case OperatorTypeNotEqual
:
301 case OperatorTypeLessThan
:
304 case OperatorTypeLessThanEqual
:
307 case OperatorTypeGreaterThan
:
310 case OperatorTypeGreaterThanEqual
:
313 case OperatorTypePreIncrement
:
314 case OperatorTypePostIncrement
:
317 case OperatorTypePreDecrement
:
318 case OperatorTypePostDecrement
: