-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathast.c
More file actions
272 lines (244 loc) · 5.91 KB
/
ast.c
File metadata and controls
272 lines (244 loc) · 5.91 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
#include "ast.h"
ast_decl_t ast_funcs_decl(int pos, list_t funcs)
{
ast_decl_t p = checked_malloc(sizeof(*p));
p->kind = AST_FUNCS_DECL;
p->pos = pos;
p->u.funcs = funcs;
return p;
}
ast_decl_t ast_types_decl(int pos, list_t types)
{
ast_decl_t p = checked_malloc(sizeof(*p));
p->kind = AST_TYPES_DECL;
p->pos = pos;
p->u.types = types;
return p;
}
ast_decl_t ast_var_decl(int pos, symbol_t var, symbol_t type, ast_expr_t init)
{
ast_decl_t p = checked_malloc(sizeof(*p));
p->kind = AST_VAR_DECL;
p->pos = pos;
p->u.var.var = var;
p->u.var.type = type;
p->u.var.init = init;
p->u.var.escape = false;
return p;
}
ast_expr_t ast_nil_expr(int pos)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_NIL_EXPR;
p->pos = pos;
return p;
}
ast_expr_t ast_var_expr(int pos, ast_var_t var)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_VAR_EXPR;
p->pos = pos;
p->u.var = var;
return p;
}
ast_expr_t ast_num_expr(int pos, int num)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_NUM_EXPR;
p->pos = pos;
p->u.num = num;
return p;
}
ast_expr_t ast_string_expr(int pos, string_t str)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_STRING_EXPR;
p->pos = pos;
p->u.str = str;
return p;
}
ast_expr_t ast_call_expr(int pos, symbol_t func, list_t args)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_CALL_EXPR;
p->pos = pos;
p->u.call.func = func;
p->u.call.args = args;
return p;
}
ast_expr_t ast_op_expr(int pos, ast_expr_t left, ast_binop_t op, ast_expr_t right)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_OP_EXPR;
p->pos = pos;
p->u.op.left = left;
p->u.op.op = op;
p->u.op.right = right;
return p;
}
ast_expr_t ast_record_expr(int pos, symbol_t type, list_t efields)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_RECORD_EXPR;
p->pos = pos;
p->u.record.type = type;
p->u.record.efields = efields;
return p;
}
ast_expr_t ast_array_expr(int pos, symbol_t type, ast_expr_t size, ast_expr_t init)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_ARRAY_EXPR;
p->pos = pos;
p->u.array.type = type;
p->u.array.size = size;
p->u.array.init = init;
return p;
}
ast_expr_t ast_seq_expr(int pos, list_t seq)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_SEQ_EXPR;
p->pos = pos;
p->u.seq = seq;
return p;
}
ast_expr_t ast_if_expr(int pos, ast_expr_t cond, ast_expr_t then, ast_expr_t else_)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_IF_EXPR;
p->pos = pos;
p->u.if_.cond = cond;
p->u.if_.then = then;
p->u.if_.else_ = else_;
return p;
}
ast_expr_t ast_while_expr(int pos, ast_expr_t cond, ast_expr_t body)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_WHILE_EXPR;
p->pos = pos;
p->u.while_.cond = cond;
p->u.while_.body = body;
return p;
}
ast_expr_t ast_for_expr(int pos, symbol_t var, ast_expr_t lo, ast_expr_t hi, ast_expr_t body)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_FOR_EXPR;
p->pos = pos;
p->u.for_.var = var;
p->u.for_.lo = lo;
p->u.for_.hi = hi;
p->u.for_.body = body;
p->u.for_.escape = false;
return p;
}
ast_expr_t ast_break_expr(int pos)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_BREAK_EXPR;
p->pos = pos;
return p;
}
ast_expr_t ast_let_expr(int pos, list_t decls, ast_expr_t body)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_LET_EXPR;
p->pos = pos;
p->u.let.decls = decls;
p->u.let.body = body;
return p;
}
ast_expr_t ast_assign_expr(int pos, ast_var_t var, ast_expr_t expr)
{
ast_expr_t p = checked_malloc(sizeof(*p));
p->kind = AST_ASSIGN_EXPR;
p->pos = pos;
p->u.assign.var = var;
p->u.assign.expr = expr;
return p;
}
ast_type_t ast_name_type(int pos, symbol_t name)
{
ast_type_t p = checked_malloc(sizeof(*p));
p->kind = AST_NAME_TYPE;
p->pos = pos;
p->u.name = name;
return p;
}
ast_type_t ast_record_type(int pos, list_t record)
{
ast_type_t p = checked_malloc(sizeof(*p));
p->kind = AST_RECORD_TYPE;
p->pos = pos;
p->u.record = record;
return p;
}
ast_type_t ast_array_type(int pos, symbol_t array)
{
ast_type_t p = checked_malloc(sizeof(*p));
p->kind = AST_ARRAY_TYPE;
p->pos = pos;
p->u.array = array;
return p;
}
ast_var_t ast_simple_var(int pos, symbol_t simple)
{
ast_var_t p = checked_malloc(sizeof(*p));
p->kind = AST_SIMPLE_VAR;
p->pos = pos;
p->u.simple = simple;
return p;
}
ast_var_t ast_field_var(int pos, ast_var_t var, symbol_t field)
{
ast_var_t p = checked_malloc(sizeof(*p));
p->kind = AST_FIELD_VAR;
p->pos = pos;
p->u.field.var = var;
p->u.field.field = field;
return p;
}
ast_var_t ast_sub_var(int pos, ast_var_t var, ast_expr_t sub)
{
ast_var_t p = checked_malloc(sizeof(*p));
p->kind = AST_SUB_VAR;
p->pos = pos;
p->u.sub.var = var;
p->u.sub.sub = sub;
return p;
}
ast_efield_t ast_efield(int pos, symbol_t name, ast_expr_t expr)
{
ast_efield_t p = checked_malloc(sizeof(*p));
p->pos = pos;
p->name = name;
p->expr = expr;
return p;
}
ast_field_t ast_field(symbol_t name, symbol_t type)
{
ast_field_t p = checked_malloc(sizeof(*p));
p->name = name;
p->type = type;
p->escape = false;
return p;
}
ast_func_t ast_func(int pos, symbol_t name, list_t params, symbol_t result, ast_expr_t body)
{
ast_func_t p = checked_malloc(sizeof(*p));
p->pos = pos;
p->name = name;
p->params = params;
p->result = result;
p->body = body;
return p;
}
ast_nametype_t ast_nametype(symbol_t name, ast_type_t type)
{
ast_nametype_t p = checked_malloc(sizeof(*p));
p->name = name;
p->type = type;
return p;
}