]> git.lizzy.rs Git - uwu-lang.git/blob - api/bool.c
Unify value types
[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 = &uwubool_type,
11                 .data = malloc(sizeof(bool)),
12         };
13
14         *(bool *) vm_value.data = value;
15         return vm_value;
16 }
17
18 bool uwubool_get(UwUVMValue vm_value)
19 {
20         if (vm_value.type == &uwunil_type)
21                 return false;
22         else if (vm_value.type == &uwubool_type)
23                 return *(bool *) vm_value.data;
24         else
25                 return true;
26 }
27
28 static void *uwubool_copy(void *data)
29 {
30         bool *copy = malloc(sizeof(*copy));
31         *copy = *(bool *) data;
32         return copy;
33 }
34
35 static char *uwubool_print(void *data)
36 {
37         return strdup(((bool *) data) ? "true" : "false");
38 }
39
40 UwUVMType uwubool_type = {
41         .copy = &uwubool_copy,
42         .delete = &free,
43         .print = &uwubool_print,
44 };