From 058d954e80f83c26deb209008f11d87a5b59418e Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 30 Dec 2021 16:02:10 +0100 Subject: Unify value types --- api/str.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'api/str.c') diff --git a/api/str.c b/api/str.c index 37b8e70..84d2b10 100644 --- a/api/str.c +++ b/api/str.c @@ -1,32 +1,32 @@ #include -#include "../src/util.h" +#include #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, +}; -- cgit v1.2.3