picrin/src/value.c

219 lines
3.1 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-11-05 06:33:24 -05:00
#include "picrin.h"
#if PIC_NAN_BOXING
enum pic_tt
pic_type(pic_value v)
{
if ((int)0xfff00000 >= v.u.type_)
return PIC_TT_FLOAT;
switch (pic_vtype(v)) {
case PIC_VTYPE_NIL:
return PIC_TT_NIL;
case PIC_VTYPE_TRUE:
return PIC_TT_BOOL;
case PIC_VTYPE_FALSE:
return PIC_TT_BOOL;
case PIC_VTYPE_UNDEF:
return PIC_TT_UNDEF;
case PIC_VTYPE_FLOAT:
return PIC_TT_FLOAT;
case PIC_VTYPE_INT:
return PIC_TT_INT;
case PIC_VTYPE_SYMBOL:
return PIC_TT_SYMBOL;
case PIC_VTYPE_CHAR:
return PIC_TT_CHAR;
case PIC_VTYPE_EOF:
return PIC_TT_EOF;
case PIC_VTYPE_HEAP:
return ((struct pic_object *)pic_ptr(v))->tt;
}
/* logic flaw (suppress warnings gcc will emit) */
abort();
}
#else
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-11-05 00:42:44 -05:00
switch (pic_vtype(v)) {
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-27 11:21:24 -04:00
case PIC_VTYPE_INT:
return PIC_TT_INT;
2013-10-28 13:11:31 -04:00
case PIC_VTYPE_SYMBOL:
return PIC_TT_SYMBOL;
2013-11-04 21:37:18 -05:00
case PIC_VTYPE_CHAR:
return PIC_TT_CHAR;
2013-10-22 03:02:20 -04:00
case PIC_VTYPE_EOF:
return PIC_TT_EOF;
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-11-05 06:33:24 -05:00
#endif
2013-10-30 02:37:33 -04:00
const char *
pic_type_repr(enum pic_tt tt)
{
static const char *reprs[13] = {
"nil",
"boolean",
"float",
"int",
"symbol",
"eof",
"undef",
"pair",
"string",
"vector",
"proc",
"port",
"env"
};
return reprs[tt];
}
2013-10-10 04:08:54 -04:00
pic_value
pic_nil_value()
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_NIL);
2013-10-10 04:08:54 -04:00
return v;
}
2013-10-16 00:17:01 -04:00
pic_value
pic_true_value()
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_TRUE);
2013-10-16 00:17:01 -04:00
return v;
}
pic_value
pic_false_value()
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_FALSE);
2013-10-16 00:17:01 -04:00
return v;
}
pic_value
pic_bool_value(bool b)
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, b ? PIC_VTYPE_TRUE : PIC_VTYPE_FALSE);
2013-10-16 00:17:01 -04:00
return v;
}
2013-11-05 06:33:24 -05:00
#if PIC_NAN_BOXING
pic_value
pic_obj_value(void *ptr)
{
pic_value v;
pic_init_value(v, PIC_VTYPE_HEAP);
v.u.data = (void*)((long long)v.u.data | ((long long)ptr));
return v;
}
pic_value
pic_float_value(double f)
{
pic_value v;
if (f != f) {
v.u.type_ = 0x7ff80000;
v.u.i = 0;
} else {
v.u.f = f;
}
return v;
}
#else
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-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_HEAP);
2013-10-10 04:54:35 -04:00
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;
2013-11-05 00:42:44 -05:00
pic_init_value(v, 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
2013-11-05 06:33:24 -05:00
#endif
2013-10-27 11:21:24 -04:00
pic_value
pic_int_value(int i)
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_INT);
2013-10-27 11:21:24 -04:00
v.u.i = i;
return v;
}
2013-10-28 13:11:31 -04:00
pic_value
pic_symbol_value(pic_sym sym)
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_SYMBOL);
2013-10-28 13:11:31 -04:00
v.u.sym = sym;
return v;
}
2013-11-04 21:37:18 -05:00
pic_value
pic_char_value(char c)
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_CHAR);
2013-11-04 21:37:18 -05:00
v.u.c = c;
return v;
}
2013-10-12 01:40:01 -04:00
pic_value
pic_undef_value()
{
pic_value v;
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_UNDEF);
2013-10-12 01:40:01 -04:00
return v;
}