]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
ast_type_adopt - ast_entfield now adopts the full type of the field
authorWolfgang (Blub) Bumiller <blub@speed.at>
Sat, 18 Aug 2012 18:30:24 +0000 (20:30 +0200)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Sat, 18 Aug 2012 18:30:24 +0000 (20:30 +0200)
ast.c
data/vars.qc

diff --git a/ast.c b/ast.c
index 3d8d45feeec65dfc9515b5065a429178425d673e..659818189ea965273926338c293f8f05cbc90a87 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -117,6 +117,28 @@ ast_value* ast_value_copy(const ast_value *self)
     return cp;
 }
 
+#define ast_type_adopt(a, b) ast_type_adopt_impl((ast_expression*)(a), (ast_expression*)(b))
+static bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
+{
+    size_t i;
+    const ast_expression_common *fromex;
+    ast_expression_common *selfex;
+    self->expression.vtype = other->expression.vtype;
+    if (other->expression.next) {
+        self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
+        if (!self->expression.next)
+            return false;
+    }
+    fromex   = &other->expression;
+    selfex = &self->expression;
+    for (i = 0; i < fromex->params_count; ++i) {
+        ast_value *v = ast_value_copy(fromex->params[i]);
+        if (!v || !ast_expression_common_params_add(selfex, v))
+            return false;
+    }
+    return true;
+}
+
 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
 {
     ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
@@ -375,12 +397,14 @@ ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expressi
 
     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
 
-    self->expression.vtype = outtype->expression.vtype;
-    self->expression.next  = ast_type_copy(ctx, outtype->expression.next);
-
     self->entity = entity;
     self->field  = field;
 
+    if (!ast_type_adopt(self, outtype)) {
+        ast_entfield_delete(self);
+        return NULL;
+    }
+
     return self;
 }
 
index 7438a10155b89847a5a7ce57e815f3b000fa157c..932df97f1c6806fe68bef927c9785c02b3c352eb 100644 (file)
@@ -8,12 +8,23 @@ string(float)       ftos   = #2;
 entity()            spawn  = #3;
 void(entity)        kill   = #4;
 
+.void(string x) printit;
+
 float(vector different_name, vector b) dot;
 
 float(vector a, vector b) dot = {
     return a * b;
 };
 
+void(string x) myprintit = {
+    print3("-> ", x, "\n");
+};
+
 void() main = {
+    local entity pawn;
     print3("should be 1: ", ftos(dot('1 1 0', '1 0 0')), "\n");
+
+    pawn = spawn();
+    pawn.printit = myprintit;
+    pawn.printit("Hello");
 };