]> 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 3474dfca472735545e6689529e1b642b753a2beb..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
 
@@ -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)
 {
@@ -228,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,
@@ -302,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;
 }