aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-11 12:56:16 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-11 12:56:16 -0400
commit02930b84f821b22cfd629b64169dc0b5a4b8fe74 (patch)
treec3b68dd707ff88baae6304366fec75bdd5e19cc0
parent908673c9d95a57e794dc1ee5708ffb511958abb9 (diff)
Optionals for threads
-rw-r--r--builtins/optionals.c3
-rw-r--r--compile.c4
-rw-r--r--test/optionals.tm19
3 files changed, 25 insertions, 1 deletions
diff --git a/builtins/optionals.c b/builtins/optionals.c
index 5a524f35..b38883fb 100644
--- a/builtins/optionals.c
+++ b/builtins/optionals.c
@@ -3,6 +3,7 @@
#include "bool.h"
#include "datatypes.h"
#include "integers.h"
+#include "thread.h"
#include "text.h"
#include "util.h"
@@ -29,6 +30,8 @@ static inline bool is_null(const void *obj, const TypeInfo *non_optional_type)
return ((OptionalInt16_t*)obj)->is_null;
else if (non_optional_type == &Int8$info)
return ((OptionalInt8_t*)obj)->is_null;
+ else if (non_optional_type == &Thread)
+ return *(pthread_t**)obj == NULL;
switch (non_optional_type->tag) {
case ChannelInfo: return *(channel_t**)obj == NULL;
diff --git a/compile.c b/compile.c
index 6688a5ed..85321000 100644
--- a/compile.c
+++ b/compile.c
@@ -27,7 +27,9 @@ static CORD promote_to_optional(type_t *t, CORD code);
CORD promote_to_optional(type_t *t, CORD code)
{
- if (t->tag == IntType) {
+ if (t == THREAD_TYPE) {
+ return code;
+ } else if (t->tag == IntType) {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS8: return CORD_all("((OptionalInt8_t){.i=", code, "})");
case TYPE_IBITS16: return CORD_all("((OptionalInt16_t){.i=", code, "})");
diff --git a/test/optionals.tm b/test/optionals.tm
index 5d54b29e..d7ce99c0 100644
--- a/test/optionals.tm
+++ b/test/optionals.tm
@@ -61,6 +61,12 @@ func maybe_channel(should_i:Bool)->|Int|?:
else:
return !|Int|
+func maybe_thread(should_i:Bool)->Thread?:
+ if should_i:
+ return Thread.new(func(): pass)
+ else:
+ return !Thread
+
func main():
>> 5?
= 5? : Int?
@@ -200,6 +206,19 @@ func main():
fail("Truthy: $nope")
else: !! Falsey: $nope
+ do:
+ !! ...
+ !! Threads:
+ >> yep := maybe_thread(yes)
+ # No "=" test here because threads use addresses in the text version
+ >> nope := maybe_thread(no)
+ = !Thread
+ >> if yep: >> yep
+ else: fail("Falsey: $yep")
+ >> if nope:
+ fail("Truthy: $nope")
+ else: !! Falsey: $nope
+
if yep := maybe_int(yes):
>> yep
= 123 : Int