aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
Diffstat (limited to 'builtins')
-rw-r--r--builtins/bool.c8
-rw-r--r--builtins/bool.h3
-rw-r--r--builtins/integers.c26
-rw-r--r--builtins/integers.h8
-rw-r--r--builtins/nums.c16
-rw-r--r--builtins/nums.h6
6 files changed, 36 insertions, 31 deletions
diff --git a/builtins/bool.c b/builtins/bool.c
index 63eb73f9..ce27ce79 100644
--- a/builtins/bool.c
+++ b/builtins/bool.c
@@ -9,6 +9,7 @@
#include <sys/param.h>
#include "bool.h"
+#include "optionals.h"
#include "text.h"
#include "types.h"
#include "util.h"
@@ -23,23 +24,20 @@ PUREFUNC public Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo
return *b ? Text("yes") : Text("no");
}
-public Bool_t Bool$from_text(Text_t text, bool *success)
+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"))) {
- if (success) *success = yes;
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"))) {
- if (success) *success = yes;
return no;
} else {
- if (success) *success = no;
- return no;
+ return NULL_BOOL;
}
}
diff --git a/builtins/bool.h b/builtins/bool.h
index 5f222c49..d6e5307a 100644
--- a/builtins/bool.h
+++ b/builtins/bool.h
@@ -7,6 +7,7 @@
#include <stdint.h>
#include "types.h"
+#include "optionals.h"
#include "util.h"
#define Bool_t bool
@@ -14,7 +15,7 @@
#define no (Bool_t)false
PUREFUNC Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo *type);
-bool Bool$from_text(Text_t text, bool *success);
+OptionalBool_t Bool$from_text(Text_t text);
Bool_t Bool$random(double p);
extern const TypeInfo Bool$info;
diff --git a/builtins/integers.c b/builtins/integers.c
index 31e32f3d..9160bfca 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -10,6 +10,7 @@
#include "array.h"
#include "datatypes.h"
#include "integers.h"
+#include "optionals.h"
#include "siphash.h"
#include "text.h"
#include "types.h"
@@ -343,7 +344,7 @@ public PUREFUNC Range_t Int$to(Int_t from, Int_t to) {
return (Range_t){from, to, Int$compare_value(to, from) >= 0 ? (Int_t){.small=(1<<2)|1} : (Int_t){.small=(-1>>2)|1}};
}
-public Int_t Int$from_str(const char *str, bool *success) {
+public Int_t Int$from_str(const char *str) {
mpz_t i;
int result;
if (strncmp(str, "0x", 2) == 0) {
@@ -355,12 +356,13 @@ public Int_t Int$from_str(const char *str, bool *success) {
} else {
result = mpz_init_set_str(i, str, 10);
}
- if (success) *success = (result == 0);
+ if (result != 0)
+ return NULL_INT;
return Int$from_mpz(i);
}
-public Int_t Int$from_text(Text_t text, bool *success) {
- return Int$from_str(Text$as_c_string(text), success);
+public OptionalInt_t Int$from_text(Text_t text) {
+ return Int$from_str(Text$as_c_string(text));
}
public bool Int$is_prime(Int_t x, Int_t reps)
@@ -458,20 +460,16 @@ public const TypeInfo Int$info = {
public Range_t KindOfInt ## $to(c_type from, c_type to) { \
return (Range_t){Int64_to_Int(from), Int64_to_Int(to), to >= from ? (Int_t){.small=(1<<2)&1} : (Int_t){.small=(1<<2)&1}}; \
} \
- public PUREFUNC c_type KindOfInt ## $from_text(Text_t text, bool *success) { \
- bool parsed_int = false; \
- Int_t full_int = Int$from_text(text, &parsed_int); \
- if (!parsed_int && success) *success = false; \
+ public PUREFUNC Optional ## KindOfInt ## _t KindOfInt ## $from_text(Text_t text) { \
+ OptionalInt_t full_int = Int$from_text(text); \
+ if (full_int.small == 0) return (Optional ## KindOfInt ## _t){.is_null=true}; \
if (Int$compare_value(full_int, I(min_val)) < 0) { \
- if (success) *success = false; \
- return min_val; \
+ return (Optional ## KindOfInt ## _t){.is_null=true}; \
} \
if (Int$compare_value(full_int, I(max_val)) > 0) { \
- if (success) *success = false; \
- return max_val; \
+ return (Optional ## KindOfInt ## _t){.is_null=true}; \
} \
- if (success && parsed_int) *success = true; \
- return Int_to_ ## KindOfInt(full_int, true); \
+ return (Optional ## KindOfInt ## _t){.i=Int_to_ ## KindOfInt(full_int, true)}; \
} \
public const c_type KindOfInt##$min = min_val; \
public const c_type KindOfInt##$max = max_val; \
diff --git a/builtins/integers.h b/builtins/integers.h
index 22dc4f7c..4f3ebe51 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -37,7 +37,7 @@
Array_t type_name ## $bits(c_type x); \
c_type type_name ## $random(c_type min, c_type max); \
Range_t type_name ## $to(c_type from, c_type to); \
- PUREFUNC c_type type_name ## $from_text(Text_t text, bool *success); \
+ PUREFUNC Optional ## type_name ## _t type_name ## $from_text(Text_t text); \
PUREFUNC static inline c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \
return x < min ? min : (x > max ? max : x); \
} \
@@ -79,6 +79,8 @@ DEFINE_INT_TYPE(int8_t, Int8, 8)
#define Int16$abs(...) I16(abs(__VA_ARGS__))
#define Int8$abs(...) I8(abs(__VA_ARGS__))
+#define OptionalInt_t Int_t
+
Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type);
PUREFUNC uint64_t Int$hash(const Int_t *x, const TypeInfo *type);
PUREFUNC int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type);
@@ -91,8 +93,8 @@ Text_t Int$octal(Int_t i, Int_t digits, bool prefix);
void Int$init_random(long seed);
Int_t Int$random(Int_t min, Int_t max);
PUREFUNC Range_t Int$to(Int_t from, Int_t to);
-Int_t Int$from_str(const char *str, bool *success);
-Int_t Int$from_text(Text_t text, bool *success);
+OptionalInt_t Int$from_str(const char *str);
+OptionalInt_t Int$from_text(Text_t text);
Int_t Int$abs(Int_t x);
Int_t Int$power(Int_t base, Int_t exponent);
Int_t Int$sqrt(Int_t i);
diff --git a/builtins/nums.c b/builtins/nums.c
index 90eb75ce..cfa14191 100644
--- a/builtins/nums.c
+++ b/builtins/nums.c
@@ -66,12 +66,14 @@ public CONSTFUNC double Num$mix(double amount, double x, double y) {
return (1.0-amount)*x + amount*y;
}
-public double Num$from_text(Text_t text, bool *success) {
+public OptionalNum_t Num$from_text(Text_t text) {
const char *str = Text$as_c_string(text);
char *end = NULL;
double d = strtod(str, &end);
- if (success) *success = (end > str && end[0] == '\0');
- return d;
+ if (end > str && end[0] == '\0')
+ return d;
+ else
+ return nan("null");
}
public double Num$nan(Text_t tag) {
@@ -145,12 +147,14 @@ public CONSTFUNC float Num32$mix(float amount, float x, float y) {
return (1.0f-amount)*x + amount*y;
}
-public float Num32$from_text(Text_t text, bool *success) {
+public OptionalNum32_t Num32$from_text(Text_t text) {
const char *str = Text$as_c_string(text);
char *end = NULL;
double d = strtod(str, &end);
- if (success) *success = (end > str && end[0] == '\0');
- return (float)d;
+ if (end > str && end[0] == '\0')
+ return d;
+ else
+ return nan("null");
}
public float Num32$nan(Text_t tag) {
diff --git a/builtins/nums.h b/builtins/nums.h
index 2ac942f4..51f65c57 100644
--- a/builtins/nums.h
+++ b/builtins/nums.h
@@ -12,6 +12,8 @@
#define Num_t double
#define Num32_t float
+#define OptionalNum_t double
+#define OptionalNum32_t float
#define N32(n) ((float)n)
#define N64(n) ((double)n)
@@ -28,7 +30,7 @@ CONSTFUNC bool Num$isnan(double n);
double Num$nan(Text_t tag);
double Num$random(void);
CONSTFUNC double Num$mix(double amount, double x, double y);
-double Num$from_text(Text_t text, bool *success);
+OptionalNum_t Num$from_text(Text_t text);
CONSTFUNC static inline double Num$clamped(double x, double low, double high) {
return (x <= low) ? low : (x >= high ? high : x);
}
@@ -46,7 +48,7 @@ CONSTFUNC bool Num32$finite(float n);
CONSTFUNC bool Num32$isnan(float n);
float Num32$random(void);
CONSTFUNC float Num32$mix(float amount, float x, float y);
-float Num32$from_text(Text_t text, bool *success);
+OptionalNum32_t Num32$from_text(Text_t text);
float Num32$nan(Text_t tag);
CONSTFUNC static inline float Num32$clamped(float x, float low, float high) {
return (x <= low) ? low : (x >= high ? high : x);