This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathast.h
361 lines (283 loc) · 5.43 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
//
// ast.h
//
// Copyright (c) 2013 TJ Holowaychuk <[email protected]>
//
#ifndef LUNA_AST_H
#define LUNA_AST_H
#include "token.h"
#include "vec.h"
#include "object.h"
/*
* Nodes.
*/
#define LUNA_NODE_LIST \
n(BLOCK) \
n(EXPR_STMT) \
n(RETURN) \
n(DEF) \
n(IF) \
n(WHILE) \
n(FOR) \
n(UNARY_OP) \
n(BINARY_OP) \
n(TERNARY_OP) \
n(BOOL) \
n(NULL) \
n(ID) \
n(DECL) \
n(LET) \
n(CALL) \
n(ARGS) \
n(INT) \
n(FLOAT) \
n(STRING) \
n(ARRAY) \
n(HASH_PAIR) \
n(HASH) \
n(FUNCTION) \
n(TYPE) \
n(SLOT) \
n(SUBSCRIPT) \
n(USE)
/*
* Nodes enum.
*/
typedef enum {
#define n(node) LUNA_NODE_##node,
LUNA_NODE_LIST
#undef n
} luna_node_type;
/*
* Luna node.
*/
typedef struct {
luna_node_type type;
int lineno;
} luna_node_t;
/*
* Luna block node.
*/
typedef struct {
luna_node_t base;
luna_vec_t *stmts;
} luna_block_node_t;
/*
* Luna args node.
*/
typedef struct {
luna_node_t base;
luna_vec_t *vec;
luna_hash_t *hash;
} luna_args_node_t;
/*
* Luna subscript node.
*/
typedef struct {
luna_node_t base;
luna_node_t *left;
luna_node_t *right;
} luna_subscript_node_t;
/*
* Luna slot access node.
*/
typedef struct {
luna_node_t base;
luna_node_t *left;
luna_node_t *right;
} luna_slot_node_t;
/*
* Luna unary operation node.
*/
typedef struct {
luna_node_t base;
luna_token op;
luna_node_t *expr;
int postfix;
} luna_unary_op_node_t;
/*
* Luna binary operation node.
*/
typedef struct {
luna_node_t base;
luna_token op;
luna_node_t *left;
luna_node_t *right;
int let;
} luna_binary_op_node_t;
/*
* Luna int node.
*/
typedef struct {
luna_node_t base;
int val;
} luna_int_node_t;
/*
* Luna float node.
*/
typedef struct {
luna_node_t base;
float val;
} luna_float_node_t;
/*
* Luna id node.
*/
typedef struct {
luna_node_t base;
const char *val;
} luna_id_node_t;
/*
* Luna declaration node.
*/
typedef struct {
luna_node_t base;
luna_vec_t *vec;
luna_node_t *type;
} luna_decl_node_t;
/*
* Luna let node.
*/
typedef struct {
luna_node_t base;
luna_vec_t *vec;
} luna_let_node_t;
/*
* Luna string node.
*/
typedef struct {
luna_node_t base;
const char *val;
} luna_string_node_t;
/*
* Luna array node.
*/
typedef struct {
luna_node_t base;
luna_vec_t *vals;
} luna_array_node_t;
/*
* Luna hash pair node.
*/
typedef struct {
luna_node_t base;
luna_node_t *key;
luna_node_t *val;
} luna_hash_pair_node_t;
/*
* Luna hash node.
*/
typedef struct {
luna_node_t base;
luna_vec_t *pairs;
} luna_hash_node_t;
/*
* Luna call node.
*/
typedef struct {
luna_node_t base;
luna_node_t *expr;
luna_args_node_t *args;
} luna_call_node_t;
/*
* Luna function node.
*/
typedef struct {
luna_node_t base;
const char *name;
luna_node_t *type;
luna_block_node_t *block;
luna_vec_t *params;
} luna_function_node_t;
/*
* Luna type node.
*/
typedef struct {
luna_node_t base;
const char *name;
luna_vec_t *fields;
} luna_type_node_t;
/*
* Luna if stmt node.
*/
typedef struct {
luna_node_t base;
int negate;
luna_node_t *expr;
luna_block_node_t *block;
luna_block_node_t *else_block;
luna_vec_t *else_ifs;
} luna_if_node_t;
/*
* Luna while loop stmt node.
*/
typedef struct {
luna_node_t base;
int negate;
luna_node_t *expr;
luna_block_node_t *block;
} luna_while_node_t;
/*
* Luna return node.
*/
typedef struct {
luna_node_t base;
luna_node_t *expr;
} luna_return_node_t;
/*
* Luna use node.
*/
typedef struct {
luna_node_t base;
char *module;
char *alias;
} luna_use_node_t;
// protos
luna_object_t *
luna_node(luna_node_t *node);
luna_block_node_t *
luna_block_node_new(int lineno);
luna_function_node_t *
luna_function_node_new(const char *name, luna_node_t *type, luna_block_node_t *block, luna_vec_t *params, int lineno);
luna_function_node_t *
luna_function_node_new_from_expr(luna_node_t *expr, luna_vec_t *params, int lineno);
luna_subscript_node_t *
luna_subscript_node_new(luna_node_t *left, luna_node_t *right, int lineno);
luna_slot_node_t *
luna_slot_node_new(luna_node_t *left, luna_node_t *right, int lineno);
luna_call_node_t *
luna_call_node_new(luna_node_t *expr, int lineno);
luna_unary_op_node_t *
luna_unary_op_node_new(luna_token op, luna_node_t *expr, int postfix, int lineno);
luna_binary_op_node_t *
luna_binary_op_node_new(luna_token op, luna_node_t *left, luna_node_t *right, int lineno);
luna_id_node_t *
luna_id_node_new(const char *val, int lineno);
luna_decl_node_t *
luna_decl_node_new(luna_vec_t *vec, luna_node_t *type, int lineno);
luna_let_node_t *
luna_let_node_new(luna_vec_t *vec, int lineno);
luna_int_node_t *
luna_int_node_new(int val, int lineno);
luna_float_node_t *
luna_float_node_new(float val, int lineno);
luna_array_node_t *
luna_array_node_new(int lineno);
luna_hash_pair_node_t *
luna_hash_pair_node_new(int lineno);
luna_hash_node_t *
luna_hash_node_new(int lineno);
luna_string_node_t *
luna_string_node_new(const char *val, int lineno);
luna_if_node_t *
luna_if_node_new(int negate, luna_node_t *expr, luna_block_node_t *block, int lineno);
luna_while_node_t *
luna_while_node_new(int negate, luna_node_t *expr, luna_block_node_t *block, int lineno);
luna_return_node_t *
luna_return_node_new(luna_node_t *expr, int lineno);
luna_args_node_t *
luna_args_node_new(int lineno);
luna_type_node_t *
luna_type_node_new(const char *name, int lineno);
luna_use_node_t *
luna_use_node_new(int lineno);
#endif /* LUNA_AST_H */