aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-11 11:08:15 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-11 11:08:15 -0400
commitbba9f1b141fba5ffc366ef9377fb3089c0c5a1a3 (patch)
tree371100a85fa638102bbf331811ef53b8fcb67159 /builtins
parent0ca48c98e897c6edd800c0aefabd20cc4e7a5409 (diff)
Optional fixed-size ints
Diffstat (limited to 'builtins')
-rw-r--r--builtins/integers.h19
-rw-r--r--builtins/optionals.c9
2 files changed, 23 insertions, 5 deletions
diff --git a/builtins/integers.h b/builtins/integers.h
index e69914c8..22dc4f7c 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -23,7 +23,11 @@
#define I16(x) ((int16_t)x)
#define I8(x) ((int8_t)x)
-#define DEFINE_INT_TYPE(c_type, type_name) \
+#define DEFINE_INT_TYPE(c_type, type_name, bits) \
+ typedef struct { \
+ c_type i:bits; \
+ bool is_null:1; \
+ } Optional ## type_name ## _t; \
Text_t type_name ## $as_text(const c_type *i, bool colorize, const TypeInfo *type); \
PUREFUNC int32_t type_name ## $compare(const c_type *x, const c_type *y, const TypeInfo *type); \
PUREFUNC bool type_name ## $equal(const c_type *x, const c_type *y, const TypeInfo *type); \
@@ -59,12 +63,17 @@
return type_name ## $modulo(D-1, d) + 1; \
}
-DEFINE_INT_TYPE(int64_t, Int64)
-DEFINE_INT_TYPE(int32_t, Int32)
-DEFINE_INT_TYPE(int16_t, Int16)
-DEFINE_INT_TYPE(int8_t, Int8)
+DEFINE_INT_TYPE(int64_t, Int64, 64)
+DEFINE_INT_TYPE(int32_t, Int32, 32)
+DEFINE_INT_TYPE(int16_t, Int16, 16)
+DEFINE_INT_TYPE(int8_t, Int8, 8)
#undef DEFINE_INT_TYPE
+#define NULL_INT64 ((OptionalInt64_t){.is_null=true})
+#define NULL_INT32 ((OptionalInt32_t){.is_null=true})
+#define NULL_INT16 ((OptionalInt16_t){.is_null=true})
+#define NULL_INT8 ((OptionalInt8_t){.is_null=true})
+
#define Int64$abs(...) I64(labs(__VA_ARGS__))
#define Int32$abs(...) I32(abs(__VA_ARGS__))
#define Int16$abs(...) I16(abs(__VA_ARGS__))
diff --git a/builtins/optionals.c b/builtins/optionals.c
index 869ee4cd..27ab8ec6 100644
--- a/builtins/optionals.c
+++ b/builtins/optionals.c
@@ -2,6 +2,7 @@
#include "bool.h"
#include "datatypes.h"
+#include "integers.h"
#include "text.h"
#include "util.h"
@@ -20,6 +21,14 @@ static inline bool is_null(const void *obj, const TypeInfo *non_optional_type)
return *((Bool_t*)obj) == NULL_BOOL;
else if (non_optional_type == &Num$info)
return isnan(*((Num_t*)obj));
+ else if (non_optional_type == &Int64$info)
+ return ((OptionalInt64_t*)obj)->is_null;
+ else if (non_optional_type == &Int32$info)
+ return ((OptionalInt32_t*)obj)->is_null;
+ else if (non_optional_type == &Int16$info)
+ return ((OptionalInt16_t*)obj)->is_null;
+ else if (non_optional_type == &Int8$info)
+ return ((OptionalInt8_t*)obj)->is_null;
switch (non_optional_type->tag) {
case PointerInfo: return *(void**)obj == NULL;