add native box API

This commit is contained in:
Yuichi Nishiwaki 2014-03-29 16:55:37 +09:00
parent 2003ad18f2
commit 2151640331
7 changed files with 77 additions and 2 deletions

28
include/picrin/box.h Normal file
View File

@ -0,0 +1,28 @@
/**
* 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(v) ((struct pic_box *)pic_ptr(v))
pic_value pic_box(pic_state *, pic_value);
pic_value pic_unbox(pic_state *, pic_value);
void pic_set_box(pic_state *, pic_value, pic_value);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -115,7 +115,8 @@ enum pic_tt {
PIC_TT_LIB,
PIC_TT_VAR,
PIC_TT_IREP,
PIC_TT_DATA
PIC_TT_DATA,
PIC_TT_BOX
};
#define PIC_OBJECT_HEADER \
@ -266,8 +267,10 @@ pic_type_repr(enum pic_tt tt)
return "irep";
case PIC_TT_DATA:
return "data";
case PIC_TT_BOX:
return "box";
}
return 0; /* logic flaw */
UNREACHABLE();
}
static inline bool

30
src/box.c Normal file
View File

@ -0,0 +1,30 @@
#include "picrin.h"
#include "picrin/box.h"
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_value
pic_unbox(pic_state *pic, pic_value box)
{
if (! pic_box_p(box)) {
pic_errorf(pic, "expected box, but got ~s", box);
}
return pic_box_ptr(box)->value;
}
void
pic_set_box(pic_state *pic, pic_value box, pic_value value)
{
if (! pic_box_p(box)) {
pic_errorf(pic, "expected box, but got ~s", box);
}
pic_box_ptr(box)->value = value;
}

View File

@ -824,6 +824,7 @@ analyze_node(analyze_state *state, pic_value obj, bool tailpos)
case PIC_TT_VAR:
case PIC_TT_IREP:
case PIC_TT_DATA:
case PIC_TT_BOX:
pic_errorf(pic, "invalid expression given: ~s", obj);
}
UNREACHABLE();

View File

@ -19,6 +19,7 @@
#include "picrin/lib.h"
#include "picrin/var.h"
#include "picrin/data.h"
#include "picrin/box.h"
#if GC_DEBUG
# include <string.h>
@ -496,6 +497,11 @@ 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:
case PIC_TT_FLOAT:
@ -646,6 +652,9 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
xh_destroy(&data->storage);
break;
}
case PIC_TT_BOX: {
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:

View File

@ -509,6 +509,7 @@ macroexpand_node(pic_state *pic, pic_value expr, struct pic_senv *senv)
case PIC_TT_VAR:
case PIC_TT_IREP:
case PIC_TT_DATA:
case PIC_TT_BOX:
pic_errorf(pic, "unexpected value type: ~s", expr);
}
UNREACHABLE();

View File

@ -335,6 +335,9 @@ write_core(struct writer_control *p, pic_value obj)
case PIC_TT_DATA:
xfprintf(file, "#<data %p>", pic_ptr(obj));
break;
case PIC_TT_BOX:
xfprintf(file, "#<box %p>", pic_ptr(obj));
break;
}
}