struct array *array_copy(const struct array* original) { if (original == NULL) return NULL; struct array* copy = array_new_size(original->size); // (struct array*)malloc(sizeof(struct array)); //copy->data = (void**)malloc(original->length * sizeof(void**)); memcpy(copy->data, original->data, original->length * sizeof(void*)); copy->length = original->length; copy->current = original->current; copy->size = original->size; //DEBUGPRINT("array_copy %p->%p\n", copy, copy->data); return copy; }
static inline struct variable *cfnc_insert(struct context *context) // todo: test { struct variable *args = (struct variable*)stack_pop(context->operand_stack); struct variable *self = (struct variable*)array_get(args->list.ordered, 0); struct variable *insertion = (struct variable*)array_get(args->list.ordered, 1); struct variable *start = args->list.ordered->length > 2 ? (struct variable*)array_get(args->list.ordered, 2) : NULL; null_check(self); null_check(insertion); assert_message(!start || start->type == VAR_INT, "non-integer index"); int32_t position = 0; switch (self->type) { case VAR_LST: { struct array *list = array_new_size(1); array_set(list, 0, insertion); insertion = variable_new_list(context, list); position = self->list.ordered->length; array_del(list); } break; case VAR_STR: assert_message(insertion->type == VAR_STR, "insertion doesn't match destination"); position = self->str->length; break; default: exit_message("bad insertion destination"); break; } struct variable *first = variable_part(context, variable_copy(context, self), 0, position); struct variable *second = variable_part(context, variable_copy(context, self), position, -1); struct variable *joined = variable_concatenate(context, 3, first, insertion, second); if (self->type == VAR_LST) { array_del(self->list.ordered); self->list.ordered = array_copy(joined->list.ordered); } else { byte_array_del(self->str); self->str = byte_array_copy(joined->str); } return joined; }
struct array *array_new() { struct array *a = array_new_size(0); //DEBUGPRINT("array_new %p->%p\n", a, a->data); return a; }