aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-17 23:43:55 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-17 23:43:55 -0500
commitfbf39cd7e907f32824cc0bb763f38851b1726cfd (patch)
treef9ef822ad1967a9d371936586ac13c890cb2cb50 /builtins
parent2345c8c5c93b8c18866f09310f8a9d9e35962823 (diff)
Various fixes, including for Null values
Diffstat (limited to 'builtins')
-rw-r--r--builtins/array.h9
-rw-r--r--builtins/pointer.h1
2 files changed, 10 insertions, 0 deletions
diff --git a/builtins/array.h b/builtins/array.h
index ff62db33..ae36795a 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -7,6 +7,15 @@
#include "functions.h"
#include "types.h"
+// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1
+#define $index(x, i) _Generic(x, array_t: ({ __typeof(x) $obj; int64_t $offset = i; $offset += ($offset < 0) * ($obj.length + 1) - 1; assert($offset >= 0 && offset < $obj.length); $obj.data + $obj.stride * $offset;}))
+#define $safe_index(x, i) _Generic(x, array_t: ({ __typeof(x) $obj; int64_t $offset = i - 1; $obj.data + $obj.stride * $offset;}))
+#define $array(x, ...) ({ __typeof(x) $items[] = {x, __VA_ARGS__}; \
+ (array_t){.length=sizeof($items)/sizeof($items[0]), \
+ .stride=(int64_t)&$items[1] - (int64_t)&$items[0], \
+ .data=memcpy(GC_MALLOC(sizeof($items)), $items, sizeof($items)), \
+ .copy_on_write=1}; })
+
void Array__insert(array_t *arr, const void *item, int64_t index, const TypeInfo *type);
void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, const TypeInfo *type);
void Array__remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *type);
diff --git a/builtins/pointer.h b/builtins/pointer.h
index b8583b72..c3554726 100644
--- a/builtins/pointer.h
+++ b/builtins/pointer.h
@@ -10,6 +10,7 @@ int32_t Pointer__compare(const void *x, const void *y, const TypeInfo *type);
bool Pointer__equal(const void *x, const void *y, const TypeInfo *type);
uint32_t Pointer__hash(const void *x, const TypeInfo *type);
+#define $Null(t) (t*)NULL
#define POINTER_TYPE(_sigil, _pointed) (&(TypeInfo){\
.size=sizeof(void*), .align=alignof(void*), .tag=PointerInfo, .PointerInfo.sigil=_sigil, .PointerInfo.pointed=_pointed})