diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-04 21:13:50 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-04 21:13:50 -0500 |
| commit | adde91636f04ae7544dba1ca5c6c1a40c074edb9 (patch) | |
| tree | dfeb8c0c16fda6a87ef30b048b070ee4cb175a78 /nextlang.h | |
| parent | b08a0d3e2bf45bae11c982dd24d0292d6436b993 (diff) | |
Builtins
Diffstat (limited to 'nextlang.h')
| -rw-r--r-- | nextlang.h | 34 |
1 files changed, 30 insertions, 4 deletions
@@ -5,8 +5,11 @@ #include <stdbool.h> #include <stdint.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include "builtins/datatypes.h" + #define Int64_t int64_t #define Int32_t int32_t #define Int16_t int16_t @@ -23,18 +26,41 @@ #define Void_t void -#ifndef auto -#define auto __auto_type -#endif +typedef struct __array_s { + void *data; + int64_t length:42; + uint8_t free:4; + bool copy_on_write:1, atomic:1; + int16_t stride:16; +} __array_t; + +#define __Array(t) __array_t #define CORD_asprintf(...) ({ CORD __c; CORD_sprintf(&__c, __VA_ARGS__); __c; }) #define __declare(var, val) __typeof(val) var = val #define __cord(x) _Generic(x, bool: x ? "yes" : "no", \ - int16_t: CORD_asprintf("%d", x), int32_t: CORD_asprintf("%d", x), int64_t: CORD_asprintf("%ld", x), \ + int8_t: CORD_asprintf("%d", x), int16_t: CORD_asprintf("%d", x), \ + int32_t: CORD_asprintf("%d", x), int64_t: CORD_asprintf("%ld", x), \ double: CORD_asprintf("%g", x), float: CORD_asprintf("%g", x), \ CORD: x, \ default: "???") #define __heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) #define __stack(x) (&(__typeof(x)){x}) +#define __length(x) _Generic(x, array_t: (x).length) +// Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1 +#define __index(x, i) _Generic(x, array_t: ({ __typeof(x) __obj; int64_t __offset = i; __offset += (__offset < 0) * (__obj.length + 1) - 1; assert(__offset >= 0 && offset < __obj.length); __obj.data + __obj.stride * __offset;})) +#define __safe_index(x, i) _Generic(x, array_t: ({ __typeof(x) __obj; int64_t __offset = i - 1; __obj.data + __obj.stride * __offset;})) +#define __array(x, ...) ({ __typeof(x) __items[] = {x, __VA_ARGS__}; \ + (array_t){.length=sizeof(__items)/sizeof(__items[0]), \ + .stride=(int64_t)&__items[1] - (int64_t)&__items[0], \ + .data=memcpy(GC_MALLOC(sizeof(__items)), __items, sizeof(__items)), \ + .copy_on_write=1}; }) + +#define not(x) _Generic(x, bool: !(x), default: ~(x)) +#define and(x, y) _Generic(x, bool: (x) && (y), default: (x) & (y)) +#define or(x, y) _Generic(x, bool: (x) || (y), default: (x) | (y)) +#define xor(x, y) ((x) ^ (y)) +#define mod(x, n) ((x) % (n)) +#define mod1(x, n) (((x) % (n)) + (__typeof(x))1) #define say(str) puts(CORD_to_const_char_star(str)) |
