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
|
#ifndef AST_H
#define AST_H
#include "scope.h"
struct stmt_node;
struct expr_node;
struct type_node {
bool _unsigned;
bool _short;
bool _long;
struct type_def def;
unsigned char ptr_level;
};
struct int_lit_node {
long long val;
};
struct float_lit_node {
double val;
};
struct char_lit_node {
char val;
};
struct str_lit_node {
char* val;
};
struct var_ref_node {
char* ident;
};
struct var_decl_node {
struct type_node type;
char* ident;
struct var_decl_node* next;
};
struct lval_node {
enum {
LVAL_VAR_DECL,
LVAL_VAR_REF,
} type;
union {
struct var_ref_node _var_ref;
struct var_decl_node _var_decl;
} as;
};
/* TODO: add to expression, parse */
struct assign_node {
struct lval_node lval;
struct expr_node* rval;
};
struct call_node {
/* TODO: eventually this could also be a function pointer */
struct fn_decl_node* called_fn;
struct expr_node* args_head;
};
struct expr_node {
enum {
EXPR_INT_LIT,
EXPR_FLOAT_LIT,
EXPR_CHAR_LIT,
EXPR_STR_LIT,
EXPR_VAR_REF,
EXPR_ASSIGN,
EXPR_CALL,
} type;
union {
struct int_lit_node _int_lit;
struct float_lit_node _float_lit;
struct char_lit_node _char_lit;
struct str_lit_node _str_lit;
struct var_ref_node _var_ref;
struct assign_node _assign;
struct call_node _call;
} as;
struct expr_node* next;
};
struct group_node {
struct stmt_node* body_head;
};
struct fn_decl_node {
struct type_node return_type;
char* name;
struct var_decl_node* args_head;
struct group_node body;
};
struct return_node {
struct expr_node* ret_val; /* null to return void */
};
struct stmt_node {
enum {
STMT_EMPTY,
STMT_EXPR,
STMT_VAR_DECL,
STMT_RETURN,
STMT_GROUP,
} type;
union {
struct expr_node _expr;
struct var_decl_node _var_decl;
struct return_node _return;
struct group_node _group;
} as;
struct stmt_node* next;
};
struct root_node {
enum {
ROOT_FN_DECL,
} type;
union {
struct fn_decl_node _fn_decl;
} as;
struct root_node* next;
};
void ast_destroy(struct root_node* head);
#endif
|