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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef AST_H
#define AST_H
enum ast_binops {
AST_ADD,
AST_SUB,
AST_MUL,
AST_DIV,
AST_REM,
AST_XOR,
AST_POW,
AST_AND,
AST_LAND,
AST_OR,
AST_LOR,
AST_LSHIFT,
AST_RSHIFT,
AST_ASSIGN_ADD,
AST_ASSIGN_SUB,
AST_ASSIGN_MUL,
AST_ASSIGN_DIV,
AST_ASSIGN_REM,
AST_ASSIGN_AND,
AST_ASSIGN_OR,
AST_ASSIGN_XOR,
AST_ASSIGN_LSHIFT,
AST_ASSIGN_RSHIFT,
AST_LT,
AST_GT,
AST_LE,
AST_GE,
AST_NE,
AST_EQ,
};
enum ast_unops {
AST_NEG,
AST_LNOT,
AST_REF,
AST_DEREF,
AST_NOT
};
/* should this also have file name? Probably? */
struct src_loc {
int first_line;
int last_line;
int first_col;
int last_col;
};
enum ast_node_type {
AST_BINOP = 1,
AST_UNOP,
AST_FETCH,
AST_INIT,
AST_ASSIGN,
AST_CALL,
AST_SIZEOF,
AST_CAST,
AST_DEFER,
AST_MACRO,
AST_LAST,
AST_PROC,
AST_GOTO,
AST_LABEL,
AST_VAR,
AST_LAMBDA,
AST_FOR,
AST_EMBED,
AST_DOT,
AST_WHILE,
AST_CTRL,
AST_RETURN,
AST_ALIAS,
AST_TEMPLATE,
AST_STRUCT,
AST_IF,
AST_TYPE,
AST_BLOCK,
AST_IMPORT,
AST_ENUM,
AST_UNION,
AST_VAL,
AST_SWITCH,
AST_CASE,
AST_CONST,
AST_ID,
AST_AS,
AST_EMPTY,
};
enum ast_ctrl_kind {
AST_CTRL_BREAK,
AST_CTRL_CONTINUE
};
enum ast_const_kind {
AST_CONST_INTEGER,
AST_CONST_STRING,
AST_CONST_FLOAT,
};
enum ast_type_kind {
AST_TYPE_ID,
AST_TYPE_ARR,
AST_TYPE_TYPEOF,
AST_TYPE_TEMPLATE,
AST_TYPE_ALIAS,
AST_TYPE_MEMBER,
AST_TYPE_POINTER,
AST_TYPE_UNION,
AST_TYPE_LAMBDA,
AST_TYPE_PROC,
AST_TYPE_STRUCT,
AST_TYPE_GENERIC,
AST_TYPE_ENUM,
AST_TYPE_SIGN,
};
enum ast_typedef_kind {
AST_TYPEDEF_ALIAS,
AST_TYPEDEF_TEMPLATE,
};
enum ast_flag {
AST_FLAG_MUTABLE = (1 << 0),
AST_FLAG_CONST = (1 << 1),
AST_FLAG_EXTERN = (1 << 2),
AST_FLAG_VARIADIC = (1 << 3),
AST_FLAG_DELAYED = (1 << 4),
AST_FLAG_PUBLIC = (1 << 5),
/* Is untyped necessary? It's fairly easy to just look at the type of an
* expression... */
AST_FLAG_UNTYPED = (1 << 6),
AST_FLAG_FALLTHROUGH = (1 << 7),
AST_FLAG_UNHYGIENIC = (1 << 8),
AST_FLAG_ACTUAL = (1 << 9),
AST_FLAG_INIT = (1 << 10),
AST_FLAG_MEMBER = (1 << 11),
AST_FLAG_SHARED = (1 << 12),
AST_FLAG_GENERIC = (1 << 13),
};
struct ast_node;
struct ast_if {
struct ast_node *cond;
struct ast_node *body;
struct ast_node *els;
};
struct ast_fetch {
struct ast_node *id;
struct ast_node *type;
};
struct ast_label {
struct ast_node *id;
struct ast_node *defers;
};
struct ast_goto {
struct ast_node *label;
struct ast_node *defers;
};
struct ast_alias {
struct ast_node *id;
struct ast_node *type;
};
struct template_implemented {
struct ast_node *type;
struct template_implemented *next;
};
/* should templates take decls or should it just be for structures? */
struct ast_template {
struct ast_node *id;
struct ast_node *body;
struct template_implemented *impl_by;
};
struct ast_cast {
struct ast_node *expr;
struct ast_node *type;
};
struct ast_binop {
enum ast_binops op;
struct ast_node *left;
struct ast_node *right;
};
struct ast_unop {
enum ast_unops op;
struct ast_node *expr;
};
struct ast_call {
struct ast_node *id;
struct ast_node *args;
};
struct ast_defer {
struct ast_node *expr;
};
struct ast_macro {
struct ast_node *id;
struct ast_node *params;
struct ast_node *body;
};
struct ast_proc {
struct ast_node *id;
struct ast_node *sign;
struct ast_node *body;
};
struct ast_dot {
struct ast_node *expr;
struct ast_node *id;
};
struct ast_as {
struct ast_node *type;
};
struct ast_sizeof {
struct ast_node *expr;
};
struct ast_var {
struct ast_node *id;
struct ast_node *type;
struct ast_node *init;
};
struct ast_lambda {
struct ast_node *captures;
struct ast_node *sign;
struct ast_node *body;
};
struct ast_for {
struct ast_node *pre;
struct ast_node *cond;
struct ast_node *post;
struct ast_node *body;
};
struct ast_while {
struct ast_node *cond;
struct ast_node *body;
};
struct ast_ctrl {
enum ast_ctrl_kind kind;
struct ast_node *defers;
};
struct ast_return {
struct ast_node *expr;
struct ast_node *defers;
};
struct ast_type {
enum ast_type_kind kind;
struct ast_node *next;
union {
struct ast_node *id;
struct {
struct ast_node *size;
} arr;
struct {
struct ast_node *expr;
struct ast_node *actual;
} typeo;
struct {
struct ast_node *id;
struct ast_node *params;
struct ast_node *ret;
} proc;
struct {
struct ast_node *alias;
struct ast_node *actual;
} alias;
struct {
struct ast_node *id;
struct ast_node *expr;
} member;
struct {
struct ast_node *template;
struct ast_node *actual;
} template;
struct {
struct ast_node *params;
struct ast_node *ret;
} lambda;
struct {
struct ast_node *id;
struct ast_node *args;
} generic;
struct {
struct ast_node *id;
struct ast_node *impls;
} struc;
struct {
struct ast_node *id;
struct ast_node *type;
} enu;
struct {
struct ast_node *id;
struct ast_node *impls;
} unio;
/* a signature can be either a procedure or a lambda, to be
* determined later. */
struct {
struct ast_node *params;
struct ast_node *ret;
} sign;
struct {
struct ast_node *id;
struct ast_node *types;
} impl;
};
};
struct ast_block {
struct ast_node *body;
struct ast_node *defers;
};
struct ast_import {
const char *file;
};
struct ast_embed {
const char *file;
};
struct ast_paste {
struct ast_node *id1;
struct ast_node *id2;
};
struct ast_enum {
struct ast_node *id;
struct ast_node *type;
struct ast_node *body;
};
struct ast_struct {
struct ast_node *id;
struct ast_node *generics;
struct ast_node *body;
};
struct ast_union {
struct ast_node *id;
struct ast_node *generics;
struct ast_node *body;
};
struct ast_val {
struct ast_node *id;
struct ast_node *val;
};
struct ast_switch {
struct ast_node *cond;
struct ast_node *cases;
};
struct ast_case {
struct ast_node *cond;
struct ast_node *body;
};
struct ast_const {
enum ast_const_kind kind;
union {
long long integer;
const char *str;
double dbl;
};
};
struct ast_init {
struct ast_node *body;
};
struct ast_assign {
struct ast_node *to;
struct ast_node *from;
};
struct ast_id {
const char *id;
};
struct ast_empty {
};
struct ast_node {
enum ast_node_type node_type;
enum ast_flag flags;
struct ast_node *next;
struct src_loc loc;
/* scope ast node belongs to */
struct scope *scope;
/* type of ast node */
struct ast_node *type;
union {
struct ast_binop _binop;
struct ast_unop _unop;
struct ast_call _call;
struct ast_cast _cast;
struct ast_macro _macro;
struct ast_proc _proc;
struct ast_goto _goto;
struct ast_label _label;
struct ast_var _var;
struct ast_lambda _lambda;
struct ast_if _if;
struct ast_for _for;
struct ast_while _while;
struct ast_ctrl _ctrl;
struct ast_defer _defer;
struct ast_type _type;
struct ast_dot _dot;
struct ast_block _block;
struct ast_import _import;
struct ast_embed _embed;
struct ast_return _return;
struct ast_switch _switch;
struct ast_paste _paste;
struct ast_alias _alias;
struct ast_template _template;
struct ast_enum _enum;
struct ast_struct _struct;
struct ast_union _union;
struct ast_val _val;
struct ast_case _case;
struct ast_const _const;
struct ast_init _init;
struct ast_assign _assign;
struct ast_id _id;
struct ast_as _as;
struct ast_sizeof _sizeof;
struct ast_fetch _fetch;
struct ast_empty _empty;
};
};
struct ast_node *gen_binop(enum ast_binops op,
struct ast_node *left, struct ast_node *right);
struct ast_node *gen_unop(enum ast_unops op, struct ast_node *expr);
struct ast_node *gen_call(struct ast_node *id, struct ast_node *args);
struct ast_node *gen_id(const char *id);
struct ast_node *gen_int(long long integer);
struct ast_node *gen_string(const char *str);
struct ast_node *gen_float(double dbl);
struct ast_node *gen_assign(struct ast_node *to, struct ast_node *from);
struct ast_node *gen_init(struct ast_node *body);
struct ast_node *gen_while(struct ast_node *cond, struct ast_node *body);
struct ast_node *gen_for(struct ast_node *pre, struct ast_node *cond,
struct ast_node *post, struct ast_node *body);
struct ast_node *gen_return(struct ast_node *expr);
struct ast_node *gen_ctrl(enum ast_ctrl_kind kind, struct src_loc loc);
struct ast_node *gen_macro(struct ast_node *id, struct ast_node *params,
struct ast_node *body);
struct ast_node *gen_if(struct ast_node *cond, struct ast_node *body,
struct ast_node *els);
struct ast_node *gen_switch(struct ast_node *cond, struct ast_node *cases);
struct ast_node *gen_case(struct ast_node *expr, struct ast_node *body);
struct ast_node *gen_type(enum ast_type_kind kind, struct ast_node *id,
struct ast_node *decl, struct ast_node *rets);
struct ast_node *gen_block(struct ast_node *body);
struct ast_node *gen_var(struct ast_node *id, struct ast_node *type,
struct ast_node *init);
struct ast_node *gen_lambda(struct ast_node *captures,
struct ast_node *type, struct ast_node * body);
struct ast_node *gen_proc(struct ast_node *id, struct ast_node *type,
struct ast_node *body);
struct ast_node *gen_dot(struct ast_node *expr, struct ast_node *id);
struct ast_node *gen_enum(struct ast_node *id, struct ast_node *type,
struct ast_node *body);
struct ast_node *gen_val(struct ast_node *id, struct ast_node *val);
struct ast_node *gen_alias(struct ast_node *id, struct ast_node *type);
struct ast_node *gen_template(struct ast_node *id, struct ast_node *body);
struct ast_node *gen_import(const char *file);
struct ast_node *gen_cast(struct ast_node *expr, struct ast_node *type);
struct ast_node *gen_embed(const char *file);
struct ast_node *gen_goto(struct ast_node *label);
struct ast_node *gen_label(struct ast_node *id);
struct ast_node *gen_defer(struct ast_node *expr);
struct ast_node *gen_as(struct ast_node *type);
struct ast_node *gen_sizeof(struct ast_node *expr);
struct ast_node *gen_struct(struct ast_node *id, struct ast_node *generics,
struct ast_node *body);
struct ast_node *gen_union(struct ast_node *id, struct ast_node *generics,
struct ast_node *body);
struct ast_node *gen_fetch(struct ast_node *id, struct ast_node *type);
/* might have to come up with a better name for this */
struct ast_node *gen_last();
struct ast_node *gen_empty();
struct ast_node *clone_ast_node(struct ast_node *node);
int identical_ast_nodes(int exact, struct ast_node *left,
struct ast_node *right);
void destroy_ast_node(struct ast_node *node);
void destroy_ast_tree(struct ast_node *root);
void dump_ast(int depth, struct ast_node *root);
void ast_append(struct ast_node *list, struct ast_node *elem);
void ast_set_flags(struct ast_node *node, enum ast_flag flags);
void ast_clear_flags(struct ast_node *node, enum ast_flag flags);
int ast_flags(struct ast_node *node, enum ast_flag flags);
int ast_call_on(int (*call)(struct ast_node *node, void *data),
struct ast_node *node, void *data);
size_t ast_list_len(struct ast_node *list);
struct ast_node *ast_last_node(struct ast_node *node);
struct ast_node *ast_block_last(struct ast_node *node);
#endif /* AST_H */
|