diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-04-10 13:33:40 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-04-10 13:33:40 -0400 |
| commit | 17cb6ffd88c4464c513b045f4b06c4e6e46e8f22 (patch) | |
| tree | 115c43f519f598f98d635502a6660c2a7df14c6f | |
| parent | e6f78f1d89acb038cf7893c325ee9acf61a555ad (diff) | |
Add Bool.from_text()
| -rw-r--r-- | builtins/bool.c | 22 | ||||
| -rw-r--r-- | builtins/bool.h | 1 | ||||
| -rw-r--r-- | environment.c | 4 |
3 files changed, 23 insertions, 4 deletions
diff --git a/builtins/bool.c b/builtins/bool.c index cced8077..ff5d0ec4 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -1,16 +1,17 @@ // Boolean methods/type info +#include <ctype.h> +#include <err.h> #include <gc.h> #include <gc/cord.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> -#include <ctype.h> #include <sys/param.h> -#include <err.h> -#include "util.h" #include "bool.h" +#include "text.h" #include "types.h" +#include "util.h" public CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) { @@ -22,6 +23,21 @@ public CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type) return *b ? "yes" : "no"; } +public Bool_t Bool$from_text(CORD text, bool *success) +{ + CORD lower = Text$lower(text); + if (CORD_cmp(lower, "yes") == 0 || CORD_cmp(lower, "on") == 0 || CORD_cmp(lower, "true") == 0) { + if (success) *success = yes; + return yes; + } else if (CORD_cmp(lower, "no") == 0 || CORD_cmp(lower, "off") == 0 || CORD_cmp(lower, "false") == 0) { + if (success) *success = yes; + return no; + } else { + if (success) *success = no; + return no; + } +} + public const TypeInfo $Bool = { .size=sizeof(bool), .align=__alignof__(bool), diff --git a/builtins/bool.h b/builtins/bool.h index d5f4b705..356889d1 100644 --- a/builtins/bool.h +++ b/builtins/bool.h @@ -13,6 +13,7 @@ #define no (Bool_t)false CORD Bool$as_text(const bool *b, bool colorize, const TypeInfo *type); +bool Bool$from_text(CORD text, bool *success); extern const TypeInfo $Bool; diff --git a/environment.c b/environment.c index e86de364..81824710 100644 --- a/environment.c +++ b/environment.c @@ -49,7 +49,9 @@ env_t *new_compilation_unit(void) CORD struct_val; array_t namespace; } global_types[] = { - {"Bool", Type(BoolType), "Bool_t", "$Bool", {}}, + {"Bool", Type(BoolType), "Bool_t", "$Bool", $TypedArray(ns_entry_t, + {"from_text", "Bool$from_text", "func(text:Text, success=!Bool)->Bool"}, + )}, {"Int", Type(IntType, .bits=64), "Int_t", "$Int", $TypedArray(ns_entry_t, {"format", "Int$format", "func(i:Int, digits=0)->Text"}, {"hex", "Int$hex", "func(i:Int, digits=0, uppercase=yes, prefix=yes)->Text"}, |
