picrin/src/value.c

235 lines
3.2 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"
enum pic_tt
pic_type(pic_value v)
{
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();
}
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;
}
2013-11-06 02:06:31 -05:00
#if PIC_NAN_BOXING
bool
pic_eq_p(pic_value x, pic_value y)
{
return x.u.data == y.u.data;
}
2013-11-06 02:12:31 -05:00
bool
pic_eqv_p(pic_value x, pic_value y)
{
return x.u.data == y.u.data;
}
2013-11-06 02:06:31 -05:00
#else
bool
pic_eq_p(pic_value x, pic_value y)
{
if (pic_type(x) != pic_type(y))
return false;
switch (pic_type(x)) {
case PIC_TT_NIL:
return true;
case PIC_TT_SYMBOL:
return pic_sym(x) == pic_sym(y);
default:
return false;
}
}
bool
pic_eqv_p(pic_value x, pic_value y)
{
if (pic_type(x) != pic_type(y))
return false;
switch (pic_type(x)) {
case PIC_TT_NIL:
return true;
case PIC_TT_SYMBOL:
return pic_sym(x) == pic_sym(y);
case PIC_TT_FLOAT:
return pic_float(x) == pic_float(y);
case PIC_TT_INT:
return pic_int(x) == pic_int(y);
default:
return false;
}
}
2013-11-06 02:06:31 -05:00
#endif