-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathparser.c
More file actions
2696 lines (2309 loc) · 81.7 KB
/
Copy pathparser.c
File metadata and controls
2696 lines (2309 loc) · 81.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "compiler.h"
#include "misc.h"
#include <stdio.h>
#include <stddef.h>
#include <assert.h>
#include <stdarg.h>
#include <memory.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
/**
* Specifies that this part of the history is a non coneable entity,
* the memory will be created on the heap to prevent it being cloned
*/
#define NON_CLONEABLE_HISTORY_VARIABLE(type, name) type *name;
/**
* Accesses this non cloneable history variable value
*/
#define NON_CLONEABLE_HISTORY_VARIABLE_ACCESS(name) *name
/**
* Initializes the non cloneable history variable by creating new memory on the heap
* This can safetly be passed down the stack without being cloned as the pointer
* will remain in tact on the heap
*/
#define NON_CLONEABLE_HISTORY_VARIABLE_INITIALIZE(name) name = calloc(sizeof(*name), 1)
// The current body that the parser is in
// Note: The set body may be uninitialized and should be used as reference only
// don't use functionality
extern struct node *parser_current_body;
// The current function we are in.
extern struct node *parser_current_function;
// NODE_TYPE_BLANK - Represents a node that does nothing, used so that we don't
// have to check for a NULL. when working with the tree.
struct node *parser_blank_node;
// The last token parsed by the parser, may be NULL
extern struct token *parser_last_token;
// First in the array = higher priority
// This array is special, its essentially a group of arrays
#define TOTAL_OPERATOR_GROUPS 14
#define MAX_OPERATORS_IN_GROUP 12
// Flags to represent history to try to make it easier to know what stage the parse process is at.
enum
{
// Signifies we are at the right operand of a structure access. I.e a.b "b" would be the right operand
HISTORY_FLAG_STRUCTURE_ACCESS_RIGHT_OPERAND = 0b00000001,
// Signifies the current code flow is inside of a structure right now.
// We are parsing a structure at this exact moment in time if this flag is set.
HISTORY_FLAG_INSIDE_STRUCTURE = 0b00000010,
// Specifies that we are outside of a function in the parsing process
// You can think of this as where global variables would be.
// Does not include situations where we are inside of a structure
HISTORY_FLAG_IS_GLOBAL_SCOPE = 0b00000100,
// This flag is set if the stack is growing upwards, i.e function arguments
HISTORY_FLAG_IS_UPWARD_STACK = 0b00001000,
HISTORY_FLAG_IS_EXPRESSION = 0b00010000,
HISTORY_FLAG_IN_SWITCH_STATEMENT = 0b00100000,
HISTORY_FLAG_INSIDE_FUNCTION_BODY = 0b01000000,
HISTORY_FLAG_PARENTHESES_IS_NOT_A_FUNCTION_CALL = 0b10000000,
HISTORY_FLAG_INSIDE_UNION = 0b100000000
};
// Expression flags
enum
{
ASSOCIATIVITY_LEFT_TO_RIGHT,
ASSOCIATIVITY_RIGHT_TO_LEFT
};
struct op_precedence_group
{
char *operators[MAX_OPERATORS_IN_GROUP];
int associativity;
};
/**
* Format:
* {operator1, operator2, operator3, NULL}
*
* end each group with NULL.
*
* Also end the collection of groups with a NULL pointer
*
* Found in expressionable.c
*/
extern struct op_precedence_group op_precedence[TOTAL_OPERATOR_GROUPS];
struct history_cases
{
// A vector of struct parsed_switch_case
struct vector *cases;
// Do we have a default case in the switch statement
bool has_default_case;
};
struct history
{
// Flags for this history.
int flags;
struct parser_history_switch
{
// We don't want our history cases to be cloned down stream
// This way we can change the memory where ever we are in the parse process.
NON_CLONEABLE_HISTORY_VARIABLE(struct history_cases, case_data);
} _switch;
};
static struct compile_process *current_process;
static struct fixup_system *parser_fixup_sys;
int parse_next();
void parse_statement(struct history *history);
void parse_expressionable_root(struct history *history);
void parse_expressionable_for_op(struct history *history, const char *op);
void parse_variable_function_or_struct_union(struct history *history);
void parse_keyword_return(struct history *history);
void parse_datatype_type(struct datatype *datatype);
void parse_datatype(struct datatype *datatype);
static struct history *history_down(struct history *history, int flags)
{
struct history *new_history = calloc(sizeof(struct history), 1);
memcpy(new_history, history, sizeof(struct history));
new_history->flags = flags;
return new_history;
}
static struct history *history_begin(struct history *history_out, int flags)
{
struct history *new_history = calloc(sizeof(struct history), 1);
new_history->flags = flags;
return new_history;
}
#define parse_err(...) \
compiler_error(current_process, __VA_ARGS__)
#define parser_err(...) \
compiler_error(current_process, __VA_ARGS__)
#define parser_scope_new() \
scope_new(current_process, 0)
#define parser_scope_finish() \
scope_finish(current_process)
#define parser_scope_push(value, elem_size) \
scope_push(current_process, value, elem_size)
#define parser_scope_last_entity() \
scope_last_entity(current_process)
#define parser_scope_last_entity_stop_global_scope() \
scope_last_entity_stop_at(current_process, current_process->scope.root)
#define parser_scope_current() \
scope_current(current_process)
int size_of_struct(const char *struct_name);
void parse_variable(struct datatype *dtype, struct token *name_token, struct history *history);
struct parser_history_switch parser_new_switch_statement(struct history *history)
{
memset(&history->_switch, 0x00, sizeof(history->_switch));
NON_CLONEABLE_HISTORY_VARIABLE_INITIALIZE(history->_switch.case_data);
history->_switch.case_data->cases = vector_create(sizeof(struct parsed_switch_case));
history->flags |= HISTORY_FLAG_IN_SWITCH_STATEMENT;
return history->_switch;
}
void parser_end_switch_statement(struct parser_history_switch *switch_history)
{
// Do nothing.
}
void parser_register_case(struct history *history, struct node *case_node)
{
assert(history->flags & HISTORY_FLAG_IN_SWITCH_STATEMENT);
struct parsed_switch_case scase;
scase.index = case_node->stmt._case.exp->llnum;
vector_push(history->_switch.case_data->cases, &scase);
}
// Simple bitmask for scope entity rules.
enum
{
PARSER_SCOPE_ENTITY_ON_STACK = 0b00000001,
PARSER_SCOPE_ENTITY_STRUCTURE_SCOPE = 0b00000010,
};
struct parser_scope_entity
{
// The flags for this scope entity
int flags;
// The stack offset this scope entity can be accessed at.
// i.e -4, -8 -12
// If this scope entity has no stack entity as its a global scope
// then this value should be ignored.
int stack_offset;
// A node to a variable declaration.
struct node *node;
};
int parser_get_random_type_index()
{
static int x = 0;
x++;
return x;
}
struct token *parser_build_random_type_name()
{
char tmp_name[25];
sprintf(tmp_name, "customtypeamenNI_%i", parser_get_random_type_index());
char *sval = malloc(sizeof(tmp_name));
strncpy(sval, tmp_name, sizeof(tmp_name));
struct token *token = calloc(sizeof(struct token), 1);
token->type = TOKEN_TYPE_IDENTIFIER;
token->sval = sval;
return token;
}
struct parser_scope_entity *parser_new_scope_entity(struct node *node, int stack_offset, int flags)
{
struct parser_scope_entity *entity = calloc(sizeof(struct parser_scope_entity), 1);
entity->node = node;
entity->flags = flags;
entity->stack_offset = stack_offset;
return entity;
}
void parser_scope_offset_for_stack(struct node *node, struct history *history)
{
struct parser_scope_entity *last_entity = parser_scope_last_entity_stop_global_scope();
bool upward_stack = history->flags & HISTORY_FLAG_IS_UPWARD_STACK;
int offset = -variable_size(node);
if (upward_stack)
{
// Do not use the variable size for the offset on an upward stack.
// The reason is because PUSH instructions consume 4 bytes of the stack,
// therefore we cannot trust anything that does not modulo into four.
// We will have to access everything as an integer and cast it down into
// the correct data type.
// Are we also the first entity? If so then the offset must start at EIGHT Bytes
size_t stack_addition = function_node_argument_stack_addition(parser_current_function);
offset = stack_addition;
if (last_entity)
{
offset = datatype_size(&variable_node(last_entity->node)->var.type);
}
}
if (last_entity)
{
offset += variable_node(last_entity->node)->var.aoffset;
if (variable_node_is_primative(node))
{
variable_node(node)->var.padding = padding(upward_stack ? offset : -offset, node->var.type.size);
variable_node(last_entity->node)->var.padding_after = node->var.padding;
}
}
bool first_entity = !last_entity;
// If this is a structure variable then we must align the padding to a 4-byte boundary so long
// as their was any padding in the original structure scope
// \attention Maybe make a new function for second operand, a bit long...
if (node_is_struct_or_union_variable(node) && variable_struct_or_union_body_node(node)->body.padded)
{
variable_node(node)->var.padding = padding(upward_stack ? offset : -offset, DATA_SIZE_DWORD);
}
variable_node(node)->var.aoffset = offset + (upward_stack ? variable_node(node)->var.padding : -variable_node(node)->var.padding);
}
void parser_scope_offset_for_structure(struct node *node, struct history *history)
{
int offset = 0;
struct parser_scope_entity *last_entity = parser_scope_last_entity();
if (last_entity)
{
offset += last_entity->stack_offset + last_entity->node->var.type.size;
if (variable_node_is_primative(node))
{
node->var.padding = padding(offset, node->var.type.size);
last_entity->node->var.padding_after = node->var.padding;
}
node->var.aoffset = offset + node->var.padding;
}
}
int parser_scope_offset_for_global(struct node *node, struct history *history)
{
// We don't have global scopes. Each variable is independant
return 0;
}
void parser_scope_offset(struct node *node, struct history *history)
{
if (history->flags & HISTORY_FLAG_INSIDE_STRUCTURE)
{
parser_scope_offset_for_structure(node, history);
return;
}
if (history->flags & HISTORY_FLAG_IS_GLOBAL_SCOPE)
{
parser_scope_offset_for_global(node, history);
return;
}
parser_scope_offset_for_stack(node, history);
}
static int parser_get_precedence_for_operator(const char *op, struct op_precedence_group **group_out)
{
*group_out = NULL;
for (int i = 0; i < TOTAL_OPERATOR_GROUPS; i++)
{
for (int b = 0; op_precedence[i].operators[b]; b++)
{
const char *_op = op_precedence[i].operators[b];
if (S_EQ(op, _op))
{
*group_out = &op_precedence[i];
return i;
}
}
}
return -1;
}
static bool parser_left_op_has_priority(const char *op_left, const char *op_right)
{
struct op_precedence_group *group_left = NULL;
struct op_precedence_group *group_right = NULL;
// Same operator? Then they have equal priority!
if (S_EQ(op_left, op_right))
return false;
int precedence_left = parser_get_precedence_for_operator(op_left, &group_left);
int precedence_right = parser_get_precedence_for_operator(op_right, &group_right);
if (group_left->associativity == ASSOCIATIVITY_RIGHT_TO_LEFT)
{
// Right to left associativity in the left group? and right group left_to_right?
// Then right group takes priority
return false;
}
return precedence_left <= precedence_right;
}
static bool parser_ops_equal_priority(const char *op_left, const char *op_right)
{
struct op_precedence_group *group_left = NULL;
struct op_precedence_group *group_right = NULL;
int precedence_left = parser_get_precedence_for_operator(op_left, &group_left);
int precedence_right = parser_get_precedence_for_operator(op_right, &group_right);
if (group_left->associativity == ASSOCIATIVITY_RIGHT_TO_LEFT)
{
// Right to left associativity in the left group? and right group left_to_right?
// Then right group takes priority
return false;
}
return precedence_left == precedence_right;
}
static bool parser_is_unary_operator(const char *op)
{
return is_unary_operator(op);
}
static bool token_is_nl_or_comment_or_newline_seperator(struct token *token)
{
return token->type == TOKEN_TYPE_NEWLINE ||
token->type == TOKEN_TYPE_COMMENT ||
token_is_symbol(token, '\\');
}
static void parser_ignore_nl_or_comment(struct token *next_token)
{
while (next_token && token_is_nl_or_comment_or_newline_seperator(next_token))
{
// Skip the token
vector_peek(current_process->token_vec);
// The parser does not care about new lines, only the preprocessor has to care about that
next_token = vector_peek_no_increment(current_process->token_vec);
}
}
static struct token *token_next()
{
struct token *next_token = vector_peek_no_increment(current_process->token_vec);
parser_ignore_nl_or_comment(next_token);
current_process->pos = next_token->pos;
parser_last_token = next_token;
return vector_peek(current_process->token_vec);
}
static struct token *token_peek_next()
{
struct token *next_token = vector_peek_no_increment(current_process->token_vec);
parser_ignore_nl_or_comment(next_token);
return vector_peek_no_increment(current_process->token_vec);
}
static bool token_next_is_operator(const char *op)
{
struct token *token = token_peek_next();
return token_is_operator(token, op);
}
static bool token_next_is_keyword(const char *keyword)
{
struct token *token = token_peek_next();
return token_is_keyword(token, keyword);
}
static bool token_next_is_symbol(char sym)
{
struct token *token = token_peek_next();
return token_is_symbol(token, sym);
}
static struct token *token_next_expected(int type)
{
struct token *token = token_next();
if (token->type != type)
parse_err("Unexpected token\n");
return token;
}
int parser_get_pointer_depth()
{
int depth = 0;
while (token_next_is_operator("*"))
{
depth += 1;
token_next();
}
return depth;
}
static void expect_sym(char c)
{
struct token *next_token = token_next();
if (next_token == NULL || next_token->type != TOKEN_TYPE_SYMBOL || next_token->cval != c)
parse_err("Expecting the symbol %c but something else was provided", c);
}
static void expect_op(const char *op)
{
struct token *next_token = token_next();
if (next_token == NULL || next_token->type != TOKEN_TYPE_OPERATOR || !S_EQ(next_token->sval, op))
parse_err("Expecting the operator %s but something else was provided", op);
}
static void expect_keyword(const char *keyword)
{
struct token *next_token = token_next();
assert(next_token);
assert(next_token->type == TOKEN_TYPE_KEYWORD);
assert(S_EQ(next_token->sval, keyword));
if (next_token == NULL || next_token->type != TOKEN_TYPE_KEYWORD || !S_EQ(next_token->sval, keyword))
parse_err("Expecting keyword %s however something else was provided", keyword);
}
void token_read_dots(size_t total)
{
for (int i = 0; i < total; i++)
{
expect_op(".");
}
}
static bool is_keyword_variable_modifier(const char *val)
{
return S_EQ(val, "unsigned") ||
S_EQ(val, "signed") ||
S_EQ(val, "static") ||
S_EQ(val, "const") ||
S_EQ(val, "extern") ||
S_EQ(val, "__ignore_typecheck__");
}
void parse_single_token_to_node()
{
struct token *token = token_next();
struct node *node = NULL;
switch (token->type)
{
case TOKEN_TYPE_NUMBER:
node = node_create(&(struct node){NODE_TYPE_NUMBER, .llnum = token->llnum});
break;
case TOKEN_TYPE_IDENTIFIER:
node = node_create(&(struct node){NODE_TYPE_IDENTIFIER, .sval = token->sval});
break;
case TOKEN_TYPE_STRING:
node = node_create(&(struct node){NODE_TYPE_STRING, .sval = token->sval});
break;
default:
parse_err("Problem converting token to node. No valid node exists for token of type %i\n", token->type);
}
}
struct datatype_struct_node_fix_private
{
// The variable node whose data type must be fixed as the structure is now present.
struct node *node;
};
bool datatype_struct_node_fix(struct fixup *fixup)
{
struct datatype_struct_node_fix_private *private = fixup_private(fixup);
struct datatype *dtype = &private->node->var.type;
dtype->type = DATA_TYPE_STRUCT;
dtype->size = size_of_struct(dtype->type_str);
dtype->struct_node = struct_node_for_name(current_process, dtype->type_str);
// Still couldnt resolve it? Then they haven't declared it anywhere and lied to us!
// There is no structure with the name dtype->type_str.
if (!dtype->struct_node)
{
return false;
}
return true;
}
void datatype_struct_node_fix_end(struct fixup *fixup)
{
// Let's free the fixup private
free(fixup_private(fixup));
}
void make_variable_node(struct datatype *datatype, struct token *name_token, struct node *value_node)
{
const char *name_str = NULL;
if (name_token)
{
name_str = name_token->sval;
}
node_create(&(struct node){NODE_TYPE_VARIABLE, .var.type = *datatype, .var.name = name_str, .var.val = value_node});
struct node *var_node = node_peek_or_null();
assert(var_node);
// Is our struct node NULL? Then a fixup is required a forward declaration was present
// Could argue that this is not the most sensible place to put it
// but this is a gauranteed way that a fixup will be registered.
if (var_node->var.type.type == DATA_TYPE_STRUCT && !var_node->var.type.struct_node)
{
struct datatype_struct_node_fix_private *private = calloc(sizeof(struct datatype_struct_node_fix_private), 1);
private
->node = var_node;
fixup_register(parser_fixup_sys, &(struct fixup_config){.fix = datatype_struct_node_fix, .end = datatype_struct_node_fix_end, .private = private});
}
}
void make_variable_node_and_register(struct history *history, struct datatype *datatype, struct token *name_token, struct node *value_node)
{
make_variable_node(datatype, name_token, value_node);
struct node *var_node = node_pop();
// Calculate scope offset
parser_scope_offset(var_node, history);
parser_scope_push(parser_new_scope_entity(var_node, var_node->var.aoffset, 0), var_node->var.type.size);
resolver_default_new_scope_entity(current_process->resolver, var_node, var_node->var.aoffset, 0);
// Push the variable node back to the stack
node_push(var_node);
}
void make_variable_list_node(struct vector *var_list)
{
node_create(&(struct node){NODE_TYPE_VARIABLE_LIST, .var_list.list = var_list});
}
void make_bracket_node(struct node *inner_node)
{
node_create(&(struct node){NODE_TYPE_BRACKET, .bracket.inner = inner_node});
}
void parse_expressionable(struct history *history);
void parse_for_parentheses(struct history *history);
static void parser_append_size_for_node_struct_union(struct history *history, size_t *_variable_size, struct node *node)
{
*_variable_size += variable_size(node);
if (node->var.type.flags & DATATYPE_FLAG_IS_POINTER)
{
// A pointer struct? Well we don't want to do anything else than this
return;
}
struct node *largest_var_node = variable_struct_or_union_body_node(node)->body.largest_var_node;
if (largest_var_node)
{
// Great we need to align to its largest datatype boundary ((Way to large, make a function for that mess))
*_variable_size = align_value(*_variable_size, largest_var_node->var.type.size);
}
}
void parser_append_size_for_node(struct history *history, size_t *_variable_size, struct node *node);
void parser_append_size_for_variable_list(struct history *history, size_t *_variable_size, struct vector *var_list)
{
vector_set_peek_pointer(var_list, 0);
struct node *node = vector_peek_ptr(var_list);
while (node)
{
parser_append_size_for_node(history, _variable_size, node);
node = vector_peek_ptr(var_list);
}
}
void parser_append_size_for_node(struct history *history, size_t *_variable_size, struct node *node)
{
if (!node)
return;
if (node->type == NODE_TYPE_VARIABLE)
{
// Is this a structure variable?
if (node_is_struct_or_union_variable(node))
{
parser_append_size_for_node_struct_union(history, _variable_size, node);
return;
}
// Normal variable, okay lets append the size.
// Ok we have a variable lets adjust the variable_size.
// *variable_size += node->var.type.size;
// Test new with all possibilities.
*_variable_size += variable_size(node);
}
else if (node->type == NODE_TYPE_VARIABLE_LIST)
{
parser_append_size_for_variable_list(history, _variable_size, node->var_list.list);
}
}
void parser_finalize_body(struct history *history, struct node *body_node, struct vector *body_vec, size_t *_variable_size, struct node *largest_align_eligible_var_node, struct node *largest_possible_var_node)
{
if (history->flags & HISTORY_FLAG_INSIDE_UNION)
{
// Unions variable size is equal to the largest variable node size
if (largest_possible_var_node)
{
*_variable_size = variable_size(largest_possible_var_node);
}
}
// Variable size should be adjusted to + the padding of all the body variables padding
int padding = compute_sum_padding(body_vec);
*_variable_size += padding;
// Our own variable size must pad to the largest member
if (largest_align_eligible_var_node)
{
*_variable_size = align_value(*_variable_size, largest_align_eligible_var_node->var.type.size);
}
// Let's make the body node now we have parsed all statements.
bool padded = padding != 0;
body_node->body.largest_var_node = largest_align_eligible_var_node;
body_node->body.padded = padded;
body_node->body.size = *_variable_size;
body_node->body.statements = body_vec;
}
/**
* Parses a single body statement and in the event the statement is a variable
* the variable_size variable will be incremented by the size of the variable
* in this statement
*/
void parse_body_single_statement(size_t *variable_size, struct vector *body_vec, struct history *history)
{
// We will create a blank body node here as we need it as a reference
make_body_node(NULL, 0, NULL, NULL);
struct node *body_node = node_pop();
body_node->binded.owner = parser_current_body;
parser_current_body = body_node;
struct node *stmt_node = NULL;
parse_statement(history_down(history, history->flags));
stmt_node = node_pop();
vector_push(body_vec, &stmt_node);
// Change the variable_size if this statement is a variable.
// Incrementing it by the size of our variable
parser_append_size_for_node(history, variable_size, variable_node_or_list(stmt_node));
struct node *largest_var_node = NULL;
if (stmt_node->type == NODE_TYPE_VARIABLE)
{
largest_var_node = stmt_node;
}
parser_finalize_body(history, body_node, body_vec, variable_size, largest_var_node, largest_var_node);
// Set the parser body node back to the previous one now that we are done.
parser_current_body = body_node->binded.owner;
// Push the body node back to the stack
node_push(body_node);
}
/**
* Parses the body_vec vector and for any variables the variable size is calculated
* and added to the variable_size variable
*/
void parse_body_multiple_statements(size_t *variable_size, struct vector *body_vec, struct history *history)
{
// We will create a blank body node here as we need it as a reference
make_body_node(NULL, 0, NULL, NULL);
struct node *body_node = node_pop();
body_node->binded.owner = parser_current_body;
parser_current_body = body_node;
struct node *stmt_node = NULL;
struct node *largest_align_eligible_var_node = NULL;
struct node *largest_possible_var_node = NULL;
// Ok we are parsing a full body with many statements.
expect_sym('{');
while (!token_next_is_symbol('}'))
{
parse_statement(history_down(history, history->flags));
stmt_node = node_pop();
if (stmt_node->type == NODE_TYPE_VARIABLE)
{
if (!largest_possible_var_node ||
(largest_possible_var_node->var.type.size <= stmt_node->var.type.size))
{
largest_possible_var_node = stmt_node;
}
}
if (stmt_node->type == NODE_TYPE_VARIABLE && variable_node_is_primative(stmt_node))
{
if (!largest_align_eligible_var_node ||
(largest_align_eligible_var_node->var.type.size <= stmt_node->var.type.size))
{
largest_align_eligible_var_node = stmt_node;
}
}
vector_push(body_vec, &stmt_node);
// Change the variable_size if this statement is a variable.
// Incrementing it by the size of our variable
parser_append_size_for_node(history, variable_size, variable_node_or_list(stmt_node));
}
// bodies must end with a right curley bracket!
expect_sym('}');
// We must finalize the body
parser_finalize_body(history, body_node, body_vec, variable_size, largest_align_eligible_var_node, largest_possible_var_node);
// Let's not forget to set the old body back now that we are done with this body
parser_current_body = body_node->binded.owner;
// Push the body node back to the stack
node_push(body_node);
}
/**
* Parses the body and stores the collective variable size in the "variable_size" variable
* This can be useful for parsing a structures body and having the size returned of the
* combined variables.
*
* Note the size will not be 16-bit aligned as required in the C standard.
*/
void parse_body(size_t *variable_size, struct history *history)
{
parser_scope_new();
resolver_default_new_scope(current_process->resolver, 0);
// We must always have a variable size pointer
// if the caller doesn't care about the size we will make our own.
size_t tmp_size = 0x00;
if (!variable_size)
{
variable_size = &tmp_size;
}
struct vector *body_vec = vector_create(sizeof(struct node *));
// We don't have a left curly? Then this body composes of only one statement
if (!token_next_is_symbol('{'))
{
parse_body_single_statement(variable_size, body_vec, history);
return;
}
// We got a couple of statements between curly braces {int a; int b;}
parse_body_multiple_statements(variable_size, body_vec, history);
resolver_default_finish_scope(current_process->resolver);
parser_scope_finish();
// If this scope is inside a function lets add the variable size to the stack size
// of the given function
if (variable_size)
{
if (history->flags & HISTORY_FLAG_INSIDE_FUNCTION_BODY)
{
parser_current_function->func.stack_size += *variable_size;
}
}
}
/**
* Shifts the children of the node to the left.
*
* I.e 50*E(20+120) will become E(50*20)+120
*/
void parser_node_shift_children_left(struct node *node)
{
assert(node->type == NODE_TYPE_EXPRESSION);
assert(node->exp.right->type == NODE_TYPE_EXPRESSION);
const char *right_op = node->exp.right->exp.op;
struct node *new_exp_left_node = node->exp.left;
struct node *new_exp_right_node = node->exp.right->exp.left;
// Make the new left operand
make_exp_node(new_exp_left_node, new_exp_right_node, node->exp.op);
struct node *new_left_operand = node_pop();
struct node *new_right_operand = node->exp.right->exp.right;
node->exp.left = new_left_operand;
node->exp.right = new_right_operand;
node->exp.op = right_op;
}
void parser_node_move_right_left_to_left(struct node *node)
{
make_exp_node(node->exp.left, node->exp.right->exp.left, node->exp.op);
struct node *completed_node = node_pop();
// Now we still have the right node to worry about
const char *new_op = node->exp.right->exp.op;
node->exp.left = completed_node;
node->exp.right = node->exp.right->exp.right;
node->exp.op = new_op;
}
/**
* Swaps the left node with the right node.
*/
void parser_node_flip_children(struct node *node)
{
struct node *tmp = node->exp.left;
node->exp.left = node->exp.right;
node->exp.right = tmp;
}
/**
* Reorders the given expression and its children, based on operator priority. I.e
* multiplication takes priority over addition.
*/
void parser_reorder_expression(struct node **node_out)
{
struct node *node = *node_out;
// The node passed to us has to be an expression
if (node->type != NODE_TYPE_EXPRESSION)
{
return;
}
// No expressions nothing to do
if (node->exp.left->type != NODE_TYPE_EXPRESSION && node->exp.right &&
node->exp.right->type != NODE_TYPE_EXPRESSION)
{
return;
}
// If we have a right expression but left is not an expression
// then some reordering may be needed
if (node->exp.left->type != NODE_TYPE_EXPRESSION && node->exp.right &&
node->exp.right->type == NODE_TYPE_EXPRESSION)
{
const char *right_op = node->exp.right->exp.op;
// We have something like 50+E(20+90)
// We must find the priority operator
if (parser_left_op_has_priority(node->exp.op, right_op))
{
// We have something like 50*E(20+120)
// We must produce the result E(50*20)+120
parser_node_shift_children_left(node);
// Reorder the shifted children.
parser_reorder_expression(&node->exp.left);
parser_reorder_expression(&node->exp.right);
}
}
// Should optimize the priority array rather than statics
// Todo...
if ((is_array_node(node->exp.left) && is_node_assignment(node->exp.right)) || ((node_is_expression(node->exp.left, "()") || node_is_expression(node->exp.left, "[]")) &&
node_is_expression(node->exp.right, ",")))
{
// We have a comma here and an expression to the left, therefore left operand
// of right node must be moved
parser_node_move_right_left_to_left(node);
}
}
int parse_expressionable_single(struct history *history);
/**
* Used for pointer access unary i.e ***abc = 50;
*/
void parse_for_indirection_unary()
{
// We have an indirection operator.
// Let's calculate the pointer depth
int depth = parser_get_pointer_depth();
// Now lets parse the expression after this unary operator
struct history history;
parse_expressionable(history_begin(&history, EXPRESSION_IS_UNARY));
struct node *unary_operand_node = node_pop();
make_unary_node("*", unary_operand_node, 0);
struct node *unary_node = node_pop();
unary_node->unary.indirection.depth = depth;
node_push(unary_node);
}
void parse_for_cast()
{
// ( is parsed by the caller.
struct datatype dtype;
parse_datatype(&dtype);
expect_sym(')');
struct history history;
parse_expressionable(&history);
struct node *operand_node = node_pop();
make_cast_node(&dtype, operand_node);
}
void parse_for_normal_unary()
{
const char *unary_op = token_next()->sval;
// Now lets parse the expression after this unary operator
struct history history;
parse_expressionable(history_begin(&history, EXPRESSION_IS_UNARY));
struct node *unary_operand_node = node_pop();
make_unary_node(unary_op, unary_operand_node, 0);
}
void parser_deal_with_additional_expression()
{
// We got an operator? If so theirs an expression after this
if (is_operator_token(token_peek_next()))
{
// Alright lets deal with it.
struct history history;
// parse_expressionable will deal with the operator
// as the unary has now been pushed to the stack
// it shall be popped off and merged into a new expression.
parse_expressionable(history_begin(&history, 0));
}
}
void parse_for_unary()
{
// Let's get the unary operator
const char *unary_op = token_peek_next()->sval;
if (op_is_indirection(unary_op))
{
parse_for_indirection_unary();
return;
}
// Read the normal unary
parse_for_normal_unary();
// We should deal with any additional expression if there is one.
parser_deal_with_additional_expression();
}
void parse_struct_no_new_scope(struct datatype *dtype, bool is_forward_declaration)
{