-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathast.h
400 lines (368 loc) · 11.3 KB
/
ast.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
/* ast.h */
#ifndef AST_H
#define AST_H
#include "lib/hm.h"
#include "lib/mem.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <setjmp.h>
#define MAX_VARS 100
/* Forward declarations */
typedef struct ASTNode ASTNode;
typedef struct StatementList StatementList;
typedef struct ArgumentList ArgumentList;
typedef struct CaseNode CaseNode;
/* Define TypeModifiers first */
typedef struct
{
bool is_volatile;
bool is_signed;
bool is_unsigned;
bool is_sizeof;
bool is_const;
} 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,
NONE,
} VarType;
/* Symbol table structure */
typedef struct
{
char *name;
union
{
int ivalue;
short svalue;
bool bvalue;
float fvalue;
double dvalue;
int *iarray;
short *sarray;
bool *barray;
float *farray;
double *darray;
char *carray;
} value;
TypeModifiers modifiers;
VarType var_type;
bool is_array;
int array_length;
} Variable;
typedef union
{
int ivalue;
short svalue;
bool bvalue;
float fvalue;
double dvalue;
} 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,
} OperatorType;
/* AST node types */
typedef enum
{
NODE_INT,
NODE_SHORT,
NODE_FLOAT,
NODE_DOUBLE,
NODE_CHAR,
NODE_BOOLEAN,
NODE_IDENTIFIER,
NODE_ASSIGNMENT,
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_FUNC_CALL,
NODE_SIZEOF,
NODE_ARRAY_ACCESS,
} NodeType;
/* 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 array_length;
union
{
short svalue;
bool bvalue;
int ivalue;
float fvalue;
double dvalue;
char *name;
struct
{
char *name;
ASTNode *index;
} array;
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
{
char *function_name;
ArgumentList *arguments;
} func_call;
StatementList *statements;
IfStatementNode if_stmt;
struct
{
ASTNode *expression;
CaseNode *cases;
} switch_stmt;
struct
{
ASTNode *expr;
} sizeof_stmt;
ASTNode *break_stmt;
} data;
};
/* Global variable declarations */
extern TypeModifiers current_modifiers;
extern HashMap *symbol_table;
extern int var_count;
/* Function prototypes */
bool set_int_variable(const char *name, int value, TypeModifiers mods);
bool set_array_variable(char *name, int length, TypeModifiers mods, VarType type);
bool set_short_variable(const char *name, short value, TypeModifiers mods);
bool set_float_variable(const char *name, float value, TypeModifiers mods);
bool set_double_variable(const char *name, double value, TypeModifiers mods);
TypeModifiers get_variable_modifiers(const char *name);
void reset_modifiers(void);
TypeModifiers get_current_modifiers(void);
/* Node creation functions */
ASTNode *create_int_node(int value);
ASTNode *create_array_declaration_node(char *name, int length, VarType type);
ASTNode *create_array_access_node(char *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(char *name);
ASTNode *create_assignment_node(char *name, ASTNode *expr);
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(char *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(char *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);
ExpressionList *create_expression_list(ASTNode *expr);
ExpressionList *append_expression_list(ExpressionList *list, ASTNode *expr);
void free_expression_list(ExpressionList *list);
void populate_array_variable(char *name, ExpressionList *list);
void free_ast(ASTNode *node);
/* 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_double_expression(ASTNode *node);
bool is_float_expression(ASTNode *node);
bool is_const_variable(const char *name);
void check_const_assignment(const char *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 execute_yapping_call(ArgumentList *args);
void execute_yappin_call(ArgumentList *args);
void execute_baka_call(ArgumentList *args);
void execute_ragequit_call(ArgumentList *args);
void execute_chill_call(ArgumentList *args);
void free_ast(ASTNode *node);
void reset_modifiers(void);
bool check_and_mark_identifier(ASTNode *node, const char *contextErrorMessage);
void bruh();
size_t count_expression_list(ExpressionList *list);
size_t handle_sizeof(ASTNode *node);
size_t get_type_size(char *name);
extern TypeModifiers current_modifiers;
/* 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 = safe_strdup(n), SAFE_FREE(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 = safe_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)
#endif /* AST_H */