7 #define ast_setfunc(me, fn, what) ( *(void**)&((me)->fn) = what )
9 #define ast_instantiate(T, ctx, destroyfn) \
10 T *self = (T*)mem_a(sizeof(T)); \
11 ast_node_init((ast_node*)self, ctx); \
12 ast_setfunc(&((ast_node*)self)->node, destroy, destroyfn)
14 /* It must not be possible to get here. */
15 static void _ast_node_destroy(ast_node *self)
17 fprintf(stderr, "ast node missing destroy()\n");
21 /* Initialize main ast node aprts */
22 static void ast_node_init(ast_node *self, lex_ctx_t ctx)
24 self->node.context = ctx;
25 self->node.destroy = &_ast_node_destroy;
28 /* General expression initialization */
29 static void ast_expression_init(ast_expression *self,
30 ast_expression_codegen *codegen)
32 ast_setfunc(&self->expression, codegen, codegen);
35 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, qc_type_t t)
37 ast_instantiate(ast_value, ctx, ast_value_delete);
38 ast_expression_init((ast_expression*)self,
39 (ast_expression_codegen*)&ast_value_codegen);
41 self->name = name ? util_strdup(name) : NULL;
44 VEC_INIT(self, params);
45 self->has_constval = ifalse;
46 memset(&self->cvalue, 0, sizeof(self->cvalue));
52 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
54 void ast_value_delete(ast_value* self)
58 mem_d((void*)self->_name);
59 for (i = 0; i < self->params_count; ++i)
60 ast_delete(self->params[i]);
61 VEC_CLEAR(self, params);
63 ast_delete(self->next);
64 if (self->has_constval) {
68 mem_d((void*)self->cvalue.vstring);
70 /* NOTE: delete function? currently collected in
71 * the parser structure
80 void ast_value_set_name(ast_value *self, const char *name)
83 mem_d((void*)self->_name);
84 self->_name = strdup(name);
87 ast_binary* ast_binary_new(filecontext_t ctx, qc_op_t op,
88 ast_value* left, ast_value* right)
90 ast_instantiate(ast_binary, ctx, ast_binary_delete);
91 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)codegen);
100 void ast_binary_delete(ast_binary *self)
105 ast_block* ast_block_new(filecontext_t ctx)
107 ast_instantiate(ast_block, ctx, ast_block_delete);
108 ast_expression_init((ast_expression*)self,
109 (ast_expression_codegen*)&ast_block_codegen);
111 VEC_INIT(self, locals);
112 VEC_INIT(self, exprs);
116 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
117 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
119 void ast_block_delete(ast_block *self)
122 for (i = 0; i < self->locals_count; ++i)
123 ast_delete(self->locals[i]);
124 VEC_CLEAR(self, locals);
125 for (i = 0; i < self->exprs_count; ++i)
126 ast_delete(self->exprs[i]);
127 VEC_CLEAR(self, exprs);
131 ast_function* ast_function_new(filecontext_t ctx, const char *name, ast_value *vtype)
133 ast_instantiate(ast_function, ctx, ast_function_delete);
136 self->_name = name ? strdup(name) : NULL;
137 VEC_INIT(self, blocks);
142 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
144 void ast_function_delete(ast_function *self)
148 mem_d((void*)self->_name);
150 ast_value_delete(self->vtype);
151 for (i = 0; i < self->blocks_count; ++i)
152 ast_delete(self->blocks[i]);
153 VEC_CLEAR(self, blocks);