-
Notifications
You must be signed in to change notification settings - Fork 36
/
parser.c
2051 lines (1774 loc) · 55.8 KB
/
parser.c
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 "helpers/vector.h"
#include <assert.h>
static struct compile_process *current_process;
static struct fixup_system *parser_fixup_sys;
static struct token *parser_last_token;
extern struct node *parser_current_body;
extern struct node *parser_current_function;
// NODE_TYPE_BLANK
struct node *parser_blank_node;
extern struct expressionable_op_precedence_group op_precedence[TOTAL_OPERATOR_GROUPS];
enum
{
PARSER_SCOPE_ENTITY_ON_STACK = 0b00000001,
PARSER_SCOPE_ENTITY_STRUCTURE_SCOPE = 0b00000010,
};
struct parser_scope_entity
{
// The entity flags of the scope entity
int flags;
// The stack offset of the scope entity.
int stack_offset;
// Variable declaration
struct node *node;
};
struct parser_scope_entity *parser_new_scope_entity(struct node *node, int stack_offset, int flags)
{
struct parser_scope_entity *entity = calloc(1, sizeof(struct parser_scope_entity));
entity->node = node;
entity->flags = flags;
entity->stack_offset = stack_offset;
return entity;
}
struct parser_scope_entity *parser_scope_last_entity_stop_global_scope()
{
return scope_last_entity_stop_at(current_process, current_process->scope.root);
}
enum
{
HISTORY_FLAG_INSIDE_UNION = 0b00000001,
HISTORY_FLAG_IS_UPWARD_STACK = 0b00000010,
HISTORY_FLAG_IS_GLOBAL_SCOPE = 0b00000100,
HISTORY_FLAG_INSIDE_STRUCTURE = 0b00001000,
HISTORY_FLAG_INSIDE_FUNCTION_BODY = 0b00010000,
HISTORY_FLAG_IN_SWITCH_STATEMENT = 0b00100000,
HISTORY_FLAG_PARENTHESES_IS_NOT_A_FUNCTION_CALL = 0b01000000,
};
struct history_cases
{
// A vector of parsed_switch_case
struct vector *cases;
// Is there a default keyword inthe switch statement body
bool has_default_case;
};
struct history
{
int flags;
struct parser_history_switch
{
struct history_cases* case_data;
} _switch;
};
int parser_get_pointer_depth();
void parser_deal_with_additional_expression();
void parse_for_parentheses(struct history *history);
static struct history *history_begin(int flags)
{
struct history *history = calloc(1, sizeof(struct history));
history->flags = flags;
return history;
}
static struct history *history_down(struct history *history, int flags)
{
struct history *new_history = calloc(1, sizeof(struct history));
memcpy(new_history, history, sizeof(struct history));
new_history->flags = flags;
return new_history;
}
struct parser_history_switch parser_new_switch_statement(struct history *history)
{
memset(&history->_switch, 0, sizeof(&history->_switch));
history->_switch.case_data = calloc(1, sizeof(struct history_cases));
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);
}
int parse_expressionable_single(struct history *history);
void parse_expressionable(struct history *history);
void parse_body(size_t *variable_size, struct history *history);
void parse_keyword(struct history *history);
struct vector *parse_function_arguments(struct history *history);
void parse_expressionable_root(struct history *history);
void parse_label(struct history *history);
void parse_for_tenary(struct history *history);
void parse_datatype(struct datatype *dtype);
void parse_for_cast();
void parser_scope_new()
{
scope_new(current_process, 0);
}
void parser_scope_finish()
{
scope_finish(current_process);
}
struct parser_scope_entity *parser_scope_last_entity()
{
return scope_last_entity(current_process);
}
void parser_scope_push(struct parser_scope_entity *entity, size_t size)
{
scope_push(current_process, entity, size);
}
static void parser_ignore_nl_or_comment(struct token *token)
{
while (token && token_is_nl_or_comment_or_newline_seperator(token))
{
// Skip the token
vector_peek(current_process->token_vec);
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);
if (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 c)
{
struct token *token = token_peek_next();
return token_is_symbol(token, c);
}
static void expect_sym(char c)
{
struct token *next_token = token_next();
if (!next_token || next_token->type != TOKEN_TYPE_SYMBOL || next_token->cval != c)
{
compiler_error(current_process, "Expecting symbol %c however something else was provided\n", c);
}
}
static void expect_op(const char *op)
{
struct token *next_token = token_next();
if (!next_token || next_token->type != TOKEN_TYPE_OPERATOR || !S_EQ(next_token->sval, op))
{
compiler_error(current_process, "Expecting the operator %s but something else was provided\n", next_token->sval);
}
}
static void expect_keyword(const char *keyword)
{
struct token *next_token = token_next();
if (!next_token || next_token->type != TOKEN_TYPE_KEYWORD || !S_EQ(next_token->sval, keyword))
{
compiler_error(current_process, "Expecting the keyword %s but something was provided\n", keyword);
}
}
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){.type = NODE_TYPE_NUMBER, .llnum = token->llnum});
break;
case TOKEN_TYPE_IDENTIFIER:
node = node_create(&(struct node){.type = NODE_TYPE_IDENTIFIER, .sval = token->sval});
break;
case TOKEN_TYPE_STRING:
node = node_create(&(struct node){.type = NODE_TYPE_STRING, .sval = token->sval});
break;
default:
compiler_error(current_process, "This is not a single token that can be converted to a node");
}
}
void parse_expressionable_for_op(struct history *history, const char *op)
{
parse_expressionable(history);
}
static int parser_get_precedence_for_operator(const char *op, struct expressionable_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 expressionable_op_precedence_group *group_left = NULL;
struct expressionable_op_precedence_group *group_right = NULL;
if (S_EQ(op_left, op_right))
{
return false;
}
int precdence_left = parser_get_precedence_for_operator(op_left, &group_left);
int precdence_right = parser_get_precedence_for_operator(op_right, &group_right);
if (group_left->associtivity == ASSOCIATIVITY_RIGHT_TO_LEFT)
{
return false;
}
return precdence_left <= precdence_right;
}
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_exp_node(new_exp_left_node, new_exp_right_node, node->exp.op);
// (50*20)
struct node *new_left_operand = node_pop();
// 120
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();
// We still need to deal with the right node
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;
}
void parser_reorder_expression(struct node **node_out)
{
struct node *node = *node_out;
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;
}
// 50*E(30+20)
// 50*EXPRESSION
// EXPRESSION(50*EXPRESSION(30+20))
// (50*30)+20
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;
if (parser_left_op_has_priority(node->exp.op, right_op))
{
// 50*E(20+120)
// E(50*20)+120
parser_node_shift_children_left(node);
parser_reorder_expression(&node->exp.left);
parser_reorder_expression(&node->exp.right);
}
}
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, ",")))
{
parser_node_move_right_left_to_left(node);
}
}
bool parser_is_unary_operator(const char *op)
{
return is_unary_operator(op);
}
void parse_for_indirection_unary()
{
int depth = parser_get_pointer_depth();
parse_expressionable(history_begin(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_normal_unary()
{
const char *unary_op = token_next()->sval;
parse_expressionable(history_begin(EXPRESSION_IS_UNARY));
struct node *unary_operand_node = node_pop();
make_unary_node(unary_op, unary_operand_node, 0);
}
void parse_for_unary()
{
const char *unary_op = token_peek_next()->sval;
if (op_is_indirection(unary_op))
{
parse_for_indirection_unary();
return;
}
parse_for_normal_unary();
parser_deal_with_additional_expression();
}
void parse_for_left_operanded_unary(struct node* left_operand_node, const char* unary_op)
{
make_unary_node(unary_op, left_operand_node, UNARY_FLAG_IS_LEFT_OPERANDED_UNARY);
}
void parse_exp_normal(struct history *history)
{
struct token *op_token = token_peek_next();
const char *op = op_token->sval;
struct node *node_left = node_peek_expressionable_or_null();
if (!node_left)
{
if (!parser_is_unary_operator(op))
{
compiler_error(current_process, "The given expression has no left operand");
}
parse_for_unary();
return;
}
// Pop off the operator token
token_next();
// Pop off the left node
node_pop();
if (is_left_operanded_unary_operator(op))
{
parse_for_left_operanded_unary(node_left, op);
return;
}
node_left->flags |= NODE_FLAG_INSIDE_EXPRESSION;
if (token_peek_next()->type == TOKEN_TYPE_OPERATOR)
{
if (S_EQ(token_peek_next()->sval, "("))
{
parse_for_parentheses(history_down(history, history->flags | HISTORY_FLAG_PARENTHESES_IS_NOT_A_FUNCTION_CALL));
}
else if (parser_is_unary_operator(token_peek_next()->sval))
{
parse_for_unary();
}
else
{
compiler_error(current_process, "Two operators are expected for a given expression for operator %s\n", token_peek_next()->sval);
}
}
else
{
parse_expressionable_for_op(history_down(history, history->flags), op);
}
struct node *node_right = node_pop();
node_right->flags |= NODE_FLAG_INSIDE_EXPRESSION;
make_exp_node(node_left, node_right, op);
struct node *exp_node = node_pop();
// Reorder the expression
parser_reorder_expression(&exp_node);
node_push(exp_node);
}
void parser_deal_with_additional_expression()
{
if (token_peek_next()->type == TOKEN_TYPE_OPERATOR)
{
parse_expressionable(history_begin(0));
}
}
void parse_for_parentheses(struct history *history)
{
expect_op("(");
if (token_peek_next()->type == TOKEN_TYPE_KEYWORD)
{
parse_for_cast();
return;
}
struct node *left_node = NULL;
struct node *tmp_node = node_peek_or_null();
// test(50+20)
if (tmp_node && node_is_value_type(tmp_node))
{
left_node = tmp_node;
node_pop();
}
struct node *exp_node = parser_blank_node;
if (!token_next_is_symbol(')'))
{
parse_expressionable_root(history_begin(0));
exp_node = node_pop();
}
expect_sym(')');
make_exp_parentheses_node(exp_node);
if (left_node)
{
struct node *parentheses_node = node_pop();
make_exp_node(left_node, parentheses_node, "()");
}
parser_deal_with_additional_expression();
}
void parse_for_comma(struct history *history)
{
// Skip the comma
token_next();
struct node *left_node = node_pop();
parse_expressionable_root(history);
struct node *right_node = node_pop();
make_exp_node(left_node, right_node, ",");
}
void parse_for_array(struct history *history)
{
struct node *left_node = node_peek_or_null();
if (left_node)
{
node_pop();
}
expect_op("[");
parse_expressionable_root(history);
expect_sym(']');
struct node *exp_node = node_pop();
make_bracket_node(exp_node);
if (left_node)
{
struct node *bracket_node = node_pop();
make_exp_node(left_node, bracket_node, "[]");
}
}
void parse_for_cast()
{
// "(" is already parsed i.e (char) seen as char)
struct datatype dtype = {};
parse_datatype(&dtype);
expect_sym(')');
parse_expressionable(history_begin(0));
struct node *operand_node = node_pop();
make_cast_node(&dtype, operand_node);
}
int parse_exp(struct history *history)
{
if (history->flags & EXPRESSION_IS_UNARY && !unary_operand_compatible(token_peek_next()))
{
return -1;
}
if (S_EQ(token_peek_next()->sval, "("))
{
parse_for_parentheses(history);
}
else if (S_EQ(token_peek_next()->sval, "["))
{
parse_for_array(history);
}
else if (S_EQ(token_peek_next()->sval, "?"))
{
parse_for_tenary(history);
}
else if (S_EQ(token_peek_next()->sval, ","))
{
parse_for_comma(history);
}
else
{
parse_exp_normal(history);
}
return 0;
}
void parse_identifier(struct history *history)
{
assert(token_peek_next()->type == TOKEN_TYPE_IDENTIFIER);
parse_single_token_to_node();
}
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_datatype_modifiers(struct datatype *dtype)
{
struct token *token = token_peek_next();
while (token && token->type == TOKEN_TYPE_KEYWORD)
{
if (!is_keyword_variable_modifier(token->sval))
{
break;
}
if (S_EQ(token->sval, "signed"))
{
dtype->flags |= DATATYPE_FLAG_IS_SIGNED;
}
else if (S_EQ(token->sval, "unsigned"))
{
dtype->flags &= ~DATATYPE_FLAG_IS_SIGNED;
}
else if (S_EQ(token->sval, "static"))
{
dtype->flags |= DATATYPE_FLAG_IS_STATIC;
}
else if (S_EQ(token->sval, "const"))
{
dtype->flags |= DATATYPE_FLAG_IS_CONST;
}
else if (S_EQ(token->sval, "extern"))
{
dtype->flags |= DATATYPE_FLAG_IS_EXTERN;
}
else if (S_EQ(token->sval, "__ignore_typecheck__"))
{
dtype->flags |= DATATYPE_FLAG_IGNORE_TYPE_CHECKING;
}
token_next();
token = token_peek_next();
}
}
void parser_get_datatype_tokens(struct token **datatype_token, struct token **datatype_secondary_token)
{
*datatype_token = token_next();
struct token *next_token = token_peek_next();
if (token_is_primitive_keyword(next_token))
{
*datatype_secondary_token = next_token;
token_next();
}
}
int parser_datatype_expected_for_type_string(const char *str)
{
int type = DATA_TYPE_EXPECT_PRIMITIVE;
if (S_EQ(str, "union"))
{
type = DATA_TYPE_EXPECT_UNION;
}
else if (S_EQ(str, "struct"))
{
type = DATA_TYPE_EXPECT_STRUCT;
}
return type;
}
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, "customtypename_%i", parser_get_random_type_index());
char *sval = malloc(sizeof(tmp_name));
strncpy(sval, tmp_name, sizeof(tmp_name));
struct token *token = calloc(1, sizeof(struct token));
token->type = TOKEN_TYPE_IDENTIFIER;
token->sval = sval;
return token;
}
int parser_get_pointer_depth()
{
int depth = 0;
while (token_next_is_operator("*"))
{
depth++;
token_next();
}
return depth;
}
bool parser_datatype_is_secondary_allowed(int expected_type)
{
return expected_type == DATA_TYPE_EXPECT_PRIMITIVE;
}
bool parser_datatype_is_secondary_allowed_for_type(const char *type)
{
return S_EQ(type, "long") || S_EQ(type, "short") || S_EQ(type, "double") || S_EQ(type, "float");
}
void parser_datatype_init_type_and_size_for_primitive(struct token *datatype_token, struct token *datatype_secondary_token, struct datatype *datatype_out);
void parser_datatype_adjust_size_for_secondary(struct datatype *datatype, struct token *datatype_secondary_token)
{
if (!datatype_secondary_token)
{
return;
}
struct datatype *secondary_data_type = calloc(1, sizeof(struct datatype));
parser_datatype_init_type_and_size_for_primitive(datatype_secondary_token, NULL, secondary_data_type);
datatype->size += secondary_data_type->size;
datatype->secondary = secondary_data_type;
datatype->flags |= DATATYPE_FLAG_IS_SECONDARY;
}
void parser_datatype_init_type_and_size_for_primitive(struct token *datatype_token, struct token *datatype_secondary_token, struct datatype *datatype_out)
{
if (!parser_datatype_is_secondary_allowed_for_type(datatype_token->sval) && datatype_secondary_token)
{
compiler_error(current_process, "Your not allowed a secondary datatype here for the given datatype %s\n", datatype_token->sval);
}
if (S_EQ(datatype_token->sval, "void"))
{
datatype_out->type = DATA_TYPE_VOID;
datatype_out->size = DATA_SIZE_ZERO;
}
else if (S_EQ(datatype_token->sval, "char"))
{
datatype_out->type = DATA_TYPE_CHAR;
datatype_out->size = DATA_SIZE_BYTE;
}
else if (S_EQ(datatype_token->sval, "short"))
{
datatype_out->type = DATA_TYPE_SHORT;
datatype_out->size = DATA_SIZE_WORD;
}
else if (S_EQ(datatype_token->sval, "int"))
{
datatype_out->type = DATA_TYPE_INTEGER;
datatype_out->size = DATA_SIZE_DWORD;
}
else if (S_EQ(datatype_token->sval, "long"))
{
datatype_out->type = DATA_TYPE_LONG;
datatype_out->size = DATA_SIZE_DWORD;
}
else if (S_EQ(datatype_token->sval, "float"))
{
datatype_out->type = DATA_TYPE_FLOAT;
datatype_out->size = DATA_SIZE_DWORD;
}
else if (S_EQ(datatype_token->sval, "double"))
{
datatype_out->size = DATA_TYPE_DOUBLE;
datatype_out->size = DATA_SIZE_DWORD;
}
else
{
compiler_error(current_process, "BUG: Invalid primitive datatype\n");
}
parser_datatype_adjust_size_for_secondary(datatype_out, datatype_secondary_token);
}
size_t size_of_struct(const char *struct_name)
{
struct symbol *sym = symresolver_get_symbol(current_process, struct_name);
if (!sym)
{
return 0;
}
assert(sym->type == SYMBOL_TYPE_NODE);
struct node *node = sym->data;
assert(node->type == NODE_TYPE_STRUCT);
return node->_struct.body_n->body.size;
}
size_t size_of_union(const char *union_name)
{
struct symbol *sym = symresolver_get_symbol(current_process, union_name);
if (!sym)
{
return 0;
}
assert(sym->type == SYMBOL_TYPE_NODE);
struct node *node = sym->data;
assert(node->type == NODE_TYPE_UNION);
return node->_union.body_n->body.size;
}
void parser_datatype_init_type_and_size(struct token *datatype_token, struct token *datatype_secondary_token, struct datatype *datatype_out, int pointer_depth, int expected_type)
{
if (!parser_datatype_is_secondary_allowed(expected_type) && datatype_secondary_token)
{
compiler_error(current_process, "You provided an invalid secondary datatype\n");
}
switch (expected_type)
{
case DATA_TYPE_EXPECT_PRIMITIVE:
parser_datatype_init_type_and_size_for_primitive(datatype_token, datatype_secondary_token, datatype_out);
break;
case DATA_TYPE_EXPECT_STRUCT:
datatype_out->type = DATA_TYPE_STRUCT;
datatype_out->size = size_of_struct(datatype_token->sval);
datatype_out->struct_node = struct_node_for_name(current_process, datatype_token->sval);
break;
case DATA_TYPE_EXPECT_UNION:
datatype_out->type = DATA_TYPE_UNION;
datatype_out->size = size_of_union(datatype_token->sval);
datatype_out->struct_node = union_node_for_name(current_process, datatype_token->sval);
break;
default:
compiler_error(current_process, "BUG: Unsupported datatype expectation\n");
}
if (pointer_depth > 0)
{
datatype_out->flags |= DATATYPE_FLAG_IS_POINTER;
datatype_out->pointer_depth = pointer_depth;
}
}
void parser_datatype_init(struct token *datatype_token, struct token *datatype_secondary_token, struct datatype *datatype_out, int pointer_depth, int expected_type)
{
parser_datatype_init_type_and_size(datatype_token, datatype_secondary_token, datatype_out, pointer_depth, expected_type);
datatype_out->type_str = datatype_token->sval;
if (S_EQ(datatype_token->sval, "long") && datatype_secondary_token && S_EQ(datatype_secondary_token->sval, "long"))
{
compiler_warning(current_process, "Our compiler does not support 64 bit longs, therefore your long long is defaulting to 32 bits\n");
datatype_out->size = DATA_SIZE_DWORD;
}
}
void parse_datatype_type(struct datatype *dtype)
{
struct token *datatype_token = NULL;
struct token *datatype_secondary_token = NULL;
parser_get_datatype_tokens(&datatype_token, &datatype_secondary_token);
int expected_type = parser_datatype_expected_for_type_string(datatype_token->sval);
if (datatype_is_struct_or_union_for_name(datatype_token->sval))
{
if (token_peek_next()->type == TOKEN_TYPE_IDENTIFIER)
{
datatype_token = token_next();
}
else
{
// This structure has no name, so we need to handle it.
datatype_token = parser_build_random_type_name();
dtype->flags |= DATATYPE_FLAG_STRUCT_UNION_NO_NAME;
}
}
// int**
int pointer_depth = parser_get_pointer_depth();
parser_datatype_init(datatype_token, datatype_secondary_token, dtype, pointer_depth, expected_type);
}
void parse_datatype(struct datatype *dtype)
{
memset(dtype, 0, sizeof(struct datatype));
dtype->flags |= DATATYPE_FLAG_IS_SIGNED;
parse_datatype_modifiers(dtype);
parse_datatype_type(dtype);
parse_datatype_modifiers(dtype);
}
bool parser_is_int_valid_after_datatype(struct datatype *dtype)
{
return dtype->type == DATA_TYPE_LONG || dtype->type == DATA_TYPE_FLOAT || dtype->type == DATA_TYPE_DOUBLE;
}
/**
* long int abc;
*
*/
void parser_ignore_int(struct datatype *dtype)
{
if (!token_is_keyword(token_peek_next(), "int"))
{
// No integer to ignore.
return;
}
if (!parser_is_int_valid_after_datatype(dtype))
{
compiler_error(current_process, "You provided a secondary \"int\" type however its not supported with this current abbrevation\n");
}
// Ignore the "int" token
token_next();
}
void parse_expressionable_root(struct history *history)
{
parse_expressionable(history);
struct node *result_node = node_pop();
node_push(result_node);
}
struct datatype_struct_node_fix_private
{
// Node to fix.
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);
if (!dtype->struct_node)
{
return false;
}
return true;
}
void datatype_struct_node_end(struct fixup *fixup)
{
free(fixup_private(fixup));
}
void make_variable_node(struct datatype *dtype, 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){.type = NODE_TYPE_VARIABLE, .var.name = name_str, .var.type = *dtype, .var.val = value_node});
struct node *var_node = node_peek_or_null();
if (var_node->var.type.type == DATA_TYPE_STRUCT && !var_node->var.type.struct_node)
{
struct datatype_struct_node_fix_private *private = calloc(1, sizeof(struct datatype_struct_node_fix_private));
private
->node = var_node;
fixup_register(parser_fixup_sys, &(struct fixup_config){.fix = datatype_struct_node_fix, .end = datatype_struct_node_end, .private = private});
}
}
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)
{
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_primitive(node))
{
variable_node(node)->var.padding = padding(upward_stack ? offset : -offset, node->var.type.size);
}
}
bool first_entity = !last_entity;
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_global(struct node *node, struct history *history)
{
}
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_primitive(node))
{