]> 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 1dfcf684ce4a56d6710b9aa543aa9d1d1e26752d..3ab7b3755db6a8372292b90d6d821b85d83f7e3f 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 
+ * Copyright (C) 2012
  *     Wolfgang Bumiller
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 #include "gmqcc.h"
 #include "ast.h"
 
-#define ast_instantiate(T, ctx, destroyfn)                    \
-    T *self = (T*)mem_a(sizeof(T));                           \
-    ast_node_init((ast_node*)self, ctx);                      \
-    ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn;
+#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
 
 /* It must not be possible to get here. */
 static void _ast_node_destroy(ast_node *self)
@@ -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)
 {
@@ -180,15 +226,16 @@ void ast_block_delete(ast_block *self)
 
 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
 {
-    if (!vtype)
-        return NULL;
-    if (vtype->isconst)
-        return NULL;
-    if (vtype->vtype != TYPE_FUNCTION)
-        return NULL;
-
     ast_instantiate(ast_function, ctx, ast_function_delete);
 
+    if (!vtype ||
+        vtype->isconst ||
+        vtype->vtype != TYPE_FUNCTION)
+    {
+        mem_d(self);
+        return NULL;
+    }
+
     self->vtype = vtype;
     self->name = name ? util_strdup(name) : NULL;
     MEM_VECTOR_INIT(self, blocks);
@@ -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,
@@ -241,6 +288,7 @@ bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
 
 bool ast_global_codegen(ast_value *self, ir_builder *ir)
 {
+    ir_value *v = NULL;
     if (self->isconst && self->vtype == TYPE_FUNCTION)
     {
         ir_function *func = ir_builder_create_function(ir, self->name);
@@ -252,7 +300,7 @@ bool ast_global_codegen(ast_value *self, ir_builder *ir)
         return true;
     }
 
-    ir_value *v = ir_builder_create_global(ir, self->name, self->vtype);
+    v = ir_builder_create_global(ir, self->name, self->vtype);
     if (!v)
         return false;
 
@@ -300,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;
 }