1 | /*
|
---|
2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef STREE_T_H_
|
---|
30 | #define STREE_T_H_
|
---|
31 |
|
---|
32 | #include "list_t.h"
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Arithmetic expressions
|
---|
36 | */
|
---|
37 |
|
---|
38 | struct stree_expr;
|
---|
39 |
|
---|
40 | /** Identifier */
|
---|
41 | typedef struct {
|
---|
42 | int sid;
|
---|
43 | } stree_ident_t;
|
---|
44 |
|
---|
45 | /** Name reference */
|
---|
46 | typedef struct {
|
---|
47 | stree_ident_t *name;
|
---|
48 | } stree_nameref_t;
|
---|
49 |
|
---|
50 | /** Reference to currently active object. */
|
---|
51 | typedef struct {
|
---|
52 | } stree_self_ref_t;
|
---|
53 |
|
---|
54 | typedef struct {
|
---|
55 | int value;
|
---|
56 | } stree_lit_int_t;
|
---|
57 |
|
---|
58 | /** Reference literal (there is only one: @c nil). */
|
---|
59 | typedef struct {
|
---|
60 | } stree_lit_ref_t;
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | char *value;
|
---|
64 | } stree_lit_string_t;
|
---|
65 |
|
---|
66 | typedef enum {
|
---|
67 | ltc_int,
|
---|
68 | ltc_ref,
|
---|
69 | ltc_string
|
---|
70 | } literal_class_t;
|
---|
71 |
|
---|
72 | /** Literal */
|
---|
73 | typedef struct {
|
---|
74 | literal_class_t ltc;
|
---|
75 | union {
|
---|
76 | stree_lit_int_t lit_int;
|
---|
77 | stree_lit_ref_t lit_ref;
|
---|
78 | stree_lit_string_t lit_string;
|
---|
79 | } u;
|
---|
80 | } stree_literal_t;
|
---|
81 |
|
---|
82 | /** Binary operation class */
|
---|
83 | typedef enum {
|
---|
84 | bo_period,
|
---|
85 | bo_slash,
|
---|
86 | bo_sbr,
|
---|
87 | bo_equal,
|
---|
88 | bo_notequal,
|
---|
89 | bo_lt,
|
---|
90 | bo_gt,
|
---|
91 | bo_lt_equal,
|
---|
92 | bo_gt_equal,
|
---|
93 | bo_plus
|
---|
94 | } binop_class_t;
|
---|
95 |
|
---|
96 | /** Unary operation class */
|
---|
97 | typedef enum {
|
---|
98 | uo_plus
|
---|
99 | } unop_class_t;
|
---|
100 |
|
---|
101 | /** Binary operation */
|
---|
102 | typedef struct {
|
---|
103 | /** Binary operation class */
|
---|
104 | binop_class_t bc;
|
---|
105 |
|
---|
106 | /** Arguments */
|
---|
107 | struct stree_expr *arg1, *arg2;
|
---|
108 | } stree_binop_t;
|
---|
109 |
|
---|
110 | /** Unary operation */
|
---|
111 | typedef struct {
|
---|
112 | /** Operation class */
|
---|
113 | unop_class_t oc;
|
---|
114 |
|
---|
115 | /** Argument */
|
---|
116 | struct stree_expr *arg;
|
---|
117 | } stree_unop_t;
|
---|
118 |
|
---|
119 | /** New operation */
|
---|
120 | typedef struct {
|
---|
121 | /** Type of object to construct. */
|
---|
122 | struct stree_texpr *texpr;
|
---|
123 | } stree_new_t;
|
---|
124 |
|
---|
125 | /** Member access operation */
|
---|
126 | typedef struct {
|
---|
127 | /** Argument */
|
---|
128 | struct stree_expr *arg;
|
---|
129 | /** Name of member being accessed. */
|
---|
130 | stree_ident_t *member_name;
|
---|
131 | } stree_access_t;
|
---|
132 |
|
---|
133 | /** Function call operation */
|
---|
134 | typedef struct {
|
---|
135 | /** Function */
|
---|
136 | struct stree_expr *fun;
|
---|
137 |
|
---|
138 | /** Arguments */
|
---|
139 | list_t args; /* of stree_expr_t */
|
---|
140 | } stree_call_t;
|
---|
141 |
|
---|
142 | typedef enum {
|
---|
143 | ac_set,
|
---|
144 | ac_increase
|
---|
145 | } assign_class_t;
|
---|
146 |
|
---|
147 | /** Assignment */
|
---|
148 | typedef struct {
|
---|
149 | assign_class_t ac;
|
---|
150 | struct stree_expr *dest, *src;
|
---|
151 | } stree_assign_t;
|
---|
152 |
|
---|
153 | /** Indexing operation */
|
---|
154 | typedef struct {
|
---|
155 | /** Base */
|
---|
156 | struct stree_expr *base;
|
---|
157 |
|
---|
158 | /** Arguments (indices) */
|
---|
159 | list_t args; /* of stree_expr_t */
|
---|
160 | } stree_index_t;
|
---|
161 |
|
---|
162 | /** Arithmetic expression class */
|
---|
163 | typedef enum {
|
---|
164 | ec_nameref,
|
---|
165 | ec_literal,
|
---|
166 | ec_self_ref,
|
---|
167 | ec_binop,
|
---|
168 | ec_unop,
|
---|
169 | ec_new,
|
---|
170 | ec_access,
|
---|
171 | ec_call,
|
---|
172 | ec_assign,
|
---|
173 | ec_index
|
---|
174 | } expr_class_t;
|
---|
175 |
|
---|
176 | /** Arithmetic expression */
|
---|
177 | typedef struct stree_expr {
|
---|
178 | expr_class_t ec;
|
---|
179 |
|
---|
180 | union {
|
---|
181 | stree_nameref_t *nameref;
|
---|
182 | stree_literal_t *literal;
|
---|
183 | stree_self_ref_t *self_ref;
|
---|
184 | stree_binop_t *binop;
|
---|
185 | stree_unop_t *unop;
|
---|
186 | stree_new_t *new_op;
|
---|
187 | stree_access_t *access;
|
---|
188 | stree_call_t *call;
|
---|
189 | stree_index_t *index;
|
---|
190 | stree_assign_t *assign;
|
---|
191 | } u;
|
---|
192 | } stree_expr_t;
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Type expressions
|
---|
196 | */
|
---|
197 |
|
---|
198 | struct stree_texpr;
|
---|
199 |
|
---|
200 | /** Type literal class */
|
---|
201 | typedef enum {
|
---|
202 | tlc_int,
|
---|
203 | tlc_string
|
---|
204 | } tliteral_class_t;
|
---|
205 |
|
---|
206 | /** Type literal */
|
---|
207 | typedef struct {
|
---|
208 | tliteral_class_t tlc;
|
---|
209 | } stree_tliteral_t;
|
---|
210 |
|
---|
211 | /** Type name reference */
|
---|
212 | typedef struct {
|
---|
213 | stree_ident_t *name;
|
---|
214 | } stree_tnameref_t;
|
---|
215 |
|
---|
216 | /** Type member access operation */
|
---|
217 | typedef struct {
|
---|
218 | /** Argument */
|
---|
219 | struct stree_texpr *arg;
|
---|
220 | /** Name of member being accessed. */
|
---|
221 | stree_ident_t *member_name;
|
---|
222 | } stree_taccess_t;
|
---|
223 |
|
---|
224 | /** Type application operation */
|
---|
225 | typedef struct {
|
---|
226 | /** Arguments */
|
---|
227 | struct stree_texpr *gtype, *targ;
|
---|
228 | } stree_tapply_t;
|
---|
229 |
|
---|
230 | /** Type index operation */
|
---|
231 | typedef struct {
|
---|
232 | /** Base type */
|
---|
233 | struct stree_texpr *base_type;
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Number of arguments (rank). Needed when only rank is specified
|
---|
237 | * and @c args are not used.
|
---|
238 | */
|
---|
239 | int n_args;
|
---|
240 |
|
---|
241 | /** Arguments (extents) */
|
---|
242 | list_t args; /* of stree_expr_t */
|
---|
243 | } stree_tindex_t;
|
---|
244 |
|
---|
245 | /** Type expression class */
|
---|
246 | typedef enum {
|
---|
247 | tc_tliteral,
|
---|
248 | tc_tnameref,
|
---|
249 | tc_taccess,
|
---|
250 | tc_tapply,
|
---|
251 | tc_tindex
|
---|
252 | } texpr_class_t;
|
---|
253 |
|
---|
254 | /** Arithmetic expression */
|
---|
255 | typedef struct stree_texpr {
|
---|
256 | texpr_class_t tc;
|
---|
257 |
|
---|
258 | union {
|
---|
259 | stree_tliteral_t *tliteral;
|
---|
260 | stree_tnameref_t *tnameref;
|
---|
261 | stree_taccess_t *taccess;
|
---|
262 | stree_tapply_t *tapply;
|
---|
263 | stree_tindex_t *tindex;
|
---|
264 | } u;
|
---|
265 | } stree_texpr_t;
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * Statements, class members and module members.
|
---|
269 | */
|
---|
270 |
|
---|
271 | /** Statement block */
|
---|
272 | typedef struct stree_block {
|
---|
273 | /** List of statements in the block */
|
---|
274 | list_t stats; /* of stree_stat_t */
|
---|
275 | } stree_block_t;
|
---|
276 |
|
---|
277 | /** Variable declaration */
|
---|
278 | typedef struct {
|
---|
279 | stree_ident_t *name;
|
---|
280 | stree_texpr_t *type;
|
---|
281 | } stree_vdecl_t;
|
---|
282 |
|
---|
283 | /** @c except clause */
|
---|
284 | typedef struct {
|
---|
285 | stree_ident_t *evar;
|
---|
286 | stree_texpr_t *etype;
|
---|
287 | stree_block_t *block;
|
---|
288 | } stree_except_t;
|
---|
289 |
|
---|
290 | /** If statement */
|
---|
291 | typedef struct {
|
---|
292 | stree_expr_t *cond;
|
---|
293 | stree_block_t *if_block;
|
---|
294 | stree_block_t *else_block;
|
---|
295 | } stree_if_t;
|
---|
296 |
|
---|
297 | /** While statement */
|
---|
298 | typedef struct {
|
---|
299 | stree_expr_t *cond;
|
---|
300 | stree_block_t *body;
|
---|
301 | } stree_while_t;
|
---|
302 |
|
---|
303 | /** For statement */
|
---|
304 | typedef struct {
|
---|
305 | stree_block_t *body;
|
---|
306 | } stree_for_t;
|
---|
307 |
|
---|
308 | /** Raise statement */
|
---|
309 | typedef struct {
|
---|
310 | stree_expr_t *expr;
|
---|
311 | } stree_raise_t;
|
---|
312 |
|
---|
313 | /** Return statement */
|
---|
314 | typedef struct {
|
---|
315 | stree_expr_t *expr;
|
---|
316 | } stree_return_t;
|
---|
317 |
|
---|
318 | /** Expression statement */
|
---|
319 | typedef struct {
|
---|
320 | stree_expr_t *expr;
|
---|
321 | } stree_exps_t;
|
---|
322 |
|
---|
323 | /** With-try-except-finally statement (WEF) */
|
---|
324 | typedef struct {
|
---|
325 | stree_block_t *with_block;
|
---|
326 | list_t except_clauses; /* of stree_except_t */
|
---|
327 | stree_block_t *finally_block;
|
---|
328 | } stree_wef_t;
|
---|
329 |
|
---|
330 | /** Statement class */
|
---|
331 | typedef enum {
|
---|
332 | st_vdecl,
|
---|
333 | st_if,
|
---|
334 | st_while,
|
---|
335 | st_for,
|
---|
336 | st_raise,
|
---|
337 | st_return,
|
---|
338 | st_exps,
|
---|
339 | st_wef
|
---|
340 | } stat_class_t;
|
---|
341 |
|
---|
342 | /** Statement */
|
---|
343 | typedef struct {
|
---|
344 | stat_class_t sc;
|
---|
345 |
|
---|
346 | union {
|
---|
347 | stree_vdecl_t *vdecl_s;
|
---|
348 | stree_if_t *if_s;
|
---|
349 | stree_while_t *while_s;
|
---|
350 | stree_for_t *for_s;
|
---|
351 | stree_raise_t *raise_s;
|
---|
352 | stree_return_t *return_s;
|
---|
353 | stree_exps_t *exp_s;
|
---|
354 | stree_wef_t *wef_s;
|
---|
355 | } u;
|
---|
356 | } stree_stat_t;
|
---|
357 |
|
---|
358 | /** Argument attribute class */
|
---|
359 | typedef enum {
|
---|
360 | /** Packed argument (for variadic functions) */
|
---|
361 | aac_packed
|
---|
362 | } arg_attr_class_t;
|
---|
363 |
|
---|
364 | /** Argument atribute */
|
---|
365 | typedef struct {
|
---|
366 | arg_attr_class_t aac;
|
---|
367 | } stree_arg_attr_t;
|
---|
368 |
|
---|
369 | /** Formal function parameter */
|
---|
370 | typedef struct {
|
---|
371 | /* Argument name */
|
---|
372 | stree_ident_t *name;
|
---|
373 |
|
---|
374 | /* Argument type */
|
---|
375 | stree_texpr_t *type;
|
---|
376 |
|
---|
377 | /* Attributes */
|
---|
378 | list_t attr; /* of stree_arg_attr_t */
|
---|
379 | } stree_proc_arg_t;
|
---|
380 |
|
---|
381 | /** Member function declaration */
|
---|
382 | typedef struct {
|
---|
383 | /** Function name */
|
---|
384 | stree_ident_t *name;
|
---|
385 |
|
---|
386 | /** Symbol */
|
---|
387 | struct stree_symbol *symbol;
|
---|
388 |
|
---|
389 | /** Formal parameters */
|
---|
390 | list_t args; /* of stree_proc_arg_t */
|
---|
391 |
|
---|
392 | /** Variadic argument or @c NULL if none. */
|
---|
393 | stree_proc_arg_t *varg;
|
---|
394 |
|
---|
395 | /** Return type */
|
---|
396 | stree_texpr_t *rtype;
|
---|
397 |
|
---|
398 | /** Function body */
|
---|
399 | stree_block_t *body;
|
---|
400 | } stree_fun_t;
|
---|
401 |
|
---|
402 | /** Member variable declaration */
|
---|
403 | typedef struct {
|
---|
404 | stree_ident_t *name;
|
---|
405 | struct stree_symbol *symbol;
|
---|
406 | stree_texpr_t *type;
|
---|
407 | } stree_var_t;
|
---|
408 |
|
---|
409 | /** Member property declaration */
|
---|
410 | typedef struct {
|
---|
411 | stree_ident_t *name;
|
---|
412 | struct stree_symbol *symbol;
|
---|
413 | stree_texpr_t *type;
|
---|
414 |
|
---|
415 | stree_block_t *getter_body;
|
---|
416 | stree_ident_t *setter_arg_name;
|
---|
417 | stree_block_t *setter_body;
|
---|
418 |
|
---|
419 | /** Formal parameters (for indexed properties) */
|
---|
420 | list_t args; /* of stree_proc_arg_t */
|
---|
421 |
|
---|
422 | /** Variadic argument or @c NULL if none. */
|
---|
423 | stree_proc_arg_t *varg;
|
---|
424 | } stree_prop_t;
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Fake identifier used with indexed properties. (Mostly for error messages.)
|
---|
428 | */
|
---|
429 | #define INDEXER_IDENT "$indexer"
|
---|
430 |
|
---|
431 | typedef enum {
|
---|
432 | csimbr_csi,
|
---|
433 | csimbr_fun,
|
---|
434 | csimbr_var,
|
---|
435 | csimbr_prop
|
---|
436 | } csimbr_class_t;
|
---|
437 |
|
---|
438 | /** Class, struct or interface member */
|
---|
439 | typedef struct {
|
---|
440 | csimbr_class_t cc;
|
---|
441 |
|
---|
442 | union {
|
---|
443 | struct stree_csi *csi;
|
---|
444 | stree_fun_t *fun;
|
---|
445 | stree_var_t *var;
|
---|
446 | stree_prop_t *prop;
|
---|
447 | } u;
|
---|
448 | } stree_csimbr_t;
|
---|
449 |
|
---|
450 | typedef enum {
|
---|
451 | csi_class,
|
---|
452 | csi_struct,
|
---|
453 | csi_interface
|
---|
454 | } csi_class_t;
|
---|
455 |
|
---|
456 | /** Class, struct or interface declaration */
|
---|
457 | typedef struct stree_csi {
|
---|
458 | /** Which of class, struct or interface */
|
---|
459 | csi_class_t cc;
|
---|
460 |
|
---|
461 | /** Name of this CSI */
|
---|
462 | stree_ident_t *name;
|
---|
463 |
|
---|
464 | /** Symbol for this CSI */
|
---|
465 | struct stree_symbol *symbol;
|
---|
466 |
|
---|
467 | /** Type expression referencing base CSI. */
|
---|
468 | stree_texpr_t *base_csi_ref;
|
---|
469 |
|
---|
470 | /** Base CSI. Only available when ancr_state == ws_visited. */
|
---|
471 | struct stree_csi *base_csi;
|
---|
472 |
|
---|
473 | /** Node state for ancr walks. */
|
---|
474 | walk_state_t ancr_state;
|
---|
475 |
|
---|
476 | /** List of CSI members */
|
---|
477 | list_t members; /* of stree_csimbr_t */
|
---|
478 | } stree_csi_t;
|
---|
479 |
|
---|
480 | typedef enum {
|
---|
481 | /* Class, struct or interface declaration */
|
---|
482 | mc_csi
|
---|
483 | } modm_class_t;
|
---|
484 |
|
---|
485 | /** Module member */
|
---|
486 | typedef struct {
|
---|
487 | modm_class_t mc;
|
---|
488 | union {
|
---|
489 | stree_csi_t *csi;
|
---|
490 | } u;
|
---|
491 | } stree_modm_t;
|
---|
492 |
|
---|
493 | /** Module */
|
---|
494 | typedef struct stree_module {
|
---|
495 | /** List of module members */
|
---|
496 | list_t members; /* of stree_modm_t */
|
---|
497 | } stree_module_t;
|
---|
498 |
|
---|
499 | typedef enum {
|
---|
500 | sc_csi,
|
---|
501 | sc_fun,
|
---|
502 | sc_var,
|
---|
503 | sc_prop
|
---|
504 | } symbol_class_t;
|
---|
505 |
|
---|
506 | /** Symbol */
|
---|
507 | typedef struct stree_symbol {
|
---|
508 | symbol_class_t sc;
|
---|
509 |
|
---|
510 | union {
|
---|
511 | struct stree_csi *csi;
|
---|
512 | stree_fun_t *fun;
|
---|
513 | stree_var_t *var;
|
---|
514 | stree_prop_t *prop;
|
---|
515 | } u;
|
---|
516 |
|
---|
517 | /** Containing CSI (for all symbols) */
|
---|
518 | stree_csi_t *outer_csi;
|
---|
519 |
|
---|
520 | /** Containing block (for block-level symbols) */
|
---|
521 | stree_block_t *outer_block;
|
---|
522 | } stree_symbol_t;
|
---|
523 |
|
---|
524 | /** Program */
|
---|
525 | typedef struct stree_program {
|
---|
526 | /** The one and only module in the program */
|
---|
527 | stree_module_t *module;
|
---|
528 | } stree_program_t;
|
---|
529 |
|
---|
530 | #endif
|
---|