diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-17 19:32:30 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-17 19:32:30 -0500 |
| commit | d46925dbfa8627a6a874545630c2acb6975bfdea (patch) | |
| tree | 2b726ea50adf91b668a24b25d4d9e6ec7f7c2663 /builtins | |
| parent | 7355b2f7fe6f5dda2aee8feca025350146ccd0f5 (diff) | |
Cleanup of builtins
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/bool.c | 11 | ||||
| -rw-r--r-- | builtins/bool.h | 16 | ||||
| -rw-r--r-- | builtins/builtins.c | 10 | ||||
| -rw-r--r-- | builtins/floats.c | 59 | ||||
| -rw-r--r-- | builtins/floats.h | 83 | ||||
| -rw-r--r-- | builtins/functions.c | 3 | ||||
| -rw-r--r-- | builtins/functions.h | 4 | ||||
| -rw-r--r-- | builtins/integers.c | 16 | ||||
| -rw-r--r-- | builtins/integers.h | 30 | ||||
| -rw-r--r-- | builtins/table.c | 8 |
10 files changed, 154 insertions, 86 deletions
diff --git a/builtins/bool.c b/builtins/bool.c index 3409ae8f..9af2ed2c 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -9,13 +9,14 @@ #include <sys/param.h> #include <err.h> -#include "types.h" -#include "../util.h" #include "../SipHash/halfsiphash.h" +#include "../util.h" +#include "bool.h" +#include "types.h" extern const void *SSS_HASH_VECTOR; -static CORD Bool__as_str(const bool *b, bool colorize, const TypeInfo *type) +public CORD Bool__as_str(const bool *b, bool colorize, const TypeInfo *type) { (void)type; if (!b) return "Bool"; @@ -25,9 +26,7 @@ static CORD Bool__as_str(const bool *b, bool colorize, const TypeInfo *type) return *b ? "yes" : "no"; } -public struct { - TypeInfo type; -} Bool_type = { +Bool_namespace_t Bool_type = { .type={ .size=sizeof(bool), .align=alignof(bool), diff --git a/builtins/bool.h b/builtins/bool.h new file mode 100644 index 00000000..703c6bc0 --- /dev/null +++ b/builtins/bool.h @@ -0,0 +1,16 @@ +#pragma once +#include <gc/cord.h> +#include <stdbool.h> +#include <stdint.h> + +#include "types.h" + +CORD Bool__as_str(const bool *b, bool colorize, const TypeInfo *type); + +typedef struct { + TypeInfo type; +} Bool_namespace_t; + +extern Bool_namespace_t Bool_type; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/builtins.c b/builtins/builtins.c deleted file mode 100644 index f01addfd..00000000 --- a/builtins/builtins.c +++ /dev/null @@ -1,10 +0,0 @@ -#include <gc.h> -#include <string.h> -#include <stdio.h> - -#include "array.h" -#include "types.h" -#include "functions.h" -#include "table.h" - -public const char *SSS_HASH_VECTOR = "sss hash vector ----------------------------------------------"; diff --git a/builtins/floats.c b/builtins/floats.c index 900d45ea..cd71700e 100644 --- a/builtins/floats.c +++ b/builtins/floats.c @@ -11,6 +11,7 @@ #include "../SipHash/halfsiphash.h" #include "array.h" +#include "floats.h" #include "string.h" #include "types.h" @@ -23,12 +24,12 @@ public CORD Num__as_str(const double *f, bool colorize, const TypeInfo *type) { return c; } -static 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); } -static 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; } @@ -50,30 +51,7 @@ 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); } -typedef bool (*double_pred_t)(double); -typedef double (*double_unary_fn_t)(double); -typedef double (*double_binary_fn_t)(double, double); - -public struct { - TypeInfo type; - // Constants: - double NaN, _2_sqrt_pi, e, half_pi, inf, inverse_half_pi, inverse_pi, ln10, ln2, - log2e, pi, quarter_pi, sqrt2, sqrt_half, tau; - // Nullary functions: - double (*random)(void); - // Predicates: - double_pred_t finite, isinf, isnan; - // Unary functions: - double_unary_fn_t abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, cos, cosh, erf, erfc, - exp, exp10, exp2, expm1, floor, j0, j1, log, log10, log1p, log2, logb, - nextdown, nextup, rint, round, roundeven, significand, sin, sinh, sqrt, - tan, tanh, tgamma, trunc, y0, y1; - // Binary functions: - double_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder; - // Odds and ends: - CORD (*format)(double f, int64_t precision); - CORD (*scientific)(double f, int64_t precision); -} Num_type = { +public Num_namespace_t Num_type = { .type=(TypeInfo){ .size=sizeof(double), .align=alignof(double), @@ -112,12 +90,12 @@ public CORD Num32__as_str(float *f, bool colorize, const TypeInfo *type) { return c; } -static 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); } -static 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; } @@ -143,30 +121,7 @@ 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); } -typedef bool (*float_pred_t)(float); -typedef float (*float_unary_fn_t)(float); -typedef float (*float_binary_fn_t)(float, float); - -public struct { - TypeInfo type; - // Alphabetized: - float NaN, _2_sqrt_pi, e, half_pi, inf, inverse_half_pi, inverse_pi, ln10, ln2, - log2e, pi, quarter_pi, sqrt2, sqrt_half, tau; - // Nullary functions: - float (*random)(void); - // Predicates: - float_pred_t finite, isinf, isnan; - // Unary functions: - float_unary_fn_t abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, cos, cosh, erf, erfc, - exp, exp10, exp2, expm1, floor, j0, j1, log, log10, log1p, log2, logb, - nextdown, nextup, rint, round, roundeven, significand, sin, sinh, sqrt, - tan, tanh, tgamma, trunc, y0, y1; - // Binary functions: - float_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder; - // Odds and ends: - CORD (*format)(float f, int64_t precision); - CORD (*scientific)(float f, int64_t precision); -} Num32_type = { +public Num32_namespace_t Num32_type = { .type=(TypeInfo){ .size=sizeof(float), .align=alignof(float), diff --git a/builtins/floats.h b/builtins/floats.h new file mode 100644 index 00000000..5e2cb062 --- /dev/null +++ b/builtins/floats.h @@ -0,0 +1,83 @@ +#pragma once +#include <gc/cord.h> +#include <stdbool.h> +#include <stdint.h> + +#include "types.h" + +typedef struct { + TypeInfo type; +} Bool_namespace_t; + +CORD Num__as_str(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); +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); + +typedef bool (*double_pred_t)(double); +typedef double (*double_unary_fn_t)(double); +typedef double (*double_binary_fn_t)(double, double); + +typedef struct { + TypeInfo type; + // Constants: + double NaN, _2_sqrt_pi, e, half_pi, inf, inverse_half_pi, inverse_pi, ln10, ln2, + log2e, pi, quarter_pi, sqrt2, sqrt_half, tau; + // Nullary functions: + double (*random)(void); + // Predicates: + double_pred_t finite, isinf, isnan; + // Unary functions: + double_unary_fn_t abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, cos, cosh, erf, erfc, + exp, exp10, exp2, expm1, floor, j0, j1, log, log10, log1p, log2, logb, + nextdown, nextup, rint, round, roundeven, significand, sin, sinh, sqrt, + tan, tanh, tgamma, trunc, y0, y1; + // Binary functions: + double_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder; + // Odds and ends: + CORD (*format)(double f, int64_t precision); + CORD (*scientific)(double f, int64_t precision); +} Num_namespace_t; + +CORD Num32__as_str(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); +CORD Num32__format(float f, int64_t precision); +CORD Num32__scientific(float f, int64_t precision); +float Num32__mod(float num, float modulus); +float Num32__random(void); +bool Num32__isinf(float n); +bool Num32__finite(float n); +bool Num32__isnan(float n); + +typedef bool (*float_pred_t)(float); +typedef float (*float_unary_fn_t)(float); +typedef float (*float_binary_fn_t)(float, float); + +typedef struct { + TypeInfo type; + // Alphabetized: + float NaN, _2_sqrt_pi, e, half_pi, inf, inverse_half_pi, inverse_pi, ln10, ln2, + log2e, pi, quarter_pi, sqrt2, sqrt_half, tau; + // Nullary functions: + float (*random)(void); + // Predicates: + float_pred_t finite, isinf, isnan; + // Unary functions: + float_unary_fn_t abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, cos, cosh, erf, erfc, + exp, exp10, exp2, expm1, floor, j0, j1, log, log10, log1p, log2, logb, + nextdown, nextup, rint, round, roundeven, significand, sin, sinh, sqrt, + tan, tanh, tgamma, trunc, y0, y1; + // Binary functions: + float_binary_fn_t atan2, copysign, dist, hypot, maxmag, minmag, mod, nextafter, pow, remainder; + // Odds and ends: + CORD (*format)(float f, int64_t precision); + CORD (*scientific)(float f, int64_t precision); +} Num32_namespace_t; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/functions.c b/builtins/functions.c index 93453a3d..f2b2fa82 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -17,7 +17,8 @@ #include "types.h" extern bool USE_COLOR; -extern const void *SSS_HASH_VECTOR; + +public const char *SSS_HASH_VECTOR = "sss hash vector ----------------------------------------------";; public void fail(const char *fmt, ...) { diff --git a/builtins/functions.h b/builtins/functions.h index d5c9cf8b..0f4beacf 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -6,10 +6,12 @@ #include "types.h" +extern const char *SSS_HASH_VECTOR; + void builtin_say(CORD str, CORD end); void builtin_fail(CORD fmt, ...); CORD builtin_last_err(); -void builtin_doctest(const char *label, CORD expr, const char *type, bool use_color, const char *expected, const char *filename, int start, int end); +void __doctest(CORD label, void *expr, TypeInfo *type, CORD expected, const char *filename, int start, int end); uint32_t generic_hash(const void *obj, const TypeInfo *type); int32_t generic_compare(const void *x, const void *y, const TypeInfo *type); diff --git a/builtins/integers.c b/builtins/integers.c index e89de0c3..36e7d7a8 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -7,11 +7,10 @@ #include "../SipHash/halfsiphash.h" #include "array.h" +#include "integers.h" #include "types.h" #include "string.h" -extern const void *SSS_HASH_VECTOR; - #define xstr(a) str(a) #define str(a) #a @@ -48,15 +47,7 @@ extern const void *SSS_HASH_VECTOR; uint32_t r = arc4random_uniform((uint32_t)range); \ return min + (c_type)r; \ } \ - public struct { \ - TypeInfo type; \ - c_type min, max; \ - c_type (*abs)(c_type i); \ - CORD (*format)(c_type i, int64_t digits); \ - CORD (*hex)(c_type i, int64_t digits, bool uppercase, bool prefix); \ - CORD (*octal)(c_type i, int64_t digits, bool prefix); \ - c_type (*random)(int64_t min, int64_t max); \ - } KindOfInt##_type = { \ + public KindOfInt##_namespace_t KindOfInt##_type = { \ .type={ \ .size=sizeof(c_type), \ .align=alignof(c_type), \ @@ -72,9 +63,10 @@ extern const void *SSS_HASH_VECTOR; .random=KindOfInt##__random, \ }; -DEFINE_INT_TYPE(int64_t, Int, "ld", labs, INT64_MIN, INT64_MAX); +DEFINE_INT_TYPE(int64_t, Int64, "ld", labs, INT64_MIN, INT64_MAX); DEFINE_INT_TYPE(int32_t, Int32, "d_i32", abs, INT32_MIN, INT32_MAX); DEFINE_INT_TYPE(int16_t, Int16, "d_i16", abs, INT16_MIN, INT16_MAX); DEFINE_INT_TYPE(int8_t, Int8, "d_i8", abs, INT8_MIN, INT8_MAX); +#undef DEFINE_INT_TYPE // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/integers.h b/builtins/integers.h new file mode 100644 index 00000000..3e00d962 --- /dev/null +++ b/builtins/integers.h @@ -0,0 +1,30 @@ +#pragma once +#include <gc/cord.h> +#include <stdbool.h> +#include <stdint.h> + +#include "types.h" + +#define DEFINE_INT_TYPE(c_type, KindOfInt)\ + CORD KindOfInt ## __as_str(const c_type *i, bool colorize, const TypeInfo *type); \ + int32_t KindOfInt ## __compare(const c_type *x, const c_type *y, const TypeInfo *type); \ + CORD KindOfInt ## __format(c_type i, int64_t digits); \ + CORD KindOfInt ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix); \ + CORD KindOfInt ## __octal(c_type i, int64_t digits, bool prefix); \ + c_type KindOfInt ## __random(int64_t min, int64_t max); \ + typedef struct { \ + TypeInfo type; \ + c_type min, max; \ + c_type (*abs)(c_type i); \ + CORD (*format)(c_type i, int64_t digits); \ + CORD (*hex)(c_type i, int64_t digits, bool uppercase, bool prefix); \ + CORD (*octal)(c_type i, int64_t digits, bool prefix); \ + c_type (*random)(int64_t min, int64_t max); \ + } KindOfInt##_namespace_t; +DEFINE_INT_TYPE(int64_t, Int64); +DEFINE_INT_TYPE(int32_t, Int32); +DEFINE_INT_TYPE(int16_t, Int16); +DEFINE_INT_TYPE(int8_t, Int8); +#undef DEFINE_INT_TYPE + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/table.c b/builtins/table.c index ad8c6e4c..6db29186 100644 --- a/builtins/table.c +++ b/builtins/table.c @@ -40,7 +40,7 @@ #define END_OF_CHAIN UINT32_MAX #define GET_ENTRY(t, i) ((t)->entries.data + (t)->entries.stride*(i)) -#define ENTRY_TYPE(type) (&(TypeInfo){.size=entry_size(type), .align=entry_align(type), .tag=OpaqueInfo}) +#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})}) extern const void *SSS_HASH_VECTOR; @@ -102,7 +102,7 @@ static inline void hshow(const table_t *t) static void maybe_copy_on_write(table_t *t, const TypeInfo *type) { if (t->entries.copy_on_write) { - Array__compact(&t->entries, ENTRY_TYPE(type)); + Array__compact(&t->entries, ENTRIES_TYPE(type)); } if (t->bucket_info && t->bucket_info->copy_on_write) { @@ -279,7 +279,7 @@ 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, ENTRY_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); @@ -365,7 +365,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, ENTRY_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 |
