-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
2217 lines (1915 loc) · 58.1 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.
Just represents a node that is `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_UPWARDS_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_cases. */
struct vector* cases;
/* Is there a default keyword in the switch statement body. */
bool has_default_case;
};
/* History system */
struct history
{
int flags;
struct parser_history_switch
{
struct history_cases case_data;
} _switch;
};
struct history*
history_begin (int flags)
{
struct history* history = calloc(1, sizeof(struct history));
history->flags = flags;
return history;
}
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.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 s_case;
s_case.index = case_node->stmt._case.exp_node->llnum;
/* In C all cases are numerical. */
vector_push (history->_switch.case_data.cases, &s_case);
}
int parse_expressionable_single (struct history* history);
void parse_expressionable (struct history* history);
void parse_body_single_statement (size_t* variable_size, struct vector* body_vec, 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);
/*
* The parser does not care about new lines,
* only the preprocessor needs to worry about that.
*/
token = vector_peek_no_increment(current_process->token_vec);
}
}
/* Grad the next token to process. */
static struct token
*token_next ()
{
struct token *next_token = vector_peek_no_increment(current_process->token_vec);
/* Ignore the newline or comment tokens. */
parser_ignore_nl_or_comment(next_token);
/*
* We need to know the line and column we are currently
* processing to be used in errors messages.
*/
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);
/* Ignore the newline or comment tokens. */
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 (const 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 j = 0; op_precedence[i].operators[j]; j++)
{
const char* _op = op_precedence[i].operators[j];
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 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->associtivity == ASSOCIATIVITY_RIGHT_TO_LEFT)
{
return false;
}
return precedence_left <= precedence_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 ();
/* 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;
}
/*
* e.i 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);
/* We just changed the abstract syntax tree, so we now need to
* call this reorder function on itself, so that it can do through
* it again, and reorder if necessary. Because we have made a change
* that might affect the rest of the tree. */
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.right, ",")))
{
parser_node_move_right_left_to_left (node);
}
}
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 the last node is not compatible to an expression, return. */
if (!node_left)
{
return;
}
/* Pop off the operator token. */
token_next();
/* Pop off the left node. */
node_pop();
node_left->flags |= NODE_FLAG_INSIDE_EXPRESSION;
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.
* Multiplication has priority over addition.
*/
parser_reorder_expression(&exp_node);
/* Push it back to the stack. */
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 ();
}
/* 50+20) */
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)
{
/* Pop off the parenthesis node just created. */
struct node* parentheses_node = node_pop ();
/* We end up with a left node: `test` and a right node: `(50+2). */
make_exp_node (left_node, parentheses_node, "()");
}
parser_deal_with_additional_expression ();
}
void
parse_for_comma (struct history* history)
{
/* Skip the comma. */
token_next ();
/* `50, 30`
`50` would be set as left_node. */
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 ();
}
/* `[50]` */
expect_op ("[");
parse_expressionable_root (history); /* <+ */
expect_sym (']'); /* | */
/* | */
/* Pop off the expression ---------------+ */
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 (void)
{
/* `(` is already parsed i.e `(char)` seen as `char)`
when it has entered this function. */
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);
}
/*
* Responsible for parsing an operator,
* and merging with the right operand.
*/
int
parse_exp (struct history* history)
{
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, "statis" ) ||
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, "restrict"))
{
dtype->flags |= DATATYPE_FLAG_IS_RESTRICT;
}
else if (S_EQ(token->sval, "__ignore_typecheck__"))
{
dtype->flags |= DATATYPE_FLAG_IS_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_keywords(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_pointer_depth()
{
int depth = 0;
while (token_next_is_operator("*"))
{
depth++;
token_next();
}
return depth;
}
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;
}
bool
parser_datatype_is_secondary_allowed_for_type (const char* type)
{
/* e.i `long long` is allowed. */
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)
{
if (parser_datatype_is_secondary_allowed_for_type(datatype_token->sval), datatype_secondary_token)
{
compiler_error(current_process, "You are not allowed a secondary datatype here for the given datatype.");
}
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;
/* This is to be cahnged later. For now, keep as DWORD. */
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->type = DATA_TYPE_DOUBLE;
datatype_out->size = DATA_SIZE_DWORD;
}
else
{
compiler_error(current_process, "BUG: Invalid primitve datatype.\n");
}
/*
* Adjust the size of the datatype according to the secondary one (if any).
* e.i if `long long` is provided, it will adjust the size from 4 to 8.
*/
parser_datatype_adjust_size_for_secondary(datatype_out, datatype_secondary_token);
}
bool
parser_datatype_is_secondary_allowed (int expected_type)
{
return expected_type == DATA_TYPE_EXPECT_PRIMITIVE;
}
size_t
size_of_union (const char* union_name)
{
struct symbol* sym = symbol_resolver_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;
}
size_t
size_of_struct (const char* struct_name)
{
struct symbol* sym = symbol_resolver_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;
}
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)
{
/*
* Need to validate the `datatye_secondary_token` becasue,
* for example, `struct int` is not allowed.
*/
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->union_node = union_node_for_name(current_process, datatype_token->sval);
break;
default:
compiler_error(current_process, "BUG: Unsupported datatype expectation.\n");
}
}
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;
}
}
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;
}
/* e.i `struct love_tania;` */
void
parse_datatype_type (struct datatype* dtype)
{
struct token* datatype_token = NULL;
struct token* datatype_secondary_token = NULL;
/*
* Define the datatype_token as `struct`.
* Define the datatype_secondary_token as `NULL`.
*/
parser_get_datatype_tokens(&datatype_token, &datatype_secondary_token);
/* Define the `expected_type` as `DATA_TYPE_EXPECTED_STRUCT`. */
int expected_type = parser_datatype_expected_for_type_string(datatype_token->sval);
if (datatype_is_struct_or_union_for_name(datatype_token->sval))
{
/* Here we parse the name "love_tania". */
if (token_peek_next()->type == TOKEN_TYPE_IDENTIFIER)
{
datatype_token = token_next();
}
else
{
/*
* Not all structs have names.
* e.i `struct { int x; } love_tania;`
*/
datatype_token = parser_build_random_type_name();
dtype->flags |= DATATYPE_FLAG_STRUCT_UNION_NO_NAME;
}
}
/* e.i `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;
}
/*
* i.e long int abc;
* It will ignore the `int`, because `long int` or `long` is the same thing.
*/
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 \"init\" type however its not suported with this current abbreviation\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->type = 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));
}