diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-05 14:40:28 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-05 14:40:28 -0400 |
| commit | c045c54309dfaa7bb11f97dc3f36c595736eca3b (patch) | |
| tree | 52c6b1602adf5d334be6e913273b609d1e88eaa4 /builtins | |
| parent | 8e2156ac483e7fa0f2007188b670eb11f2a44720 (diff) | |
Add a Range datatype with creation methods like `5:to(10)` and
modification methods like `range:by(2)` or `range:reversed()`
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/datatypes.h | 4 | ||||
| -rw-r--r-- | builtins/integers.c | 3 | ||||
| -rw-r--r-- | builtins/integers.h | 1 | ||||
| -rw-r--r-- | builtins/range.c | 58 | ||||
| -rw-r--r-- | builtins/range.h | 10 | ||||
| -rw-r--r-- | builtins/tomo.h | 1 |
6 files changed, 77 insertions, 0 deletions
diff --git a/builtins/datatypes.h b/builtins/datatypes.h index f1aa5747..12c7cbc4 100644 --- a/builtins/datatypes.h +++ b/builtins/datatypes.h @@ -54,4 +54,8 @@ typedef struct { void *fn, *userdata; } closure_t; +typedef struct Range_s { + int64_t first, last, step; +} Range_t; + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/integers.c b/builtins/integers.c index d50c6573..db1a73e2 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -68,6 +68,9 @@ } \ return (c_type)((uint64_t)min + (r % range)); \ } \ + public Range_t KindOfInt ## $to(c_type from, c_type to) { \ + return (Range_t){from, to, to >= from ? 1 : -1}; \ + } \ public c_type KindOfInt ## $from_text(CORD text, CORD *the_rest) { \ const char *str = CORD_to_const_char_star(text); \ long i; \ diff --git a/builtins/integers.h b/builtins/integers.h index 2c638a4a..4ab30490 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -28,6 +28,7 @@ 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(c_type min, c_type max); \ + Range_t type_name ## $to(c_type from, c_type to); \ c_type type_name ## $from_text(CORD text, CORD *the_rest); \ extern const c_type type_name ## $min, type_name##$max; \ extern const TypeInfo $ ## type_name; diff --git a/builtins/range.c b/builtins/range.c new file mode 100644 index 00000000..ced2a26f --- /dev/null +++ b/builtins/range.c @@ -0,0 +1,58 @@ +// Functions that operate on numeric ranges + +#include <ctype.h> +#include <err.h> +#include <gc.h> +#include <gc/cord.h> +#include <math.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/param.h> + +#include "types.h" +#include "util.h" + + +static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo *type) +{ + (void)type; + int32_t diff = (x->first > y->first) - (x->first < y->first); + if (diff != 0) return diff; + diff = (x->last > y->last) - (x->last < y->last); + if (diff != 0) return diff; + return (x->step > y->step) - (x->step < y->step); +} + +static bool Range$equal(const Range_t *x, const Range_t *y, const TypeInfo *type) +{ + (void)type; + return (x->first == y->first) && (x->last == y->last) && (x->step == y->step); +} + +static CORD Range$as_text(const Range_t *r, bool use_color, const TypeInfo *type) +{ + (void)type; + if (!r) return "Range"; + + return CORD_asprintf(use_color ? "\x1b[0;1mRange\x1b[m(first=\x1b[35m%ld\x1b[m, last=\x1b[35m%ld\x1b[m, step=\x1b[35m%ld\x1b[m)" + : "Range(first=%ld, last=%ld, step=%ld)", r->first, r->last, r->step); +} + +public Range_t Range$reversed(Range_t r) +{ + return (Range_t){r.last, r.first, -r.step}; +} + +public Range_t Range$by(Range_t r, int64_t step) +{ + return (Range_t){r.first, r.last, step*r.step}; +} + +public const TypeInfo Range = {sizeof(Range_t), __alignof(Range_t), {.tag=CustomInfo, .CustomInfo={ + .as_text=(void*)Range$as_text, + .compare=(void*)Range$compare, + .equal=(void*)Range$equal, +}}}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/range.h b/builtins/range.h new file mode 100644 index 00000000..9f3b25af --- /dev/null +++ b/builtins/range.h @@ -0,0 +1,10 @@ +#pragma once + +// Ranges represent numeric ranges + +Range_t Range$reversed(Range_t r); +Range_t Range$by(Range_t r, int64_t step); + +extern const TypeInfo Range; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 diff --git a/builtins/tomo.h b/builtins/tomo.h index 482c8d2d..595d6310 100644 --- a/builtins/tomo.h +++ b/builtins/tomo.h @@ -20,6 +20,7 @@ #include "memory.h" #include "nums.h" #include "pointer.h" +#include "range.h" #include "table.h" #include "text.h" #include "types.h" |
