From e87f6323273e077d4453758511009be92c14c77c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 17 Oct 2013 22:42:20 +0900 Subject: [PATCH] writer writes lists in short form --- include/picrin/value.h | 3 ++- src/gc.c | 4 ++-- src/port.c | 23 ++++++++++++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/include/picrin/value.h b/include/picrin/value.h index e752f36a..743e04dd 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -50,7 +50,7 @@ struct pic_symbol { struct pic_proc; -#define pic_object_ptr(o) ((struct pic_object *)o.u.data) +#define pic_obj_ptr(o) ((struct pic_object *)o.u.data) #define pic_pair_ptr(o) ((struct pic_pair *)o.u.data) #define pic_symbol_ptr(o) ((struct pic_symbol *)o.u.data) @@ -70,5 +70,6 @@ pic_value pic_float_value(double); #define pic_true_p(v) ((v).type == PIC_VTYPE_TRUE) #define pic_false_p(v) ((v).type == PIC_VTYPE_FALSE) #define pic_undef_p(v) ((v).type == PIC_VTYPE_UNDEF) +#define pic_pair_p(v) (pic_type(v) == PIC_TT_PAIR) #endif diff --git a/src/gc.c b/src/gc.c index 7902a92e..436fbcc6 100644 --- a/src/gc.c +++ b/src/gc.c @@ -78,7 +78,7 @@ pic_gc_protect(pic_state *pic, pic_value v) if (v.type != PIC_VTYPE_HEAP) { return; } - obj = pic_object_ptr(v); + obj = pic_obj_ptr(v); gc_protect(pic, obj); } @@ -160,7 +160,7 @@ gc_mark(pic_state *pic, pic_value v) if (v.type != PIC_VTYPE_HEAP) return; - obj = pic_object_ptr(v); + obj = pic_obj_ptr(v); gc_mark_object(pic, obj); } diff --git a/src/port.c b/src/port.c index 457bd004..f4e5de2b 100644 --- a/src/port.c +++ b/src/port.c @@ -4,6 +4,8 @@ #include "picrin.h" #include "picrin/proc.h" +static void write_pair(pic_state *pic, struct pic_pair *pair); + static void write(pic_state *pic, pic_value obj) { @@ -19,9 +21,7 @@ write(pic_state *pic, pic_value obj) break; case PIC_TT_PAIR: printf("("); - write(pic, pic_car(pic, obj)); - printf(" . "); - write(pic, pic_cdr(pic, obj)); + write_pair(pic, pic_pair_ptr(obj)); printf(")"); break; case PIC_TT_SYMBOL: @@ -39,6 +39,23 @@ write(pic_state *pic, pic_value obj) } } +static void +write_pair(pic_state *pic, struct pic_pair *pair) +{ + write(pic, pair->car); + + if (pic_nil_p(pair->cdr)) { + return; + } + if (pic_pair_p(pair->cdr)) { + printf(" "); + write_pair(pic, pic_pair_ptr(pair->cdr)); + return; + } + printf(" . "); + write(pic, pair->cdr); +} + void pic_debug(pic_state *pic, pic_value obj) {