-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.h
1491 lines (1222 loc) · 40.5 KB
/
compiler.h
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
#ifndef FODOCOMPILER_H
#define FODOCOMPILER_H
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
#define PATH_MAX MAX_PATH
#elif __linux__
#include <linux/limits.h>
#elif __APPLE__
#include <limits.h>
#endif
#define FAIL_ERR(message) assert(0 == 1 && message)
#define S_EQ(str, str2) \
(str && str2 && (strcmp(str, str2) == 0))
// Lexer definitions
#define NUMERIC_CASE \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9'
#define OPERATOR_CASE_EXCLUDING_DIVISION \
case '+': \
case '-': \
case '*': \
case '>': \
case '<': \
case '^': \
case '%': \
case '!': \
case '=': \
case '~': \
case '|': \
case '&': \
case '(': \
case '[': \
case ',': \
case '.': \
case '?'
#define SYMBOL_CASE \
case '{': \
case '}': \
case ':': \
case ';': \
case '#': \
case '\\': \
case ')': \
case ']'
// Codegen definitions
#define STACK_PUSH_SIZE 4
#define C_STACK_ALIGNMENT 16
#define C_ALIGN(size) (size % C_STACK_ALIGNMENT) ? size + (C_STACK_ALIGNMENT - (size % C_STACK_ALIGNMENT)) : size
#define GENERATOR_BEGIN_EXPRESSION(gen)
#define GENERATOR_END_EXPRESSION(gen) gen->end_exp(gen)
// Lexer case defintions end
enum
{
COMPILER_FILE_COMPILED_OK,
COMPILER_FAILED_WITH_ERRORS
};
// Compiler options
enum
{
COMPILE_PROCESS_OUTPUT_ASM = 0b00000001,
COMPILE_PROCESS_EXECUTE_NASM = 0b00000010,
COMPILE_PROCESS_EXPORT_AS_OBJECT = 0b00000100
};
// Lexer enums
enum
{
LEXICAL_ANALYSIS_ALL_OK,
LEXICAL_ANALYSIS_INPUT_ERROR
};
// Preprocessor run enums
enum
{
PREPROCESSOR_ALL_OK,
PREPROCESSOR_GENERAL_ERROR
};
// Parser enums
enum
{
PARSE_ALL_OK,
PARSE_GENERAL_ERROR
};
// Validator enums
enum
{
VALIDATOR_ALL_OK,
VALIDATOR_GENERAL_ERROR
};
// Codegen enums
enum
{
CODEGEN_ALL_OK,
CODEGEN_GENERAL_ERROR
};
// Token types enum
enum
{
TOKEN_TYPE_IDENTIFIER,
TOKEN_TYPE_KEYWORD,
TOKEN_TYPE_OPERATOR,
TOKEN_TYPE_SYMBOL,
TOKEN_TYPE_NUMBER,
TOKEN_TYPE_STRING,
TOKEN_TYPE_COMMENT,
TOKEN_TYPE_NEWLINE
};
// Token number type enum
enum
{
NUMBER_TYPE_NORMAL,
NUMBER_TYPE_LONG,
NUMBER_TYPE_FLOAT,
NUMBER_TYPE_DOUBLE
};
enum
{
TOKEN_FLAG_IS_CUSTOM_OPERATOR = 0b00000001
};
// Node types enum start
enum
{
NODE_TYPE_EXPRESSION,
NODE_TYPE_EXPRESSION_PARENTHESES,
NODE_TYPE_NUMBER,
NODE_TYPE_IDENTIFIER,
NODE_TYPE_STRING,
NODE_TYPE_VARIABLE,
NODE_TYPE_VARIABLE_LIST,
NODE_TYPE_FUNCTION,
NODE_TYPE_BODY,
NODE_TYPE_STATEMENT_RETURN,
NODE_TYPE_STATEMENT_IF,
NODE_TYPE_STATEMENT_ELSE,
NODE_TYPE_STATEMENT_WHILE,
NODE_TYPE_STATEMENT_DO_WHILE,
NODE_TYPE_STATEMENT_FOR,
NODE_TYPE_STATEMENT_BREAK,
NODE_TYPE_STATEMENT_CONTINUE,
NODE_TYPE_STATEMENT_SWITCH,
NODE_TYPE_STATEMENT_CASE,
NODE_TYPE_STATEMENT_DEFAULT,
NODE_TYPE_STATEMENT_GOTO,
NODE_TYPE_UNARY,
NODE_TYPE_TENARY,
NODE_TYPE_LABEL,
NODE_TYPE_STRUCT,
NODE_TYPE_UNION,
NODE_TYPE_BRACKET,
NODE_TYPE_CAST,
NODE_TYPE_BLANK
};
// Node types enum end
// Node flags enum
enum
{
NODE_FLAG_INSIDE_EXPRESSION = 0b00000001,
NODE_FLAG_IS_FORWARD_DECLARATION = 0b00000010,
NODE_FLAG_HAS_VARIABLE_COMBINED = 0b00000100
};
// unary flags
enum
{
UNARY_FLAG_IS_LEFT_OPERAND_UNARY = 0b00000001
};
// < Expressionable definitions start
// < Datatype flags enum start
enum
{
DATATYPE_FLAG_IS_SIGNED = 0b00000001,
DATATYPE_FLAG_IS_STATIC = 0b00000010,
DATATYPE_FLAG_IS_CONST = 0b00000100,
DATATYPE_FLAG_IS_POINTER = 0b00001000,
DATATYPE_FLAG_IS_ARRAY = 0b00010000,
DATATYPE_FLAG_IS_EXTERN = 0b00100000,
DATATYPE_FLAG_IS_RESTRICT = 0b01000000,
DATATYPE_FLAG_IGNORE_TYPE_CHECKING = 0b10000000,
DATATYPE_FLAG_IS_SECONDARY = 0b100000000,
DATATYPE_FLAG_IS_STRUCT_UNION_NO_NAME = 0b1000000000,
DATATYPE_FLAG_IS_LITERAL = 0b10000000000
};
// > Datatype flags enum end
// < Data type enum start
enum
{
DATA_TYPE_VOID,
DATA_TYPE_CHAR,
DATA_TYPE_SHORT,
DATA_TYPE_INTEGER,
DATA_TYPE_LONG,
DATA_TYPE_FLOAT,
DATA_TYPE_DOUBLE,
DATA_TYPE_STRUCT,
DATA_TYPE_UNION,
DATA_TYPE_UNKNOWN
};
enum
{
DATA_TYPE_EXPECT_PRIMITIVE,
DATA_TYPE_EXPECT_UNION,
DATA_TYPE_EXPECT_STRUCT
};
enum
{
DATA_SIZE_ZERO = 0,
DATA_SIZE_BYTE = 1,
DATA_SIZE_WORD = 2,
DATA_SIZE_DWORD = 4,
DATA_SIZE_DDWORD = 8
};
// > Data type enum end
// < Structure flags enum start
enum
{
STRUCT_ACCESS_BACKWARDS = 0b00000001,
STRUCT_STOP_AT_POINTER_ACCESS = 0b00000010
};
// > Structure flags enum end
// < Symbol type enum start
enum
{
SYMBOL_TYPE_NODE,
SYMBOL_TYPE_NATIVE_FUNCTION,
SYMBOL_TYPE_UNKNOWN
};
// > Symnol type enum end
// < Function flags enum start
enum
{
// The flag is set for native functions
FUNCTION_NODE_FLAG_IS_NATIVE = 0b00000001
};
// > Function flags enum end
#define TOTAL_OPERATOR_GROUPS 14
#define MAX_OPERATORS_IN_GROUP 12
enum
{
ASSOCIATIVITY_LEFT_TO_RIGHT,
ASSOCIATIVITY_RIGHT_TO_LEFT
};
// < Resolver entity flags enum start
enum
{
RESOLVER_ENTITY_FLAG_IS_STACK = 0b00000001,
RESOLVER_ENTITY_FLAG_NO_MERGE_WITH_NEXT_ENTITY = 0b00000010,
RESOLVER_ENTITY_FLAG_NO_MERGE_WITH_LEFT_ENTITY = 0b00000100,
RESOLVER_ENTITY_FLAG_DO_INDIRECTION = 0b00001000,
RESOLVER_ENTITY_FLAG_JUST_USE_OFFSET = 0b00010000,
RESOLVER_ENTITY_FLAG_IS_POINTER_ARRAY_ENTITY = 0b00100000,
RESOLVER_ENTITY_FLAG_WAS_CASTED = 0b01000000,
RESOLVER_ENTITY_FLAG_USES_ARRAY_BRACKETS = 0b10000000
};
// > Resolver entity flags enum end
// < Resolver entity type enum start
enum
{
RESOLVER_ENTITY_TYPE_VARIABLE,
RESOLVER_ENTITY_TYPE_FUNCTION,
RESOLVER_ENTITY_TYPE_NATIVE_FUNCTION,
RESOLVER_ENTITY_TYPE_STRUCTURE,
RESOLVER_ENTITY_TYPE_FUNCTION_CALL,
RESOLVER_ENTITY_TYPE_ARRAY_BRACKET,
RESOLVER_ENTITY_TYPE_RULE,
RESOLVER_ENTITY_TYPE_GENERAL,
RESOLVER_ENTITY_TYPE_UNARY_GET_ADDRESS,
RESOLVER_ENTITY_TYPE_UNARY_INDIRECTION,
RESOLVER_ENTITY_TYPE_CAST,
RESOLVER_ENTITY_TYPE_UNSUPPORTED
};
// > Resolver entity type enum end
// < Resolver scope flags enum start
enum
{
RESOLVER_SCOPE_FLAG_IS_STACK = 0b00000001
};
// > Resolver scope flags enum end
// < Resolver result flags enum start
enum
{
RESOLVER_RESULT_FLAG_FAILED = 0b0000001,
RESOLVER_RESULT_FLAG_RUNTIME_NEEDED_TO_FINISH_PATH = 0b00000010,
RESOLVER_RESULT_FLAG_PROCESSING_ARRAY_ENTITIES = 0b00000100,
RESOLVER_RESULT_FLAG_HAS_POINTER_ARRAY_ACCESS = 0b00001000,
RESOLVER_RESULT_FLAG_FIRST_ENTITY_LOAD_TO_EBX = 0b00010000,
RESOLVER_RESULT_FLAG_FIRST_ENTITY_PUSH_VALUE = 0b00100000,
RESOLVER_RESULT_FLAG_FINAL_INDIRECTION_REQUIRED_FOR_VALUE = 0b01000000,
RESOLVER_RESULT_FLAG_DOES_GET_ADDRESS = 0b10000000
};
// > Resolver result flags enum end
// < Resolver default entity type enum start
enum
{
RESOLVER_DEFAULT_ENTITY_TYPE_STACK,
RESOLVER_DEFAULT_ENTITY_TYPE_SYMBOL
};
// > Resolver default entity type enum end
// < Resolver default datatype enum start
enum
{
RESOLVER_DEFAULT_ENTITY_DATA_TYPE_VARIABLE,
RESOLVER_DEFAULT_ENTITY_DATA_TYPE_FUNCTION,
RESOLVER_DEFAULT_ENTITY_DATA_TYPE_ARRAY_BRACKET
};
// > Resolver default datatype enum end
// < Resolver default flags enum start
enum
{
RESOLVER_DEFAULT_ENTITY_FLAG_IS_LOCAL_STACK = 0b00000001
};
// > Resolver default flags enum end
// < Codegen enums start
enum
{
EXPRESSION_FLAG_RIGHT_NODE = 0b0000000000000001,
EXPRESSION_IN_FUNCTION_CALL_ARGUMENTS = 0b0000000000000010,
EXPRESSION_IN_FUNCTION_CALL_LEFT_OPERAND = 0b0000000000000100,
EXPRESSION_IS_ADDITION = 0b0000000000001000,
EXPRESSION_IS_SUBSTRACTION = 0b0000000000010000,
EXPRESSION_IS_MULTIPLICATION = 0b0000000000100000,
EXPRESSION_IS_DIVISION = 0b0000000001000000,
EXPRESSION_IS_FUNCTION_CALL = 0b0000000010000000,
EXPRESSION_INDIRECTION = 0b0000000100000000,
EXPRESSION_GET_ADDRESS = 0b0000001000000000,
EXPRESSION_IS_ABOVE = 0b0000010000000000,
EXPRESSION_IS_ABOVE_OR_EQUAL = 0b0000100000000000,
EXPRESSION_IS_BELOW = 0b0001000000000000,
EXPRESSION_IS_BELOW_OR_EQUAL = 0b0010000000000000,
EXPRESSION_IS_EQUAL = 0b0100000000000000,
EXPRESSION_IS_NOT_EQUAL = 0b1000000000000000,
EXPRESSION_LOGICAL_AND = 0b10000000000000000,
EXPRESSION_LOGICAL_OR = 0b100000000000000000,
EXPRESSION_IN_LOGICAL_EXPRESSION = 0b1000000000000000000,
EXPRESSION_IS_BITSHIFT_LEFT = 0b10000000000000000000,
EXPRESSION_IS_BITSHIFT_RIGHT = 0b100000000000000000000,
EXPRESSION_IS_BITWISE_OR = 0b1000000000000000000000,
EXPRESSION_IS_BITWISE_AND = 0b10000000000000000000000,
EXPRESSION_IS_BITWISE_XOR = 0b100000000000000000000000,
EXPRESSION_IS_NOT_ROOT_NODE = 0b1000000000000000000000000,
EXPRESSION_IS_ASSIGNMENT = 0b10000000000000000000000000,
IS_ALONE_STATEMENT = 0b100000000000000000000000000,
EXPRESSION_IS_UNARY = 0b1000000000000000000000000000,
IS_STATMENT_RETURN = 0b10000000000000000000000000000,
IS_RIGHT_OPERAND_OF_ASSIGNMENT = 0b100000000000000000000000000000,
IS_LEFT_OPERAND_OF_ASSIGNMENT = 0b1000000000000000000000000000000,
EXPRESSION_IS_MODULAS = 0b10000000000000000000000000000000
};
#define EXPRESSION_GEN_MATHABLE ( \
EXPRESSION_IS_ADDITION | \
EXPRESSION_IS_SUBSTRACTION | \
EXPRESSION_IS_MULTIPLICATION | \
EXPRESSION_IS_DIVISION | \
EXPRESSION_IS_MODULAS | \
EXPRESSION_IS_FUNCTION_CALL | \
EXPRESSION_INDIRECTION | \
EXPRESSION_GET_ADDRESS | \
EXPRESSION_IS_ABOVE | \
EXPRESSION_IS_ABOVE_OR_EQUAL | \
EXPRESSION_IS_BELOW | \
EXPRESSION_IS_BELOW_OR_EQUAL | \
EXPRESSION_IS_EQUAL | \
EXPRESSION_IS_NOT_EQUAL | \
EXPRESSION_LOGICAL_AND | \
EXPRESSION_LOGICAL_OR | \
EXPRESSION_IN_LOGICAL_EXPRESSION | \
EXPRESSION_IS_BITSHIFT_LEFT | \
EXPRESSION_IS_BITSHIFT_RIGHT | \
EXPRESSION_IS_BITWISE_AND | \
EXPRESSION_IS_BITWISE_OR | \
EXPRESSION_IS_BITWISE_XOR \
)
#define EXPRESSION_UNINHERITABLE_FLAGS ( \
EXPRESSION_FLAG_RIGHT_NODE | EXPRESSION_IN_FUNCTION_CALL_ARGUMENTS | \
EXPRESSION_IS_ADDITION | EXPRESSION_IS_MODULAS | EXPRESSION_IS_SUBSTRACTION | EXPRESSION_IS_MULTIPLICATION | \
EXPRESSION_IS_DIVISION | EXPRESSION_IS_ABOVE | EXPRESSION_IS_ABOVE_OR_EQUAL | \
EXPRESSION_IS_BELOW | EXPRESSION_IS_BELOW_OR_EQUAL | EXPRESSION_IS_EQUAL | EXPRESSION_IS_NOT_EQUAL | \
EXPRESSION_LOGICAL_AND | EXPRESSION_LOGICAL_OR | \
EXPRESSION_IS_BITSHIFT_LEFT | EXPRESSION_IS_BITSHIFT_RIGHT | \
EXPRESSION_IS_BITWISE_AND | EXPRESSION_IS_BITWISE_OR | EXPRESSION_IS_BITWISE_XOR | \
EXPRESSION_IS_ASSIGNMENT | IS_ALONE_STATEMENT \
)
// > Codegen enums end
// < Expressionable definitions start
struct expressionable_op_precedence_group
{
char* operators[MAX_OPERATORS_IN_GROUP];
int associativity;
};
// > Expressionable definitions end
struct pos
{
int line;
int col;
const char* filename;
};
struct token
{
int type;
int flags;
struct pos pos;
union
{
char cval;
const char* sval;
unsigned int inum;
unsigned long lnum;
unsigned long long llnum;
void* any;
};
struct token_number
{
int type;
} num;
// True if there is whitespace between the token and the next token
// i.e "* a" for operator token "*" would mean whitespace would be set for token "a"
bool whitespace;
// (5+10+20)
const char* between_brackets;
// Anything between function call arguments. ABC(hello world)
const char* between_arguments;
};
struct lex_process;
typedef char (*LEX_PROCESS_NEXT_CHAR)(struct lex_process* lex_process);
typedef char (*LEX_PROCESS_PEEK_CHAR)(struct lex_process* lex_process);
typedef void (*LEX_PROCESS_PUSH_CHAR)(struct lex_process* lex_process, char c);
struct lex_process_functions
{
LEX_PROCESS_NEXT_CHAR next_char;
LEX_PROCESS_PEEK_CHAR peek_char;
LEX_PROCESS_PUSH_CHAR push_char;
};
struct lex_process
{
struct pos pos;
struct vector* token_vec;
struct compile_process* compiler;
// ((50))
int current_expression_count;
struct buffer* parentheses_buffer;
struct buffer* argument_string_buffer;
struct lex_process_functions* function;
// This will be private data that the lexer does not understand
// but the person using the lexer does understand
void* private;
};
// < Scope structure start
struct scope
{
int flags;
// type of void*
struct vector* entities;
// The total number of bytes this scope uses. Aligned to 16 bits
size_t size;
// NULL if no parent
struct scope* parent;
};
// > Scope structure end
// < Codegen structure start
struct code_generator
{
struct generator_switch_stmt
{
struct generator_switch_stmt_entity
{
int id;
} current;
// Vector of generator_switch_stmt_entity
struct vector* switches;
} _switch;
// Vector of struct string_table_element*
struct vector* string_table;
// Vector of struct codegen_entry_point*
struct vector* entry_points;
// Vector of struct codegen_exit_point*
struct vector* exit_points;
// Vector of const char* that will go in the data section
struct vector* custom_data_section;
// Vector of struct response*
struct vector* responses;
};
// > Codegen structure end
struct compile_process
{
// The flags in regards to how this file should be compiled
int flags;
struct pos pos;
struct compile_process_input_file
{
FILE* fp;
const char* abs_path;
} cfile;
// Untampered token vector, contains defintions and source code tokens, the preprocessor
// will go through this vector and populate the "token_vec" vector after it is done
struct vector* token_vec_original;
// A vector of tokens from lexical analysis
struct vector* token_vec;
struct vector* node_vec;
struct vector* node_tree_vec;
FILE* ofile;
struct
{
struct scope* root;
struct scope* current;
} scope;
struct
{
// Current active symbol table, struct symbol*
struct vector* table;
// Multiple symbol tables stored in here, struct vector*
struct vector* tables;
} symbols;
// Pointer to our codegenerator
struct code_generator* generator;
struct resolver_process* resolver;
// A vector of const char* the represents include directories
struct vector* include_dirs;
struct preprocessor* preprocessor;
};
// < Symbol structure start
struct symbol
{
const char* name;
int type;
void* data;
};
// > Symbol structure end
// < Array brackets structure start
struct array_brackets
{
// A vector of struct node*
struct vector* n_brackets;
};
// > Array brackets structure end
// < Dataype structure start
struct node;
struct datatype
{
int flags;
// i.e type of long, int, float etc...
int type;
// i.e long int, int being the secondary
struct datatype* secondary;
// long
const char* type_str;
// The size of the datatype
size_t size;
int pointer_depth;
union
{
struct node* struct_node;
struct node* union_node;
};
struct array
{
struct array_brackets* brackets;
// The total array size: equation = DATATYPE_SIZE * EACH_INDEX
size_t size;
} array;
};
// > Dataype structure end
// < Switch cases structure start
struct parsed_switch_case
{
// Index of the parsed case
int index;
};
// > Switch cases structure end
// < Stack frame structure start
struct stack_frame
{
// A vector of struct stack_frame_element
struct vector* elements;
};
// > Stack frame structure end
// < Unary structure start
struct node;
struct unary
{
int flags;
// "*" for pointer accss... *** even for multiple pointer access only the first
// operator is here
const char* op;
struct node* operand;
union
{
struct indirection
{
// The pointer depth
int depth;
} indirection;
};
};
// > Unary structure end
// < Node structure start
struct node
{
int type;
int flags;
struct pos pos;
struct node_binded
{
// Pointer to our body node
struct node* owner;
// Pointer to the function this node is in
struct node* function;
} binded;
union
{
struct exp
{
struct node* left;
struct node* right;
const char* op;
} exp;
struct parenthesis
{
// The expression inside the parenthesis node
struct node* exp;
} parenthesis;
struct var
{
struct datatype type;
int padding;
// Aligned offset
int aoffset;
const char* name;
struct node* val;
} var;
struct varlist
{
// A list of struct node* variables
struct vector* list;
} var_list;
struct node_tenary
{
struct node* true_node;
struct node* false_node;
} tenary;
struct bracket
{
// int x[50]; [50] would be out bracket node. The inner node would be NODE_TYPE_VARIABLE with a value of 50
struct node* inner;
} bracket;
struct _struct
{
const char* name;
struct node* body_n;
/**
* struct abc
* {
*
* } var_name;
*
* NULL if no variable attached to structure
*/
struct node* var;
} _struct;
struct _union
{
const char* name;
struct node* body_n;
struct node* var;
} _union;
struct body
{
// struct node* vector of statements
struct vector* statements;
// The size of combined variables inside this body
size_t size;
// True if the variable size has to be increased due to padding in the body
bool padded;
// A pointer to the largest variable node in the statements vector
struct node* largest_var_node;
} body;
struct function
{
// Special flags
int flags;
// Return type i.e void, int, long ....
struct datatype rtype;
// I.e function name "main"
const char* name;
struct function_arguments
{
// Vector of struct node* must be NODE_TYPE_VARIABLE
struct vector* vector;
// How much to add to the EBP to find the first argument
size_t stack_addition;
} args;
// Pointer to the function body node, NULL if this is a function prototype
struct node* body_n;
struct stack_frame frame;
// The stack size for all variables inside this function
size_t stack_size;
} func;
struct statement
{
struct return_stmt
{
// The expression of the return
struct node* exp;
} return_stmt;
struct if_stmt
{
// if (COND) { // body }
struct node* cond_node;
struct node* body_node;
// if (COND) {} else {}
struct node* next;
} if_stmt;
struct else_stmt
{
struct node* body_node;
} else_stmt;
struct for_stmt
{
struct node* init_node;
struct node* cond_node;
struct node* loop_node;
struct node* body_node;
} for_stmt;
struct while_stmt
{
struct node* exp_node;
struct node* body_node;
} while_stmt;
struct do_while_stmt
{
struct node* exp_node;
struct node* body_node;
} do_while_stmt;
struct switch_stmt
{
struct node* exp_node;
struct node* body_node;
struct vector* cases;
bool has_default_case;
} switch_stmt;
struct case_stmt
{
struct node* exp;
} case_stmt;
struct goto_stmt
{
struct node* label;
} goto_stmt;
} stmt;
struct node_label
{
struct node* name;
} label;
struct cast
{
struct datatype dtype;
struct node* operand;
} cast;
struct unary unary;
};
union
{
char cval;
const char* sval;
unsigned int inum;
unsigned long lnum;
unsigned long long llnum;
};
};
// > Node structure end
// < Resolver function pointers start
struct resolver_scope;
struct resolver_result;
struct resolver_entity;
struct resolver_process;
typedef void* (*RESOLVER_NEW_ARRAY_BRACKET_ENTITY)(struct resolver_result* result, struct node* array_entity_node);
typedef void (*RESOLVER_DELETE_SCOPE)(struct resolver_scope* scope);
typedef void (*RESOLVER_DELETE_ENTITY)(struct resolver_entity* entity);
typedef void* (*RESOLVER_MAKE_PRIVATE)(struct resolver_entity* entity, struct node* node, int offset, struct resolver_scope* scope);
typedef void (*RESOLVER_SET_RESULT_BASE)(struct resolver_result* result, struct resolver_entity* base_entity);
typedef struct resolver_entity* (*RESOLVER_MERGE_ENTITIES)(struct resolver_process* process, struct resolver_result* result, struct resolver_entity* left_entity, struct resolver_entity* right_entity);
// > Resolver function pointers end
// < Resolver structures start
struct resolver_array_data
{
// A vector that holds nodes of type resolver_entity
struct vector* array_entities;
};
struct resolver_scope
{
// Resolver scope flags
int flags;
struct vector* entities;
struct resolver_scope* next;
struct resolver_scope* prev;
// Private data for the resolver scope
void* private;
};
struct resolver_callbacks
{
RESOLVER_NEW_ARRAY_BRACKET_ENTITY new_array_entity;
RESOLVER_DELETE_SCOPE delete_scope;
RESOLVER_DELETE_ENTITY delete_entity;
RESOLVER_MAKE_PRIVATE make_private;