add box type

This commit is contained in:
Yuichi Nishiwaki 2015-07-06 21:32:42 +09:00
parent acc1852400
commit d53f0cf9e4
4 changed files with 67 additions and 1 deletions

View File

@ -490,6 +490,12 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
}
break;
}
case PIC_TT_BOX: {
struct pic_box *box = (struct pic_box *)obj;
gc_mark(pic, box->value);
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
#if PIC_ENABLE_FLOAT
@ -737,6 +743,9 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
case PIC_TT_CP: {
break;
}
case PIC_TT_BOX: {
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
#if PIC_ENABLE_FLOAT

View File

@ -268,6 +268,7 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *);
#include "picrin/symbol.h"
#include "picrin/vector.h"
#include "picrin/reg.h"
#include "picrin/box.h"
#if defined(__cplusplus)
}

View File

@ -0,0 +1,53 @@
/**
* See Copyright Notice in picrin.h
*/
#ifndef PICRIN_BOX_H
#define PICRIN_BOX_H
#if defined(__cplusplus)
extern "C" {
#endif
struct pic_box {
PIC_OBJECT_HEADER
pic_value value;
};
#define pic_box_p(v) (pic_type(v) == PIC_TT_BOX)
#define pic_box_ptr(o) ((struct pic_box *)pic_ptr(o))
PIC_INLINE pic_value
pic_box(pic_state *pic, pic_value value)
{
struct pic_box *box;
box = (struct pic_box *)pic_obj_alloc(pic, sizeof(struct pic_box), PIC_TT_BOX);
box->value = value;
return pic_obj_value(box);
}
PIC_INLINE pic_value
pic_unbox(pic_state *pic, pic_value box)
{
if (! pic_box_p(box)) {
pic_errorf(pic, "box required");
}
return pic_box_ptr(box)->value;
}
PIC_INLINE void
pic_set_box(pic_state *pic, pic_value box, pic_value value)
{
if (! pic_box_p(box)) {
pic_errorf(pic, "box required");
}
pic_box_ptr(box)->value = value;
}
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -163,10 +163,11 @@ enum pic_tt {
PIC_TT_DATA,
PIC_TT_DICT,
PIC_TT_REG,
PIC_TT_BOX,
PIC_TT_RECORD,
PIC_TT_CXT,
PIC_TT_IREP,
PIC_TT_CP
PIC_TT_CP,
};
#define PIC_OBJECT_HEADER \
@ -337,6 +338,8 @@ pic_type_repr(enum pic_tt tt)
return "reg";
case PIC_TT_RECORD:
return "record";
case PIC_TT_BOX:
return "box";
case PIC_TT_CP:
return "checkpoint";
}