From 4380039acc881703ef9d144bbf39d82da4beb936 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 13 Sep 2024 20:08:20 -0400 Subject: Rename builtins to use plurals when appropriate --- Makefile | 8 +- builtins/array.c | 684 ------------------------------ builtins/array.h | 103 ----- builtins/arrays.c | 684 ++++++++++++++++++++++++++++++ builtins/arrays.h | 103 +++++ builtins/bool.c | 54 --- builtins/bool.h | 22 - builtins/bools.c | 54 +++ builtins/bools.h | 22 + builtins/channel.c | 137 ------ builtins/channel.h | 28 -- builtins/channels.c | 137 ++++++ builtins/channels.h | 28 ++ builtins/functiontype.c | 2 +- builtins/integers.c | 2 +- builtins/metamethods.c | 8 +- builtins/nums.c | 2 +- builtins/optionals.c | 6 +- builtins/path.c | 481 --------------------- builtins/path.h | 50 --- builtins/paths.c | 481 +++++++++++++++++++++ builtins/paths.h | 50 +++ builtins/pattern.c | 1064 ----------------------------------------------- builtins/pattern.h | 33 -- builtins/patterns.c | 1064 +++++++++++++++++++++++++++++++++++++++++++++++ builtins/patterns.h | 33 ++ builtins/pointer.c | 84 ---- builtins/pointer.h | 19 - builtins/pointers.c | 84 ++++ builtins/pointers.h | 19 + builtins/range.c | 63 --- builtins/range.h | 10 - builtins/ranges.c | 63 +++ builtins/ranges.h | 10 + builtins/shell.c | 4 +- builtins/stdlib.c | 4 +- builtins/table.c | 636 ---------------------------- builtins/table.h | 84 ---- builtins/tables.c | 636 ++++++++++++++++++++++++++++ builtins/tables.h | 84 ++++ builtins/text.c | 6 +- builtins/thread.c | 55 --- builtins/thread.h | 20 - builtins/threads.c | 55 +++ builtins/threads.h | 20 + builtins/tomo.h | 18 +- builtins/types.c | 6 +- environment.c | 2 +- environment.h | 2 +- parse.c | 2 +- tomo.c | 2 +- types.c | 2 +- types.h | 2 +- 53 files changed, 3667 insertions(+), 3665 deletions(-) delete mode 100644 builtins/array.c delete mode 100644 builtins/array.h create mode 100644 builtins/arrays.c create mode 100644 builtins/arrays.h delete mode 100644 builtins/bool.c delete mode 100644 builtins/bool.h create mode 100644 builtins/bools.c create mode 100644 builtins/bools.h delete mode 100644 builtins/channel.c delete mode 100644 builtins/channel.h create mode 100644 builtins/channels.c create mode 100644 builtins/channels.h delete mode 100644 builtins/path.c delete mode 100644 builtins/path.h create mode 100644 builtins/paths.c create mode 100644 builtins/paths.h delete mode 100644 builtins/pattern.c delete mode 100644 builtins/pattern.h create mode 100644 builtins/patterns.c create mode 100644 builtins/patterns.h delete mode 100644 builtins/pointer.c delete mode 100644 builtins/pointer.h create mode 100644 builtins/pointers.c create mode 100644 builtins/pointers.h delete mode 100644 builtins/range.c delete mode 100644 builtins/range.h create mode 100644 builtins/ranges.c create mode 100644 builtins/ranges.h delete mode 100644 builtins/table.c delete mode 100644 builtins/table.h create mode 100644 builtins/tables.c create mode 100644 builtins/tables.h delete mode 100644 builtins/thread.c delete mode 100644 builtins/thread.h create mode 100644 builtins/threads.c create mode 100644 builtins/threads.h diff --git a/Makefile b/Makefile index 6f29a964..b82efdb8 100644 --- a/Makefile +++ b/Makefile @@ -28,10 +28,10 @@ O=-Og CFLAGS=$(CCONFIG) $(EXTRA) $(CWARN) $(G) $(O) $(OSFLAGS) CFLAGS_PLACEHOLDER="$$(echo -e '\033[2m\033[m')" LDLIBS=-lgc -lcord -lm -lunistring -lgmp -ldl -BUILTIN_OBJS=builtins/siphash.o builtins/array.o builtins/bool.o builtins/channel.o builtins/nums.o builtins/integers.o \ - builtins/pointer.o builtins/memory.o builtins/text.o builtins/thread.o builtins/c_string.o builtins/table.o \ - builtins/types.o builtins/util.o builtins/files.o builtins/range.o builtins/shell.o builtins/path.o \ - builtins/optionals.o builtins/pattern.o builtins/metamethods.o builtins/functiontype.o builtins/stdlib.o +BUILTIN_OBJS=builtins/siphash.o builtins/arrays.o builtins/bools.o builtins/channels.o builtins/nums.o builtins/integers.o \ + builtins/pointers.o builtins/memory.o builtins/text.o builtins/threads.o builtins/c_string.o builtins/tables.o \ + builtins/types.o builtins/util.o builtins/files.o builtins/ranges.o builtins/shell.o builtins/paths.o \ + builtins/optionals.o builtins/patterns.o builtins/metamethods.o builtins/functiontype.o builtins/stdlib.o TESTS=$(patsubst %.tm,%.tm.testresult,$(wildcard test/*.tm)) all: libtomo.so tomo diff --git a/builtins/array.c b/builtins/array.c deleted file mode 100644 index 35227725..00000000 --- a/builtins/array.c +++ /dev/null @@ -1,684 +0,0 @@ -// Functions that operate on arrays - -#include -#include -#include -#include - -#include "array.h" -#include "metamethods.h" -#include "optionals.h" -#include "table.h" -#include "text.h" -#include "util.h" - -// Use inline version of siphash code: -#include "siphash.h" -#include "siphash-internals.h" - -PUREFUNC static inline int64_t get_padded_item_size(const TypeInfo *info) -{ - int64_t size = info->ArrayInfo.item->size; - if (info->ArrayInfo.item->align > 1 && size % info->ArrayInfo.item->align) - size += info->ArrayInfo.item->align - (size % info->ArrayInfo.item->align); // padding - return size; -} - -// Replace the array's .data pointer with a new pointer to a copy of the -// data that is compacted and has a stride of exactly `padded_item_size` -public void Array$compact(Array_t *arr, int64_t padded_item_size) -{ - void *copy = NULL; - if (arr->length > 0) { - copy = arr->atomic ? GC_MALLOC_ATOMIC((size_t)arr->length * (size_t)padded_item_size) - : GC_MALLOC((size_t)arr->length * (size_t)padded_item_size); - if ((int64_t)arr->stride == padded_item_size) { - memcpy(copy, arr->data, (size_t)arr->length * (size_t)padded_item_size); - } else { - for (int64_t i = 0; i < arr->length; i++) - memcpy(copy + i*padded_item_size, arr->data + arr->stride*i, (size_t)padded_item_size); - } - } - *arr = (Array_t){ - .data=copy, - .length=arr->length, - .stride=padded_item_size, - .atomic=arr->atomic, - }; -} - -public void Array$insert(Array_t *arr, const void *item, Int_t int_index, int64_t padded_item_size) -{ - int64_t index = Int_to_Int64(int_index, false); - if (index <= 0) index = arr->length + index + 1; - - if (index < 1) index = 1; - else if (index > (int64_t)arr->length + 1) - fail("Invalid insertion index %ld for an array with length %ld", index, arr->length); - - if (!arr->data) { - arr->free = 4; - arr->data = arr->atomic ? GC_MALLOC_ATOMIC((size_t)arr->free * (size_t)padded_item_size) - : GC_MALLOC((size_t)arr->free * (size_t)padded_item_size); - arr->stride = padded_item_size; - } else if (arr->free < 1 || arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) { - arr->free = MIN(ARRAY_MAX_FREE_ENTRIES, MAX(8, arr->length/4)); - void *copy = arr->atomic ? GC_MALLOC_ATOMIC((size_t)(arr->length + arr->free) * (size_t)padded_item_size) - : GC_MALLOC((size_t)(arr->length + arr->free) * (size_t)padded_item_size); - for (int64_t i = 0; i < index-1; i++) - memcpy(copy + i*padded_item_size, arr->data + arr->stride*i, (size_t)padded_item_size); - for (int64_t i = index-1; i < (int64_t)arr->length; i++) - memcpy(copy + (i+1)*padded_item_size, arr->data + arr->stride*i, (size_t)padded_item_size); - arr->data = copy; - arr->data_refcount = 0; - arr->stride = padded_item_size; - } else { - if (index != arr->length+1) - memmove( - arr->data + index*padded_item_size, - arr->data + (index-1)*padded_item_size, - (size_t)((arr->length - index + 1)*padded_item_size)); - } - assert(arr->free > 0); - --arr->free; - ++arr->length; - memcpy((void*)arr->data + (index-1)*padded_item_size, item, (size_t)padded_item_size); -} - -public void Array$insert_all(Array_t *arr, Array_t to_insert, Int_t int_index, int64_t padded_item_size) -{ - int64_t index = Int_to_Int64(int_index, false); - if (to_insert.length == 0) - return; - - if (!arr->data) { - *arr = to_insert; - ARRAY_INCREF(*arr); - return; - } - - if (index < 1) index = arr->length + index + 1; - - if (index < 1) index = 1; - else if (index > (int64_t)arr->length + 1) - fail("Invalid insertion index %ld for an array with length %ld", index, arr->length); - - if ((int64_t)arr->free >= (int64_t)to_insert.length // Adequate free space - && arr->data_refcount == 0 // Not aliased memory - && (int64_t)arr->stride == padded_item_size) { // Contiguous array - // If we can fit this within the array's preallocated free space, do that: - arr->free -= to_insert.length; - arr->length += to_insert.length; - if (index != arr->length+1) - memmove((void*)arr->data + index*padded_item_size, - arr->data + (index-1)*padded_item_size, - (size_t)((arr->length - index + to_insert.length-1)*padded_item_size)); - for (int64_t i = 0; i < to_insert.length; i++) - memcpy((void*)arr->data + (index-1 + i)*padded_item_size, - to_insert.data + i*to_insert.stride, (size_t)padded_item_size); - } else { - // Otherwise, allocate a new chunk of memory for the array and populate it: - int64_t new_len = arr->length + to_insert.length; - arr->free = MIN(ARRAY_MAX_FREE_ENTRIES, MAX(8, new_len/4)); - void *data = arr->atomic ? GC_MALLOC_ATOMIC((size_t)((new_len + arr->free) * padded_item_size)) - : GC_MALLOC((size_t)((new_len + arr->free) * padded_item_size)); - void *p = data; - - // Copy first chunk of `arr` if needed: - if (index > 1) { - if (arr->stride == padded_item_size) { - p = mempcpy(p, arr->data, (size_t)((index-1)*padded_item_size)); - } else { - for (int64_t i = 0; i < index-1; i++) - p = mempcpy(p, arr->data + arr->stride*i, (size_t)padded_item_size); - } - } - - // Copy `to_insert` - if (to_insert.stride == padded_item_size) { - p = mempcpy(p, to_insert.data, (size_t)(to_insert.length*padded_item_size)); - } else { - for (int64_t i = 0; i < index-1; i++) - p = mempcpy(p, to_insert.data + to_insert.stride*i, (size_t)padded_item_size); - } - - // Copy last chunk of `arr` if needed: - if (index < arr->length + 1) { - if (arr->stride == padded_item_size) { - p = mempcpy(p, arr->data + padded_item_size*(index-1), (size_t)((arr->length - index + 1)*padded_item_size)); - } else { - for (int64_t i = index-1; i < arr->length-1; i++) - p = mempcpy(p, arr->data + arr->stride*i, (size_t)padded_item_size); - } - } - arr->length = new_len; - arr->stride = padded_item_size; - arr->data = data; - arr->data_refcount = 0; - } -} - -public void Array$remove_at(Array_t *arr, Int_t int_index, Int_t int_count, int64_t padded_item_size) -{ - int64_t index = Int_to_Int64(int_index, false); - if (index < 1) index = arr->length + index + 1; - - int64_t count = Int_to_Int64(int_count, false); - if (index < 1 || index > (int64_t)arr->length || count < 1) return; - - if (count > arr->length - index + 1) - count = (arr->length - index) + 1; - - if (index == 1) { - arr->data += arr->stride * count; - } else if (index + count > arr->length) { - if (arr->free >= 0) - arr->free += count; - } else if (arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) { - void *copy = arr->atomic ? GC_MALLOC_ATOMIC((size_t)((arr->length-1) * padded_item_size)) - : GC_MALLOC((size_t)((arr->length-1) * padded_item_size)); - for (int64_t src = 1, dest = 1; src <= (int64_t)arr->length; src++) { - if (src < index || src >= index + count) { - memcpy(copy + (dest - 1)*padded_item_size, arr->data + arr->stride*(src - 1), (size_t)padded_item_size); - ++dest; - } - } - arr->data = copy; - arr->free = 0; - arr->data_refcount = 0; - } else { - memmove((void*)arr->data + (index-1)*padded_item_size, arr->data + (index-1 + count)*padded_item_size, - (size_t)((arr->length - index + count - 1)*padded_item_size)); - arr->free += count; - } - arr->length -= count; - if (arr->length == 0) arr->data = NULL; -} - -public void Array$remove_item(Array_t *arr, void *item, Int_t max_removals, const TypeInfo *type) -{ - int64_t padded_item_size = get_padded_item_size(type); - const Int_t ZERO = (Int_t){.small=(0<<2)|1}; - const Int_t ONE = (Int_t){.small=(1<<2)|1}; - const TypeInfo *item_type = type->ArrayInfo.item; - for (int64_t i = 0; i < arr->length; ) { - if (max_removals.small == ZERO.small) // zero - break; - - if (generic_equal(item, arr->data + i*arr->stride, item_type)) { - Array$remove_at(arr, I(i+1), ONE, padded_item_size); - max_removals = Int$minus(max_removals, ONE); - } else { - i++; - } - } -} - -public Int_t Array$find(Array_t arr, void *item, const TypeInfo *type) -{ - const TypeInfo *item_type = type->ArrayInfo.item; - for (int64_t i = 0; i < arr.length; i++) { - if (generic_equal(item, arr.data + i*arr.stride, item_type)) - return I(i+1); - } - return NULL_INT; -} - -public Int_t Array$first(Array_t arr, Closure_t predicate) -{ - bool (*is_good)(void*, void*) = (void*)predicate.fn; - for (int64_t i = 0; i < arr.length; i++) { - if (is_good(arr.data + i*arr.stride, predicate.userdata)) - return I(i+1); - } - return NULL_INT; -} - -public void Array$sort(Array_t *arr, Closure_t comparison, int64_t padded_item_size) -{ - if (arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) - Array$compact(arr, padded_item_size); - - qsort_r(arr->data, (size_t)arr->length, (size_t)padded_item_size, comparison.fn, comparison.userdata); -} - -public Array_t Array$sorted(Array_t arr, Closure_t comparison, int64_t padded_item_size) -{ - Array$compact(&arr, padded_item_size); - qsort_r(arr.data, (size_t)arr.length, (size_t)padded_item_size, comparison.fn, comparison.userdata); - return arr; -} - -#pragma GCC diagnostic ignored "-Wstack-protector" -public void Array$shuffle(Array_t *arr, int64_t padded_item_size) -{ - if (arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) - Array$compact(arr, padded_item_size); - - char tmp[padded_item_size]; - for (int64_t i = arr->length-1; i > 1; i--) { - int64_t j = arc4random_uniform(i+1); - memcpy(tmp, arr->data + i*padded_item_size, (size_t)padded_item_size); - memcpy((void*)arr->data + i*padded_item_size, arr->data + j*padded_item_size, (size_t)padded_item_size); - memcpy((void*)arr->data + j*padded_item_size, tmp, (size_t)padded_item_size); - } -} - -public Array_t Array$shuffled(Array_t arr, int64_t padded_item_size) -{ - Array$compact(&arr, padded_item_size); - Array$shuffle(&arr, padded_item_size); - return arr; -} - -public void *Array$random(Array_t arr) -{ - if (arr.length == 0) - return NULL; // fail("Cannot get a random item from an empty array!"); - int64_t index = arc4random_uniform(arr.length); - return arr.data + arr.stride*index; -} - -public Table_t Array$counts(Array_t arr, const TypeInfo *type) -{ - Table_t counts = {}; - const TypeInfo count_type = {.size=sizeof(Table_t), .align=__alignof__(Table_t), - .tag=TableInfo, .TableInfo.key=type->ArrayInfo.item, .TableInfo.value=&Int$info}; - for (int64_t i = 0; i < arr.length; i++) { - void *key = arr.data + i*arr.stride; - int64_t *count = Table$get(counts, key, &count_type); - int64_t val = count ? *count + 1 : 1; - Table$set(&counts, key, &val, &count_type); - } - return counts; -} - -public Array_t Array$sample(Array_t arr, Int_t int_n, Array_t weights, int64_t padded_item_size) -{ - int64_t n = Int_to_Int64(int_n, false); - if (arr.length == 0 || n <= 0) - return (Array_t){}; - - Array_t selected = { - .data=arr.atomic ? GC_MALLOC_ATOMIC((size_t)(n * padded_item_size)) : GC_MALLOC((size_t)(n * padded_item_size)), - .length=n, - .stride=padded_item_size, .atomic=arr.atomic}; - - double total = 0.0; - for (int64_t i = 0; i < weights.length && i < arr.length; i++) { - double weight = *(double*)(weights.data + weights.stride*i); - if (isinf(weight)) - fail("Infinite weight!"); - else if (isnan(weight)) - fail("NaN weight!"); - else if (weight < 0.0) - fail("Negative weight!"); - else - total += weight; - } - - if (isinf(total)) - fail("Sample weights have overflowed to infinity"); - - if (total == 0.0) { - for (int64_t i = 0; i < n; i++) { - int64_t index = arc4random_uniform(arr.length); - memcpy(selected.data + i*padded_item_size, arr.data + arr.stride*index, (size_t)padded_item_size); - } - } else { - double inverse_average = (double)arr.length / total; - - struct { - int64_t alias; - double odds; - } aliases[arr.length] = {}; - - for (int64_t i = 0; i < arr.length; i++) { - double weight = i >= weights.length ? 0.0 : *(double*)(weights.data + weights.stride*i); - aliases[i].odds = weight * inverse_average; - aliases[i].alias = -1; - } - - int64_t small = 0; - for (int64_t big = 0; big < arr.length; big++) { - while (aliases[big].odds >= 1.0) { - while (small < arr.length && (aliases[small].odds >= 1.0 || aliases[small].alias != -1)) - ++small; - - if (small >= arr.length) { - aliases[big].odds = 1.0; - aliases[big].alias = big; - break; - } - - aliases[small].alias = big; - aliases[big].odds = (aliases[small].odds + aliases[big].odds) - 1.0; - } - if (big < small) small = big; - } - - for (int64_t i = small; i < arr.length; i++) - if (aliases[i].alias == -1) - aliases[i].alias = i; - - for (int64_t i = 0; i < n; i++) { - double r = drand48() * arr.length; - int64_t index = (int64_t)r; - if ((r - (double)index) > aliases[index].odds) - index = aliases[index].alias; - memcpy(selected.data + i*selected.stride, arr.data + index*arr.stride, (size_t)padded_item_size); - } - } - return selected; -} - -public Array_t Array$from(Array_t array, Int_t int_first) -{ - int64_t first = Int_to_Int64(int_first, false); - if (first < 0) - first = array.length + first + 1; - - if (first < 1 || first > array.length) - return (Array_t){.atomic=array.atomic}; - - return (Array_t){ - .atomic=array.atomic, - .data=array.data + array.stride*(first-1), - .length=array.length - first + 1, - .stride=array.stride, - .data_refcount=array.data_refcount, - }; -} - -public Array_t Array$to(Array_t array, Int_t int_last) -{ - int64_t last = Int_to_Int64(int_last, false); - if (last < 0) - last = array.length + last + 1; - - if (last > array.length) - last = array.length; - - if (last == 0) - return (Array_t){.atomic=array.atomic}; - - return (Array_t){ - .atomic=array.atomic, - .data=array.data, - .length=last, - .stride=array.stride, - .data_refcount=array.data_refcount, - }; -} - -public Array_t Array$by(Array_t array, Int_t int_stride, int64_t padded_item_size) -{ - int64_t stride = Int_to_Int64(int_stride, false); - // In the unlikely event that the stride value would be too large to fit in - // a 15-bit integer, fall back to creating a copy of the array: - if (__builtin_expect(array.stride*stride < ARRAY_MIN_STRIDE || array.stride*stride > ARRAY_MAX_STRIDE, 0)) { - void *copy = NULL; - int64_t len = (stride < 0 ? array.length / -stride : array.length / stride) + ((array.length % stride) != 0); - if (len > 0) { - copy = array.atomic ? GC_MALLOC_ATOMIC((size_t)(len * padded_item_size)) : GC_MALLOC((size_t)(len * padded_item_size)); - void *start = (stride < 0 ? array.data + (array.stride * (array.length - 1)) : array.data); - for (int64_t i = 0; i < len; i++) - memcpy(copy + i*padded_item_size, start + array.stride*stride*i, (size_t)padded_item_size); - } - return (Array_t){ - .data=copy, - .length=len, - .stride=padded_item_size, - .atomic=array.atomic, - }; - } - - if (stride == 0) - return (Array_t){.atomic=array.atomic}; - - return (Array_t){ - .atomic=array.atomic, - .data=(stride < 0 ? array.data + (array.stride * (array.length - 1)) : array.data), - .length=(stride < 0 ? array.length / -stride : array.length / stride) + ((array.length % stride) != 0), - .stride=array.stride * stride, - .data_refcount=array.data_refcount, - }; -} - -public Array_t Array$reversed(Array_t array, int64_t padded_item_size) -{ - // Just in case negating the stride gives a value that doesn't fit into a - // 15-bit integer, fall back to Array$by()'s more general method of copying - // the array. This should only happen if array.stride is MIN_STRIDE to - // begin with (very unlikely). - if (__builtin_expect(-array.stride < ARRAY_MIN_STRIDE || -array.stride > ARRAY_MAX_STRIDE, 0)) - return Array$by(array, I(-1), padded_item_size); - - Array_t reversed = array; - reversed.stride = -array.stride; - reversed.data = array.data + (array.length-1)*array.stride; - return reversed; -} - -public Array_t Array$concat(Array_t x, Array_t y, int64_t padded_item_size) -{ - void *data = x.atomic ? GC_MALLOC_ATOMIC((size_t)(padded_item_size*(x.length + y.length))) - : GC_MALLOC((size_t)(padded_item_size*(x.length + y.length))); - if (x.stride == padded_item_size) { - memcpy(data, x.data, (size_t)(padded_item_size*x.length)); - } else { - for (int64_t i = 0; i < x.length; i++) - memcpy(data + i*padded_item_size, x.data + i*padded_item_size, (size_t)padded_item_size); - } - - if (y.stride == padded_item_size) { - memcpy(data + padded_item_size*x.length, y.data, (size_t)(padded_item_size*y.length)); - } else { - for (int64_t i = 0; i < x.length; i++) - memcpy(data + (x.length + i)*padded_item_size, y.data + i*padded_item_size, (size_t)padded_item_size); - } - - return (Array_t){ - .data=data, - .length=x.length + y.length, - .stride=padded_item_size, - .atomic=x.atomic, - }; -} - -public bool Array$has(Array_t array, void *item, const TypeInfo *type) -{ - const TypeInfo *item_type = type->ArrayInfo.item; - for (int64_t i = 0; i < array.length; i++) { - if (generic_equal(array.data + i*array.stride, item, item_type)) - return true; - } - return false; -} - -public void Array$clear(Array_t *array) -{ - *array = (Array_t){.data=0, .length=0}; -} - -public int32_t Array$compare(const Array_t *x, const Array_t *y, const TypeInfo *type) -{ - // Early out for arrays with the same data, e.g. two copies of the same array: - if (x->data == y->data && x->stride == y->stride) - return (x->length > y->length) - (x->length < y->length); - - const TypeInfo *item = type->ArrayInfo.item; - if (item->tag == PointerInfo || (item->tag == CustomInfo && item->CustomInfo.compare == NULL)) { // data comparison - int64_t item_padded_size = type->ArrayInfo.item->size; - if (type->ArrayInfo.item->align > 1 && item_padded_size % type->ArrayInfo.item->align) - item_padded_size += type->ArrayInfo.item->align - (item_padded_size % type->ArrayInfo.item->align); // padding - - if ((int64_t)x->stride == item_padded_size && (int64_t)y->stride == item_padded_size && item->size == item_padded_size) { - int32_t cmp = (int32_t)memcmp(x->data, y->data, (size_t)(MIN(x->length, y->length)*item_padded_size)); - if (cmp != 0) return cmp; - } else { - for (int32_t i = 0, len = MIN(x->length, y->length); i < len; i++) { - int32_t cmp = (int32_t)memcmp(x->data+ x->stride*i, y->data + y->stride*i, (size_t)(item->size)); - if (cmp != 0) return cmp; - } - } - } else { - for (int32_t i = 0, len = MIN(x->length, y->length); i < len; i++) { - int32_t cmp = generic_compare(x->data + x->stride*i, y->data + y->stride*i, item); - if (cmp != 0) return cmp; - } - } - return (x->length > y->length) - (x->length < y->length); -} - -public bool Array$equal(const Array_t *x, const Array_t *y, const TypeInfo *type) -{ - return x == y || (x->length == y->length && Array$compare(x, y, type) == 0); -} - -public Text_t Array$as_text(const Array_t *arr, bool colorize, const TypeInfo *type) -{ - if (!arr) - return Text$concat(Text("["), generic_as_text(NULL, false, type->ArrayInfo.item), Text("]")); - - const TypeInfo *item_type = type->ArrayInfo.item; - Text_t text = Text("["); - for (int64_t i = 0; i < arr->length; i++) { - if (i > 0) - text = Text$concat(text, Text(", ")); - Text_t item_text = generic_as_text(arr->data + i*arr->stride, colorize, item_type); - text = Text$concat(text, item_text); - } - text = Text$concat(text, Text("]")); - return text; -} - -public uint64_t Array$hash(const Array_t *arr, const TypeInfo *type) -{ - const TypeInfo *item = type->ArrayInfo.item; - siphash sh; - siphashinit(&sh, sizeof(uint64_t[arr->length])); - if (item->tag == PointerInfo || (item->tag == CustomInfo && item->CustomInfo.hash == NULL && item->size == sizeof(void*))) { // Raw data hash - for (int64_t i = 0; i < arr->length; i++) - siphashadd64bits(&sh, (uint64_t)(arr->data + i*arr->stride)); - } else { - for (int64_t i = 0; i < arr->length; i++) { - uint64_t item_hash = generic_hash(arr->data + i*arr->stride, item); - siphashadd64bits(&sh, item_hash); - } - } - return siphashfinish_last_part(&sh, 0); -} - -#pragma GCC diagnostic ignored "-Wstack-protector" -static void siftdown(Array_t *heap, int64_t startpos, int64_t pos, Closure_t comparison, int64_t padded_item_size) -{ - assert(pos > 0 && pos < heap->length); - char newitem[padded_item_size]; - memcpy(newitem, heap->data + heap->stride*pos, (size_t)(padded_item_size)); - while (pos > startpos) { - int64_t parentpos = (pos - 1) >> 1; - typedef int32_t (*cmp_fn_t)(void*, void*, void*); - int32_t cmp = ((cmp_fn_t)comparison.fn)(newitem, heap->data + heap->stride*parentpos, comparison.userdata); - if (cmp >= 0) - break; - - memcpy(heap->data + heap->stride*pos, heap->data + heap->stride*parentpos, (size_t)(padded_item_size)); - pos = parentpos; - } - memcpy(heap->data + heap->stride*pos, newitem, (size_t)(padded_item_size)); -} - -static void siftup(Array_t *heap, int64_t pos, Closure_t comparison, int64_t padded_item_size) -{ - int64_t endpos = heap->length; - int64_t startpos = pos; - assert(pos < endpos); - - char old_top[padded_item_size]; - memcpy(old_top, heap->data + heap->stride*pos, (size_t)(padded_item_size)); - // Bubble up the smallest leaf node - int64_t limit = endpos >> 1; - while (pos < limit) { - int64_t childpos = 2*pos + 1; // Smaller of the two child nodes - if (childpos + 1 < endpos) { - typedef int32_t (*cmp_fn_t)(void*, void*, void*); - int32_t cmp = ((cmp_fn_t)comparison.fn)( - heap->data + heap->stride*childpos, - heap->data + heap->stride*(childpos + 1), - comparison.userdata); - childpos += (cmp >= 0); - } - - // Move the child node up: - memcpy(heap->data + heap->stride*pos, heap->data + heap->stride*childpos, (size_t)(padded_item_size)); - pos = childpos; - } - memcpy(heap->data + heap->stride*pos, old_top, (size_t)(padded_item_size)); - // Shift the node's parents down: - siftdown(heap, startpos, pos, comparison, padded_item_size); -} - -public void Array$heap_push(Array_t *heap, const void *item, Closure_t comparison, int64_t padded_item_size) -{ - Array$insert(heap, item, I(0), padded_item_size); - - if (heap->length > 1) { - if (heap->data_refcount != 0) - Array$compact(heap, padded_item_size); - siftdown(heap, 0, heap->length-1, comparison, padded_item_size); - } -} - -public void Array$heap_pop(Array_t *heap, Closure_t comparison, int64_t padded_item_size) -{ - if (heap->length == 0) - fail("Attempt to pop from an empty array"); - - if (heap->length == 1) { - *heap = (Array_t){}; - } else if (heap->length == 2) { - heap->data += heap->stride; - --heap->length; - } else { - if (heap->data_refcount != 0) - Array$compact(heap, padded_item_size); - memcpy(heap->data, heap->data + heap->stride*(heap->length-1), (size_t)(padded_item_size)); - --heap->length; - siftup(heap, 0, comparison, padded_item_size); - } -} - -public void Array$heapify(Array_t *heap, Closure_t comparison, int64_t padded_item_size) -{ - if (heap->data_refcount != 0) - Array$compact(heap, padded_item_size); - - // It's necessary to bump the refcount because the user's comparison - // function could do stuff that modifies the heap's data. - ARRAY_INCREF(*heap); - int64_t i, n = heap->length; - for (i = (n >> 1) - 1 ; i >= 0 ; i--) - siftup(heap, i, comparison, padded_item_size); - ARRAY_DECREF(*heap); -} - -public Int_t Array$binary_search(Array_t array, void *target, Closure_t comparison) -{ - typedef int32_t (*cmp_fn_t)(void*, void*, void*); - int64_t lo = 0, hi = array.length-1; - while (lo <= hi) { - int64_t mid = (lo + hi) / 2; - int32_t cmp = ((cmp_fn_t)comparison.fn)( - array.data + array.stride*mid, target, comparison.userdata); - if (cmp == 0) - return I(mid+1); - else if (cmp < 0) - lo = mid + 1; - else if (cmp > 0) - hi = mid - 1; - } - return I(lo+1); // Return the index where the target would be inserted -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/array.h b/builtins/array.h deleted file mode 100644 index 1e945e5e..00000000 --- a/builtins/array.h +++ /dev/null @@ -1,103 +0,0 @@ -#pragma once - -// Functions that operate on arrays - -#include - -#include "datatypes.h" -#include "integers.h" -#include "types.h" -#include "util.h" - -// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1 -#define Array_get(item_type, arr_expr, index_expr, start, end) *({ \ - const Array_t arr = arr_expr; int64_t index = index_expr; \ - int64_t off = index + (index < 0) * (arr.length + 1) - 1; \ - if (__builtin_expect(off < 0 || off >= arr.length, 0)) \ - fail_source(__SOURCE_FILE__, start, end, "Invalid array index: %s (array has length %ld)\n", Text$as_c_string(Int64$as_text(&index, no, NULL)), arr.length); \ - (item_type*)(arr.data + arr.stride * off);}) -#define Array_get_unchecked(type, x, i) *({ const Array_t arr = x; int64_t index = i; \ - int64_t off = index + (index < 0) * (arr.length + 1) - 1; \ - (type*)(arr.data + arr.stride * off);}) -#define Array_lvalue(item_type, arr_expr, index_expr, padded_item_size, start, end) *({ \ - Array_t *arr = arr_expr; int64_t index = index_expr; \ - int64_t off = index + (index < 0) * (arr->length + 1) - 1; \ - if (__builtin_expect(off < 0 || off >= arr->length, 0)) \ - fail_source(__SOURCE_FILE__, start, end, "Invalid array index: %s (array has length %ld)\n", Text$as_c_string(Int64$as_text(&index, no, NULL)), arr->length); \ - if (arr->data_refcount > 0) \ - Array$compact(arr, padded_item_size); \ - (item_type*)(arr->data + arr->stride * off); }) -#define Array_lvalue_unchecked(item_type, arr_expr, index_expr, padded_item_size) *({ \ - Array_t *arr = arr_expr; int64_t index = index_expr; \ - int64_t off = index + (index < 0) * (arr->length + 1) - 1; \ - if (arr->data_refcount > 0) \ - Array$compact(arr, padded_item_size); \ - (item_type*)(arr->data + arr->stride * off); }) -#define Array_set(item_type, arr, index, value, padded_item_size, start, end) \ - Array_lvalue(item_type, arr_expr, index, padded_item_size, start, end) = value -#define is_atomic(x) _Generic(x, bool: true, int8_t: true, int16_t: true, int32_t: true, int64_t: true, float: true, double: true, default: false) -#define TypedArray(t, ...) ({ t items[] = {__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)), \ - .atomic=0, \ - .data_refcount=0}; }) -#define TypedArrayN(t, N, ...) ({ t items[N] = {__VA_ARGS__}; \ - (Array_t){.length=N, \ - .stride=(int64_t)&items[1] - (int64_t)&items[0], \ - .data=memcpy(GC_MALLOC(sizeof(items)), items, sizeof(items)), \ - .atomic=0, \ - .data_refcount=0}; }) -#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(is_atomic(x) ? GC_MALLOC_ATOMIC(sizeof(items)) : GC_MALLOC(sizeof(items)), items, sizeof(items)), \ - .atomic=is_atomic(x), \ - .data_refcount=0}; }) -// Array refcounts use a saturating add, where once it's at the max value, it stays there. -#define ARRAY_INCREF(arr) (arr).data_refcount += ((arr).data_refcount < ARRAY_MAX_DATA_REFCOUNT) -#define ARRAY_DECREF(arr) (arr).data_refcount -= ((arr).data_refcount < ARRAY_MAX_DATA_REFCOUNT) -#define ARRAY_COPY(arr) ({ ARRAY_INCREF(arr); arr; }) - -#define Array$insert_value(arr, item_expr, index, padded_item_size) ({ __typeof(item_expr) item = item_expr; Array$insert(arr, &item, index, padded_item_size); }) -void Array$insert(Array_t *arr, const void *item, Int_t index, int64_t padded_item_size); -void Array$insert_all(Array_t *arr, Array_t to_insert, Int_t index, int64_t padded_item_size); -void Array$remove_at(Array_t *arr, Int_t index, Int_t count, int64_t padded_item_size); -void Array$remove_item(Array_t *arr, void *item, Int_t max_removals, const TypeInfo *type); -#define Array$remove_item_value(arr, item_expr, max, type) ({ __typeof(item_expr) item = item_expr; Array$remove_item(arr, &item, max, type); }) -Int_t Array$find(Array_t arr, void *item, const TypeInfo *type); -#define Array$find_value(arr, item_expr, type) ({ __typeof(item_expr) item = item_expr; Array$find(arr, &item, type); }) -Int_t Array$first(Array_t arr, Closure_t predicate); -void Array$sort(Array_t *arr, Closure_t comparison, int64_t padded_item_size); -Array_t Array$sorted(Array_t arr, Closure_t comparison, int64_t padded_item_size); -void Array$shuffle(Array_t *arr, int64_t padded_item_size); -Array_t Array$shuffled(Array_t arr, int64_t padded_item_size); -void *Array$random(Array_t arr); -#define Array$random_value(arr, t) ({ Array_t _arr = arr; if (_arr.length == 0) fail("Cannot get a random value from an empty array!"); *(t*)Array$random(_arr); }) -Array_t Array$sample(Array_t arr, Int_t n, Array_t weights, int64_t padded_item_size); -Table_t Array$counts(Array_t arr, const TypeInfo *type); -void Array$clear(Array_t *array); -void Array$compact(Array_t *arr, int64_t padded_item_size); -PUREFUNC bool Array$has(Array_t array, void *item, const TypeInfo *type); -#define Array$has_value(arr, item_expr, type) ({ __typeof(item_expr) item = item_expr; Array$has(arr, &item, type); }) -PUREFUNC Array_t Array$from(Array_t array, Int_t first); -PUREFUNC Array_t Array$to(Array_t array, Int_t last); -PUREFUNC Array_t Array$by(Array_t array, Int_t stride, int64_t padded_item_size); -PUREFUNC Array_t Array$reversed(Array_t array, int64_t padded_item_size); -Array_t Array$concat(Array_t x, Array_t y, int64_t padded_item_size); -PUREFUNC uint64_t Array$hash(const Array_t *arr, const TypeInfo *type); -PUREFUNC int32_t Array$compare(const Array_t *x, const Array_t *y, const TypeInfo *type); -PUREFUNC bool Array$equal(const Array_t *x, const Array_t *y, const TypeInfo *type); -Text_t Array$as_text(const Array_t *arr, bool colorize, const TypeInfo *type); -void Array$heapify(Array_t *heap, Closure_t comparison, int64_t padded_item_size); -void Array$heap_push(Array_t *heap, const void *item, Closure_t comparison, int64_t padded_item_size); -#define Array$heap_push_value(heap, _value, comparison, padded_item_size) ({ __typeof(_value) value = _value; Array$heap_push(heap, &value, comparison, padded_item_size); }) -void Array$heap_pop(Array_t *heap, Closure_t comparison, int64_t padded_item_size); -#define Array$heap_pop_value(heap, comparison, padded_item_size, type) \ - ({ Array_t *_heap = heap; if (_heap->length == 0) fail("Attempt to pop from an empty array"); \ - type value = *(type*)_heap->data; Array$heap_pop(_heap, comparison, padded_item_size); value; }) -Int_t Array$binary_search(Array_t array, void *target, Closure_t comparison); -#define Array$binary_search_value(array, target, comparison) \ - ({ __typeof(target) _target = target; Array$binary_search(array, &_target, comparison); }) - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/arrays.c b/builtins/arrays.c new file mode 100644 index 00000000..58a33754 --- /dev/null +++ b/builtins/arrays.c @@ -0,0 +1,684 @@ +// Functions that operate on arrays + +#include +#include +#include +#include + +#include "arrays.h" +#include "metamethods.h" +#include "optionals.h" +#include "tables.h" +#include "text.h" +#include "util.h" + +// Use inline version of siphash code: +#include "siphash.h" +#include "siphash-internals.h" + +PUREFUNC static inline int64_t get_padded_item_size(const TypeInfo *info) +{ + int64_t size = info->ArrayInfo.item->size; + if (info->ArrayInfo.item->align > 1 && size % info->ArrayInfo.item->align) + size += info->ArrayInfo.item->align - (size % info->ArrayInfo.item->align); // padding + return size; +} + +// Replace the array's .data pointer with a new pointer to a copy of the +// data that is compacted and has a stride of exactly `padded_item_size` +public void Array$compact(Array_t *arr, int64_t padded_item_size) +{ + void *copy = NULL; + if (arr->length > 0) { + copy = arr->atomic ? GC_MALLOC_ATOMIC((size_t)arr->length * (size_t)padded_item_size) + : GC_MALLOC((size_t)arr->length * (size_t)padded_item_size); + if ((int64_t)arr->stride == padded_item_size) { + memcpy(copy, arr->data, (size_t)arr->length * (size_t)padded_item_size); + } else { + for (int64_t i = 0; i < arr->length; i++) + memcpy(copy + i*padded_item_size, arr->data + arr->stride*i, (size_t)padded_item_size); + } + } + *arr = (Array_t){ + .data=copy, + .length=arr->length, + .stride=padded_item_size, + .atomic=arr->atomic, + }; +} + +public void Array$insert(Array_t *arr, const void *item, Int_t int_index, int64_t padded_item_size) +{ + int64_t index = Int_to_Int64(int_index, false); + if (index <= 0) index = arr->length + index + 1; + + if (index < 1) index = 1; + else if (index > (int64_t)arr->length + 1) + fail("Invalid insertion index %ld for an array with length %ld", index, arr->length); + + if (!arr->data) { + arr->free = 4; + arr->data = arr->atomic ? GC_MALLOC_ATOMIC((size_t)arr->free * (size_t)padded_item_size) + : GC_MALLOC((size_t)arr->free * (size_t)padded_item_size); + arr->stride = padded_item_size; + } else if (arr->free < 1 || arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) { + arr->free = MIN(ARRAY_MAX_FREE_ENTRIES, MAX(8, arr->length/4)); + void *copy = arr->atomic ? GC_MALLOC_ATOMIC((size_t)(arr->length + arr->free) * (size_t)padded_item_size) + : GC_MALLOC((size_t)(arr->length + arr->free) * (size_t)padded_item_size); + for (int64_t i = 0; i < index-1; i++) + memcpy(copy + i*padded_item_size, arr->data + arr->stride*i, (size_t)padded_item_size); + for (int64_t i = index-1; i < (int64_t)arr->length; i++) + memcpy(copy + (i+1)*padded_item_size, arr->data + arr->stride*i, (size_t)padded_item_size); + arr->data = copy; + arr->data_refcount = 0; + arr->stride = padded_item_size; + } else { + if (index != arr->length+1) + memmove( + arr->data + index*padded_item_size, + arr->data + (index-1)*padded_item_size, + (size_t)((arr->length - index + 1)*padded_item_size)); + } + assert(arr->free > 0); + --arr->free; + ++arr->length; + memcpy((void*)arr->data + (index-1)*padded_item_size, item, (size_t)padded_item_size); +} + +public void Array$insert_all(Array_t *arr, Array_t to_insert, Int_t int_index, int64_t padded_item_size) +{ + int64_t index = Int_to_Int64(int_index, false); + if (to_insert.length == 0) + return; + + if (!arr->data) { + *arr = to_insert; + ARRAY_INCREF(*arr); + return; + } + + if (index < 1) index = arr->length + index + 1; + + if (index < 1) index = 1; + else if (index > (int64_t)arr->length + 1) + fail("Invalid insertion index %ld for an array with length %ld", index, arr->length); + + if ((int64_t)arr->free >= (int64_t)to_insert.length // Adequate free space + && arr->data_refcount == 0 // Not aliased memory + && (int64_t)arr->stride == padded_item_size) { // Contiguous array + // If we can fit this within the array's preallocated free space, do that: + arr->free -= to_insert.length; + arr->length += to_insert.length; + if (index != arr->length+1) + memmove((void*)arr->data + index*padded_item_size, + arr->data + (index-1)*padded_item_size, + (size_t)((arr->length - index + to_insert.length-1)*padded_item_size)); + for (int64_t i = 0; i < to_insert.length; i++) + memcpy((void*)arr->data + (index-1 + i)*padded_item_size, + to_insert.data + i*to_insert.stride, (size_t)padded_item_size); + } else { + // Otherwise, allocate a new chunk of memory for the array and populate it: + int64_t new_len = arr->length + to_insert.length; + arr->free = MIN(ARRAY_MAX_FREE_ENTRIES, MAX(8, new_len/4)); + void *data = arr->atomic ? GC_MALLOC_ATOMIC((size_t)((new_len + arr->free) * padded_item_size)) + : GC_MALLOC((size_t)((new_len + arr->free) * padded_item_size)); + void *p = data; + + // Copy first chunk of `arr` if needed: + if (index > 1) { + if (arr->stride == padded_item_size) { + p = mempcpy(p, arr->data, (size_t)((index-1)*padded_item_size)); + } else { + for (int64_t i = 0; i < index-1; i++) + p = mempcpy(p, arr->data + arr->stride*i, (size_t)padded_item_size); + } + } + + // Copy `to_insert` + if (to_insert.stride == padded_item_size) { + p = mempcpy(p, to_insert.data, (size_t)(to_insert.length*padded_item_size)); + } else { + for (int64_t i = 0; i < index-1; i++) + p = mempcpy(p, to_insert.data + to_insert.stride*i, (size_t)padded_item_size); + } + + // Copy last chunk of `arr` if needed: + if (index < arr->length + 1) { + if (arr->stride == padded_item_size) { + p = mempcpy(p, arr->data + padded_item_size*(index-1), (size_t)((arr->length - index + 1)*padded_item_size)); + } else { + for (int64_t i = index-1; i < arr->length-1; i++) + p = mempcpy(p, arr->data + arr->stride*i, (size_t)padded_item_size); + } + } + arr->length = new_len; + arr->stride = padded_item_size; + arr->data = data; + arr->data_refcount = 0; + } +} + +public void Array$remove_at(Array_t *arr, Int_t int_index, Int_t int_count, int64_t padded_item_size) +{ + int64_t index = Int_to_Int64(int_index, false); + if (index < 1) index = arr->length + index + 1; + + int64_t count = Int_to_Int64(int_count, false); + if (index < 1 || index > (int64_t)arr->length || count < 1) return; + + if (count > arr->length - index + 1) + count = (arr->length - index) + 1; + + if (index == 1) { + arr->data += arr->stride * count; + } else if (index + count > arr->length) { + if (arr->free >= 0) + arr->free += count; + } else if (arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) { + void *copy = arr->atomic ? GC_MALLOC_ATOMIC((size_t)((arr->length-1) * padded_item_size)) + : GC_MALLOC((size_t)((arr->length-1) * padded_item_size)); + for (int64_t src = 1, dest = 1; src <= (int64_t)arr->length; src++) { + if (src < index || src >= index + count) { + memcpy(copy + (dest - 1)*padded_item_size, arr->data + arr->stride*(src - 1), (size_t)padded_item_size); + ++dest; + } + } + arr->data = copy; + arr->free = 0; + arr->data_refcount = 0; + } else { + memmove((void*)arr->data + (index-1)*padded_item_size, arr->data + (index-1 + count)*padded_item_size, + (size_t)((arr->length - index + count - 1)*padded_item_size)); + arr->free += count; + } + arr->length -= count; + if (arr->length == 0) arr->data = NULL; +} + +public void Array$remove_item(Array_t *arr, void *item, Int_t max_removals, const TypeInfo *type) +{ + int64_t padded_item_size = get_padded_item_size(type); + const Int_t ZERO = (Int_t){.small=(0<<2)|1}; + const Int_t ONE = (Int_t){.small=(1<<2)|1}; + const TypeInfo *item_type = type->ArrayInfo.item; + for (int64_t i = 0; i < arr->length; ) { + if (max_removals.small == ZERO.small) // zero + break; + + if (generic_equal(item, arr->data + i*arr->stride, item_type)) { + Array$remove_at(arr, I(i+1), ONE, padded_item_size); + max_removals = Int$minus(max_removals, ONE); + } else { + i++; + } + } +} + +public Int_t Array$find(Array_t arr, void *item, const TypeInfo *type) +{ + const TypeInfo *item_type = type->ArrayInfo.item; + for (int64_t i = 0; i < arr.length; i++) { + if (generic_equal(item, arr.data + i*arr.stride, item_type)) + return I(i+1); + } + return NULL_INT; +} + +public Int_t Array$first(Array_t arr, Closure_t predicate) +{ + bool (*is_good)(void*, void*) = (void*)predicate.fn; + for (int64_t i = 0; i < arr.length; i++) { + if (is_good(arr.data + i*arr.stride, predicate.userdata)) + return I(i+1); + } + return NULL_INT; +} + +public void Array$sort(Array_t *arr, Closure_t comparison, int64_t padded_item_size) +{ + if (arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) + Array$compact(arr, padded_item_size); + + qsort_r(arr->data, (size_t)arr->length, (size_t)padded_item_size, comparison.fn, comparison.userdata); +} + +public Array_t Array$sorted(Array_t arr, Closure_t comparison, int64_t padded_item_size) +{ + Array$compact(&arr, padded_item_size); + qsort_r(arr.data, (size_t)arr.length, (size_t)padded_item_size, comparison.fn, comparison.userdata); + return arr; +} + +#pragma GCC diagnostic ignored "-Wstack-protector" +public void Array$shuffle(Array_t *arr, int64_t padded_item_size) +{ + if (arr->data_refcount != 0 || (int64_t)arr->stride != padded_item_size) + Array$compact(arr, padded_item_size); + + char tmp[padded_item_size]; + for (int64_t i = arr->length-1; i > 1; i--) { + int64_t j = arc4random_uniform(i+1); + memcpy(tmp, arr->data + i*padded_item_size, (size_t)padded_item_size); + memcpy((void*)arr->data + i*padded_item_size, arr->data + j*padded_item_size, (size_t)padded_item_size); + memcpy((void*)arr->data + j*padded_item_size, tmp, (size_t)padded_item_size); + } +} + +public Array_t Array$shuffled(Array_t arr, int64_t padded_item_size) +{ + Array$compact(&arr, padded_item_size); + Array$shuffle(&arr, padded_item_size); + return arr; +} + +public void *Array$random(Array_t arr) +{ + if (arr.length == 0) + return NULL; // fail("Cannot get a random item from an empty array!"); + int64_t index = arc4random_uniform(arr.length); + return arr.data + arr.stride*index; +} + +public Table_t Array$counts(Array_t arr, const TypeInfo *type) +{ + Table_t counts = {}; + const TypeInfo count_type = {.size=sizeof(Table_t), .align=__alignof__(Table_t), + .tag=TableInfo, .TableInfo.key=type->ArrayInfo.item, .TableInfo.value=&Int$info}; + for (int64_t i = 0; i < arr.length; i++) { + void *key = arr.data + i*arr.stride; + int64_t *count = Table$get(counts, key, &count_type); + int64_t val = count ? *count + 1 : 1; + Table$set(&counts, key, &val, &count_type); + } + return counts; +} + +public Array_t Array$sample(Array_t arr, Int_t int_n, Array_t weights, int64_t padded_item_size) +{ + int64_t n = Int_to_Int64(int_n, false); + if (arr.length == 0 || n <= 0) + return (Array_t){}; + + Array_t selected = { + .data=arr.atomic ? GC_MALLOC_ATOMIC((size_t)(n * padded_item_size)) : GC_MALLOC((size_t)(n * padded_item_size)), + .length=n, + .stride=padded_item_size, .atomic=arr.atomic}; + + double total = 0.0; + for (int64_t i = 0; i < weights.length && i < arr.length; i++) { + double weight = *(double*)(weights.data + weights.stride*i); + if (isinf(weight)) + fail("Infinite weight!"); + else if (isnan(weight)) + fail("NaN weight!"); + else if (weight < 0.0) + fail("Negative weight!"); + else + total += weight; + } + + if (isinf(total)) + fail("Sample weights have overflowed to infinity"); + + if (total == 0.0) { + for (int64_t i = 0; i < n; i++) { + int64_t index = arc4random_uniform(arr.length); + memcpy(selected.data + i*padded_item_size, arr.data + arr.stride*index, (size_t)padded_item_size); + } + } else { + double inverse_average = (double)arr.length / total; + + struct { + int64_t alias; + double odds; + } aliases[arr.length] = {}; + + for (int64_t i = 0; i < arr.length; i++) { + double weight = i >= weights.length ? 0.0 : *(double*)(weights.data + weights.stride*i); + aliases[i].odds = weight * inverse_average; + aliases[i].alias = -1; + } + + int64_t small = 0; + for (int64_t big = 0; big < arr.length; big++) { + while (aliases[big].odds >= 1.0) { + while (small < arr.length && (aliases[small].odds >= 1.0 || aliases[small].alias != -1)) + ++small; + + if (small >= arr.length) { + aliases[big].odds = 1.0; + aliases[big].alias = big; + break; + } + + aliases[small].alias = big; + aliases[big].odds = (aliases[small].odds + aliases[big].odds) - 1.0; + } + if (big < small) small = big; + } + + for (int64_t i = small; i < arr.length; i++) + if (aliases[i].alias == -1) + aliases[i].alias = i; + + for (int64_t i = 0; i < n; i++) { + double r = drand48() * arr.length; + int64_t index = (int64_t)r; + if ((r - (double)index) > aliases[index].odds) + index = aliases[index].alias; + memcpy(selected.data + i*selected.stride, arr.data + index*arr.stride, (size_t)padded_item_size); + } + } + return selected; +} + +public Array_t Array$from(Array_t array, Int_t int_first) +{ + int64_t first = Int_to_Int64(int_first, false); + if (first < 0) + first = array.length + first + 1; + + if (first < 1 || first > array.length) + return (Array_t){.atomic=array.atomic}; + + return (Array_t){ + .atomic=array.atomic, + .data=array.data + array.stride*(first-1), + .length=array.length - first + 1, + .stride=array.stride, + .data_refcount=array.data_refcount, + }; +} + +public Array_t Array$to(Array_t array, Int_t int_last) +{ + int64_t last = Int_to_Int64(int_last, false); + if (last < 0) + last = array.length + last + 1; + + if (last > array.length) + last = array.length; + + if (last == 0) + return (Array_t){.atomic=array.atomic}; + + return (Array_t){ + .atomic=array.atomic, + .data=array.data, + .length=last, + .stride=array.stride, + .data_refcount=array.data_refcount, + }; +} + +public Array_t Array$by(Array_t array, Int_t int_stride, int64_t padded_item_size) +{ + int64_t stride = Int_to_Int64(int_stride, false); + // In the unlikely event that the stride value would be too large to fit in + // a 15-bit integer, fall back to creating a copy of the array: + if (__builtin_expect(array.stride*stride < ARRAY_MIN_STRIDE || array.stride*stride > ARRAY_MAX_STRIDE, 0)) { + void *copy = NULL; + int64_t len = (stride < 0 ? array.length / -stride : array.length / stride) + ((array.length % stride) != 0); + if (len > 0) { + copy = array.atomic ? GC_MALLOC_ATOMIC((size_t)(len * padded_item_size)) : GC_MALLOC((size_t)(len * padded_item_size)); + void *start = (stride < 0 ? array.data + (array.stride * (array.length - 1)) : array.data); + for (int64_t i = 0; i < len; i++) + memcpy(copy + i*padded_item_size, start + array.stride*stride*i, (size_t)padded_item_size); + } + return (Array_t){ + .data=copy, + .length=len, + .stride=padded_item_size, + .atomic=array.atomic, + }; + } + + if (stride == 0) + return (Array_t){.atomic=array.atomic}; + + return (Array_t){ + .atomic=array.atomic, + .data=(stride < 0 ? array.data + (array.stride * (array.length - 1)) : array.data), + .length=(stride < 0 ? array.length / -stride : array.length / stride) + ((array.length % stride) != 0), + .stride=array.stride * stride, + .data_refcount=array.data_refcount, + }; +} + +public Array_t Array$reversed(Array_t array, int64_t padded_item_size) +{ + // Just in case negating the stride gives a value that doesn't fit into a + // 15-bit integer, fall back to Array$by()'s more general method of copying + // the array. This should only happen if array.stride is MIN_STRIDE to + // begin with (very unlikely). + if (__builtin_expect(-array.stride < ARRAY_MIN_STRIDE || -array.stride > ARRAY_MAX_STRIDE, 0)) + return Array$by(array, I(-1), padded_item_size); + + Array_t reversed = array; + reversed.stride = -array.stride; + reversed.data = array.data + (array.length-1)*array.stride; + return reversed; +} + +public Array_t Array$concat(Array_t x, Array_t y, int64_t padded_item_size) +{ + void *data = x.atomic ? GC_MALLOC_ATOMIC((size_t)(padded_item_size*(x.length + y.length))) + : GC_MALLOC((size_t)(padded_item_size*(x.length + y.length))); + if (x.stride == padded_item_size) { + memcpy(data, x.data, (size_t)(padded_item_size*x.length)); + } else { + for (int64_t i = 0; i < x.length; i++) + memcpy(data + i*padded_item_size, x.data + i*padded_item_size, (size_t)padded_item_size); + } + + if (y.stride == padded_item_size) { + memcpy(data + padded_item_size*x.length, y.data, (size_t)(padded_item_size*y.length)); + } else { + for (int64_t i = 0; i < x.length; i++) + memcpy(data + (x.length + i)*padded_item_size, y.data + i*padded_item_size, (size_t)padded_item_size); + } + + return (Array_t){ + .data=data, + .length=x.length + y.length, + .stride=padded_item_size, + .atomic=x.atomic, + }; +} + +public bool Array$has(Array_t array, void *item, const TypeInfo *type) +{ + const TypeInfo *item_type = type->ArrayInfo.item; + for (int64_t i = 0; i < array.length; i++) { + if (generic_equal(array.data + i*array.stride, item, item_type)) + return true; + } + return false; +} + +public void Array$clear(Array_t *array) +{ + *array = (Array_t){.data=0, .length=0}; +} + +public int32_t Array$compare(const Array_t *x, const Array_t *y, const TypeInfo *type) +{ + // Early out for arrays with the same data, e.g. two copies of the same array: + if (x->data == y->data && x->stride == y->stride) + return (x->length > y->length) - (x->length < y->length); + + const TypeInfo *item = type->ArrayInfo.item; + if (item->tag == PointerInfo || (item->tag == CustomInfo && item->CustomInfo.compare == NULL)) { // data comparison + int64_t item_padded_size = type->ArrayInfo.item->size; + if (type->ArrayInfo.item->align > 1 && item_padded_size % type->ArrayInfo.item->align) + item_padded_size += type->ArrayInfo.item->align - (item_padded_size % type->ArrayInfo.item->align); // padding + + if ((int64_t)x->stride == item_padded_size && (int64_t)y->stride == item_padded_size && item->size == item_padded_size) { + int32_t cmp = (int32_t)memcmp(x->data, y->data, (size_t)(MIN(x->length, y->length)*item_padded_size)); + if (cmp != 0) return cmp; + } else { + for (int32_t i = 0, len = MIN(x->length, y->length); i < len; i++) { + int32_t cmp = (int32_t)memcmp(x->data+ x->stride*i, y->data + y->stride*i, (size_t)(item->size)); + if (cmp != 0) return cmp; + } + } + } else { + for (int32_t i = 0, len = MIN(x->length, y->length); i < len; i++) { + int32_t cmp = generic_compare(x->data + x->stride*i, y->data + y->stride*i, item); + if (cmp != 0) return cmp; + } + } + return (x->length > y->length) - (x->length < y->length); +} + +public bool Array$equal(const Array_t *x, const Array_t *y, const TypeInfo *type) +{ + return x == y || (x->length == y->length && Array$compare(x, y, type) == 0); +} + +public Text_t Array$as_text(const Array_t *arr, bool colorize, const TypeInfo *type) +{ + if (!arr) + return Text$concat(Text("["), generic_as_text(NULL, false, type->ArrayInfo.item), Text("]")); + + const TypeInfo *item_type = type->ArrayInfo.item; + Text_t text = Text("["); + for (int64_t i = 0; i < arr->length; i++) { + if (i > 0) + text = Text$concat(text, Text(", ")); + Text_t item_text = generic_as_text(arr->data + i*arr->stride, colorize, item_type); + text = Text$concat(text, item_text); + } + text = Text$concat(text, Text("]")); + return text; +} + +public uint64_t Array$hash(const Array_t *arr, const TypeInfo *type) +{ + const TypeInfo *item = type->ArrayInfo.item; + siphash sh; + siphashinit(&sh, sizeof(uint64_t[arr->length])); + if (item->tag == PointerInfo || (item->tag == CustomInfo && item->CustomInfo.hash == NULL && item->size == sizeof(void*))) { // Raw data hash + for (int64_t i = 0; i < arr->length; i++) + siphashadd64bits(&sh, (uint64_t)(arr->data + i*arr->stride)); + } else { + for (int64_t i = 0; i < arr->length; i++) { + uint64_t item_hash = generic_hash(arr->data + i*arr->stride, item); + siphashadd64bits(&sh, item_hash); + } + } + return siphashfinish_last_part(&sh, 0); +} + +#pragma GCC diagnostic ignored "-Wstack-protector" +static void siftdown(Array_t *heap, int64_t startpos, int64_t pos, Closure_t comparison, int64_t padded_item_size) +{ + assert(pos > 0 && pos < heap->length); + char newitem[padded_item_size]; + memcpy(newitem, heap->data + heap->stride*pos, (size_t)(padded_item_size)); + while (pos > startpos) { + int64_t parentpos = (pos - 1) >> 1; + typedef int32_t (*cmp_fn_t)(void*, void*, void*); + int32_t cmp = ((cmp_fn_t)comparison.fn)(newitem, heap->data + heap->stride*parentpos, comparison.userdata); + if (cmp >= 0) + break; + + memcpy(heap->data + heap->stride*pos, heap->data + heap->stride*parentpos, (size_t)(padded_item_size)); + pos = parentpos; + } + memcpy(heap->data + heap->stride*pos, newitem, (size_t)(padded_item_size)); +} + +static void siftup(Array_t *heap, int64_t pos, Closure_t comparison, int64_t padded_item_size) +{ + int64_t endpos = heap->length; + int64_t startpos = pos; + assert(pos < endpos); + + char old_top[padded_item_size]; + memcpy(old_top, heap->data + heap->stride*pos, (size_t)(padded_item_size)); + // Bubble up the smallest leaf node + int64_t limit = endpos >> 1; + while (pos < limit) { + int64_t childpos = 2*pos + 1; // Smaller of the two child nodes + if (childpos + 1 < endpos) { + typedef int32_t (*cmp_fn_t)(void*, void*, void*); + int32_t cmp = ((cmp_fn_t)comparison.fn)( + heap->data + heap->stride*childpos, + heap->data + heap->stride*(childpos + 1), + comparison.userdata); + childpos += (cmp >= 0); + } + + // Move the child node up: + memcpy(heap->data + heap->stride*pos, heap->data + heap->stride*childpos, (size_t)(padded_item_size)); + pos = childpos; + } + memcpy(heap->data + heap->stride*pos, old_top, (size_t)(padded_item_size)); + // Shift the node's parents down: + siftdown(heap, startpos, pos, comparison, padded_item_size); +} + +public void Array$heap_push(Array_t *heap, const void *item, Closure_t comparison, int64_t padded_item_size) +{ + Array$insert(heap, item, I(0), padded_item_size); + + if (heap->length > 1) { + if (heap->data_refcount != 0) + Array$compact(heap, padded_item_size); + siftdown(heap, 0, heap->length-1, comparison, padded_item_size); + } +} + +public void Array$heap_pop(Array_t *heap, Closure_t comparison, int64_t padded_item_size) +{ + if (heap->length == 0) + fail("Attempt to pop from an empty array"); + + if (heap->length == 1) { + *heap = (Array_t){}; + } else if (heap->length == 2) { + heap->data += heap->stride; + --heap->length; + } else { + if (heap->data_refcount != 0) + Array$compact(heap, padded_item_size); + memcpy(heap->data, heap->data + heap->stride*(heap->length-1), (size_t)(padded_item_size)); + --heap->length; + siftup(heap, 0, comparison, padded_item_size); + } +} + +public void Array$heapify(Array_t *heap, Closure_t comparison, int64_t padded_item_size) +{ + if (heap->data_refcount != 0) + Array$compact(heap, padded_item_size); + + // It's necessary to bump the refcount because the user's comparison + // function could do stuff that modifies the heap's data. + ARRAY_INCREF(*heap); + int64_t i, n = heap->length; + for (i = (n >> 1) - 1 ; i >= 0 ; i--) + siftup(heap, i, comparison, padded_item_size); + ARRAY_DECREF(*heap); +} + +public Int_t Array$binary_search(Array_t array, void *target, Closure_t comparison) +{ + typedef int32_t (*cmp_fn_t)(void*, void*, void*); + int64_t lo = 0, hi = array.length-1; + while (lo <= hi) { + int64_t mid = (lo + hi) / 2; + int32_t cmp = ((cmp_fn_t)comparison.fn)( + array.data + array.stride*mid, target, comparison.userdata); + if (cmp == 0) + return I(mid+1); + else if (cmp < 0) + lo = mid + 1; + else if (cmp > 0) + hi = mid - 1; + } + return I(lo+1); // Return the index where the target would be inserted +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/arrays.h b/builtins/arrays.h new file mode 100644 index 00000000..1e945e5e --- /dev/null +++ b/builtins/arrays.h @@ -0,0 +1,103 @@ +#pragma once + +// Functions that operate on arrays + +#include + +#include "datatypes.h" +#include "integers.h" +#include "types.h" +#include "util.h" + +// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1 +#define Array_get(item_type, arr_expr, index_expr, start, end) *({ \ + const Array_t arr = arr_expr; int64_t index = index_expr; \ + int64_t off = index + (index < 0) * (arr.length + 1) - 1; \ + if (__builtin_expect(off < 0 || off >= arr.length, 0)) \ + fail_source(__SOURCE_FILE__, start, end, "Invalid array index: %s (array has length %ld)\n", Text$as_c_string(Int64$as_text(&index, no, NULL)), arr.length); \ + (item_type*)(arr.data + arr.stride * off);}) +#define Array_get_unchecked(type, x, i) *({ const Array_t arr = x; int64_t index = i; \ + int64_t off = index + (index < 0) * (arr.length + 1) - 1; \ + (type*)(arr.data + arr.stride * off);}) +#define Array_lvalue(item_type, arr_expr, index_expr, padded_item_size, start, end) *({ \ + Array_t *arr = arr_expr; int64_t index = index_expr; \ + int64_t off = index + (index < 0) * (arr->length + 1) - 1; \ + if (__builtin_expect(off < 0 || off >= arr->length, 0)) \ + fail_source(__SOURCE_FILE__, start, end, "Invalid array index: %s (array has length %ld)\n", Text$as_c_string(Int64$as_text(&index, no, NULL)), arr->length); \ + if (arr->data_refcount > 0) \ + Array$compact(arr, padded_item_size); \ + (item_type*)(arr->data + arr->stride * off); }) +#define Array_lvalue_unchecked(item_type, arr_expr, index_expr, padded_item_size) *({ \ + Array_t *arr = arr_expr; int64_t index = index_expr; \ + int64_t off = index + (index < 0) * (arr->length + 1) - 1; \ + if (arr->data_refcount > 0) \ + Array$compact(arr, padded_item_size); \ + (item_type*)(arr->data + arr->stride * off); }) +#define Array_set(item_type, arr, index, value, padded_item_size, start, end) \ + Array_lvalue(item_type, arr_expr, index, padded_item_size, start, end) = value +#define is_atomic(x) _Generic(x, bool: true, int8_t: true, int16_t: true, int32_t: true, int64_t: true, float: true, double: true, default: false) +#define TypedArray(t, ...) ({ t items[] = {__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)), \ + .atomic=0, \ + .data_refcount=0}; }) +#define TypedArrayN(t, N, ...) ({ t items[N] = {__VA_ARGS__}; \ + (Array_t){.length=N, \ + .stride=(int64_t)&items[1] - (int64_t)&items[0], \ + .data=memcpy(GC_MALLOC(sizeof(items)), items, sizeof(items)), \ + .atomic=0, \ + .data_refcount=0}; }) +#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(is_atomic(x) ? GC_MALLOC_ATOMIC(sizeof(items)) : GC_MALLOC(sizeof(items)), items, sizeof(items)), \ + .atomic=is_atomic(x), \ + .data_refcount=0}; }) +// Array refcounts use a saturating add, where once it's at the max value, it stays there. +#define ARRAY_INCREF(arr) (arr).data_refcount += ((arr).data_refcount < ARRAY_MAX_DATA_REFCOUNT) +#define ARRAY_DECREF(arr) (arr).data_refcount -= ((arr).data_refcount < ARRAY_MAX_DATA_REFCOUNT) +#define ARRAY_COPY(arr) ({ ARRAY_INCREF(arr); arr; }) + +#define Array$insert_value(arr, item_expr, index, padded_item_size) ({ __typeof(item_expr) item = item_expr; Array$insert(arr, &item, index, padded_item_size); }) +void Array$insert(Array_t *arr, const void *item, Int_t index, int64_t padded_item_size); +void Array$insert_all(Array_t *arr, Array_t to_insert, Int_t index, int64_t padded_item_size); +void Array$remove_at(Array_t *arr, Int_t index, Int_t count, int64_t padded_item_size); +void Array$remove_item(Array_t *arr, void *item, Int_t max_removals, const TypeInfo *type); +#define Array$remove_item_value(arr, item_expr, max, type) ({ __typeof(item_expr) item = item_expr; Array$remove_item(arr, &item, max, type); }) +Int_t Array$find(Array_t arr, void *item, const TypeInfo *type); +#define Array$find_value(arr, item_expr, type) ({ __typeof(item_expr) item = item_expr; Array$find(arr, &item, type); }) +Int_t Array$first(Array_t arr, Closure_t predicate); +void Array$sort(Array_t *arr, Closure_t comparison, int64_t padded_item_size); +Array_t Array$sorted(Array_t arr, Closure_t comparison, int64_t padded_item_size); +void Array$shuffle(Array_t *arr, int64_t padded_item_size); +Array_t Array$shuffled(Array_t arr, int64_t padded_item_size); +void *Array$random(Array_t arr); +#define Array$random_value(arr, t) ({ Array_t _arr = arr; if (_arr.length == 0) fail("Cannot get a random value from an empty array!"); *(t*)Array$random(_arr); }) +Array_t Array$sample(Array_t arr, Int_t n, Array_t weights, int64_t padded_item_size); +Table_t Array$counts(Array_t arr, const TypeInfo *type); +void Array$clear(Array_t *array); +void Array$compact(Array_t *arr, int64_t padded_item_size); +PUREFUNC bool Array$has(Array_t array, void *item, const TypeInfo *type); +#define Array$has_value(arr, item_expr, type) ({ __typeof(item_expr) item = item_expr; Array$has(arr, &item, type); }) +PUREFUNC Array_t Array$from(Array_t array, Int_t first); +PUREFUNC Array_t Array$to(Array_t array, Int_t last); +PUREFUNC Array_t Array$by(Array_t array, Int_t stride, int64_t padded_item_size); +PUREFUNC Array_t Array$reversed(Array_t array, int64_t padded_item_size); +Array_t Array$concat(Array_t x, Array_t y, int64_t padded_item_size); +PUREFUNC uint64_t Array$hash(const Array_t *arr, const TypeInfo *type); +PUREFUNC int32_t Array$compare(const Array_t *x, const Array_t *y, const TypeInfo *type); +PUREFUNC bool Array$equal(const Array_t *x, const Array_t *y, const TypeInfo *type); +Text_t Array$as_text(const Array_t *arr, bool colorize, const TypeInfo *type); +void Array$heapify(Array_t *heap, Closure_t comparison, int64_t padded_item_size); +void Array$heap_push(Array_t *heap, const void *item, Closure_t comparison, int64_t padded_item_size); +#define Array$heap_push_value(heap, _value, comparison, padded_item_size) ({ __typeof(_value) value = _value; Array$heap_push(heap, &value, comparison, padded_item_size); }) +void Array$heap_pop(Array_t *heap, Closure_t comparison, int64_t padded_item_size); +#define Array$heap_pop_value(heap, comparison, padded_item_size, type) \ + ({ Array_t *_heap = heap; if (_heap->length == 0) fail("Attempt to pop from an empty array"); \ + type value = *(type*)_heap->data; Array$heap_pop(_heap, comparison, padded_item_size); value; }) +Int_t Array$binary_search(Array_t array, void *target, Closure_t comparison); +#define Array$binary_search_value(array, target, comparison) \ + ({ __typeof(target) _target = target; Array$binary_search(array, &_target, comparison); }) + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/bool.c b/builtins/bool.c deleted file mode 100644 index 14406c5f..00000000 --- a/builtins/bool.c +++ /dev/null @@ -1,54 +0,0 @@ -// Boolean methods/type info -#include -#include -#include -#include -#include -#include -#include - -#include "bool.h" -#include "optionals.h" -#include "text.h" -#include "util.h" - -PUREFUNC public Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) -{ - (void)type; - if (!b) return Text("Bool"); - if (colorize) - return *b ? Text("\x1b[35myes\x1b[m") : Text("\x1b[35mno\x1b[m"); - else - return *b ? Text("yes") : Text("no"); -} - -PUREFUNC public OptionalBool_t Bool$from_text(Text_t text) -{ - if (Text$equal_ignoring_case(text, Text("yes")) - || Text$equal_ignoring_case(text, Text("on")) - || Text$equal_ignoring_case(text, Text("true")) - || Text$equal_ignoring_case(text, Text("1"))) { - return yes; - } else if (Text$equal_ignoring_case(text, Text("no")) - || Text$equal_ignoring_case(text, Text("off")) - || Text$equal_ignoring_case(text, Text("false")) - || Text$equal_ignoring_case(text, Text("0"))) { - return no; - } else { - return NULL_BOOL; - } -} - -public Bool_t Bool$random(double p) -{ - return (drand48() < p); -} - -public const TypeInfo Bool$info = { - .size=sizeof(bool), - .align=__alignof__(bool), - .tag=CustomInfo, - .CustomInfo={.as_text=(void*)Bool$as_text}, -}; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/bool.h b/builtins/bool.h deleted file mode 100644 index 98b2ac06..00000000 --- a/builtins/bool.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -// Boolean functions/type info - -#include -#include - -#include "types.h" -#include "optionals.h" -#include "util.h" - -#define Bool_t bool -#define yes (Bool_t)true -#define no (Bool_t)false - -PUREFUNC Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type); -OptionalBool_t Bool$from_text(Text_t text); -Bool_t Bool$random(double p); - -extern const TypeInfo Bool$info; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/bools.c b/builtins/bools.c new file mode 100644 index 00000000..d7b3718f --- /dev/null +++ b/builtins/bools.c @@ -0,0 +1,54 @@ +// Boolean methods/type info +#include +#include +#include +#include +#include +#include +#include + +#include "bools.h" +#include "optionals.h" +#include "text.h" +#include "util.h" + +PUREFUNC public Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) +{ + (void)type; + if (!b) return Text("Bool"); + if (colorize) + return *b ? Text("\x1b[35myes\x1b[m") : Text("\x1b[35mno\x1b[m"); + else + return *b ? Text("yes") : Text("no"); +} + +PUREFUNC public OptionalBool_t Bool$from_text(Text_t text) +{ + if (Text$equal_ignoring_case(text, Text("yes")) + || Text$equal_ignoring_case(text, Text("on")) + || Text$equal_ignoring_case(text, Text("true")) + || Text$equal_ignoring_case(text, Text("1"))) { + return yes; + } else if (Text$equal_ignoring_case(text, Text("no")) + || Text$equal_ignoring_case(text, Text("off")) + || Text$equal_ignoring_case(text, Text("false")) + || Text$equal_ignoring_case(text, Text("0"))) { + return no; + } else { + return NULL_BOOL; + } +} + +public Bool_t Bool$random(double p) +{ + return (drand48() < p); +} + +public const TypeInfo Bool$info = { + .size=sizeof(bool), + .align=__alignof__(bool), + .tag=CustomInfo, + .CustomInfo={.as_text=(void*)Bool$as_text}, +}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/bools.h b/builtins/bools.h new file mode 100644 index 00000000..98b2ac06 --- /dev/null +++ b/builtins/bools.h @@ -0,0 +1,22 @@ +#pragma once + +// Boolean functions/type info + +#include +#include + +#include "types.h" +#include "optionals.h" +#include "util.h" + +#define Bool_t bool +#define yes (Bool_t)true +#define no (Bool_t)false + +PUREFUNC Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type); +OptionalBool_t Bool$from_text(Text_t text); +Bool_t Bool$random(double p); + +extern const TypeInfo Bool$info; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/channel.c b/builtins/channel.c deleted file mode 100644 index 11978397..00000000 --- a/builtins/channel.c +++ /dev/null @@ -1,137 +0,0 @@ -// Functions that operate on channels - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "array.h" -#include "metamethods.h" -#include "integers.h" -#include "siphash.h" -#include "text.h" -#include "types.h" -#include "util.h" - -public Channel_t *Channel$new(Int_t max_size) -{ - if (Int$compare_value(max_size, I_small(0)) <= 0) - fail("Cannot create a channel with a size less than one: %ld", max_size); - Channel_t *channel = new(Channel_t); - channel->items = (Array_t){}; - channel->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; - channel->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; - channel->max_size = Int_to_Int64(max_size, false); - return channel; -} - -public void Channel$give(Channel_t *channel, const void *item, bool front, int64_t padded_item_size) -{ - (void)pthread_mutex_lock(&channel->mutex); - while (channel->items.length >= channel->max_size) - pthread_cond_wait(&channel->cond, &channel->mutex); - Int_t index = front ? I_small(1) : I_small(0); - Array$insert(&channel->items, item, index, padded_item_size); - (void)pthread_mutex_unlock(&channel->mutex); - (void)pthread_cond_signal(&channel->cond); -} - -public void Channel$give_all(Channel_t *channel, Array_t to_give, bool front, int64_t padded_item_size) -{ - if (to_give.length == 0) return; - (void)pthread_mutex_lock(&channel->mutex); - Int_t index = front ? I_small(1) : I_small(0); - if (channel->items.length + to_give.length >= channel->max_size) { - for (int64_t i = 0; i < to_give.length; i++) { - while (channel->items.length >= channel->max_size) - pthread_cond_wait(&channel->cond, &channel->mutex); - Array$insert(&channel->items, to_give.data + i*to_give.stride, index, padded_item_size); - } - } else { - Array$insert_all(&channel->items, to_give, index, padded_item_size); - } - (void)pthread_mutex_unlock(&channel->mutex); - (void)pthread_cond_signal(&channel->cond); -} - -public void Channel$get(Channel_t *channel, void *out, bool front, int64_t item_size, int64_t padded_item_size) -{ - (void)pthread_mutex_lock(&channel->mutex); - while (channel->items.length == 0) - pthread_cond_wait(&channel->cond, &channel->mutex); - memcpy(out, channel->items.data + channel->items.stride * (front ? 0 : channel->items.length-1), (size_t)(item_size)); - Int_t index = front ? I_small(1) : Int64_to_Int(channel->items.length); - Array$remove_at(&channel->items, index, I_small(1), padded_item_size); - (void)pthread_mutex_unlock(&channel->mutex); - (void)pthread_cond_signal(&channel->cond); -} - -public void Channel$peek(Channel_t *channel, void *out, bool front, int64_t item_size) -{ - (void)pthread_mutex_lock(&channel->mutex); - while (channel->items.length == 0) - pthread_cond_wait(&channel->cond, &channel->mutex); - int64_t index = front ? 0 : channel->items.length-1; - memcpy(out, channel->items.data + channel->items.stride*index, (size_t)(item_size)); - (void)pthread_mutex_unlock(&channel->mutex); - (void)pthread_cond_signal(&channel->cond); -} - -public Array_t Channel$view(Channel_t *channel) -{ - (void)pthread_mutex_lock(&channel->mutex); - ARRAY_INCREF(channel->items); - Array_t ret = channel->items; - (void)pthread_mutex_unlock(&channel->mutex); - return ret; -} - -public void Channel$clear(Channel_t *channel) -{ - (void)pthread_mutex_lock(&channel->mutex); - Array$clear(&channel->items); - (void)pthread_mutex_unlock(&channel->mutex); - (void)pthread_cond_signal(&channel->cond); -} - -PUREFUNC public uint64_t Channel$hash(Channel_t **channel, const TypeInfo *type) -{ - (void)type; - return siphash24((void*)*channel, sizeof(Channel_t*)); -} - -PUREFUNC public int32_t Channel$compare(Channel_t **x, Channel_t **y, const TypeInfo *type) -{ - (void)type; - return (*x > *y) - (*x < *y); -} - -PUREFUNC public bool Channel$equal(Channel_t **x, Channel_t **y, const TypeInfo *type) -{ - (void)type; - return (*x == *y); -} - -public Text_t Channel$as_text(Channel_t **channel, bool colorize, const TypeInfo *type) -{ - const TypeInfo *item_type = type->ChannelInfo.item; - if (!channel) { - Text_t typename = generic_as_text(NULL, false, item_type); - return Text$concat(colorize ? Text("\x1b[34;1m|:") : Text("|:"), typename, colorize ? Text("|\x1b[m") : Text("|")); - } - Text_t typename = generic_as_text(NULL, false, item_type); - return Text$concat( - colorize ? Text("\x1b[34;1m|:") : Text("|:"), - typename, - Text("|<"), - Int64$hex((int64_t)(void*)*channel, I_small(0), true, true), - colorize ? Text(">\x1b[m") : Text(">") - ); -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/channel.h b/builtins/channel.h deleted file mode 100644 index 8deb0569..00000000 --- a/builtins/channel.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -// Functions that operate on channels (thread-safe arrays) - -#include - -#include "datatypes.h" -#include "types.h" -#include "util.h" - -Channel_t *Channel$new(Int_t max_size); -void Channel$give(Channel_t *channel, const void *item, bool front, int64_t padded_item_size); -#define Channel$give_value(channel, item, front, padded_item_size) \ - ({ __typeof(item) _item = item; Channel$give(channel, &_item, front, padded_item_size); }) -void Channel$give_all(Channel_t *channel, Array_t to_give, bool front, int64_t padded_item_size); -void Channel$get(Channel_t *channel, void *out, bool front, int64_t item_size, int64_t padded_item_size); -#define Channel$get_value(channel, front, t, padded_item_size) \ - ({ t _val; Channel$get(channel, &_val, front, sizeof(t), padded_item_size); _val; }) -void Channel$peek(Channel_t *channel, void *out, bool front, int64_t item_size); -#define Channel$peek_value(channel, front, t) ({ t _val; Channel$peek(channel, &_val, front, sizeof(t)); _val; }) -void Channel$clear(Channel_t *channel); -Array_t Channel$view(Channel_t *channel); -PUREFUNC uint64_t Channel$hash(Channel_t **channel, const TypeInfo *type); -PUREFUNC int32_t Channel$compare(Channel_t **x, Channel_t **y, const TypeInfo *type); -PUREFUNC bool Channel$equal(Channel_t **x, Channel_t **y, const TypeInfo *type); -Text_t Channel$as_text(Channel_t **channel, bool colorize, const TypeInfo *type); - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/channels.c b/builtins/channels.c new file mode 100644 index 00000000..3681b0b8 --- /dev/null +++ b/builtins/channels.c @@ -0,0 +1,137 @@ +// Functions that operate on channels + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arrays.h" +#include "metamethods.h" +#include "integers.h" +#include "siphash.h" +#include "text.h" +#include "types.h" +#include "util.h" + +public Channel_t *Channel$new(Int_t max_size) +{ + if (Int$compare_value(max_size, I_small(0)) <= 0) + fail("Cannot create a channel with a size less than one: %ld", max_size); + Channel_t *channel = new(Channel_t); + channel->items = (Array_t){}; + channel->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; + channel->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER; + channel->max_size = Int_to_Int64(max_size, false); + return channel; +} + +public void Channel$give(Channel_t *channel, const void *item, bool front, int64_t padded_item_size) +{ + (void)pthread_mutex_lock(&channel->mutex); + while (channel->items.length >= channel->max_size) + pthread_cond_wait(&channel->cond, &channel->mutex); + Int_t index = front ? I_small(1) : I_small(0); + Array$insert(&channel->items, item, index, padded_item_size); + (void)pthread_mutex_unlock(&channel->mutex); + (void)pthread_cond_signal(&channel->cond); +} + +public void Channel$give_all(Channel_t *channel, Array_t to_give, bool front, int64_t padded_item_size) +{ + if (to_give.length == 0) return; + (void)pthread_mutex_lock(&channel->mutex); + Int_t index = front ? I_small(1) : I_small(0); + if (channel->items.length + to_give.length >= channel->max_size) { + for (int64_t i = 0; i < to_give.length; i++) { + while (channel->items.length >= channel->max_size) + pthread_cond_wait(&channel->cond, &channel->mutex); + Array$insert(&channel->items, to_give.data + i*to_give.stride, index, padded_item_size); + } + } else { + Array$insert_all(&channel->items, to_give, index, padded_item_size); + } + (void)pthread_mutex_unlock(&channel->mutex); + (void)pthread_cond_signal(&channel->cond); +} + +public void Channel$get(Channel_t *channel, void *out, bool front, int64_t item_size, int64_t padded_item_size) +{ + (void)pthread_mutex_lock(&channel->mutex); + while (channel->items.length == 0) + pthread_cond_wait(&channel->cond, &channel->mutex); + memcpy(out, channel->items.data + channel->items.stride * (front ? 0 : channel->items.length-1), (size_t)(item_size)); + Int_t index = front ? I_small(1) : Int64_to_Int(channel->items.length); + Array$remove_at(&channel->items, index, I_small(1), padded_item_size); + (void)pthread_mutex_unlock(&channel->mutex); + (void)pthread_cond_signal(&channel->cond); +} + +public void Channel$peek(Channel_t *channel, void *out, bool front, int64_t item_size) +{ + (void)pthread_mutex_lock(&channel->mutex); + while (channel->items.length == 0) + pthread_cond_wait(&channel->cond, &channel->mutex); + int64_t index = front ? 0 : channel->items.length-1; + memcpy(out, channel->items.data + channel->items.stride*index, (size_t)(item_size)); + (void)pthread_mutex_unlock(&channel->mutex); + (void)pthread_cond_signal(&channel->cond); +} + +public Array_t Channel$view(Channel_t *channel) +{ + (void)pthread_mutex_lock(&channel->mutex); + ARRAY_INCREF(channel->items); + Array_t ret = channel->items; + (void)pthread_mutex_unlock(&channel->mutex); + return ret; +} + +public void Channel$clear(Channel_t *channel) +{ + (void)pthread_mutex_lock(&channel->mutex); + Array$clear(&channel->items); + (void)pthread_mutex_unlock(&channel->mutex); + (void)pthread_cond_signal(&channel->cond); +} + +PUREFUNC public uint64_t Channel$hash(Channel_t **channel, const TypeInfo *type) +{ + (void)type; + return siphash24((void*)*channel, sizeof(Channel_t*)); +} + +PUREFUNC public int32_t Channel$compare(Channel_t **x, Channel_t **y, const TypeInfo *type) +{ + (void)type; + return (*x > *y) - (*x < *y); +} + +PUREFUNC public bool Channel$equal(Channel_t **x, Channel_t **y, const TypeInfo *type) +{ + (void)type; + return (*x == *y); +} + +public Text_t Channel$as_text(Channel_t **channel, bool colorize, const TypeInfo *type) +{ + const TypeInfo *item_type = type->ChannelInfo.item; + if (!channel) { + Text_t typename = generic_as_text(NULL, false, item_type); + return Text$concat(colorize ? Text("\x1b[34;1m|:") : Text("|:"), typename, colorize ? Text("|\x1b[m") : Text("|")); + } + Text_t typename = generic_as_text(NULL, false, item_type); + return Text$concat( + colorize ? Text("\x1b[34;1m|:") : Text("|:"), + typename, + Text("|<"), + Int64$hex((int64_t)(void*)*channel, I_small(0), true, true), + colorize ? Text(">\x1b[m") : Text(">") + ); +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/channels.h b/builtins/channels.h new file mode 100644 index 00000000..8deb0569 --- /dev/null +++ b/builtins/channels.h @@ -0,0 +1,28 @@ +#pragma once + +// Functions that operate on channels (thread-safe arrays) + +#include + +#include "datatypes.h" +#include "types.h" +#include "util.h" + +Channel_t *Channel$new(Int_t max_size); +void Channel$give(Channel_t *channel, const void *item, bool front, int64_t padded_item_size); +#define Channel$give_value(channel, item, front, padded_item_size) \ + ({ __typeof(item) _item = item; Channel$give(channel, &_item, front, padded_item_size); }) +void Channel$give_all(Channel_t *channel, Array_t to_give, bool front, int64_t padded_item_size); +void Channel$get(Channel_t *channel, void *out, bool front, int64_t item_size, int64_t padded_item_size); +#define Channel$get_value(channel, front, t, padded_item_size) \ + ({ t _val; Channel$get(channel, &_val, front, sizeof(t), padded_item_size); _val; }) +void Channel$peek(Channel_t *channel, void *out, bool front, int64_t item_size); +#define Channel$peek_value(channel, front, t) ({ t _val; Channel$peek(channel, &_val, front, sizeof(t)); _val; }) +void Channel$clear(Channel_t *channel); +Array_t Channel$view(Channel_t *channel); +PUREFUNC uint64_t Channel$hash(Channel_t **channel, const TypeInfo *type); +PUREFUNC int32_t Channel$compare(Channel_t **x, Channel_t **y, const TypeInfo *type); +PUREFUNC bool Channel$equal(Channel_t **x, Channel_t **y, const TypeInfo *type); +Text_t Channel$as_text(Channel_t **channel, bool colorize, const TypeInfo *type); + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/functiontype.c b/builtins/functiontype.c index c376ba26..251a01ed 100644 --- a/builtins/functiontype.c +++ b/builtins/functiontype.c @@ -1,7 +1,7 @@ // Logic for handling function type values #include "datatypes.h" -#include "table.h" +#include "tables.h" #include "text.h" #include "types.h" #include "util.h" diff --git a/builtins/integers.c b/builtins/integers.c index 65ac5c82..ef588984 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -6,7 +6,7 @@ #include #include -#include "array.h" +#include "arrays.h" #include "datatypes.h" #include "integers.h" #include "optionals.h" diff --git a/builtins/metamethods.c b/builtins/metamethods.c index a244bf75..9d5bfcaa 100644 --- a/builtins/metamethods.c +++ b/builtins/metamethods.c @@ -2,14 +2,14 @@ #include -#include "array.h" -#include "channel.h" +#include "arrays.h" +#include "channels.h" #include "functiontype.h" #include "metamethods.h" #include "optionals.h" -#include "pointer.h" +#include "pointers.h" #include "siphash.h" -#include "table.h" +#include "tables.h" #include "text.h" #include "util.h" diff --git a/builtins/nums.c b/builtins/nums.c index c54e8fc5..1956140a 100644 --- a/builtins/nums.c +++ b/builtins/nums.c @@ -7,7 +7,7 @@ #include #include -#include "array.h" +#include "arrays.h" #include "nums.h" #include "string.h" #include "text.h" diff --git a/builtins/optionals.c b/builtins/optionals.c index 276e92f8..b6ca8dfb 100644 --- a/builtins/optionals.c +++ b/builtins/optionals.c @@ -1,10 +1,12 @@ // Optional types -#include "bool.h" +#include + +#include "bools.h" #include "datatypes.h" #include "integers.h" #include "metamethods.h" -#include "thread.h" +#include "threads.h" #include "text.h" #include "util.h" diff --git a/builtins/path.c b/builtins/path.c deleted file mode 100644 index 0168462f..00000000 --- a/builtins/path.c +++ /dev/null @@ -1,481 +0,0 @@ -// A lang for filesystem paths -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "array.h" -#include "files.h" -#include "integers.h" -#include "optionals.h" -#include "path.h" -#include "pattern.h" -#include "text.h" -#include "types.h" -#include "util.h" - -PUREFUNC public Path_t Path$escape_text(Text_t text) -{ - if (Text$has(text, Pattern("/"))) - fail("Path interpolations cannot contain slashes: %k", &text); - else if (Text$has(text, Pattern(";"))) - fail("Path interpolations cannot contain semicolons: %k", &text); - else if (Text$equal_values(text, Path(".")) || Text$equal_values(text, Path(".."))) - fail("Path interpolation is \"%k\" which is disallowed to prevent security vulnerabilities", &text); - return (Path_t)text; -} - -PUREFUNC public Path_t Path$escape_path(Path_t path) -{ - if (Text$starts_with(path, Path("~/")) || Text$starts_with(path, Path("/"))) - fail("Invalid path component: %k", &path); - return path; -} - -public Path_t Path$cleanup(Path_t path) -{ - if (!Text$starts_with(path, Path("/")) && !Text$starts_with(path, Path("./")) - && !Text$starts_with(path, Path("../")) && !Text$starts_with(path, Path("~/"))) - path = Text$concat(Text("./"), path); - - // Not fully resolved, but at least get rid of some of the cruft like "/./" - // and "/foo/../" and "//" - bool trailing_slash = Text$ends_with(path, Path("/")); - Array_t components = Text$split(path, Pattern("/")); - if (components.length == 0) return Path("/"); - Path_t root = *(Path_t*)components.data; - Array$remove_at(&components, I(1), I(1), sizeof(Path_t)); - - for (int64_t i = 0; i < components.length; ) { - Path_t component = *(Path_t*)(components.data + i*components.stride); - if (component.length == 0 || Text$equal_values(component, Path("."))) { // Skip (//) and (/./) - Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); - } else if (Text$equal_values(component, Path(".."))) { - if (i == 0) { - if (root.length == 0) { // (/..) -> (/) - Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); - i += 1; - } else if (Text$equal_values(root, Path("."))) { // (./..) -> (..) - root = Path(".."); - Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); - i += 1; - } else if (Text$equal_values(root, Path("~"))) { - root = Path(""); // Convert $HOME to absolute path: - - Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); - // `i` is pointing to where the `..` lived - - const char *home = getenv("HOME"); - if (!home) fail("Could not get $HOME directory!"); - - // Insert all but the last component: - for (const char *p = home + 1; *p; ) { - const char *next_slash = strchr(p, '/'); - if (!next_slash) break; // Skip last component - Path_t home_component = Text$format("%.*s", (int)(next_slash - p), p); - Array$insert(&components, &home_component, I(i+1), sizeof(Path_t)); - i += 1; - p = next_slash + 1; - } - } else { // (../..) -> (../..) - i += 1; - } - } else if (Text$equal(&component, (Path_t*)(components.data + (i-1)*components.stride))) { // (___/../..) -> (____/../..) - i += 1; - } else { // (___/foo/..) -> (___) - Array$remove_at(&components, I(i), I(2), sizeof(Path_t)); - i -= 1; - } - } else { // (___/foo/baz) -> (___/foo/baz) - i++; - } - } - - Text_t cleaned_up = Text$concat(root, Text("/"), Text$join(Text("/"), components)); - if (trailing_slash && !Text$ends_with(cleaned_up, Text("/"))) - cleaned_up = Text$concat(cleaned_up, Text("/")); - return cleaned_up; -} - -static inline Path_t Path$_expand_home(Path_t path) -{ - if (Text$starts_with(path, Path("~/"))) { - Path_t after_tilde = Text$slice(path, I(2), I(-1)); - return Text$format("%s%k", getenv("HOME"), &after_tilde); - } else { - return path; - } -} - -public Path_t Path$_concat(int n, Path_t items[n]) -{ - Path_t cleaned_up = Path$cleanup(Text$_concat(n, items)); - if (cleaned_up.length > PATH_MAX) - fail("Path exceeds the maximum path length: %k", &cleaned_up); - return cleaned_up; -} - -public Text_t Path$resolved(Path_t path, Path_t relative_to) -{ - path = Path$cleanup(path); - - const char *path_str = Text$as_c_string(path); - const char *relative_to_str = Text$as_c_string(relative_to); - const char *resolved_path = resolve_path(path_str, relative_to_str, relative_to_str); - if (resolved_path) { - return (Path_t)(Text$from_str(resolved_path)); - } else if (path_str[0] == '/') { - return path; - } else if (path_str[0] == '~' && path_str[1] == '/') { - return (Path_t)Text$format("%s%s", getenv("HOME"), path_str + 1); - } else { - return Text$concat(Path$resolved(relative_to, Path(".")), Path("/"), path); - } -} - -public Text_t Path$relative(Path_t path, Path_t relative_to) -{ - path = Path$resolved(path, relative_to); - relative_to = Path$resolved(relative_to, Path(".")); - if (Text$matches(path, Patterns(Pattern("{start}"), relative_to, Pattern("{0+..}")))) - return Text$slice(path, I(relative_to.length + 2), I(-1)); - return path; -} - -public bool Path$exists(Path_t path) -{ - path = Path$_expand_home(path); - struct stat sb; - return (stat(Text$as_c_string(path), &sb) == 0); -} - -public bool Path$is_file(Path_t path, bool follow_symlinks) -{ - path = Path$_expand_home(path); - struct stat sb; - const char *path_str = Text$as_c_string(path); - int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); - if (status != 0) return false; - return (sb.st_mode & S_IFMT) == S_IFREG; -} - -public bool Path$is_directory(Path_t path, bool follow_symlinks) -{ - path = Path$_expand_home(path); - struct stat sb; - const char *path_str = Text$as_c_string(path); - int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); - if (status != 0) return false; - return (sb.st_mode & S_IFMT) == S_IFDIR; -} - -public bool Path$is_pipe(Path_t path, bool follow_symlinks) -{ - path = Path$_expand_home(path); - struct stat sb; - const char *path_str = Text$as_c_string(path); - int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); - if (status != 0) return false; - return (sb.st_mode & S_IFMT) == S_IFIFO; -} - -public bool Path$is_socket(Path_t path, bool follow_symlinks) -{ - path = Path$_expand_home(path); - struct stat sb; - const char *path_str = Text$as_c_string(path); - int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); - if (status != 0) return false; - return (sb.st_mode & S_IFMT) == S_IFSOCK; -} - -public bool Path$is_symlink(Path_t path) -{ - path = Path$_expand_home(path); - struct stat sb; - const char *path_str = Text$as_c_string(path); - int status = stat(path_str, &sb); - if (status != 0) return false; - return (sb.st_mode & S_IFMT) == S_IFLNK; -} - -static void _write(Path_t path, Text_t text, int mode, int permissions) -{ - path = Path$_expand_home(path); - const char *path_str = Text$as_c_string(path); - int fd = open(path_str, mode, permissions); - if (fd == -1) - fail("Could not write to file: %s\n%s", path_str, strerror(errno)); - - const char *str = Text$as_c_string(text); - size_t len = strlen(str); - ssize_t written = write(fd, str, len); - if (written != (ssize_t)len) - fail("Could not write to file: %s\n%s", path_str, strerror(errno)); -} - -public void Path$write(Path_t path, Text_t text, int permissions) -{ - _write(path, text, O_WRONLY | O_CREAT, permissions); -} - -public void Path$append(Path_t path, Text_t text, int permissions) -{ - _write(path, text, O_WRONLY | O_APPEND | O_CREAT, permissions); -} - -public Text_t Path$read(Path_t path) -{ - path = Path$_expand_home(path); - int fd = open(Text$as_c_string(path), O_RDONLY); - if (fd == -1) - fail("Could not read file: %k (%s)", &path, strerror(errno)); - - struct stat sb; - if (fstat(fd, &sb) != 0) - fail("Could not read file: %k (%s)", &path, strerror(errno)); - - if ((sb.st_mode & S_IFMT) == S_IFREG) { // Use memory mapping if it's a real file: - const char *mem = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - char *gc_mem = GC_MALLOC_ATOMIC((size_t)sb.st_size+1); - memcpy(gc_mem, mem, (size_t)sb.st_size); - gc_mem[sb.st_size] = '\0'; - close(fd); - return Text$from_strn(gc_mem, (size_t)sb.st_size); - } else { - size_t capacity = 256, len = 0; - char *content = GC_MALLOC_ATOMIC(capacity); - for (;;) { - char chunk[256]; - ssize_t just_read = read(fd, chunk, sizeof(chunk)); - if (just_read < 0) - fail("Failed while reading file: %k (%s)", &path, strerror(errno)); - else if (just_read == 0) { - if (errno == EAGAIN || errno == EINTR) - continue; - break; - } - - if (len + (size_t)just_read >= capacity) { - content = GC_REALLOC(content, (capacity *= 2)); - } - - memcpy(&content[len], chunk, (size_t)just_read); - len += (size_t)just_read; - - if ((size_t)just_read < sizeof(chunk)) - break; - } - close(fd); - - if (u8_check((uint8_t*)content, len) != NULL) - fail("File does not contain valid UTF8 data!"); - - return Text$from_strn(content, len); - } -} - -public void Path$remove(Path_t path, bool ignore_missing) -{ - path = Path$_expand_home(path); - const char *path_str = Text$as_c_string(path); - struct stat sb; - if (lstat(path_str, &sb) != 0) { - if (!ignore_missing) - fail("Could not remove file: %s (%s)", path_str, strerror(errno)); - } - - if ((sb.st_mode & S_IFMT) == S_IFREG || (sb.st_mode & S_IFMT) == S_IFLNK) { - if (unlink(path_str) != 0 && !ignore_missing) - fail("Could not remove file: %s (%s)", path_str, strerror(errno)); - } else if ((sb.st_mode & S_IFMT) == S_IFDIR) { - if (rmdir(path_str) != 0 && !ignore_missing) - fail("Could not remove directory: %s (%s)", path_str, strerror(errno)); - } else { - fail("Could not remove path: %s (not a file or directory)", path_str, strerror(errno)); - } -} - -public void Path$create_directory(Path_t path, int permissions) -{ - path = Path$_expand_home(path); - if (mkdir(Text$as_c_string(path), (mode_t)permissions) != 0) - fail("Could not create directory: %k (%s)", &path, strerror(errno)); -} - -static Array_t _filtered_children(Path_t path, bool include_hidden, mode_t filter) -{ - path = Path$_expand_home(path); - struct dirent *dir; - Array_t children = {}; - const char *path_str = Text$as_c_string(path); - size_t path_len = strlen(path_str); - DIR *d = opendir(path_str); - if (!d) - fail("Could not open directory: %k (%s)", &path, strerror(errno)); - - if (path_str[path_len-1] == '/') - --path_len; - - while ((dir = readdir(d)) != NULL) { - if (!include_hidden && dir->d_name[0] == '.') - continue; - if (streq(dir->d_name, ".") || streq(dir->d_name, "..")) - continue; - - const char *child_str = heap_strf("%.*s/%s", path_len, path_str, dir->d_name); - struct stat sb; - if (stat(child_str, &sb) != 0) - continue; - if (!((sb.st_mode & S_IFMT) & filter)) - continue; - - Path_t child = Text$format("%s%s", child_str, ((sb.st_mode & S_IFMT) == S_IFDIR) ? "/" : ""); // Trailing slash for dirs - Array$insert(&children, &child, I(0), sizeof(Path_t)); - } - closedir(d); - return children; -} - -public Array_t Path$children(Path_t path, bool include_hidden) -{ - return _filtered_children(path, include_hidden, (mode_t)-1); -} - -public Array_t Path$files(Path_t path, bool include_hidden) -{ - return _filtered_children(path, include_hidden, S_IFREG); -} - -public Array_t Path$subdirectories(Path_t path, bool include_hidden) -{ - return _filtered_children(path, include_hidden, S_IFDIR); -} - -public Path_t Path$unique_directory(Path_t path) -{ - path = Path$_expand_home(path); - const char *path_str = Text$as_c_string(path); - size_t len = strlen(path_str); - if (len >= PATH_MAX) fail("Path is too long: %s", path_str); - char buf[PATH_MAX] = {}; - strcpy(buf, path_str); - if (buf[len-1] == '/') - buf[--len] = '\0'; - char *created = mkdtemp(buf); - if (!created) fail("Failed to create temporary directory: %s (%s)", path_str, strerror(errno)); - return Text$format("%s/", created); -} - -public Text_t Path$write_unique(Path_t path, Text_t text) -{ - path = Path$_expand_home(path); - const char *path_str = Text$as_c_string(path); - size_t len = strlen(path_str); - if (len >= PATH_MAX) fail("Path is too long: %s", path_str); - char buf[PATH_MAX] = {}; - strcpy(buf, path_str); - - int64_t suffixlen = 0; - (void)Text$find(path, Pattern("{0+!X}{end}"), I(1), &suffixlen); - if (suffixlen < 0) suffixlen = 0; - - int fd = mkstemps(buf, suffixlen); - if (fd == -1) - fail("Could not write to unique file: %s\n%s", buf, strerror(errno)); - - const char *str = Text$as_c_string(text); - size_t write_len = strlen(str); - ssize_t written = write(fd, str, write_len); - if (written != (ssize_t)write_len) - fail("Could not write to file: %s\n%s", buf, strerror(errno)); - return Text$format("%s", buf); -} - -public Path_t Path$parent(Path_t path) -{ - return Path$cleanup(Text$concat(path, Path("/../"))); -} - -public Text_t Path$base_name(Path_t path) -{ - path = Path$cleanup(path); - if (Text$ends_with(path, Path("/"))) - return Text$replace(path, Pattern("{0+..}/{!/}/{end}"), Text("@2"), Text("@"), false); - else - return Text$replace(path, Pattern("{0+..}/{!/}{end}"), Text("@2"), Text("@"), false); -} - -public Text_t Path$extension(Path_t path, bool full) -{ - Text_t base = Path$base_name(path); - if (Text$matches(base, Pattern(".{!.}.{..}"))) - return Text$replace(base, full ? Pattern(".{!.}.{..}") : Pattern(".{..}.{!.}{end}"), Text("@2"), Text("@"), false); - else if (Text$matches(base, Pattern("{!.}.{..}"))) - return Text$replace(base, full ? Pattern("{!.}.{..}") : Pattern("{..}.{!.}{end}"), Text("@2"), Text("@"), false); - else - return Text(""); -} - -static void _line_reader_cleanup(FILE **f) -{ - if (f && *f) { - fclose(*f); - *f = NULL; - } -} - -static Text_t _next_line(FILE **f) -{ - if (!f || !*f) return NULL_TEXT; - - char *line = NULL; - size_t size = 0; - ssize_t len = getline(&line, &size, *f); - if (len <= 0) { - _line_reader_cleanup(f); - return NULL_TEXT; - } - - while (len > 0 && (line[len-1] == '\r' || line[len-1] == '\n')) - --len; - - if (u8_check((uint8_t*)line, (size_t)len) != NULL) - fail("Invalid UTF8!"); - - Text_t line_text = Text$format("%.*s", len, line); - free(line); - return line_text; -} - -public Closure_t Path$by_line(Path_t path) -{ - path = Path$_expand_home(path); - - FILE *f = fopen(Text$as_c_string(path), "r"); - if (f == NULL) - fail("Could not read file: %k (%s)", &path, strerror(errno)); - - FILE **wrapper = GC_MALLOC(sizeof(FILE*)); - *wrapper = f; - GC_register_finalizer(wrapper, (void*)_line_reader_cleanup, NULL, NULL, NULL); - return (Closure_t){.fn=(void*)_next_line, .userdata=wrapper}; -} - -public const TypeInfo Path$info = { - .size=sizeof(Path_t), - .align=__alignof__(Path_t), - .tag=TextInfo, - .TextInfo={.lang="Path"}, -}; - - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/path.h b/builtins/path.h deleted file mode 100644 index e0d85258..00000000 --- a/builtins/path.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -// A lang for filesystem paths - -#include -#include - -#include "types.h" -#include "datatypes.h" - -#define Path_t Text_t -#define Path(text) ((Path_t)Text(text)) -#define Paths(...) Path$_concat(sizeof((Path_t[]){__VA_ARGS__})/sizeof(Path_t), (Path_t[]){__VA_ARGS__}) - -Path_t Path$cleanup(Path_t path); -Path_t Path$_concat(int n, Path_t items[n]); -#define Path$concat(a, b) Paths(a, Path("/"), b) -PUREFUNC Path_t Path$escape_text(Text_t text); -PUREFUNC Path_t Path$escape_path(Text_t path); -Path_t Path$resolved(Path_t path, Path_t relative_to); -Path_t Path$relative(Path_t path, Path_t relative_to); -bool Path$exists(Path_t path); -bool Path$is_file(Path_t path, bool follow_symlinks); -bool Path$is_directory(Path_t path, bool follow_symlinks); -bool Path$is_pipe(Path_t path, bool follow_symlinks); -bool Path$is_socket(Path_t path, bool follow_symlinks); -bool Path$is_symlink(Path_t path); -void Path$write(Path_t path, Text_t text, int permissions); -void Path$append(Path_t path, Text_t text, int permissions); -Text_t Path$read(Path_t path); -void Path$remove(Path_t path, bool ignore_missing); -void Path$create_directory(Path_t path, int permissions); -Array_t Path$children(Path_t path, bool include_hidden); -Array_t Path$files(Path_t path, bool include_hidden); -Array_t Path$subdirectories(Path_t path, bool include_hidden); -Path_t Path$unique_directory(Path_t path); -Text_t Path$write_unique(Path_t path, Text_t text); -Path_t Path$parent(Path_t path); -Text_t Path$base_name(Path_t path); -Text_t Path$extension(Path_t path, bool full); -Closure_t Path$by_line(Path_t path); - -#define Path$hash Text$hash -#define Path$compare Text$compare -#define Path$equal Text$equal - -extern const TypeInfo Path$info; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 - diff --git a/builtins/paths.c b/builtins/paths.c new file mode 100644 index 00000000..231a7c23 --- /dev/null +++ b/builtins/paths.c @@ -0,0 +1,481 @@ +// A lang for filesystem paths +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arrays.h" +#include "files.h" +#include "integers.h" +#include "optionals.h" +#include "paths.h" +#include "patterns.h" +#include "text.h" +#include "types.h" +#include "util.h" + +PUREFUNC public Path_t Path$escape_text(Text_t text) +{ + if (Text$has(text, Pattern("/"))) + fail("Path interpolations cannot contain slashes: %k", &text); + else if (Text$has(text, Pattern(";"))) + fail("Path interpolations cannot contain semicolons: %k", &text); + else if (Text$equal_values(text, Path(".")) || Text$equal_values(text, Path(".."))) + fail("Path interpolation is \"%k\" which is disallowed to prevent security vulnerabilities", &text); + return (Path_t)text; +} + +PUREFUNC public Path_t Path$escape_path(Path_t path) +{ + if (Text$starts_with(path, Path("~/")) || Text$starts_with(path, Path("/"))) + fail("Invalid path component: %k", &path); + return path; +} + +public Path_t Path$cleanup(Path_t path) +{ + if (!Text$starts_with(path, Path("/")) && !Text$starts_with(path, Path("./")) + && !Text$starts_with(path, Path("../")) && !Text$starts_with(path, Path("~/"))) + path = Text$concat(Text("./"), path); + + // Not fully resolved, but at least get rid of some of the cruft like "/./" + // and "/foo/../" and "//" + bool trailing_slash = Text$ends_with(path, Path("/")); + Array_t components = Text$split(path, Pattern("/")); + if (components.length == 0) return Path("/"); + Path_t root = *(Path_t*)components.data; + Array$remove_at(&components, I(1), I(1), sizeof(Path_t)); + + for (int64_t i = 0; i < components.length; ) { + Path_t component = *(Path_t*)(components.data + i*components.stride); + if (component.length == 0 || Text$equal_values(component, Path("."))) { // Skip (//) and (/./) + Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); + } else if (Text$equal_values(component, Path(".."))) { + if (i == 0) { + if (root.length == 0) { // (/..) -> (/) + Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); + i += 1; + } else if (Text$equal_values(root, Path("."))) { // (./..) -> (..) + root = Path(".."); + Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); + i += 1; + } else if (Text$equal_values(root, Path("~"))) { + root = Path(""); // Convert $HOME to absolute path: + + Array$remove_at(&components, I(i+1), I(1), sizeof(Path_t)); + // `i` is pointing to where the `..` lived + + const char *home = getenv("HOME"); + if (!home) fail("Could not get $HOME directory!"); + + // Insert all but the last component: + for (const char *p = home + 1; *p; ) { + const char *next_slash = strchr(p, '/'); + if (!next_slash) break; // Skip last component + Path_t home_component = Text$format("%.*s", (int)(next_slash - p), p); + Array$insert(&components, &home_component, I(i+1), sizeof(Path_t)); + i += 1; + p = next_slash + 1; + } + } else { // (../..) -> (../..) + i += 1; + } + } else if (Text$equal(&component, (Path_t*)(components.data + (i-1)*components.stride))) { // (___/../..) -> (____/../..) + i += 1; + } else { // (___/foo/..) -> (___) + Array$remove_at(&components, I(i), I(2), sizeof(Path_t)); + i -= 1; + } + } else { // (___/foo/baz) -> (___/foo/baz) + i++; + } + } + + Text_t cleaned_up = Text$concat(root, Text("/"), Text$join(Text("/"), components)); + if (trailing_slash && !Text$ends_with(cleaned_up, Text("/"))) + cleaned_up = Text$concat(cleaned_up, Text("/")); + return cleaned_up; +} + +static inline Path_t Path$_expand_home(Path_t path) +{ + if (Text$starts_with(path, Path("~/"))) { + Path_t after_tilde = Text$slice(path, I(2), I(-1)); + return Text$format("%s%k", getenv("HOME"), &after_tilde); + } else { + return path; + } +} + +public Path_t Path$_concat(int n, Path_t items[n]) +{ + Path_t cleaned_up = Path$cleanup(Text$_concat(n, items)); + if (cleaned_up.length > PATH_MAX) + fail("Path exceeds the maximum path length: %k", &cleaned_up); + return cleaned_up; +} + +public Text_t Path$resolved(Path_t path, Path_t relative_to) +{ + path = Path$cleanup(path); + + const char *path_str = Text$as_c_string(path); + const char *relative_to_str = Text$as_c_string(relative_to); + const char *resolved_path = resolve_path(path_str, relative_to_str, relative_to_str); + if (resolved_path) { + return (Path_t)(Text$from_str(resolved_path)); + } else if (path_str[0] == '/') { + return path; + } else if (path_str[0] == '~' && path_str[1] == '/') { + return (Path_t)Text$format("%s%s", getenv("HOME"), path_str + 1); + } else { + return Text$concat(Path$resolved(relative_to, Path(".")), Path("/"), path); + } +} + +public Text_t Path$relative(Path_t path, Path_t relative_to) +{ + path = Path$resolved(path, relative_to); + relative_to = Path$resolved(relative_to, Path(".")); + if (Text$matches(path, Patterns(Pattern("{start}"), relative_to, Pattern("{0+..}")))) + return Text$slice(path, I(relative_to.length + 2), I(-1)); + return path; +} + +public bool Path$exists(Path_t path) +{ + path = Path$_expand_home(path); + struct stat sb; + return (stat(Text$as_c_string(path), &sb) == 0); +} + +public bool Path$is_file(Path_t path, bool follow_symlinks) +{ + path = Path$_expand_home(path); + struct stat sb; + const char *path_str = Text$as_c_string(path); + int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); + if (status != 0) return false; + return (sb.st_mode & S_IFMT) == S_IFREG; +} + +public bool Path$is_directory(Path_t path, bool follow_symlinks) +{ + path = Path$_expand_home(path); + struct stat sb; + const char *path_str = Text$as_c_string(path); + int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); + if (status != 0) return false; + return (sb.st_mode & S_IFMT) == S_IFDIR; +} + +public bool Path$is_pipe(Path_t path, bool follow_symlinks) +{ + path = Path$_expand_home(path); + struct stat sb; + const char *path_str = Text$as_c_string(path); + int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); + if (status != 0) return false; + return (sb.st_mode & S_IFMT) == S_IFIFO; +} + +public bool Path$is_socket(Path_t path, bool follow_symlinks) +{ + path = Path$_expand_home(path); + struct stat sb; + const char *path_str = Text$as_c_string(path); + int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb); + if (status != 0) return false; + return (sb.st_mode & S_IFMT) == S_IFSOCK; +} + +public bool Path$is_symlink(Path_t path) +{ + path = Path$_expand_home(path); + struct stat sb; + const char *path_str = Text$as_c_string(path); + int status = stat(path_str, &sb); + if (status != 0) return false; + return (sb.st_mode & S_IFMT) == S_IFLNK; +} + +static void _write(Path_t path, Text_t text, int mode, int permissions) +{ + path = Path$_expand_home(path); + const char *path_str = Text$as_c_string(path); + int fd = open(path_str, mode, permissions); + if (fd == -1) + fail("Could not write to file: %s\n%s", path_str, strerror(errno)); + + const char *str = Text$as_c_string(text); + size_t len = strlen(str); + ssize_t written = write(fd, str, len); + if (written != (ssize_t)len) + fail("Could not write to file: %s\n%s", path_str, strerror(errno)); +} + +public void Path$write(Path_t path, Text_t text, int permissions) +{ + _write(path, text, O_WRONLY | O_CREAT, permissions); +} + +public void Path$append(Path_t path, Text_t text, int permissions) +{ + _write(path, text, O_WRONLY | O_APPEND | O_CREAT, permissions); +} + +public Text_t Path$read(Path_t path) +{ + path = Path$_expand_home(path); + int fd = open(Text$as_c_string(path), O_RDONLY); + if (fd == -1) + fail("Could not read file: %k (%s)", &path, strerror(errno)); + + struct stat sb; + if (fstat(fd, &sb) != 0) + fail("Could not read file: %k (%s)", &path, strerror(errno)); + + if ((sb.st_mode & S_IFMT) == S_IFREG) { // Use memory mapping if it's a real file: + const char *mem = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + char *gc_mem = GC_MALLOC_ATOMIC((size_t)sb.st_size+1); + memcpy(gc_mem, mem, (size_t)sb.st_size); + gc_mem[sb.st_size] = '\0'; + close(fd); + return Text$from_strn(gc_mem, (size_t)sb.st_size); + } else { + size_t capacity = 256, len = 0; + char *content = GC_MALLOC_ATOMIC(capacity); + for (;;) { + char chunk[256]; + ssize_t just_read = read(fd, chunk, sizeof(chunk)); + if (just_read < 0) + fail("Failed while reading file: %k (%s)", &path, strerror(errno)); + else if (just_read == 0) { + if (errno == EAGAIN || errno == EINTR) + continue; + break; + } + + if (len + (size_t)just_read >= capacity) { + content = GC_REALLOC(content, (capacity *= 2)); + } + + memcpy(&content[len], chunk, (size_t)just_read); + len += (size_t)just_read; + + if ((size_t)just_read < sizeof(chunk)) + break; + } + close(fd); + + if (u8_check((uint8_t*)content, len) != NULL) + fail("File does not contain valid UTF8 data!"); + + return Text$from_strn(content, len); + } +} + +public void Path$remove(Path_t path, bool ignore_missing) +{ + path = Path$_expand_home(path); + const char *path_str = Text$as_c_string(path); + struct stat sb; + if (lstat(path_str, &sb) != 0) { + if (!ignore_missing) + fail("Could not remove file: %s (%s)", path_str, strerror(errno)); + } + + if ((sb.st_mode & S_IFMT) == S_IFREG || (sb.st_mode & S_IFMT) == S_IFLNK) { + if (unlink(path_str) != 0 && !ignore_missing) + fail("Could not remove file: %s (%s)", path_str, strerror(errno)); + } else if ((sb.st_mode & S_IFMT) == S_IFDIR) { + if (rmdir(path_str) != 0 && !ignore_missing) + fail("Could not remove directory: %s (%s)", path_str, strerror(errno)); + } else { + fail("Could not remove path: %s (not a file or directory)", path_str, strerror(errno)); + } +} + +public void Path$create_directory(Path_t path, int permissions) +{ + path = Path$_expand_home(path); + if (mkdir(Text$as_c_string(path), (mode_t)permissions) != 0) + fail("Could not create directory: %k (%s)", &path, strerror(errno)); +} + +static Array_t _filtered_children(Path_t path, bool include_hidden, mode_t filter) +{ + path = Path$_expand_home(path); + struct dirent *dir; + Array_t children = {}; + const char *path_str = Text$as_c_string(path); + size_t path_len = strlen(path_str); + DIR *d = opendir(path_str); + if (!d) + fail("Could not open directory: %k (%s)", &path, strerror(errno)); + + if (path_str[path_len-1] == '/') + --path_len; + + while ((dir = readdir(d)) != NULL) { + if (!include_hidden && dir->d_name[0] == '.') + continue; + if (streq(dir->d_name, ".") || streq(dir->d_name, "..")) + continue; + + const char *child_str = heap_strf("%.*s/%s", path_len, path_str, dir->d_name); + struct stat sb; + if (stat(child_str, &sb) != 0) + continue; + if (!((sb.st_mode & S_IFMT) & filter)) + continue; + + Path_t child = Text$format("%s%s", child_str, ((sb.st_mode & S_IFMT) == S_IFDIR) ? "/" : ""); // Trailing slash for dirs + Array$insert(&children, &child, I(0), sizeof(Path_t)); + } + closedir(d); + return children; +} + +public Array_t Path$children(Path_t path, bool include_hidden) +{ + return _filtered_children(path, include_hidden, (mode_t)-1); +} + +public Array_t Path$files(Path_t path, bool include_hidden) +{ + return _filtered_children(path, include_hidden, S_IFREG); +} + +public Array_t Path$subdirectories(Path_t path, bool include_hidden) +{ + return _filtered_children(path, include_hidden, S_IFDIR); +} + +public Path_t Path$unique_directory(Path_t path) +{ + path = Path$_expand_home(path); + const char *path_str = Text$as_c_string(path); + size_t len = strlen(path_str); + if (len >= PATH_MAX) fail("Path is too long: %s", path_str); + char buf[PATH_MAX] = {}; + strcpy(buf, path_str); + if (buf[len-1] == '/') + buf[--len] = '\0'; + char *created = mkdtemp(buf); + if (!created) fail("Failed to create temporary directory: %s (%s)", path_str, strerror(errno)); + return Text$format("%s/", created); +} + +public Text_t Path$write_unique(Path_t path, Text_t text) +{ + path = Path$_expand_home(path); + const char *path_str = Text$as_c_string(path); + size_t len = strlen(path_str); + if (len >= PATH_MAX) fail("Path is too long: %s", path_str); + char buf[PATH_MAX] = {}; + strcpy(buf, path_str); + + int64_t suffixlen = 0; + (void)Text$find(path, Pattern("{0+!X}{end}"), I(1), &suffixlen); + if (suffixlen < 0) suffixlen = 0; + + int fd = mkstemps(buf, suffixlen); + if (fd == -1) + fail("Could not write to unique file: %s\n%s", buf, strerror(errno)); + + const char *str = Text$as_c_string(text); + size_t write_len = strlen(str); + ssize_t written = write(fd, str, write_len); + if (written != (ssize_t)write_len) + fail("Could not write to file: %s\n%s", buf, strerror(errno)); + return Text$format("%s", buf); +} + +public Path_t Path$parent(Path_t path) +{ + return Path$cleanup(Text$concat(path, Path("/../"))); +} + +public Text_t Path$base_name(Path_t path) +{ + path = Path$cleanup(path); + if (Text$ends_with(path, Path("/"))) + return Text$replace(path, Pattern("{0+..}/{!/}/{end}"), Text("@2"), Text("@"), false); + else + return Text$replace(path, Pattern("{0+..}/{!/}{end}"), Text("@2"), Text("@"), false); +} + +public Text_t Path$extension(Path_t path, bool full) +{ + Text_t base = Path$base_name(path); + if (Text$matches(base, Pattern(".{!.}.{..}"))) + return Text$replace(base, full ? Pattern(".{!.}.{..}") : Pattern(".{..}.{!.}{end}"), Text("@2"), Text("@"), false); + else if (Text$matches(base, Pattern("{!.}.{..}"))) + return Text$replace(base, full ? Pattern("{!.}.{..}") : Pattern("{..}.{!.}{end}"), Text("@2"), Text("@"), false); + else + return Text(""); +} + +static void _line_reader_cleanup(FILE **f) +{ + if (f && *f) { + fclose(*f); + *f = NULL; + } +} + +static Text_t _next_line(FILE **f) +{ + if (!f || !*f) return NULL_TEXT; + + char *line = NULL; + size_t size = 0; + ssize_t len = getline(&line, &size, *f); + if (len <= 0) { + _line_reader_cleanup(f); + return NULL_TEXT; + } + + while (len > 0 && (line[len-1] == '\r' || line[len-1] == '\n')) + --len; + + if (u8_check((uint8_t*)line, (size_t)len) != NULL) + fail("Invalid UTF8!"); + + Text_t line_text = Text$format("%.*s", len, line); + free(line); + return line_text; +} + +public Closure_t Path$by_line(Path_t path) +{ + path = Path$_expand_home(path); + + FILE *f = fopen(Text$as_c_string(path), "r"); + if (f == NULL) + fail("Could not read file: %k (%s)", &path, strerror(errno)); + + FILE **wrapper = GC_MALLOC(sizeof(FILE*)); + *wrapper = f; + GC_register_finalizer(wrapper, (void*)_line_reader_cleanup, NULL, NULL, NULL); + return (Closure_t){.fn=(void*)_next_line, .userdata=wrapper}; +} + +public const TypeInfo Path$info = { + .size=sizeof(Path_t), + .align=__alignof__(Path_t), + .tag=TextInfo, + .TextInfo={.lang="Path"}, +}; + + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/paths.h b/builtins/paths.h new file mode 100644 index 00000000..e0d85258 --- /dev/null +++ b/builtins/paths.h @@ -0,0 +1,50 @@ +#pragma once + +// A lang for filesystem paths + +#include +#include + +#include "types.h" +#include "datatypes.h" + +#define Path_t Text_t +#define Path(text) ((Path_t)Text(text)) +#define Paths(...) Path$_concat(sizeof((Path_t[]){__VA_ARGS__})/sizeof(Path_t), (Path_t[]){__VA_ARGS__}) + +Path_t Path$cleanup(Path_t path); +Path_t Path$_concat(int n, Path_t items[n]); +#define Path$concat(a, b) Paths(a, Path("/"), b) +PUREFUNC Path_t Path$escape_text(Text_t text); +PUREFUNC Path_t Path$escape_path(Text_t path); +Path_t Path$resolved(Path_t path, Path_t relative_to); +Path_t Path$relative(Path_t path, Path_t relative_to); +bool Path$exists(Path_t path); +bool Path$is_file(Path_t path, bool follow_symlinks); +bool Path$is_directory(Path_t path, bool follow_symlinks); +bool Path$is_pipe(Path_t path, bool follow_symlinks); +bool Path$is_socket(Path_t path, bool follow_symlinks); +bool Path$is_symlink(Path_t path); +void Path$write(Path_t path, Text_t text, int permissions); +void Path$append(Path_t path, Text_t text, int permissions); +Text_t Path$read(Path_t path); +void Path$remove(Path_t path, bool ignore_missing); +void Path$create_directory(Path_t path, int permissions); +Array_t Path$children(Path_t path, bool include_hidden); +Array_t Path$files(Path_t path, bool include_hidden); +Array_t Path$subdirectories(Path_t path, bool include_hidden); +Path_t Path$unique_directory(Path_t path); +Text_t Path$write_unique(Path_t path, Text_t text); +Path_t Path$parent(Path_t path); +Text_t Path$base_name(Path_t path); +Text_t Path$extension(Path_t path, bool full); +Closure_t Path$by_line(Path_t path); + +#define Path$hash Text$hash +#define Path$compare Text$compare +#define Path$equal Text$equal + +extern const TypeInfo Path$info; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 + diff --git a/builtins/pattern.c b/builtins/pattern.c deleted file mode 100644 index 7e82ec13..00000000 --- a/builtins/pattern.c +++ /dev/null @@ -1,1064 +0,0 @@ -// Logic for text pattern matching - -#include -#include -#include -#include - -#include "array.h" -#include "integers.h" -#include "pattern.h" -#include "table.h" -#include "text.h" -#include "types.h" - -#define MAX_BACKREFS 100 - -static inline void skip_whitespace(Text_t text, int64_t *i) -{ - TextIter_t state = {0, 0}; - while (*i < text.length) { - int32_t grapheme = Text$get_grapheme_fast(text, &state, *i); - if (grapheme > 0 && !uc_is_property_white_space((ucs4_t)grapheme)) - return; - *i += 1; - } -} - -static inline bool match_grapheme(Text_t text, int64_t *i, int32_t grapheme) -{ - if (*i < text.length && Text$get_grapheme(text, *i) == grapheme) { - *i += 1; - return true; - } - return false; -} - -static inline bool match_str(Text_t text, int64_t *i, const char *str) -{ - TextIter_t state = {0, 0}; - int64_t matched = 0; - while (matched[str]) { - if (*i + matched >= text.length || Text$get_grapheme_fast(text, &state, *i + matched) != str[matched]) - return false; - matched += 1; - } - *i += matched; - return true; -} - -static inline bool match_property(Text_t text, int64_t *i, uc_property_t prop) -{ - if (*i >= text.length) return false; - TextIter_t state = {}; - ucs4_t grapheme = Text$get_main_grapheme_fast(text, &state, *i); - // TODO: check every codepoint in the cluster? - if (uc_is_property(grapheme, prop)) { - *i += 1; - return true; - } - return false; -} - -static int64_t parse_int(Text_t text, int64_t *i) -{ - TextIter_t state = {0, 0}; - int64_t value = 0; - for (;; *i += 1) { - ucs4_t grapheme = Text$get_main_grapheme_fast(text, &state, *i); - int digit = uc_digit_value((ucs4_t)grapheme); - if (digit < 0) break; - if (value >= INT64_MAX/10) break; - value = 10*value + digit; - } - return value; -} - -const char *get_property_name(Text_t text, int64_t *i) -{ - skip_whitespace(text, i); - char *name = GC_MALLOC_ATOMIC(UNINAME_MAX); - char *dest = name; - TextIter_t state = {0, 0}; - while (*i < text.length) { - int32_t grapheme = Text$get_grapheme_fast(text, &state, *i); - if (!(grapheme & ~0xFF) && (isalnum(grapheme) || grapheme == ' ' || grapheme == '_' || grapheme == '-')) { - *dest = (char)grapheme; - ++dest; - if (dest >= name + UNINAME_MAX - 1) - break; - } else { - break; - } - *i += 1; - } - - while (dest > name && dest[-1] == ' ') - *(dest--) = '\0'; - - if (dest == name) return NULL; - *dest = '\0'; - return name; -} - -#define EAT1(text, state, index, cond) ({\ - int32_t grapheme = Text$get_grapheme_fast(text, state, index); \ - bool success = (cond); \ - if (success) index += 1; \ - success; }) - -#define EAT2(text, state, index, cond1, cond2) ({\ - int32_t grapheme = Text$get_grapheme_fast(text, state, index); \ - bool success = (cond1); \ - if (success) { \ - grapheme = Text$get_grapheme_fast(text, state, index + 1); \ - success = (cond2); \ - if (success) \ - index += 2; \ - } \ - success; }) - - -#define EAT_MANY(text, state, index, cond) ({ int64_t _n = 0; while (EAT1(text, state, index, cond)) { _n += 1; } _n; }) - -int64_t match_email(Text_t text, int64_t index) -{ - // email = local "@" domain - // local = 1-64 ([a-zA-Z0-9!#$%&‘*+–/=?^_`.{|}~] | non-ascii) - // domain = dns-label ("." dns-label)* - // dns-label = 1-63 ([a-zA-Z0-9-] | non-ascii) - - TextIter_t state = {0, 0}; - if (index > 0) { - ucs4_t prev_codepoint = Text$get_main_grapheme_fast(text, &state, index - 1); - if (uc_is_property_alphabetic((ucs4_t)prev_codepoint)) - return -1; - } - - int64_t start_index = index; - - // Local part: - int64_t local_len = 0; - static const char *allowed_local = "!#$%&‘*+–/=?^_`.{|}~"; - while (EAT1(text, &state, index, - (grapheme & ~0x7F) || isalnum((char)grapheme) || strchr(allowed_local, (char)grapheme))) { - local_len += 1; - if (local_len > 64) return -1; - } - - if (!EAT1(text, &state, index, grapheme == '@')) - return -1; - - // Host - int64_t host_len = 0; - do { - int64_t label_len = 0; - while (EAT1(text, &state, index, - (grapheme & ~0x7F) || isalnum((char)grapheme) || grapheme == '-')) { - label_len += 1; - if (label_len > 63) return -1; - } - - if (label_len == 0) - return -1; - - host_len += label_len; - if (host_len > 255) - return -1; - host_len += 1; - } while (EAT1(text, &state, index, grapheme == '.')); - - return index - start_index; -} - -int64_t match_ipv6(Text_t text, int64_t index) -{ - TextIter_t state = {0, 0}; - if (index > 0) { - int32_t prev_codepoint = Text$get_grapheme_fast(text, &state, index - 1); - if ((prev_codepoint & ~0x7F) && (isxdigit(prev_codepoint) || prev_codepoint == ':')) - return -1; - } - int64_t start_index = index; - const int NUM_CLUSTERS = 8; - bool double_colon_used = false; - for (int cluster = 0; cluster < NUM_CLUSTERS; cluster++) { - for (int digits = 0; digits < 4; digits++) { - if (!EAT1(text, &state, index, ~(grapheme & ~0x7F) && isxdigit((char)grapheme))) - break; - } - if (EAT1(text, &state, index, ~(grapheme & ~0x7F) && isxdigit((char)grapheme))) - return -1; // Too many digits - - if (cluster == NUM_CLUSTERS-1) { - break; - } else if (!EAT1(text, &state, index, grapheme == ':')) { - if (double_colon_used) - break; - return -1; - } - - if (EAT1(text, &state, index, grapheme == ':')) { - if (double_colon_used) - return -1; - double_colon_used = true; - } - } - return index - start_index; -} - -static int64_t match_ipv4(Text_t text, int64_t index) -{ - TextIter_t state = {0, 0}; - if (index > 0) { - int32_t prev_codepoint = Text$get_grapheme_fast(text, &state, index - 1); - if ((prev_codepoint & ~0x7F) && (isdigit(prev_codepoint) || prev_codepoint == '.')) - return -1; - } - int64_t start_index = index; - - const int NUM_CLUSTERS = 4; - for (int cluster = 0; cluster < NUM_CLUSTERS; cluster++) { - for (int digits = 0; digits < 3; digits++) { - if (!EAT1(text, &state, index, ~(grapheme & ~0x7F) && isdigit((char)grapheme))) { - if (digits == 0) return -1; - break; - } - } - - if (EAT1(text, &state, index, ~(grapheme & ~0x7F) && isdigit((char)grapheme))) - return -1; // Too many digits - - if (cluster == NUM_CLUSTERS-1) - break; - else if (!EAT1(text, &state, index, grapheme == '.')) - return -1; - } - return (index - start_index); -} - -int64_t match_ip(Text_t text, int64_t index) -{ - int64_t len = match_ipv6(text, index); - if (len >= 0) return len; - len = match_ipv4(text, index); - return (len >= 0) ? len : -1; -} - -int64_t match_uri(Text_t text, int64_t index) -{ - // URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment] - // scheme = [a-zA-Z] [a-zA-Z0-9+.-] - // authority = [userinfo "@"] host [":" port] - - TextIter_t state = {0, 0}; - if (index > 0) { - ucs4_t prev_codepoint = Text$get_main_grapheme_fast(text, &state, index - 1); - if (uc_is_property_alphabetic(prev_codepoint)) - return -1; - } - - int64_t start_index = index; - - // Scheme: - if (!EAT1(text, &state, index, isalpha(grapheme))) - return -1; - - EAT_MANY(text, &state, index, - !(grapheme & ~0x7F) && (isalnum(grapheme) || grapheme == '+' || grapheme == '.' || grapheme == '-')); - - if (index == start_index) - return -1; - - if (!match_grapheme(text, &index, ':')) - return -1; - - // Authority: - if (match_str(text, &index, "//")) { - int64_t authority_start = index; - // Username or host: - static const char *forbidden = "#?:@ \t\r\n<>[]{}\\^|\"`/"; - if (EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(forbidden, (char)grapheme)) == 0) - return -1; - - if (EAT1(text, &state, index, grapheme == '@')) { - // Found a username, now get a host: - if (EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(forbidden, (char)grapheme)) == 0) - return -1; - } else { - int64_t ip = authority_start; - int64_t ipv4_len = match_ipv4(text, ip); - if (ipv4_len > 0) { - ip += ipv4_len; - } else if (match_grapheme(text, &ip, '[')) { - ip += match_ipv6(text, ip); - if (ip > authority_start + 1 && match_grapheme(text, &ip, ']')) - index = ip; - } - } - - // Port: - if (EAT1(text, &state, index, grapheme == ':')) { - if (EAT_MANY(text, &state, index, !(grapheme & ~0x7F) && isdigit(grapheme)) == 0) - return -1; - } - if (!EAT1(text, &state, index, grapheme == '/')) - return (index - start_index); // No path - } else { - // Optional path root: - EAT1(text, &state, index, grapheme == '/'); - } - - // Path: - static const char *non_path = " \"#?<>[]{}\\^`|"; - EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(non_path, (char)grapheme)); - - if (EAT1(text, &state, index, grapheme == '?')) { // Query - static const char *non_query = " \"#<>[]{}\\^`|"; - EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(non_query, (char)grapheme)); - } - - if (EAT1(text, &state, index, grapheme == '#')) { // Fragment - static const char *non_fragment = " \"#<>[]{}\\^`|"; - EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(non_fragment, (char)grapheme)); - } - return index - start_index; -} - -int64_t match_url(Text_t text, int64_t index) -{ - int64_t lookahead = index; - if (!(match_str(text, &lookahead, "https:") - || match_str(text, &lookahead, "http:") - || match_str(text, &lookahead, "ftp:") - || match_str(text, &lookahead, "wss:") - || match_str(text, &lookahead, "ws:"))) - return -1; - - return match_uri(text, index); -} - -int64_t match_id(Text_t text, int64_t index) -{ - TextIter_t state = {0, 0}; - if (!EAT1(text, &state, index, uc_is_property((ucs4_t)grapheme, UC_PROPERTY_XID_START))) - return -1; - return 1 + EAT_MANY(text, &state, index, uc_is_property((ucs4_t)grapheme, UC_PROPERTY_XID_CONTINUE)); -} - -int64_t match_int(Text_t text, int64_t index) -{ - TextIter_t state = {0, 0}; - int64_t len = EAT_MANY(text, &state, index, uc_is_property((ucs4_t)grapheme, UC_PROPERTY_DECIMAL_DIGIT)); - return len >= 0 ? len : -1; -} - -int64_t match_num(Text_t text, int64_t index) -{ - TextIter_t state = {0, 0}; - bool negative = EAT1(text, &state, index, grapheme == '-') ? 1 : 0; - int64_t pre_decimal = EAT_MANY(text, &state, index, - uc_is_property((ucs4_t)grapheme, UC_PROPERTY_DECIMAL_DIGIT)); - bool decimal = (EAT1(text, &state, index, grapheme == '.') == 1); - int64_t post_decimal = decimal ? EAT_MANY(text, &state, index, - uc_is_property((ucs4_t)grapheme, UC_PROPERTY_DECIMAL_DIGIT)) : 0; - if (pre_decimal == 0 && post_decimal == 0) - return -1; - return negative + pre_decimal + decimal + post_decimal; -} - -int64_t match_newline(Text_t text, int64_t index) -{ - if (index >= text.length) - return -1; - - TextIter_t state = {0, 0}; - ucs4_t grapheme = index >= text.length ? 0 : Text$get_main_grapheme_fast(text, &state, index); - if (grapheme == '\n') - return 1; - if (grapheme == '\r' && Text$get_grapheme_fast(text, &state, index + 1) == '\n') - return 2; - return -1; -} - -typedef struct { - int64_t index, length; - bool occupied, recursive; -} capture_t; - -typedef struct { - enum { PAT_START, PAT_END, PAT_ANY, PAT_GRAPHEME, PAT_PROPERTY, PAT_QUOTE, PAT_PAIR, PAT_FUNCTION } tag; - bool negated, non_capturing; - int64_t min, max; - union { - int32_t grapheme; - uc_property_t property; - int64_t (*fn)(Text_t, int64_t); - int32_t quote_graphemes[2]; - int32_t pair_graphemes[2]; - }; -} pat_t; - -int64_t match_pat(Text_t text, TextIter_t *state, int64_t index, pat_t pat) -{ - int32_t grapheme = index >= text.length ? 0 : Text$get_grapheme_fast(text, state, index); - - switch (pat.tag) { - case PAT_START: { - if (index == 0) - return pat.negated ? -1 : 0; - return pat.negated ? 0 : -1; - } - case PAT_END: { - if (index >= text.length) - return pat.negated ? -1 : 0; - return pat.negated ? 0 : -1; - } - case PAT_ANY: { - assert(!pat.negated); - return (index < text.length) ? 1 : -1; - } - case PAT_GRAPHEME: { - if (index >= text.length) - return -1; - else if (grapheme == pat.grapheme) - return pat.negated ? -1 : 1; - return pat.negated ? 1 : -1; - } - case PAT_PROPERTY: { - if (index >= text.length) - return -1; - else if (uc_is_property((ucs4_t)grapheme, pat.property)) - return pat.negated ? -1 : 1; - return pat.negated ? 1 : -1; - } - case PAT_PAIR: { - // Nested punctuation: (?), [?], etc - if (index >= text.length) - return -1; - - int32_t open = pat.pair_graphemes[0]; - if (grapheme != open) - return pat.negated ? 1 : -1; - - int32_t close = pat.pair_graphemes[1]; - int64_t depth = 1; - int64_t match_len = 1; - for (; depth > 0; match_len++) { - if (index + match_len >= text.length) - return pat.negated ? 1 : -1; - - int32_t c = Text$get_grapheme_fast(text, state, index + match_len); - if (c == open) - depth += 1; - else if (c == close) - depth -= 1; - } - return pat.negated ? -1 : match_len; - } - case PAT_QUOTE: { - // Nested quotes: "?", '?', etc - if (index >= text.length) - return -1; - - int32_t open = pat.quote_graphemes[0]; - if (grapheme != open) - return pat.negated ? 1 : -1; - - int32_t close = pat.quote_graphemes[1]; - for (int64_t i = index + 1; i < text.length; i++) { - int32_t c = Text$get_grapheme_fast(text, state, i); - if (c == close) { - return pat.negated ? -1 : (i - index) + 1; - } else if (c == '\\' && index + 1 < text.length) { - i += 1; // Skip ahead an extra step - } - } - return pat.negated ? 1 : -1; - } - case PAT_FUNCTION: { - int64_t match_len = pat.fn(text, index); - if (match_len >= 0) - return pat.negated ? -1 : match_len; - return pat.negated ? 1 : -1; - } - default: errx(1, "Invalid pattern"); - } - errx(1, "Unreachable"); -} - -pat_t parse_next_pat(Text_t pattern, TextIter_t *state, int64_t *index) -{ - if (EAT2(pattern, state, *index, - uc_is_property((ucs4_t)grapheme, UC_PROPERTY_QUOTATION_MARK), - grapheme == '?')) { - // Quotations: "?", '?', etc - int32_t open = Text$get_grapheme_fast(pattern, state, *index-2); - int32_t close = open; - uc_mirror_char((ucs4_t)open, (ucs4_t*)&close); - if (!match_grapheme(pattern, index, close)) - fail("Pattern's closing quote is missing: %k", &pattern); - - return (pat_t){ - .tag=PAT_QUOTE, - .min=1, .max=1, - .quote_graphemes={open, close}, - }; - } else if (EAT2(pattern, state, *index, - uc_is_property((ucs4_t)grapheme, UC_PROPERTY_PAIRED_PUNCTUATION), - grapheme == '?')) { - // Nested punctuation: (?), [?], etc - int32_t open = Text$get_grapheme_fast(pattern, state, *index-2); - int32_t close = open; - uc_mirror_char((ucs4_t)open, (ucs4_t*)&close); - if (!match_grapheme(pattern, index, close)) - fail("Pattern's closing brace is missing: %k", &pattern); - - return (pat_t){ - .tag=PAT_PAIR, - .min=1, .max=1, - .pair_graphemes={open, close}, - }; - } else if (EAT1(pattern, state, *index, - grapheme == '{')) { // named patterns {id}, {2-3 hex}, etc. - skip_whitespace(pattern, index); - int64_t min, max; - if (uc_is_digit((ucs4_t)Text$get_grapheme_fast(pattern, state, *index))) { - min = parse_int(pattern, index); - skip_whitespace(pattern, index); - if (match_grapheme(pattern, index, '+')) { - max = INT64_MAX; - } else if (match_grapheme(pattern, index, '-')) { - max = parse_int(pattern, index); - } else { - max = min; - } - if (min > max) fail("Minimum repetitions (%ld) is less than the maximum (%ld)", min, max); - } else { - min = -1, max = -1; - } - - skip_whitespace(pattern, index); - - bool negated = match_grapheme(pattern, index, '!'); -#define PAT(_tag, ...) ((pat_t){.min=min, .max=max, .negated=negated, .tag=_tag, __VA_ARGS__}) - const char *prop_name; - if (match_str(pattern, index, "..")) - prop_name = ".."; - else - prop_name = get_property_name(pattern, index); - - if (!prop_name) { - // Literal character, e.g. {1?} - skip_whitespace(pattern, index); - int32_t grapheme = Text$get_grapheme_fast(pattern, state, (*index)++); - if (!match_grapheme(pattern, index, '}')) - fail("Missing closing '}' in pattern: %k", &pattern); - return PAT(PAT_GRAPHEME, .grapheme=grapheme); - } else if (strlen(prop_name) == 1) { - // Single letter names: {1+ A} - skip_whitespace(pattern, index); - if (!match_grapheme(pattern, index, '}')) - fail("Missing closing '}' in pattern: %k", &pattern); - return PAT(PAT_GRAPHEME, .grapheme=prop_name[0]); - } - - skip_whitespace(pattern, index); - if (!match_grapheme(pattern, index, '}')) - fail("Missing closing '}' in pattern: %k", &pattern); - - switch (tolower(prop_name[0])) { - case '.': - if (prop_name[1] == '.') { - if (negated) - return ((pat_t){.tag=PAT_END, .min=min, .max=max, .non_capturing=true}); - else - return PAT(PAT_ANY); - } - break; - case 'd': - if (strcasecmp(prop_name, "digit") == 0) { - return PAT(PAT_PROPERTY, .property=UC_PROPERTY_DECIMAL_DIGIT); - } - break; - case 'e': - if (strcasecmp(prop_name, "end") == 0) { - return PAT(PAT_END, .non_capturing=!negated); - } else if (strcasecmp(prop_name, "email") == 0) { - return PAT(PAT_FUNCTION, .fn=match_email); - } else if (strcasecmp(prop_name, "emoji") == 0) { - return PAT(PAT_PROPERTY, .property=UC_PROPERTY_EMOJI); - } - break; - case 'i': - if (strcasecmp(prop_name, "id") == 0) { - return PAT(PAT_FUNCTION, .fn=match_id); - } else if (strcasecmp(prop_name, "int") == 0) { - return PAT(PAT_FUNCTION, .fn=match_int); - } else if (strcasecmp(prop_name, "ipv4") == 0) { - return PAT(PAT_FUNCTION, .fn=match_ipv4); - } else if (strcasecmp(prop_name, "ipv6") == 0) { - return PAT(PAT_FUNCTION, .fn=match_ipv6); - } else if (strcasecmp(prop_name, "ip") == 0) { - return PAT(PAT_FUNCTION, .fn=match_ip); - } - break; - case 'n': - if (strcasecmp(prop_name, "nl") == 0 || strcasecmp(prop_name, "newline") == 0 - || strcasecmp(prop_name, "crlf")) { - return PAT(PAT_FUNCTION, .fn=match_newline); - } else if (strcasecmp(prop_name, "num") == 0) { - return PAT(PAT_FUNCTION, .fn=match_num); - } - break; - case 's': - if (strcasecmp(prop_name, "start") == 0) { - return PAT(PAT_START, .non_capturing=!negated); - } - break; - case 'u': - if (strcasecmp(prop_name, "uri") == 0) { - return PAT(PAT_FUNCTION, .fn=match_uri); - } else if (strcasecmp(prop_name, "url") == 0) { - return PAT(PAT_FUNCTION, .fn=match_url); - } - break; - default: break; - } - - uc_property_t prop = uc_property_byname(prop_name); - if (uc_property_is_valid(prop)) - return PAT(PAT_PROPERTY, .property=prop); - - ucs4_t grapheme = unicode_name_character(prop_name); - if (grapheme == UNINAME_INVALID) - fail("Not a valid property or character name: %s", prop_name); - return PAT(PAT_GRAPHEME, .grapheme=(int32_t)grapheme); -#undef PAT - } else { - return (pat_t){.tag=PAT_GRAPHEME, .non_capturing=true, .min=1, .max=1, .grapheme=Text$get_grapheme_fast(pattern, state, (*index)++)}; - } -} - -int64_t match(Text_t text, int64_t text_index, Pattern_t pattern, int64_t pattern_index, capture_t *captures, int64_t capture_index) -{ - if (pattern_index >= pattern.length) // End of the pattern - return 0; - - int64_t start_index = text_index; - TextIter_t pattern_state = {0, 0}, text_state = {0, 0}; - pat_t pat = parse_next_pat(pattern, &pattern_state, &pattern_index); - - if (pat.min == -1 && pat.max == -1) { - if (pat.tag == PAT_ANY && pattern_index >= pattern.length) { - pat.min = pat.max = MAX(1, text.length - text_index); - } else { - pat.min = 1; - pat.max = INT64_MAX; - } - } - - int64_t capture_start = text_index; - int64_t count = 0, capture_len = 0, next_match_len = 0; - - if (pat.tag == PAT_ANY && pattern_index >= pattern.length) { - int64_t remaining = text.length - text_index; - capture_len = remaining >= pat.min ? MIN(remaining, pat.max) : -1; - text_index += capture_len; - goto success; - } - - if (pat.min == 0 && pattern_index < pattern.length) { - next_match_len = match(text, text_index, pattern, pattern_index, captures, capture_index + (pat.non_capturing ? 0 : 1)); - if (next_match_len >= 0) { - capture_len = 0; - goto success; - } - } - - while (count < pat.max) { - int64_t match_len = match_pat(text, &text_state, text_index, pat); - if (match_len < 0) - break; - capture_len += match_len; - text_index += match_len; - count += 1; - - if (pattern_index < pattern.length) { // More stuff after this - if (count < pat.min) - next_match_len = -1; - else - next_match_len = match(text, text_index, pattern, pattern_index, captures, capture_index + (pat.non_capturing ? 0 : 1)); - } else { - next_match_len = 0; - } - - if (match_len == 0) { - if (next_match_len >= 0) { - // If we're good to go, no need to keep re-matching zero-length - // matches till we hit max: - count = pat.max; - break; - } else { - return -1; - } - } - - if (pattern_index < pattern.length && next_match_len >= 0) - break; // Next guy exists and wants to stop here - - if (text_index >= text.length) - break; - } - - if (count < pat.min || next_match_len < 0) - return -1; - - success: - if (captures && capture_index < MAX_BACKREFS && !pat.non_capturing) { - if (pat.tag == PAT_PAIR || pat.tag == PAT_QUOTE) { - assert(capture_len > 0); - captures[capture_index] = (capture_t){ - .index=capture_start + 1, // Skip leading quote/paren - .length=capture_len - 2, // Skip open/close - .occupied=true, - .recursive=(pat.tag == PAT_PAIR), - }; - } else { - captures[capture_index] = (capture_t){ - .index=capture_start, - .length=capture_len, - .occupied=true, - .recursive=false, - }; - } - } - return (text_index - start_index) + next_match_len; -} - -#undef EAT1 -#undef EAT2 -#undef EAT_MANY - -static int64_t _find(Text_t text, Pattern_t pattern, int64_t first, int64_t last, int64_t *match_length) -{ - int32_t first_grapheme = Text$get_grapheme(pattern, 0); - bool find_first = (first_grapheme != '{' - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); - - TextIter_t text_state = {0, 0}; - - for (int64_t i = first; i <= last; i++) { - // Optimization: quickly skip ahead to first char in pattern: - if (find_first) { - while (i < text.length && Text$get_grapheme_fast(text, &text_state, i) != first_grapheme) - ++i; - } - - int64_t m = match(text, i, pattern, 0, NULL, 0); - if (m >= 0) { - if (match_length) - *match_length = m; - return i; - } - } - if (match_length) - *match_length = -1; - return -1; -} - -public Int_t Text$find(Text_t text, Pattern_t pattern, Int_t from_index, int64_t *match_length) -{ - int64_t first = Int_to_Int64(from_index, false); - if (first == 0) fail("Invalid index: 0"); - if (first < 0) first = text.length + first + 1; - if (first > text.length || first < 1) - return I(0); - int64_t found = _find(text, pattern, first-1, text.length-1, match_length); - return I(found+1); -} - -PUREFUNC public bool Text$has(Text_t text, Pattern_t pattern) -{ - if (Text$starts_with(pattern, Text("{start}"))) { - int64_t m = match(text, 0, pattern, 0, NULL, 0); - return m >= 0; - } else if (Text$ends_with(text, Text("{end}"))) { - for (int64_t i = text.length-1; i >= 0; i--) { - int64_t match_len = match(text, i, pattern, 0, NULL, 0); - if (match_len >= 0 && i + match_len == text.length) - return true; - } - return false; - } else { - int64_t found = _find(text, pattern, 0, text.length-1, NULL); - return (found >= 0); - } -} - -PUREFUNC public bool Text$matches(Text_t text, Pattern_t pattern) -{ - int64_t m = match(text, 0, pattern, 0, NULL, 0); - return m == text.length; -} - -public Array_t Text$find_all(Text_t text, Pattern_t pattern) -{ - if (pattern.length == 0) // special case - return (Array_t){.length=0}; - - Array_t matches = {}; - - for (int64_t i = 0; ; ) { - int64_t len = 0; - int64_t found = _find(text, pattern, i, text.length-1, &len); - if (found < 0) break; - Text_t match = Text$slice(text, I(found+1), I(found + len)); - Array$insert(&matches, &match, I_small(0), sizeof(Text_t)); - i = found + MAX(len, 1); - } - - return matches; -} - -static Text_t apply_backrefs(Text_t text, Pattern_t original_pattern, Text_t replacement, Pattern_t backref_pat, capture_t *captures) -{ - if (backref_pat.length == 0) - return replacement; - - int32_t first_grapheme = Text$get_grapheme(backref_pat, 0); - bool find_first = (first_grapheme != '{' - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); - - Text_t ret = Text(""); - TextIter_t state = {0, 0}; - int64_t nonmatching_pos = 0; - for (int64_t pos = 0; pos < replacement.length; ) { - // Optimization: quickly skip ahead to first char in the backref pattern: - if (find_first) { - while (pos < replacement.length && Text$get_grapheme_fast(replacement, &state, pos) != first_grapheme) - ++pos; - } - - int64_t backref_len = match(replacement, pos, backref_pat, 0, NULL, 0); - if (backref_len < 0) { - pos += 1; - continue; - } - - int64_t after_backref = pos + backref_len; - int64_t backref = parse_int(replacement, &after_backref); - if (after_backref == pos + backref_len) { // Not actually a backref if there's no number - pos += 1; - continue; - } - if (backref < 0 || backref > 9) fail("Invalid backref index: %ld (only 0-%d are allowed)", backref, MAX_BACKREFS-1); - backref_len = (after_backref - pos); - - if (Text$get_grapheme_fast(replacement, &state, pos + backref_len) == ';') - backref_len += 1; // skip optional semicolon - - if (!captures[backref].occupied) - fail("There is no capture number %ld!", backref); - - Text_t backref_text = Text$slice(text, I(captures[backref].index+1), I(captures[backref].index + captures[backref].length)); - - if (captures[backref].recursive && original_pattern.length > 0) - backref_text = Text$replace(backref_text, original_pattern, replacement, backref_pat, true); - - if (pos > nonmatching_pos) { - Text_t before_slice = Text$slice(replacement, I(nonmatching_pos+1), I(pos)); - ret = Text$concat(ret, before_slice, backref_text); - } else { - ret = Text$concat(ret, backref_text); - } - - pos += backref_len; - nonmatching_pos = pos; - } - if (nonmatching_pos < replacement.length) { - Text_t last_slice = Text$slice(replacement, I(nonmatching_pos+1), I(replacement.length)); - ret = Text$concat(ret, last_slice); - } - return ret; -} - -public Text_t Text$replace(Text_t text, Pattern_t pattern, Text_t replacement, Pattern_t backref_pat, bool recursive) -{ - Text_t ret = {.length=0}; - - int32_t first_grapheme = Text$get_grapheme(pattern, 0); - bool find_first = (first_grapheme != '{' - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); - - TextIter_t text_state = {0, 0}; - int64_t nonmatching_pos = 0; - for (int64_t pos = 0; pos < text.length; ) { - // Optimization: quickly skip ahead to first char in pattern: - if (find_first) { - while (pos < text.length && Text$get_grapheme_fast(text, &text_state, pos) != first_grapheme) - ++pos; - } - - capture_t captures[MAX_BACKREFS] = {}; - int64_t match_len = match(text, pos, pattern, 0, captures, 1); - if (match_len < 0) { - pos += 1; - continue; - } - captures[0] = (capture_t){ - .index = pos, .length = match_len, - .occupied = true, .recursive = false, - }; - - Text_t replacement_text = apply_backrefs(text, recursive ? pattern : Text(""), replacement, backref_pat, captures); - if (pos > nonmatching_pos) { - Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); - ret = Text$concat(ret, before_slice, replacement_text); - } else { - ret = Text$concat(ret, replacement_text); - } - nonmatching_pos = pos + match_len; - pos += MAX(match_len, 1); - } - if (nonmatching_pos < text.length) { - Text_t last_slice = Text$slice(text, I(nonmatching_pos+1), I(text.length)); - ret = Text$concat(ret, last_slice); - } - return ret; -} - -public Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right) -{ - int64_t first = 0, last = text.length-1; - if (trim_left) { - int64_t match_len = match(text, 0, pattern, 0, NULL, 0); - if (match_len > 0) - first = match_len; - } - - if (trim_right) { - for (int64_t i = text.length-1; i >= first; i--) { - int64_t match_len = match(text, i, pattern, 0, NULL, 0); - if (match_len > 0 && i + match_len == text.length) - last = i-1; - } - } - return Text$slice(text, I(first+1), I(last+1)); -} - -public Text_t Text$map(Text_t text, Pattern_t pattern, Closure_t fn) -{ - Text_t ret = {.length=0}; - - int32_t first_grapheme = Text$get_grapheme(pattern, 0); - bool find_first = (first_grapheme != '{' - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) - && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); - - TextIter_t text_state = {0, 0}; - int64_t nonmatching_pos = 0; - - Text_t (*text_mapper)(Text_t, void*) = fn.fn; - for (int64_t pos = 0; pos < text.length; pos++) { - // Optimization: quickly skip ahead to first char in pattern: - if (find_first) { - while (pos < text.length && Text$get_grapheme_fast(text, &text_state, pos) != first_grapheme) - ++pos; - } - - int64_t match_len = match(text, pos, pattern, 0, NULL, 0); - if (match_len < 0) continue; - - Text_t replacement = text_mapper(Text$slice(text, I(pos+1), I(pos+match_len)), fn.userdata); - if (pos > nonmatching_pos) { - Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); - ret = Text$concat(ret, before_slice, replacement); - } else { - ret = Text$concat(ret, replacement); - } - nonmatching_pos = pos + match_len; - pos += (match_len - 1); - } - if (nonmatching_pos < text.length) { - Text_t last_slice = Text$slice(text, I(nonmatching_pos+1), I(text.length)); - ret = Text$concat(ret, last_slice); - } - return ret; -} - -public Text_t Text$replace_all(Text_t text, Table_t replacements, Text_t backref_pat, bool recursive) -{ - if (replacements.entries.length == 0) return text; - - Text_t ret = {.length=0}; - - int64_t nonmatch_pos = 0; - for (int64_t pos = 0; pos < text.length; ) { - // Find the first matching pattern at this position: - for (int64_t i = 0; i < replacements.entries.length; i++) { - Pattern_t pattern = *(Pattern_t*)(replacements.entries.data + i*replacements.entries.stride); - capture_t captures[MAX_BACKREFS] = {}; - int64_t len = match(text, pos, pattern, 0, captures, 1); - if (len < 0) continue; - captures[0].index = pos; - captures[0].length = len; - - // If we skipped over some non-matching text before finding a match, insert it here: - if (pos > nonmatch_pos) { - Text_t before_slice = Text$slice(text, I(nonmatch_pos+1), I(pos)); - ret = Text$concat(ret, before_slice); - } - - // Concatenate the replacement: - Text_t replacement = *(Text_t*)(replacements.entries.data + i*replacements.entries.stride + sizeof(Text_t)); - Text_t replacement_text = apply_backrefs(text, recursive ? pattern : Text(""), replacement, backref_pat, captures); - ret = Text$concat(ret, replacement_text); - pos += MAX(len, 1); - nonmatch_pos = pos; - goto next_pos; - } - - pos += 1; - next_pos: - continue; - } - - if (nonmatch_pos <= text.length) { - Text_t last_slice = Text$slice(text, I(nonmatch_pos+1), I(text.length)); - ret = Text$concat(ret, last_slice); - } - return ret; -} - -public Array_t Text$split(Text_t text, Pattern_t pattern) -{ - if (text.length == 0) // special case - return (Array_t){.length=0}; - - if (pattern.length == 0) // special case - return Text$clusters(text); - - Array_t chunks = {}; - - Int_t i = I_small(1); - for (;;) { - int64_t len = 0; - Int_t found = Text$find(text, pattern, i, &len); - if (I_is_zero(found)) break; - Text_t chunk = Text$slice(text, i, Int$minus(found, I_small(1))); - Array$insert(&chunks, &chunk, I_small(0), sizeof(Text_t)); - i = Int$plus(found, I(MAX(len, 1))); - } - - Text_t last_chunk = Text$slice(text, i, I(text.length)); - Array$insert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); - - return chunks; -} - - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/pattern.h b/builtins/pattern.h deleted file mode 100644 index 804fb286..00000000 --- a/builtins/pattern.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -// The type representing text patterns for pattern matching. - -#include -#include -#include - -#include "datatypes.h" -#include "integers.h" -#include "types.h" - -#define Pattern(text) ((Pattern_t)Text(text)) -#define Patterns(...) ((Pattern_t)Texts(__VA_ARGS__)) - -Text_t Text$replace(Text_t str, Pattern_t pat, Text_t replacement, Pattern_t backref_pat, bool recursive); -Pattern_t Pattern$escape_text(Text_t text); -Text_t Text$replace_all(Text_t text, Table_t replacements, Pattern_t backref_pat, bool recursive); -Array_t Text$split(Text_t text, Pattern_t pattern); -Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right); -Int_t Text$find(Text_t text, Pattern_t pattern, Int_t i, int64_t *match_length); -Array_t Text$find_all(Text_t text, Pattern_t pattern); -PUREFUNC bool Text$has(Text_t text, Pattern_t pattern); -PUREFUNC bool Text$matches(Text_t text, Pattern_t pattern); -Text_t Text$map(Text_t text, Pattern_t pattern, Closure_t fn); - -#define Pattern$hash Text$hash -#define Pattern$compare Text$compare -#define Pattern$equal Text$equal - -extern const TypeInfo Pattern$info; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/patterns.c b/builtins/patterns.c new file mode 100644 index 00000000..81beaffe --- /dev/null +++ b/builtins/patterns.c @@ -0,0 +1,1064 @@ +// Logic for text pattern matching + +#include +#include +#include +#include + +#include "arrays.h" +#include "integers.h" +#include "patterns.h" +#include "tables.h" +#include "text.h" +#include "types.h" + +#define MAX_BACKREFS 100 + +static inline void skip_whitespace(Text_t text, int64_t *i) +{ + TextIter_t state = {0, 0}; + while (*i < text.length) { + int32_t grapheme = Text$get_grapheme_fast(text, &state, *i); + if (grapheme > 0 && !uc_is_property_white_space((ucs4_t)grapheme)) + return; + *i += 1; + } +} + +static inline bool match_grapheme(Text_t text, int64_t *i, int32_t grapheme) +{ + if (*i < text.length && Text$get_grapheme(text, *i) == grapheme) { + *i += 1; + return true; + } + return false; +} + +static inline bool match_str(Text_t text, int64_t *i, const char *str) +{ + TextIter_t state = {0, 0}; + int64_t matched = 0; + while (matched[str]) { + if (*i + matched >= text.length || Text$get_grapheme_fast(text, &state, *i + matched) != str[matched]) + return false; + matched += 1; + } + *i += matched; + return true; +} + +static inline bool match_property(Text_t text, int64_t *i, uc_property_t prop) +{ + if (*i >= text.length) return false; + TextIter_t state = {}; + ucs4_t grapheme = Text$get_main_grapheme_fast(text, &state, *i); + // TODO: check every codepoint in the cluster? + if (uc_is_property(grapheme, prop)) { + *i += 1; + return true; + } + return false; +} + +static int64_t parse_int(Text_t text, int64_t *i) +{ + TextIter_t state = {0, 0}; + int64_t value = 0; + for (;; *i += 1) { + ucs4_t grapheme = Text$get_main_grapheme_fast(text, &state, *i); + int digit = uc_digit_value((ucs4_t)grapheme); + if (digit < 0) break; + if (value >= INT64_MAX/10) break; + value = 10*value + digit; + } + return value; +} + +const char *get_property_name(Text_t text, int64_t *i) +{ + skip_whitespace(text, i); + char *name = GC_MALLOC_ATOMIC(UNINAME_MAX); + char *dest = name; + TextIter_t state = {0, 0}; + while (*i < text.length) { + int32_t grapheme = Text$get_grapheme_fast(text, &state, *i); + if (!(grapheme & ~0xFF) && (isalnum(grapheme) || grapheme == ' ' || grapheme == '_' || grapheme == '-')) { + *dest = (char)grapheme; + ++dest; + if (dest >= name + UNINAME_MAX - 1) + break; + } else { + break; + } + *i += 1; + } + + while (dest > name && dest[-1] == ' ') + *(dest--) = '\0'; + + if (dest == name) return NULL; + *dest = '\0'; + return name; +} + +#define EAT1(text, state, index, cond) ({\ + int32_t grapheme = Text$get_grapheme_fast(text, state, index); \ + bool success = (cond); \ + if (success) index += 1; \ + success; }) + +#define EAT2(text, state, index, cond1, cond2) ({\ + int32_t grapheme = Text$get_grapheme_fast(text, state, index); \ + bool success = (cond1); \ + if (success) { \ + grapheme = Text$get_grapheme_fast(text, state, index + 1); \ + success = (cond2); \ + if (success) \ + index += 2; \ + } \ + success; }) + + +#define EAT_MANY(text, state, index, cond) ({ int64_t _n = 0; while (EAT1(text, state, index, cond)) { _n += 1; } _n; }) + +int64_t match_email(Text_t text, int64_t index) +{ + // email = local "@" domain + // local = 1-64 ([a-zA-Z0-9!#$%&‘*+–/=?^_`.{|}~] | non-ascii) + // domain = dns-label ("." dns-label)* + // dns-label = 1-63 ([a-zA-Z0-9-] | non-ascii) + + TextIter_t state = {0, 0}; + if (index > 0) { + ucs4_t prev_codepoint = Text$get_main_grapheme_fast(text, &state, index - 1); + if (uc_is_property_alphabetic((ucs4_t)prev_codepoint)) + return -1; + } + + int64_t start_index = index; + + // Local part: + int64_t local_len = 0; + static const char *allowed_local = "!#$%&‘*+–/=?^_`.{|}~"; + while (EAT1(text, &state, index, + (grapheme & ~0x7F) || isalnum((char)grapheme) || strchr(allowed_local, (char)grapheme))) { + local_len += 1; + if (local_len > 64) return -1; + } + + if (!EAT1(text, &state, index, grapheme == '@')) + return -1; + + // Host + int64_t host_len = 0; + do { + int64_t label_len = 0; + while (EAT1(text, &state, index, + (grapheme & ~0x7F) || isalnum((char)grapheme) || grapheme == '-')) { + label_len += 1; + if (label_len > 63) return -1; + } + + if (label_len == 0) + return -1; + + host_len += label_len; + if (host_len > 255) + return -1; + host_len += 1; + } while (EAT1(text, &state, index, grapheme == '.')); + + return index - start_index; +} + +int64_t match_ipv6(Text_t text, int64_t index) +{ + TextIter_t state = {0, 0}; + if (index > 0) { + int32_t prev_codepoint = Text$get_grapheme_fast(text, &state, index - 1); + if ((prev_codepoint & ~0x7F) && (isxdigit(prev_codepoint) || prev_codepoint == ':')) + return -1; + } + int64_t start_index = index; + const int NUM_CLUSTERS = 8; + bool double_colon_used = false; + for (int cluster = 0; cluster < NUM_CLUSTERS; cluster++) { + for (int digits = 0; digits < 4; digits++) { + if (!EAT1(text, &state, index, ~(grapheme & ~0x7F) && isxdigit((char)grapheme))) + break; + } + if (EAT1(text, &state, index, ~(grapheme & ~0x7F) && isxdigit((char)grapheme))) + return -1; // Too many digits + + if (cluster == NUM_CLUSTERS-1) { + break; + } else if (!EAT1(text, &state, index, grapheme == ':')) { + if (double_colon_used) + break; + return -1; + } + + if (EAT1(text, &state, index, grapheme == ':')) { + if (double_colon_used) + return -1; + double_colon_used = true; + } + } + return index - start_index; +} + +static int64_t match_ipv4(Text_t text, int64_t index) +{ + TextIter_t state = {0, 0}; + if (index > 0) { + int32_t prev_codepoint = Text$get_grapheme_fast(text, &state, index - 1); + if ((prev_codepoint & ~0x7F) && (isdigit(prev_codepoint) || prev_codepoint == '.')) + return -1; + } + int64_t start_index = index; + + const int NUM_CLUSTERS = 4; + for (int cluster = 0; cluster < NUM_CLUSTERS; cluster++) { + for (int digits = 0; digits < 3; digits++) { + if (!EAT1(text, &state, index, ~(grapheme & ~0x7F) && isdigit((char)grapheme))) { + if (digits == 0) return -1; + break; + } + } + + if (EAT1(text, &state, index, ~(grapheme & ~0x7F) && isdigit((char)grapheme))) + return -1; // Too many digits + + if (cluster == NUM_CLUSTERS-1) + break; + else if (!EAT1(text, &state, index, grapheme == '.')) + return -1; + } + return (index - start_index); +} + +int64_t match_ip(Text_t text, int64_t index) +{ + int64_t len = match_ipv6(text, index); + if (len >= 0) return len; + len = match_ipv4(text, index); + return (len >= 0) ? len : -1; +} + +int64_t match_uri(Text_t text, int64_t index) +{ + // URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment] + // scheme = [a-zA-Z] [a-zA-Z0-9+.-] + // authority = [userinfo "@"] host [":" port] + + TextIter_t state = {0, 0}; + if (index > 0) { + ucs4_t prev_codepoint = Text$get_main_grapheme_fast(text, &state, index - 1); + if (uc_is_property_alphabetic(prev_codepoint)) + return -1; + } + + int64_t start_index = index; + + // Scheme: + if (!EAT1(text, &state, index, isalpha(grapheme))) + return -1; + + EAT_MANY(text, &state, index, + !(grapheme & ~0x7F) && (isalnum(grapheme) || grapheme == '+' || grapheme == '.' || grapheme == '-')); + + if (index == start_index) + return -1; + + if (!match_grapheme(text, &index, ':')) + return -1; + + // Authority: + if (match_str(text, &index, "//")) { + int64_t authority_start = index; + // Username or host: + static const char *forbidden = "#?:@ \t\r\n<>[]{}\\^|\"`/"; + if (EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(forbidden, (char)grapheme)) == 0) + return -1; + + if (EAT1(text, &state, index, grapheme == '@')) { + // Found a username, now get a host: + if (EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(forbidden, (char)grapheme)) == 0) + return -1; + } else { + int64_t ip = authority_start; + int64_t ipv4_len = match_ipv4(text, ip); + if (ipv4_len > 0) { + ip += ipv4_len; + } else if (match_grapheme(text, &ip, '[')) { + ip += match_ipv6(text, ip); + if (ip > authority_start + 1 && match_grapheme(text, &ip, ']')) + index = ip; + } + } + + // Port: + if (EAT1(text, &state, index, grapheme == ':')) { + if (EAT_MANY(text, &state, index, !(grapheme & ~0x7F) && isdigit(grapheme)) == 0) + return -1; + } + if (!EAT1(text, &state, index, grapheme == '/')) + return (index - start_index); // No path + } else { + // Optional path root: + EAT1(text, &state, index, grapheme == '/'); + } + + // Path: + static const char *non_path = " \"#?<>[]{}\\^`|"; + EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(non_path, (char)grapheme)); + + if (EAT1(text, &state, index, grapheme == '?')) { // Query + static const char *non_query = " \"#<>[]{}\\^`|"; + EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(non_query, (char)grapheme)); + } + + if (EAT1(text, &state, index, grapheme == '#')) { // Fragment + static const char *non_fragment = " \"#<>[]{}\\^`|"; + EAT_MANY(text, &state, index, (grapheme & ~0x7F) || !strchr(non_fragment, (char)grapheme)); + } + return index - start_index; +} + +int64_t match_url(Text_t text, int64_t index) +{ + int64_t lookahead = index; + if (!(match_str(text, &lookahead, "https:") + || match_str(text, &lookahead, "http:") + || match_str(text, &lookahead, "ftp:") + || match_str(text, &lookahead, "wss:") + || match_str(text, &lookahead, "ws:"))) + return -1; + + return match_uri(text, index); +} + +int64_t match_id(Text_t text, int64_t index) +{ + TextIter_t state = {0, 0}; + if (!EAT1(text, &state, index, uc_is_property((ucs4_t)grapheme, UC_PROPERTY_XID_START))) + return -1; + return 1 + EAT_MANY(text, &state, index, uc_is_property((ucs4_t)grapheme, UC_PROPERTY_XID_CONTINUE)); +} + +int64_t match_int(Text_t text, int64_t index) +{ + TextIter_t state = {0, 0}; + int64_t len = EAT_MANY(text, &state, index, uc_is_property((ucs4_t)grapheme, UC_PROPERTY_DECIMAL_DIGIT)); + return len >= 0 ? len : -1; +} + +int64_t match_num(Text_t text, int64_t index) +{ + TextIter_t state = {0, 0}; + bool negative = EAT1(text, &state, index, grapheme == '-') ? 1 : 0; + int64_t pre_decimal = EAT_MANY(text, &state, index, + uc_is_property((ucs4_t)grapheme, UC_PROPERTY_DECIMAL_DIGIT)); + bool decimal = (EAT1(text, &state, index, grapheme == '.') == 1); + int64_t post_decimal = decimal ? EAT_MANY(text, &state, index, + uc_is_property((ucs4_t)grapheme, UC_PROPERTY_DECIMAL_DIGIT)) : 0; + if (pre_decimal == 0 && post_decimal == 0) + return -1; + return negative + pre_decimal + decimal + post_decimal; +} + +int64_t match_newline(Text_t text, int64_t index) +{ + if (index >= text.length) + return -1; + + TextIter_t state = {0, 0}; + ucs4_t grapheme = index >= text.length ? 0 : Text$get_main_grapheme_fast(text, &state, index); + if (grapheme == '\n') + return 1; + if (grapheme == '\r' && Text$get_grapheme_fast(text, &state, index + 1) == '\n') + return 2; + return -1; +} + +typedef struct { + int64_t index, length; + bool occupied, recursive; +} capture_t; + +typedef struct { + enum { PAT_START, PAT_END, PAT_ANY, PAT_GRAPHEME, PAT_PROPERTY, PAT_QUOTE, PAT_PAIR, PAT_FUNCTION } tag; + bool negated, non_capturing; + int64_t min, max; + union { + int32_t grapheme; + uc_property_t property; + int64_t (*fn)(Text_t, int64_t); + int32_t quote_graphemes[2]; + int32_t pair_graphemes[2]; + }; +} pat_t; + +int64_t match_pat(Text_t text, TextIter_t *state, int64_t index, pat_t pat) +{ + int32_t grapheme = index >= text.length ? 0 : Text$get_grapheme_fast(text, state, index); + + switch (pat.tag) { + case PAT_START: { + if (index == 0) + return pat.negated ? -1 : 0; + return pat.negated ? 0 : -1; + } + case PAT_END: { + if (index >= text.length) + return pat.negated ? -1 : 0; + return pat.negated ? 0 : -1; + } + case PAT_ANY: { + assert(!pat.negated); + return (index < text.length) ? 1 : -1; + } + case PAT_GRAPHEME: { + if (index >= text.length) + return -1; + else if (grapheme == pat.grapheme) + return pat.negated ? -1 : 1; + return pat.negated ? 1 : -1; + } + case PAT_PROPERTY: { + if (index >= text.length) + return -1; + else if (uc_is_property((ucs4_t)grapheme, pat.property)) + return pat.negated ? -1 : 1; + return pat.negated ? 1 : -1; + } + case PAT_PAIR: { + // Nested punctuation: (?), [?], etc + if (index >= text.length) + return -1; + + int32_t open = pat.pair_graphemes[0]; + if (grapheme != open) + return pat.negated ? 1 : -1; + + int32_t close = pat.pair_graphemes[1]; + int64_t depth = 1; + int64_t match_len = 1; + for (; depth > 0; match_len++) { + if (index + match_len >= text.length) + return pat.negated ? 1 : -1; + + int32_t c = Text$get_grapheme_fast(text, state, index + match_len); + if (c == open) + depth += 1; + else if (c == close) + depth -= 1; + } + return pat.negated ? -1 : match_len; + } + case PAT_QUOTE: { + // Nested quotes: "?", '?', etc + if (index >= text.length) + return -1; + + int32_t open = pat.quote_graphemes[0]; + if (grapheme != open) + return pat.negated ? 1 : -1; + + int32_t close = pat.quote_graphemes[1]; + for (int64_t i = index + 1; i < text.length; i++) { + int32_t c = Text$get_grapheme_fast(text, state, i); + if (c == close) { + return pat.negated ? -1 : (i - index) + 1; + } else if (c == '\\' && index + 1 < text.length) { + i += 1; // Skip ahead an extra step + } + } + return pat.negated ? 1 : -1; + } + case PAT_FUNCTION: { + int64_t match_len = pat.fn(text, index); + if (match_len >= 0) + return pat.negated ? -1 : match_len; + return pat.negated ? 1 : -1; + } + default: errx(1, "Invalid pattern"); + } + errx(1, "Unreachable"); +} + +pat_t parse_next_pat(Text_t pattern, TextIter_t *state, int64_t *index) +{ + if (EAT2(pattern, state, *index, + uc_is_property((ucs4_t)grapheme, UC_PROPERTY_QUOTATION_MARK), + grapheme == '?')) { + // Quotations: "?", '?', etc + int32_t open = Text$get_grapheme_fast(pattern, state, *index-2); + int32_t close = open; + uc_mirror_char((ucs4_t)open, (ucs4_t*)&close); + if (!match_grapheme(pattern, index, close)) + fail("Pattern's closing quote is missing: %k", &pattern); + + return (pat_t){ + .tag=PAT_QUOTE, + .min=1, .max=1, + .quote_graphemes={open, close}, + }; + } else if (EAT2(pattern, state, *index, + uc_is_property((ucs4_t)grapheme, UC_PROPERTY_PAIRED_PUNCTUATION), + grapheme == '?')) { + // Nested punctuation: (?), [?], etc + int32_t open = Text$get_grapheme_fast(pattern, state, *index-2); + int32_t close = open; + uc_mirror_char((ucs4_t)open, (ucs4_t*)&close); + if (!match_grapheme(pattern, index, close)) + fail("Pattern's closing brace is missing: %k", &pattern); + + return (pat_t){ + .tag=PAT_PAIR, + .min=1, .max=1, + .pair_graphemes={open, close}, + }; + } else if (EAT1(pattern, state, *index, + grapheme == '{')) { // named patterns {id}, {2-3 hex}, etc. + skip_whitespace(pattern, index); + int64_t min, max; + if (uc_is_digit((ucs4_t)Text$get_grapheme_fast(pattern, state, *index))) { + min = parse_int(pattern, index); + skip_whitespace(pattern, index); + if (match_grapheme(pattern, index, '+')) { + max = INT64_MAX; + } else if (match_grapheme(pattern, index, '-')) { + max = parse_int(pattern, index); + } else { + max = min; + } + if (min > max) fail("Minimum repetitions (%ld) is less than the maximum (%ld)", min, max); + } else { + min = -1, max = -1; + } + + skip_whitespace(pattern, index); + + bool negated = match_grapheme(pattern, index, '!'); +#define PAT(_tag, ...) ((pat_t){.min=min, .max=max, .negated=negated, .tag=_tag, __VA_ARGS__}) + const char *prop_name; + if (match_str(pattern, index, "..")) + prop_name = ".."; + else + prop_name = get_property_name(pattern, index); + + if (!prop_name) { + // Literal character, e.g. {1?} + skip_whitespace(pattern, index); + int32_t grapheme = Text$get_grapheme_fast(pattern, state, (*index)++); + if (!match_grapheme(pattern, index, '}')) + fail("Missing closing '}' in pattern: %k", &pattern); + return PAT(PAT_GRAPHEME, .grapheme=grapheme); + } else if (strlen(prop_name) == 1) { + // Single letter names: {1+ A} + skip_whitespace(pattern, index); + if (!match_grapheme(pattern, index, '}')) + fail("Missing closing '}' in pattern: %k", &pattern); + return PAT(PAT_GRAPHEME, .grapheme=prop_name[0]); + } + + skip_whitespace(pattern, index); + if (!match_grapheme(pattern, index, '}')) + fail("Missing closing '}' in pattern: %k", &pattern); + + switch (tolower(prop_name[0])) { + case '.': + if (prop_name[1] == '.') { + if (negated) + return ((pat_t){.tag=PAT_END, .min=min, .max=max, .non_capturing=true}); + else + return PAT(PAT_ANY); + } + break; + case 'd': + if (strcasecmp(prop_name, "digit") == 0) { + return PAT(PAT_PROPERTY, .property=UC_PROPERTY_DECIMAL_DIGIT); + } + break; + case 'e': + if (strcasecmp(prop_name, "end") == 0) { + return PAT(PAT_END, .non_capturing=!negated); + } else if (strcasecmp(prop_name, "email") == 0) { + return PAT(PAT_FUNCTION, .fn=match_email); + } else if (strcasecmp(prop_name, "emoji") == 0) { + return PAT(PAT_PROPERTY, .property=UC_PROPERTY_EMOJI); + } + break; + case 'i': + if (strcasecmp(prop_name, "id") == 0) { + return PAT(PAT_FUNCTION, .fn=match_id); + } else if (strcasecmp(prop_name, "int") == 0) { + return PAT(PAT_FUNCTION, .fn=match_int); + } else if (strcasecmp(prop_name, "ipv4") == 0) { + return PAT(PAT_FUNCTION, .fn=match_ipv4); + } else if (strcasecmp(prop_name, "ipv6") == 0) { + return PAT(PAT_FUNCTION, .fn=match_ipv6); + } else if (strcasecmp(prop_name, "ip") == 0) { + return PAT(PAT_FUNCTION, .fn=match_ip); + } + break; + case 'n': + if (strcasecmp(prop_name, "nl") == 0 || strcasecmp(prop_name, "newline") == 0 + || strcasecmp(prop_name, "crlf")) { + return PAT(PAT_FUNCTION, .fn=match_newline); + } else if (strcasecmp(prop_name, "num") == 0) { + return PAT(PAT_FUNCTION, .fn=match_num); + } + break; + case 's': + if (strcasecmp(prop_name, "start") == 0) { + return PAT(PAT_START, .non_capturing=!negated); + } + break; + case 'u': + if (strcasecmp(prop_name, "uri") == 0) { + return PAT(PAT_FUNCTION, .fn=match_uri); + } else if (strcasecmp(prop_name, "url") == 0) { + return PAT(PAT_FUNCTION, .fn=match_url); + } + break; + default: break; + } + + uc_property_t prop = uc_property_byname(prop_name); + if (uc_property_is_valid(prop)) + return PAT(PAT_PROPERTY, .property=prop); + + ucs4_t grapheme = unicode_name_character(prop_name); + if (grapheme == UNINAME_INVALID) + fail("Not a valid property or character name: %s", prop_name); + return PAT(PAT_GRAPHEME, .grapheme=(int32_t)grapheme); +#undef PAT + } else { + return (pat_t){.tag=PAT_GRAPHEME, .non_capturing=true, .min=1, .max=1, .grapheme=Text$get_grapheme_fast(pattern, state, (*index)++)}; + } +} + +int64_t match(Text_t text, int64_t text_index, Pattern_t pattern, int64_t pattern_index, capture_t *captures, int64_t capture_index) +{ + if (pattern_index >= pattern.length) // End of the pattern + return 0; + + int64_t start_index = text_index; + TextIter_t pattern_state = {0, 0}, text_state = {0, 0}; + pat_t pat = parse_next_pat(pattern, &pattern_state, &pattern_index); + + if (pat.min == -1 && pat.max == -1) { + if (pat.tag == PAT_ANY && pattern_index >= pattern.length) { + pat.min = pat.max = MAX(1, text.length - text_index); + } else { + pat.min = 1; + pat.max = INT64_MAX; + } + } + + int64_t capture_start = text_index; + int64_t count = 0, capture_len = 0, next_match_len = 0; + + if (pat.tag == PAT_ANY && pattern_index >= pattern.length) { + int64_t remaining = text.length - text_index; + capture_len = remaining >= pat.min ? MIN(remaining, pat.max) : -1; + text_index += capture_len; + goto success; + } + + if (pat.min == 0 && pattern_index < pattern.length) { + next_match_len = match(text, text_index, pattern, pattern_index, captures, capture_index + (pat.non_capturing ? 0 : 1)); + if (next_match_len >= 0) { + capture_len = 0; + goto success; + } + } + + while (count < pat.max) { + int64_t match_len = match_pat(text, &text_state, text_index, pat); + if (match_len < 0) + break; + capture_len += match_len; + text_index += match_len; + count += 1; + + if (pattern_index < pattern.length) { // More stuff after this + if (count < pat.min) + next_match_len = -1; + else + next_match_len = match(text, text_index, pattern, pattern_index, captures, capture_index + (pat.non_capturing ? 0 : 1)); + } else { + next_match_len = 0; + } + + if (match_len == 0) { + if (next_match_len >= 0) { + // If we're good to go, no need to keep re-matching zero-length + // matches till we hit max: + count = pat.max; + break; + } else { + return -1; + } + } + + if (pattern_index < pattern.length && next_match_len >= 0) + break; // Next guy exists and wants to stop here + + if (text_index >= text.length) + break; + } + + if (count < pat.min || next_match_len < 0) + return -1; + + success: + if (captures && capture_index < MAX_BACKREFS && !pat.non_capturing) { + if (pat.tag == PAT_PAIR || pat.tag == PAT_QUOTE) { + assert(capture_len > 0); + captures[capture_index] = (capture_t){ + .index=capture_start + 1, // Skip leading quote/paren + .length=capture_len - 2, // Skip open/close + .occupied=true, + .recursive=(pat.tag == PAT_PAIR), + }; + } else { + captures[capture_index] = (capture_t){ + .index=capture_start, + .length=capture_len, + .occupied=true, + .recursive=false, + }; + } + } + return (text_index - start_index) + next_match_len; +} + +#undef EAT1 +#undef EAT2 +#undef EAT_MANY + +static int64_t _find(Text_t text, Pattern_t pattern, int64_t first, int64_t last, int64_t *match_length) +{ + int32_t first_grapheme = Text$get_grapheme(pattern, 0); + bool find_first = (first_grapheme != '{' + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); + + TextIter_t text_state = {0, 0}; + + for (int64_t i = first; i <= last; i++) { + // Optimization: quickly skip ahead to first char in pattern: + if (find_first) { + while (i < text.length && Text$get_grapheme_fast(text, &text_state, i) != first_grapheme) + ++i; + } + + int64_t m = match(text, i, pattern, 0, NULL, 0); + if (m >= 0) { + if (match_length) + *match_length = m; + return i; + } + } + if (match_length) + *match_length = -1; + return -1; +} + +public Int_t Text$find(Text_t text, Pattern_t pattern, Int_t from_index, int64_t *match_length) +{ + int64_t first = Int_to_Int64(from_index, false); + if (first == 0) fail("Invalid index: 0"); + if (first < 0) first = text.length + first + 1; + if (first > text.length || first < 1) + return I(0); + int64_t found = _find(text, pattern, first-1, text.length-1, match_length); + return I(found+1); +} + +PUREFUNC public bool Text$has(Text_t text, Pattern_t pattern) +{ + if (Text$starts_with(pattern, Text("{start}"))) { + int64_t m = match(text, 0, pattern, 0, NULL, 0); + return m >= 0; + } else if (Text$ends_with(text, Text("{end}"))) { + for (int64_t i = text.length-1; i >= 0; i--) { + int64_t match_len = match(text, i, pattern, 0, NULL, 0); + if (match_len >= 0 && i + match_len == text.length) + return true; + } + return false; + } else { + int64_t found = _find(text, pattern, 0, text.length-1, NULL); + return (found >= 0); + } +} + +PUREFUNC public bool Text$matches(Text_t text, Pattern_t pattern) +{ + int64_t m = match(text, 0, pattern, 0, NULL, 0); + return m == text.length; +} + +public Array_t Text$find_all(Text_t text, Pattern_t pattern) +{ + if (pattern.length == 0) // special case + return (Array_t){.length=0}; + + Array_t matches = {}; + + for (int64_t i = 0; ; ) { + int64_t len = 0; + int64_t found = _find(text, pattern, i, text.length-1, &len); + if (found < 0) break; + Text_t match = Text$slice(text, I(found+1), I(found + len)); + Array$insert(&matches, &match, I_small(0), sizeof(Text_t)); + i = found + MAX(len, 1); + } + + return matches; +} + +static Text_t apply_backrefs(Text_t text, Pattern_t original_pattern, Text_t replacement, Pattern_t backref_pat, capture_t *captures) +{ + if (backref_pat.length == 0) + return replacement; + + int32_t first_grapheme = Text$get_grapheme(backref_pat, 0); + bool find_first = (first_grapheme != '{' + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); + + Text_t ret = Text(""); + TextIter_t state = {0, 0}; + int64_t nonmatching_pos = 0; + for (int64_t pos = 0; pos < replacement.length; ) { + // Optimization: quickly skip ahead to first char in the backref pattern: + if (find_first) { + while (pos < replacement.length && Text$get_grapheme_fast(replacement, &state, pos) != first_grapheme) + ++pos; + } + + int64_t backref_len = match(replacement, pos, backref_pat, 0, NULL, 0); + if (backref_len < 0) { + pos += 1; + continue; + } + + int64_t after_backref = pos + backref_len; + int64_t backref = parse_int(replacement, &after_backref); + if (after_backref == pos + backref_len) { // Not actually a backref if there's no number + pos += 1; + continue; + } + if (backref < 0 || backref > 9) fail("Invalid backref index: %ld (only 0-%d are allowed)", backref, MAX_BACKREFS-1); + backref_len = (after_backref - pos); + + if (Text$get_grapheme_fast(replacement, &state, pos + backref_len) == ';') + backref_len += 1; // skip optional semicolon + + if (!captures[backref].occupied) + fail("There is no capture number %ld!", backref); + + Text_t backref_text = Text$slice(text, I(captures[backref].index+1), I(captures[backref].index + captures[backref].length)); + + if (captures[backref].recursive && original_pattern.length > 0) + backref_text = Text$replace(backref_text, original_pattern, replacement, backref_pat, true); + + if (pos > nonmatching_pos) { + Text_t before_slice = Text$slice(replacement, I(nonmatching_pos+1), I(pos)); + ret = Text$concat(ret, before_slice, backref_text); + } else { + ret = Text$concat(ret, backref_text); + } + + pos += backref_len; + nonmatching_pos = pos; + } + if (nonmatching_pos < replacement.length) { + Text_t last_slice = Text$slice(replacement, I(nonmatching_pos+1), I(replacement.length)); + ret = Text$concat(ret, last_slice); + } + return ret; +} + +public Text_t Text$replace(Text_t text, Pattern_t pattern, Text_t replacement, Pattern_t backref_pat, bool recursive) +{ + Text_t ret = {.length=0}; + + int32_t first_grapheme = Text$get_grapheme(pattern, 0); + bool find_first = (first_grapheme != '{' + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); + + TextIter_t text_state = {0, 0}; + int64_t nonmatching_pos = 0; + for (int64_t pos = 0; pos < text.length; ) { + // Optimization: quickly skip ahead to first char in pattern: + if (find_first) { + while (pos < text.length && Text$get_grapheme_fast(text, &text_state, pos) != first_grapheme) + ++pos; + } + + capture_t captures[MAX_BACKREFS] = {}; + int64_t match_len = match(text, pos, pattern, 0, captures, 1); + if (match_len < 0) { + pos += 1; + continue; + } + captures[0] = (capture_t){ + .index = pos, .length = match_len, + .occupied = true, .recursive = false, + }; + + Text_t replacement_text = apply_backrefs(text, recursive ? pattern : Text(""), replacement, backref_pat, captures); + if (pos > nonmatching_pos) { + Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); + ret = Text$concat(ret, before_slice, replacement_text); + } else { + ret = Text$concat(ret, replacement_text); + } + nonmatching_pos = pos + match_len; + pos += MAX(match_len, 1); + } + if (nonmatching_pos < text.length) { + Text_t last_slice = Text$slice(text, I(nonmatching_pos+1), I(text.length)); + ret = Text$concat(ret, last_slice); + } + return ret; +} + +public Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right) +{ + int64_t first = 0, last = text.length-1; + if (trim_left) { + int64_t match_len = match(text, 0, pattern, 0, NULL, 0); + if (match_len > 0) + first = match_len; + } + + if (trim_right) { + for (int64_t i = text.length-1; i >= first; i--) { + int64_t match_len = match(text, i, pattern, 0, NULL, 0); + if (match_len > 0 && i + match_len == text.length) + last = i-1; + } + } + return Text$slice(text, I(first+1), I(last+1)); +} + +public Text_t Text$map(Text_t text, Pattern_t pattern, Closure_t fn) +{ + Text_t ret = {.length=0}; + + int32_t first_grapheme = Text$get_grapheme(pattern, 0); + bool find_first = (first_grapheme != '{' + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_QUOTATION_MARK) + && !uc_is_property((ucs4_t)first_grapheme, UC_PROPERTY_PAIRED_PUNCTUATION)); + + TextIter_t text_state = {0, 0}; + int64_t nonmatching_pos = 0; + + Text_t (*text_mapper)(Text_t, void*) = fn.fn; + for (int64_t pos = 0; pos < text.length; pos++) { + // Optimization: quickly skip ahead to first char in pattern: + if (find_first) { + while (pos < text.length && Text$get_grapheme_fast(text, &text_state, pos) != first_grapheme) + ++pos; + } + + int64_t match_len = match(text, pos, pattern, 0, NULL, 0); + if (match_len < 0) continue; + + Text_t replacement = text_mapper(Text$slice(text, I(pos+1), I(pos+match_len)), fn.userdata); + if (pos > nonmatching_pos) { + Text_t before_slice = Text$slice(text, I(nonmatching_pos+1), I(pos)); + ret = Text$concat(ret, before_slice, replacement); + } else { + ret = Text$concat(ret, replacement); + } + nonmatching_pos = pos + match_len; + pos += (match_len - 1); + } + if (nonmatching_pos < text.length) { + Text_t last_slice = Text$slice(text, I(nonmatching_pos+1), I(text.length)); + ret = Text$concat(ret, last_slice); + } + return ret; +} + +public Text_t Text$replace_all(Text_t text, Table_t replacements, Text_t backref_pat, bool recursive) +{ + if (replacements.entries.length == 0) return text; + + Text_t ret = {.length=0}; + + int64_t nonmatch_pos = 0; + for (int64_t pos = 0; pos < text.length; ) { + // Find the first matching pattern at this position: + for (int64_t i = 0; i < replacements.entries.length; i++) { + Pattern_t pattern = *(Pattern_t*)(replacements.entries.data + i*replacements.entries.stride); + capture_t captures[MAX_BACKREFS] = {}; + int64_t len = match(text, pos, pattern, 0, captures, 1); + if (len < 0) continue; + captures[0].index = pos; + captures[0].length = len; + + // If we skipped over some non-matching text before finding a match, insert it here: + if (pos > nonmatch_pos) { + Text_t before_slice = Text$slice(text, I(nonmatch_pos+1), I(pos)); + ret = Text$concat(ret, before_slice); + } + + // Concatenate the replacement: + Text_t replacement = *(Text_t*)(replacements.entries.data + i*replacements.entries.stride + sizeof(Text_t)); + Text_t replacement_text = apply_backrefs(text, recursive ? pattern : Text(""), replacement, backref_pat, captures); + ret = Text$concat(ret, replacement_text); + pos += MAX(len, 1); + nonmatch_pos = pos; + goto next_pos; + } + + pos += 1; + next_pos: + continue; + } + + if (nonmatch_pos <= text.length) { + Text_t last_slice = Text$slice(text, I(nonmatch_pos+1), I(text.length)); + ret = Text$concat(ret, last_slice); + } + return ret; +} + +public Array_t Text$split(Text_t text, Pattern_t pattern) +{ + if (text.length == 0) // special case + return (Array_t){.length=0}; + + if (pattern.length == 0) // special case + return Text$clusters(text); + + Array_t chunks = {}; + + Int_t i = I_small(1); + for (;;) { + int64_t len = 0; + Int_t found = Text$find(text, pattern, i, &len); + if (I_is_zero(found)) break; + Text_t chunk = Text$slice(text, i, Int$minus(found, I_small(1))); + Array$insert(&chunks, &chunk, I_small(0), sizeof(Text_t)); + i = Int$plus(found, I(MAX(len, 1))); + } + + Text_t last_chunk = Text$slice(text, i, I(text.length)); + Array$insert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); + + return chunks; +} + + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/patterns.h b/builtins/patterns.h new file mode 100644 index 00000000..804fb286 --- /dev/null +++ b/builtins/patterns.h @@ -0,0 +1,33 @@ +#pragma once + +// The type representing text patterns for pattern matching. + +#include +#include +#include + +#include "datatypes.h" +#include "integers.h" +#include "types.h" + +#define Pattern(text) ((Pattern_t)Text(text)) +#define Patterns(...) ((Pattern_t)Texts(__VA_ARGS__)) + +Text_t Text$replace(Text_t str, Pattern_t pat, Text_t replacement, Pattern_t backref_pat, bool recursive); +Pattern_t Pattern$escape_text(Text_t text); +Text_t Text$replace_all(Text_t text, Table_t replacements, Pattern_t backref_pat, bool recursive); +Array_t Text$split(Text_t text, Pattern_t pattern); +Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right); +Int_t Text$find(Text_t text, Pattern_t pattern, Int_t i, int64_t *match_length); +Array_t Text$find_all(Text_t text, Pattern_t pattern); +PUREFUNC bool Text$has(Text_t text, Pattern_t pattern); +PUREFUNC bool Text$matches(Text_t text, Pattern_t pattern); +Text_t Text$map(Text_t text, Pattern_t pattern, Closure_t fn); + +#define Pattern$hash Text$hash +#define Pattern$compare Text$compare +#define Pattern$equal Text$equal + +extern const TypeInfo Pattern$info; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/pointer.c b/builtins/pointer.c deleted file mode 100644 index 1ad9f407..00000000 --- a/builtins/pointer.c +++ /dev/null @@ -1,84 +0,0 @@ -// Type infos and methods for Pointer types -#include -#include -#include -#include -#include -#include -#include - -#include "metamethods.h" -#include "text.h" -#include "types.h" -#include "util.h" - -typedef struct recursion_s { - const void *ptr; - struct recursion_s *next; -} recursion_t; - -public Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) { - auto ptr_info = type->PointerInfo; - if (!x) { - Text_t typename = generic_as_text(NULL, false, ptr_info.pointed); - Text_t text; - if (colorize) - text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), typename, Text("\x1b[m")); - else - text = Text$concat(Text$from_str(ptr_info.sigil), typename); - return text; - } - const void *ptr = *(const void**)x; - if (!ptr) { - Text_t typename = generic_as_text(NULL, false, ptr_info.pointed); - if (colorize) - return Text$concat(Text("\x1b[34;1m!"), typename, Text("\x1b[m")); - else - return Text$concat(Text("!"), typename); - } - - // Check for recursive references, so if `x.foo = x`, then it prints as - // `@Foo{foo=@..1}` instead of overflowing the stack: - static recursion_t *recursion = NULL; - int32_t depth = 0; - for (recursion_t *r = recursion; r; r = r->next) { - ++depth; - if (r->ptr == ptr) { - Text_t text = Text$concat( - colorize ? Text("\x1b[34;1m") : Text(""), - Text$from_str(ptr_info.sigil), - Text(".."), - Int32$as_text(&depth, false, &Int32$info), - colorize ? Text("\x1b[m") : Text("")); - return text; - } - } - - Text_t pointed; - { // Stringify with this pointer flagged as a recursive one: - recursion_t my_recursion = {.ptr=ptr, .next=recursion}; - recursion = &my_recursion; - pointed = generic_as_text(ptr, colorize, ptr_info.pointed); - recursion = recursion->next; - } - Text_t text; - if (colorize) - text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), Text("\x1b[m"), pointed); - else - text = Text$concat(Text$from_str(ptr_info.sigil), pointed); - return text; -} - -PUREFUNC public int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type) { - (void)type; - const void *xp = *(const void**)x, *yp = *(const void**)y; - return (xp > yp) - (xp < yp); -} - -PUREFUNC public bool Pointer$equal(const void *x, const void *y, const TypeInfo *type) { - (void)type; - const void *xp = *(const void**)x, *yp = *(const void**)y; - return xp == yp; -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/pointer.h b/builtins/pointer.h deleted file mode 100644 index faa95316..00000000 --- a/builtins/pointer.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -// Type infos and methods for Pointer types - -#include -#include - -#include "types.h" -#include "util.h" - -Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo *type); -PUREFUNC int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type); -PUREFUNC bool Pointer$equal(const void *x, const void *y, 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}) - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/pointers.c b/builtins/pointers.c new file mode 100644 index 00000000..1ad9f407 --- /dev/null +++ b/builtins/pointers.c @@ -0,0 +1,84 @@ +// Type infos and methods for Pointer types +#include +#include +#include +#include +#include +#include +#include + +#include "metamethods.h" +#include "text.h" +#include "types.h" +#include "util.h" + +typedef struct recursion_s { + const void *ptr; + struct recursion_s *next; +} recursion_t; + +public Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) { + auto ptr_info = type->PointerInfo; + if (!x) { + Text_t typename = generic_as_text(NULL, false, ptr_info.pointed); + Text_t text; + if (colorize) + text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), typename, Text("\x1b[m")); + else + text = Text$concat(Text$from_str(ptr_info.sigil), typename); + return text; + } + const void *ptr = *(const void**)x; + if (!ptr) { + Text_t typename = generic_as_text(NULL, false, ptr_info.pointed); + if (colorize) + return Text$concat(Text("\x1b[34;1m!"), typename, Text("\x1b[m")); + else + return Text$concat(Text("!"), typename); + } + + // Check for recursive references, so if `x.foo = x`, then it prints as + // `@Foo{foo=@..1}` instead of overflowing the stack: + static recursion_t *recursion = NULL; + int32_t depth = 0; + for (recursion_t *r = recursion; r; r = r->next) { + ++depth; + if (r->ptr == ptr) { + Text_t text = Text$concat( + colorize ? Text("\x1b[34;1m") : Text(""), + Text$from_str(ptr_info.sigil), + Text(".."), + Int32$as_text(&depth, false, &Int32$info), + colorize ? Text("\x1b[m") : Text("")); + return text; + } + } + + Text_t pointed; + { // Stringify with this pointer flagged as a recursive one: + recursion_t my_recursion = {.ptr=ptr, .next=recursion}; + recursion = &my_recursion; + pointed = generic_as_text(ptr, colorize, ptr_info.pointed); + recursion = recursion->next; + } + Text_t text; + if (colorize) + text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), Text("\x1b[m"), pointed); + else + text = Text$concat(Text$from_str(ptr_info.sigil), pointed); + return text; +} + +PUREFUNC public int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type) { + (void)type; + const void *xp = *(const void**)x, *yp = *(const void**)y; + return (xp > yp) - (xp < yp); +} + +PUREFUNC public bool Pointer$equal(const void *x, const void *y, const TypeInfo *type) { + (void)type; + const void *xp = *(const void**)x, *yp = *(const void**)y; + return xp == yp; +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/pointers.h b/builtins/pointers.h new file mode 100644 index 00000000..faa95316 --- /dev/null +++ b/builtins/pointers.h @@ -0,0 +1,19 @@ +#pragma once + +// Type infos and methods for Pointer types + +#include +#include + +#include "types.h" +#include "util.h" + +Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo *type); +PUREFUNC int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type); +PUREFUNC bool Pointer$equal(const void *x, const void *y, 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}) + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/range.c b/builtins/range.c deleted file mode 100644 index 9dfd1efe..00000000 --- a/builtins/range.c +++ /dev/null @@ -1,63 +0,0 @@ -// Functions that operate on numeric ranges - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "integers.h" -#include "text.h" -#include "types.h" -#include "util.h" - - -PUREFUNC static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo *type) -{ - (void)type; - if (x == y) return 0; - int32_t diff = Int$compare(&x->first, &y->first, &Int$info); - if (diff != 0) return diff; - diff = Int$compare(&x->last, &y->last, &Int$info); - if (diff != 0) return diff; - return Int$compare(&x->step, &y->step, &Int$info); -} - -PUREFUNC static bool Range$equal(const Range_t *x, const Range_t *y, const TypeInfo *type) -{ - (void)type; - if (x == y) return true; - return Int$equal(&x->first, &y->first, &Int$info) && Int$equal(&x->last, &y->last, &Int$info) && Int$equal(&x->step, &y->step, &Int$info); -} - -static Text_t Range$as_text(const Range_t *r, bool use_color, const TypeInfo *type) -{ - (void)type; - if (!r) return Text("Range"); - - return Text$format(use_color ? "\x1b[0;1mRange\x1b[m(first=%r, last=%r, step=%r)" - : "Range(first=%r, last=%r, step=%r)", - Int$as_text(&r->first, use_color, &Int$info), Int$as_text(&r->last, use_color, &Int$info), - Int$as_text(&r->step, use_color, &Int$info)); -} - -PUREFUNC public Range_t Range$reversed(Range_t r) -{ - return (Range_t){r.last, r.first, Int$negative(r.step)}; -} - -PUREFUNC public Range_t Range$by(Range_t r, Int_t step) -{ - return (Range_t){r.first, r.last, Int$times(step, r.step)}; -} - -public const TypeInfo Range = {sizeof(Range_t), __alignof(Range_t), {.tag=CustomInfo, .CustomInfo={ - .as_text=(void*)Range$as_text, - .compare=(void*)Range$compare, - .equal=(void*)Range$equal, -}}}; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/range.h b/builtins/range.h deleted file mode 100644 index 2a4f1d68..00000000 --- a/builtins/range.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -// Ranges represent numeric ranges - -PUREFUNC Range_t Range$reversed(Range_t r); -PUREFUNC Range_t Range$by(Range_t r, Int_t step); - -extern const TypeInfo Range; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/ranges.c b/builtins/ranges.c new file mode 100644 index 00000000..9dfd1efe --- /dev/null +++ b/builtins/ranges.c @@ -0,0 +1,63 @@ +// Functions that operate on numeric ranges + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "integers.h" +#include "text.h" +#include "types.h" +#include "util.h" + + +PUREFUNC static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo *type) +{ + (void)type; + if (x == y) return 0; + int32_t diff = Int$compare(&x->first, &y->first, &Int$info); + if (diff != 0) return diff; + diff = Int$compare(&x->last, &y->last, &Int$info); + if (diff != 0) return diff; + return Int$compare(&x->step, &y->step, &Int$info); +} + +PUREFUNC static bool Range$equal(const Range_t *x, const Range_t *y, const TypeInfo *type) +{ + (void)type; + if (x == y) return true; + return Int$equal(&x->first, &y->first, &Int$info) && Int$equal(&x->last, &y->last, &Int$info) && Int$equal(&x->step, &y->step, &Int$info); +} + +static Text_t Range$as_text(const Range_t *r, bool use_color, const TypeInfo *type) +{ + (void)type; + if (!r) return Text("Range"); + + return Text$format(use_color ? "\x1b[0;1mRange\x1b[m(first=%r, last=%r, step=%r)" + : "Range(first=%r, last=%r, step=%r)", + Int$as_text(&r->first, use_color, &Int$info), Int$as_text(&r->last, use_color, &Int$info), + Int$as_text(&r->step, use_color, &Int$info)); +} + +PUREFUNC public Range_t Range$reversed(Range_t r) +{ + return (Range_t){r.last, r.first, Int$negative(r.step)}; +} + +PUREFUNC public Range_t Range$by(Range_t r, Int_t step) +{ + return (Range_t){r.first, r.last, Int$times(step, r.step)}; +} + +public const TypeInfo Range = {sizeof(Range_t), __alignof(Range_t), {.tag=CustomInfo, .CustomInfo={ + .as_text=(void*)Range$as_text, + .compare=(void*)Range$compare, + .equal=(void*)Range$equal, +}}}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/ranges.h b/builtins/ranges.h new file mode 100644 index 00000000..2a4f1d68 --- /dev/null +++ b/builtins/ranges.h @@ -0,0 +1,10 @@ +#pragma once + +// Ranges represent numeric ranges + +PUREFUNC Range_t Range$reversed(Range_t r); +PUREFUNC Range_t Range$by(Range_t r, Int_t step); + +extern const TypeInfo Range; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/shell.c b/builtins/shell.c index a73b0aac..36b6a9ad 100644 --- a/builtins/shell.c +++ b/builtins/shell.c @@ -2,9 +2,9 @@ #include #include -#include "array.h" +#include "arrays.h" #include "integers.h" -#include "pattern.h" +#include "patterns.h" #include "shell.h" #include "text.h" #include "types.h" diff --git a/builtins/stdlib.c b/builtins/stdlib.c index e072a8fc..b8e40a54 100644 --- a/builtins/stdlib.c +++ b/builtins/stdlib.c @@ -13,9 +13,9 @@ #include "files.h" #include "integers.h" #include "metamethods.h" -#include "pattern.h" +#include "patterns.h" #include "siphash.h" -#include "table.h" +#include "tables.h" #include "text.h" #include "util.h" diff --git a/builtins/table.c b/builtins/table.c deleted file mode 100644 index 1b017ff6..00000000 --- a/builtins/table.c +++ /dev/null @@ -1,636 +0,0 @@ -// table.c - C Hash table implementation -// Copyright 2024 Bruce Hill -// Provided under the MIT license with the Commons Clause -// See included LICENSE for details. - -// Hash table (aka Dictionary) Implementation -// Hash keys and values are stored *by value* -// The hash insertion/lookup implementation is based on Lua's tables, -// which use a chained scatter with Brent's variation. - -#include -#include -#include -#include -#include -#include -#include - -#include "array.h" -#include "c_string.h" -#include "datatypes.h" -#include "memory.h" -#include "metamethods.h" -#include "siphash.h" -#include "table.h" -#include "text.h" -#include "types.h" -#include "util.h" - -// #define DEBUG_TABLES - -#ifdef DEBUG_TABLES -#define hdebug(fmt, ...) printf("\x1b[2m" fmt "\x1b[m" __VA_OPT__(,) __VA_ARGS__) -#else -#define hdebug(...) (void)0 -#endif - -// Helper accessors for type functions/values: -#define HASH_KEY(t, k) (generic_hash((k), type->TableInfo.key) % ((t).bucket_info->count)) -#define EQUAL_KEYS(x, y) (generic_equal((x), (y), type->TableInfo.key)) -#define END_OF_CHAIN UINT32_MAX - -#define GET_ENTRY(t, i) ((t).entries.data + (t).entries.stride*(i)) - -static const TypeInfo MemoryPointer = { - .size=sizeof(void*), - .align=__alignof__(void*), - .tag=PointerInfo, - .PointerInfo={ - .sigil="@", - .pointed=&Memory$info, - }, -}; - -const TypeInfo CStrToVoidStarTable = { - .size=sizeof(Table_t), - .align=__alignof__(Table_t), - .tag=TableInfo, - .TableInfo={.key=&CString$info, .value=&MemoryPointer}, -}; - -PUREFUNC static inline size_t entry_size(const TypeInfo *info) -{ - size_t size = (size_t)info->TableInfo.key->size; - if (info->TableInfo.value->align > 1 && size % (size_t)info->TableInfo.value->align) - size += (size_t)info->TableInfo.value->align - (size % (size_t)info->TableInfo.value->align); // padding - size += (size_t)info->TableInfo.value->size; - if (info->TableInfo.key->align > 1 && size % (size_t)info->TableInfo.key->align) - size += (size_t)info->TableInfo.key->align - (size % (size_t)info->TableInfo.key->align); // padding - return size; -} - -PUREFUNC static inline size_t entry_align(const TypeInfo *info) -{ - return (size_t)MAX(info->TableInfo.key->align, info->TableInfo.value->align); -} - -PUREFUNC static inline size_t value_offset(const TypeInfo *info) -{ - size_t offset = (size_t)info->TableInfo.key->size; - if ((size_t)info->TableInfo.value->align > 1 && offset % (size_t)info->TableInfo.value->align) - offset += (size_t)info->TableInfo.value->align - (offset % (size_t)info->TableInfo.value->align); // padding - return offset; -} - -static inline void hshow(const Table_t *t) -{ - hdebug("{"); - for (uint32_t i = 0; t->bucket_info && i < t->bucket_info->count; i++) { - if (i > 0) hdebug(" "); - if (t->bucket_info->buckets[i].occupied) - hdebug("[%d]=%d(%d)", i, t->bucket_info->buckets[i].index, t->bucket_info->buckets[i].next_bucket); - else - hdebug("[%d]=_", i); - } - hdebug("}\n"); -} - -static void maybe_copy_on_write(Table_t *t, const TypeInfo *type) -{ - if (t->entries.data_refcount != 0) - Array$compact(&t->entries, (int64_t)entry_size(type)); - - if (t->bucket_info && t->bucket_info->data_refcount != 0) { - size_t size = sizeof(bucket_info_t) + sizeof(bucket_t[t->bucket_info->count]); - t->bucket_info = memcpy(GC_MALLOC(size), t->bucket_info, size); - t->bucket_info->data_refcount = 0; - } -} - -// Return address of value or NULL -PUREFUNC public void *Table$get_raw(Table_t t, const void *key, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - if (!key || !t.bucket_info) return NULL; - - uint64_t hash = HASH_KEY(t, key); - hshow(&t); - hdebug("Getting value with initial probe at %u\n", hash); - bucket_t *buckets = t.bucket_info->buckets; - for (uint64_t i = hash; buckets[i].occupied; i = buckets[i].next_bucket) { - hdebug("Checking against key in bucket %u\n", i); - void *entry = GET_ENTRY(t, buckets[i].index); - if (EQUAL_KEYS(entry, key)) { - hdebug("Found key!\n"); - return entry + value_offset(type); - } - if (buckets[i].next_bucket == END_OF_CHAIN) - break; - } - return NULL; -} - -PUREFUNC public void *Table$get(Table_t t, const void *key, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - for (const Table_t *iter = &t; iter; iter = iter->fallback) { - void *ret = Table$get_raw(*iter, key, type); - if (ret) return ret; - } - return NULL; -} - -static void Table$set_bucket(Table_t *t, const void *entry, int32_t index, const TypeInfo *type) -{ - assert(t->bucket_info); - hshow(t); - const void *key = entry; - bucket_t *buckets = t->bucket_info->buckets; - uint64_t hash = HASH_KEY(*t, key); - hdebug("Hash value (mod %u) = %u\n", t->bucket_info->count, hash); - bucket_t *bucket = &buckets[hash]; - if (!bucket->occupied) { - hdebug("Got an empty space\n"); - // Empty space: - bucket->occupied = 1; - bucket->index = index; - bucket->next_bucket = END_OF_CHAIN; - hshow(t); - return; - } - - hdebug("Collision detected in bucket %u (entry %u)\n", hash, bucket->index); - - while (buckets[t->bucket_info->last_free].occupied) { - assert(t->bucket_info->last_free > 0); - --t->bucket_info->last_free; - } - - uint64_t collided_hash = HASH_KEY(*t, GET_ENTRY(*t, bucket->index)); - if (collided_hash != hash) { // Collided with a mid-chain entry - hdebug("Hit a mid-chain entry at bucket %u (chain starting at %u)\n", hash, collided_hash); - // Find chain predecessor - uint64_t predecessor = collided_hash; - while (buckets[predecessor].next_bucket != hash) - predecessor = buckets[predecessor].next_bucket; - - // Move mid-chain entry to free space and update predecessor - buckets[predecessor].next_bucket = t->bucket_info->last_free; - buckets[t->bucket_info->last_free] = *bucket; - } else { // Collided with the start of a chain - hdebug("Hit start of a chain\n"); - uint64_t end_of_chain = hash; - while (buckets[end_of_chain].next_bucket != END_OF_CHAIN) - end_of_chain = buckets[end_of_chain].next_bucket; - hdebug("Appending to chain\n"); - // Chain now ends on the free space: - buckets[end_of_chain].next_bucket = t->bucket_info->last_free; - bucket = &buckets[t->bucket_info->last_free]; - } - - bucket->occupied = 1; - bucket->index = index; - bucket->next_bucket = END_OF_CHAIN; - hshow(t); -} - -static void hashmap_resize_buckets(Table_t *t, uint32_t new_capacity, const TypeInfo *type) -{ - if (__builtin_expect(new_capacity > TABLE_MAX_BUCKETS, 0)) - fail("Table has exceeded the maximum table size (2^31) and cannot grow further!"); - hdebug("About to resize from %u to %u\n", t->bucket_info ? t->bucket_info->count : 0, new_capacity); - hshow(t); - size_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[new_capacity]); - t->bucket_info = GC_MALLOC_ATOMIC(alloc_size); - memset(t->bucket_info->buckets, 0, sizeof(bucket_t[new_capacity])); - t->bucket_info->count = new_capacity; - t->bucket_info->last_free = new_capacity-1; - // Rehash: - for (int64_t i = 0; i < Table$length(*t); i++) { - hdebug("Rehashing %u\n", i); - Table$set_bucket(t, GET_ENTRY(*t, i), i, type); - } - - hshow(t); - hdebug("Finished resizing\n"); -} - -// Return address of value -#pragma GCC diagnostic ignored "-Wstack-protector" -public void *Table$reserve(Table_t *t, const void *key, const void *value, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - if (!t || !key) return NULL; - hshow(t); - - int64_t key_size = type->TableInfo.key->size, - value_size = type->TableInfo.value->size; - if (!t->bucket_info || t->bucket_info->count == 0) { - hashmap_resize_buckets(t, 4, type); - } else { - // Check if we are clobbering a value: - void *value_home = Table$get_raw(*t, key, type); - if (value_home) { // Update existing slot - // Ensure that `value_home` is still inside t->entries, even if COW occurs - ptrdiff_t offset = value_home - t->entries.data; - maybe_copy_on_write(t, type); - value_home = t->entries.data + offset; - - if (value && value_size > 0) - memcpy(value_home, value, (size_t)value_size); - - return value_home; - } - } - // Otherwise add a new entry: - - // Resize buckets if necessary - if (t->entries.length >= (int64_t)t->bucket_info->count) { - uint32_t newsize = (uint32_t)t->bucket_info->count + MIN((uint32_t)t->bucket_info->count, 64); - if (__builtin_expect(newsize > TABLE_MAX_BUCKETS, 0)) - newsize = t->entries.length + 1; - hashmap_resize_buckets(t, newsize, type); - } - - if (!value && value_size > 0) { - for (Table_t *iter = t->fallback; iter; iter = iter->fallback) { - value = Table$get_raw(*iter, key, type); - if (value) break; - } - } - - maybe_copy_on_write(t, type); - - char buf[entry_size(type)]; - memset(buf, 0, sizeof(buf)); - memcpy(buf, key, (size_t)key_size); - if (value && value_size > 0) - memcpy(buf + value_offset(type), value, (size_t)value_size); - else - memset(buf + value_offset(type), 0, (size_t)value_size); - Array$insert(&t->entries, buf, I(0), (int64_t)entry_size(type)); - - int64_t entry_index = t->entries.length-1; - void *entry = GET_ENTRY(*t, entry_index); - Table$set_bucket(t, entry, entry_index, type); - return entry + value_offset(type); -} - -public void Table$set(Table_t *t, const void *key, const void *value, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - (void)Table$reserve(t, key, value, type); -} - -public void Table$remove(Table_t *t, const void *key, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - if (!t || Table$length(*t) == 0) return; - - // TODO: this work doesn't need to be done if the key is already missing - maybe_copy_on_write(t, type); - - // If unspecified, pop the last key: - if (!key) - key = GET_ENTRY(*t, t->entries.length-1); - - // Steps: look up the bucket for the removed key - // If missing, then return immediately - // Swap last key/value into the removed bucket's index1 - // Zero out the last key/value and decrement the count - // Find the last key/value's bucket and update its index1 - // Look up the bucket for the removed key - // If bucket is first in chain: - // Move bucket->next to bucket's spot - // zero out bucket->next's old spot - // maybe update lastfree_index1 to second-in-chain's index - // Else: - // set prev->next = bucket->next - // zero out bucket - // maybe update lastfree_index1 to removed bucket's index - - uint64_t hash = HASH_KEY(*t, key); - hdebug("Removing key with hash %u\n", hash); - bucket_t *bucket, *prev = NULL; - for (uint64_t i = hash; t->bucket_info->buckets[i].occupied; i = t->bucket_info->buckets[i].next_bucket) { - if (EQUAL_KEYS(GET_ENTRY(*t, t->bucket_info->buckets[i].index), key)) { - bucket = &t->bucket_info->buckets[i]; - hdebug("Found key to delete in bucket %u\n", i); - goto found_it; - } - if (t->bucket_info->buckets[i].next_bucket == END_OF_CHAIN) - return; - prev = &t->bucket_info->buckets[i]; - } - return; - - found_it:; - assert(bucket->occupied); - - // Always remove the last entry. If we need to remove some other entry, - // swap the other entry into the last position and then remove the last - // entry. This disturbs the ordering of the table, but keeps removal O(1) - // instead of O(N) - int64_t last_entry = t->entries.length-1; - if (bucket->index != last_entry) { - hdebug("Removing key/value from the middle of the entries array\n"); - - // Find the bucket that points to the last entry's index: - uint64_t i = HASH_KEY(*t, GET_ENTRY(*t, last_entry)); - while (t->bucket_info->buckets[i].index != last_entry) - i = t->bucket_info->buckets[i].next_bucket; - // Update the bucket to point to the last entry's new home (the space - // where the removed entry currently sits): - t->bucket_info->buckets[i].index = bucket->index; - - // Clobber the entry being removed (in the middle of the array) with - // the last entry: - memcpy(GET_ENTRY(*t, bucket->index), GET_ENTRY(*t, last_entry), entry_size(type)); - } - - // Last entry is being removed, so clear it out to be safe: - memset(GET_ENTRY(*t, last_entry), 0, entry_size(type)); - - Array$remove_at(&t->entries, I(t->entries.length), I(1), (int64_t)entry_size(type)); - - int64_t bucket_to_clear; - if (prev) { // Middle (or end) of a chain - hdebug("Removing from middle of a chain\n"); - bucket_to_clear = (bucket - t->bucket_info->buckets); - prev->next_bucket = bucket->next_bucket; - } else if (bucket->next_bucket != END_OF_CHAIN) { // Start of a chain - hdebug("Removing from start of a chain\n"); - bucket_to_clear = bucket->next_bucket; - *bucket = t->bucket_info->buckets[bucket_to_clear]; - } else { // Empty chain - hdebug("Removing from empty chain\n"); - bucket_to_clear = (bucket - t->bucket_info->buckets); - } - - t->bucket_info->buckets[bucket_to_clear] = (bucket_t){0}; - if (bucket_to_clear > t->bucket_info->last_free) - t->bucket_info->last_free = bucket_to_clear; - - hshow(t); -} - -CONSTFUNC public void *Table$entry(Table_t t, int64_t n) -{ - if (n < 1 || n > Table$length(t)) - return NULL; - return GET_ENTRY(t, n-1); -} - -public void Table$clear(Table_t *t) -{ - memset(t, 0, sizeof(Table_t)); -} - -public Table_t Table$sorted(Table_t t, const TypeInfo *type) -{ - Closure_t cmp = (Closure_t){.fn=generic_compare, .userdata=(void*)type->TableInfo.key}; - Array_t entries = Array$sorted(t.entries, cmp, (int64_t)entry_size(type)); - return Table$from_entries(entries, type); -} - -PUREFUNC public bool Table$equal(const Table_t *x, const Table_t *y, const TypeInfo *type) -{ - if (x == y) return true; - - assert(type->tag == TableInfo); - if (Table$length(*x) != Table$length(*y)) - return false; - - if ((x->fallback != NULL) != (y->fallback != NULL)) - return false; - - return (Table$compare(x, y, type) == 0); -} - -PUREFUNC public int32_t Table$compare(const Table_t *x, const Table_t *y, const TypeInfo *type) -{ - if (x == y) return 0; - - assert(type->tag == TableInfo); - auto table = type->TableInfo; - if (x->entries.length == 0) - return 0; - else if (x->entries.length != y->entries.length) - return (x->entries.length > y->entries.length) - (x->entries.length < y->entries.length); - - for (int64_t i = 0; i < x->entries.length; i++) { - void *x_key = x->entries.data + x->entries.stride * i; - void *y_key = y->entries.data + y->entries.stride * i; - int32_t diff = generic_compare(x_key, y_key, table.key); - if (diff != 0) return diff; - void *x_value = x_key + value_offset(type); - void *y_value = y_key + value_offset(type); - diff = generic_compare(x_value, y_value, table.value); - if (diff != 0) return diff; - } - - if (!x->fallback != !y->fallback) { - return (!x->fallback) - (!y->fallback); - } else if (x->fallback && y->fallback) { - return generic_compare(x->fallback, y->fallback, type); - } - - return 0; -} - -PUREFUNC public uint64_t Table$hash(const Table_t *t, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - // Table hashes are computed as: - // hash(hash(t.keys), hash(t.values), hash(t.fallback), hash(t.default)) - // Where fallback and default hash to zero if absent - auto table = type->TableInfo; - uint64_t components[] = { - Array$hash(&t->entries, Array$info(table.key)), - Array$hash(&t->entries + value_offset(type), Array$info(table.value)), - t->fallback ? Table$hash(t->fallback, type) : 0, - }; - return siphash24((void*)&components, sizeof(components)); -} - -public Text_t Table$as_text(const Table_t *t, bool colorize, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - auto table = type->TableInfo; - - if (!t) { - if (table.value != &Void$info) - return Text$concat( - Text("{"), - generic_as_text(NULL, false, table.key), - Text(":"), - generic_as_text(NULL, false, table.value), - Text("}")); - else - return Text$concat( - Text("{"), - generic_as_text(NULL, false, table.key), - Text("}")); - } - - int64_t val_off = (int64_t)value_offset(type); - Text_t text = Text("{"); - for (int64_t i = 0, length = Table$length(*t); i < length; i++) { - if (i > 0) - text = Text$concat(text, Text(", ")); - void *entry = GET_ENTRY(*t, i); - text = Text$concat(text, generic_as_text(entry, colorize, table.key)); - if (table.value != &Void$info) - text = Text$concat(text, Text(":"), generic_as_text(entry + val_off, colorize, table.value)); - } - - if (t->fallback) { - text = Text$concat(text, Text("; fallback="), Table$as_text(t->fallback, colorize, type)); - } - - text = Text$concat(text, Text("}")); - return text; -} - -public Table_t Table$from_entries(Array_t entries, const TypeInfo *type) -{ - assert(type->tag == TableInfo); - if (entries.length == 0) - return (Table_t){}; - - Table_t t = {}; - int64_t length = entries.length + entries.length / 4; - size_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[length]); - t.bucket_info = GC_MALLOC_ATOMIC(alloc_size); - memset(t.bucket_info->buckets, 0, sizeof(bucket_t[length])); - t.bucket_info->count = length; - t.bucket_info->last_free = length-1; - - size_t offset = value_offset(type); - for (int64_t i = 0; i < entries.length; i++) { - void *key = entries.data + i*entries.stride; - Table$set(&t, key, key + offset, type); - } - return t; -} - -// Overlap is "set intersection" in formal terms -public Table_t Table$overlap(Table_t a, Table_t b, const TypeInfo *type) -{ - // Return a table such that t[k]==a[k] for all k such that a:has(k), b:has(k), and a[k]==b[k] - Table_t result = {}; - const size_t offset = value_offset(type); - for (int64_t i = 0; i < Table$length(a); i++) { - void *key = GET_ENTRY(a, i); - void *a_value = key + offset; - void *b_value = Table$get(b, key, type); - if (b_value && generic_equal(a_value, b_value, type->TableInfo.value)) - Table$set(&result, key, a_value, type); - } - - if (a.fallback) { - result.fallback = new(Table_t); - *result.fallback = Table$overlap(*a.fallback, b, type); - } - - return result; -} - -// With is "set union" in formal terms -public Table_t Table$with(Table_t a, Table_t b, const TypeInfo *type) -{ - // return a table such that t[k]==b[k] for all k such that b:has(k), and t[k]==a[k] for all k such that a:has(k) and not b:has(k) - Table_t result = {}; - const size_t offset = value_offset(type); - for (int64_t i = 0; i < Table$length(a); i++) { - void *key = GET_ENTRY(a, i); - Table$set(&result, key, key + offset, type); - } - for (int64_t i = 0; i < Table$length(b); i++) { - void *key = GET_ENTRY(b, i); - Table$set(&result, key, key + offset, type); - } - - if (a.fallback && b.fallback) { - result.fallback = new(Table_t); - *result.fallback = Table$with(*a.fallback, *b.fallback, type); - } else { - result.fallback = a.fallback ? a.fallback : b.fallback; - } - - return result; -} - -// Without is "set difference" in formal terms -public Table_t Table$without(Table_t a, Table_t b, const TypeInfo *type) -{ - // Return a table such that t[k]==a[k] for all k such that not b:has(k) or b[k] != a[k] - Table_t result = {}; - const size_t offset = value_offset(type); - for (int64_t i = 0; i < Table$length(a); i++) { - void *key = GET_ENTRY(a, i); - void *a_value = key + offset; - void *b_value = Table$get(b, key, type); - if (!b_value || !generic_equal(a_value, b_value, type->TableInfo.value)) - Table$set(&result, key, a_value, type); - } - - if (a.fallback) { - result.fallback = new(Table_t); - *result.fallback = Table$without(*a.fallback, b, type); - } - - return result; -} - -PUREFUNC public bool Table$is_subset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type) -{ - if (a.entries.length > b.entries.length || (strict && a.entries.length == b.entries.length)) - return false; - - for (int64_t i = 0; i < Table$length(a); i++) { - void *found = Table$get_raw(b, GET_ENTRY(a, i), type); - if (!found) return false; - } - return true; -} - -PUREFUNC public bool Table$is_superset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type) -{ - return Table$is_subset_of(b, a, strict, type); -} - -PUREFUNC public void *Table$str_get(Table_t t, const char *key) -{ - void **ret = Table$get(t, &key, &CStrToVoidStarTable); - return ret ? *ret : NULL; -} - -PUREFUNC public void *Table$str_get_raw(Table_t t, const char *key) -{ - void **ret = Table$get_raw(t, &key, &CStrToVoidStarTable); - return ret ? *ret : NULL; -} - -public void *Table$str_reserve(Table_t *t, const char *key, const void *value) -{ - return Table$reserve(t, &key, &value, &CStrToVoidStarTable); -} - -public void Table$str_set(Table_t *t, const char *key, const void *value) -{ - Table$set(t, &key, &value, &CStrToVoidStarTable); -} - -public void Table$str_remove(Table_t *t, const char *key) -{ - return Table$remove(t, &key, &CStrToVoidStarTable); -} - -CONSTFUNC public void *Table$str_entry(Table_t t, int64_t n) -{ - return Table$entry(t, n); -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/table.h b/builtins/table.h deleted file mode 100644 index e589e6ec..00000000 --- a/builtins/table.h +++ /dev/null @@ -1,84 +0,0 @@ -#pragma once - -// Hash table datastructure with methods and type information - -#include -#include -#include - -#include "array.h" -#include "datatypes.h" -#include "types.h" -#include "util.h" - -#define Table(key_t, val_t, key_info, value_info, fb, N, ...) ({ \ - struct { key_t k; val_t v; } ents[N] = {__VA_ARGS__}; \ - Table_t table = Table$from_entries((Array_t){ \ - .data=memcpy(GC_MALLOC(sizeof(ents)), ents, sizeof(ents)), \ - .length=sizeof(ents)/sizeof(ents[0]), \ - .stride=(void*)&ents[1] - (void*)&ents[0], \ - }, Table$info(key_info, value_info)); \ - table.fallback = fb; \ - table; }) -#define Set(item_t, item_info, N, ...) ({ \ - item_t ents[N] = {__VA_ARGS__}; \ - Table_t set = Table$from_entries((Array_t){ \ - .data=memcpy(GC_MALLOC(sizeof(ents)), ents, sizeof(ents)), \ - .length=sizeof(ents)/sizeof(ents[0]), \ - .stride=(void*)&ents[1] - (void*)&ents[0], \ - }, Set$info(item_info)); \ - set; }) - -Table_t Table$from_entries(Array_t entries, const TypeInfo *type); -void *Table$get(Table_t t, const void *key, const TypeInfo *type); -#define Table$get_optional(table_expr, key_t, val_t, key_expr, nonnull_var, nonnull_expr, null_expr, info_expr) ({ \ - const Table_t t = table_expr; const key_t k = key_expr; \ - val_t *nonnull_var = Table$get(t, &k, info_expr); \ - nonnull_var ? nonnull_expr : null_expr; }) -#define Table$has_value(table_expr, key_expr, info_expr) ({ \ - const Table_t t = table_expr; __typeof(key_expr) k = key_expr; \ - (Table$get(t, &k, info_expr) != NULL); }) -PUREFUNC void *Table$get_raw(Table_t t, const void *key, const TypeInfo *type); -CONSTFUNC void *Table$entry(Table_t t, int64_t n); -void *Table$reserve(Table_t *t, const void *key, const void *value, const TypeInfo *type); -void Table$set(Table_t *t, const void *key, const void *value, const TypeInfo *type); -#define Table$set_value(t, key_expr, value_expr, type) ({ __typeof(key_expr) k = key_expr; __typeof(value_expr) v = value_expr; \ - Table$set(t, &k, &v, type); }) -#define Table$reserve_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$reserve(t, &k, NULL, type); }) -#define Table$bump(t_expr, key_expr, amount_expr, type) ({ __typeof(key_expr) key = key_expr; \ - Table_t *t = t_expr; \ - __typeof(amount_expr) *val = Table$get_raw(*t, &key, type); \ - if (val) *val += amount_expr; \ - else { __typeof(amount_expr) init = amount_expr; Table$set(t, &key, &init, type); } (void)0; }) - -void Table$remove(Table_t *t, const void *key, const TypeInfo *type); -#define Table$remove_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$remove(t, &k, type); }) - -Table_t Table$overlap(Table_t a, Table_t b, const TypeInfo *type); -Table_t Table$with(Table_t a, Table_t b, const TypeInfo *type); -Table_t Table$without(Table_t a, Table_t b, const TypeInfo *type); -PUREFUNC bool Table$is_subset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type); -PUREFUNC bool Table$is_superset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type); - -void Table$clear(Table_t *t); -Table_t Table$sorted(Table_t t, const TypeInfo *type); -void Table$mark_copy_on_write(Table_t *t); -#define TABLE_INCREF(t) ({ ARRAY_INCREF((t).entries); if ((t).bucket_info) (t).bucket_info->data_refcount += ((t).bucket_info->data_refcount < TABLE_MAX_DATA_REFCOUNT); }) -#define TABLE_COPY(t) ({ TABLE_INCREF(t); t; }) -PUREFUNC int32_t Table$compare(const Table_t *x, const Table_t *y, const TypeInfo *type); -PUREFUNC bool Table$equal(const Table_t *x, const Table_t *y, const TypeInfo *type); -PUREFUNC uint64_t Table$hash(const Table_t *t, const TypeInfo *type); -Text_t Table$as_text(const Table_t *t, bool colorize, const TypeInfo *type); - -CONSTFUNC void *Table$str_entry(Table_t t, int64_t n); -PUREFUNC void *Table$str_get(Table_t t, const char *key); -PUREFUNC void *Table$str_get_raw(Table_t t, const char *key); -void Table$str_set(Table_t *t, const char *key, const void *value); -void *Table$str_reserve(Table_t *t, const char *key, const void *value); -void Table$str_remove(Table_t *t, const char *key); - -#define Table$length(t) ((t).entries.length) - -extern const TypeInfo CStrToVoidStarTable; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/tables.c b/builtins/tables.c new file mode 100644 index 00000000..17e7339f --- /dev/null +++ b/builtins/tables.c @@ -0,0 +1,636 @@ +// table.c - C Hash table implementation +// Copyright 2024 Bruce Hill +// Provided under the MIT license with the Commons Clause +// See included LICENSE for details. + +// Hash table (aka Dictionary) Implementation +// Hash keys and values are stored *by value* +// The hash insertion/lookup implementation is based on Lua's tables, +// which use a chained scatter with Brent's variation. + +#include +#include +#include +#include +#include +#include +#include + +#include "arrays.h" +#include "c_string.h" +#include "datatypes.h" +#include "memory.h" +#include "metamethods.h" +#include "siphash.h" +#include "tables.h" +#include "text.h" +#include "types.h" +#include "util.h" + +// #define DEBUG_TABLES + +#ifdef DEBUG_TABLES +#define hdebug(fmt, ...) printf("\x1b[2m" fmt "\x1b[m" __VA_OPT__(,) __VA_ARGS__) +#else +#define hdebug(...) (void)0 +#endif + +// Helper accessors for type functions/values: +#define HASH_KEY(t, k) (generic_hash((k), type->TableInfo.key) % ((t).bucket_info->count)) +#define EQUAL_KEYS(x, y) (generic_equal((x), (y), type->TableInfo.key)) +#define END_OF_CHAIN UINT32_MAX + +#define GET_ENTRY(t, i) ((t).entries.data + (t).entries.stride*(i)) + +static const TypeInfo MemoryPointer = { + .size=sizeof(void*), + .align=__alignof__(void*), + .tag=PointerInfo, + .PointerInfo={ + .sigil="@", + .pointed=&Memory$info, + }, +}; + +const TypeInfo CStrToVoidStarTable = { + .size=sizeof(Table_t), + .align=__alignof__(Table_t), + .tag=TableInfo, + .TableInfo={.key=&CString$info, .value=&MemoryPointer}, +}; + +PUREFUNC static inline size_t entry_size(const TypeInfo *info) +{ + size_t size = (size_t)info->TableInfo.key->size; + if (info->TableInfo.value->align > 1 && size % (size_t)info->TableInfo.value->align) + size += (size_t)info->TableInfo.value->align - (size % (size_t)info->TableInfo.value->align); // padding + size += (size_t)info->TableInfo.value->size; + if (info->TableInfo.key->align > 1 && size % (size_t)info->TableInfo.key->align) + size += (size_t)info->TableInfo.key->align - (size % (size_t)info->TableInfo.key->align); // padding + return size; +} + +PUREFUNC static inline size_t entry_align(const TypeInfo *info) +{ + return (size_t)MAX(info->TableInfo.key->align, info->TableInfo.value->align); +} + +PUREFUNC static inline size_t value_offset(const TypeInfo *info) +{ + size_t offset = (size_t)info->TableInfo.key->size; + if ((size_t)info->TableInfo.value->align > 1 && offset % (size_t)info->TableInfo.value->align) + offset += (size_t)info->TableInfo.value->align - (offset % (size_t)info->TableInfo.value->align); // padding + return offset; +} + +static inline void hshow(const Table_t *t) +{ + hdebug("{"); + for (uint32_t i = 0; t->bucket_info && i < t->bucket_info->count; i++) { + if (i > 0) hdebug(" "); + if (t->bucket_info->buckets[i].occupied) + hdebug("[%d]=%d(%d)", i, t->bucket_info->buckets[i].index, t->bucket_info->buckets[i].next_bucket); + else + hdebug("[%d]=_", i); + } + hdebug("}\n"); +} + +static void maybe_copy_on_write(Table_t *t, const TypeInfo *type) +{ + if (t->entries.data_refcount != 0) + Array$compact(&t->entries, (int64_t)entry_size(type)); + + if (t->bucket_info && t->bucket_info->data_refcount != 0) { + size_t size = sizeof(bucket_info_t) + sizeof(bucket_t[t->bucket_info->count]); + t->bucket_info = memcpy(GC_MALLOC(size), t->bucket_info, size); + t->bucket_info->data_refcount = 0; + } +} + +// Return address of value or NULL +PUREFUNC public void *Table$get_raw(Table_t t, const void *key, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + if (!key || !t.bucket_info) return NULL; + + uint64_t hash = HASH_KEY(t, key); + hshow(&t); + hdebug("Getting value with initial probe at %u\n", hash); + bucket_t *buckets = t.bucket_info->buckets; + for (uint64_t i = hash; buckets[i].occupied; i = buckets[i].next_bucket) { + hdebug("Checking against key in bucket %u\n", i); + void *entry = GET_ENTRY(t, buckets[i].index); + if (EQUAL_KEYS(entry, key)) { + hdebug("Found key!\n"); + return entry + value_offset(type); + } + if (buckets[i].next_bucket == END_OF_CHAIN) + break; + } + return NULL; +} + +PUREFUNC public void *Table$get(Table_t t, const void *key, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + for (const Table_t *iter = &t; iter; iter = iter->fallback) { + void *ret = Table$get_raw(*iter, key, type); + if (ret) return ret; + } + return NULL; +} + +static void Table$set_bucket(Table_t *t, const void *entry, int32_t index, const TypeInfo *type) +{ + assert(t->bucket_info); + hshow(t); + const void *key = entry; + bucket_t *buckets = t->bucket_info->buckets; + uint64_t hash = HASH_KEY(*t, key); + hdebug("Hash value (mod %u) = %u\n", t->bucket_info->count, hash); + bucket_t *bucket = &buckets[hash]; + if (!bucket->occupied) { + hdebug("Got an empty space\n"); + // Empty space: + bucket->occupied = 1; + bucket->index = index; + bucket->next_bucket = END_OF_CHAIN; + hshow(t); + return; + } + + hdebug("Collision detected in bucket %u (entry %u)\n", hash, bucket->index); + + while (buckets[t->bucket_info->last_free].occupied) { + assert(t->bucket_info->last_free > 0); + --t->bucket_info->last_free; + } + + uint64_t collided_hash = HASH_KEY(*t, GET_ENTRY(*t, bucket->index)); + if (collided_hash != hash) { // Collided with a mid-chain entry + hdebug("Hit a mid-chain entry at bucket %u (chain starting at %u)\n", hash, collided_hash); + // Find chain predecessor + uint64_t predecessor = collided_hash; + while (buckets[predecessor].next_bucket != hash) + predecessor = buckets[predecessor].next_bucket; + + // Move mid-chain entry to free space and update predecessor + buckets[predecessor].next_bucket = t->bucket_info->last_free; + buckets[t->bucket_info->last_free] = *bucket; + } else { // Collided with the start of a chain + hdebug("Hit start of a chain\n"); + uint64_t end_of_chain = hash; + while (buckets[end_of_chain].next_bucket != END_OF_CHAIN) + end_of_chain = buckets[end_of_chain].next_bucket; + hdebug("Appending to chain\n"); + // Chain now ends on the free space: + buckets[end_of_chain].next_bucket = t->bucket_info->last_free; + bucket = &buckets[t->bucket_info->last_free]; + } + + bucket->occupied = 1; + bucket->index = index; + bucket->next_bucket = END_OF_CHAIN; + hshow(t); +} + +static void hashmap_resize_buckets(Table_t *t, uint32_t new_capacity, const TypeInfo *type) +{ + if (__builtin_expect(new_capacity > TABLE_MAX_BUCKETS, 0)) + fail("Table has exceeded the maximum table size (2^31) and cannot grow further!"); + hdebug("About to resize from %u to %u\n", t->bucket_info ? t->bucket_info->count : 0, new_capacity); + hshow(t); + size_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[new_capacity]); + t->bucket_info = GC_MALLOC_ATOMIC(alloc_size); + memset(t->bucket_info->buckets, 0, sizeof(bucket_t[new_capacity])); + t->bucket_info->count = new_capacity; + t->bucket_info->last_free = new_capacity-1; + // Rehash: + for (int64_t i = 0; i < Table$length(*t); i++) { + hdebug("Rehashing %u\n", i); + Table$set_bucket(t, GET_ENTRY(*t, i), i, type); + } + + hshow(t); + hdebug("Finished resizing\n"); +} + +// Return address of value +#pragma GCC diagnostic ignored "-Wstack-protector" +public void *Table$reserve(Table_t *t, const void *key, const void *value, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + if (!t || !key) return NULL; + hshow(t); + + int64_t key_size = type->TableInfo.key->size, + value_size = type->TableInfo.value->size; + if (!t->bucket_info || t->bucket_info->count == 0) { + hashmap_resize_buckets(t, 4, type); + } else { + // Check if we are clobbering a value: + void *value_home = Table$get_raw(*t, key, type); + if (value_home) { // Update existing slot + // Ensure that `value_home` is still inside t->entries, even if COW occurs + ptrdiff_t offset = value_home - t->entries.data; + maybe_copy_on_write(t, type); + value_home = t->entries.data + offset; + + if (value && value_size > 0) + memcpy(value_home, value, (size_t)value_size); + + return value_home; + } + } + // Otherwise add a new entry: + + // Resize buckets if necessary + if (t->entries.length >= (int64_t)t->bucket_info->count) { + uint32_t newsize = (uint32_t)t->bucket_info->count + MIN((uint32_t)t->bucket_info->count, 64); + if (__builtin_expect(newsize > TABLE_MAX_BUCKETS, 0)) + newsize = t->entries.length + 1; + hashmap_resize_buckets(t, newsize, type); + } + + if (!value && value_size > 0) { + for (Table_t *iter = t->fallback; iter; iter = iter->fallback) { + value = Table$get_raw(*iter, key, type); + if (value) break; + } + } + + maybe_copy_on_write(t, type); + + char buf[entry_size(type)]; + memset(buf, 0, sizeof(buf)); + memcpy(buf, key, (size_t)key_size); + if (value && value_size > 0) + memcpy(buf + value_offset(type), value, (size_t)value_size); + else + memset(buf + value_offset(type), 0, (size_t)value_size); + Array$insert(&t->entries, buf, I(0), (int64_t)entry_size(type)); + + int64_t entry_index = t->entries.length-1; + void *entry = GET_ENTRY(*t, entry_index); + Table$set_bucket(t, entry, entry_index, type); + return entry + value_offset(type); +} + +public void Table$set(Table_t *t, const void *key, const void *value, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + (void)Table$reserve(t, key, value, type); +} + +public void Table$remove(Table_t *t, const void *key, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + if (!t || Table$length(*t) == 0) return; + + // TODO: this work doesn't need to be done if the key is already missing + maybe_copy_on_write(t, type); + + // If unspecified, pop the last key: + if (!key) + key = GET_ENTRY(*t, t->entries.length-1); + + // Steps: look up the bucket for the removed key + // If missing, then return immediately + // Swap last key/value into the removed bucket's index1 + // Zero out the last key/value and decrement the count + // Find the last key/value's bucket and update its index1 + // Look up the bucket for the removed key + // If bucket is first in chain: + // Move bucket->next to bucket's spot + // zero out bucket->next's old spot + // maybe update lastfree_index1 to second-in-chain's index + // Else: + // set prev->next = bucket->next + // zero out bucket + // maybe update lastfree_index1 to removed bucket's index + + uint64_t hash = HASH_KEY(*t, key); + hdebug("Removing key with hash %u\n", hash); + bucket_t *bucket, *prev = NULL; + for (uint64_t i = hash; t->bucket_info->buckets[i].occupied; i = t->bucket_info->buckets[i].next_bucket) { + if (EQUAL_KEYS(GET_ENTRY(*t, t->bucket_info->buckets[i].index), key)) { + bucket = &t->bucket_info->buckets[i]; + hdebug("Found key to delete in bucket %u\n", i); + goto found_it; + } + if (t->bucket_info->buckets[i].next_bucket == END_OF_CHAIN) + return; + prev = &t->bucket_info->buckets[i]; + } + return; + + found_it:; + assert(bucket->occupied); + + // Always remove the last entry. If we need to remove some other entry, + // swap the other entry into the last position and then remove the last + // entry. This disturbs the ordering of the table, but keeps removal O(1) + // instead of O(N) + int64_t last_entry = t->entries.length-1; + if (bucket->index != last_entry) { + hdebug("Removing key/value from the middle of the entries array\n"); + + // Find the bucket that points to the last entry's index: + uint64_t i = HASH_KEY(*t, GET_ENTRY(*t, last_entry)); + while (t->bucket_info->buckets[i].index != last_entry) + i = t->bucket_info->buckets[i].next_bucket; + // Update the bucket to point to the last entry's new home (the space + // where the removed entry currently sits): + t->bucket_info->buckets[i].index = bucket->index; + + // Clobber the entry being removed (in the middle of the array) with + // the last entry: + memcpy(GET_ENTRY(*t, bucket->index), GET_ENTRY(*t, last_entry), entry_size(type)); + } + + // Last entry is being removed, so clear it out to be safe: + memset(GET_ENTRY(*t, last_entry), 0, entry_size(type)); + + Array$remove_at(&t->entries, I(t->entries.length), I(1), (int64_t)entry_size(type)); + + int64_t bucket_to_clear; + if (prev) { // Middle (or end) of a chain + hdebug("Removing from middle of a chain\n"); + bucket_to_clear = (bucket - t->bucket_info->buckets); + prev->next_bucket = bucket->next_bucket; + } else if (bucket->next_bucket != END_OF_CHAIN) { // Start of a chain + hdebug("Removing from start of a chain\n"); + bucket_to_clear = bucket->next_bucket; + *bucket = t->bucket_info->buckets[bucket_to_clear]; + } else { // Empty chain + hdebug("Removing from empty chain\n"); + bucket_to_clear = (bucket - t->bucket_info->buckets); + } + + t->bucket_info->buckets[bucket_to_clear] = (bucket_t){0}; + if (bucket_to_clear > t->bucket_info->last_free) + t->bucket_info->last_free = bucket_to_clear; + + hshow(t); +} + +CONSTFUNC public void *Table$entry(Table_t t, int64_t n) +{ + if (n < 1 || n > Table$length(t)) + return NULL; + return GET_ENTRY(t, n-1); +} + +public void Table$clear(Table_t *t) +{ + memset(t, 0, sizeof(Table_t)); +} + +public Table_t Table$sorted(Table_t t, const TypeInfo *type) +{ + Closure_t cmp = (Closure_t){.fn=generic_compare, .userdata=(void*)type->TableInfo.key}; + Array_t entries = Array$sorted(t.entries, cmp, (int64_t)entry_size(type)); + return Table$from_entries(entries, type); +} + +PUREFUNC public bool Table$equal(const Table_t *x, const Table_t *y, const TypeInfo *type) +{ + if (x == y) return true; + + assert(type->tag == TableInfo); + if (Table$length(*x) != Table$length(*y)) + return false; + + if ((x->fallback != NULL) != (y->fallback != NULL)) + return false; + + return (Table$compare(x, y, type) == 0); +} + +PUREFUNC public int32_t Table$compare(const Table_t *x, const Table_t *y, const TypeInfo *type) +{ + if (x == y) return 0; + + assert(type->tag == TableInfo); + auto table = type->TableInfo; + if (x->entries.length == 0) + return 0; + else if (x->entries.length != y->entries.length) + return (x->entries.length > y->entries.length) - (x->entries.length < y->entries.length); + + for (int64_t i = 0; i < x->entries.length; i++) { + void *x_key = x->entries.data + x->entries.stride * i; + void *y_key = y->entries.data + y->entries.stride * i; + int32_t diff = generic_compare(x_key, y_key, table.key); + if (diff != 0) return diff; + void *x_value = x_key + value_offset(type); + void *y_value = y_key + value_offset(type); + diff = generic_compare(x_value, y_value, table.value); + if (diff != 0) return diff; + } + + if (!x->fallback != !y->fallback) { + return (!x->fallback) - (!y->fallback); + } else if (x->fallback && y->fallback) { + return generic_compare(x->fallback, y->fallback, type); + } + + return 0; +} + +PUREFUNC public uint64_t Table$hash(const Table_t *t, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + // Table hashes are computed as: + // hash(hash(t.keys), hash(t.values), hash(t.fallback), hash(t.default)) + // Where fallback and default hash to zero if absent + auto table = type->TableInfo; + uint64_t components[] = { + Array$hash(&t->entries, Array$info(table.key)), + Array$hash(&t->entries + value_offset(type), Array$info(table.value)), + t->fallback ? Table$hash(t->fallback, type) : 0, + }; + return siphash24((void*)&components, sizeof(components)); +} + +public Text_t Table$as_text(const Table_t *t, bool colorize, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + auto table = type->TableInfo; + + if (!t) { + if (table.value != &Void$info) + return Text$concat( + Text("{"), + generic_as_text(NULL, false, table.key), + Text(":"), + generic_as_text(NULL, false, table.value), + Text("}")); + else + return Text$concat( + Text("{"), + generic_as_text(NULL, false, table.key), + Text("}")); + } + + int64_t val_off = (int64_t)value_offset(type); + Text_t text = Text("{"); + for (int64_t i = 0, length = Table$length(*t); i < length; i++) { + if (i > 0) + text = Text$concat(text, Text(", ")); + void *entry = GET_ENTRY(*t, i); + text = Text$concat(text, generic_as_text(entry, colorize, table.key)); + if (table.value != &Void$info) + text = Text$concat(text, Text(":"), generic_as_text(entry + val_off, colorize, table.value)); + } + + if (t->fallback) { + text = Text$concat(text, Text("; fallback="), Table$as_text(t->fallback, colorize, type)); + } + + text = Text$concat(text, Text("}")); + return text; +} + +public Table_t Table$from_entries(Array_t entries, const TypeInfo *type) +{ + assert(type->tag == TableInfo); + if (entries.length == 0) + return (Table_t){}; + + Table_t t = {}; + int64_t length = entries.length + entries.length / 4; + size_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[length]); + t.bucket_info = GC_MALLOC_ATOMIC(alloc_size); + memset(t.bucket_info->buckets, 0, sizeof(bucket_t[length])); + t.bucket_info->count = length; + t.bucket_info->last_free = length-1; + + size_t offset = value_offset(type); + for (int64_t i = 0; i < entries.length; i++) { + void *key = entries.data + i*entries.stride; + Table$set(&t, key, key + offset, type); + } + return t; +} + +// Overlap is "set intersection" in formal terms +public Table_t Table$overlap(Table_t a, Table_t b, const TypeInfo *type) +{ + // Return a table such that t[k]==a[k] for all k such that a:has(k), b:has(k), and a[k]==b[k] + Table_t result = {}; + const size_t offset = value_offset(type); + for (int64_t i = 0; i < Table$length(a); i++) { + void *key = GET_ENTRY(a, i); + void *a_value = key + offset; + void *b_value = Table$get(b, key, type); + if (b_value && generic_equal(a_value, b_value, type->TableInfo.value)) + Table$set(&result, key, a_value, type); + } + + if (a.fallback) { + result.fallback = new(Table_t); + *result.fallback = Table$overlap(*a.fallback, b, type); + } + + return result; +} + +// With is "set union" in formal terms +public Table_t Table$with(Table_t a, Table_t b, const TypeInfo *type) +{ + // return a table such that t[k]==b[k] for all k such that b:has(k), and t[k]==a[k] for all k such that a:has(k) and not b:has(k) + Table_t result = {}; + const size_t offset = value_offset(type); + for (int64_t i = 0; i < Table$length(a); i++) { + void *key = GET_ENTRY(a, i); + Table$set(&result, key, key + offset, type); + } + for (int64_t i = 0; i < Table$length(b); i++) { + void *key = GET_ENTRY(b, i); + Table$set(&result, key, key + offset, type); + } + + if (a.fallback && b.fallback) { + result.fallback = new(Table_t); + *result.fallback = Table$with(*a.fallback, *b.fallback, type); + } else { + result.fallback = a.fallback ? a.fallback : b.fallback; + } + + return result; +} + +// Without is "set difference" in formal terms +public Table_t Table$without(Table_t a, Table_t b, const TypeInfo *type) +{ + // Return a table such that t[k]==a[k] for all k such that not b:has(k) or b[k] != a[k] + Table_t result = {}; + const size_t offset = value_offset(type); + for (int64_t i = 0; i < Table$length(a); i++) { + void *key = GET_ENTRY(a, i); + void *a_value = key + offset; + void *b_value = Table$get(b, key, type); + if (!b_value || !generic_equal(a_value, b_value, type->TableInfo.value)) + Table$set(&result, key, a_value, type); + } + + if (a.fallback) { + result.fallback = new(Table_t); + *result.fallback = Table$without(*a.fallback, b, type); + } + + return result; +} + +PUREFUNC public bool Table$is_subset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type) +{ + if (a.entries.length > b.entries.length || (strict && a.entries.length == b.entries.length)) + return false; + + for (int64_t i = 0; i < Table$length(a); i++) { + void *found = Table$get_raw(b, GET_ENTRY(a, i), type); + if (!found) return false; + } + return true; +} + +PUREFUNC public bool Table$is_superset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type) +{ + return Table$is_subset_of(b, a, strict, type); +} + +PUREFUNC public void *Table$str_get(Table_t t, const char *key) +{ + void **ret = Table$get(t, &key, &CStrToVoidStarTable); + return ret ? *ret : NULL; +} + +PUREFUNC public void *Table$str_get_raw(Table_t t, const char *key) +{ + void **ret = Table$get_raw(t, &key, &CStrToVoidStarTable); + return ret ? *ret : NULL; +} + +public void *Table$str_reserve(Table_t *t, const char *key, const void *value) +{ + return Table$reserve(t, &key, &value, &CStrToVoidStarTable); +} + +public void Table$str_set(Table_t *t, const char *key, const void *value) +{ + Table$set(t, &key, &value, &CStrToVoidStarTable); +} + +public void Table$str_remove(Table_t *t, const char *key) +{ + return Table$remove(t, &key, &CStrToVoidStarTable); +} + +CONSTFUNC public void *Table$str_entry(Table_t t, int64_t n) +{ + return Table$entry(t, n); +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/tables.h b/builtins/tables.h new file mode 100644 index 00000000..53e0c583 --- /dev/null +++ b/builtins/tables.h @@ -0,0 +1,84 @@ +#pragma once + +// Hash table datastructure with methods and type information + +#include +#include +#include + +#include "arrays.h" +#include "datatypes.h" +#include "types.h" +#include "util.h" + +#define Table(key_t, val_t, key_info, value_info, fb, N, ...) ({ \ + struct { key_t k; val_t v; } ents[N] = {__VA_ARGS__}; \ + Table_t table = Table$from_entries((Array_t){ \ + .data=memcpy(GC_MALLOC(sizeof(ents)), ents, sizeof(ents)), \ + .length=sizeof(ents)/sizeof(ents[0]), \ + .stride=(void*)&ents[1] - (void*)&ents[0], \ + }, Table$info(key_info, value_info)); \ + table.fallback = fb; \ + table; }) +#define Set(item_t, item_info, N, ...) ({ \ + item_t ents[N] = {__VA_ARGS__}; \ + Table_t set = Table$from_entries((Array_t){ \ + .data=memcpy(GC_MALLOC(sizeof(ents)), ents, sizeof(ents)), \ + .length=sizeof(ents)/sizeof(ents[0]), \ + .stride=(void*)&ents[1] - (void*)&ents[0], \ + }, Set$info(item_info)); \ + set; }) + +Table_t Table$from_entries(Array_t entries, const TypeInfo *type); +void *Table$get(Table_t t, const void *key, const TypeInfo *type); +#define Table$get_optional(table_expr, key_t, val_t, key_expr, nonnull_var, nonnull_expr, null_expr, info_expr) ({ \ + const Table_t t = table_expr; const key_t k = key_expr; \ + val_t *nonnull_var = Table$get(t, &k, info_expr); \ + nonnull_var ? nonnull_expr : null_expr; }) +#define Table$has_value(table_expr, key_expr, info_expr) ({ \ + const Table_t t = table_expr; __typeof(key_expr) k = key_expr; \ + (Table$get(t, &k, info_expr) != NULL); }) +PUREFUNC void *Table$get_raw(Table_t t, const void *key, const TypeInfo *type); +CONSTFUNC void *Table$entry(Table_t t, int64_t n); +void *Table$reserve(Table_t *t, const void *key, const void *value, const TypeInfo *type); +void Table$set(Table_t *t, const void *key, const void *value, const TypeInfo *type); +#define Table$set_value(t, key_expr, value_expr, type) ({ __typeof(key_expr) k = key_expr; __typeof(value_expr) v = value_expr; \ + Table$set(t, &k, &v, type); }) +#define Table$reserve_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$reserve(t, &k, NULL, type); }) +#define Table$bump(t_expr, key_expr, amount_expr, type) ({ __typeof(key_expr) key = key_expr; \ + Table_t *t = t_expr; \ + __typeof(amount_expr) *val = Table$get_raw(*t, &key, type); \ + if (val) *val += amount_expr; \ + else { __typeof(amount_expr) init = amount_expr; Table$set(t, &key, &init, type); } (void)0; }) + +void Table$remove(Table_t *t, const void *key, const TypeInfo *type); +#define Table$remove_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$remove(t, &k, type); }) + +Table_t Table$overlap(Table_t a, Table_t b, const TypeInfo *type); +Table_t Table$with(Table_t a, Table_t b, const TypeInfo *type); +Table_t Table$without(Table_t a, Table_t b, const TypeInfo *type); +PUREFUNC bool Table$is_subset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type); +PUREFUNC bool Table$is_superset_of(Table_t a, Table_t b, bool strict, const TypeInfo *type); + +void Table$clear(Table_t *t); +Table_t Table$sorted(Table_t t, const TypeInfo *type); +void Table$mark_copy_on_write(Table_t *t); +#define TABLE_INCREF(t) ({ ARRAY_INCREF((t).entries); if ((t).bucket_info) (t).bucket_info->data_refcount += ((t).bucket_info->data_refcount < TABLE_MAX_DATA_REFCOUNT); }) +#define TABLE_COPY(t) ({ TABLE_INCREF(t); t; }) +PUREFUNC int32_t Table$compare(const Table_t *x, const Table_t *y, const TypeInfo *type); +PUREFUNC bool Table$equal(const Table_t *x, const Table_t *y, const TypeInfo *type); +PUREFUNC uint64_t Table$hash(const Table_t *t, const TypeInfo *type); +Text_t Table$as_text(const Table_t *t, bool colorize, const TypeInfo *type); + +CONSTFUNC void *Table$str_entry(Table_t t, int64_t n); +PUREFUNC void *Table$str_get(Table_t t, const char *key); +PUREFUNC void *Table$str_get_raw(Table_t t, const char *key); +void Table$str_set(Table_t *t, const char *key, const void *value); +void *Table$str_reserve(Table_t *t, const char *key, const void *value); +void Table$str_remove(Table_t *t, const char *key); + +#define Table$length(t) ((t).entries.length) + +extern const TypeInfo CStrToVoidStarTable; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/text.c b/builtins/text.c index 42a6c165..283dfb01 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -61,10 +61,10 @@ #include #include -#include "array.h" +#include "arrays.h" #include "integers.h" -#include "pattern.h" -#include "table.h" +#include "patterns.h" +#include "tables.h" #include "text.h" // Use inline version of the siphash code for performance: diff --git a/builtins/thread.c b/builtins/thread.c deleted file mode 100644 index 2772604d..00000000 --- a/builtins/thread.c +++ /dev/null @@ -1,55 +0,0 @@ -// Logic for the Thread type, representing a pthread - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "array.h" -#include "text.h" -#include "types.h" -#include "util.h" - -public pthread_t *Thread$new(Closure_t fn) -{ - pthread_t *thread = new(pthread_t); - pthread_create(thread, NULL, fn.fn, fn.userdata); - return thread; -} - -public void Thread$join(pthread_t *thread) -{ - pthread_join(*thread, NULL); -} - -public void Thread$cancel(pthread_t *thread) -{ - pthread_cancel(*thread); -} - -public void Thread$detach(pthread_t *thread) -{ - pthread_detach(*thread); -} - -Text_t Thread$as_text(const pthread_t **thread, bool colorize, const TypeInfo *type) -{ - (void)type; - if (!thread) { - return colorize ? Text("\x1b[34;1mThread\x1b[m") : Text("Thread"); - } - return Text$format(colorize ? "\x1b[34;1mThread(%p)\x1b[m" : "Thread(%p)", *thread); -} - -public const TypeInfo Thread = { - .size=sizeof(pthread_t*), .align=__alignof(pthread_t*), - .tag=CustomInfo, - .CustomInfo={.as_text=(void*)Thread$as_text}, -}; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/thread.h b/builtins/thread.h deleted file mode 100644 index 52091677..00000000 --- a/builtins/thread.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -// Logic for the Thread type, representing a pthread - -#include -#include - -#include "datatypes.h" -#include "types.h" -#include "util.h" - -pthread_t *Thread$new(Closure_t fn); -void Thread$cancel(pthread_t *thread); -void Thread$join(pthread_t *thread); -void Thread$detach(pthread_t *thread); -Text_t Thread$as_text(const pthread_t **thread, bool colorize, const TypeInfo *type); - -extern TypeInfo Thread; - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/threads.c b/builtins/threads.c new file mode 100644 index 00000000..74e73832 --- /dev/null +++ b/builtins/threads.c @@ -0,0 +1,55 @@ +// Logic for the Thread type, representing a pthread + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arrays.h" +#include "text.h" +#include "types.h" +#include "util.h" + +public pthread_t *Thread$new(Closure_t fn) +{ + pthread_t *thread = new(pthread_t); + pthread_create(thread, NULL, fn.fn, fn.userdata); + return thread; +} + +public void Thread$join(pthread_t *thread) +{ + pthread_join(*thread, NULL); +} + +public void Thread$cancel(pthread_t *thread) +{ + pthread_cancel(*thread); +} + +public void Thread$detach(pthread_t *thread) +{ + pthread_detach(*thread); +} + +Text_t Thread$as_text(const pthread_t **thread, bool colorize, const TypeInfo *type) +{ + (void)type; + if (!thread) { + return colorize ? Text("\x1b[34;1mThread\x1b[m") : Text("Thread"); + } + return Text$format(colorize ? "\x1b[34;1mThread(%p)\x1b[m" : "Thread(%p)", *thread); +} + +public const TypeInfo Thread = { + .size=sizeof(pthread_t*), .align=__alignof(pthread_t*), + .tag=CustomInfo, + .CustomInfo={.as_text=(void*)Thread$as_text}, +}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/threads.h b/builtins/threads.h new file mode 100644 index 00000000..52091677 --- /dev/null +++ b/builtins/threads.h @@ -0,0 +1,20 @@ +#pragma once + +// Logic for the Thread type, representing a pthread + +#include +#include + +#include "datatypes.h" +#include "types.h" +#include "util.h" + +pthread_t *Thread$new(Closure_t fn); +void Thread$cancel(pthread_t *thread); +void Thread$join(pthread_t *thread); +void Thread$detach(pthread_t *thread); +Text_t Thread$as_text(const pthread_t **thread, bool colorize, const TypeInfo *type); + +extern TypeInfo Thread; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/tomo.h b/builtins/tomo.h index 78458304..fdd3ea0c 100644 --- a/builtins/tomo.h +++ b/builtins/tomo.h @@ -9,10 +9,10 @@ #include #include -#include "array.h" -#include "bool.h" +#include "arrays.h" +#include "bools.h" #include "c_string.h" -#include "channel.h" +#include "channels.h" #include "datatypes.h" #include "functiontype.h" #include "integers.h" @@ -21,15 +21,15 @@ #include "metamethods.h" #include "nums.h" #include "optionals.h" -#include "path.h" -#include "pattern.h" -#include "pointer.h" -#include "range.h" +#include "paths.h" +#include "patterns.h" +#include "pointers.h" +#include "ranges.h" #include "shell.h" #include "siphash.h" -#include "table.h" +#include "tables.h" #include "text.h" -#include "thread.h" +#include "threads.h" #include "types.h" // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/types.c b/builtins/types.c index cf0e30c1..c9f2578f 100644 --- a/builtins/types.c +++ b/builtins/types.c @@ -6,9 +6,9 @@ #include #include "util.h" -#include "array.h" -#include "pointer.h" -#include "table.h" +#include "arrays.h" +#include "pointers.h" +#include "tables.h" #include "text.h" #include "types.h" diff --git a/environment.c b/environment.c index ac1e27e1..5170fa95 100644 --- a/environment.c +++ b/environment.c @@ -3,7 +3,7 @@ #include #include -#include "builtins/table.h" +#include "builtins/tables.h" #include "builtins/text.h" #include "builtins/util.h" #include "cordhelpers.h" diff --git a/environment.h b/environment.h index dac3065a..005fda7b 100644 --- a/environment.h +++ b/environment.h @@ -5,7 +5,7 @@ #include #include "types.h" -#include "builtins/table.h" +#include "builtins/tables.h" typedef struct { CORD local_typedefs; diff --git a/parse.c b/parse.c index e1567c4b..4e57cabf 100644 --- a/parse.c +++ b/parse.c @@ -14,7 +14,7 @@ #include "ast.h" #include "builtins/integers.h" #include "builtins/text.h" -#include "builtins/table.h" +#include "builtins/tables.h" #include "builtins/util.h" #include "cordhelpers.h" diff --git a/tomo.c b/tomo.c index c92887d9..ec4f1c35 100644 --- a/tomo.c +++ b/tomo.c @@ -10,7 +10,7 @@ #include #include "ast.h" -#include "builtins/array.h" +#include "builtins/arrays.h" #include "builtins/datatypes.h" #include "builtins/text.h" #include "compile.h" diff --git a/types.c b/types.c index 321e6a5d..3f546432 100644 --- a/types.c +++ b/types.c @@ -7,7 +7,7 @@ #include #include "builtins/integers.h" -#include "builtins/table.h" +#include "builtins/tables.h" #include "builtins/util.h" #include "cordhelpers.h" #include "types.h" diff --git a/types.h b/types.h index 6dedeaf8..94d74022 100644 --- a/types.h +++ b/types.h @@ -6,7 +6,7 @@ #include #include "ast.h" -#include "builtins/array.h" +#include "builtins/arrays.h" typedef struct type_s type_t; -- cgit v1.2.3