moka/lib/value.c

62 lines
1.1 KiB
C
Raw Normal View History

2024-03-26 18:31:33 +00:00
#include "value.h"
MK_ENUM_C(TypeKind, TYPE_KIND);
void value_init_int(struct value* self, int value, int line)
{
assert(self);
self->data.integer = value;
self->type = TY_INT;
self->line = line;
}
void value_init_float(struct value* self, float value, int line)
{
assert(self);
self->data.real = value;
self->type = TY_FLOAT;
self->line = line;
}
void value_init_bool(struct value* self, bool value, int line)
{
assert(self);
self->data.boolean = value;
self->type = TY_BOOL;
self->line = line;
}
void value_init_string(struct value* self, char const* value, int line)
{
assert(self);
assert(value);
self->data.str = strdup(value);
self->type = TY_STRING;
self->line = line;
}
void value_init_symbol(struct value* self, char const* value, int line)
{
assert(self);
assert(value);
self->data.sym = strdup(value);
self->type = TY_SYMBOL;
self->line = line;
}
void value_free(struct value* self)
{
assert(self);
if (self->type == TY_STRING)
{
free(self->data.str);
}
if (self->type == TY_SYMBOL)
{
free(self->data.sym);
}
}