aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-17 19:52:37 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-17 19:52:37 -0500
commitdd2de3166653c1bf0d33d5872791ae6fbe8e2998 (patch)
treee7a8818fa1e7663dd8b921440a32e8ed1e6148df /builtins
parenta09a8339e78923688440e6b303e49eb7c6a02b29 (diff)
Fix up USE_COLOR
Diffstat (limited to 'builtins')
-rw-r--r--builtins/array.c4
-rw-r--r--builtins/color.c17
-rw-r--r--builtins/color.h9
-rw-r--r--builtins/functions.c6
-rw-r--r--builtins/functions.h3
-rw-r--r--builtins/integers.c9
-rw-r--r--builtins/integers.h19
-rw-r--r--builtins/nums.h4
-rw-r--r--builtins/pointer.c2
-rw-r--r--builtins/string.c2
-rw-r--r--builtins/table.c3
-rw-r--r--builtins/types.c2
12 files changed, 47 insertions, 33 deletions
diff --git a/builtins/array.c b/builtins/array.c
index 447650fc..bf1b9309 100644
--- a/builtins/array.c
+++ b/builtins/array.c
@@ -15,8 +15,6 @@
#include "../SipHash/halfsiphash.h"
#include "../util.h"
-extern const void *SSS_HASH_VECTOR;
-
static inline size_t get_item_size(const TypeInfo *info)
{
return info->ArrayInfo.item->size;
@@ -278,7 +276,7 @@ public bool Array__equal(const array_t *x, const array_t *y, const TypeInfo *typ
return (Array__compare(x, y, type) == 0);
}
-public CORD Array__cord(const array_t *arr, bool colorize, const TypeInfo *type)
+public CORD Array__as_str(const array_t *arr, bool colorize, const TypeInfo *type)
{
if (!arr)
return CORD_all("[", generic_as_str(NULL, false, type->ArrayInfo.item), "]");
diff --git a/builtins/color.c b/builtins/color.c
new file mode 100644
index 00000000..b84d8a71
--- /dev/null
+++ b/builtins/color.c
@@ -0,0 +1,17 @@
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "../util.h"
+#include "color.h"
+
+public bool USE_COLOR = true;
+
+public void detect_color(void)
+{
+ USE_COLOR = getenv("COLOR") ? strcmp(getenv("COLOR"), "1") == 0 : isatty(STDOUT_FILENO);
+}
+
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/builtins/color.h b/builtins/color.h
new file mode 100644
index 00000000..d9e5c9f1
--- /dev/null
+++ b/builtins/color.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include <stdbool.h>
+
+extern bool USE_COLOR;
+
+void detect_color(void);
+
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/builtins/functions.c b/builtins/functions.c
index f2b2fa82..03dfe738 100644
--- a/builtins/functions.c
+++ b/builtins/functions.c
@@ -90,7 +90,7 @@ public CORD generic_as_str(const void *obj, bool colorize, const TypeInfo *type)
case TypeInfoInfo: return Type__as_str(obj, colorize, type);
case CustomInfo:
if (!type->CustomInfo.as_str)
- builtin_fail("No cord function provided for type!\n");
+ fail("No cord function provided for type!\n");
return type->CustomInfo.as_str(obj, colorize, type);
default: errx(1, "Invalid type tag: %d", type->tag);
}
@@ -146,8 +146,8 @@ public void __doctest(CORD label, void *expr, TypeInfo *type, CORD expected, con
if (!success) {
if (filename && file)
fprint_span(stderr, file, file->text+start, file->text+end, "\x1b[31;1m", 2, USE_COLOR);
- builtin_fail(USE_COLOR ? "\x1b[31;1mExpected: \x1b[32;7m%s\x1b[0m\n\x1b[31;1m But got: \x1b[31;7m%s\x1b[0m\n" : "Expected: %s\n But got: %s\n",
- expected, expr_str);
+ fail(USE_COLOR ? "\x1b[31;1mExpected: \x1b[32;7m%s\x1b[0m\n\x1b[31;1m But got: \x1b[31;7m%s\x1b[0m\n" : "Expected: %s\n But got: %s\n",
+ expected, expr_str);
}
}
}
diff --git a/builtins/functions.h b/builtins/functions.h
index 0f4beacf..133c76fc 100644
--- a/builtins/functions.h
+++ b/builtins/functions.h
@@ -8,8 +8,7 @@
extern const char *SSS_HASH_VECTOR;
-void builtin_say(CORD str, CORD end);
-void builtin_fail(CORD fmt, ...);
+void fail(CORD fmt, ...);
CORD builtin_last_err();
void __doctest(CORD label, void *expr, TypeInfo *type, CORD expected, const char *filename, int start, int end);
diff --git a/builtins/integers.c b/builtins/integers.c
index 36e7d7a8..b4170f7b 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -39,11 +39,11 @@
return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)i); \
} \
public c_type KindOfInt ## __random(int64_t min, int64_t max) { \
- if (min > max) builtin_fail("Random min (%ld) is larger than max (%ld)", min, max); \
- if (min < (int64_t)min_val) builtin_fail("Random min (%ld) is smaller than the minimum "#KindOfInt" value", min); \
- if (max > (int64_t)max_val) builtin_fail("Random max (%ld) is smaller than the maximum "#KindOfInt" value", max); \
+ if (min > max) fail("Random min (%ld) is larger than max (%ld)", min, max); \
+ if (min < (int64_t)min_val) fail("Random min (%ld) is smaller than the minimum "#KindOfInt" value", min); \
+ if (max > (int64_t)max_val) fail("Random max (%ld) is smaller than the maximum "#KindOfInt" value", max); \
int64_t range = max - min; \
- if (range > UINT32_MAX) builtin_fail("Random range (%ld) is larger than the maximum allowed (%ld)", range, UINT32_MAX); \
+ if (range > UINT32_MAX) fail("Random range (%ld) is larger than the maximum allowed (%ld)", range, UINT32_MAX); \
uint32_t r = arc4random_uniform((uint32_t)range); \
return min + (c_type)r; \
} \
@@ -63,6 +63,7 @@
.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);
diff --git a/builtins/integers.h b/builtins/integers.h
index 3e00d962..5eec40a8 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -5,13 +5,13 @@
#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); \
+#define DEFINE_INT_TYPE(c_type, type_name) \
+ CORD type_name ## __as_str(const c_type *i, bool colorize, const TypeInfo *type); \
+ int32_t type_name ## __compare(const c_type *x, const c_type *y, const TypeInfo *type); \
+ CORD type_name ## __format(c_type i, int64_t digits); \
+ CORD type_name ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix); \
+ CORD type_name ## __octal(c_type i, int64_t digits, bool prefix); \
+ c_type type_name ## __random(int64_t min, int64_t max); \
typedef struct { \
TypeInfo type; \
c_type min, max; \
@@ -20,7 +20,10 @@
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;
+ } type_name##_namespace_t; \
+ extern type_name##_namespace_t type_name##_type;
+
+DEFINE_INT_TYPE(int64_t, Int);
DEFINE_INT_TYPE(int64_t, Int64);
DEFINE_INT_TYPE(int32_t, Int32);
DEFINE_INT_TYPE(int16_t, Int16);
diff --git a/builtins/nums.h b/builtins/nums.h
index 5e2cb062..1f755047 100644
--- a/builtins/nums.h
+++ b/builtins/nums.h
@@ -5,10 +5,6 @@
#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);
diff --git a/builtins/pointer.c b/builtins/pointer.c
index f978acab..bc358488 100644
--- a/builtins/pointer.c
+++ b/builtins/pointer.c
@@ -14,8 +14,6 @@
#include "functions.h"
#include "types.h"
-extern const void *SSS_HASH_VECTOR;
-
typedef struct recursion_s {
const void *ptr;
struct recursion_s *next;
diff --git a/builtins/string.c b/builtins/string.c
index dd56893a..6f692b64 100644
--- a/builtins/string.c
+++ b/builtins/string.c
@@ -19,8 +19,6 @@
#define CLAMP(x, lo, hi) MIN(hi, MAX(x,lo))
-extern const void *SSS_HASH_VECTOR;
-
public CORD Str__as_str(const void *str, bool colorize, const TypeInfo *info)
{
(void)info;
diff --git a/builtins/table.c b/builtins/table.c
index 6db29186..0b377bb1 100644
--- a/builtins/table.c
+++ b/builtins/table.c
@@ -11,7 +11,6 @@
#include <assert.h>
#include <gc.h>
-#include <stdalign.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -42,8 +41,6 @@
#define GET_ENTRY(t, i) ((t)->entries.data + (t)->entries.stride*(i))
#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;
-
TypeInfo MemoryPointer_typeinfo = {
.size=sizeof(void*),
.align=alignof(void*),
diff --git a/builtins/types.c b/builtins/types.c
index f77a36f9..d8b90021 100644
--- a/builtins/types.c
+++ b/builtins/types.c
@@ -13,8 +13,6 @@
#include "../util.h"
#include "../SipHash/halfsiphash.h"
-extern const void *SSS_HASH_VECTOR;
-
public CORD Type__as_str(const void *typeinfo, bool colorize, const TypeInfo *type)
{
if (!typeinfo) return "TypeInfo";