diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-12-30 16:02:10 +0100 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-12-30 16:02:10 +0100 |
commit | 058d954e80f83c26deb209008f11d87a5b59418e (patch) | |
tree | ef7b5a95cfd1dbdba71041e1f0608d551adfa360 /api/str.c | |
parent | 3ba311b0afdf9cc62d630687d171bee0b6435e4a (diff) | |
download | uwu-lang-058d954e80f83c26deb209008f11d87a5b59418e.tar.xz |
Unify value types
Diffstat (limited to 'api/str.c')
-rw-r--r-- | api/str.c | 36 |
1 files changed, 18 insertions, 18 deletions
@@ -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, +}; |