]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ast.c
if-then-else AST node - this one is not for ternary expressions
[xonotic/gmqcc.git] / ast.c
diff --git a/ast.c b/ast.c
index aede5aa2688fd7166d420a0521d3f66ec48cbac1..3ab7b3755db6a8372292b90d6d821b85d83f7e3f 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -29,6 +29,9 @@
 
 #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
 
@@ -80,7 +83,7 @@ void ast_value_delete(ast_value* self)
     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);
@@ -132,6 +135,49 @@ void ast_binary_delete(ast_binary *self)
     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)
 {
@@ -181,13 +227,14 @@ void ast_block_delete(ast_block *self)
 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;
@@ -227,7 +274,7 @@ void ast_function_delete(ast_function *self)
 /* 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,
@@ -301,17 +348,29 @@ bool ast_function_codegen(ast_function *self, ir_builder *ir)
     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;
 }