1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB and Kjell Winblad 2019. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * %CopyrightEnd%
19 */
20
21 /*
22 * Author: Kjell Winblad
23 */
24
25 #include "ycf_yield_fun.h"
26 #include "ycf_utils.h"
27 #include "ycf_node.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 void print_symbol_code(ycf_symbol* s);
33 ycf_parse_result parse_exp_statement(ycf_symbol* symbols);
34
fail_parse_result()35 ycf_parse_result fail_parse_result(){
36 ycf_parse_result r;
37 r.success = false;
38 r.next_symbol = NULL;
39 r.result = NULL;
40 return r;
41 }
42
success_parse_result(ycf_symbol * next,ycf_node * result)43 ycf_parse_result success_parse_result(ycf_symbol* next,
44 ycf_node* result){
45 ycf_parse_result r;
46 r.success = true;
47 r.next_symbol = next;
48 r.result = result;
49 return r;
50 }
51
52 typedef struct {
53 ycf_node_list list;
54 ycf_symbol* next_symbol;
55 } parse_list_res;
56
57 typedef struct {
58 ycf_symbol_list list;
59 ycf_symbol* next_symbol;
60 } parse_symbol_list_res;
61
62
parse_list_generic(int number_of_parsers,ycf_parse_result (* parsers[])(ycf_symbol *),ycf_symbol * symbols,bool use_break_symbol,int nr_of_break_symbols,ycf_symbol_type break_symbols[])63 parse_list_res parse_list_generic(int number_of_parsers,
64 ycf_parse_result (*parsers[])(ycf_symbol*),
65 ycf_symbol* symbols,
66 bool use_break_symbol,
67 int nr_of_break_symbols,
68 ycf_symbol_type break_symbols[]){
69 parse_list_res r;
70 ycf_symbol* current = symbols;
71 int i;
72 r.list = ycf_node_list_empty();
73 while(current != NULL){
74 if(use_break_symbol){
75 bool is_break = false;
76 for(int i = 0; i < nr_of_break_symbols; i++){
77 if(break_symbols[i] == current->type){
78 is_break = true;
79 }
80 }
81 if(is_break){
82 break;
83 }
84 }
85 ycf_symbol* prev_current_symbol = current;
86 for(i = 0; i < number_of_parsers; i++){
87 ycf_parse_result res = parsers[i](current);
88 if(res.success){
89 ycf_node_list_append(&r.list, res.result);
90 current = res.next_symbol;
91 break;
92 }
93 }
94 if(prev_current_symbol == current){
95 break;
96 }
97 }
98 r.next_symbol = current;
99 return r;
100 }
101
parse_list(int number_of_parsers,ycf_parse_result (* parsers[])(ycf_symbol *),ycf_symbol * symbols)102 parse_list_res parse_list(int number_of_parsers,
103 ycf_parse_result (*parsers[])(ycf_symbol*),
104 ycf_symbol* symbols){
105 return parse_list_generic(number_of_parsers,
106 parsers,
107 symbols,
108 false,
109 0,
110 NULL);
111
112 }
113
parse_list_break_symbol(int number_of_parsers,ycf_parse_result (* parsers[])(ycf_symbol *),ycf_symbol * symbols,ycf_symbol_type break_symbol)114 parse_list_res parse_list_break_symbol(int number_of_parsers,
115 ycf_parse_result (*parsers[])(ycf_symbol*),
116 ycf_symbol* symbols,
117 ycf_symbol_type break_symbol){
118 ycf_symbol_type break_s[1] = {break_symbol};
119 return parse_list_generic(number_of_parsers,
120 parsers,
121 symbols,
122 true,
123 1,
124 break_s);
125
126 }
127
parse_list_break_symbols(int number_of_parsers,ycf_parse_result (* parsers[])(ycf_symbol *),ycf_symbol * symbols,int nr_of_break_symbols,ycf_symbol_type break_symbols[])128 parse_list_res parse_list_break_symbols(int number_of_parsers,
129 ycf_parse_result (*parsers[])(ycf_symbol*),
130 ycf_symbol* symbols,
131 int nr_of_break_symbols,
132 ycf_symbol_type break_symbols[]){
133 return parse_list_generic(number_of_parsers,
134 parsers,
135 symbols,
136 true,
137 nr_of_break_symbols,
138 break_symbols);
139
140 }
141
142
ycf_node_defenition_new(ycf_symbol_list type_spec,ycf_symbol * id,ycf_node_list array_brackets,ycf_symbol * end)143 ycf_node* ycf_node_defenition_new(ycf_symbol_list type_spec,
144 ycf_symbol* id,
145 ycf_node_list array_brackets,
146 ycf_symbol* end){
147 ycf_node* n = ycf_malloc(sizeof(ycf_node));
148 n->type = ycf_node_type_variable_definition;
149 n->next = NULL;
150 n->u.definition.identifier = ycf_symbol_copy(id);
151 n->u.definition.type_specifiers = type_spec;
152 n->u.definition.array_brackets = array_brackets;
153 n->u.definition.end = ycf_symbol_copy(end);
154 return n;
155 }
156
ycf_node_defenition_with_init_new(ycf_node_definition def,ycf_symbol_list expression,ycf_symbol * end)157 ycf_node* ycf_node_defenition_with_init_new(ycf_node_definition def,
158 ycf_symbol_list expression,
159 ycf_symbol* end){
160 ycf_node* n = ycf_malloc(sizeof(ycf_node));
161 n->type = ycf_node_type_variable_definition_init;
162 n->next = NULL;
163 n->u.definition_init.definition = def;
164 n->u.definition_init.initializer_expression = expression;
165 n->u.definition_init.end = ycf_symbol_copy(end);
166 return n;
167 }
168
ycf_node_c_file_new(ycf_node_list content)169 ycf_node* ycf_node_c_file_new(ycf_node_list content){
170 ycf_node* n = ycf_malloc(sizeof(ycf_node));
171 n->type = ycf_node_type_c_file;
172 n->next = NULL;
173 n->u.c_file.content = content;
174 return n;
175 }
176
177
ycf_node_function_def_new(ycf_node_definition def_node,ycf_node_list parameters,bool ignore_param_ending,ycf_symbol_list end)178 ycf_node* ycf_node_function_def_new(ycf_node_definition def_node,
179 ycf_node_list parameters,
180 bool ignore_param_ending,
181 ycf_symbol_list end){
182 ycf_node* n = ycf_malloc(sizeof(ycf_node));
183 n->type = ycf_node_type_function_declaration;
184 n->next = NULL;
185 n->u.function_definition.definition = def_node;
186 n->u.function_definition.parameters = parameters;
187 n->u.function_definition.ignore_param_ending = ignore_param_ending;
188 n->u.function_definition.end = end;
189 return n;
190 }
191
ycf_node_yield_new(ycf_symbol * yield_symbol,ycf_symbol * end)192 ycf_node* ycf_node_yield_new(ycf_symbol* yield_symbol,
193 ycf_symbol* end){
194 ycf_node* n = ycf_malloc(sizeof(ycf_node));
195 n->type = ycf_node_type_yield;
196 n->next = NULL;
197 n->u.yield.yield_symbol = ycf_symbol_copy(yield_symbol);
198 n->u.yield.end_symbol = ycf_symbol_copy(end);
199 return n;
200 }
201
202
203
ycf_node_consume_reds_new(ycf_symbol * consume_reds_symbol,ycf_node_parentheses_expression nr_of_reds_expression,ycf_symbol * end_symbol)204 ycf_node* ycf_node_consume_reds_new(ycf_symbol* consume_reds_symbol,
205 ycf_node_parentheses_expression nr_of_reds_expression,
206 ycf_symbol* end_symbol){
207 ycf_node* n = ycf_malloc(sizeof(ycf_node));
208 n->type = ycf_node_type_consume_reds;
209 n->next = NULL;
210 n->u.consume_reds.consume_reds_symbol = ycf_symbol_copy(consume_reds_symbol);
211 n->u.consume_reds.nr_of_reds_expression = nr_of_reds_expression;
212 n->u.consume_reds.end_symbol = ycf_symbol_copy(end_symbol);
213 return n;
214 }
215
216
ycf_node_other_new(ycf_symbol * other_symbol)217 ycf_node* ycf_node_other_new(ycf_symbol* other_symbol){
218 ycf_node* n = ycf_malloc(sizeof(ycf_node));
219 n->type = ycf_node_type_other;
220 n->next = NULL;
221 n->u.other.what = ycf_symbol_copy(other_symbol);
222 return n;
223 }
224
ycf_node_scope_new(ycf_symbol * start,ycf_node_list declaration_nodes,ycf_node_list other_nodes,ycf_symbol * end)225 ycf_node* ycf_node_scope_new(ycf_symbol* start,
226 ycf_node_list declaration_nodes,
227 ycf_node_list other_nodes,
228 ycf_symbol* end){
229 ycf_node* n = ycf_malloc(sizeof(ycf_node));
230 n->type = ycf_node_type_code_scope;
231 n->next = NULL;
232 n->u.code_scope.start = ycf_symbol_copy(start);
233 n->u.code_scope.definition_nodes = declaration_nodes;
234 n->u.code_scope.other_nodes = other_nodes;
235 n->u.code_scope.end = ycf_symbol_copy(end);
236 return n;
237 }
238
ycf_node_function_new(ycf_node_function_definition fun_def,ycf_node_code_scope body)239 ycf_node* ycf_node_function_new(ycf_node_function_definition fun_def,
240 ycf_node_code_scope body){
241 ycf_node* n = ycf_malloc(sizeof(ycf_node));
242 n->type = ycf_node_type_function_definition;
243 n->next = NULL;
244 n->u.function.definition = fun_def;
245 n->u.function.body = body;
246 return n;
247 }
248
ycf_node_function_call_new(ycf_symbol_list neg_symbols,ycf_symbol * ident,ycf_symbol * paran_start,ycf_node_list parameters,ycf_symbol * paran_end)249 ycf_node* ycf_node_function_call_new(ycf_symbol_list neg_symbols,
250 ycf_symbol* ident,
251 ycf_symbol* paran_start,
252 ycf_node_list parameters,
253 ycf_symbol* paran_end){
254 ycf_node* n = ycf_malloc(sizeof(ycf_node));
255 n->type = ycf_node_type_function_call;
256 n->next = NULL;
257 n->u.function_call.neg_symbols = neg_symbols;
258 n->u.function_call.identifier = ident;
259 n->u.function_call.start_symbol = paran_start;
260 n->u.function_call.parameter_expressions = parameters;
261 n->u.function_call.end_symbol = paran_end;
262 return n;
263 }
264
ycf_node_paran_expression_new(ycf_symbol * start,ycf_node_expression expr,ycf_symbol * end)265 ycf_node* ycf_node_paran_expression_new(ycf_symbol* start, ycf_node_expression expr, ycf_symbol* end){
266 ycf_node* n = ycf_malloc(sizeof(ycf_node));
267 n->type = ycf_node_type_parentheses_expression;
268 n->next = NULL;
269 n->u.parentheses_expression.start_symbol = start;
270 n->u.parentheses_expression.content = expr;
271 n->u.parentheses_expression.end_symbol = end;
272 return n;
273 }
274
ycf_node_expression_new(ycf_node_list expr)275 ycf_node* ycf_node_expression_new(ycf_node_list expr){
276 ycf_node* n = ycf_malloc(sizeof(ycf_node));
277 n->type = ycf_node_type_expression;
278 n->next = NULL;
279 n->u.expression.content = expr;
280 n->u.expression.end_symbol = NULL;
281 return n;
282 }
283
ycf_node_statement_new(ycf_node * expression,ycf_symbol * end_symbol)284 ycf_node* ycf_node_statement_new(ycf_node* expression,
285 ycf_symbol* end_symbol){
286 ycf_node* n = ycf_malloc(sizeof(ycf_node));
287 n->type = ycf_node_type_statement;
288 n->next = NULL;
289 n->u.statement.expression = expression;
290 n->u.statement.end_symbol = end_symbol;
291 return n;
292 }
293
ycf_node_while_new(ycf_symbol * while_word,ycf_node_parentheses_expression expression,ycf_node * statement)294 ycf_node* ycf_node_while_new(ycf_symbol* while_word,
295 ycf_node_parentheses_expression expression,
296 ycf_node* statement){
297 ycf_node* n = ycf_malloc(sizeof(ycf_node));
298 n->next = NULL;
299 n->type = ycf_node_type_while;
300 n->u.while_n.while_word = while_word;
301 n->u.while_n.expression = expression;
302 n->u.while_n.statement = statement;
303 return n;
304 }
305
306
307
ycf_node_do_while_new(ycf_symbol * do_word,ycf_node * statm,ycf_symbol * while_word,ycf_node_parentheses_expression expression,ycf_symbol * end)308 ycf_node* ycf_node_do_while_new(ycf_symbol* do_word,
309 ycf_node* statm,
310 ycf_symbol* while_word,
311 ycf_node_parentheses_expression expression,
312 ycf_symbol* end){
313 ycf_node* n = ycf_malloc(sizeof(ycf_node));
314 n->next = NULL;
315 n->type = ycf_node_type_do_while;
316 n->u.do_while.do_word = do_word;
317 n->u.do_while.statement = statm;
318 n->u.do_while.while_word = while_word;
319 n->u.do_while.expression = expression;
320 n->u.do_while.end = end;
321 return n;
322 }
323
ycf_node_for_new(ycf_symbol * for_word,ycf_symbol * start_paran,struct ycf_node * init,ycf_node * stop_cond,ycf_symbol * stop_cond_end,ycf_node * end_exp,ycf_symbol * end_paran,ycf_node * statem)324 ycf_node* ycf_node_for_new(ycf_symbol* for_word,
325 ycf_symbol* start_paran,
326 struct ycf_node* init,
327 ycf_node* stop_cond,
328 ycf_symbol* stop_cond_end,
329 ycf_node* end_exp,
330 ycf_symbol* end_paran,
331 ycf_node* statem){
332 ycf_node* n = ycf_malloc(sizeof(ycf_node));
333 n->next = NULL;
334 n->type = ycf_node_type_for;
335 n->u.for_n.for_word = for_word;
336 n->u.for_n.start_parentheses = start_paran;
337 n->u.for_n.init = init;
338 n->u.for_n.stop_cond = stop_cond;
339 n->u.for_n.stop_cond_end = stop_cond_end;
340 n->u.for_n.end_exp = end_exp;
341 n->u.for_n.end_parentheses = end_paran;
342 n->u.for_n.statement = statem;
343 return n;
344 }
345
ycf_node_switch_new(ycf_symbol * switch_word,ycf_node_parentheses_expression expression,ycf_node_code_scope scope)346 ycf_node* ycf_node_switch_new(ycf_symbol* switch_word,
347 ycf_node_parentheses_expression expression,
348 ycf_node_code_scope scope){
349 ycf_node* n = ycf_malloc(sizeof(ycf_node));
350 n->next = NULL;
351 n->type = ycf_node_type_switch;
352 n->u.switch_n.switch_word = switch_word;
353 n->u.switch_n.expression = expression;
354 n->u.switch_n.scope = scope;
355 return n;
356 }
357
ycf_node_if_new(ycf_symbol * if_word,ycf_node_parentheses_expression expression,struct ycf_node * if_statem)358 ycf_node* ycf_node_if_new(ycf_symbol* if_word,
359 ycf_node_parentheses_expression expression,
360 struct ycf_node* if_statem){
361 ycf_node* n = ycf_malloc(sizeof(ycf_node));
362 n->next = NULL;
363 n->type = ycf_node_type_if;
364 n->u.if_n.if_word = if_word;
365 n->u.if_n.expression = expression;
366 n->u.if_n.if_statement = if_statem;
367 return n;
368 }
369
ycf_node_if_else_new(ycf_node_if if_n,ycf_symbol * else_word,struct ycf_node * else_statement)370 ycf_node* ycf_node_if_else_new(ycf_node_if if_n,
371 ycf_symbol* else_word,
372 struct ycf_node* else_statement){
373 ycf_node* n = ycf_malloc(sizeof(ycf_node));
374 n->next = NULL;
375 n->type = ycf_node_type_if_else;
376 n->u.if_else.if_part = if_n;
377 n->u.if_else.else_word = else_word;
378 n->u.if_else.else_statement = else_statement;
379 return n;
380 }
381
382
ycf_node_fun_call_assignment_new(ycf_node_expression left_side,ycf_symbol * assignment_symbol,ycf_node_function_call fun_call)383 ycf_node* ycf_node_fun_call_assignment_new(ycf_node_expression left_side,
384 ycf_symbol* assignment_symbol,
385 ycf_node_function_call fun_call){
386 ycf_node* n = ycf_malloc(sizeof(ycf_node));
387 n->next = NULL;
388 n->type = ycf_node_type_assignment_function_call;
389 n->u.function_call_assignment.left_side = left_side;
390 n->u.function_call_assignment.assignment_symbol = assignment_symbol;
391 n->u.function_call_assignment.fun_call = fun_call;
392 return n;
393 }
394
ycf_node_assignment_new(ycf_node_expression left_side,ycf_symbol * assignment_symbol,ycf_node_expression right_side,ycf_symbol * end)395 ycf_node* ycf_node_assignment_new(ycf_node_expression left_side,
396 ycf_symbol* assignment_symbol,
397 ycf_node_expression right_side,
398 ycf_symbol* end){
399 ycf_node* n = ycf_malloc(sizeof(ycf_node));
400 n->next = NULL;
401 n->type = ycf_node_type_assignment;
402 n->u.a.left_side = left_side;
403 n->u.a.right_side = right_side;
404 n->u.a.assignment_symbol = assignment_symbol;
405 n->u.a.end = end;
406 return n;
407 }
408
ycf_node_comma_new(ycf_symbol * comma_symbol)409 ycf_node* ycf_node_comma_new(ycf_symbol* comma_symbol){
410 ycf_node* n = ycf_malloc(sizeof(ycf_node));
411 n->next = NULL;
412 n->type = ycf_node_type_comma;
413 n->u.comma.comma_symbol = comma_symbol;
414 return n;
415 }
416
ycf_node_array_bracket_new(ycf_symbol * start,bool empty,ycf_node_expression content,ycf_symbol * end)417 ycf_node* ycf_node_array_bracket_new(ycf_symbol* start,
418 bool empty,
419 ycf_node_expression content,
420 ycf_symbol* end){
421 ycf_node* n = ycf_malloc(sizeof(ycf_node));
422 n->next = NULL;
423 n->type = ycf_node_type_array_bracket;
424 n->u.array_bracket.start = start;
425 n->u.array_bracket.empty = empty;
426 n->u.array_bracket.content = content;
427 n->u.array_bracket.end = end;
428 return n;
429 }
430
ycf_node_macro_cmd_new(ycf_symbol * macro_symbol)431 ycf_node* ycf_node_macro_cmd_new(ycf_symbol* macro_symbol){
432 ycf_node* n = ycf_malloc(sizeof(ycf_node));
433 n->next = NULL;
434 n->type = ycf_node_type_macro_cmd;
435 n->u.macro_cmd.symbol = macro_symbol;
436 return n;
437 }
438
ycf_node_special_code_block_new(ycf_node_type type,ycf_symbol * start,ycf_node_if code,ycf_symbol * end)439 ycf_node* ycf_node_special_code_block_new(ycf_node_type type,
440 ycf_symbol* start,
441 ycf_node_if code,
442 ycf_symbol* end){
443 ycf_node* n = ycf_malloc(sizeof(ycf_node));
444 n->next = NULL;
445 n->type = type;
446 n->u.special_code_block.start = start;
447 n->u.special_code_block.code = code;
448 n->u.special_code_block.end = end;
449 return n;
450 }
451
ycf_node_goto_new(ycf_symbol * goto_symbol,ycf_symbol * label_symbol,ycf_symbol * end_symbol)452 ycf_node* ycf_node_goto_new(ycf_symbol* goto_symbol,
453 ycf_symbol* label_symbol,
454 ycf_symbol* end_symbol){
455 ycf_node* n = ycf_malloc(sizeof(ycf_node));
456 n->next = NULL;
457 n->type = ycf_node_type_goto;
458 n->u.goto_n.goto_symbol = goto_symbol;
459 n->u.goto_n.label_symbol = label_symbol;
460 n->u.goto_n.end_symbol = end_symbol;
461 return n;
462 }
463
ycf_node_return_new(ycf_symbol * return_symbol,ycf_node * return_expression,ycf_symbol * end_symbol)464 ycf_node* ycf_node_return_new(ycf_symbol* return_symbol,
465 ycf_node* return_expression,
466 ycf_symbol* end_symbol){
467 ycf_node* n = ycf_malloc(sizeof(ycf_node));
468 n->next = NULL;
469 n->type = ycf_node_type_return_statement;
470 n->u.return_n.return_symbol = return_symbol;
471 n->u.return_n.return_expression = return_expression;
472 n->u.return_n.end_symbol = end_symbol;
473 return n;
474 }
475
476
ycf_node_period_field_access_new(ycf_symbol * period,ycf_symbol * field_name)477 ycf_node* ycf_node_period_field_access_new(ycf_symbol* period,
478 ycf_symbol* field_name){
479 ycf_node* n = ycf_malloc(sizeof(ycf_node));
480 n->next = NULL;
481 n->type = ycf_node_type_period_field_access;
482 n->u.period_field_access.period = period;
483 n->u.period_field_access.field_name = field_name;
484 return n;
485 }
486
ycf_pointer_field_access_new(ycf_symbol * pointer,ycf_symbol * field_name)487 ycf_node* ycf_pointer_field_access_new(ycf_symbol* pointer,
488 ycf_symbol* field_name){
489 ycf_node* n = ycf_malloc(sizeof(ycf_node));
490 n->next = NULL;
491 n->type = ycf_node_type_period_field_access;
492 n->u.pointer_field_access.pointer = pointer;
493 n->u.pointer_field_access.field_name = field_name;
494 return n;
495 }
496
parse_symbol_list(int nr_of_aloved_symbols,ycf_symbol_type accept_symbols[],ycf_symbol * symbols)497 parse_symbol_list_res parse_symbol_list(int nr_of_aloved_symbols,
498 ycf_symbol_type accept_symbols[],
499 ycf_symbol* symbols){
500 int i;
501 parse_symbol_list_res r;
502 ycf_symbol* current = symbols;
503 r.list = ycf_symbol_list_empty();
504 while (current != NULL){
505 ycf_symbol* prev_current = current;
506 for(i = 0; i < nr_of_aloved_symbols; i++){
507 if(current->type == accept_symbols[i]){
508 ycf_symbol_list_append(&r.list, ycf_symbol_copy(current));
509 current = current->next;
510 }
511 }
512 if(prev_current == current){
513 break;
514 }
515 }
516 r.next_symbol = current;
517 return r;
518 }
519
520 parse_symbol_list_res
parse_symbol_list_until(ycf_symbol * symbols,ycf_symbol_type end_symbol)521 parse_symbol_list_until(ycf_symbol* symbols, ycf_symbol_type end_symbol){
522 parse_symbol_list_res r;
523 ycf_symbol* current = symbols;
524 r.list = ycf_symbol_list_empty();
525 while (current->type != end_symbol){
526 ycf_symbol_list_append(&r.list, ycf_symbol_copy(current));
527 current = current->next;
528 }
529 r.next_symbol = current;
530 return r;
531 }
532
533 ycf_parse_result parse_expression_end_end_squarebracket(ycf_symbol* symbols);
534
parse_array_bracket(ycf_symbol * symbols)535 ycf_parse_result parse_array_bracket(ycf_symbol* symbols){
536 if(symbols->type != ycf_symbol_type_open_square_bracket){
537 return fail_parse_result();
538 }
539 if(symbols->next->type == ycf_symbol_type_end_square_bracket){
540 ycf_node_expression empty = {.content = ycf_node_list_empty(), .end_symbol = NULL};
541 return success_parse_result(symbols->next->next,
542 ycf_node_array_bracket_new(symbols, true, empty, symbols->next));
543 }
544 ycf_parse_result square_bracket_content =
545 parse_expression_end_end_squarebracket(symbols->next);
546 if(!square_bracket_content.success){
547 return fail_parse_result();
548 }
549 return success_parse_result(square_bracket_content.next_symbol->next,
550 ycf_node_array_bracket_new(symbols,
551 false,
552 square_bracket_content.result->u.expression,
553 square_bracket_content.next_symbol));
554 }
555
parse_defenition_until_identifier(ycf_symbol * symbols,ycf_symbol_type end_symbol_type)556 ycf_parse_result parse_defenition_until_identifier(ycf_symbol* symbols,
557 ycf_symbol_type end_symbol_type){
558 ycf_symbol* current = symbols;
559 ycf_symbol_list type_specifier = ycf_symbol_list_empty();
560 ycf_symbol* ident = NULL;
561 ycf_node_list array_brackets;
562 /* Parse modifiers */
563 {
564 int nr_of_aloved_symbols = 4;
565 ycf_symbol_type accept_symbols[4];
566 accept_symbols[0] = ycf_symbol_type_static;
567 accept_symbols[1] = ycf_symbol_type_const;
568 accept_symbols[2] = ycf_symbol_type_inline;
569 accept_symbols[3] = ycf_symbol_type_volatile;
570 parse_symbol_list_res more =
571 parse_symbol_list(nr_of_aloved_symbols,
572 accept_symbols,
573 current);
574 ycf_symbol_list_concat(&type_specifier, &more.list);
575 current = more.next_symbol;
576 }
577 /* Parse first symbol */
578 if(current->type == ycf_symbol_type_identifier ||
579 current->type == ycf_symbol_type_void){
580 ycf_symbol_list_append(&type_specifier, ycf_symbol_copy(current));
581 current = current->next;
582 } else {
583 return fail_parse_result();
584 }
585 if(type_specifier.last->type == ycf_symbol_type_identifier){
586 /* Parse remaining identifiers if first symbol is ycf_symbol_type_identifier */
587 int nr_of_aloved_symbols = 2;
588 ycf_symbol_type accept_symbols[2];
589 accept_symbols[0] = ycf_symbol_type_identifier;
590 accept_symbols[1] = ycf_symbol_type_const;
591 parse_symbol_list_res more =
592 parse_symbol_list(nr_of_aloved_symbols,
593 accept_symbols,
594 current);
595 ycf_symbol_list_concat(&type_specifier, &more.list);
596 current = more.next_symbol;
597 }
598 {
599 /* Parse stars */
600 int nr_of_aloved_symbols = 1;
601 ycf_symbol_type accept_symbols[1];
602 accept_symbols[0] = ycf_symbol_type_star;
603 parse_symbol_list_res more =
604 parse_symbol_list(nr_of_aloved_symbols,
605 accept_symbols,
606 current);
607 ycf_symbol_list_concat(&type_specifier, &more.list);
608 current = more.next_symbol;
609 }
610 /* Handle ycf_symbol_type_identifier */
611 if(type_specifier.last->type == ycf_symbol_type_identifier){
612 ident = type_specifier.last;
613 ycf_symbol_list_remove(&type_specifier, type_specifier.last);
614 if (type_specifier.head == NULL) {
615 return fail_parse_result();
616 }
617 } else if (current->type != ycf_symbol_type_identifier){
618 return fail_parse_result();
619 } else {
620 ident = current;
621 current = current->next;
622 }
623 /* Handle array brackets */
624 {
625 ycf_parse_result (*parsers[])(ycf_symbol *) = {
626 parse_array_bracket
627 };
628 parse_list_res res = parse_list(1, parsers, current);
629 array_brackets = res.list;
630 current = res.next_symbol;
631 }
632 /* Parse end symbol */
633 if (current == NULL ||
634 current->type != end_symbol_type){
635 return fail_parse_result();
636 } else {
637 return success_parse_result(current->next,
638 ycf_node_defenition_new(type_specifier,
639 ident,
640 array_brackets,
641 current));
642 }
643 }
644
parse_defenition_no_init(ycf_symbol * symbols)645 ycf_parse_result parse_defenition_no_init(ycf_symbol* symbols){
646 ycf_parse_result res = parse_defenition_until_identifier(symbols, ycf_symbol_type_semicolon);
647 return res;
648 }
649
parse_defenition_with_init(ycf_symbol * symbols)650 ycf_parse_result parse_defenition_with_init(ycf_symbol* symbols){
651 ycf_parse_result dec = parse_defenition_until_identifier(symbols, ycf_symbol_type_equal_sign);
652 parse_symbol_list_res expression_list_res;
653 if(!dec.success){
654 return fail_parse_result();
655 }
656 expression_list_res =
657 parse_symbol_list_until(dec.next_symbol, ycf_symbol_type_semicolon);
658 return success_parse_result(expression_list_res.next_symbol->next,
659 ycf_node_defenition_with_init_new(dec.result->u.definition,
660 expression_list_res.list,
661 expression_list_res.next_symbol));
662 }
663
parse_defenition_comma(ycf_symbol * symbols)664 ycf_parse_result parse_defenition_comma(ycf_symbol* symbols){
665 ycf_parse_result res = parse_defenition_until_identifier(symbols, ycf_symbol_type_comma);
666 return res;
667 }
668
parse_defenition_end_paran(ycf_symbol * symbols)669 ycf_parse_result parse_defenition_end_paran(ycf_symbol* symbols){
670 ycf_parse_result res = parse_defenition_until_identifier(symbols, ycf_symbol_type_end_parenthesis);
671 return res;
672 }
673
parse_function_head(ycf_symbol * symbols,ycf_symbol_type end_symbol,bool exclude_end_symbol)674 ycf_parse_result parse_function_head(ycf_symbol* symbols,
675 ycf_symbol_type end_symbol,
676 bool exclude_end_symbol){
677 ycf_symbol* current = symbols;
678 ycf_parse_result def_res =
679 parse_defenition_until_identifier(current,
680 ycf_symbol_type_open_parenthesis);
681 if(!def_res.success){
682 return fail_parse_result();
683 }
684 current = def_res.next_symbol;
685
686 /* Parse function without parameters */
687
688 if(current->type == ycf_symbol_type_end_parenthesis &&
689 current->next->type == end_symbol){
690 ycf_symbol_list end = ycf_symbol_list_empty();
691 ycf_symbol* next_symbol;
692 ycf_node_list parameter_list = ycf_node_list_empty();
693 ycf_symbol_list_append(&end, ycf_symbol_copy(current));
694 if(!exclude_end_symbol){
695 next_symbol = current->next->next;
696 ycf_symbol_list_append(&end, ycf_symbol_copy(current->next));
697 } else {
698 next_symbol = current->next;
699 }
700 return success_parse_result(next_symbol,
701 ycf_node_function_def_new(def_res.result->u.definition,
702 parameter_list,
703 false,
704 end));
705 } else if(current->type == ycf_symbol_type_void &&
706 current->next->type == ycf_symbol_type_end_parenthesis &&
707 current->next->next->type == end_symbol){
708 ycf_symbol_list end = ycf_symbol_list_empty();
709 ycf_symbol* next_symbol;
710 ycf_node_list parameter_list = ycf_node_list_empty();
711 ycf_symbol_list_append(&end, ycf_symbol_copy(current));
712 ycf_symbol_list_append(&end, ycf_symbol_copy(current->next));
713 if(!exclude_end_symbol){
714 next_symbol = current->next->next->next;
715 ycf_symbol_list_append(&end, ycf_symbol_copy(current->next->next));
716 }else{
717 next_symbol = current->next->next;
718 }
719 return success_parse_result(next_symbol,
720 ycf_node_function_def_new(def_res.result->u.definition,
721 parameter_list,
722 false,
723 end));
724 }
725
726 /* Parse parameters */
727
728 int number_of_parsers = 2;
729 ycf_parse_result (*parsers[])(ycf_symbol *) = {
730 parse_defenition_comma,
731 parse_defenition_end_paran
732 };
733 parse_list_res param_list =
734 parse_list(number_of_parsers, parsers, current);
735 if(param_list.next_symbol == NULL ||
736 param_list.next_symbol->type != end_symbol){
737 return fail_parse_result();
738 }
739
740 /* Parse ending */
741
742 {
743 ycf_symbol_list end = ycf_symbol_list_empty();
744 ycf_symbol* next_symbol;
745 if(!exclude_end_symbol){
746 next_symbol = param_list.next_symbol->next;
747 ycf_symbol_list_append(&end, ycf_symbol_copy(param_list.next_symbol));
748 }else{
749 next_symbol = param_list.next_symbol;
750 }
751 return success_parse_result(next_symbol,
752 ycf_node_function_def_new(def_res.result->u.definition,
753 param_list.list,
754 false,
755 end));
756 }
757 }
758
parse_function_def(ycf_symbol * symbols)759 ycf_parse_result parse_function_def(ycf_symbol* symbols){
760 return parse_function_head(symbols, ycf_symbol_type_semicolon, false);
761 }
762
parse_other(ycf_symbol * symbols)763 ycf_parse_result parse_other(ycf_symbol* symbols){
764 ycf_symbol* what = symbols;
765 return success_parse_result(symbols->next,
766 ycf_node_other_new(what));
767 }
768
769
parse_goto(ycf_symbol * symbols)770 ycf_parse_result parse_goto(ycf_symbol* symbols){
771 ycf_symbol* goto_symbol = symbols;
772 if(goto_symbol->type != ycf_symbol_type_goto){
773 return fail_parse_result();
774 }
775 ycf_symbol* label_symbol = goto_symbol->next;
776 if(label_symbol->type != ycf_symbol_type_identifier){
777 return fail_parse_result();
778 }
779 ycf_symbol* end_symbol = label_symbol->next;
780 if(end_symbol->type != ycf_symbol_type_semicolon){
781 return fail_parse_result();
782 }
783 return success_parse_result(end_symbol->next,
784 ycf_node_goto_new(goto_symbol, label_symbol, end_symbol));
785 }
786
787
parse_comma(ycf_symbol * symbols)788 ycf_parse_result parse_comma(ycf_symbol* symbols){
789 if(symbols->type == ycf_symbol_type_comma){
790 return success_parse_result(symbols->next, ycf_node_comma_new(symbols));
791 } else {
792 return fail_parse_result();
793 }
794 }
795
796
parse_pointer_field_access_node(ycf_symbol * symbols)797 ycf_parse_result parse_pointer_field_access_node(ycf_symbol* symbols){
798 ycf_symbol* pointer = symbols;
799 if(pointer->type != ycf_symbol_type_pointer_field_access){
800 return fail_parse_result();
801 }
802 ycf_symbol* field_name = pointer->next;
803 if(field_name->type != ycf_symbol_type_identifier){
804 return fail_parse_result();
805 }
806 return success_parse_result(field_name->next,
807 ycf_pointer_field_access_new(pointer, field_name));
808 }
809
parse_period_field_access_node(ycf_symbol * symbols)810 ycf_parse_result parse_period_field_access_node(ycf_symbol* symbols){
811 ycf_symbol* p = symbols;
812 if(p->type != ycf_symbol_type_period){
813 return fail_parse_result();
814 }
815 ycf_symbol* field_name = p->next;
816 if(field_name->type != ycf_symbol_type_identifier){
817 return fail_parse_result();
818 }
819 return success_parse_result(field_name->next,
820 ycf_node_period_field_access_new(p, field_name));
821 }
822
823 ycf_parse_result parse_function_call(ycf_symbol* symbols);
824 ycf_parse_result parse_expression_generic(ycf_symbol* symbols,
825 bool use_end_symbol,
826 bool no_function_call_assignment,
827 int nr_of_end_symbols,
828 ycf_symbol_type end_symbols[]);
829
parse_paran_expression(ycf_symbol * symbols)830 ycf_parse_result parse_paran_expression(ycf_symbol* symbols){
831 if(symbols->type != ycf_symbol_type_open_parenthesis){
832 return fail_parse_result();
833 }
834 ycf_symbol* start = symbols;
835 ycf_parse_result exp = parse_expression_generic(start->next,
836 true,
837 false,
838 1,
839 (ycf_symbol_type[]){ycf_symbol_type_end_parenthesis});
840 if(!exp.success || exp.next_symbol->type != ycf_symbol_type_end_parenthesis ){
841 return fail_parse_result();
842 }
843 return success_parse_result(exp.next_symbol->next,
844 ycf_node_paran_expression_new(start,
845 exp.result->u.expression,
846 exp.next_symbol));
847 }
848
849 ycf_parse_result parse_function_call_assignment(ycf_symbol* symbols);
850 ycf_parse_result parse_assignment(ycf_symbol* symbols);
851
parse_expression_generic(ycf_symbol * symbols,bool use_end_symbol,bool no_function_call_assignment,int nr_of_end_symbols,ycf_symbol_type end_symbols[])852 ycf_parse_result parse_expression_generic(ycf_symbol* symbols,
853 bool use_end_symbol,
854 bool no_function_call_assignment,
855 int nr_of_end_symbols,
856 ycf_symbol_type end_symbols[]){
857 int number_of_parsers = (!no_function_call_assignment ? 1 : 0) + 5;
858 ycf_parse_result (*parsers[7])(ycf_symbol *);
859 if(!no_function_call_assignment){
860 parsers[0] = parse_function_call;
861 parsers[1] = parse_function_call_assignment;
862 parsers[2] = parse_paran_expression;
863 parsers[3] = parse_period_field_access_node;
864 parsers[4] = parse_pointer_field_access_node;
865 parsers[5] = parse_other;
866 }else{
867 parsers[0] = parse_function_call;
868 parsers[1] = parse_paran_expression;
869 parsers[2] = parse_period_field_access_node;
870 parsers[3] = parse_pointer_field_access_node;
871 parsers[4] = parse_other;
872 }
873 parse_list_res res = parse_list_break_symbols(number_of_parsers, parsers, symbols, nr_of_end_symbols,end_symbols);
874 if(res.list.head == NULL || (use_end_symbol &&
875 (res.next_symbol == NULL ||res.next_symbol->type != end_symbols[0]))){
876 return fail_parse_result();
877 }
878 return success_parse_result(res.next_symbol,
879 ycf_node_expression_new(res.list));
880 }
881
parse_expression(ycf_symbol * symbols)882 ycf_parse_result parse_expression(ycf_symbol* symbols){
883 return parse_expression_generic(symbols,
884 false,
885 false,
886 0 /* ignored */,
887 NULL);
888 }
889
parse_expression_end_comma(ycf_symbol * symbols)890 ycf_parse_result parse_expression_end_comma(ycf_symbol* symbols){
891 return parse_expression_generic(symbols,
892 true,
893 false,
894 2,
895 (ycf_symbol_type[]){ycf_symbol_type_comma, ycf_symbol_type_end_parenthesis});
896 }
897
parse_expression_end_end_paren(ycf_symbol * symbols)898 ycf_parse_result parse_expression_end_end_paren(ycf_symbol* symbols){
899 return parse_expression_generic(symbols,
900 true,
901 false,
902 1,
903 (ycf_symbol_type[]){ycf_symbol_type_end_parenthesis});
904 }
905
parse_expression_end_end_semicolon(ycf_symbol * symbols)906 ycf_parse_result parse_expression_end_end_semicolon(ycf_symbol* symbols){
907 return parse_expression_generic(symbols,
908 true,
909 false,
910 1,
911 (ycf_symbol_type[]){ycf_symbol_type_semicolon});
912 }
913
parse_expression_end_end_squarebracket(ycf_symbol * symbols)914 ycf_parse_result parse_expression_end_end_squarebracket(ycf_symbol* symbols){
915 return parse_expression_generic(symbols,
916 true,
917 false,
918 1,
919 (ycf_symbol_type[]){ycf_symbol_type_end_square_bracket});
920 }
921
922
parse_function_call(ycf_symbol * symbols)923 ycf_parse_result parse_function_call(ycf_symbol* symbols){
924 parse_symbol_list_res negs;
925 {
926 /* Parse neg symbols */
927 int nr_of_aloved_symbols = 1;
928 ycf_symbol_type accept_symbols[1];
929 accept_symbols[0] = ycf_symbol_type_neg;
930 negs =
931 parse_symbol_list(nr_of_aloved_symbols,
932 accept_symbols,
933 symbols);
934 }
935 ycf_symbol* ident = negs.next_symbol;
936 if(ident == NULL ||
937 ident->type != ycf_symbol_type_identifier){
938 return fail_parse_result();
939 }
940 ycf_symbol* paran_start = ident->next;
941 if(paran_start == NULL || paran_start->type != ycf_symbol_type_open_parenthesis){
942 return fail_parse_result();
943 }
944 int number_of_parsers = 3;
945 ycf_parse_result (*parsers[])(ycf_symbol *) = {
946 parse_comma,
947 parse_expression_end_comma,
948 parse_expression_end_end_paren
949 };
950 parse_list_res param_list_res =
951 parse_list_break_symbol(number_of_parsers,
952 parsers,
953 paran_start->next,
954 ycf_symbol_type_end_parenthesis);
955 if(param_list_res.next_symbol == NULL ||
956 param_list_res.next_symbol->type != ycf_symbol_type_end_parenthesis){
957 return fail_parse_result();
958 }
959 return success_parse_result(param_list_res.next_symbol->next,
960 ycf_node_function_call_new(negs.list,
961 ident,
962 paran_start,
963 param_list_res.list,
964 param_list_res.next_symbol));
965 }
966
parse_assignment(ycf_symbol * symbols)967 ycf_parse_result parse_assignment(ycf_symbol* symbols) {
968 if(symbols->type != ycf_symbol_type_identifier){
969 return fail_parse_result();
970 }
971 if(symbols->next == NULL || symbols->next->type != ycf_symbol_type_equal_sign){
972 return fail_parse_result();
973 }
974 ycf_parse_result left_expression = parse_expression_generic(symbols,
975 false,
976 true,
977 3,
978 (ycf_symbol_type[]){
979 ycf_symbol_type_equal_sign,
980 ycf_symbol_type_semicolon,
981 ycf_symbol_type_end_parenthesis});
982 if(!left_expression.success){
983 return fail_parse_result();
984 }
985 ycf_symbol* equal_sign = left_expression.next_symbol;
986 if(equal_sign == NULL || equal_sign->type != ycf_symbol_type_equal_sign) {
987 return fail_parse_result();
988 }
989 if(equal_sign->next == NULL || equal_sign->next->type != ycf_symbol_type_identifier){
990 return fail_parse_result();
991 }
992 if(equal_sign->next->next == NULL || equal_sign->next->next->type != ycf_symbol_type_semicolon){
993 return fail_parse_result();
994 }
995 ycf_parse_result right_expression = parse_expression_generic(equal_sign->next,
996 false,
997 true,
998 3,
999 (ycf_symbol_type[]){
1000 ycf_symbol_type_semicolon,
1001 ycf_symbol_type_equal_sign,
1002 ycf_symbol_type_end_parenthesis});
1003 if(!right_expression.success ||
1004 right_expression.next_symbol == NULL ||
1005 right_expression.next_symbol->type != ycf_symbol_type_semicolon){
1006 return fail_parse_result();
1007 }
1008 ycf_node* node = ycf_node_assignment_new(left_expression.result->u.expression,
1009 ycf_symbol_copy(equal_sign),
1010 right_expression.result->u.expression,
1011 ycf_symbol_copy(right_expression.next_symbol));
1012 return success_parse_result(right_expression.next_symbol->next,
1013 node);
1014 }
1015
parse_function_call_assignment(ycf_symbol * symbols)1016 ycf_parse_result parse_function_call_assignment(ycf_symbol* symbols){
1017 ycf_parse_result expression = parse_expression_generic(symbols,
1018 true,
1019 true,
1020 3,
1021 (ycf_symbol_type[]){ycf_symbol_type_equal_sign,
1022 ycf_symbol_type_semicolon,
1023 ycf_symbol_type_end_parenthesis});
1024 if(!expression.success){
1025 return fail_parse_result();
1026 }
1027 ycf_symbol* equal_sign = expression.next_symbol;
1028 ycf_parse_result fun_call = parse_function_call(equal_sign->next);
1029 if(!fun_call.success){
1030 return fail_parse_result();
1031 }
1032 return success_parse_result(fun_call.next_symbol,
1033 ycf_node_fun_call_assignment_new(expression.result->u.expression,
1034 equal_sign,
1035 fun_call.result->u.function_call));
1036 }
1037
1038
parse_function_call_assignment_statement(ycf_symbol * symbols)1039 ycf_parse_result parse_function_call_assignment_statement(ycf_symbol* symbols){
1040 ycf_parse_result fun_call_ass = parse_function_call_assignment(symbols);
1041 if(!fun_call_ass.success || fun_call_ass.next_symbol->type != ycf_symbol_type_semicolon){
1042 return fail_parse_result();
1043 }
1044 return success_parse_result(fun_call_ass.next_symbol->next,
1045 ycf_node_statement_new(fun_call_ass.result,
1046 fun_call_ass.next_symbol));
1047 }
1048
1049
parse_function_call_statement(ycf_symbol * symbols)1050 ycf_parse_result parse_function_call_statement(ycf_symbol* symbols){
1051 ycf_parse_result fun_call = parse_function_call(symbols);
1052 if(!fun_call.success || fun_call.next_symbol->type != ycf_symbol_type_semicolon){
1053 return fail_parse_result();
1054 }
1055 return success_parse_result(fun_call.next_symbol->next,
1056 ycf_node_statement_new(fun_call.result,
1057 fun_call.next_symbol));
1058 }
1059
1060 ycf_parse_result parse_statement(ycf_symbol* symbols);
1061
1062
parse_cond_statement(ycf_symbol * symbols,ycf_symbol_type cond_keyword)1063 ycf_parse_result parse_cond_statement(ycf_symbol* symbols, ycf_symbol_type cond_keyword){
1064 if(symbols->type != cond_keyword){
1065 return fail_parse_result();
1066 }
1067 ycf_symbol* cond_word = symbols;
1068 ycf_parse_result cond_exp = parse_paran_expression(cond_word->next);
1069 if(!cond_exp.success){
1070 return fail_parse_result();
1071 }
1072 ycf_parse_result cond_statement = parse_statement(cond_exp.next_symbol);
1073 if(!cond_statement.success){
1074 return fail_parse_result();
1075 }
1076 /* Return if node even though it might be something else */
1077 return success_parse_result(cond_statement.next_symbol,
1078 ycf_node_if_new(cond_word,
1079 cond_exp.result->u.parentheses_expression,
1080 cond_statement.result));
1081 }
1082
parse_if_statement(ycf_symbol * symbols)1083 ycf_parse_result parse_if_statement(ycf_symbol* symbols){
1084 return parse_cond_statement(symbols, ycf_symbol_type_if);
1085 }
1086
parse_if_else_statement(ycf_symbol * symbols)1087 ycf_parse_result parse_if_else_statement(ycf_symbol* symbols){
1088 ycf_parse_result if_statem = parse_if_statement(symbols);
1089 if(!if_statem.success){
1090 return fail_parse_result();
1091 }
1092 if(if_statem.next_symbol->type != ycf_symbol_type_else){
1093 return fail_parse_result();
1094 }
1095 ycf_symbol* else_w = if_statem.next_symbol;
1096 ycf_parse_result else_statement = parse_statement(else_w->next);
1097 if(!else_statement.success){
1098 return fail_parse_result();
1099 }
1100 return success_parse_result(else_statement.next_symbol,
1101 ycf_node_if_else_new(if_statem.result->u.if_n,
1102 else_w,
1103 else_statement.result));
1104 }
1105
parse_while_statement(ycf_symbol * symbols)1106 ycf_parse_result parse_while_statement(ycf_symbol* symbols){
1107 ycf_parse_result cond_statem = parse_cond_statement(symbols, ycf_symbol_type_while);
1108 if(!cond_statem.success){
1109 return fail_parse_result();
1110 }
1111 return success_parse_result(cond_statem.next_symbol,
1112 ycf_node_while_new(cond_statem.result->u.if_n.if_word,
1113 cond_statem.result->u.if_n.expression,
1114 cond_statem.result->u.if_n.if_statement));
1115 }
1116
parse_do_while_statement(ycf_symbol * symbols)1117 ycf_parse_result parse_do_while_statement(ycf_symbol* symbols){
1118 if(symbols->type != ycf_symbol_type_do){
1119 return fail_parse_result();
1120 }
1121 ycf_symbol* do_word = symbols;
1122 ycf_parse_result statement = parse_statement(do_word->next);
1123 if(!statement.success || statement.next_symbol->type != ycf_symbol_type_while){
1124 return fail_parse_result();
1125 }
1126 ycf_symbol* while_word = statement.next_symbol;
1127 ycf_parse_result cond = parse_paran_expression(while_word->next);
1128 if(!cond.success){
1129 return fail_parse_result();
1130 }
1131 if(cond.next_symbol->type != ycf_symbol_type_semicolon){
1132 return fail_parse_result();
1133 }
1134 return success_parse_result(cond.next_symbol->next,
1135 ycf_node_do_while_new(do_word, statement.result, while_word,
1136 cond.result->u.parentheses_expression, cond.next_symbol));
1137 }
1138
parse_for_statement(ycf_symbol * symbols)1139 ycf_parse_result parse_for_statement(ycf_symbol* symbols){
1140 if(symbols->type != ycf_symbol_type_for){
1141 return fail_parse_result();
1142 }
1143 ycf_symbol* for_word = symbols;
1144 if(for_word->next->type != ycf_symbol_type_open_parenthesis){
1145 return fail_parse_result();
1146 }
1147 ycf_symbol* start_paran = for_word->next;
1148 ycf_node* init = NULL;
1149 ycf_parse_result init_res = parse_defenition_with_init(start_paran->next);
1150 if(!init_res.success){
1151 init_res = parse_defenition_no_init(start_paran->next);
1152 }
1153 if(!init_res.success){
1154 init_res = parse_exp_statement(start_paran->next);
1155 }
1156 if(!init_res.success){
1157 return fail_parse_result();
1158 }
1159 init = init_res.result;
1160 ycf_node* stop_cond = NULL;
1161 ycf_symbol* stop_cond_end = NULL;
1162 if(init_res.next_symbol->type == ycf_symbol_type_semicolon){
1163 stop_cond_end = init_res.next_symbol;
1164 }else{
1165 ycf_parse_result stop_cond_res = parse_expression_end_end_semicolon(init_res.next_symbol);
1166 if(!stop_cond_res.success || stop_cond_res.next_symbol->type != ycf_symbol_type_semicolon){
1167 return fail_parse_result();
1168 }
1169 stop_cond = stop_cond_res.result;
1170 stop_cond_end = stop_cond_res.next_symbol;
1171 }
1172 ycf_node* end_exp = NULL;
1173 ycf_symbol* end_exp_end = NULL;
1174 if(stop_cond_end->next->type == ycf_symbol_type_end_parenthesis){
1175 end_exp_end = stop_cond_end->next;
1176 }else{
1177 ycf_parse_result end_exp_res = parse_expression_end_end_paren(stop_cond_end->next);
1178 if(!end_exp_res.success || end_exp_res.next_symbol->type != ycf_symbol_type_end_parenthesis){
1179 return fail_parse_result();
1180 }
1181 end_exp = end_exp_res.result;
1182 end_exp_end = end_exp_res.next_symbol;
1183 }
1184 ycf_parse_result for_statement = parse_statement(end_exp_end->next);
1185 if(!for_statement.success){
1186 return fail_parse_result();
1187 }
1188 return success_parse_result(for_statement.next_symbol,
1189 ycf_node_for_new(for_word,
1190 start_paran,
1191 init,
1192 stop_cond,
1193 stop_cond_end,
1194 end_exp,
1195 end_exp_end,
1196 for_statement.result));
1197 }
1198
1199
parse_switch_statement(ycf_symbol * symbols)1200 ycf_parse_result parse_switch_statement(ycf_symbol* symbols){
1201 ycf_parse_result cond_statem = parse_cond_statement(symbols, ycf_symbol_type_switch);
1202 if(!cond_statem.success){
1203 return fail_parse_result();
1204 }
1205 if(cond_statem.result->u.if_n.if_statement->type != ycf_node_type_code_scope){
1206 return fail_parse_result();
1207 }
1208 return success_parse_result(cond_statem.next_symbol,
1209 ycf_node_switch_new(cond_statem.result->u.if_n.if_word,
1210 cond_statem.result->u.if_n.expression,
1211 cond_statem.result->u.if_n.if_statement->u.code_scope));
1212 }
1213
parse_exp_statement(ycf_symbol * symbols)1214 ycf_parse_result parse_exp_statement(ycf_symbol* symbols){
1215 if(symbols->type == ycf_symbol_type_semicolon){
1216 /*Empty statement*/
1217 return success_parse_result(symbols->next,
1218 ycf_node_statement_new(ycf_node_expression_new(ycf_node_list_empty()),
1219 symbols));
1220 }
1221 ycf_parse_result expression = parse_expression_end_end_semicolon(symbols);
1222 if(!expression.success || expression.next_symbol->type != ycf_symbol_type_semicolon){
1223 return fail_parse_result();
1224 }
1225 return success_parse_result(expression.next_symbol->next,
1226 ycf_node_statement_new(expression.result,
1227 expression.next_symbol));
1228 }
1229
parse_return_statement(ycf_symbol * symbols)1230 ycf_parse_result parse_return_statement(ycf_symbol* symbols){
1231 ycf_symbol* return_symbol = symbols;
1232 if(return_symbol->type != ycf_symbol_type_return){
1233 return fail_parse_result();
1234 }
1235 if(return_symbol->next->type == ycf_symbol_type_semicolon){
1236 return success_parse_result(return_symbol->next->next,
1237 ycf_node_return_new(return_symbol,
1238 NULL,
1239 return_symbol->next));
1240 }
1241 ycf_parse_result expression = parse_expression_end_end_semicolon(return_symbol->next);
1242 if(!expression.success || expression.next_symbol->type != ycf_symbol_type_semicolon){
1243 return fail_parse_result();
1244 }
1245 return success_parse_result(expression.next_symbol->next,
1246 ycf_node_return_new(return_symbol,
1247 expression.result,
1248 expression.next_symbol));
1249 }
1250
parse_macro_command(ycf_symbol * symbols)1251 ycf_parse_result parse_macro_command(ycf_symbol* symbols){
1252 if(symbols->type == ycf_symbol_type_macro_command || symbols->type == ycf_symbol_type_macro_define){
1253 return success_parse_result(symbols->next, ycf_node_macro_cmd_new(symbols));
1254 } else {
1255 return fail_parse_result();
1256 }
1257 }
1258
1259 ycf_parse_result parse_scope(ycf_symbol* symbols);
1260
parse_special_code_block(ycf_symbol * symbols,char * code_block_name,ycf_node_type type)1261 ycf_parse_result parse_special_code_block(ycf_symbol* symbols, char* code_block_name, ycf_node_type type){
1262 ycf_symbol* start = symbols;
1263 if(symbols->type != ycf_symbol_type_special_code_start ||
1264 !ycf_symbol_is_text_eq(start,
1265 ycf_string_new("/*special_code_start:%s*/", code_block_name))){
1266 return fail_parse_result();
1267 }
1268 ycf_parse_result code = parse_if_statement(start->next);
1269 if(!code.success){
1270 return fail_parse_result();
1271 }
1272 ycf_symbol* end = code.next_symbol;
1273 if(end->type != ycf_symbol_type_special_code_end){
1274 return fail_parse_result();
1275 }
1276 return success_parse_result(end->next,
1277 ycf_node_special_code_block_new(type,
1278 start,
1279 code.result->u.if_n,
1280 end));
1281 }
1282
parse_on_save_yield_state(ycf_symbol * symbols)1283 ycf_parse_result parse_on_save_yield_state(ycf_symbol* symbols){
1284 return parse_special_code_block(symbols, "ON_SAVE_YIELD_STATE", ycf_node_type_on_save_yield_state_code);
1285 }
1286
parse_on_restore_yield_state(ycf_symbol * symbols)1287 ycf_parse_result parse_on_restore_yield_state(ycf_symbol* symbols){
1288 return parse_special_code_block(symbols, "ON_RESTORE_YIELD_STATE", ycf_node_type_on_restore_yield_state_code);
1289 }
1290
parse_destroy_state_code(ycf_symbol * symbols)1291 ycf_parse_result parse_destroy_state_code(ycf_symbol* symbols){
1292 ycf_parse_result res = parse_special_code_block(symbols, "ON_DESTROY_STATE", ycf_node_type_on_destroy_state_code);
1293 return res;
1294 }
1295
parse_on_return_code(ycf_symbol * symbols)1296 ycf_parse_result parse_on_return_code(ycf_symbol* symbols){
1297 ycf_parse_result res = parse_special_code_block(symbols, "ON_RETURN", ycf_node_type_on_return_code);
1298 return res;
1299 }
1300
parse_on_destroy_state_or_return_code(ycf_symbol * symbols)1301 ycf_parse_result parse_on_destroy_state_or_return_code(ycf_symbol* symbols){
1302 ycf_parse_result res = parse_special_code_block(symbols, "ON_DESTROY_STATE_OR_RETURN", ycf_node_type_on_destroy_state_or_return_code);
1303 return res;
1304 }
1305
parse_consume_reds(ycf_symbol * symbols)1306 ycf_parse_result parse_consume_reds(ycf_symbol* symbols){
1307 ycf_symbol* consume_reds_symbol = symbols;
1308 if(consume_reds_symbol->type != ycf_symbol_type_consume_reds){
1309 return fail_parse_result();
1310 }
1311 ycf_parse_result paran_expression =
1312 parse_paran_expression(consume_reds_symbol->next);
1313 if(!paran_expression.success){
1314 return fail_parse_result();
1315 }
1316 if(paran_expression.next_symbol->type != ycf_symbol_type_semicolon){
1317 return fail_parse_result();
1318 }
1319 return success_parse_result(paran_expression.next_symbol->next,
1320 ycf_node_consume_reds_new(consume_reds_symbol,
1321 paran_expression.result->u.parentheses_expression,
1322 paran_expression.next_symbol));
1323 }
1324
parse_statement(ycf_symbol * symbols)1325 ycf_parse_result parse_statement(ycf_symbol* symbols){
1326 int number_of_parsers = 22;
1327 ycf_parse_result (*parsers[])(ycf_symbol *) = {
1328 parse_defenition_no_init,
1329 parse_defenition_with_init,
1330 parse_if_else_statement,
1331 parse_if_statement,
1332 parse_while_statement,
1333 parse_do_while_statement,
1334 parse_for_statement,
1335 parse_switch_statement,
1336 parse_scope,
1337 parse_goto,
1338 parse_return_statement,
1339 parse_consume_reds,
1340 parse_function_call_statement,
1341 parse_function_call_assignment_statement,
1342 parse_assignment,
1343 parse_on_save_yield_state,
1344 parse_on_restore_yield_state,
1345 parse_on_return_code,
1346 parse_on_destroy_state_or_return_code,
1347 parse_destroy_state_code,
1348 parse_macro_command,
1349 parse_exp_statement
1350 };
1351 for(int i = 0; i < number_of_parsers; i++){
1352 ycf_parse_result res = parsers[i](symbols);
1353 if(res.success){
1354 return res;
1355 }
1356 }
1357 return fail_parse_result();
1358 }
1359
parse_scope(ycf_symbol * symbols)1360 ycf_parse_result parse_scope(ycf_symbol* symbols){
1361 ycf_symbol* current = symbols;
1362 ycf_symbol* start;
1363 if(current->type != ycf_symbol_type_open_curly_brace){
1364 return fail_parse_result();
1365 }
1366 start = current;
1367 current = current->next;
1368 int number_of_parsers = 2;
1369 ycf_parse_result (*parsers[])(ycf_symbol *) = {
1370 parse_defenition_no_init,
1371 parse_defenition_with_init
1372 };
1373 parse_list_res decs = parse_list(number_of_parsers, parsers, current);
1374 current = decs.next_symbol;
1375 /* parse rest */
1376 number_of_parsers = 1;
1377 ycf_parse_result (*rest_parsers[])(ycf_symbol *) = {
1378 parse_statement
1379 };
1380 parse_list_res others =
1381 parse_list_break_symbol(number_of_parsers,
1382 rest_parsers,
1383 current,
1384 ycf_symbol_type_end_curly_brace);
1385 current = others.next_symbol;
1386 if (current == NULL) {
1387 return fail_parse_result();
1388 }
1389 return success_parse_result(current->next, ycf_node_scope_new(start,
1390 decs.list,
1391 others.list,
1392 current));
1393 }
1394
parse_function(ycf_symbol * symbols)1395 ycf_parse_result parse_function(ycf_symbol* symbols){
1396 ycf_parse_result fun_head = parse_function_head(symbols, ycf_symbol_type_open_curly_brace, true);
1397 if(!fun_head.success){
1398 return fail_parse_result();
1399 }
1400 ycf_parse_result scope = parse_scope(fun_head.next_symbol);
1401 if(!scope.success){
1402 return fail_parse_result();
1403 }
1404 return success_parse_result(scope.next_symbol,
1405 ycf_node_function_new(fun_head.result->u.function_definition,
1406 scope.result->u.code_scope));
1407 }
1408
parse_c_file(ycf_symbol * symbols)1409 ycf_parse_result parse_c_file(ycf_symbol* symbols){
1410 int number_of_parsers = 5;
1411 ycf_parse_result (*parsers[])(ycf_symbol *) = {
1412 parse_defenition_no_init,
1413 parse_defenition_with_init,
1414 parse_function_def,
1415 parse_function,
1416 parse_other
1417 };
1418 parse_list_res program_list =
1419 parse_list(number_of_parsers, parsers, symbols);
1420 if(program_list.next_symbol != NULL){
1421 return fail_parse_result();
1422 } else {
1423 return success_parse_result(NULL,
1424 ycf_node_c_file_new(program_list.list));
1425 }
1426 }
1427
1428
get_abstract_syntax_tree_root(ycf_symbol_list * symbols)1429 ycf_node* get_abstract_syntax_tree_root(ycf_symbol_list* symbols){
1430 ycf_parse_result res = parse_c_file(symbols->head);
1431 if(!res.success){
1432 printf("ERROR: Could not parse file\n");
1433 exit(1);
1434 }
1435 return res.result;
1436 }
1437
ycf_node_deep_copy(ycf_node * n)1438 ycf_node* ycf_node_deep_copy(ycf_node *n) {
1439 ycf_string_printable_buffer *b = ycf_string_printable_buffer_new();
1440 ycf_node_print(n, b);
1441 ycf_symbol_list symbols = ycf_symbol_list_from_text(b->buffer);
1442 ycf_parse_result res;
1443 if (n->type == ycf_node_type_c_file) {
1444 res = parse_c_file(symbols.head);
1445 } else if (n->type == ycf_node_type_variable_definition ||
1446 n->type == ycf_node_type_variable_definition_init ||
1447 n->type == ycf_node_type_other ||
1448 n->type == ycf_node_type_code_scope ||
1449 n->type == ycf_node_type_assignment ||
1450 n->type == ycf_node_type_yield ||
1451 n->type == ycf_node_type_statement ||
1452 n->type == ycf_node_type_if ||
1453 n->type == ycf_node_type_if_else ||
1454 n->type == ycf_node_type_while ||
1455 n->type == ycf_node_type_do_while ||
1456 n->type == ycf_node_type_switch ||
1457 n->type == ycf_node_type_for ||
1458 n->type == ycf_node_type_assignment_function_call ||
1459 n->type == ycf_node_type_on_save_yield_state_code ||
1460 n->type == ycf_node_type_on_restore_yield_state_code ||
1461 n->type == ycf_node_type_on_destroy_state_code ||
1462 n->type == ycf_node_type_on_return_code ||
1463 n->type == ycf_node_type_on_destroy_state_or_return_code ||
1464 n->type == ycf_node_type_goto ||
1465 n->type == ycf_node_type_return_statement ||
1466 n->type == ycf_node_type_consume_reds) {
1467 res = parse_statement(symbols.head);
1468 } else if (n->type == ycf_node_type_function_declaration) {
1469 res = parse_function_def(symbols.head);
1470 } else if (n->type == ycf_node_type_function_definition) {
1471 res = parse_function(symbols.head);
1472 } else if (n->type == ycf_node_type_function_call) {
1473 res = parse_function_call(symbols.head);
1474 } else if (n->type == ycf_node_type_expression) {
1475 res = parse_expression(symbols.head);
1476 } else if (n->type == ycf_node_type_parentheses_expression) {
1477 res = parse_paran_expression(symbols.head);
1478 } else if (n->type == ycf_node_type_comma) {
1479 res = parse_comma(symbols.head);
1480 } else if (n->type == ycf_node_type_array_bracket) {
1481 res = parse_array_bracket(symbols.head);
1482 } else if (n->type == ycf_node_type_macro_cmd) {
1483 res = parse_macro_command(symbols.head);
1484 } else if (n->type == ycf_node_type_period_field_access) {
1485 res = parse_period_field_access_node(symbols.head);
1486 } else if (n->type == ycf_node_type_pointer_field_access) {
1487 res = parse_pointer_field_access_node(symbols.head);
1488 } else {
1489 fprintf(stderr, "Unknown node type %d\n", n->type);
1490 exit(1);
1491 }
1492 if(!res.success) {
1493 fprintf(stderr, "An error has been detected in the function ycf_node_deep_copy\n");
1494 exit(1);
1495 }
1496 return res.result;
1497 }
1498
1499