remove pair.h

This commit is contained in:
Yuichi Nishiwaki 2016-02-19 02:29:40 +09:00
parent 9ae6f0cbe9
commit 00e98548d7
11 changed files with 263 additions and 428 deletions

View File

@ -225,7 +225,7 @@ cont_call(pic_state *pic)
pic_get_args(pic, "*", &argc, &argv); pic_get_args(pic, "*", &argc, &argv);
cont = pic_data(pic, pic_closure_ref(pic, 0)); cont = pic_data(pic, pic_closure_ref(pic, 0));
cont->results = pic_list_by_array(pic, argc, argv); cont->results = pic_make_list(pic, argc, argv);
/* execute guard handlers */ /* execute guard handlers */
pic_wind(pic, pic->cp, cont->cp); pic_wind(pic, pic->cp, cont->cp);

View File

@ -115,7 +115,7 @@ pic_system_getenvs(pic_state *pic)
val = pic_cstr_value(pic, getenv(pic_str(pic, key))); val = pic_cstr_value(pic, getenv(pic_str(pic, key)));
/* push */ /* push */
data = pic_acons(pic, pic_obj_value(key), pic_obj_value(val), data); data = pic_cons(pic, pic_cons(pic, pic_obj_value(key), pic_obj_value(val)), data);
pic_gc_arena_restore(pic, ai); pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, data); pic_gc_protect(pic, data);

View File

@ -109,7 +109,7 @@ cont_call(pic_state *pic)
} }
cont = pic_data_ptr(pic_closure_ref(pic, CV_ESCAPE))->data; cont = pic_data_ptr(pic_closure_ref(pic, CV_ESCAPE))->data;
cont->results = pic_list_by_array(pic, argc, argv); cont->results = pic_make_list(pic, argc, argv);
pic_load_point(pic, cont); pic_load_point(pic, cont);

View File

@ -194,7 +194,7 @@ pic_error_error(pic_state *pic)
pic_get_args(pic, "z*", &str, &argc, &argv); pic_get_args(pic, "z*", &str, &argc, &argv);
pic_error(pic, "", str, pic_list_by_array(pic, argc, argv)); pic_error(pic, "", str, pic_make_list(pic, argc, argv));
} }
static pic_value static pic_value

View File

@ -24,7 +24,7 @@ optimize_beta(pic_state *pic, pic_value expr)
if (sym == pic->sQUOTE) { if (sym == pic->sQUOTE) {
return expr; return expr;
} else if (sym == pic->sLAMBDA) { } else if (sym == pic->sLAMBDA) {
return pic_list3(pic, pic_list_ref(pic, expr, 0), pic_list_ref(pic, expr, 1), optimize_beta(pic, pic_list_ref(pic, expr, 2))); return pic_list(pic, 3, pic_list_ref(pic, expr, 0), pic_list_ref(pic, expr, 1), optimize_beta(pic, pic_list_ref(pic, expr, 2)));
} }
} }
@ -47,12 +47,12 @@ optimize_beta(pic_state *pic, pic_value expr)
goto exit; goto exit;
defs = pic_nil_value(pic); defs = pic_nil_value(pic);
pic_for_each (val, args, it) { pic_for_each (val, args, it) {
pic_push(pic, pic_list3(pic, pic_obj_value(pic->sDEFINE), pic_car(pic, formals), val), defs); pic_push(pic, pic_list(pic, 3, pic_obj_value(pic->sDEFINE), pic_car(pic, formals), val), defs);
formals = pic_cdr(pic, formals); formals = pic_cdr(pic, formals);
} }
expr = pic_list_ref(pic, functor, 2); expr = pic_list_ref(pic, functor, 2);
pic_for_each (val, defs, it) { pic_for_each (val, defs, it) {
expr = pic_list3(pic, pic_obj_value(pic->sBEGIN), val, expr); expr = pic_list(pic, 3, pic_obj_value(pic->sBEGIN), val, expr);
} }
} }
exit: exit:
@ -106,7 +106,7 @@ analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, anal
scope->up = up; scope->up = up;
scope->depth = up ? up->depth + 1 : 0; scope->depth = up ? up->depth + 1 : 0;
scope->defer = pic_list1(pic, pic_nil_value(pic)); scope->defer = pic_list(pic, 1, pic_nil_value(pic));
} }
static void static void
@ -174,11 +174,11 @@ analyze_var(pic_state *pic, analyze_scope *scope, pic_sym *sym)
depth = find_var(pic, scope, sym); depth = find_var(pic, scope, sym);
if (depth == scope->depth) { if (depth == scope->depth) {
return pic_list2(pic, pic_obj_value(GREF), pic_obj_value(sym)); return pic_list(pic, 2, pic_obj_value(GREF), pic_obj_value(sym));
} else if (depth == 0) { } else if (depth == 0) {
return pic_list2(pic, pic_obj_value(LREF), pic_obj_value(sym)); return pic_list(pic, 2, pic_obj_value(LREF), pic_obj_value(sym));
} else { } else {
return pic_list3(pic, pic_obj_value(CREF), pic_int_value(pic, depth), pic_obj_value(sym)); return pic_list(pic, 3, pic_obj_value(CREF), pic_int_value(pic, depth), pic_obj_value(sym));
} }
} }
@ -187,7 +187,7 @@ analyze_defer(pic_state *pic, analyze_scope *scope, pic_value form)
{ {
pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value()); pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value());
pic_set_car(pic, scope->defer, pic_acons(pic, form, skel, pic_car(pic, scope->defer))); pic_set_car(pic, scope->defer, pic_cons(pic, pic_cons(pic, form, skel), pic_car(pic, scope->defer)));
return skel; return skel;
} }
@ -261,7 +261,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form)
analyzer_scope_destroy(pic, scope); analyzer_scope_destroy(pic, scope);
return pic_list6(pic, pic_obj_value(pic->sLAMBDA), rest, pic_obj_value(args), pic_obj_value(locals), pic_obj_value(captures), body); return pic_list(pic, 6, pic_obj_value(pic->sLAMBDA), rest, pic_obj_value(args), pic_obj_value(locals), pic_obj_value(captures), body);
} }
static pic_value static pic_value
@ -325,7 +325,7 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj)
return analyze_call(pic, scope, obj); return analyze_call(pic, scope, obj);
} }
default: default:
return pic_list2(pic, pic_obj_value(pic->sQUOTE), obj); return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), obj);
} }
} }

View File

@ -205,20 +205,29 @@ bool pic_eq_p(pic_state *, pic_value, pic_value);
bool pic_eqv_p(pic_state *, pic_value, pic_value); bool pic_eqv_p(pic_state *, pic_value, pic_value);
bool pic_equal_p(pic_state *, pic_value, pic_value); bool pic_equal_p(pic_state *, pic_value, pic_value);
/* pair */
pic_value pic_cons(pic_state *, pic_value car, pic_value cdr);
pic_value pic_car(pic_state *, pic_value pair);
pic_value pic_cdr(pic_state *, pic_value pair);
void pic_set_car(pic_state *, pic_value pair, pic_value car);
void pic_set_cdr(pic_state *, pic_value pair, pic_value cdr);
pic_value pic_caar(pic_state *, pic_value);
pic_value pic_cadr(pic_state *, pic_value);
pic_value pic_cdar(pic_state *, pic_value);
pic_value pic_cddr(pic_state *, pic_value);
/* list */ /* list */
pic_value pic_nil_value(pic_state *); pic_value pic_nil_value(pic_state *);
pic_value pic_cons(pic_state *, pic_value, pic_value);
PIC_INLINE pic_value pic_car(pic_state *, pic_value);
PIC_INLINE pic_value pic_cdr(pic_state *, pic_value);
void pic_set_car(pic_state *, pic_value, pic_value);
void pic_set_cdr(pic_state *, pic_value, pic_value);
bool pic_list_p(pic_state *, pic_value); bool pic_list_p(pic_state *, pic_value);
pic_value pic_make_list(pic_state *, int n, pic_value *argv);
pic_value pic_list(pic_state *, int n, ...); pic_value pic_list(pic_state *, int n, ...);
pic_value pic_vlist(pic_state *, int n, va_list); pic_value pic_vlist(pic_state *, int n, va_list);
pic_value pic_list_ref(pic_state *, pic_value, int); pic_value pic_list_ref(pic_state *, pic_value list, int i);
pic_value pic_list_tail(pic_state *, pic_value, int); void pic_list_set(pic_state *, pic_value list, int i, pic_value v);
void pic_list_set(pic_state *, pic_value, int, pic_value); pic_value pic_list_tail(pic_state *, pic_value list, int i);
int pic_length(pic_state *, pic_value); int pic_length(pic_state *, pic_value list);
pic_value pic_reverse(pic_state *, pic_value list);
pic_value pic_append(pic_state *, pic_value xs, pic_value ys);
/* vector */ /* vector */
pic_vec *pic_make_vec(pic_state *, int); pic_vec *pic_make_vec(pic_state *, int);
@ -263,7 +272,6 @@ int pic_str_hash(pic_state *, struct pic_string *);
#include "picrin/cont.h" #include "picrin/cont.h"
#include "picrin/macro.h" #include "picrin/macro.h"
#include "picrin/pair.h"
#include "picrin/port.h" #include "picrin/port.h"
void *pic_default_allocf(void *, void *, size_t); void *pic_default_allocf(void *, void *, size_t);
@ -331,6 +339,13 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *);
if (0) \ if (0) \
label: label:
#define pic_for_each(var, list, it) \
for (it = (list); ! pic_nil_p(pic, it); it = pic_cdr(pic, it)) \
if ((var = pic_car(pic, it)), true)
#define pic_push(pic, item, place) (place = pic_cons(pic, item, place))
#define pic_pop(pic, place) (place = pic_cdr(pic, place))
void pic_warnf(pic_state *, const char *, ...); void pic_warnf(pic_state *, const char *, ...);
struct pic_string *pic_get_backtrace(pic_state *); struct pic_string *pic_get_backtrace(pic_state *);
void pic_print_backtrace(pic_state *, xFILE *); void pic_print_backtrace(pic_state *, xFILE *);

View File

@ -36,6 +36,17 @@ pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *);
struct pic_string *pic_id_name(pic_state *, pic_id *); struct pic_string *pic_id_name(pic_state *, pic_id *);
/* pair */
struct pic_pair {
PIC_OBJECT_HEADER
pic_value car;
pic_value cdr;
};
#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o))
/* blob */ /* blob */
struct pic_blob { struct pic_blob {

View File

@ -1,87 +0,0 @@
/**
* See Copyright Notice in picrin.h
*/
#ifndef PICRIN_PAIR_H
#define PICRIN_PAIR_H
#if defined(__cplusplus)
extern "C" {
#endif
struct pic_pair {
PIC_OBJECT_HEADER
pic_value car;
pic_value cdr;
};
#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o))
PIC_INLINE pic_value
pic_car(pic_state *pic, pic_value obj)
{
struct pic_pair *pair;
if (! pic_pair_p(pic, obj)) {
pic_errorf(pic, "car: pair required, but got ~s", obj);
}
pair = pic_pair_ptr(obj);
return pair->car;
}
PIC_INLINE pic_value
pic_cdr(pic_state *pic, pic_value obj)
{
struct pic_pair *pair;
if (! pic_pair_p(pic, obj)) {
pic_errorf(pic, "cdr: pair required, but got ~s", obj);
}
pair = pic_pair_ptr(obj);
return pair->cdr;
}
pic_value pic_list1(pic_state *, pic_value);
pic_value pic_list2(pic_state *, pic_value, pic_value);
pic_value pic_list3(pic_state *, pic_value, pic_value, pic_value);
pic_value pic_list4(pic_state *, pic_value, pic_value, pic_value, pic_value);
pic_value pic_list5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value);
pic_value pic_list6(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value);
pic_value pic_list7(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value);
pic_value pic_list_by_array(pic_state *, int, pic_value *);
pic_value pic_make_list(pic_state *, int, pic_value);
#define pic_for_each(var, list, it) \
for (it = (list); ! pic_nil_p(pic, it); it = pic_cdr(pic, it)) \
if ((var = pic_car(pic, it)), true)
#define pic_push(pic, item, place) (place = pic_cons(pic, item, place))
#define pic_pop(pic, place) (place = pic_cdr(pic, place))
pic_value pic_reverse(pic_state *, pic_value);
pic_value pic_append(pic_state *, pic_value, pic_value);
pic_value pic_memq(pic_state *, pic_value key, pic_value list);
pic_value pic_memv(pic_state *, pic_value key, pic_value list);
pic_value pic_member(pic_state *, pic_value key, pic_value list, struct pic_proc * /* = NULL */);
pic_value pic_assq(pic_state *, pic_value key, pic_value assoc);
pic_value pic_assv(pic_state *, pic_value key, pic_value assoc);
pic_value pic_assoc(pic_state *, pic_value key, pic_value assoc, struct pic_proc * /* = NULL */);
pic_value pic_acons(pic_state *, pic_value key, pic_value val, pic_value assoc);
pic_value pic_caar(pic_state *, pic_value);
pic_value pic_cadr(pic_state *, pic_value);
pic_value pic_cdar(pic_state *, pic_value);
pic_value pic_cddr(pic_state *, pic_value);
pic_value pic_list_copy(pic_state *, pic_value);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -191,7 +191,7 @@ expand_defer(pic_state *pic, pic_value expr, pic_value deferred)
{ {
pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value()); pic_value skel = pic_cons(pic, pic_invalid_value(), pic_invalid_value());
pic_set_car(pic, deferred, pic_acons(pic, expr, skel, pic_car(pic, deferred))); pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred)));
return skel; return skel;
} }
@ -231,14 +231,14 @@ expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env)
pic_add_identifier(pic, pic_id_ptr(a), in); pic_add_identifier(pic, pic_id_ptr(a), in);
} }
deferred = pic_list1(pic, pic_nil_value(pic)); deferred = pic_list(pic, 1, pic_nil_value(pic));
formal = expand_list(pic, pic_list_ref(pic, expr, 1), in, deferred); formal = expand_list(pic, pic_list_ref(pic, expr, 1), in, deferred);
body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred); body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred);
expand_deferred(pic, deferred, in); expand_deferred(pic, deferred, in);
return pic_list3(pic, pic_obj_value(pic->sLAMBDA), formal, body); return pic_list(pic, 3, pic_obj_value(pic->sLAMBDA), formal, body);
} }
static pic_value static pic_value
@ -256,7 +256,7 @@ expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value def
} }
val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred); val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred);
return pic_list3(pic, pic_obj_value(pic->sDEFINE), pic_obj_value(uid), val); return pic_list(pic, 3, pic_obj_value(pic->sDEFINE), pic_obj_value(uid), val);
} }
static pic_value static pic_value
@ -350,7 +350,7 @@ pic_expand(pic_state *pic, pic_value expr, struct pic_env *env)
puts(""); puts("");
#endif #endif
deferred = pic_list1(pic, pic_nil_value(pic)); deferred = pic_list(pic, 1, pic_nil_value(pic));
v = expand(pic, expr, env, deferred); v = expand(pic, expr, env, deferred);

View File

@ -3,6 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/object.h"
pic_value pic_value
pic_cons(pic_state *pic, pic_value car, pic_value cdr) pic_cons(pic_state *pic, pic_value car, pic_value cdr)
@ -16,6 +17,32 @@ pic_cons(pic_state *pic, pic_value car, pic_value cdr)
return pic_obj_value(pair); return pic_obj_value(pair);
} }
pic_value
pic_car(pic_state *pic, pic_value obj)
{
struct pic_pair *pair;
if (! pic_pair_p(pic, obj)) {
pic_errorf(pic, "car: pair required, but got ~s", obj);
}
pair = pic_pair_ptr(obj);
return pair->car;
}
pic_value
pic_cdr(pic_state *pic, pic_value obj)
{
struct pic_pair *pair;
if (! pic_pair_p(pic, obj)) {
pic_errorf(pic, "cdr: pair required, but got ~s", obj);
}
pair = pic_pair_ptr(obj);
return pair->cdr;
}
void void
pic_set_car(pic_state *pic, pic_value obj, pic_value val) pic_set_car(pic_state *pic, pic_value obj, pic_value val)
{ {
@ -42,6 +69,30 @@ pic_set_cdr(pic_state *pic, pic_value obj, pic_value val)
pair->cdr = val; pair->cdr = val;
} }
pic_value
pic_caar(pic_state *pic, pic_value v)
{
return pic_car(pic, pic_car(pic, v));
}
pic_value
pic_cadr(pic_state *pic, pic_value v)
{
return pic_car(pic, pic_cdr(pic, v));
}
pic_value
pic_cdar(pic_state *pic, pic_value v)
{
return pic_cdr(pic, pic_car(pic, v));
}
pic_value
pic_cddr(pic_state *pic, pic_value v)
{
return pic_cdr(pic, pic_cdr(pic, v));
}
bool bool
pic_list_p(pic_state *pic, pic_value obj) pic_list_p(pic_state *pic, pic_value obj)
{ {
@ -73,112 +124,60 @@ pic_list_p(pic_state *pic, pic_value obj)
} }
pic_value pic_value
pic_list1(pic_state *pic, pic_value obj1) pic_make_list(pic_state *pic, int n, pic_value *argv)
{
return pic_cons(pic, obj1, pic_nil_value(pic));
}
pic_value
pic_list2(pic_state *pic, pic_value obj1, pic_value obj2)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value val;
val = pic_cons(pic, obj1, pic_list1(pic, obj2));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, val);
return val;
}
pic_value
pic_list3(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value val;
val = pic_cons(pic, obj1, pic_list2(pic, obj2, obj3));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, val);
return val;
}
pic_value
pic_list4(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value val;
val = pic_cons(pic, obj1, pic_list3(pic, obj2, obj3, obj4));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, val);
return val;
}
pic_value
pic_list5(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4, pic_value obj5)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value val;
val = pic_cons(pic, obj1, pic_list4(pic, obj2, obj3, obj4, obj5));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, val);
return val;
}
pic_value
pic_list6(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4, pic_value obj5, pic_value obj6)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value val;
val = pic_cons(pic, obj1, pic_list5(pic, obj2, obj3, obj4, obj5, obj6));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, val);
return val;
}
pic_value
pic_list7(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4, pic_value obj5, pic_value obj6, pic_value obj7)
{
size_t ai = pic_gc_arena_preserve(pic);
pic_value val;
val = pic_cons(pic, obj1, pic_list6(pic, obj2, obj3, obj4, obj5, obj6, obj7));
pic_gc_arena_restore(pic, ai);
pic_gc_protect(pic, val);
return val;
}
pic_value
pic_list_by_array(pic_state *pic, int c, pic_value *vs)
{
pic_value v;
v = pic_nil_value(pic);
while (c--) {
v = pic_cons(pic, vs[c], v);
}
return v;
}
pic_value
pic_make_list(pic_state *pic, int k, pic_value fill)
{ {
pic_value list; pic_value list;
int i; int i;
list = pic_nil_value(pic); list = pic_nil_value(pic);
for (i = 0; i < k; ++i) { for (i = n - 1; i >= 0; --i) {
list = pic_cons(pic, fill, list); list = pic_cons(pic, argv[i], list);
} }
return list;
}
pic_value
pic_list(pic_state *pic, int n, ...)
{
va_list ap;
pic_value list;
va_start(ap, n);
list = pic_vlist(pic, n, ap);
va_end(ap);
return list;
}
pic_value
pic_vlist(pic_state *pic, int n, va_list ap)
{
pic_value *argv = pic_alloca(pic, sizeof(pic_value) * n);
int i;
for (i = 0; i < n; ++i) {
argv[i] = va_arg(ap, pic_value);
}
return pic_make_list(pic, n, argv);
}
pic_value
pic_list_ref(pic_state *pic, pic_value list, int i)
{
return pic_car(pic, pic_list_tail(pic, list, i));
}
void
pic_list_set(pic_state *pic, pic_value list, int i, pic_value obj)
{
pic_pair_ptr(pic_list_tail(pic, list, i))->car = obj;
}
pic_value
pic_list_tail(pic_state *pic, pic_value list, int i)
{
while (i-- > 0) {
list = pic_cdr(pic, list);
}
return list; return list;
} }
@ -232,177 +231,6 @@ pic_append(pic_state *pic, pic_value xs, pic_value ys)
return ys; return ys;
} }
pic_value
pic_memq(pic_state *pic, pic_value key, pic_value list)
{
enter:
if (pic_nil_p(pic, list))
return pic_false_value(pic);
if (pic_eq_p(pic, key, pic_car(pic, list)))
return list;
list = pic_cdr(pic, list);
goto enter;
}
pic_value
pic_memv(pic_state *pic, pic_value key, pic_value list)
{
enter:
if (pic_nil_p(pic, list))
return pic_false_value(pic);
if (pic_eqv_p(pic, key, pic_car(pic, list)))
return list;
list = pic_cdr(pic, list);
goto enter;
}
pic_value
pic_member(pic_state *pic, pic_value key, pic_value list, struct pic_proc *compar)
{
enter:
if (pic_nil_p(pic, list))
return pic_false_value(pic);
if (compar == NULL) {
if (pic_equal_p(pic, key, pic_car(pic, list)))
return list;
} else {
if (pic_test(pic, pic_call(pic, compar, 2, key, pic_car(pic, list))))
return list;
}
list = pic_cdr(pic, list);
goto enter;
}
pic_value
pic_assq(pic_state *pic, pic_value key, pic_value assoc)
{
pic_value cell;
enter:
if (pic_nil_p(pic, assoc))
return pic_false_value(pic);
cell = pic_car(pic, assoc);
if (pic_eq_p(pic, key, pic_car(pic, cell)))
return cell;
assoc = pic_cdr(pic, assoc);
goto enter;
}
pic_value
pic_assv(pic_state *pic, pic_value key, pic_value assoc)
{
pic_value cell;
enter:
if (pic_nil_p(pic, assoc))
return pic_false_value(pic);
cell = pic_car(pic, assoc);
if (pic_eqv_p(pic, key, pic_car(pic, cell)))
return cell;
assoc = pic_cdr(pic, assoc);
goto enter;
}
pic_value
pic_assoc(pic_state *pic, pic_value key, pic_value assoc, struct pic_proc *compar)
{
pic_value cell;
enter:
if (pic_nil_p(pic, assoc))
return pic_false_value(pic);
cell = pic_car(pic, assoc);
if (compar == NULL) {
if (pic_equal_p(pic, key, pic_car(pic, cell)))
return cell;
} else {
if (pic_test(pic, pic_call(pic, compar, 2, key, pic_car(pic, cell))))
return cell;
}
assoc = pic_cdr(pic, assoc);
goto enter;
}
pic_value
pic_acons(pic_state *pic, pic_value key, pic_value val, pic_value assoc)
{
return pic_cons(pic, pic_cons(pic, key, val), assoc);
}
pic_value
pic_caar(pic_state *pic, pic_value v)
{
return pic_car(pic, pic_car(pic, v));
}
pic_value
pic_cadr(pic_state *pic, pic_value v)
{
return pic_car(pic, pic_cdr(pic, v));
}
pic_value
pic_cdar(pic_state *pic, pic_value v)
{
return pic_cdr(pic, pic_car(pic, v));
}
pic_value
pic_cddr(pic_state *pic, pic_value v)
{
return pic_cdr(pic, pic_cdr(pic, v));
}
pic_value
pic_list_tail(pic_state *pic, pic_value list, int i)
{
while (i-- > 0) {
list = pic_cdr(pic, list);
}
return list;
}
pic_value
pic_list_ref(pic_state *pic, pic_value list, int i)
{
return pic_car(pic, pic_list_tail(pic, list, i));
}
void
pic_list_set(pic_state *pic, pic_value list, int i, pic_value obj)
{
pic_pair_ptr(pic_list_tail(pic, list, i))->car = obj;
}
pic_value
pic_list_copy(pic_state *pic, pic_value obj)
{
if (pic_pair_p(pic, obj)) {
return pic_cons(pic, pic_car(pic, obj), pic_list_copy(pic, pic_cdr(pic, obj)));
}
else {
return obj;
}
}
static pic_value static pic_value
pic_pair_pair_p(pic_state *pic) pic_pair_pair_p(pic_state *pic)
{ {
@ -530,12 +358,16 @@ pic_pair_list_p(pic_state *pic)
static pic_value static pic_value
pic_pair_make_list(pic_state *pic) pic_pair_make_list(pic_state *pic)
{ {
int i; int k, i;
pic_value fill = pic_undef_value(pic); pic_value list, fill = pic_undef_value(pic);
pic_get_args(pic, "i|o", &i, &fill); pic_get_args(pic, "i|o", &k, &fill);
return pic_make_list(pic, i, fill); list = pic_nil_value(pic);
for (i = 0; i < k; ++i) {
list = pic_cons(pic, fill, list);
}
return list;
} }
static pic_value static pic_value
@ -546,7 +378,7 @@ pic_pair_list(pic_state *pic)
pic_get_args(pic, "*", &argc, &argv); pic_get_args(pic, "*", &argc, &argv);
return pic_list_by_array(pic, argc, argv); return pic_make_list(pic, argc, argv);
} }
static pic_value static pic_value
@ -627,11 +459,28 @@ pic_pair_list_set(pic_state *pic)
static pic_value static pic_value
pic_pair_list_copy(pic_state *pic) pic_pair_list_copy(pic_state *pic)
{ {
pic_value obj; pic_value list, head, tail, tmp;
pic_get_args(pic, "o", &obj); pic_get_args(pic, "o", &list);
return pic_list_copy(pic, obj); head = tail = pic_nil_value(pic);
while (pic_pair_p(pic, list)) {
tmp = pic_list(pic, 1, pic_car(pic, list));
if (! pic_nil_p(pic, tail)) {
pic_set_cdr(pic, tail, tmp);
}
tail = tmp;
if (pic_nil_p(pic, head)) {
head = tail;
}
list = pic_cdr(pic, list);
}
if (pic_nil_p(pic, tail)) {
return list;
}
pic_set_cdr(pic, tail, list);
return head;
} }
static pic_value static pic_value
@ -702,7 +551,13 @@ pic_pair_memq(pic_state *pic)
pic_get_args(pic, "oo", &key, &list); pic_get_args(pic, "oo", &key, &list);
return pic_memq(pic, key, list); while (! pic_nil_p(pic, list)) {
if (pic_eq_p(pic, key, pic_car(pic, list))) {
return list;
}
list = pic_cdr(pic, list);
}
return pic_false_value(pic);
} }
static pic_value static pic_value
@ -712,7 +567,13 @@ pic_pair_memv(pic_state *pic)
pic_get_args(pic, "oo", &key, &list); pic_get_args(pic, "oo", &key, &list);
return pic_memv(pic, key, list); while (! pic_nil_p(pic, list)) {
if (pic_eqv_p(pic, key, pic_car(pic, list))) {
return list;
}
list = pic_cdr(pic, list);
}
return pic_false_value(pic);
} }
static pic_value static pic_value
@ -723,38 +584,73 @@ pic_pair_member(pic_state *pic)
pic_get_args(pic, "oo|l", &key, &list, &proc); pic_get_args(pic, "oo|l", &key, &list, &proc);
return pic_member(pic, key, list, proc); while (! pic_nil_p(pic, list)) {
if (proc == NULL) {
if (pic_equal_p(pic, key, pic_car(pic, list)))
return list;
} else {
if (pic_test(pic, pic_call(pic, proc, 2, key, pic_car(pic, list))))
return list;
}
list = pic_cdr(pic, list);
}
return pic_false_value(pic);
} }
static pic_value static pic_value
pic_pair_assq(pic_state *pic) pic_pair_assq(pic_state *pic)
{ {
pic_value key, list; pic_value key, alist, cell;
pic_get_args(pic, "oo", &key, &list); pic_get_args(pic, "oo", &key, &alist);
return pic_assq(pic, key, list); while (! pic_nil_p(pic, alist)) {
cell = pic_car(pic, alist);
if (pic_eq_p(pic, key, pic_car(pic, cell))) {
return cell;
}
alist = pic_cdr(pic, alist);
}
return pic_false_value(pic);
} }
static pic_value static pic_value
pic_pair_assv(pic_state *pic) pic_pair_assv(pic_state *pic)
{ {
pic_value key, list; pic_value key, alist, cell;
pic_get_args(pic, "oo", &key, &list); pic_get_args(pic, "oo", &key, &alist);
return pic_assv(pic, key, list); while (! pic_nil_p(pic, alist)) {
cell = pic_car(pic, alist);
if (pic_eqv_p(pic, key, pic_car(pic, cell))) {
return cell;
}
alist = pic_cdr(pic, alist);
}
return pic_false_value(pic);
} }
static pic_value static pic_value
pic_pair_assoc(pic_state *pic) pic_pair_assoc(pic_state *pic)
{ {
struct pic_proc *proc = NULL; struct pic_proc *proc = NULL;
pic_value key, list; pic_value key, alist, cell;
pic_get_args(pic, "oo|l", &key, &list, &proc); pic_get_args(pic, "oo|l", &key, &alist, &proc);
return pic_assoc(pic, key, list, proc); while (! pic_nil_p(pic, alist)) {
cell = pic_car(pic, alist);
if (proc == NULL) {
if (pic_equal_p(pic, key, pic_car(pic, cell)))
return cell;
} else {
if (pic_test(pic, pic_call(pic, proc, 2, key, pic_car(pic, cell))))
return cell;
}
alist = pic_cdr(pic, alist);
}
return pic_false_value(pic);
} }
void void

View File

@ -146,13 +146,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c)
static pic_value static pic_value
read_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) read_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c))
{ {
return pic_list2(pic, pic_obj_value(pic->sQUOTE), read(pic, port, next(pic, port))); return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), read(pic, port, next(pic, port)));
} }
static pic_value static pic_value
read_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) read_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c))
{ {
return pic_list2(pic, pic_obj_value(pic->sQUASIQUOTE), read(pic, port, next(pic, port))); return pic_list(pic, 2, pic_obj_value(pic->sQUASIQUOTE), read(pic, port, next(pic, port)));
} }
static pic_value static pic_value
@ -164,19 +164,19 @@ read_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c))
tag = pic->sUNQUOTE_SPLICING; tag = pic->sUNQUOTE_SPLICING;
next(pic, port); next(pic, port);
} }
return pic_list2(pic, pic_obj_value(tag), read(pic, port, next(pic, port))); return pic_list(pic, 2, pic_obj_value(tag), read(pic, port, next(pic, port)));
} }
static pic_value static pic_value
read_syntax_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) read_syntax_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c))
{ {
return pic_list2(pic, pic_obj_value(pic->sSYNTAX_QUOTE), read(pic, port, next(pic, port))); return pic_list(pic, 2, pic_obj_value(pic->sSYNTAX_QUOTE), read(pic, port, next(pic, port)));
} }
static pic_value static pic_value
read_syntax_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) read_syntax_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c))
{ {
return pic_list2(pic, pic_obj_value(pic->sSYNTAX_QUASIQUOTE), read(pic, port, next(pic, port))); return pic_list(pic, 2, pic_obj_value(pic->sSYNTAX_QUASIQUOTE), read(pic, port, next(pic, port)));
} }
static pic_value static pic_value
@ -188,7 +188,7 @@ read_syntax_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c))
tag = pic->sSYNTAX_UNQUOTE_SPLICING; tag = pic->sSYNTAX_UNQUOTE_SPLICING;
next(pic, port); next(pic, port);
} }
return pic_list2(pic, pic_obj_value(tag), read(pic, port, next(pic, port))); return pic_list(pic, 2, pic_obj_value(tag), read(pic, port, next(pic, port)));
} }
static pic_value static pic_value
@ -223,7 +223,7 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c)
unsigned u = 0; unsigned u = 0;
if (! isdigit(c)) { if (! isdigit(c)) {
read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c)));
} }
u = c - '0'; u = c - '0';
@ -244,7 +244,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
int dpe = 0; /* the number of '.' or 'e' characters seen */ int dpe = 0; /* the number of '.' or 'e' characters seen */
if (! isdigit(c)) { if (! isdigit(c)) {
read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c)));
} }
buf[idx++] = (char )c; buf[idx++] = (char )c;
while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) { while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) {
@ -271,7 +271,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
break; break;
} }
if (! isdigit(peek(pic, port))) { if (! isdigit(peek(pic, port))) {
read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c)));
} }
while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) { while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) {
buf[idx++] = (char )next(pic, port); buf[idx++] = (char )next(pic, port);
@ -282,7 +282,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
pic_obj_value(pic_str_value(pic, (const char *)buf, ATOF_BUF_SIZE))); pic_obj_value(pic_str_value(pic, (const char *)buf, ATOF_BUF_SIZE)));
if (! isdelim(c)) if (! isdelim(c))
read_error(pic, "non-delimiter character given after number", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "non-delimiter character given after number", pic_list(pic, 1, pic_char_value(pic, c)));
buf[idx] = 0; buf[idx] = 0;
flt = PIC_CSTRING_TO_DOUBLE(buf); flt = PIC_CSTRING_TO_DOUBLE(buf);
@ -356,7 +356,7 @@ read_true(pic_state *pic, struct pic_port *port, int c)
read_error(pic, "unexpected character while reading #true", pic_nil_value(pic)); read_error(pic, "unexpected character while reading #true", pic_nil_value(pic));
} }
} else if (! isdelim(c)) { } else if (! isdelim(c)) {
read_error(pic, "non-delimiter character given after #t", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "non-delimiter character given after #t", pic_list(pic, 1, pic_char_value(pic, c)));
} }
return pic_true_value(pic); return pic_true_value(pic);
@ -370,7 +370,7 @@ read_false(pic_state *pic, struct pic_port *port, int c)
read_error(pic, "unexpected character while reading #false", pic_nil_value(pic)); read_error(pic, "unexpected character while reading #false", pic_nil_value(pic));
} }
} else if (! isdelim(c)) { } else if (! isdelim(c)) {
read_error(pic, "non-delimiter character given after #f", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "non-delimiter character given after #f", pic_list(pic, 1, pic_char_value(pic, c)));
} }
return pic_false_value(pic); return pic_false_value(pic);
@ -383,7 +383,7 @@ read_char(pic_state *pic, struct pic_port *port, int c)
if (! isdelim(peek(pic, port))) { if (! isdelim(peek(pic, port))) {
switch (c) { switch (c) {
default: read_error(pic, "unexpected character after char literal", pic_list1(pic, pic_char_value(pic, c))); default: read_error(pic, "unexpected character after char literal", pic_list(pic, 1, pic_char_value(pic, c)));
case 'a': c = '\a'; if (! expect(pic, port, "larm")) goto fail; break; case 'a': c = '\a'; if (! expect(pic, port, "larm")) goto fail; break;
case 'b': c = '\b'; if (! expect(pic, port, "ackspace")) goto fail; break; case 'b': c = '\b'; if (! expect(pic, port, "ackspace")) goto fail; break;
case 'd': c = 0x7F; if (! expect(pic, port, "elete")) goto fail; break; case 'd': c = 0x7F; if (! expect(pic, port, "elete")) goto fail; break;
@ -408,7 +408,7 @@ read_char(pic_state *pic, struct pic_port *port, int c)
return pic_char_value(pic, (char)c); return pic_char_value(pic, (char)c);
fail: fail:
read_error(pic, "unexpected character while reading character literal", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "unexpected character while reading character literal", pic_list(pic, 1, pic_char_value(pic, c)));
} }
static pic_value static pic_value
@ -471,7 +471,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c)
i = 0; i = 0;
while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') { while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') {
if (i >= sizeof HEX_BUF) if (i >= sizeof HEX_BUF)
read_error(pic, "expected ';'", pic_list1(pic, pic_char_value(pic, HEX_BUF[sizeof(HEX_BUF) - 1]))); read_error(pic, "expected ';'", pic_list(pic, 1, pic_char_value(pic, HEX_BUF[sizeof(HEX_BUF) - 1])));
} }
c = (char)strtol(HEX_BUF, NULL, 16); c = (char)strtol(HEX_BUF, NULL, 16);
break; break;
@ -505,11 +505,11 @@ read_blob(pic_state *pic, struct pic_port *port, int c)
} }
if (nbits != 8) { if (nbits != 8) {
read_error(pic, "unsupported bytevector bit width", pic_list1(pic, pic_int_value(pic, nbits))); read_error(pic, "unsupported bytevector bit width", pic_list(pic, 1, pic_int_value(pic, nbits)));
} }
if (c != '(') { if (c != '(') {
read_error(pic, "expected '(' character", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "expected '(' character", pic_list(pic, 1, pic_char_value(pic, c)));
} }
len = 0; len = 0;
@ -518,7 +518,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c)
while ((c = skip(pic, port, c)) != ')') { while ((c = skip(pic, port, c)) != ')') {
n = read_uinteger(pic, port, c); n = read_uinteger(pic, port, c);
if (n < 0 || (1 << nbits) <= n) { if (n < 0 || (1 << nbits) <= n) {
read_error(pic, "invalid element in bytevector literal", pic_list1(pic, pic_int_value(pic, n))); read_error(pic, "invalid element in bytevector literal", pic_list(pic, 1, pic_int_value(pic, n)));
} }
len += 1; len += 1;
dat = pic_realloc(pic, dat, len); dat = pic_realloc(pic, dat, len);
@ -542,7 +542,7 @@ read_undef_or_blob(pic_state *pic, struct pic_port *port, int c)
return pic_undef_value(pic); return pic_undef_value(pic);
} }
if (! isdigit(c)) { if (! isdigit(c)) {
read_error(pic, "expect #undefined or #u8(...), but illegal character given", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "expect #undefined or #u8(...), but illegal character given", pic_list(pic, 1, pic_char_value(pic, c)));
} }
return read_blob(pic, port, 'u'); return read_blob(pic, port, 'u');
} }
@ -666,7 +666,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i)
it = kh_get(read, h, i); it = kh_get(read, h, i);
if (it == kh_end(h)) { if (it == kh_end(h)) {
read_error(pic, "label of given index not defined", pic_list1(pic, pic_int_value(pic, i))); read_error(pic, "label of given index not defined", pic_list(pic, 1, pic_int_value(pic, i)));
} }
return kh_val(h, it); return kh_val(h, it);
} }
@ -706,7 +706,7 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c)
} }
if (pic->reader.dispatch[c] == NULL) { if (pic->reader.dispatch[c] == NULL) {
read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "invalid character at the seeker head", pic_list(pic, 1, pic_char_value(pic, c)));
} }
return pic->reader.dispatch[c](pic, port, c); return pic->reader.dispatch[c](pic, port, c);
@ -722,7 +722,7 @@ read_nullable(pic_state *pic, struct pic_port *port, int c)
} }
if (pic->reader.table[c] == NULL) { if (pic->reader.table[c] == NULL) {
read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(pic, c))); read_error(pic, "invalid character at the seeker head", pic_list(pic, 1, pic_char_value(pic, c)));
} }
return pic->reader.table[c](pic, port, c); return pic->reader.table[c](pic, port, c);