roza/lib/value.c

53 lines
841 B
C

#include "value.h"
void value_init(value_t* value, type_t* type)
{
assert(value);
assert(type);
value->type = type;
}
void value_free(value_t* value)
{
assert(value);
}
int value_eq(value_t* value, value_t* rhs)
{
if (!type_eq(value->type, rhs->type))
{
return 0;
}
switch (value->type->kind)
{
case TYPE_NUM: {
return value->value.num == rhs->value.num;
} break;
default: {
fprintf(stderr, "Cannot compare value of type '%s'.\n",
TypeKindStr[value->type->kind]);
abort();
};
}
return 0;
}
size_t value_str(value_t* value, char* buffer, size_t size)
{
assert(value);
assert(buffer);
switch (value->type->kind)
{
case TYPE_NUM:
return snprintf(buffer, size, "%lf", value->value.num);
break;
default: return 0;
}
}