diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-04-10 13:23:49 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-04-10 13:23:49 -0400 |
| commit | e6f78f1d89acb038cf7893c325ee9acf61a555ad (patch) | |
| tree | 77995a17758c8dc6599033ba4f7dccfb8fc42244 | |
| parent | ccb9e367047148f5b15316b667a139106858ab61 (diff) | |
Add from_text() method for ints/nums
| -rw-r--r-- | builtins/integers.c | 18 | ||||
| -rw-r--r-- | builtins/integers.h | 1 | ||||
| -rw-r--r-- | builtins/nums.c | 16 | ||||
| -rw-r--r-- | builtins/nums.h | 2 | ||||
| -rw-r--r-- | environment.c | 6 |
5 files changed, 43 insertions, 0 deletions
diff --git a/builtins/integers.c b/builtins/integers.c index 86c1d5e6..ac8e5069 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -60,6 +60,24 @@ uint32_t r = arc4random_uniform((uint32_t)range); \ return min + (c_type)r; \ } \ + public c_type KindOfInt ## $from_text(CORD text, CORD *the_rest) { \ + const char *str = CORD_to_const_char_star(text); \ + long i; \ + char *end_ptr = NULL; \ + if (strncmp(str, "0x", 2) == 0) { \ + i = strtol(str, &end_ptr, 16); \ + } else if (strncmp(str, "0o", 2) == 0) { \ + i = strtol(str, &end_ptr, 8); \ + } else if (strncmp(str, "0b", 2) == 0) { \ + i = strtol(str, &end_ptr, 2); \ + } else { \ + i = strtol(str, &end_ptr, 10); \ + } \ + if (the_rest) *the_rest = CORD_from_char_star(end_ptr); \ + if (i < min_val) i = min_val; \ + else if (i > max_val) i = min_val; \ + return (c_type)i; \ + } \ public const c_type KindOfInt##$min = min_val; \ public const c_type KindOfInt##$max = max_val; \ public const TypeInfo $ ## KindOfInt = { \ diff --git a/builtins/integers.h b/builtins/integers.h index 8ee78789..707bc5c8 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -27,6 +27,7 @@ CORD type_name ## $octal(c_type i, int64_t digits, bool prefix); \ array_t type_name ## $bits(c_type x); \ c_type type_name ## $random(int64_t min, int64_t max); \ + c_type type_name ## $from_text(CORD text, CORD *the_rest); \ extern const c_type type_name ## $min, type_name##$max; \ extern const TypeInfo $ ## type_name; diff --git a/builtins/nums.c b/builtins/nums.c index 0bb604eb..0ad2bdc2 100644 --- a/builtins/nums.c +++ b/builtins/nums.c @@ -64,6 +64,14 @@ public double Num$random(void) { return drand48(); } +public double Num$from_text(CORD text, CORD *the_rest) { + const char *str = CORD_to_const_char_star(text); + char *end = NULL; + double d = strtod(str, &end); + if (the_rest) *the_rest = CORD_from_char_star(end); + return d; +} + public double Num$nan(CORD tag) { return nan(CORD_to_const_char_star(tag)); } @@ -134,6 +142,14 @@ public float Num32$random(void) { return (float)drand48(); } +public float Num32$from_text(CORD text, CORD *the_rest) { + const char *str = CORD_to_const_char_star(text); + char *end = NULL; + double d = strtod(str, &end); + if (the_rest) *the_rest = CORD_from_char_star(end); + return (float)d; +} + public float Num32$nan(CORD tag) { return nanf(CORD_to_const_char_star(tag)); } diff --git a/builtins/nums.h b/builtins/nums.h index 0d9f2e99..3b4386e0 100644 --- a/builtins/nums.h +++ b/builtins/nums.h @@ -24,6 +24,7 @@ bool Num$finite(double n); bool Num$isnan(double n); double Num$nan(CORD tag); double Num$random(void); +double Num$from_text(CORD text, CORD *the_rest); extern const TypeInfo $Num; CORD Num32$as_text(const float *f, bool colorize, const TypeInfo *type); @@ -37,6 +38,7 @@ bool Num32$isinf(float n); bool Num32$finite(float n); bool Num32$isnan(float n); float Num32$random(void); +float Num32$from_text(CORD text, CORD *the_rest); float Num32$nan(CORD tag); extern const TypeInfo $Num32; diff --git a/environment.c b/environment.c index 6de8fb21..e86de364 100644 --- a/environment.c +++ b/environment.c @@ -55,6 +55,7 @@ env_t *new_compilation_unit(void) {"hex", "Int$hex", "func(i:Int, digits=0, uppercase=yes, prefix=yes)->Text"}, {"octal", "Int$octal", "func(i:Int, digits=0, prefix=yes)->Text"}, {"random", "Int$random", "func(min=0, max=0xffffffff)->Int"}, + {"from_text", "Int$from_text", "func(text:Text, the_rest=!Text)->Int"}, {"bits", "Int$bits", "func(x:Int)->[Bool]"}, {"abs", "labs", "func(i:Int)->Int"}, {"min", "Int$min", "Int"}, @@ -65,6 +66,7 @@ env_t *new_compilation_unit(void) {"hex", "Int32$hex", "func(i:Int32, digits=0, uppercase=yes, prefix=yes)->Text"}, {"octal", "Int32$octal", "func(i:Int32, digits=0, prefix=yes)->Text"}, {"random", "Int32$random", "func(min=0, max=0xffffffff)->Int32"}, + {"from_text", "Int$from_text", "func(text:Text, the_rest=!Text)->Int32"}, {"bits", "Int32$bits", "func(x:Int32)->[Bool]"}, {"abs", "abs", "func(i:Int32)->Int32"}, {"min", "Int32$min", "Int32"}, @@ -75,6 +77,7 @@ env_t *new_compilation_unit(void) {"hex", "Int16$hex", "func(i:Int16, digits=0, uppercase=yes, prefix=yes)->Text"}, {"octal", "Int16$octal", "func(i:Int16, digits=0, prefix=yes)->Text"}, {"random", "Int16$random", "func(min=0, max=0xffffffff)->Int16"}, + {"from_text", "Int$from_text", "func(text:Text, the_rest=!Text)->Int16"}, {"bits", "Int16$bits", "func(x:Int16)->[Bool]"}, {"abs", "abs", "func(i:Int16)->Int16"}, {"min", "Int16$min", "Int16"}, @@ -85,6 +88,7 @@ env_t *new_compilation_unit(void) {"hex", "Int8$hex", "func(i:Int8, digits=0, uppercase=yes, prefix=yes)->Text"}, {"octal", "Int8$octal", "func(i:Int8, digits=0, prefix=yes)->Text"}, {"random", "Int8$random", "func(min=0, max=0xffffffff)->Int8"}, + {"from_text", "Int$from_text", "func(text:Text, the_rest=!Text)->Int8"}, {"bits", "Int8$bits", "func(x:Int8)->[Bool]"}, {"abs", "abs", "func(i:Int8)->Int8"}, {"min", "Int8$min", "Int8"}, @@ -106,6 +110,7 @@ env_t *new_compilation_unit(void) {"INF", "INFINITY", "Num"}, {"TAU", "(2.*M_PI)", "Num"}, {"random", "Num$random", "func()->Num"}, + {"from_text", "Num$from_text", "func(text:Text, the_rest=!Text)->Num"}, {"abs", "fabs", "func(n:Num)->Num"}, F(acos), F(acosh), F(asin), F(asinh), F(atan), F(atanh), F(cbrt), F(ceil), F(cos), F(cosh), F(erf), F(erfc), F(exp), F(exp2), F(expm1), F(floor), F(j0), F(j1), F(log), F(log10), F(log1p), F(log2), F(logb), @@ -132,6 +137,7 @@ env_t *new_compilation_unit(void) {"INF", "(Num32_t)(INFINITY)", "Num32"}, {"TAU", "(Num32_t)(2.f*M_PI)", "Num32"}, {"random", "Num32$random", "func()->Num32"}, + {"from_text", "Num32$from_text", "func(text:Text, the_rest=!Text)->Num32"}, {"abs", "fabsf", "func(n:Num32)->Num32"}, F(acos), F(acosh), F(asin), F(asinh), F(atan), F(atanh), F(cbrt), F(ceil), F(cos), F(cosh), F(erf), F(erfc), F(exp), F(exp2), F(expm1), F(floor), F(j0), F(j1), F(log), F(log10), F(log1p), F(log2), F(logb), |
