diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-03-29 12:54:31 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-03-29 12:54:31 -0400 |
| commit | 04d9adc8138566eec5d6bf7b233a6c617306bcce (patch) | |
| tree | 8414c07e92cdd17613190cd6a8e179b2eed2f00c /builtins | |
| parent | d94053ca7768037016cbcacc4cadf843dad1bea6 (diff) | |
Switch naming convention to use '$' in symbols more
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/array.c | 38 | ||||
| -rw-r--r-- | builtins/array.h | 40 | ||||
| -rw-r--r-- | builtins/bool.c | 6 | ||||
| -rw-r--r-- | builtins/bool.h | 4 | ||||
| -rw-r--r-- | builtins/functions.c | 40 | ||||
| -rw-r--r-- | builtins/integers.c | 24 | ||||
| -rw-r--r-- | builtins/integers.h | 38 | ||||
| -rw-r--r-- | builtins/memory.c | 2 | ||||
| -rw-r--r-- | builtins/memory.h | 4 | ||||
| -rw-r--r-- | builtins/nums.c | 64 | ||||
| -rw-r--r-- | builtins/nums.h | 52 | ||||
| -rw-r--r-- | builtins/pointer.c | 8 | ||||
| -rw-r--r-- | builtins/pointer.h | 8 | ||||
| -rw-r--r-- | builtins/table.c | 105 | ||||
| -rw-r--r-- | builtins/table.h | 54 | ||||
| -rw-r--r-- | builtins/text.c | 54 | ||||
| -rw-r--r-- | builtins/text.h | 48 | ||||
| -rw-r--r-- | builtins/types.c | 10 | ||||
| -rw-r--r-- | builtins/types.h | 10 | ||||
| -rw-r--r-- | builtins/util.h | 14 |
20 files changed, 311 insertions, 312 deletions
diff --git a/builtins/array.c b/builtins/array.c index 6ebc6429..8b50dbcf 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -22,7 +22,7 @@ static inline size_t get_item_size(const TypeInfo *info) // 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 `item_size` -public void Array__compact(array_t *arr, const TypeInfo *type) +public void Array$compact(array_t *arr, const TypeInfo *type) { void *copy = NULL; int64_t item_size = get_item_size(type); @@ -43,7 +43,7 @@ public void Array__compact(array_t *arr, const TypeInfo *type) }; } -public void Array__insert(array_t *arr, const void *item, int64_t index, const TypeInfo *type) +public void Array$insert(array_t *arr, const void *item, int64_t index, const TypeInfo *type) { if (index <= 0) index = arr->length + index + 1; @@ -75,7 +75,7 @@ public void Array__insert(array_t *arr, const void *item, int64_t index, const T memcpy((void*)arr->data + (index-1)*item_size, item, item_size); } -public void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, const TypeInfo *type) +public void Array$insert_all(array_t *arr, array_t to_insert, int64_t index, const TypeInfo *type) { if (index < 1) index = arr->length + index + 1; @@ -105,7 +105,7 @@ public void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, co memcpy((void*)arr->data + (index-1 + i)*item_size, to_insert.data + i*to_insert.stride, item_size); } -public void Array__remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *type) +public void Array$remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *type) { if (index < 1) index = arr->length + index + 1; @@ -138,7 +138,7 @@ public void Array__remove(array_t *arr, int64_t index, int64_t count, const Type arr->length -= count; } -public void Array__sort(array_t *arr, const TypeInfo *type) +public void Array$sort(array_t *arr, const TypeInfo *type) { const TypeInfo *item_type = type->ArrayInfo.item; int64_t item_size = item_type->size; @@ -146,16 +146,16 @@ public void Array__sort(array_t *arr, const TypeInfo *type) item_size += item_type->align - (item_size % item_type->align); // padding if (arr->data_refcount || (int64_t)arr->stride != item_size) - Array__compact(arr, type); + Array$compact(arr, type); qsort_r(arr->data, arr->length, item_size, (void*)generic_compare, (void*)item_type); } -public void Array__shuffle(array_t *arr, const TypeInfo *type) +public void Array$shuffle(array_t *arr, const TypeInfo *type) { int64_t item_size = get_item_size(type); if (arr->data_refcount || (int64_t)arr->stride != item_size) - Array__compact(arr, type); + Array$compact(arr, type); char tmp[item_size]; for (int64_t i = arr->length-1; i > 1; i--) { @@ -166,7 +166,7 @@ public void Array__shuffle(array_t *arr, const TypeInfo *type) } } -public void *Array__random(array_t arr) +public void *Array$random(array_t arr) { if (arr.length == 0) return NULL; // fail("Cannot get a random item from an empty array!"); @@ -174,7 +174,7 @@ public void *Array__random(array_t arr) return arr.data + arr.stride*index; } -public array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type) +public array_t Array$slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type) { if (stride > MAX_STRIDE || stride < MIN_STRIDE) fail("Stride is too big: %ld", stride); @@ -223,7 +223,7 @@ public array_t Array__slice(array_t *array, int64_t first, int64_t length, int64 }; } -public array_t Array__reversed(array_t array) +public array_t Array$reversed(array_t array) { array_t reversed = array; reversed.stride = -array.stride; @@ -231,7 +231,7 @@ public array_t Array__reversed(array_t array) return reversed; } -public array_t Array__concat(array_t x, array_t y, const TypeInfo *type) +public array_t Array$concat(array_t x, array_t y, const TypeInfo *type) { int64_t item_size = get_item_size(type); void *data = x.atomic ? GC_MALLOC_ATOMIC(item_size*(x.length + y.length)) : GC_MALLOC(item_size*(x.length + y.length)); @@ -257,7 +257,7 @@ public array_t Array__concat(array_t x, array_t y, const TypeInfo *type) }; } -public bool Array__contains(array_t array, void *item, const TypeInfo *type) +public bool Array$contains(array_t array, void *item, const TypeInfo *type) { const TypeInfo *item_type = type->ArrayInfo.item; for (int64_t i = 0; i < array.length; i++) @@ -266,12 +266,12 @@ public bool Array__contains(array_t array, void *item, const TypeInfo *type) return false; } -public void Array__clear(array_t *array) +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) +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) @@ -298,12 +298,12 @@ public int32_t Array__compare(const array_t *x, const array_t *y, const TypeInfo return (x->length > y->length) - (x->length < y->length); } -public bool Array__equal(const array_t *x, const array_t *y, const TypeInfo *type) +public bool Array$equal(const array_t *x, const array_t *y, const TypeInfo *type) { - return (Array__compare(x, y, type) == 0); + return (Array$compare(x, y, type) == 0); } -public CORD Array__as_text(const array_t *arr, bool colorize, const TypeInfo *type) +public CORD Array$as_text(const array_t *arr, bool colorize, const TypeInfo *type) { if (!arr) return CORD_all("[", generic_as_text(NULL, false, type->ArrayInfo.item), "]"); @@ -320,7 +320,7 @@ public CORD Array__as_text(const array_t *arr, bool colorize, const TypeInfo *ty return c; } -public uint32_t Array__hash(const array_t *arr, const TypeInfo *type) +public uint32_t Array$hash(const array_t *arr, const TypeInfo *type) { // Array hash is calculated as a rolling, compacting hash of the length of the array, followed by // the hashes of its items (or the items themselves if they're small plain data) diff --git a/builtins/array.h b/builtins/array.h index dad72b7e..98638222 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -15,15 +15,15 @@ const array_t $arr = arr_expr; int64_t $index = (int64_t)(index_expr); \ int64_t $off = $index + ($index < 0) * ($arr.length + 1) - 1; \ if (__builtin_expect($off < 0 || $off >= $arr.length, 0)) \ - fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int__as_text(&$index, USE_COLOR, NULL), $arr.length); \ + fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int$as_text(&$index, USE_COLOR, NULL), $arr.length); \ (item_type*)($arr.data + $arr.stride * $off);}) #define $Array_lvalue(item_type, arr_expr, index_expr, typeinfo, filename, start, end) { \ array_t *$arr = arr_expr; int64_t $index = (int64_t)(index_expr); \ int64_t $off = $index + ($index < 0) * ($arr->length + 1) - 1; \ if (__builtin_expect($off < 0 || $off >= $arr->length, 0)) \ - fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int__as_text(&$index, USE_COLOR, NULL), $arr->length); \ + fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int$as_text(&$index, USE_COLOR, NULL), $arr->length); \ if ($arr->data_refcount > 0) \ - Array__compact($arr, typeinfo); \ + Array$compact($arr, typeinfo); \ *(item_type*)($arr->data + $arr->stride * $off); } #define $Array_set(item_type, arr, index, value, typeinfo, filename, start, end) \ $Array_lvalue(item_type, arr_expr, index, typeinfo, filename, start, end) = value @@ -52,22 +52,22 @@ #define $ARRAY_INCREF(arr) (arr).data_refcount |= ((arr).data_refcount << 1) | 1 #define $ARRAY_DECREF(arr) (arr).data_refcount &= 2 -#define Array__insert_value(arr, item_expr, index, type) ({ __typeof(item_expr) $item = item_expr; Array__insert(arr, &$item, index, type); }) -void Array__insert(array_t *arr, const void *item, int64_t index, const TypeInfo *type); -void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, const TypeInfo *type); -void Array__remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *type); -void Array__sort(array_t *arr, const TypeInfo *type); -void Array__shuffle(array_t *arr, const TypeInfo *type); -void *Array__random(array_t arr); -void Array__clear(array_t *array); -void Array__compact(array_t *arr, const TypeInfo *type); -bool Array__contains(array_t array, void *item, const TypeInfo *type); -array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type); -array_t Array__reversed(array_t array); -array_t Array__concat(array_t x, array_t y, const TypeInfo *type); -uint32_t Array__hash(const array_t *arr, const TypeInfo *type); -int32_t Array__compare(const array_t *x, const array_t *y, const TypeInfo *type); -bool Array__equal(const array_t *x, const array_t *y, const TypeInfo *type); -CORD Array__as_text(const array_t *arr, bool colorize, const TypeInfo *type); +#define Array$insert_value(arr, item_expr, index, type) ({ __typeof(item_expr) $item = item_expr; Array$insert(arr, &$item, index, type); }) +void Array$insert(array_t *arr, const void *item, int64_t index, const TypeInfo *type); +void Array$insert_all(array_t *arr, array_t to_insert, int64_t index, const TypeInfo *type); +void Array$remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *type); +void Array$sort(array_t *arr, const TypeInfo *type); +void Array$shuffle(array_t *arr, const TypeInfo *type); +void *Array$random(array_t arr); +void Array$clear(array_t *array); +void Array$compact(array_t *arr, const TypeInfo *type); +bool Array$contains(array_t array, void *item, const TypeInfo *type); +array_t Array$slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type); +array_t Array$reversed(array_t array); +array_t Array$concat(array_t x, array_t y, const TypeInfo *type); +uint32_t Array$hash(const array_t *arr, const TypeInfo *type); +int32_t Array$compare(const array_t *x, const array_t *y, const TypeInfo *type); +bool Array$equal(const array_t *x, const array_t *y, const TypeInfo *type); +CORD Array$as_text(const array_t *arr, bool colorize, const TypeInfo *type); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/bool.c b/builtins/bool.c index 7a34073b..cced8077 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -12,7 +12,7 @@ #include "bool.h" #include "types.h" -public CORD Bool__as_text(const bool *b, bool colorize, const TypeInfo *type) +public CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) { (void)type; if (!b) return "Bool"; @@ -22,11 +22,11 @@ public CORD Bool__as_text(const bool *b, bool colorize, const TypeInfo *type) return *b ? "yes" : "no"; } -public const TypeInfo Bool = { +public const TypeInfo $Bool = { .size=sizeof(bool), .align=__alignof__(bool), .tag=CustomInfo, - .CustomInfo={.as_text=(void*)Bool__as_text}, + .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 index 1462e7d6..d5f4b705 100644 --- a/builtins/bool.h +++ b/builtins/bool.h @@ -12,8 +12,8 @@ #define yes (Bool_t)true #define no (Bool_t)false -CORD Bool__as_text(const bool *b, bool colorize, const TypeInfo *type); +CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type); -extern const TypeInfo Bool; +extern const TypeInfo $Bool; // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/functions.c b/builtins/functions.c index 71fbfd63..53c4db0f 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -57,10 +57,10 @@ public void fail_source(const char *filename, int64_t start, int64_t end, CORD f public uint32_t generic_hash(const void *obj, const TypeInfo *type) { switch (type->tag) { - case PointerInfo: case FunctionInfo: return Pointer__hash(obj, type); - case TextInfo: return Text__hash(obj); - case ArrayInfo: return Array__hash(obj, type); - case TableInfo: return Table_hash(obj, type); + case PointerInfo: case FunctionInfo: return Pointer$hash(obj, type); + case TextInfo: return Text$hash(obj); + case ArrayInfo: return Array$hash(obj, type); + case TableInfo: return Table$hash(obj, type); case CustomInfo: if (!type->CustomInfo.hash) goto hash_data; @@ -77,10 +77,10 @@ public uint32_t generic_hash(const void *obj, const TypeInfo *type) public int32_t generic_compare(const void *x, const void *y, const TypeInfo *type) { switch (type->tag) { - case PointerInfo: case FunctionInfo: return Pointer__compare(x, y, type); - case TextInfo: return Text__compare(x, y); - case ArrayInfo: return Array__compare(x, y, type); - case TableInfo: return Table_compare(x, y, type); + case PointerInfo: case FunctionInfo: return Pointer$compare(x, y, type); + case TextInfo: return Text$compare(x, y); + case ArrayInfo: return Array$compare(x, y, type); + case TableInfo: return Table$compare(x, y, type); case CustomInfo: if (!type->CustomInfo.compare) goto compare_data; @@ -94,10 +94,10 @@ public int32_t generic_compare(const void *x, const void *y, const TypeInfo *typ public bool generic_equal(const void *x, const void *y, const TypeInfo *type) { switch (type->tag) { - case PointerInfo: case FunctionInfo: return Pointer__equal(x, y, type); - case TextInfo: return Text__equal(x, y); - case ArrayInfo: return Array__equal(x, y, type); - case TableInfo: return Table_equal(x, y, type); + case PointerInfo: case FunctionInfo: return Pointer$equal(x, y, type); + case TextInfo: return Text$equal(x, y); + case ArrayInfo: return Array$equal(x, y, type); + case TableInfo: return Table$equal(x, y, type); case CustomInfo: if (!type->CustomInfo.equal) goto use_generic_compare; @@ -111,12 +111,12 @@ public bool generic_equal(const void *x, const void *y, const TypeInfo *type) public CORD generic_as_text(const void *obj, bool colorize, const TypeInfo *type) { switch (type->tag) { - case PointerInfo: return Pointer__as_text(obj, colorize, type); - case FunctionInfo: return Func__as_text(obj, colorize, type); - case TextInfo: return Text__as_text(obj, colorize, type); - case ArrayInfo: return Array__as_text(obj, colorize, type); - case TableInfo: return Table_as_text(obj, colorize, type); - case TypeInfoInfo: return Type__as_text(obj, colorize, type); + case PointerInfo: return Pointer$as_text(obj, colorize, type); + case FunctionInfo: return Func$as_text(obj, colorize, type); + case TextInfo: return Text$as_text(obj, colorize, type); + case ArrayInfo: return Array$as_text(obj, colorize, type); + case TableInfo: return Table$as_text(obj, colorize, type); + case TypeInfoInfo: return Type$as_text(obj, colorize, type); case CustomInfo: if (!type->CustomInfo.as_text) fail("No cord function provided for type!\n"); @@ -156,10 +156,10 @@ public void $test(void *expr, const TypeInfo *type, CORD expected, const char *f CORD_fprintf(stderr, USE_COLOR ? "\x1b[2m=\x1b[0m %r \x1b[2m: %r\x1b[m\n" : "= %r : %r\n", expr_normalized, type_name); if (expected) { CORD expr_plain = USE_COLOR ? generic_as_text(expr, false, type) : expr_normalized; - bool success = Text__equal(&expr_plain, &expected); + bool success = Text$equal(&expr_plain, &expected); if (!success && CORD_chr(expected, 0, ':')) { CORD with_type = CORD_catn(3, expr_plain, " : ", type_name); - success = Text__equal(&with_type, &expected); + success = Text$equal(&with_type, &expected); } if (!success) { diff --git a/builtins/integers.c b/builtins/integers.c index 159220a9..86c1d5e6 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -15,7 +15,7 @@ #define str(a) #a #define DEFINE_INT_TYPE(c_type, KindOfInt, fmt, min_val, max_val)\ - public CORD KindOfInt ## __as_text(const c_type *i, bool colorize, const TypeInfo *type) { \ + public CORD KindOfInt ## $as_text(const c_type *i, bool colorize, const TypeInfo *type) { \ (void)type; \ if (!i) return #KindOfInt; \ CORD c; \ @@ -23,26 +23,26 @@ else CORD_sprintf(&c, "%"fmt, *i); \ return c; \ } \ - public int32_t KindOfInt ## __compare(const c_type *x, const c_type *y, const TypeInfo *type) { \ + public int32_t KindOfInt ## $compare(const c_type *x, const c_type *y, const TypeInfo *type) { \ (void)type; \ return (*x > *y) - (*x < *y); \ } \ - public bool KindOfInt ## __equal(const c_type *x, const c_type *y, const TypeInfo *type) { \ + public bool KindOfInt ## $equal(const c_type *x, const c_type *y, const TypeInfo *type) { \ (void)type; \ return *x == *y; \ } \ - public CORD KindOfInt ## __format(c_type i, int64_t digits) { \ + public CORD KindOfInt ## $format(c_type i, int64_t digits) { \ return CORD_asprintf("%0*" fmt, (int)digits, i); \ } \ - public CORD KindOfInt ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix) { \ + public CORD KindOfInt ## $hex(c_type i, int64_t digits, bool uppercase, bool prefix) { \ const char *hex_fmt = uppercase ? (prefix ? "0x%0.*lX" : "%0.*lX") : (prefix ? "0x%0.*lx" : "%0.*lx"); \ return CORD_asprintf(hex_fmt, (int)digits, (uint64_t)i); \ } \ - public CORD KindOfInt ## __octal(c_type i, int64_t digits, bool prefix) { \ + public CORD KindOfInt ## $octal(c_type i, int64_t digits, bool prefix) { \ const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo"; \ return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)i); \ } \ - public array_t KindOfInt ## __bits(c_type x) { \ + public array_t KindOfInt ## $bits(c_type x) { \ array_t bit_array = (array_t){.data=GC_MALLOC_ATOMIC(sizeof(bool)*8*sizeof(c_type)), .atomic=1, .stride=sizeof(bool), .length=8*sizeof(c_type)}; \ bool *bits = bit_array.data + sizeof(c_type)*8; \ for (size_t i = 0; i < 8*sizeof(c_type); i++) { \ @@ -51,7 +51,7 @@ } \ return bit_array; \ } \ - public c_type KindOfInt ## __random(int64_t min, int64_t max) { \ + public c_type KindOfInt ## $random(int64_t min, int64_t max) { \ if (min > max) fail("Random min (%ld) is larger than max (%ld)", min, max); \ if (min < (int64_t)min_val) fail("Random min (%ld) is smaller than the minimum "#KindOfInt" value", min); \ if (max > (int64_t)max_val) fail("Random max (%ld) is smaller than the maximum "#KindOfInt" value", max); \ @@ -60,13 +60,13 @@ uint32_t r = arc4random_uniform((uint32_t)range); \ return min + (c_type)r; \ } \ - public const c_type KindOfInt##__min = min_val; \ - public const c_type KindOfInt##__max = max_val; \ - public const TypeInfo KindOfInt = { \ + public const c_type KindOfInt##$min = min_val; \ + public const c_type KindOfInt##$max = max_val; \ + public const TypeInfo $ ## KindOfInt = { \ .size=sizeof(c_type), \ .align=__alignof__(c_type), \ .tag=CustomInfo, \ - .CustomInfo={.compare=(void*)KindOfInt##__compare, .as_text=(void*)KindOfInt##__as_text}, \ + .CustomInfo={.compare=(void*)KindOfInt##$compare, .as_text=(void*)KindOfInt##$as_text}, \ }; DEFINE_INT_TYPE(int64_t, Int, "ld", INT64_MIN, INT64_MAX); diff --git a/builtins/integers.h b/builtins/integers.h index b6b41d6e..8ee78789 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -19,26 +19,26 @@ #define I8(x) ((int8_t)x) #define DEFINE_INT_TYPE(c_type, type_name) \ - CORD type_name ## __as_text(const c_type *i, bool colorize, const TypeInfo *type); \ - int32_t type_name ## __compare(const c_type *x, const c_type *y, const TypeInfo *type); \ - bool type_name ## __equal(const c_type *x, const c_type *y, const TypeInfo *type); \ - CORD type_name ## __format(c_type i, int64_t digits); \ - CORD type_name ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix); \ - CORD type_name ## __octal(c_type i, int64_t digits, bool prefix); \ - array_t type_name ## __bits(c_type x); \ - c_type type_name ## __random(int64_t min, int64_t max); \ - extern const c_type type_name ## __min, type_name##__max; \ - extern const TypeInfo type_name; - -DEFINE_INT_TYPE(int64_t, Int); -DEFINE_INT_TYPE(int32_t, Int32); -DEFINE_INT_TYPE(int16_t, Int16); -DEFINE_INT_TYPE(int8_t, Int8); + CORD type_name ## $as_text(const c_type *i, bool colorize, const TypeInfo *type); \ + int32_t type_name ## $compare(const c_type *x, const c_type *y, const TypeInfo *type); \ + bool type_name ## $equal(const c_type *x, const c_type *y, const TypeInfo *type); \ + CORD type_name ## $format(c_type i, int64_t digits); \ + CORD type_name ## $hex(c_type i, int64_t digits, bool uppercase, bool prefix); \ + CORD type_name ## $octal(c_type i, int64_t digits, bool prefix); \ + array_t type_name ## $bits(c_type x); \ + c_type type_name ## $random(int64_t min, int64_t max); \ + extern const c_type type_name ## $min, type_name##$max; \ + extern const TypeInfo $ ## type_name; + +DEFINE_INT_TYPE(int64_t, Int); +DEFINE_INT_TYPE(int32_t, Int32); +DEFINE_INT_TYPE(int16_t, Int16); +DEFINE_INT_TYPE(int8_t, Int8); #undef DEFINE_INT_TYPE -#define Int__abs(...) I64(labs(__VA_ARGS__)) -#define Int32__abs(...) I32(abs(__VA_ARGS__)) -#define Int16__abs(...) I16(abs(__VA_ARGS__)) -#define Int8__abs(...) I8(abs(__VA_ARGS__)) +#define Int$abs(...) I64(labs(__VA_ARGS__)) +#define Int32$abs(...) I32(abs(__VA_ARGS__)) +#define Int16$abs(...) I16(abs(__VA_ARGS__)) +#define Int8$abs(...) I8(abs(__VA_ARGS__)) // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/memory.c b/builtins/memory.c index 290a6f1b..5b9f39ad 100644 --- a/builtins/memory.c +++ b/builtins/memory.c @@ -21,7 +21,7 @@ public CORD Memory__as_text(const void *p, bool colorize, const TypeInfo *type) return cord; } -public const TypeInfo Memory = { +public const TypeInfo $Memory = { .size=0, .align=0, .tag=CustomInfo, diff --git a/builtins/memory.h b/builtins/memory.h index b5519027..48a2dafd 100644 --- a/builtins/memory.h +++ b/builtins/memory.h @@ -8,7 +8,7 @@ #include "types.h" -extern const TypeInfo Memory; -CORD Memory__as_text(const void *p, bool colorize, const TypeInfo *type); +extern const TypeInfo $Memory; +CORD Memory$as_text(const void *p, bool colorize, const TypeInfo *type); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/nums.c b/builtins/nums.c index 7d20314a..0bb604eb 100644 --- a/builtins/nums.c +++ b/builtins/nums.c @@ -13,7 +13,7 @@ #include "string.h" #include "types.h" -public CORD Num__as_text(const double *f, bool colorize, const TypeInfo *type) { +public CORD Num$as_text(const double *f, bool colorize, const TypeInfo *type) { (void)type; if (!f) return "Num"; CORD c; @@ -22,17 +22,17 @@ public CORD Num__as_text(const double *f, bool colorize, const TypeInfo *type) { return c; } -public int32_t Num__compare(const double *x, const double *y, const TypeInfo *type) { +public int32_t Num$compare(const double *x, const double *y, const TypeInfo *type) { (void)type; return (*x > *y) - (*x < *y); } -public bool Num__equal(const double *x, const double *y, const TypeInfo *type) { +public bool Num$equal(const double *x, const double *y, const TypeInfo *type) { (void)type; return *x == *y; } -public bool Num__near(double a, double b, double ratio, double absolute) { +public bool Num$near(double a, double b, double ratio, double absolute) { if (ratio < 0) ratio = 0; else if (ratio > 0) ratio = 1; @@ -47,43 +47,43 @@ public bool Num__near(double a, double b, double ratio, double absolute) { return (diff < epsilon); } -public CORD Num__format(double f, int64_t precision) { +public CORD Num$format(double f, int64_t precision) { return CORD_asprintf("%.*f", (int)precision, f); } -public CORD Num__scientific(double f, int64_t precision) { +public CORD Num$scientific(double f, int64_t precision) { return CORD_asprintf("%.*e", (int)precision, f); } -public double Num__mod(double num, double modulus) { +public double Num$mod(double num, double modulus) { double result = fmod(num, modulus); return (result < 0) != (modulus < 0) ? result + modulus : result; } -public double Num__random(void) { +public double Num$random(void) { return drand48(); } -public double Num__nan(CORD tag) { +public double Num$nan(CORD tag) { return nan(CORD_to_const_char_star(tag)); } -public bool Num__isinf(double n) { return !!isinf(n); } -public bool Num__finite(double n) { return !!finite(n); } -public bool Num__isnan(double n) { return !!isnan(n); } +public bool Num$isinf(double n) { return !!isinf(n); } +public bool Num$finite(double n) { return !!finite(n); } +public bool Num$isnan(double n) { return !!isnan(n); } -public const TypeInfo Num = { +public const TypeInfo $Num = { .size=sizeof(double), .align=__alignof__(double), .tag=CustomInfo, .CustomInfo={ - .compare=(void*)Num__compare, - .equal=(void*)Num__equal, - .as_text=(void*)Num__as_text, + .compare=(void*)Num$compare, + .equal=(void*)Num$equal, + .as_text=(void*)Num$as_text, }, }; -public CORD Num32__as_text(const float *f, bool colorize, const TypeInfo *type) { +public CORD Num32$as_text(const float *f, bool colorize, const TypeInfo *type) { (void)type; if (!f) return "Num32"; CORD c; @@ -92,17 +92,17 @@ public CORD Num32__as_text(const float *f, bool colorize, const TypeInfo *type) return c; } -public int32_t Num32__compare(const float *x, const float *y, const TypeInfo *type) { +public int32_t Num32$compare(const float *x, const float *y, const TypeInfo *type) { (void)type; return (*x > *y) - (*x < *y); } -public bool Num32__equal(const float *x, const float *y, const TypeInfo *type) { +public bool Num32$equal(const float *x, const float *y, const TypeInfo *type) { (void)type; return *x == *y; } -public bool Num32__near(float a, float b, float ratio, float absolute) { +public bool Num32$near(float a, float b, float ratio, float absolute) { if (ratio < 0) ratio = 0; else if (ratio > 0) ratio = 1; @@ -117,39 +117,39 @@ public bool Num32__near(float a, float b, float ratio, float absolute) { return (diff < epsilon); } -public CORD Num32__format(float f, int64_t precision) { +public CORD Num32$format(float f, int64_t precision) { return CORD_asprintf("%.*f", (int)precision, f); } -public CORD Num32__scientific(float f, int64_t precision) { +public CORD Num32$scientific(float f, int64_t precision) { return CORD_asprintf("%.*e", (int)precision, f); } -public float Num32__mod(float num, float modulus) { +public float Num32$mod(float num, float modulus) { float result = fmodf(num, modulus); return (result < 0) != (modulus < 0) ? result + modulus : result; } -public float Num32__random(void) { +public float Num32$random(void) { return (float)drand48(); } -public float Num32__nan(CORD tag) { +public float Num32$nan(CORD tag) { return nanf(CORD_to_const_char_star(tag)); } -public bool Num32__isinf(float n) { return isinf(n); } -public bool Num32__finite(float n) { return finite(n); } -public bool Num32__isnan(float n) { return isnan(n); } +public bool Num32$isinf(float n) { return isinf(n); } +public bool Num32$finite(float n) { return finite(n); } +public bool Num32$isnan(float n) { return isnan(n); } -public const TypeInfo Num32 = { +public const TypeInfo $Num32 = { .size=sizeof(float), .align=__alignof__(float), .tag=CustomInfo, .CustomInfo={ - .compare=(void*)Num32__compare, - .equal=(void*)Num32__equal, - .as_text=(void*)Num32__as_text, + .compare=(void*)Num32$compare, + .equal=(void*)Num32$equal, + .as_text=(void*)Num32$as_text, }, }; diff --git a/builtins/nums.h b/builtins/nums.h index 97d72c50..0d9f2e99 100644 --- a/builtins/nums.h +++ b/builtins/nums.h @@ -12,32 +12,32 @@ #define Num_t double #define Num32_t float -CORD Num__as_text(const double *f, bool colorize, const TypeInfo *type); -int32_t Num__compare(const double *x, const double *y, const TypeInfo *type); -bool Num__equal(const double *x, const double *y, const TypeInfo *type); -bool Num__near(double a, double b, double ratio, double absolute); -CORD Num__format(double f, int64_t precision); -CORD Num__scientific(double f, int64_t precision); -double Num__mod(double num, double modulus); -bool Num__isinf(double n); -bool Num__finite(double n); -bool Num__isnan(double n); -double Num__nan(CORD tag); -double Num__random(void); -extern const TypeInfo Num; +CORD Num$as_text(const double *f, bool colorize, const TypeInfo *type); +int32_t Num$compare(const double *x, const double *y, const TypeInfo *type); +bool Num$equal(const double *x, const double *y, const TypeInfo *type); +bool Num$near(double a, double b, double ratio, double absolute); +CORD Num$format(double f, int64_t precision); +CORD Num$scientific(double f, int64_t precision); +double Num$mod(double num, double modulus); +bool Num$isinf(double n); +bool Num$finite(double n); +bool Num$isnan(double n); +double Num$nan(CORD tag); +double Num$random(void); +extern const TypeInfo $Num; -CORD Num32__as_text(const float *f, bool colorize, const TypeInfo *type); -int32_t Num32__compare(const float *x, const float *y, const TypeInfo *type); -bool Num32__equal(const float *x, const float *y, const TypeInfo *type); -bool Num32__near(float a, float b, float ratio, float absolute); -CORD Num32__format(float f, int64_t precision); -CORD Num32__scientific(float f, int64_t precision); -float Num32__mod(float num, float modulus); -bool Num32__isinf(float n); -bool Num32__finite(float n); -bool Num32__isnan(float n); -float Num32__random(void); -float Num32__nan(CORD tag); -extern const TypeInfo Num32; +CORD Num32$as_text(const float *f, bool colorize, const TypeInfo *type); +int32_t Num32$compare(const float *x, const float *y, const TypeInfo *type); +bool Num32$equal(const float *x, const float *y, const TypeInfo *type); +bool Num32$near(float a, float b, float ratio, float absolute); +CORD Num32$format(float f, int64_t precision); +CORD Num32$scientific(float f, int64_t precision); +float Num32$mod(float num, float modulus); +bool Num32$isinf(float n); +bool Num32$finite(float n); +bool Num32$isnan(float n); +float Num32$random(void); +float Num32$nan(CORD tag); +extern const TypeInfo $Num32; // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/pointer.c b/builtins/pointer.c index a894e6a5..271bb6cc 100644 --- a/builtins/pointer.c +++ b/builtins/pointer.c @@ -18,7 +18,7 @@ typedef struct recursion_s { struct recursion_s *next; } recursion_t; -public CORD Pointer__as_text(const void *x, bool colorize, const TypeInfo *type) { +public CORD Pointer$as_text(const void *x, bool colorize, const TypeInfo *type) { auto ptr_info = type->PointerInfo; if (!x) { CORD typename = generic_as_text(NULL, false, ptr_info.pointed); @@ -50,19 +50,19 @@ public CORD Pointer__as_text(const void *x, bool colorize, const TypeInfo *type) return colorize ? CORD_asprintf("\x1b[34;1m%s\x1b[m%r", ptr_info.sigil, pointed) : CORD_cat(ptr_info.sigil, pointed); } -public int32_t Pointer__compare(const void *x, const void *y, const TypeInfo *type) { +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); } -public bool Pointer__equal(const void *x, const void *y, const TypeInfo *type) { +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; } -public uint32_t Pointer__hash(const void *x, const TypeInfo *type) { +public uint32_t Pointer$hash(const void *x, const TypeInfo *type) { (void)type; uint32_t hash; halfsiphash(x, sizeof(void*), SSS_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); diff --git a/builtins/pointer.h b/builtins/pointer.h index edaaaa21..e5705d20 100644 --- a/builtins/pointer.h +++ b/builtins/pointer.h @@ -8,10 +8,10 @@ #include "types.h" -CORD Pointer__as_text(const void *x, bool colorize, const TypeInfo *type); -int32_t Pointer__compare(const void *x, const void *y, const TypeInfo *type); -bool Pointer__equal(const void *x, const void *y, const TypeInfo *type); -uint32_t Pointer__hash(const void *x, const TypeInfo *type); +CORD Pointer$as_text(const void *x, bool colorize, const TypeInfo *type); +int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type); +bool Pointer$equal(const void *x, const void *y, const TypeInfo *type); +uint32_t Pointer$hash(const void *x, const TypeInfo *type); #define $Null(t) (t*)NULL #define POINTER_TYPE(_sigil, _pointed) (&(TypeInfo){\ diff --git a/builtins/table.c b/builtins/table.c index 03f3e6b3..32bf6858 100644 --- a/builtins/table.c +++ b/builtins/table.c @@ -41,13 +41,13 @@ #define GET_ENTRY(t, i) ((t).entries.data + (t).entries.stride*(i)) #define ENTRIES_TYPE(type) (&(TypeInfo){.size=sizeof(array_t), .align=__alignof__(array_t), .tag=ArrayInfo, .ArrayInfo.item=(&(TypeInfo){.size=entry_size(type), .align=entry_align(type), .tag=OpaqueInfo})}) -const TypeInfo MemoryPointer = { +static const TypeInfo MemoryPointer = { .size=sizeof(void*), .align=__alignof__(void*), .tag=PointerInfo, .PointerInfo={ .sigil="@", - .pointed=&Memory, + .pointed=&$Memory, }, }; @@ -55,7 +55,7 @@ const TypeInfo StrToVoidStarTable = { .size=sizeof(table_t), .align=__alignof__(table_t), .tag=TableInfo, - .TableInfo={.key=&Text, .value=&MemoryPointer}, + .TableInfo={.key=&$Text, .value=&MemoryPointer}, }; static inline size_t entry_size(const TypeInfo *info) @@ -82,7 +82,6 @@ static inline size_t value_offset(const TypeInfo *info) return offset; } - static inline void hshow(const table_t *t) { hdebug("{"); @@ -99,7 +98,7 @@ static inline void hshow(const table_t *t) static void maybe_copy_on_write(table_t *t, const TypeInfo *type) { if (t->entries.data_refcount) { - Array__compact(&t->entries, ENTRIES_TYPE(type)); + Array$compact(&t->entries, ENTRIES_TYPE(type)); } if (t->bucket_info && t->bucket_info->data_refcount) { @@ -109,14 +108,14 @@ static void maybe_copy_on_write(table_t *t, const TypeInfo *type) } } -public void Table_mark_copy_on_write(table_t *t) +public void Table$mark_copy_on_write(table_t *t) { t->entries.data_refcount = 3; if (t->bucket_info) t->bucket_info->data_refcount = 3; } // Return address of value or NULL -public void *Table_get_raw(table_t t, const void *key, const TypeInfo *type) +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; @@ -138,11 +137,11 @@ public void *Table_get_raw(table_t t, const void *key, const TypeInfo *type) return NULL; } -public void *Table_get(table_t t, const void *key, const TypeInfo *type) +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); + void *ret = Table$get_raw(*iter, key, type); if (ret) return ret; } for (const table_t *iter = &t; iter; iter = iter->fallback) { @@ -151,7 +150,7 @@ public void *Table_get(table_t t, const void *key, const TypeInfo *type) return NULL; } -static void Table_set_bucket(table_t *t, const void *entry, int32_t index, const TypeInfo *type) +static void Table$set_bucket(table_t *t, const void *entry, int32_t index, const TypeInfo *type) { assert(t->bucket_info); hshow(t); @@ -215,9 +214,9 @@ static void hashmap_resize_buckets(table_t *t, uint32_t new_capacity, const Type 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++) { + 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); + Table$set_bucket(t, GET_ENTRY(*t, i), i, type); } hshow(t); @@ -225,7 +224,7 @@ static void hashmap_resize_buckets(table_t *t, uint32_t new_capacity, const Type } // Return address of value -public void *Table_reserve(table_t *t, const void *key, const void *value, const TypeInfo *type) +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; @@ -237,7 +236,7 @@ public void *Table_reserve(table_t *t, const void *key, const void *value, const hashmap_resize_buckets(t, 4, type); } else { // Check if we are clobbering a value: - void *value_home = Table_get_raw(*t, key, type); + 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; @@ -260,7 +259,7 @@ public void *Table_reserve(table_t *t, const void *key, const void *value, const if (!value && value_size > 0) { for (table_t *iter = t->fallback; iter; iter = iter->fallback) { - value = Table_get_raw(*iter, key, type); + value = Table$get_raw(*iter, key, type); if (value) break; } for (table_t *iter = t; !value && iter; iter = iter->fallback) { @@ -277,24 +276,24 @@ public void *Table_reserve(table_t *t, const void *key, const void *value, const memcpy(buf + value_offset(type), value, value_size); else memset(buf + value_offset(type), 0, value_size); - Array__insert(&t->entries, buf, 0, ENTRIES_TYPE(type)); + Array$insert(&t->entries, buf, 0, ENTRIES_TYPE(type)); int64_t entry_index = t->entries.length-1; void *entry = GET_ENTRY(*t, entry_index); - Table_set_bucket(t, entry, entry_index, type); + 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) +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); + (void)Table$reserve(t, key, value, type); } -public void Table_remove(table_t *t, const void *key, const TypeInfo *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; + 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); @@ -363,7 +362,7 @@ public void Table_remove(table_t *t, const void *key, const TypeInfo *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(&t->entries, t->entries.length, 1, ENTRIES_TYPE(type)); + Array$remove(&t->entries, t->entries.length, 1, ENTRIES_TYPE(type)); int64_t bucket_to_clear; if (prev) { // Middle (or end) of a chain @@ -386,22 +385,22 @@ public void Table_remove(table_t *t, const void *key, const TypeInfo *type) hshow(t); } -public void *Table_entry(table_t t, int64_t n) +public void *Table$entry(table_t t, int64_t n) { - if (n < 1 || n > Table_length(t)) + if (n < 1 || n > Table$length(t)) return NULL; return GET_ENTRY(t, n-1); } -public void Table_clear(table_t *t) +public void Table$clear(table_t *t) { memset(t, 0, sizeof(table_t)); } -public bool Table_equal(const table_t *x, const table_t *y, const TypeInfo *type) +public bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type) { assert(type->tag == TableInfo); - if (Table_length(*x) != Table_length(*y)) + if (Table$length(*x) != Table$length(*y)) return false; if ((x->default_value != NULL) != (y->default_value != NULL)) @@ -411,10 +410,10 @@ public bool Table_equal(const table_t *x, const table_t *y, const TypeInfo *type return false; const TypeInfo *value_type = type->TableInfo.value; - for (int64_t i = 0, length = Table_length(*x); i < length; i++) { + for (int64_t i = 0, length = Table$length(*x); i < length; i++) { void *x_key = GET_ENTRY(*x, i); void *x_value = x_key + value_offset(type); - void *y_value = Table_get_raw(*y, x_key, type); + void *y_value = Table$get_raw(*y, x_key, type); if (!y_value) return false; if (!generic_equal(x_value, y_value, value_type)) @@ -426,13 +425,13 @@ public bool Table_equal(const table_t *x, const table_t *y, const TypeInfo *type return false; if (x->fallback && y->fallback - && !Table_equal(x->fallback, y->fallback, type)) + && !Table$equal(x->fallback, y->fallback, type)) return false; return true; } -public int32_t Table_compare(const table_t *x, const table_t *y, const TypeInfo *type) +public int32_t Table$compare(const table_t *x, const table_t *y, const TypeInfo *type) { assert(type->tag == TableInfo); auto table = type->TableInfo; @@ -442,8 +441,8 @@ public int32_t Table_compare(const table_t *x, const table_t *y, const TypeInfo return (x->entries.length > y->entries.length) - (x->entries.length < y->entries.length); array_t x_entries = x->entries, y_entries = y->entries; - Array__sort(&x_entries, table.key); - Array__sort(&y_entries, table.key); + Array$sort(&x_entries, table.key); + Array$sort(&y_entries, table.key); 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; @@ -471,7 +470,7 @@ public int32_t Table_compare(const table_t *x, const table_t *y, const TypeInfo return 0; } -public uint32_t Table_hash(const table_t *t, const TypeInfo *type) +public uint32_t Table$hash(const table_t *t, const TypeInfo *type) { assert(type->tag == TableInfo); // Table hashes are computed as: @@ -481,20 +480,20 @@ public uint32_t Table_hash(const table_t *t, const TypeInfo *type) int64_t val_off = value_offset(type); uint32_t key_hashes = 0, value_hashes = 0, fallback_hash = 0, default_hash = 0; - for (int64_t i = 0, length = Table_length(*t); i < length; i++) { + for (int64_t i = 0, length = Table$length(*t); i < length; i++) { void *entry = GET_ENTRY(*t, i); key_hashes ^= generic_hash(entry, table.key); value_hashes ^= generic_hash(entry + val_off, table.value); } if (t->fallback) - fallback_hash = Table_hash(t->fallback, type); + fallback_hash = Table$hash(t->fallback, type); if (t->default_value) default_hash = generic_hash(t->default_value, table.value); struct { int64_t len; uint32_t k, v, f, d; } components = { - Table_length(*t), + Table$length(*t), key_hashes, value_hashes, fallback_hash, @@ -505,7 +504,7 @@ public uint32_t Table_hash(const table_t *t, const TypeInfo *type) return hash; } -public CORD Table_as_text(const table_t *t, bool colorize, const TypeInfo *type) +public CORD Table$as_text(const table_t *t, bool colorize, const TypeInfo *type) { assert(type->tag == TableInfo); auto table = type->TableInfo; @@ -515,7 +514,7 @@ public CORD Table_as_text(const table_t *t, bool colorize, const TypeInfo *type) int64_t val_off = value_offset(type); CORD c = "{"; - for (int64_t i = 0, length = Table_length(*t); i < length; i++) { + for (int64_t i = 0, length = Table$length(*t); i < length; i++) { if (i > 0) c = CORD_cat(c, ", "); void *entry = GET_ENTRY(*t, i); @@ -526,7 +525,7 @@ public CORD Table_as_text(const table_t *t, bool colorize, const TypeInfo *type) if (t->fallback) { c = CORD_cat(c, "; fallback="); - c = CORD_cat(c, Table_as_text(t->fallback, colorize, type)); + c = CORD_cat(c, Table$as_text(t->fallback, colorize, type)); } if (t->default_value) { @@ -538,7 +537,7 @@ public CORD Table_as_text(const table_t *t, bool colorize, const TypeInfo *type) return c; } -public table_t Table_from_entries(array_t entries, const TypeInfo *type) +public table_t Table$from_entries(array_t entries, const TypeInfo *type) { assert(type->tag == TableInfo); table_t t = {.entries=entries}; @@ -546,36 +545,36 @@ public table_t Table_from_entries(array_t entries, const TypeInfo *type) return t; } -void *Table_str_get(table_t t, const char *key) +public void *Table$str_get(table_t t, const char *key) { - void **ret = Table_get(t, &key, &StrToVoidStarTable); + void **ret = Table$get(t, &key, &StrToVoidStarTable); return ret ? *ret : NULL; } -void *Table_str_get_raw(table_t t, const char *key) +public void *Table$str_get_raw(table_t t, const char *key) { - void **ret = Table_get_raw(t, &key, &StrToVoidStarTable); + void **ret = Table$get_raw(t, &key, &StrToVoidStarTable); return ret ? *ret : NULL; } -void *Table_str_reserve(table_t *t, const char *key, const void *value) +public void *Table$str_reserve(table_t *t, const char *key, const void *value) { - return Table_reserve(t, &key, &value, &StrToVoidStarTable); + return Table$reserve(t, &key, &value, &StrToVoidStarTable); } -void Table_str_set(table_t *t, const char *key, const void *value) +public void Table$str_set(table_t *t, const char *key, const void *value) { - Table_set(t, &key, &value, &StrToVoidStarTable); + Table$set(t, &key, &value, &StrToVoidStarTable); } -void Table_str_remove(table_t *t, const char *key) +public void Table$str_remove(table_t *t, const char *key) { - return Table_remove(t, &key, &StrToVoidStarTable); + return Table$remove(t, &key, &StrToVoidStarTable); } -void *Table_str_entry(table_t t, int64_t n) +public void *Table$str_entry(table_t t, int64_t n) { - return Table_entry(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 index 38c66427..fdde32ae 100644 --- a/builtins/table.h +++ b/builtins/table.h @@ -12,7 +12,7 @@ #define $Table(key_t, val_t, key_info, value_info, fb, def, N, ...) ({ \ struct { key_t k; val_t v; } $ents[N] = {__VA_ARGS__}; \ - table_t $table = Table_from_entries((array_t){ \ + 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], \ @@ -22,36 +22,36 @@ $table; }) #define $Table_get(table_expr, key_t, val_t, key_expr, info_expr, filename, start, end) ({ \ const table_t $t = table_expr; key_t $k = key_expr; const TypeInfo* $info = info_expr; \ - const val_t *$v = Table_get($t, &$k, $info); \ + const val_t *$v = Table$get($t, &$k, $info); \ if (__builtin_expect($v == NULL, 0)) \ fail_source(filename, start, end, "The key %r is not in this table\n", generic_as_text(&$k, USE_COLOR, $info->TableInfo.key)); \ *$v; }) -table_t Table_from_entries(array_t entries, const TypeInfo *type); -void *Table_get(table_t t, const void *key, const TypeInfo *type); -void *Table_get_raw(table_t t, const void *key, const TypeInfo *type); -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); }) -void Table_remove(table_t *t, const void *key, const TypeInfo *type); -void Table_clear(table_t *t); -void Table_mark_copy_on_write(table_t *t); -int32_t Table_compare(const table_t *x, const table_t *y, const TypeInfo *type); -bool Table_equal(const table_t *x, const table_t *y, const TypeInfo *type); -uint32_t Table_hash(const table_t *t, const TypeInfo *type); -CORD Table_as_text(const table_t *t, bool colorize, const TypeInfo *type); - -void *Table_str_entry(table_t t, int64_t n); -void *Table_str_get(table_t t, const char *key); -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) +table_t Table$from_entries(array_t entries, const TypeInfo *type); +void *Table$get(table_t t, const void *key, const TypeInfo *type); +void *Table$get_raw(table_t t, const void *key, const TypeInfo *type); +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); }) +void Table$remove(table_t *t, const void *key, const TypeInfo *type); +void Table$clear(table_t *t); +void Table$mark_copy_on_write(table_t *t); +int32_t Table$compare(const table_t *x, const table_t *y, const TypeInfo *type); +bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type); +uint32_t Table$hash(const table_t *t, const TypeInfo *type); +CORD Table$as_text(const table_t *t, bool colorize, const TypeInfo *type); + +void *Table$str_entry(table_t t, int64_t n); +void *Table$str_get(table_t t, const char *key); +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 StrToVoidStarTable; diff --git a/builtins/text.c b/builtins/text.c index f2a7c2f9..0450e9b2 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -24,16 +24,16 @@ #define CLAMP(x, lo, hi) MIN(hi, MAX(x,lo)) -public CORD Text__as_text(const void *text, bool colorize, const TypeInfo *info) +public CORD Text$as_text(const void *text, bool colorize, const TypeInfo *info) { if (!text) return info->TextInfo.lang; - CORD ret = Text__quoted(*(CORD*)text, colorize); + CORD ret = Text$quoted(*(CORD*)text, colorize); if (!streq(info->TextInfo.lang, "Text")) ret = colorize ? CORD_all("\x1b[1m$", info->TextInfo.lang, "\x1b[m", ret) : CORD_all("$", info->TextInfo.lang, ret); return ret; } -public CORD Text__quoted(CORD str, bool colorize) +public CORD Text$quoted(CORD str, bool colorize) { // Note: it's important to have unicode strings not get broken up with // escapes, otherwise they won't print right. @@ -92,7 +92,7 @@ public CORD Text__quoted(CORD str, bool colorize) } } -public int Text__compare(const CORD *x, const CORD *y) +public int Text$compare(const CORD *x, const CORD *y) { uint8_t *xx = (uint8_t*)CORD_to_const_char_star(*x); uint8_t *yy = (uint8_t*)CORD_to_const_char_star(*y); @@ -102,12 +102,12 @@ public int Text__compare(const CORD *x, const CORD *y) return result; } -public bool Text__equal(const CORD *x, const CORD *y) +public bool Text$equal(const CORD *x, const CORD *y) { - return Text__compare(x, y) == 0; + return Text$compare(x, y) == 0; } -public uint32_t Text__hash(const CORD *cord) +public uint32_t Text$hash(const CORD *cord) { if (!*cord) return 0; @@ -124,7 +124,7 @@ public uint32_t Text__hash(const CORD *cord) return hash; } -public CORD Text__upper(CORD str) +public CORD Text$upper(CORD str) { if (!str) return str; size_t len = strlen(str) + 1; @@ -133,7 +133,7 @@ public CORD Text__upper(CORD str) return (CORD)u8_toupper((const uint8_t*)str, len-1, uc_locale_language(), NULL, dest, &len); } -public CORD Text__lower(CORD str) +public CORD Text$lower(CORD str) { if (!str) return str; size_t len = strlen(str) + 1; @@ -142,7 +142,7 @@ public CORD Text__lower(CORD str) return (CORD)u8_tolower((const uint8_t*)str, len-1, uc_locale_language(), NULL, dest, &len); } -public CORD Text__title(CORD str) +public CORD Text$title(CORD str) { if (!str) return str; size_t len = strlen(str) + 1; @@ -151,7 +151,7 @@ public CORD Text__title(CORD str) return (CORD)u8_totitle((const uint8_t*)str, len-1, uc_locale_language(), NULL, dest, &len); } -public bool Text__has(CORD str, CORD target, where_e where) +public bool Text$has(CORD str, CORD target, where_e where) { if (!target) return true; if (!str) return false; @@ -168,7 +168,7 @@ public bool Text__has(CORD str, CORD target, where_e where) } } -public CORD Text__without(CORD str, CORD target, where_e where) +public CORD Text$without(CORD str, CORD target, where_e where) { if (!str || !target) return str; @@ -187,7 +187,7 @@ public CORD Text__without(CORD str, CORD target, where_e where) } } -public CORD Text__trimmed(CORD str, CORD skip, where_e where) +public CORD Text$trimmed(CORD str, CORD skip, where_e where) { if (!str || !skip) return str; const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(str); @@ -213,14 +213,14 @@ public CORD Text__trimmed(CORD str, CORD skip, where_e where) } } -public find_result_t Text__find(CORD str, CORD pat) +public find_result_t Text$find(CORD str, CORD pat) { if (!pat) return (find_result_t){.status=FIND_SUCCESS, .index=1}; size_t pos = CORD_str(str, 0, pat); return (pos == CORD_NOT_FOUND) ? (find_result_t){.status=FIND_FAILURE} : (find_result_t){.status=FIND_SUCCESS, .index=(int32_t)pos}; } -public CORD Text__replace(CORD text, CORD pat, CORD replacement, int64_t limit) +public CORD Text$replace(CORD text, CORD pat, CORD replacement, int64_t limit) { if (!text || !pat) return text; CORD ret = CORD_EMPTY; @@ -233,7 +233,7 @@ public CORD Text__replace(CORD text, CORD pat, CORD replacement, int64_t limit) return CORD_cat(ret, CORD_substr(text, pos, str_len - pos)); } -public array_t Text__split(CORD str, CORD split) +public array_t Text$split(CORD str, CORD split) { if (!str) return (array_t){.data=GC_MALLOC(sizeof(CORD)), .atomic=1, .length=1, .stride=sizeof(CORD)}; array_t strings = {.stride=sizeof(CORD), .atomic=1}; @@ -257,7 +257,7 @@ public array_t Text__split(CORD str, CORD split) return strings; } -public CORD Text__join(CORD glue, array_t pieces) +public CORD Text$join(CORD glue, array_t pieces) { if (pieces.length == 0) return CORD_EMPTY; @@ -269,7 +269,7 @@ public CORD Text__join(CORD glue, array_t pieces) return ret; } -public array_t Text__clusters(CORD text) +public array_t Text$clusters(CORD text) { array_t clusters = {.atomic=1}; const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(text); @@ -285,7 +285,7 @@ public array_t Text__clusters(CORD text) char cluster_buf[len+1]; strlcpy(cluster_buf, (char*)pos, len+1); CORD cluster = CORD_from_char_star(cluster_buf); - Array__insert(&clusters, &cluster, 0, $ArrayInfo(&Text)); + Array$insert(&clusters, &cluster, 0, $ArrayInfo(&$Text)); pos = next; } @@ -293,7 +293,7 @@ public array_t Text__clusters(CORD text) return clusters; } -public array_t Text__codepoints(CORD text) +public array_t Text$codepoints(CORD text) { const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(text); uint8_t norm_buf[128] = {0}; @@ -316,7 +316,7 @@ public array_t Text__codepoints(CORD text) return ret; } -public array_t Text__bytes(CORD text) +public array_t Text$bytes(CORD text) { const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(text); uint8_t norm_buf[128] = {0}; @@ -336,7 +336,7 @@ public array_t Text__bytes(CORD text) return ret; } -public int64_t Text__num_clusters(CORD text) +public int64_t Text$num_clusters(CORD text) { const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(text); int64_t num_clusters = 0; @@ -349,7 +349,7 @@ public int64_t Text__num_clusters(CORD text) return num_clusters; } -public int64_t Text__num_codepoints(CORD text) +public int64_t Text$num_codepoints(CORD text) { const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(text); uint8_t buf[128] = {0}; @@ -361,7 +361,7 @@ public int64_t Text__num_codepoints(CORD text) return num_codepoints; } -public int64_t Text__num_bytes(CORD text) +public int64_t Text$num_bytes(CORD text) { const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(text); uint8_t norm_buf[128] = {0}; @@ -373,9 +373,9 @@ public int64_t Text__num_bytes(CORD text) return norm_len; } -public array_t Text__character_names(CORD text) +public array_t Text$character_names(CORD text) { - array_t codepoints = Text__codepoints(text); + array_t codepoints = Text$codepoints(text); array_t ret = {.length=codepoints.length, .stride=sizeof(CORD), .data=GC_MALLOC(sizeof(CORD)*codepoints.length)}; for (int64_t i = 0; i < codepoints.length; i++) { char buf[UNINAME_MAX]; @@ -385,7 +385,7 @@ public array_t Text__character_names(CORD text) return ret; } -public const TypeInfo Text = { +public const TypeInfo $Text = { .size=sizeof(CORD), .align=__alignof__(CORD), .tag=TextInfo, diff --git a/builtins/text.h b/builtins/text.h index edbd119c..3cda9d7b 100644 --- a/builtins/text.h +++ b/builtins/text.h @@ -18,29 +18,29 @@ typedef struct { int32_t index; } find_result_t; -CORD Text__as_text(const void *str, bool colorize, const TypeInfo *info); -CORD Text__quoted(CORD str, bool colorize); -int Text__compare(const CORD *x, const CORD *y); -bool Text__equal(const CORD *x, const CORD *y); -uint32_t Text__hash(const CORD *cord); -CORD Text__upper(CORD str); -CORD Text__lower(CORD str); -CORD Text__title(CORD str); -bool Text__has(CORD str, CORD target, where_e where); -CORD Text__without(CORD str, CORD target, where_e where); -CORD Text__trimmed(CORD str, CORD skip, where_e where); -find_result_t Text__find(CORD str, CORD pat); -CORD Text__replace(CORD text, CORD pat, CORD replacement, int64_t limit); -array_t Text__split(CORD str, CORD split); -CORD Text__join(CORD glue, array_t pieces); -array_t Text__clusters(CORD text); -array_t Text__codepoints(CORD text); -array_t Text__bytes(CORD text); -int64_t Text__num_clusters(CORD text); -int64_t Text__num_codepoints(CORD text); -int64_t Text__num_bytes(CORD text); -array_t Text__character_names(CORD text); - -extern const TypeInfo Text; +CORD Text$as_text(const void *str, bool colorize, const TypeInfo *info); +CORD Text$quoted(CORD str, bool colorize); +int Text$compare(const CORD *x, const CORD *y); +bool Text$equal(const CORD *x, const CORD *y); +uint32_t Text$hash(const CORD *cord); +CORD Text$upper(CORD str); +CORD Text$lower(CORD str); +CORD Text$title(CORD str); +bool Text$has(CORD str, CORD target, where_e where); +CORD Text$without(CORD str, CORD target, where_e where); +CORD Text$trimmed(CORD str, CORD skip, where_e where); +find_result_t Text$find(CORD str, CORD pat); +CORD Text$replace(CORD text, CORD pat, CORD replacement, int64_t limit); +array_t Text$split(CORD str, CORD split); +CORD Text$join(CORD glue, array_t pieces); +array_t Text$clusters(CORD text); +array_t Text$codepoints(CORD text); +array_t Text$bytes(CORD text); +int64_t Text$num_clusters(CORD text); +int64_t Text$num_codepoints(CORD text); +int64_t Text$num_bytes(CORD text); +array_t Text$character_names(CORD text); + +extern const TypeInfo $Text; // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/types.c b/builtins/types.c index cdf58735..355473e9 100644 --- a/builtins/types.c +++ b/builtins/types.c @@ -11,7 +11,7 @@ #include "table.h" #include "types.h" -public CORD Type__as_text(const void *typeinfo, bool colorize, const TypeInfo *type) +public CORD Type$as_text(const void *typeinfo, bool colorize, const TypeInfo *type) { if (!typeinfo) return "TypeInfo"; @@ -22,17 +22,17 @@ public CORD Type__as_text(const void *typeinfo, bool colorize, const TypeInfo *t return c; } -public const TypeInfo TypeInfo_info = { +public const TypeInfo $TypeInfo = { .size=sizeof(TypeInfo), .align=__alignof__(TypeInfo), .tag=CustomInfo, .TypeInfoInfo.type_str="TypeInfo", }; -public const TypeInfo Void = {.size=0, .align=0}; -public const TypeInfo Abort = {.size=0, .align=0}; +public const TypeInfo $Void = {.size=0, .align=0}; +public const TypeInfo $Abort = {.size=0, .align=0}; -public CORD Func__as_text(const void *fn, bool colorize, const TypeInfo *type) +public CORD Func$as_text(const void *fn, bool colorize, const TypeInfo *type) { (void)fn; CORD c = type->FunctionInfo.type_str; diff --git a/builtins/types.h b/builtins/types.h index bf94b526..ee49d9ac 100644 --- a/builtins/types.h +++ b/builtins/types.h @@ -63,12 +63,12 @@ typedef struct TypeInfo { #define $TypeInfoInfo(typestr) &((TypeInfo){.size=sizeof(TypeInfo), .align=__alignof__(TypeInfo), \ .tag=TypeInfoInfo, .TypeInfoInfo.type_str=typestr}) -extern const TypeInfo TypeInfo_info; -extern const TypeInfo Void; -extern const TypeInfo Abort; +extern const TypeInfo $TypeInfo; +extern const TypeInfo $Void; +extern const TypeInfo $Abort; #define Void_t void -CORD Type__as_text(const void *typeinfo, bool colorize, const TypeInfo *type); -CORD Func__as_text(const void *fn, bool colorize, const TypeInfo *type); +CORD Type$as_text(const void *typeinfo, bool colorize, const TypeInfo *type); +CORD Func$as_text(const void *fn, bool colorize, const TypeInfo *type); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/util.h b/builtins/util.h index 9c8d8e9e..5344a7c9 100644 --- a/builtins/util.h +++ b/builtins/util.h @@ -47,15 +47,15 @@ CORD CORD_asprintf(CORD fmt, ...); } while(0) #define LIST_MAP(src, var, ...) ({\ - __typeof(src) __mapped = NULL; \ - __typeof(src) *__next = &__mapped; \ + __typeof(src) $mapped = NULL; \ + __typeof(src) *$next = &$mapped; \ for (__typeof(src) var = src; var; var = var->next) { \ - *__next = GC_MALLOC(sizeof(__typeof(*(src)))); \ - **__next = *var; \ - **__next = (__typeof(*(src))){__VA_ARGS__}; \ - __next = &((*__next)->next); \ + *$next = GC_MALLOC(sizeof(__typeof(*(src)))); \ + **$next = *var; \ + **$next = (__typeof(*(src))){__VA_ARGS__}; \ + $next = &((*$next)->next); \ } \ - __mapped; }) + $mapped; }) // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
