picrin/src/value.c

96 lines
1.3 KiB
C
Raw Normal View History

2013-10-12 05:46:41 -04:00
#include <stdlib.h>
2013-10-19 14:05:42 -04:00
#include <stdbool.h>
2013-10-12 05:46:41 -04:00
2013-10-19 14:05:42 -04:00
#include "picrin/value.h"
2013-10-10 04:05:58 -04:00
2013-10-10 04:48:01 -04:00
enum pic_tt
pic_type(pic_value v)
{
2013-10-10 04:54:35 -04:00
switch (v.type) {
2013-10-10 04:48:01 -04:00
case PIC_VTYPE_NIL:
return PIC_TT_NIL;
2013-10-16 00:17:01 -04:00
case PIC_VTYPE_TRUE:
2013-10-19 14:05:42 -04:00
return PIC_TT_BOOL;
2013-10-16 00:17:01 -04:00
case PIC_VTYPE_FALSE:
return PIC_TT_BOOL;
2013-10-12 01:40:01 -04:00
case PIC_VTYPE_UNDEF:
return PIC_TT_UNDEF;
2013-10-19 14:05:42 -04:00
case PIC_VTYPE_FLOAT:
return PIC_TT_FLOAT;
2013-10-10 04:48:01 -04:00
case PIC_VTYPE_HEAP:
2013-10-10 04:54:35 -04:00
return ((struct pic_object *)v.u.data)->tt;
2013-10-10 04:48:01 -04:00
}
2013-10-19 14:05:42 -04:00
/* logic flaw (suppress warnings gcc will emit) */
abort();
2013-10-10 04:48:01 -04:00
}
2013-10-10 04:08:54 -04:00
pic_value
pic_nil_value()
{
pic_value v;
2013-10-10 04:54:35 -04:00
v.type = PIC_VTYPE_NIL;
2013-10-10 04:08:54 -04:00
v.u.data = NULL;
return v;
}
2013-10-16 00:17:01 -04:00
pic_value
pic_true_value()
{
pic_value v;
v.type = PIC_VTYPE_TRUE;
v.u.data = NULL;
return v;
}
pic_value
pic_false_value()
{
pic_value v;
v.type = PIC_VTYPE_FALSE;
v.u.data = NULL;
return v;
}
pic_value
pic_bool_value(bool b)
{
pic_value v;
v.type = b ? PIC_VTYPE_TRUE : PIC_VTYPE_FALSE;
v.u.data = NULL;
return v;
}
2013-10-10 04:05:58 -04:00
pic_value
2013-10-10 04:54:35 -04:00
pic_obj_value(void *ptr)
2013-10-10 04:05:58 -04:00
{
pic_value v;
2013-10-10 04:54:35 -04:00
v.type = PIC_VTYPE_HEAP;
v.u.data = ptr;
2013-10-10 04:05:58 -04:00
return v;
}
2013-10-11 11:15:46 -04:00
pic_value
pic_float_value(double f)
2013-10-11 11:15:46 -04:00
{
pic_value v;
v.type = PIC_VTYPE_FLOAT;
v.u.f = f;
2013-10-11 11:15:46 -04:00
return v;
}
2013-10-12 01:40:01 -04:00
pic_value
pic_undef_value()
{
pic_value v;
v.type = PIC_VTYPE_UNDEF;
v.u.data = NULL;
return v;
}