diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-13 20:08:20 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-13 20:08:20 -0400 |
| commit | 4380039acc881703ef9d144bbf39d82da4beb936 (patch) | |
| tree | 111eda9fedaa13f593cdf47f75277d740207c637 /builtins/bools.c | |
| parent | 51c346bbc5f6c5179b56b09b75eec466acbe7ad7 (diff) | |
Rename builtins to use plurals when appropriate
Diffstat (limited to 'builtins/bools.c')
| -rw-r--r-- | builtins/bools.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/builtins/bools.c b/builtins/bools.c new file mode 100644 index 00000000..d7b3718f --- /dev/null +++ b/builtins/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 |
