]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
if-then-else AST node - this one is not for ternary expressions
authorWolfgang Bumiller <wolfgang.linux@bumiller.com>
Tue, 1 May 2012 14:55:02 +0000 (16:55 +0200)
committerWolfgang Bumiller <wolfgang.linux@bumiller.com>
Tue, 1 May 2012 14:55:02 +0000 (16:55 +0200)
ast.c
ast.h

diff --git a/ast.c b/ast.c
index 168a80e228ec3d3ce08647e2886d9bd1f1dee4b8..3ab7b3755db6a8372292b90d6d821b85d83f7e3f 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -153,6 +153,31 @@ void ast_entfield_delete(ast_entfield *self)
     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)
 {
@@ -343,3 +368,9 @@ bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, i
 {
     return false;
 }
+
+bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
+{
+    if (out) *out = NULL;
+    return false;
+}
diff --git a/ast.h b/ast.h
index 1437de84fb155c0a63f8190c5087cce55ab9621d..c9311eeb7e4e6b96eaf23785029a509ec108f4ed 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -37,6 +37,7 @@ typedef struct ast_block_s      ast_block;
 typedef struct ast_binary_s     ast_binary;
 typedef struct ast_store_s      ast_store;
 typedef struct ast_entfield_s   ast_entfield;
+typedef struct ast_ifthen_s     ast_ifthen;
 
 /* Node interface with common components
  */
@@ -180,6 +181,30 @@ void ast_store_delete(ast_store*);
 
 bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
 
+/* If
+ *
+ * A general 'if then else' statement, either side can be NULL and will
+ * thus be omitted. It is an error for *both* cases to be NULL at once.
+ *
+ * During its 'codegen' it'll be changing the ast_function's block.
+ *
+ * An if is also an "expression". Its codegen will put NULL into the
+ * output field though. For ternary expressions an ast_ternary will be
+ * added.
+ */
+struct ast_ifthen_s
+{
+    ast_expression_common expression;
+    ast_expression *cond;
+    /* It's all just 'expressions', since an ast_block is one too. */
+    ast_expression *on_true;
+    ast_expression *on_false;
+};
+ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
+void ast_ifthen_delete(ast_ifthen*);
+
+bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
+
 /* Blocks
  *
  */