diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-04-16 13:50:07 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-04-16 13:50:07 -0400 |
| commit | 369c601a560f9c081e2bc04e4f4fe5a8b7b1a6a0 (patch) | |
| tree | aeaa5f7a1af83e25de99187f6e4c31e0609de65b /builtins | |
| parent | 98b93bb15922974feb06103bea06ec305e17b2ce (diff) | |
Invert escaping so user symbols get prepended with "$" and builtin
symbols don't
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/array.h | 70 | ||||
| -rw-r--r-- | builtins/functions.c | 2 | ||||
| -rw-r--r-- | builtins/functions.h | 2 | ||||
| -rw-r--r-- | builtins/macros.h | 8 | ||||
| -rw-r--r-- | builtins/pointer.h | 2 | ||||
| -rw-r--r-- | builtins/table.h | 36 | ||||
| -rw-r--r-- | builtins/util.h | 14 |
7 files changed, 67 insertions, 67 deletions
diff --git a/builtins/array.h b/builtins/array.h index 18757850..711b89c4 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -11,48 +11,48 @@ #include "types.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, filename, start, end) *({ \ - 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); \ - (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); \ - if ($arr->data_refcount > 0) \ - 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 -#define $Array_get_unchecked(type, x, i) *({ const array_t $arr = x; int64_t $index = (int64_t)(i); \ - int64_t $off = $index + ($index < 0) * ($arr.length + 1) - 1; \ - (type*)($arr.data + $arr.stride * $off);}) -#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)), \ +#define Array_get(item_type, arr_expr, index_expr, filename, start, end) *({ \ + 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); \ + (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); \ + if (arr->data_refcount > 0) \ + 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 +#define Array_get_unchecked(type, x, i) *({ const array_t arr = x; int64_t index = (int64_t)(i); \ + int64_t off = index + (index < 0) * (arr.length + 1) - 1; \ + (type*)(arr.data + arr.stride * off);}) +#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=1}; }) -#define $TypedArrayN(t, N, ...) ({ t $items[N] = {__VA_ARGS__}; \ +#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)), \ + .stride=(int64_t)&items[1] - (int64_t)&items[0], \ + .data=memcpy(GC_MALLOC(sizeof(items)), items, sizeof(items)), \ .atomic=0, \ .data_refcount=1}; }) -#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), \ +#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=1}; }) -#define $ARRAY_INCREF(arr) (arr).data_refcount |= ((arr).data_refcount << 1) | 1 -#define $ARRAY_DECREF(arr) (arr).data_refcount &= 2 +#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); }) +#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); diff --git a/builtins/functions.c b/builtins/functions.c index b292f511..a1ebc6a8 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -153,7 +153,7 @@ public CORD builtin_last_err() return CORD_from_char_star(strerror(errno)); } -public void $test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end) +public void test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end) { static file_t *file = NULL; if (filename && (file == NULL || strcmp(file->filename, filename) != 0)) diff --git a/builtins/functions.h b/builtins/functions.h index cd1771a8..ed63ec36 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -13,7 +13,7 @@ extern const char *TOMO_HASH_VECTOR; void fail(CORD fmt, ...); void fail_source(const char *filename, int64_t start, int64_t end, CORD fmt, ...); CORD builtin_last_err(); -void $test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end); +void test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end); void say(CORD text); uint32_t generic_hash(const void *obj, const TypeInfo *type); diff --git a/builtins/macros.h b/builtins/macros.h index 6128480a..4fbaa5f5 100644 --- a/builtins/macros.h +++ b/builtins/macros.h @@ -5,7 +5,7 @@ #include <gc.h> #include <string.h> -#define $heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) -#define $stack(x) (__typeof(x)*)((__typeof(x)[1]){x}) -#define $tagged(obj_expr, type_name, tag_name) ({ __typeof(obj_expr) $obj = obj_expr; \ - $obj.$tag == $tag$##type_name##$##tag_name ? &$obj.tag_name : NULL; }) +#define heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) +#define stack(x) (__typeof(x)*)((__typeof(x)[1]){x}) +#define tagged(obj_expr, type_name, tag_name) ({ __typeof(obj_expr) obj = obj_expr; \ + obj.$tag == $tag$##type_name##$##tag_name ? &obj.tag_name : NULL; }) diff --git a/builtins/pointer.h b/builtins/pointer.h index e5705d20..538960b3 100644 --- a/builtins/pointer.h +++ b/builtins/pointer.h @@ -13,7 +13,7 @@ int32_t Pointer$compare(const void *x, const void *y, const TypeInfo *type); bool Pointer$equal(const void *x, const void *y, const TypeInfo *type); uint32_t Pointer$hash(const void *x, const TypeInfo *type); -#define $Null(t) (t*)NULL +#define Null(t) (t*)NULL #define POINTER_TYPE(_sigil, _pointed) (&(TypeInfo){\ .size=sizeof(void*), .align=alignof(void*), .tag=PointerInfo, .PointerInfo.sigil=_sigil, .PointerInfo.pointed=_pointed}) diff --git a/builtins/table.h b/builtins/table.h index fdde32ae..0d7af587 100644 --- a/builtins/table.h +++ b/builtins/table.h @@ -10,22 +10,22 @@ #include "datatypes.h" #include "array.h" -#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){ \ - .data=memcpy(GC_MALLOC(sizeof($ents)), $ents, sizeof($ents)), \ - .length=sizeof($ents)/sizeof($ents[0]), \ - .stride=(void*)&$ents[1] - (void*)&$ents[0], \ +#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){ \ + .data=memcpy(GC_MALLOC(sizeof(ents)), ents, sizeof(ents)), \ + .length=sizeof(ents)/sizeof(ents[0]), \ + .stride=(void*)&ents[1] - (void*)&ents[0], \ }, $TableInfo(key_info, value_info)); \ - $table.fallback = fb; \ - $table.default_value = def; \ - $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); \ - 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.fallback = fb; \ + table.default_value = def; \ + 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); \ + 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); @@ -33,9 +33,9 @@ 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); }) +#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); diff --git a/builtins/util.h b/builtins/util.h index 5344a7c9..79925a51 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 |
