add nilptrs, more conversion functions and change how ptrs work

Pointers now no longer point to the Value struct (the internal wrapper for
values) but to the value itself.
This commit is contained in:
r4
2021-12-28 17:53:27 +01:00
parent befce544e7
commit ba8d2f0702
5 changed files with 51 additions and 10 deletions

15
tok.c
View File

@@ -11,6 +11,7 @@ size_t type_size[TypeEnumSize] = {
[TypeInt] = sizeof(((Value*)NULL)->Int),
[TypeBool] = sizeof(((Value*)NULL)->Bool),
[TypeChar] = sizeof(((Value*)NULL)->Char),
[TypePtr] = sizeof(((Value*)NULL)->Ptr),
[TypeArr] = sizeof(((Value*)NULL)->Arr),
};
@@ -47,11 +48,17 @@ void print_value(const Value *v, bool raw) {
else printf("'%c'", v->Char);
}
break;
case TypePtr:
printf("ptr<%s>(", type_str[v->Ptr.type]);
print_value(v->Ptr.val, false);
printf(")");
case TypePtr: {
if (v->Ptr.val) {
printf("ptr<%s>(", type_str[v->Ptr.type]);
Value deref = { .type = v->Ptr.type };
memcpy(&deref.Void, v->Ptr.val, type_size[v->Ptr.type]);
print_value(&deref, false);
printf(")");
} else
printf("ptr<%s>(nil)", type_str[v->Ptr.type]);
break;
}
case TypeArr:
if (v->Arr.is_string) {
if (v->Arr.type != TypeChar)