ast_type_adopt - ast_entfield now adopts the full type of the field

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-08-18 20:30:24 +02:00
parent 6223d78ee3
commit 20635c5ae4
2 changed files with 40 additions and 5 deletions

30
ast.c
View file

@ -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;
}

View 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() main = {
print3("should be 1: ", ftos(dot('1 1 0', '1 0 0')), "\n");
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");
};