From ecc06c082036aa93f6810ec21e73610c55f5a57b Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 30 Dec 2021 14:18:15 +0100 Subject: Initial commit --- api/bool.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 api/bool.c (limited to 'api/bool.c') diff --git a/api/bool.c b/api/bool.c new file mode 100644 index 0000000..2857f0b --- /dev/null +++ b/api/bool.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include "bool.h" +#include "nil.h" + +UwUVMValue uwubool_create(bool value) +{ + UwUVMValue vm_value = { + .type = VT_NAT, + .value = { + .nat_value = { + .type = &uwubool_type, + .data = malloc(sizeof(bool)) + }, + }, + }; + + *(bool *) vm_value.value.nat_value.data = value; + return vm_value; +} + +bool uwubool_get(UwUVMValue vm_value) +{ + if (vm_value.type != VT_NAT) + return true; + else if (vm_value.value.nat_value.type == &uwunil_type) + return false; + else if (vm_value.value.nat_value.type == &uwubool_type) + return *(bool *) vm_value.value.nat_value.data; + else + return true; +} + +static void *uwubool_copy(void *data) +{ + bool *copy = malloc(sizeof(*copy)); + *copy = *(bool *) data; + return copy; +} + +static char *uwubool_print(void *data) +{ + return strdup(((bool *) data) ? "true" : "false"); +} + +UwUVMNativeType uwubool_type = { + .copy = &uwubool_copy, + .delete = &free, + .print = &uwubool_print, +}; -- cgit v1.2.3