#define ast_instantiate(T, ctx, destroyfn) \
T* self = (T*)mem_a(sizeof(T)); \
+ if (!self) { \
+ return NULL; \
+ } \
ast_node_init((ast_node*)self, ctx); \
( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
if (self->name)
mem_d((void*)self->name);
for (i = 0; i < self->params_count; ++i)
- ast_unref(self->params[i]);
+ ast_value_delete(self->params[i]); /* delete, the ast_function is expected to die first */
MEM_VECTOR_CLEAR(self, params);
if (self->next) /* delete, not unref, types are always copied */
ast_delete(self->next);
mem_d(self);
}
+ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
+{
+ ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
+ ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
+
+ self->entity = entity;
+ self->field = field;
+
+ return self;
+}
+
+void ast_entfield_delete(ast_entfield *self)
+{
+ ast_unref(self->entity);
+ ast_unref(self->field);
+ mem_d(self);
+}
+
+ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
+{
+ ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
+ if (!ontrue && !onfalse) {
+ /* because it is invalid */
+ mem_d(self);
+ return NULL;
+ }
+ ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
+
+ self->cond = cond;
+ self->on_true = ontrue;
+ self->on_false = onfalse;
+
+ return self;
+}
+
+void ast_ifthen_delete(ast_ifthen *self)
+{
+ ast_unref(self->cond);
+ ast_unref(self->on_true);
+ ast_unref(self->on_false);
+ mem_d(self);
+}
+
ast_store* ast_store_new(lex_ctx ctx, int op,
ast_value *dest, ast_expression *source)
{
ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
{
ast_instantiate(ast_function, ctx, ast_function_delete);
-
- if (!vtype)
- return NULL;
- if (vtype->isconst)
- return NULL;
- if (vtype->vtype != TYPE_FUNCTION)
+
+ if (!vtype ||
+ vtype->isconst ||
+ vtype->vtype != TYPE_FUNCTION)
+ {
+ mem_d(self);
return NULL;
+ }
self->vtype = vtype;
self->name = name ? util_strdup(name) : NULL;
/* AST codegen aprt
*/
-bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
+bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
{
/* NOTE: This is the codegen for a variable used in an expression.
* It is not the codegen to generate the value. For this purpose,
return false;
}
-bool ast_block_codegen(ast_block *self, ast_function *func, ir_value **out)
+bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ return false;
+}
+
+bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
+{
+ /* NOTE: remember: destination codegen needs to have lvalue=true */
+ return false;
+}
+
+bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
{
return false;
}
-bool ast_store_codegen(ast_store *self, ast_function *func, ir_value **out)
+bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
{
return false;
}
-bool ast_binary_codegen(ast_binary *self, ast_function *func, ir_value **out)
+bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
{
+ if (out) *out = NULL;
return false;
}