manage converter in var

This commit is contained in:
Yuichi Nishiwaki 2014-07-23 09:11:31 +09:00
parent e3887691d2
commit 424216ca23
3 changed files with 16 additions and 4 deletions

View File

@ -12,12 +12,13 @@ extern "C" {
struct pic_var { struct pic_var {
PIC_OBJECT_HEADER PIC_OBJECT_HEADER
pic_value stack; pic_value stack;
struct pic_proc *conv;
}; };
#define pic_var_p(o) (pic_type(o) == PIC_TT_VAR) #define pic_var_p(o) (pic_type(o) == PIC_TT_VAR)
#define pic_var_ptr(o) ((struct pic_var *)pic_ptr(o)) #define pic_var_ptr(o) ((struct pic_var *)pic_ptr(o))
struct pic_var *pic_var_new(pic_state *, pic_value); struct pic_var *pic_var_new(pic_state *, pic_value, struct pic_proc * /* = NULL */);
pic_value pic_var_ref(pic_state *, struct pic_var *); pic_value pic_var_ref(pic_state *, struct pic_var *);
void pic_var_set(pic_state *, struct pic_var *, pic_value); void pic_var_set(pic_state *, struct pic_var *, pic_value);

View File

@ -469,6 +469,9 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
case PIC_TT_VAR: { case PIC_TT_VAR: {
struct pic_var *var = (struct pic_var *)obj; struct pic_var *var = (struct pic_var *)obj;
gc_mark(pic, var->stack); gc_mark(pic, var->stack);
if (var->conv) {
gc_mark_object(pic, (struct pic_object *)var->conv);
}
break; break;
} }
case PIC_TT_IREP: { case PIC_TT_IREP: {

View File

@ -7,12 +7,13 @@
#include "picrin/pair.h" #include "picrin/pair.h"
struct pic_var * struct pic_var *
pic_var_new(pic_state *pic, pic_value init) pic_var_new(pic_state *pic, pic_value init, struct pic_proc *conv)
{ {
struct pic_var *var; struct pic_var *var;
var = (struct pic_var *)pic_obj_alloc(pic, sizeof(struct pic_var), PIC_TT_VAR); var = (struct pic_var *)pic_obj_alloc(pic, sizeof(struct pic_var), PIC_TT_VAR);
var->stack = pic_nil_value(); var->stack = pic_nil_value();
var->conv = conv;
pic_var_push(pic, var, init); pic_var_push(pic, var, init);
@ -28,12 +29,18 @@ pic_var_ref(pic_state *pic, struct pic_var *var)
void void
pic_var_set(pic_state *pic, struct pic_var *var, pic_value value) pic_var_set(pic_state *pic, struct pic_var *var, pic_value value)
{ {
if (var->conv != NULL) {
value = pic_apply1(pic, var->conv, value);
}
pic_set_car(pic, var->stack, value); pic_set_car(pic, var->stack, value);
} }
void void
pic_var_push(pic_state *pic, struct pic_var *var, pic_value value) pic_var_push(pic_state *pic, struct pic_var *var, pic_value value)
{ {
if (var->conv != NULL) {
value = pic_apply1(pic, var->conv, value);
}
var->stack = pic_cons(pic, value, var->stack); var->stack = pic_cons(pic, value, var->stack);
} }
@ -46,11 +53,12 @@ pic_var_pop(pic_state *pic, struct pic_var *var)
static pic_value static pic_value
pic_var_make_var(pic_state *pic) pic_var_make_var(pic_state *pic)
{ {
struct pic_proc *conv = NULL;
pic_value init; pic_value init;
pic_get_args(pic, "o", &init); pic_get_args(pic, "o|l", &init, &conv);
return pic_obj_value(pic_var_new(pic, init)); return pic_obj_value(pic_var_new(pic, init, conv));
} }
static pic_value static pic_value