]> git.lizzy.rs Git - uwu-lang.git/blobdiff - api/str.c
Unify value types
[uwu-lang.git] / api / str.c
index 37b8e7015ca5b97b641f1b0b843002effcbd072b..84d2b107047ea1cad107200fdcca40ee253824f9 100644 (file)
--- a/api/str.c
+++ b/api/str.c
@@ -1,32 +1,32 @@
 #include <string.h>
-#include "../src/util.h"
+#include <stdlib.h>
 #include "str.h"
 
 UwUVMValue uwustr_create(const char *value)
 {
        return (UwUVMValue) {
-               .type = VT_STR,
-               .value = {
-                       .str_value = strdup(value),
-               },
+               .type = &uwustr_type,
+               .data = strdup(value),
        };
 }
 
 char *uwustr_get(UwUVMValue vm_value)
 {
-       switch (vm_value.type) {
-               case VT_INT:
-                       return asprintf_wrapper("%d", vm_value.value.int_value);
-
-               case VT_STR:
-                       return strdup(vm_value.value.str_value);
+       vm_value.type->print(vm_value.data);
+}
 
-               case VT_REF:
-                       return asprintf_wrapper("[Function reference: %p]", vm_value.value.ref_value);
+static void *uwustr_copy(void *data)
+{
+       return strdup(data);
+}
 
-               case VT_NAT:
-                       return vm_value.value.nat_value.type->print
-                               ? vm_value.value.nat_value.type->print(vm_value.value.nat_value.data)
-                               : asprintf_wrapper("[Native value: %p: %p]", vm_value.value.nat_value.data, vm_value.value.nat_value.type);
-       }
+static char *uwustr_print(void *data)
+{
+       return strdup(data);
 }
+
+UwUVMType uwustr_type = {
+       .copy = &uwustr_copy,
+       .delete = &free,
+       .print = &uwustr_print,
+};