Implement Array__concat and ++=

This commit is contained in:
Bruce Hill 2024-02-23 13:24:06 -05:00
parent 9aec32149f
commit 9f56266ae4
3 changed files with 37 additions and 3 deletions

View File

@ -228,6 +228,33 @@ public array_t Array__slice(array_t *array, int64_t first, int64_t stride, int64
};
}
array_t Array__concat(array_t *x, array_t *y, const TypeInfo *type)
{
int64_t item_size = get_item_size(type);
void *data = x->atomic ? GC_MALLOC_ATOMIC(item_size*(x->length + y->length)) : GC_MALLOC(item_size*(x->length + y->length));
if (x->stride == item_size) {
memcpy(data, x->data, item_size*x->length);
} else {
for (int64_t i = 0; i < x->length; i++)
memcpy(data + i*item_size, x->data + i*item_size, item_size);
}
if (y->stride == item_size) {
memcpy(data + item_size*x->length, y->data, item_size*y->length);
} else {
for (int64_t i = 0; i < x->length; i++)
memcpy(data + (x->length + i)*item_size, y->data + i*item_size, item_size);
}
return (array_t){
.data=data,
.length=x->length + y->length,
.stride=item_size,
.copy_on_write=0,
.atomic=x->atomic,
};
}
public bool Array__contains(array_t array, void *item, const TypeInfo *type)
{
TypeInfo *item_type = type->ArrayInfo.item;

View File

@ -32,6 +32,7 @@ void Array__clear(array_t *array, const TypeInfo *type);
void Array__compact(array_t *arr, const TypeInfo *type);
bool Array__contains(array_t array, void *item, const TypeInfo *type);
array_t Array__slice(array_t *array, int64_t first, int64_t stride, int64_t length, bool readonly, const TypeInfo *type);
array_t Array__concat(array_t *x, array_t *y, const TypeInfo *type);
uint32_t Array__hash(const array_t *arr, const TypeInfo *type);
int32_t Array__compare(const array_t *x, const array_t *y, const TypeInfo *type);
bool Array__equal(const array_t *x, const array_t *y, const TypeInfo *type);

View File

@ -314,9 +314,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all("CORD_cat(", lhs, ", ", rhs, ")");
}
case ArrayType: {
return CORD_all("({ array_t $joined = ", lhs, ", $rhs = ", rhs, ";\n"
"Array__insert_all(&$joined, $rhs, 0, ", compile_type_info(env, operand_t), ");\n"
"$joined; })");
return CORD_all("Array__concat(", lhs, ", ", rhs, ", ", compile_type_info(env, operand_t), ")");
}
default:
code_err(ast, "Concatenation isn't supported for %T types", operand_t);
@ -375,6 +373,14 @@ CORD compile(env_t *env, ast_t *ast)
code_err(ast, "'or=' is not implemented for %T types", operand_t);
}
case BINOP_XOR: return CORD_asprintf("%r ^= %r;", lhs, rhs);
case BINOP_CONCAT: {
if (operand_t->tag == StringType)
return CORD_asprintf("%r = CORD_cat(%r, %r);", lhs, lhs, rhs);
else if (operand_t->tag == ArrayType)
return CORD_all(lhs, "Array__concat(", lhs, ", ", rhs, ", ", compile_type_info(env, operand_t), ")");
else
code_err(ast, "'++=' is not implemented for %T types", operand_t);
}
default: code_err(ast, "Update assignments are not implemented for this operation");
}
}