]> git.lizzy.rs Git - uwu-lang.git/blob - api/bool.c
2857f0b76c68a7788f4a1f70d791da84f05c9d56
[uwu-lang.git] / api / bool.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "bool.h"
5 #include "nil.h"
6
7 UwUVMValue uwubool_create(bool value)
8 {
9         UwUVMValue vm_value = {
10                 .type = VT_NAT,
11                 .value = {
12                         .nat_value = {
13                                 .type = &uwubool_type,
14                                 .data = malloc(sizeof(bool))
15                         },
16                 },
17         };
18
19         *(bool *) vm_value.value.nat_value.data = value;
20         return vm_value;
21 }
22
23 bool uwubool_get(UwUVMValue vm_value)
24 {
25         if (vm_value.type != VT_NAT)
26                 return true;
27         else if (vm_value.value.nat_value.type == &uwunil_type)
28                 return false;
29         else if (vm_value.value.nat_value.type == &uwubool_type)
30                 return *(bool *) vm_value.value.nat_value.data;
31         else
32                 return true;
33 }
34
35 static void *uwubool_copy(void *data)
36 {
37         bool *copy = malloc(sizeof(*copy));
38         *copy = *(bool *) data;
39         return  copy;
40 }
41
42 static char *uwubool_print(void *data)
43 {
44         return strdup(((bool *) data) ? "true" : "false");
45 }
46
47 UwUVMNativeType uwubool_type = {
48         .copy = &uwubool_copy,
49         .delete = &free,
50         .print = &uwubool_print,
51 };