diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-13 20:18:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-13 20:18:08 -0400 |
| commit | c455e7b67d2e55e6ed03e3449203d4e307f5a7dd (patch) | |
| tree | 27d9d4c77193f7aa1fe3a3c6fe5631d0ccfd59e2 /stdlib/bools.c | |
| parent | 816aa29b799132acb8c71d4968df6c4619fb2b1d (diff) | |
Rename builtins/ -> stdlib/
Diffstat (limited to 'stdlib/bools.c')
| -rw-r--r-- | stdlib/bools.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/stdlib/bools.c b/stdlib/bools.c new file mode 100644 index 00000000..d7b3718f --- /dev/null +++ b/stdlib/bools.c @@ -0,0 +1,54 @@ +// Boolean methods/type info +#include <ctype.h> +#include <err.h> +#include <gc.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/param.h> + +#include "bools.h" +#include "optionals.h" +#include "text.h" +#include "util.h" + +PUREFUNC public Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) +{ + (void)type; + if (!b) return Text("Bool"); + if (colorize) + return *b ? Text("\x1b[35myes\x1b[m") : Text("\x1b[35mno\x1b[m"); + else + return *b ? Text("yes") : Text("no"); +} + +PUREFUNC public OptionalBool_t Bool$from_text(Text_t text) +{ + if (Text$equal_ignoring_case(text, Text("yes")) + || Text$equal_ignoring_case(text, Text("on")) + || Text$equal_ignoring_case(text, Text("true")) + || Text$equal_ignoring_case(text, Text("1"))) { + return yes; + } else if (Text$equal_ignoring_case(text, Text("no")) + || Text$equal_ignoring_case(text, Text("off")) + || Text$equal_ignoring_case(text, Text("false")) + || Text$equal_ignoring_case(text, Text("0"))) { + return no; + } else { + return NULL_BOOL; + } +} + +public Bool_t Bool$random(double p) +{ + return (drand48() < p); +} + +public const TypeInfo Bool$info = { + .size=sizeof(bool), + .align=__alignof__(bool), + .tag=CustomInfo, + .CustomInfo={.as_text=(void*)Bool$as_text}, +}; + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
