aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-23 13:29:20 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-23 13:29:20 -0500
commit87bc0cfdbddeb550b7d7959c88088ef9658a5e2d (patch)
tree1b5d83c44f43d5db1db970160682b02ee37383e4
parent9f56266ae4a2d458576821d74906ab5bc4498b5b (diff)
Fix up some ++ stuff for arrays
-rw-r--r--Makefile2
-rw-r--r--builtins/array.c24
-rw-r--r--builtins/array.h2
-rw-r--r--compile.c12
4 files changed, 22 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index 94dd0ec7..a5b37b5a 100644
--- a/Makefile
+++ b/Makefile
@@ -40,7 +40,7 @@ tags:
ctags *.[ch] **/*.[ch]
clean:
- rm -f nextlang *.o builtins/*.o
+ rm -f nextlang *.o builtins/*.o libnext.so
%.1: %.1.md
pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@
diff --git a/builtins/array.c b/builtins/array.c
index 4f9070fe..85eaeb83 100644
--- a/builtins/array.c
+++ b/builtins/array.c
@@ -228,30 +228,30 @@ 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)
+public 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);
+ 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);
+ 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);
+ 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);
+ 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,
+ .length=x.length + y.length,
.stride=item_size,
.copy_on_write=0,
- .atomic=x->atomic,
+ .atomic=x.atomic,
};
}
diff --git a/builtins/array.h b/builtins/array.h
index 5a4b29ab..0cbc12f8 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -32,7 +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);
+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);
diff --git a/compile.c b/compile.c
index 06908801..56e8b5a6 100644
--- a/compile.c
+++ b/compile.c
@@ -374,12 +374,16 @@ CORD compile(env_t *env, ast_t *ast)
}
case BINOP_XOR: return CORD_asprintf("%r ^= %r;", lhs, rhs);
case BINOP_CONCAT: {
- if (operand_t->tag == StringType)
+ 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
+ } else if (operand_t->tag == ArrayType) {
+ if (update->lhs->tag == Var)
+ return CORD_all("Array__insert_all(&", lhs, ", ", rhs, ", 0, ", compile_type_info(env, operand_t), ")");
+ else
+ 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");
}