]> git.bts.cx Git - sun.git/blob - aot/c_generator.c
Fixed syntax for definitions
[sun.git] / aot / c_generator.c
1 #include "c_generator.h"
2
3 #include "node_internal.h"
4 #include "node_type.h"
5
6 #include <stdbool.h>
7
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);
12
13 int c_generator_generate_module(FILE *output_file, Node *module) {
14 return c_generator_output_node(output_file, module);
15 }
16
17 static int c_generator_output_node(FILE *output_file, Node *node) {
18 if (node != NULL) {
19 switch (node->type) {
20 case NodeTypeUnknown:
21 return -1;
22
23 case NodeTypeIdentifier:
24 fprintf(output_file, "%s", node->string_value);
25 break;
26
27 case NodeTypeInteger:
28 fprintf(output_file, "%d", node->integer_value);
29 break;
30
31 case NodeTypeFloat:
32 fprintf(output_file, "%f", node->float_value);
33 break;
34
35 case NodeTypeString:
36 fprintf(output_file, "%s", node->string_value); // NB: Quotes are left in by lexer
37 break;
38
39 case NodeTypeCompound:
40 fprintf(output_file, "(");
41 c_generator_output_node(output_file, node->compound_subexpression);
42 fprintf(output_file, ")");
43 break;
44
45 case NodeTypeArgument:
46 c_generator_output_node(output_file, node->argument_expression);
47 break;
48
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, "]");
54 break;
55
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);
60 break;
61
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, ")");
67 break;
68
69
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);
74 } else {
75 c_generator_output_node(output_file, node->unary_operation_operand);
76 c_generator_output_node(output_file, node->unary_operation_operator);
77 }
78 break;
79
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);
84 break;
85
86 case NodeTypeOperator:
87 fprintf(output_file, "%s", c_generator_operator_string(node->operator_type));
88 break;
89
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);
94 break;
95
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);
105 break;
106
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, ")");
113 break;
114
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);
120 break;
121
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);
130 }
131 break;
132
133 case NodeTypeStatement:
134 c_generator_output_node(output_file, node->statement_contents);
135 fprintf(output_file, ";\n");
136 break;
137
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);
145 }
146 break;
147
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");
156 break;
157
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");
163 break;
164
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);
173 break;
174
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);
179 break;
180
181 case NodeTypeModule:
182 c_generator_output_sequence_node(output_file, node->module_definitions, NULL);
183 break;
184
185 case NodeTypeSequence:
186 fprintf(output_file, "{\n");
187 c_generator_output_sequence_node(output_file, node, NULL);
188 fprintf(output_file, "}");
189 break;
190 }
191 }
192
193 return 0;
194 }
195
196 static int c_generator_output_sequence_node(FILE *output_file, Node *node, const char *separator) {
197 if (node != NULL) {
198 if (node->type != NodeTypeSequence) {
199 c_generator_output_node(output_file, node);
200 } else {
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;
205 }
206 }
207 }
208
209 return 0;
210 }
211
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
216
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:
239 return true;
240
241 case OperatorTypePostIncrement:
242 case OperatorTypePostDecrement:
243 return false;
244 }
245 }
246
247 char *c_generator_operator_string(OperatorType operator_type) {
248 switch (operator_type) {
249 default:
250 case OperatorTypeUnknown:
251 return "<unknown>"; // FIXME, should never reach here
252
253 case OperatorTypeAdd:
254 return "+";
255
256 case OperatorTypeSubtract:
257 return "-";
258
259 case OperatorTypeMultiply:
260 return "*";
261
262 case OperatorTypeDivide:
263 return "/";
264
265 case OperatorTypeModulo:
266 return "%";
267
268 case OperatorTypePositive:
269 return "+";
270
271 case OperatorTypeNegative:
272 return "-";
273
274 case OperatorTypeBitwiseAnd:
275 return "&";
276
277 case OperatorTypeBitwiseOr:
278 return "|";
279
280 case OperatorTypeBitwiseXor:
281 return "^";
282
283 case OperatorTypeBitwiseNot:
284 return "~";
285
286 case OperatorTypeLogicalAnd:
287 return "&&";
288
289 case OperatorTypeLogicalOr:
290 return "||";
291
292 case OperatorTypeLogicalNot:
293 return "!";
294
295 case OperatorTypeEqual:
296 return "==";
297
298 case OperatorTypeNotEqual:
299 return "!=";
300
301 case OperatorTypeLessThan:
302 return "<";
303
304 case OperatorTypeLessThanEqual:
305 return "<=";
306
307 case OperatorTypeGreaterThan:
308 return ">";
309
310 case OperatorTypeGreaterThanEqual:
311 return ">=";
312
313 case OperatorTypePreIncrement:
314 case OperatorTypePostIncrement:
315 return "++";
316
317 case OperatorTypePreDecrement:
318 case OperatorTypePostDecrement:
319 return "--";
320 }
321 }