moka/lib/value.c

94 lines
1.8 KiB
C
Raw Normal View History

2024-03-26 18:31:33 +00:00
#include "value.h"
#include "node.h"
2024-03-26 18:31:33 +00:00
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_init_ref(struct value* self, size_t value, int line)
{
assert(self);
self->data.ref = value;
self->type = TY_REF;
self->line = line;
}
void value_init_native(struct value* self, struct native* value, int line)
{
assert(self);
assert(value);
self->data.native = value;
self->type = TY_NATIVE;
self->line = line;
}
void value_init_lazy(struct value* self, struct node* value, int line)
{
assert(self);
assert(value);
self->data.lazy = value;
self->type = TY_LAZY;
self->line = line;
}
2024-03-26 18:31:33 +00:00
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);
}
if (self->type == TY_NATIVE)
{
native_free(self->data.native);
free(self->data.native);
}
2024-03-26 18:31:33 +00:00
}