-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathast.h
More file actions
592 lines (545 loc) · 17.6 KB
/
ast.h
File metadata and controls
592 lines (545 loc) · 17.6 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
/* ast.h */
#ifndef AST_H
#define AST_H
#include "lib/hm.h"
#include "lib/arena.h"
#include "lib/mem.h"
#include "lib/string_value.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <setjmp.h>
#define MAX_VARS 100
#define MAX_ARGUMENTS 100
#define MAX_DIMENSIONS 10
/* Forward declarations */
typedef struct ASTNode ASTNode;
typedef struct StatementList StatementList;
typedef struct ArgumentList ArgumentList;
typedef struct CaseNode CaseNode;
typedef struct
{
String name;
int pointer_level;
} Declarator;
/* Define Array Dimensions */
typedef struct
{
int dimensions[MAX_DIMENSIONS];
int num_dimensions;
size_t total_size;
} ArrayDimensions;
/* Define TypeModifiers first */
typedef struct
{
bool is_volatile;
bool is_signed;
bool is_unsigned;
bool is_sizeof;
bool is_const;
bool is_long;
bool is_long_long;
bool is_static;
} TypeModifiers;
typedef struct JumpBuffer
{
jmp_buf data;
struct JumpBuffer *next;
} JumpBuffer;
typedef struct ExpressionList
{
ASTNode *expr;
struct ExpressionList *next;
struct ExpressionList *prev;
} ExpressionList;
typedef enum
{
VAR_INT,
VAR_SHORT,
VAR_FLOAT,
VAR_DOUBLE,
VAR_BOOL,
VAR_CHAR,
VAR_STRING,
VAR_STRUCT,
NONE,
} VarType;
/* A single field inside a struct definition */
typedef struct StructField {
String name;
VarType type;
int pointer_level;
size_t offset; /* byte offset within the struct blob */
struct StructField *next;
} StructField;
/* A struct definition (the "template") */
typedef struct StructDef {
String name;
StructField *fields;
size_t total_size; /* total byte size of one instance */
struct StructDef *next_def;
} StructDef;
/* AST helper functions */
ASTNode* arena_alloc_astnode(void);
typedef struct Parameter
{
String name;
VarType type;
int pointer_level;
TypeModifiers modifiers;
struct Parameter *next;
} Parameter;
typedef struct Function
{
String name;
VarType return_type;
int return_pointer_level;
Parameter *parameters;
ASTNode *body;
} Function;
typedef struct
{
bool has_value;
union
{
int ivalue;
float fvalue;
double dvalue;
bool bvalue;
short svalue;
String strvalue;
uintptr_t pvalue;
} value;
VarType type;
int pointer_level;
} ReturnValue;
/* Symbol table structure */
typedef struct
{
String name;
union
{
int ivalue;
short svalue;
bool bvalue;
float fvalue;
double dvalue;
void *array_data;
String strvalue;
uintptr_t pvalue;
} value;
TypeModifiers modifiers;
VarType var_type;
int pointer_level;
bool is_array;
int array_length; // lets keep it for now for backword compatibility
ArrayDimensions array_dimensions;
String struct_name; /* non-NULL when var_type == VAR_STRUCT */
} Variable;
typedef union
{
VarType type;
union
{
int ivalue;
short svalue;
bool bvalue;
float fvalue;
double dvalue;
String strvalue;
uintptr_t pvalue;
};
int pointer_level;
} Value;
/* Operator types */
typedef enum
{
OP_PLUS,
OP_MINUS,
OP_TIMES,
OP_DIVIDE,
OP_MOD,
OP_LT,
OP_GT,
OP_LE,
OP_GE,
OP_EQ,
OP_NE,
OP_AND,
OP_OR,
OP_NEG,
OP_POST_INC,
OP_POST_DEC,
OP_PRE_INC,
OP_PRE_DEC,
OP_ASSIGN,
OP_ADDRESS_OF,
OP_DEREFERENCE,
} OperatorType;
/* AST node types */
typedef enum
{
NODE_INT,
NODE_SHORT,
NODE_FLOAT,
NODE_DOUBLE,
NODE_CHAR,
NODE_BOOLEAN,
NODE_STRING,
NODE_IDENTIFIER,
NODE_ASSIGNMENT,
NODE_DECLARATION,
NODE_OPERATION,
NODE_UNARY_OPERATION,
NODE_FOR_STATEMENT,
NODE_WHILE_STATEMENT,
NODE_DO_WHILE_STATEMENT,
NODE_PRINT_STATEMENT,
NODE_ERROR_STATEMENT,
NODE_STATEMENT_LIST,
NODE_IF_STATEMENT,
NODE_STRING_LITERAL,
NODE_SWITCH_STATEMENT,
NODE_CASE,
NODE_DEFAULT_CASE,
NODE_BREAK_STATEMENT,
NODE_SIZEOF,
NODE_ARRAY_ACCESS,
NODE_FUNC_CALL,
NODE_FUNCTION_DEF,
NODE_RETURN,
NODE_STRUCT_DEF,
NODE_STRUCT_ACCESS,
} NodeType;
typedef struct
{
String name;
ASTNode *index;
ASTNode *indices[MAX_DIMENSIONS];
int num_dimensions;
} Array;
/* Rest of the structure definitions */
struct StatementList
{
ASTNode *statement;
struct StatementList *next;
};
typedef struct
{
ASTNode *condition;
ASTNode *then_branch;
ASTNode *else_branch;
} IfStatementNode;
struct CaseNode
{
ASTNode *value;
ASTNode *statements;
struct CaseNode *next;
};
struct ArgumentList
{
struct ASTNode *expr;
struct ArgumentList *next;
};
/* AST node structure */
struct ASTNode
{
NodeType type;
TypeModifiers modifiers;
VarType var_type;
bool already_checked;
bool is_valid_symbol;
bool is_array;
int pointer_level;
int array_length;
ArrayDimensions array_dimensions;
int line_number; /* Line number for error reporting */
union
{
short svalue;
bool bvalue;
int ivalue;
float fvalue;
double dvalue;
String strvalue;
String name;
Array array;
struct {
String struct_name; /* name of the struct type */
String member_name; /* field being accessed */
ASTNode *object; /* the struct-valued expr */
} struct_access;
struct {
String name; /* struct tag */
StructField *fields; /* linked list of fields */
} struct_def;
struct
{
ASTNode *left;
ASTNode *right;
OperatorType op;
} op;
struct
{
ASTNode *operand;
OperatorType op;
} unary;
struct
{
ASTNode *init;
ASTNode *cond;
ASTNode *incr;
ASTNode *body;
} for_stmt;
struct
{
ASTNode *cond;
ASTNode *body;
} while_stmt;
struct
{
String function_name;
ArgumentList *arguments;
} func_call;
StatementList *statements;
IfStatementNode if_stmt;
struct
{
ASTNode *expression;
CaseNode *cases;
} switch_stmt;
struct
{
ASTNode *expr;
} sizeof_stmt;
struct
{
String name;
VarType return_type;
Parameter *parameters;
ASTNode *body;
} function_def;
ASTNode *break_stmt;
} data;
};
typedef struct Scope
{
HashMap *variables;
struct Scope *parent;
bool is_function_scope;
String function_name;
} Scope;
/* Global variable declarations */
extern TypeModifiers current_modifiers;
extern Scope *current_scope;
extern HashMap *function_map;
extern ReturnValue current_return_value;
extern JumpBuffer *jump_buffer;
/* Function prototypes */
bool set_int_variable(const String name, int value, TypeModifiers mods);
bool set_array_variable(String name, int length, TypeModifiers mods, VarType type);
bool set_short_variable(const String name, short value, TypeModifiers mods);
bool set_float_variable(const String name, float value, TypeModifiers mods);
bool set_double_variable(const String name, double value, TypeModifiers mods);
TypeModifiers get_variable_modifiers(const String name);
void reset_modifiers(void);
TypeModifiers get_current_modifiers(void);
Variable *get_variable(const String name);
Scope *create_scope(Scope *parent);
void enter_function_scope(Function *func, ArgumentList *args);
void exit_scope();
void enter_scope();
void free_scope(Scope *scope);
void add_variable_to_scope(const String name, Variable *var);
Variable *variable_new(String name);
Function *get_function(const String name);
VarType get_function_return_type(const String name);
/* Node creation functions */
ASTNode *create_int_node(int value);
ASTNode *create_array_declaration_node(String name, int length, VarType type);
ASTNode *create_array_access_node(String name, ASTNode *index);
ASTNode *create_short_node(short value);
ASTNode *create_float_node(float value);
ASTNode *create_double_node(double value);
ASTNode *create_char_node(char value);
ASTNode *create_boolean_node(bool value);
ASTNode *create_identifier_node(String name);
ASTNode *create_identifier_node_ex(String name, int pointer_level);
ASTNode *create_assignment_node(String name, ASTNode *expr);
ASTNode *create_assignment_target_node(ASTNode *target, ASTNode *expr);
ASTNode *create_declaration_node(String name, ASTNode *expr);
ASTNode *create_declaration_node_ex(String name, ASTNode *expr, int pointer_level);
ASTNode *create_operation_node(OperatorType op, ASTNode *left, ASTNode *right);
ASTNode *create_unary_operation_node(OperatorType op, ASTNode *operand);
ASTNode *create_for_statement_node(ASTNode *init, ASTNode *cond, ASTNode *incr, ASTNode *body);
ASTNode *create_while_statement_node(ASTNode *cond, ASTNode *body);
ASTNode *create_do_while_statement_node(ASTNode *cond, ASTNode *body);
ASTNode *create_function_call_node(String func_name, ArgumentList *args);
ArgumentList *create_argument_list(ASTNode *expr, ArgumentList *existing_list);
ASTNode *create_print_statement_node(ASTNode *expr);
ASTNode *create_sizeof_node(ASTNode *node);
ASTNode *create_error_statement_node(ASTNode *expr);
ASTNode *create_statement_list(ASTNode *statement, ASTNode *next_statement);
ASTNode *create_if_statement_node(ASTNode *condition, ASTNode *then_branch, ASTNode *else_branch);
ASTNode *create_string_literal_node(String string);
ASTNode *create_switch_statement_node(ASTNode *expression, CaseNode *cases);
CaseNode *create_case_node(ASTNode *value, ASTNode *statements);
CaseNode *create_default_case_node(ASTNode *statements);
CaseNode *append_case_list(CaseNode *list, CaseNode *case_node);
ASTNode *create_break_node(void);
ASTNode *create_default_node(VarType var_type);
ASTNode *create_return_node(ASTNode *expr);
ExpressionList *create_expression_list(ASTNode *expr);
ExpressionList *append_expression_list(ExpressionList *list, ASTNode *expr);
void free_expression_list(ExpressionList *list);
void populate_multi_array_variable(String name, ExpressionList *list, int dimensions[], int num_dimensions);
void free_ast(void);
/* Evaluation and execution functions */
void *evaluate_array_access(ASTNode *node);
double evaluate_expression_double(ASTNode *node);
float evaluate_expression_float(ASTNode *node);
int evaluate_expression_int(ASTNode *node);
short evaluate_expression_short(ASTNode *node);
bool evaluate_expression_bool(ASTNode *node);
int evaluate_expression(ASTNode *node);
bool is_const_variable(const String name);
void check_const_assignment(const String name);
void execute_statement(ASTNode *node);
void execute_statements(ASTNode *node);
void execute_assignment(ASTNode *node);
void execute_for_statement(ASTNode *node);
void execute_while_statement(ASTNode *node);
void execute_do_while_statement(ASTNode *node);
void execute_if_statement(ASTNode *node);
void reset_modifiers(void);
bool check_and_mark_identifier(ASTNode *node, const String contextErrorMessage);
bool is_expression(ASTNode *node, VarType type);
int get_expression_pointer_level(ASTNode *node);
uintptr_t evaluate_expression_pointer(ASTNode *node);
void *evaluate_lvalue_address(ASTNode *node);
void bruh();
size_t count_expression_list(ExpressionList *list);
size_t handle_sizeof(ASTNode *node);
size_t get_type_size(String name);
size_t get_type_size_for_descriptor(VarType type, int pointer_level, TypeModifiers mods);
void *handle_function_call(ASTNode *node);
ASTNode *create_multi_array_declaration_node(String name, int dimensions[], int num_dimensions, VarType type);
bool set_multi_array_variable(const String name, int dimensions[], int num_dimensions, TypeModifiers mods, VarType type);
ASTNode *create_array_access_node_single(String name, ASTNode *index);
ASTNode *create_multi_array_access_node(String name, ASTNode *indices[], int num_indices);
/* User-defined functions */
Function *create_function(String name, VarType return_type, Parameter *params, ASTNode *body);
Function *create_function_ex(String name, VarType return_type, int return_pointer_level, Parameter *params, ASTNode *body);
Parameter *create_parameter(String name, VarType type, Parameter *next, TypeModifiers mods);
Parameter *create_parameter_ex(String name, VarType type, int pointer_level, Parameter *next, TypeModifiers mods);
void execute_function_call(const String name, ArgumentList *args);
ASTNode *create_function_def_node(String name, VarType return_type, Parameter *params, ASTNode *body);
ASTNode *create_function_def_node_ex(String name, VarType return_type, int return_pointer_level, Parameter *params, ASTNode *body);
void handle_return_statement(ASTNode *expr);
void *handle_binary_operation(ASTNode *node);
void free_function_table(void);
void free_static_variable_map(void);
/* Struct types */
void register_struct_def(StructDef *def);
StructDef *get_struct_def(const String name);
void free_struct_registry(void);
StructField *find_struct_field(StructDef *def, const String name);
size_t compute_struct_layout(StructField *fields); /* fills offsets, returns total */
ASTNode *create_struct_def_node(String name, StructField *fields);
ASTNode *create_struct_access_node(ASTNode *object, String member);
void *evaluate_struct_member_address(ASTNode *node);
void populate_struct_variable(const String name, ExpressionList *list);
extern TypeModifiers current_modifiers;
extern Arena arena;
#define ARENA_ALLOC(type) arena_alloc(&arena, sizeof(type))
#define ARENA_ALLOC_ASTNODE() arena_alloc_astnode()
#define ARENA_STRDUP(str) arena_strdup(&arena, str)
/* Macros for assigning specific fields to a node */
#define SET_DATA_INT(node, value) ((node)->data.ivalue = (value))
#define SET_DATA_SHORT(node, value) ((node)->data.svalue = (value))
#define SET_DATA_FLOAT(node, value) ((node)->data.fvalue = (value))
#define SET_DATA_DOUBLE(node, value) ((node)->data.dvalue = (value))
#define SET_DATA_BOOL(node, value) ((node)->data.bvalue = (value) ? 1 : 0)
#define SET_DATA_NAME(node, n) ((node)->data.name = ARENA_STRDUP(n))
#define SET_SIZEOF(node, n) ((node)->data.sizeof_stmt.expr = (n))
#define SET_DATA_OP(node, l, r, opr) \
do \
{ \
(node)->data.op.left = (l); \
(node)->data.op.right = (r); \
(node)->data.op.op = (opr); \
} while (0)
#define SET_DATA_UNARY_OP(node, o, opr) \
do \
{ \
(node)->data.unary.operand = (o); \
(node)->data.unary.op = (opr); \
} while (0)
#define SET_DATA_FOR(node, i, c, inc, b) \
do \
{ \
(node)->data.for_stmt.init = (i); \
(node)->data.for_stmt.cond = (c); \
(node)->data.for_stmt.incr = (inc); \
(node)->data.for_stmt.body = (b); \
} while (0)
#define SET_DATA_WHILE(node, c, b) \
do \
{ \
(node)->data.while_stmt.cond = (c); \
(node)->data.while_stmt.body = (b); \
} while (0)
#define SET_DATA_FUNC_CALL(node, func_name, args) \
do \
{ \
(node)->data.func_call.function_name = ARENA_STRDUP(func_name); \
(node)->data.func_call.arguments = (args); \
} while (0)
/* Macros for handling jump buffer */
#define PUSH_JUMP_BUFFER() \
do \
{ \
JumpBuffer *jb = SAFE_MALLOC(JumpBuffer); \
jb->next = jump_buffer; \
jump_buffer = jb; \
} while (0)
#define POP_JUMP_BUFFER() \
do \
{ \
JumpBuffer *jb = jump_buffer; \
jump_buffer = jump_buffer->next; \
SAFE_FREE(jb); \
} while (0)
#define LONGJMP() \
do \
{ \
if (jump_buffer != NULL) \
{ \
longjmp(jump_buffer->data, 1); \
} \
else \
{ \
yyerror("No jump buffer available"); \
exit(1); \
} \
} while (0)
#define CURRENT_JUMP_BUFFER() (jump_buffer->data)
#define CLEAN_JUMP_BUFFER() \
do \
{ \
while (jump_buffer) \
{ \
POP_JUMP_BUFFER(); \
} \
} while (0)
#define VART_TO_NODET(var_type) \
((var_type) == VAR_INT ? NODE_INT \
: (var_type) == VAR_SHORT ? NODE_SHORT \
: (var_type) == VAR_FLOAT ? NODE_FLOAT \
: (var_type) == VAR_DOUBLE ? NODE_DOUBLE \
: (var_type) == VAR_BOOL ? NODE_BOOLEAN \
: (var_type) == VAR_CHAR ? NODE_CHAR \
: (var_type) == VAR_STRING ? NODE_STRING : (NodeType)-1)
#endif /* AST_H */