From 0c1babb6c5a289e13105b6e99fd2896ffdc4f944 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 22 Oct 2013 00:19:43 +0900 Subject: [PATCH] add pic_reverse --- include/picrin/pair.h | 2 ++ src/codegen.c | 10 +--------- src/pair.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/picrin/pair.h b/include/picrin/pair.h index 2d5a02b2..c44aef8e 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -8,6 +8,8 @@ pic_value pic_cdr(pic_state *, pic_value); bool pic_list_p(pic_state *, pic_value); pic_value pic_list(pic_state *, size_t, ...); +pic_value pic_reverse(pic_state *, pic_value); + pic_value pic_assq(pic_state *, pic_value key, pic_value assoc); pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc); diff --git a/src/codegen.c b/src/codegen.c index d9b36e43..1d26dab6 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -389,21 +389,13 @@ pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *en } } -static pic_value -reverse(pic_state *pic, pic_value list, pic_value acc) -{ - if (pic_nil_p(list)) - return acc; - return reverse(pic, pic_cdr(pic, list), pic_cons(pic, pic_car(pic, list), acc)); -} - static void pic_gen_call(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *env) { pic_value seq; int i = 0; - seq = reverse(pic, obj, pic_nil_value()); + seq = pic_reverse(pic, obj); for (; ! pic_nil_p(seq); seq = pic_cdr(pic, seq)) { pic_value v; diff --git a/src/pair.c b/src/pair.c index 63fa1e40..9325cf22 100644 --- a/src/pair.c +++ b/src/pair.c @@ -48,6 +48,17 @@ pic_list_p(pic_state *pic, pic_value obj) return pic_nil_p(obj); } +pic_value +pic_reverse(pic_state *pic, pic_value list) +{ + pic_value v, acc = pic_nil_value(); + + for (v = list; ! pic_nil_p(v); v = pic_cdr(pic ,v)) { + acc = pic_cons(pic, pic_car(pic, v), acc); + } + return acc; +} + pic_value pic_assq(pic_state *pic, pic_value key, pic_value assoc) {