From b640dc08e8a5d794337a8ccad43f3cdf8d55695d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 10 Oct 2013 17:48:01 +0900 Subject: [PATCH] add pic_debug function --- include/picrin.h | 2 ++ include/picrin/value.h | 8 ++++++++ src/value.c | 11 +++++++++++ src/write.c | 26 ++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/write.c diff --git a/include/picrin.h b/include/picrin.h index bfb72de7..fb5d964d 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -19,4 +19,6 @@ pic_value pic_cdr(pic_state *, pic_value); pic_value pic_intern_cstr(pic_state *, const char *); +void pic_debug(pic_state *, pic_value); + #endif diff --git a/include/picrin/value.h b/include/picrin/value.h index 86146585..85cb863a 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -14,6 +14,9 @@ typedef struct { } pic_value; enum pic_tt { + /* immediate */ + PIC_TT_NIL, + /* heap */ PIC_TT_PAIR, PIC_TT_SYMBOL }; @@ -36,6 +39,11 @@ struct pic_symbol { char *name; }; +#define pic_pair_ptr(o) ((struct pic_pair *)o->u->data) +#define pic_symbol_ptr(o) ((struct pic_symbol *)o->u->data) + +enum pic_tt pic_type(pic_value v); + pic_value pic_nil_value(); pic_value pic_obj_value(struct pic_object *obj); diff --git a/src/value.c b/src/value.c index 1c60e3ce..b06007a1 100644 --- a/src/value.c +++ b/src/value.c @@ -1,5 +1,16 @@ #include "picrin.h" +enum pic_tt +pic_type(pic_value v) +{ + switch (v->type) { + case PIC_VTYPE_NIL: + return PIC_TT_NIL; + case PIC_VTYPE_HEAP: + return v->u->data->tt; + } +} + pic_value pic_nil_value() { diff --git a/src/write.c b/src/write.c new file mode 100644 index 00000000..bc4483e3 --- /dev/null +++ b/src/write.c @@ -0,0 +1,26 @@ +#include +#include + +#include "picrin.h" + +void +pic_debug(pic_state *pic, pic_value obj) +{ + switch (pic_type(obj)) { + case PIC_TT_NIL: + printf("()"); + break; + case PIC_TT_PAIR: + printf("("); + pic_debug(pic, pic_car(pic, obj)); + printf(" . "); + pic_debug(pic, pic_cdr(pic, obj)); + printf(")"); + break; + case PIC_TT_SYMBOL: + printf("%s", pic_symbol_ptr(obj)->name); + break; + default: + abort(); + } +}