-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
374 lines (314 loc) · 8.24 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
// Reference:
// 1. https://lambda.uta.edu/cse5317/notes/node26.html
// 2. https://www.reddit.com/r/C_Programming/comments/lzq2t2/how_to_make_an_ast_in_c/
#ifndef AST_H
#define AST_H
typedef struct translation_unit translation_unit;
typedef struct external_declaration external_declaration;
typedef struct statement statement;
typedef struct statement_list statement_list;
typedef struct jump_statement jump_statement;
typedef struct compound_statement compound_statement;
typedef struct expression_statement expression_statement;
typedef struct expression expression;
typedef struct argument_expression_list argument_expression_list;
typedef struct function_definition function_definition;
typedef struct declaration declaration;
typedef struct declaration_list declaration_list;
typedef struct declarator declarator;
typedef struct direct_declarator direct_declarator;
typedef struct declaration_specifiers declaration_specifiers;
typedef struct pointer pointer;
typedef struct init_declarator_list init_declarator_list;
typedef struct init_declarator init_declarator;
typedef struct initializer initializer;
typedef struct parameter_type_list parameter_type_list;
typedef struct parameter_list parameter_list;
typedef struct parameter_declaration parameter_declaration;
typedef struct type_qualifier_list type_qualifier_list;
typedef enum type_qualifier type_qualifier;
typedef struct type_specifier type_specifier;
typedef enum storage_class_specifier storage_class_specifier;
typedef struct type type;
typedef struct type_list type_list;
typedef struct identifier {
char *name;
} identifier;
// Declarations
struct declaration {
declaration_specifiers *specifiers;
init_declarator_list *init_decls;
};
struct declaration_list {
declaration decl;
declaration_list *next;
};
struct parameter_declaration {
declaration_specifiers *specifiers;
declarator *decl;
};
struct parameter_type_list {
parameter_list *params;
};
struct parameter_list {
parameter_declaration decl;
parameter_list *next;
};
struct declarator {
pointer *pointer;
direct_declarator *direct_decl;
};
struct pointer {
// type_qualifier_list *qualifiers;
pointer *next;
};
enum type_qualifier {
TQ_CONST,
TQ_VOLATILE
};
struct type_qualifier_list {
type_qualifier tq;
type_qualifier_list *next;
};
struct type_specifier {
enum {
TS_VOID,
TS_CHAR,
TS_SHORT,
TS_INT,
TS_LONG,
TS_FLOAT,
TS_DOUBLE,
TS_SIGNED,
TS_UNSIGNED,
TS_STRUCT_OR_UNION,
TS_ENUM,
TS_TYPEDEF_NAME
} tag;
};
enum storage_class_specifier {
SCS_TYPEDEF,
SCS_EXTERN,
SCS_STATIC,
SCS_AUTO,
SCS_REGISTER
};
typedef struct declaration_specifier {
enum {
DS_TYPE_SPECIFIER,
DS_TYPE_QUALIFIER,
DS_STORAGE_CLASS_SPECIFIER
} tag;
union {
type_specifier s;
type_qualifier q;
storage_class_specifier sc;
} specifier;
} declaration_specifier;
struct declaration_specifiers {
declaration_specifier specifier;
declaration_specifiers *next;
};
struct init_declarator {
declarator *decl;
initializer *init;
};
struct init_declarator_list {
init_declarator decl;
init_declarator_list *next;
};
struct initializer {
expression *expr;
};
struct direct_declarator {
enum {
DDECL_IDENTIFIER,
DDECL_DECLARATOR,
DDECL_FUNCTION
} tag;
union {
identifier identifier_decl;
declarator *decl;
struct {
direct_declarator *function;
parameter_type_list param_types;
} function_decl;
} op;
};
struct function_definition {
declaration_specifiers *specifiers;
declarator *declarator;
compound_statement *statement;
};
// Expressions
#define unary_expr(name) expression *name
#define binary_expr(name) struct { \
expression *left; \
expression *right; \
} name
#define projection_expr(name) struct { \
expression *base; \
identifier member; \
} name
struct argument_expression_list {
expression *expr;
argument_expression_list *next;
};
struct expression {
enum {
EXPR_INTEGER,
EXPR_STRING,
EXPR_IDENTIFIER,
EXPR_ADD,
EXPR_SUBTRACT,
EXPR_MULTIPLY,
EXPR_DIVIDE,
EXPR_MODULO,
EXPR_DEREFERENCE,
EXPR_REFERENCE,
EXPR_MULTI,
EXPR_PRE_INC,
EXPR_PRE_DEC,
EXPR_POST_INC,
EXPR_POST_DEC,
EXPR_DIRECT_PROJ,
EXPR_INDIRECT_PROJ,
EXPR_CALL,
EXPR_ARRAY_INDEX
} tag;
union {
int integer_expr;
char *string_expr;
identifier identifier_expr;
binary_expr(add_expr);
binary_expr(subtract_expr);
binary_expr(multiply_expr);
binary_expr(divide_expr);
binary_expr(modulo_expr);
unary_expr(dereference_expr);
unary_expr(reference_expr);
unary_expr(preincrement_expr);
unary_expr(predecrement_expr);
unary_expr(postincrement_expr);
unary_expr(postdecrement_expr);
unary_expr(sizeof_expr);
struct {
expression *func;
argument_expression_list *args;
} call_expr;
binary_expr(array_index_expr);
projection_expr(direct_projection_expr);
projection_expr(indirect_projection_expr);
binary_expr(multi_expr);
} op;
type *t;
};
// Statements
struct compound_statement {
declaration_list *declarations;
statement_list *statements;
};
struct expression_statement {
expression *expr;
};
struct jump_statement {
enum {
JUMP_GOTO,
JUMP_CONTINUE,
JUMP_BREAK,
JUMP_RETURN
} tag;
union {
identifier goto_ident;
expression* return_expr;
} jump;
};
struct statement {
enum {
STMT_COMPOUND,
STMT_JUMP,
STMT_EXPRESSION,
} tag;
union {
jump_statement *jump;
compound_statement *compound;
expression_statement expression;
} stmt;
};
struct statement_list {
statement *stmt;
statement_list *next;
};
// Module
struct external_declaration {
enum {
ED_DECLARATION,
ED_FUNCTION_DEFINITION
} op;
struct {
declaration decl;
function_definition func;
} decl;
};
struct translation_unit {
external_declaration decl;
translation_unit *next;
};
// types
struct type {
enum {
TYPE_VOID,
TYPE_CHAR,
TYPE_SIGNED_CHAR,
TYPE_UNSIGNED_CHAR,
TYPE_SHORT,
TYPE_UNSIGNED_SHORT,
TYPE_INT,
TYPE_UNSIGNED_INT,
TYPE_LONG,
TYPE_UNSIGNED_LONG,
TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_LONG_DOUBLE,
TYPE_STRUCT_OR_UNION,
TYPE_ENUM,
TYPE_TYPEDEF,
TYPE_POINTER,
TYPE_QUALIFIED,
TYPE_FUNCTION
} tag;
union {
identifier tag_name;
identifier typedef_name;
type *pointer_base_type;
struct {
type *base_type;
type_qualifier_list *qualifiers;
} qualified;
struct {
type *return_type;
type_list *argument_types;
} function;
} data;
};
struct type_list {
type *type;
type_list *next;
};
// builders
identifier new_identifier(char *name);
expression *new_identifier_expression(identifier ident);
expression *new_string_literal_expression(char* name);
expression *new_multi_expression(expression *left, expression *right);
pointer *new_pointer(pointer *next);
declaration_specifiers *new_declaration_specifiers(declaration_specifier spec, declaration_specifiers *next);
init_declarator_list *new_init_declarator_list(init_declarator decl, init_declarator_list *next);
parameter_list *new_parameter_list(parameter_declaration decl, parameter_list *next);
declaration_list *new_declaration_list(declaration decl, declaration_list *next);
statement_list *new_statement_list(statement *statement, statement_list *next);
translation_unit *new_translation_unit(external_declaration decl, translation_unit *next);
type_list *new_type_list(type *type, type_list *type_list);
type *new_type();
type *new_function_type(type *return_type, type_list *argument_types);
type *new_pointer_type(type *base_type);
#endif