From 8a80f3805e4f9a391e53adf69c1ace2aac7ec687 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:06:54 +0900 Subject: [PATCH 001/119] update copyright --- extlib/benz/include/picrin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 88ed0176..784cb518 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2013-2014 Yuichi Nishiwaki and other picrin contributors. + * Copyright (c) 2013-2016 Picrin developers. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the From dc80bc5850a1a1528502d09307eb6c7ffa6d571b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:15:33 +0900 Subject: [PATCH 002/119] cleanup picrin.h --- extlib/benz/include/picrin.h | 107 ++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 784cb518..e7544ea6 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -39,6 +39,8 @@ extern "C" { typedef struct pic_state pic_state; +typedef void *(*pic_allocf)(void *, void *, size_t); + #include "picrin/type.h" #include "picrin/irep.h" #include "picrin/file.h" @@ -66,8 +68,6 @@ typedef struct { struct pic_context *up; } pic_callinfo; -typedef void *(*pic_allocf)(void *, void *, size_t); - struct pic_state { pic_allocf allocf; void *userdata; @@ -125,44 +125,25 @@ struct pic_state { char *native_stack_start; }; -typedef pic_value (*pic_func_t)(pic_state *); +pic_state *pic_open(pic_allocf, void *); +void pic_close(pic_state *); + +int pic_get_args(pic_state *, const char *, ...); void *pic_malloc(pic_state *, size_t); void *pic_realloc(pic_state *, void *, size_t); void *pic_calloc(pic_state *, size_t, size_t); void pic_free(pic_state *, void *); -struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); +typedef pic_value (*pic_func_t)(pic_state *); + void pic_gc_run(pic_state *); pic_value pic_gc_protect(pic_state *, pic_value); size_t pic_gc_arena_preserve(pic_state *); void pic_gc_arena_restore(pic_state *, size_t); -#define pic_void(exec) \ - pic_void_(PIC_GENSYM(ai), exec) -#define pic_void_(ai,exec) do { \ - size_t ai = pic_gc_arena_preserve(pic); \ - exec; \ - pic_gc_arena_restore(pic, ai); \ - } while (0) - -void *pic_default_allocf(void *, void *, size_t); -pic_state *pic_open(pic_allocf, void *); -void pic_close(pic_state *); void pic_add_feature(pic_state *, const char *); -int pic_get_args(pic_state *, const char *, ...); - -bool pic_eq_p(pic_value, pic_value); -bool pic_eqv_p(pic_value, pic_value); -bool pic_equal_p(pic_state *, pic_value, pic_value); - -pic_value pic_read(pic_state *, struct pic_port *); -pic_value pic_read_cstr(pic_state *, const char *); - -void pic_load(pic_state *, struct pic_port *); -void pic_load_cstr(pic_state *, const char *); - void pic_define(pic_state *, const char *, pic_value); void pic_defun(pic_state *, const char *, pic_func_t); void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *); @@ -179,6 +160,14 @@ pic_value pic_funcall1(pic_state *pic, struct pic_lib *, const char *, pic_value pic_value pic_funcall2(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value); pic_value pic_funcall3(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value, pic_value); +struct pic_lib *pic_make_library(pic_state *, pic_value); +struct pic_lib *pic_find_library(pic_state *, pic_value); +void pic_import(pic_state *, struct pic_lib *); +void pic_export(pic_state *, pic_sym *); + +PIC_NORETURN void pic_panic(pic_state *, const char *); +PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); + pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); pic_value pic_apply0(pic_state *, struct pic_proc *); pic_value pic_apply1(pic_state *, struct pic_proc *, pic_value); @@ -191,10 +180,47 @@ pic_value pic_apply_trampoline(pic_state *, struct pic_proc *, int, pic_value *) pic_value pic_apply_trampoline_list(pic_state *, struct pic_proc *, pic_value); pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); -struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); +bool pic_eq_p(pic_value, pic_value); +bool pic_eqv_p(pic_value, pic_value); +bool pic_equal_p(pic_state *, pic_value, pic_value); -struct pic_lib *pic_make_library(pic_state *, pic_value); -struct pic_lib *pic_find_library(pic_state *, pic_value); +#include "picrin/blob.h" +#include "picrin/cont.h" +#include "picrin/data.h" +#include "picrin/dict.h" +#include "picrin/error.h" +#include "picrin/lib.h" +#include "picrin/macro.h" +#include "picrin/pair.h" +#include "picrin/port.h" +#include "picrin/proc.h" +#include "picrin/record.h" +#include "picrin/string.h" +#include "picrin/symbol.h" +#include "picrin/vector.h" +#include "picrin/weak.h" + +/* extra stuff */ + +void *pic_default_allocf(void *, void *, size_t); + +struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); + +#define pic_void(exec) \ + pic_void_(PIC_GENSYM(ai), exec) +#define pic_void_(ai,exec) do { \ + size_t ai = pic_gc_arena_preserve(pic); \ + exec; \ + pic_gc_arena_restore(pic, ai); \ + } while (0) + +pic_value pic_read(pic_state *, struct pic_port *); +pic_value pic_read_cstr(pic_state *, const char *); + +void pic_load(pic_state *, struct pic_port *); +void pic_load_cstr(pic_state *, const char *); + +struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); #define pic_deflibrary(pic, spec) \ for (((assert(pic->prev_lib == NULL)), \ @@ -207,11 +233,6 @@ struct pic_lib *pic_find_library(pic_state *, pic_value); ((pic->lib = pic->prev_lib), \ (pic->prev_lib = NULL))) -void pic_import(pic_state *, struct pic_lib *); -void pic_export(pic_state *, pic_sym *); - -PIC_NORETURN void pic_panic(pic_state *, const char *); -PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); void pic_warnf(pic_state *, const char *, ...); pic_str *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); @@ -232,22 +253,6 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); # define pic_fdebug(pic,obj,file) pic_fwrite(pic,obj,file) #endif -#include "picrin/blob.h" -#include "picrin/cont.h" -#include "picrin/data.h" -#include "picrin/dict.h" -#include "picrin/error.h" -#include "picrin/lib.h" -#include "picrin/macro.h" -#include "picrin/pair.h" -#include "picrin/port.h" -#include "picrin/proc.h" -#include "picrin/record.h" -#include "picrin/string.h" -#include "picrin/symbol.h" -#include "picrin/vector.h" -#include "picrin/weak.h" - #if defined(__cplusplus) } #endif From 5946c3ab39ef4eaef59ff769522035d24dfa39ea Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:17:12 +0900 Subject: [PATCH 003/119] pic_gc_run -> pic_gc --- extlib/benz/gc.c | 6 +++--- extlib/benz/include/picrin.h | 2 +- extlib/benz/state.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index c5cbe77c..300b131f 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -686,7 +686,7 @@ gc_sweep_phase(pic_state *pic) } void -pic_gc_run(pic_state *pic) +pic_gc(pic_state *pic) { if (! pic->gc_enable) { return; @@ -702,12 +702,12 @@ pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt) struct pic_object *obj; #if GC_STRESS - pic_gc_run(pic); + pic_gc(pic); #endif obj = (struct pic_object *)heap_alloc(pic, size); if (obj == NULL) { - pic_gc_run(pic); + pic_gc(pic); obj = (struct pic_object *)heap_alloc(pic, size); if (obj == NULL) { heap_morecore(pic); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index e7544ea6..93322e57 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -137,10 +137,10 @@ void pic_free(pic_state *, void *); typedef pic_value (*pic_func_t)(pic_state *); -void pic_gc_run(pic_state *); pic_value pic_gc_protect(pic_state *, pic_value); size_t pic_gc_arena_preserve(pic_state *); void pic_gc_arena_restore(pic_state *, size_t); +void pic_gc(pic_state *); void pic_add_feature(pic_state *, const char *); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index a84dcf38..961b832b 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -391,7 +391,7 @@ pic_close(pic_state *pic) pic->libs = pic_nil_value(); /* free all heap objects */ - pic_gc_run(pic); + pic_gc(pic); #if 0 { From 549d939ce6d90944eb95f72c50455d1e0074a34a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:19:54 +0900 Subject: [PATCH 004/119] add pic_alloca --- extlib/benz/gc.c | 9 +++++++++ extlib/benz/include/picrin.h | 1 + 2 files changed, 10 insertions(+) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 300b131f..18906f40 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -696,6 +696,15 @@ pic_gc(pic_state *pic) gc_sweep_phase(pic); } +void * +pic_alloca(pic_state *pic, size_t n) +{ + static const pic_data_type t = { "pic_alloca", pic_free, 0 }; + + /* TODO: optimize */ + return pic_data_alloc(pic, &t, pic_malloc(pic, n))->data; +} + struct pic_object * pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt) { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 93322e57..99ab5562 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -137,6 +137,7 @@ void pic_free(pic_state *, void *); typedef pic_value (*pic_func_t)(pic_state *); +void *pic_alloca(pic_state *, size_t); pic_value pic_gc_protect(pic_state *, pic_value); size_t pic_gc_arena_preserve(pic_state *); void pic_gc_arena_restore(pic_state *, size_t); From 417a44b7862481d4743790d360b7236eb72d16a4 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:25:18 +0900 Subject: [PATCH 005/119] change the behavior of pic_define --- contrib/40.srfi/src/106.c | 3 +++ extlib/benz/include/picrin.h | 6 +----- extlib/benz/proc.c | 31 ++++++------------------------- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index b7328172..cafea041 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -399,6 +399,9 @@ pic_socket_call_with_socket(pic_state *pic) void pic_init_srfi_106(pic_state *pic) { +#define pic_defun_(pic, name, f) pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f))) +#define pic_define_(pic, name, v) pic_define(pic, pic->lib, name, v) + pic_deflibrary (pic, "(srfi 106)") { pic_defun_(pic, "socket?", pic_socket_socket_p); pic_defun_(pic, "make-socket", pic_socket_make_socket); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 99ab5562..84a54f71 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -145,14 +145,10 @@ void pic_gc(pic_state *); void pic_add_feature(pic_state *, const char *); -void pic_define(pic_state *, const char *, pic_value); void pic_defun(pic_state *, const char *, pic_func_t); void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *); -/* functions suffixed with '_' do not involve automatic export */ -void pic_define_(pic_state *, const char *, pic_value); -void pic_defun_(pic_state *, const char *, pic_func_t); -void pic_defvar_(pic_state *, const char *, pic_value, struct pic_proc *); +void pic_define(pic_state *, struct pic_lib *, const char *, pic_value); pic_value pic_ref(pic_state *, struct pic_lib *, const char *); void pic_set(pic_state *, struct pic_lib *, const char *, pic_value); pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, pic_value); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 2f3aa9ba..1298acae 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -900,53 +900,34 @@ pic_apply5(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2 } void -pic_define_(pic_state *pic, const char *name, pic_value val) +pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) { pic_sym *sym, *uid; sym = pic_intern_cstr(pic, name); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, pic->lib->env)) == NULL) { - uid = pic_add_identifier(pic, (pic_id *)sym, pic->lib->env); + if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) { + uid = pic_add_identifier(pic, (pic_id *)sym, lib->env); } else { if (pic_weak_has(pic, pic->globals, uid)) { pic_warnf(pic, "redefining variable: ~s", pic_obj_value(uid)); } } - pic_set(pic, pic->lib, name, val); -} - -void -pic_define(pic_state *pic, const char *name, pic_value val) -{ - pic_define_(pic, name, val); - pic_export(pic, pic_intern_cstr(pic, name)); -} - -void -pic_defun_(pic_state *pic, const char *name, pic_func_t cfunc) -{ - pic_define_(pic, name, pic_obj_value(pic_make_proc(pic, cfunc))); + pic_set(pic, lib, name, val); } void pic_defun(pic_state *pic, const char *name, pic_func_t cfunc) { - pic_defun_(pic, name, cfunc); + pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, cfunc))); pic_export(pic, pic_intern_cstr(pic, name)); } -void -pic_defvar_(pic_state *pic, const char *name, pic_value init, struct pic_proc *conv) -{ - pic_define_(pic, name, pic_obj_value(pic_make_var(pic, init, conv))); -} - void pic_defvar(pic_state *pic, const char *name, pic_value init, struct pic_proc *conv) { - pic_defvar_(pic, name, init, conv); + pic_define(pic, pic->lib, name, pic_obj_value(pic_make_var(pic, init, conv))); pic_export(pic, pic_intern_cstr(pic, name)); } From 69b660e9a37229a53e7a2dd8ea5a68b4ef0e24d8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:33:15 +0900 Subject: [PATCH 006/119] remove pic_funcallk and add pic_call --- extlib/benz/include/picrin.h | 8 ++--- extlib/benz/port.c | 2 +- extlib/benz/proc.c | 65 ++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 39 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 84a54f71..8ac61f8a 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -151,11 +151,7 @@ void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *); void pic_define(pic_state *, struct pic_lib *, const char *, pic_value); pic_value pic_ref(pic_state *, struct pic_lib *, const char *); void pic_set(pic_state *, struct pic_lib *, const char *, pic_value); -pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, pic_value); -pic_value pic_funcall0(pic_state *pic, struct pic_lib *, const char *); -pic_value pic_funcall1(pic_state *pic, struct pic_lib *, const char *, pic_value); -pic_value pic_funcall2(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value); -pic_value pic_funcall3(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value, pic_value); +pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, int, ...); struct pic_lib *pic_make_library(pic_state *, pic_value); struct pic_lib *pic_find_library(pic_state *, pic_value); @@ -165,6 +161,8 @@ void pic_export(pic_state *, pic_sym *); PIC_NORETURN void pic_panic(pic_state *, const char *); PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); +pic_value pic_call(pic_state *, struct pic_proc *, int, ...); +pic_value pic_vcall(pic_state *, struct pic_proc *, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); pic_value pic_apply0(pic_state *, struct pic_proc *); pic_value pic_apply1(pic_state *, struct pic_proc *, pic_value); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index e1056aba..fb6c32c4 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -172,7 +172,7 @@ pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) { \ pic_value obj; \ \ - obj = pic_funcall0(pic, pic->PICRIN_BASE, var); \ + obj = pic_funcall(pic, pic->PICRIN_BASE, var, 0); \ \ return pic_port_ptr(obj); \ } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 1298acae..f82bc8cf 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -783,6 +783,30 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) } VM_LOOP_END; } +pic_value +pic_call(pic_state *pic, struct pic_proc *proc, int n, ...) +{ + pic_value r; + va_list ap; + + va_start(ap, n); + r = pic_vcall(pic, proc, n, ap); + va_end(ap); + return r; +} + +pic_value +pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap) +{ + pic_value *args = pic_alloca(pic, sizeof(pic_value) * n); + int i; + + for (i = 0; i < n; ++i) { + args[i] = va_arg(ap, pic_value); + } + return pic_apply(pic, proc, n, args); +} + pic_value pic_apply_list(pic_state *pic, struct pic_proc *proc, pic_value list) { @@ -959,46 +983,21 @@ pic_set(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) vm_gset(pic, uid, val); } -static struct pic_proc * -pic_ref_proc(pic_state *pic, struct pic_lib *lib, const char *name) +pic_value +pic_funcall(pic_state *pic, struct pic_lib *lib, const char *name, int n, ...) { - pic_value proc; + pic_value proc, r; + va_list ap; proc = pic_ref(pic, lib, name); pic_assert_type(pic, proc, proc); - return pic_proc_ptr(proc); -} + va_start(ap, n); + r = pic_vcall(pic, pic_proc_ptr(proc), n, ap); + va_end(ap); -pic_value -pic_funcall(pic_state *pic, struct pic_lib *lib, const char *name, pic_value args) -{ - return pic_apply_list(pic, pic_ref_proc(pic, lib, name), args); -} - -pic_value -pic_funcall0(pic_state *pic, struct pic_lib *lib, const char *name) -{ - return pic_apply0(pic, pic_ref_proc(pic, lib, name)); -} - -pic_value -pic_funcall1(pic_state *pic, struct pic_lib *lib, const char *name, pic_value arg0) -{ - return pic_apply1(pic, pic_ref_proc(pic, lib, name), arg0); -} - -pic_value -pic_funcall2(pic_state *pic, struct pic_lib *lib, const char *name, pic_value arg0, pic_value arg1) -{ - return pic_apply2(pic, pic_ref_proc(pic, lib, name), arg0, arg1); -} - -pic_value -pic_funcall3(pic_state *pic, struct pic_lib *lib, const char *name, pic_value arg0, pic_value arg1, pic_value arg2) -{ - return pic_apply3(pic, pic_ref_proc(pic, lib, name), arg0, arg1, arg2); + return r; } void From e8a6f64dd56e5dc5164b947dbf7bec5c4b03f932 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 13:46:55 +0900 Subject: [PATCH 007/119] remove pic_applyk --- contrib/10.callcc/callcc.c | 2 +- contrib/40.srfi/src/106.c | 2 +- extlib/benz/cont.c | 14 +++++----- extlib/benz/dict.c | 4 +-- extlib/benz/error.c | 6 ++-- extlib/benz/eval.c | 2 +- extlib/benz/include/picrin.h | 7 +---- extlib/benz/macro.c | 6 ++-- extlib/benz/pair.c | 4 +-- extlib/benz/port.c | 2 +- extlib/benz/proc.c | 54 ------------------------------------ extlib/benz/var.c | 6 ++-- 12 files changed, 25 insertions(+), 84 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 49691a0c..6ba305ca 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -254,7 +254,7 @@ pic_callcc_full(pic_state *pic, struct pic_proc *proc) /* save the continuation object in proc */ pic_proc_env_set(pic, c, "cont", pic_obj_value(dat)); - return pic_apply1(pic, proc, pic_obj_value(c)); + return pic_call(pic, proc, 1, pic_obj_value(c)); } } diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index cafea041..e81a47e6 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -389,7 +389,7 @@ pic_socket_call_with_socket(pic_state *pic) sock = pic_socket_data_ptr(obj); ensure_socket_is_open(pic, sock); - result = pic_apply1(pic, proc, obj); + result = pic_call(pic, proc, 1, obj); socket_close(sock); diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index b2984fb0..16f7bf04 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -12,10 +12,10 @@ pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) if (here->depth < there->depth) { pic_wind(pic, here, there->prev); - pic_apply0(pic, there->in); + pic_call(pic, there->in, 0); } else { - pic_apply0(pic, there->out); + pic_call(pic, there->out, 0); pic_wind(pic, here->prev, there); } } @@ -27,7 +27,7 @@ pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, st pic_value val; if (in != NULL) { - pic_apply0(pic, in); /* enter */ + pic_call(pic, in, 0); /* enter */ } here = pic->cp; @@ -37,12 +37,12 @@ pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, st pic->cp->in = in; pic->cp->out = out; - val = pic_apply0(pic, thunk); + val = pic_call(pic, thunk, 0); pic->cp = here; if (out != NULL) { - pic_apply0(pic, out); /* exit */ + pic_call(pic, out, 0); /* exit */ } return val; @@ -146,7 +146,7 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) else { pic_value val; - val = pic_apply1(pic, proc, pic_obj_value(pic_make_cont(pic, &cont))); + val = pic_call(pic, proc, 1, pic_obj_value(pic_make_cont(pic, &cont))); pic->cc = pic->cc->prev; @@ -293,7 +293,7 @@ pic_cont_call_with_values(pic_state *pic) pic_get_args(pic, "ll", &producer, &consumer); - pic_apply0(pic, producer); + pic_call(pic, producer, 0); argc = pic_receive(pic, 0, NULL); args = pic_make_vec(pic, argc); diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 32af91f5..12cae194 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -166,7 +166,7 @@ pic_dict_dictionary_map(pic_state *pic) for (it = kh_begin(kh); it != kh_end(kh); ++it) { if (kh_exist(kh, it)) { - pic_push(pic, pic_apply1(pic, proc, pic_obj_value(kh_key(kh, it))), ret); + pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))), ret); } } @@ -187,7 +187,7 @@ pic_dict_dictionary_for_each(pic_state *pic) for (it = kh_begin(kh); it != kh_end(kh); ++it) { if (kh_exist(kh, it)) { - pic_apply1(pic, proc, pic_obj_value(kh_key(kh, it))); + pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))); } } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index e3427809..6286c8cc 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -58,7 +58,7 @@ pic_native_exception_handler(pic_state *pic) cont = pic_proc_ptr(pic_proc_env_ref(pic, self, "cont")); - pic_apply1(pic, cont, pic_false_value()); + pic_call(pic, cont, 1, pic_false_value()); PIC_UNREACHABLE(); } @@ -117,7 +117,7 @@ pic_raise_continuable(pic_state *pic, pic_value err) pic_gc_protect(pic, pic_obj_value(handler)); - v = pic_apply1(pic, handler, err); + v = pic_call(pic, handler, 1, err); pic_push_handler(pic, handler); @@ -156,7 +156,7 @@ pic_error_with_exception_handler(pic_state *pic) pic_push_handler(pic, handler); - val = pic_apply0(pic, thunk); + val = pic_call(pic, thunk, 0); pic_pop_handler(pic); diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index ffbafd6c..b825e077 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -892,7 +892,7 @@ pic_eval(pic_state *pic, pic_value program, struct pic_lib *lib) pic->lib = lib; pic_try { - r = pic_apply0(pic, pic_compile(pic, pic_expand(pic, program, lib->env))); + r = pic_call(pic, pic_compile(pic, pic_expand(pic, program, lib->env)), 0); } pic_catch { pic->lib = prev_lib; diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 8ac61f8a..456beef1 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -164,15 +164,10 @@ PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); pic_value pic_call(pic_state *, struct pic_proc *, int, ...); pic_value pic_vcall(pic_state *, struct pic_proc *, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); -pic_value pic_apply0(pic_state *, struct pic_proc *); -pic_value pic_apply1(pic_state *, struct pic_proc *, pic_value); -pic_value pic_apply2(pic_state *, struct pic_proc *, pic_value, pic_value); -pic_value pic_apply3(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value); -pic_value pic_apply4(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value, pic_value); -pic_value pic_apply5(pic_state *, struct pic_proc *, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_apply_list(pic_state *, struct pic_proc *, pic_value); pic_value pic_apply_trampoline(pic_state *, struct pic_proc *, int, pic_value *); pic_value pic_apply_trampoline_list(pic_state *, struct pic_proc *, pic_value); + pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); bool pic_eq_p(pic_value, pic_value); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index ad39e354..8646986d 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -155,7 +155,7 @@ expand_var(pic_state *pic, pic_id *id, struct pic_env *env, pic_value deferred) functor = pic_lookup_identifier(pic, id, env); if ((mac = find_macro(pic, functor)) != NULL) { - return expand(pic, pic_apply2(pic, mac, pic_obj_value(id), pic_obj_value(env)), env, deferred); + return expand(pic, pic_call(pic, mac, 2, pic_obj_value(id), pic_obj_value(env)), env, deferred); } return pic_obj_value(functor); } @@ -271,7 +271,7 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) uid = pic_add_identifier(pic, id, env); } - val = pic_apply0(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env))); + val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); if (! pic_proc_p(val)) { pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_identifier_name(pic, id)); } @@ -315,7 +315,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer } if ((mac = find_macro(pic, functor)) != NULL) { - return expand(pic, pic_apply2(pic, mac, expr, pic_obj_value(env)), env, deferred); + return expand(pic, pic_call(pic, mac, 2, expr, pic_obj_value(env)), env, deferred); } } return expand_list(pic, expr, env, deferred); diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 6bb698c7..66d5b073 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -274,7 +274,7 @@ pic_member(pic_state *pic, pic_value key, pic_value list, struct pic_proc *compa if (pic_equal_p(pic, key, pic_car(pic, list))) return list; } else { - if (pic_test(pic_apply2(pic, compar, key, pic_car(pic, list)))) + if (pic_test(pic_call(pic, compar, 2, key, pic_car(pic, list)))) return list; } @@ -333,7 +333,7 @@ pic_assoc(pic_state *pic, pic_value key, pic_value assoc, struct pic_proc *compa if (pic_equal_p(pic, key, pic_car(pic, cell))) return cell; } else { - if (pic_test(pic_apply2(pic, compar, key, pic_car(pic, cell)))) + if (pic_test(pic_call(pic, compar, 2, key, pic_car(pic, cell)))) return cell; } diff --git a/extlib/benz/port.c b/extlib/benz/port.c index fb6c32c4..311e5698 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -332,7 +332,7 @@ pic_port_call_with_port(pic_state *pic) pic_get_args(pic, "pl", &port, &proc); - value = pic_apply1(pic, proc, pic_obj_value(port)); + value = pic_call(pic, proc, 1, pic_obj_value(port)); pic_close_port(pic, port); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index f82bc8cf..1a1dad59 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -869,60 +869,6 @@ pic_apply_trampoline_list(pic_state *pic, struct pic_proc *proc, pic_value args) return pic_apply_trampoline(pic, proc, argc, argv->data); } -static pic_value -pic_va_apply(pic_state *pic, struct pic_proc *proc, int n, ...) -{ - pic_vec *args = pic_make_vec(pic, n); - va_list ap; - int i = 0; - - va_start(ap, n); - - while (i < n) { - args->data[i++] = va_arg(ap, pic_value); - } - - va_end(ap); - - return pic_apply(pic, proc, n, args->data); -} - -pic_value -pic_apply0(pic_state *pic, struct pic_proc *proc) -{ - return pic_va_apply(pic, proc, 0); -} - -pic_value -pic_apply1(pic_state *pic, struct pic_proc *proc, pic_value arg1) -{ - return pic_va_apply(pic, proc, 1, arg1); -} - -pic_value -pic_apply2(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2) -{ - return pic_va_apply(pic, proc, 2, arg1, arg2); -} - -pic_value -pic_apply3(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2, pic_value arg3) -{ - return pic_va_apply(pic, proc, 3, arg1, arg2, arg3); -} - -pic_value -pic_apply4(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2, pic_value arg3, pic_value arg4) -{ - return pic_va_apply(pic, proc, 4, arg1, arg2, arg3, arg4); -} - -pic_value -pic_apply5(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2, pic_value arg3, pic_value arg4, pic_value arg5) -{ - return pic_va_apply(pic, proc, 5, arg1, arg2, arg3, arg4, arg5); -} - void pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) { diff --git a/extlib/benz/var.c b/extlib/benz/var.c index e7c3d55a..1c0ad304 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -8,7 +8,7 @@ static pic_value var_conv(pic_state *pic, struct pic_proc *var, pic_value val) { if (pic_proc_env_has(pic, var, "conv") != 0) { - return pic_apply1(pic, pic_proc_ptr(pic_proc_env_ref(pic, var, "conv")), val); + return pic_call(pic, pic_proc_ptr(pic_proc_env_ref(pic, var, "conv")), 1, val); } return val; } @@ -67,7 +67,7 @@ pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv) pic_proc_env_set(pic, var, "conv", pic_obj_value(conv)); } - pic_apply1(pic, var, init); + pic_call(pic, var, 1, init); return var; } @@ -93,7 +93,7 @@ pic_var_with_parameter(pic_state *pic) pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable); - val = pic_apply0(pic, body); + val = pic_call(pic, body, 0); pic->ptable = pic_cdr(pic, pic->ptable); From 2632956b6ec7621792602daf18fc2d3730ba4843 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 17:13:38 +0900 Subject: [PATCH 008/119] fix regression --- extlib/benz/symbol.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index a3965dce..0c81ddfa 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -24,6 +24,8 @@ pic_intern(pic_state *pic, pic_str *str) return sym; } + kh_val(h, it) = pic->sQUOTE; /* dummy */ + sym = (pic_sym *)pic_obj_alloc(pic, sizeof(pic_sym), PIC_TT_SYMBOL); sym->str = str; kh_val(h, it) = sym; From 8814469eac246eef08887716408044b2e5453ffa Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 17:14:33 +0900 Subject: [PATCH 009/119] pic_apply_trampoline -> pic_applyk --- contrib/10.callcc/callcc.c | 4 +- extlib/benz/cont.c | 2 +- extlib/benz/include/picrin.h | 4 +- extlib/benz/pair.c | 20 ++++---- extlib/benz/proc.c | 91 ++++++++++++------------------------ extlib/benz/string.c | 4 +- extlib/benz/vector.c | 4 +- 7 files changed, 50 insertions(+), 79 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 6ba305ca..60347935 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -273,6 +273,7 @@ pic_callcc_callcc(pic_state *pic) else { struct pic_proc *c; struct pic_data *dat; + pic_value args[1]; c = pic_make_proc(pic, cont_call); @@ -281,7 +282,8 @@ pic_callcc_callcc(pic_state *pic) /* save the continuation object in proc */ pic_proc_env_set(pic, c, "cont", pic_obj_value(dat)); - return pic_apply_trampoline_list(pic, proc, pic_list1(pic, pic_obj_value(c))); + args[0] = pic_obj_value(c); + return pic_applyk(pic, proc, 1, args); } } diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 16f7bf04..5966a904 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -300,7 +300,7 @@ pic_cont_call_with_values(pic_state *pic) pic_receive(pic, argc, args->data); - return pic_apply_trampoline(pic, consumer, argc, args->data); + return pic_applyk(pic, consumer, argc, args->data); } void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 456beef1..adabd21e 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -164,9 +164,7 @@ PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); pic_value pic_call(pic_state *, struct pic_proc *, int, ...); pic_value pic_vcall(pic_state *, struct pic_proc *, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); -pic_value pic_apply_list(pic_state *, struct pic_proc *, pic_value); -pic_value pic_apply_trampoline(pic_state *, struct pic_proc *, int, pic_value *); -pic_value pic_apply_trampoline_list(pic_state *, struct pic_proc *, pic_value); +pic_value pic_applyk(pic_state *, struct pic_proc *, int, pic_value *); pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 66d5b073..09138a80 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -639,29 +639,29 @@ pic_pair_map(pic_state *pic) { struct pic_proc *proc; int argc, i; - pic_value *args; - pic_value arg, ret; + pic_value *args, *arg_list, ret; pic_get_args(pic, "l*", &proc, &argc, &args); if (argc == 0) pic_errorf(pic, "map: wrong number of arguments (1 for at least 2)"); + arg_list = pic_alloca(pic, sizeof(pic_value) * argc); + ret = pic_nil_value(); do { - arg = pic_nil_value(); for (i = 0; i < argc; ++i) { if (! pic_pair_p(args[i])) { break; } - pic_push(pic, pic_car(pic, args[i]), arg); + arg_list[i] = pic_car(pic, args[i]); args[i] = pic_cdr(pic, args[i]); } if (i != argc) { break; } - pic_push(pic, pic_apply_list(pic, proc, pic_reverse(pic, arg)), ret); + pic_push(pic, pic_apply(pic, proc, i, arg_list), ret); } while (1); return pic_reverse(pic, ret); @@ -672,24 +672,24 @@ pic_pair_for_each(pic_state *pic) { struct pic_proc *proc; int argc, i; - pic_value *args; - pic_value arg; + pic_value *args, *arg_list; pic_get_args(pic, "l*", &proc, &argc, &args); + arg_list = pic_alloca(pic, sizeof(pic_value) * argc); + do { - arg = pic_nil_value(); for (i = 0; i < argc; ++i) { if (! pic_pair_p(args[i])) { break; } - pic_push(pic, pic_car(pic, args[i]), arg); + arg_list[i] = pic_car(pic, args[i]); args[i] = pic_cdr(pic, args[i]); } if (i != argc) { break; } - pic_apply_list(pic, proc, pic_reverse(pic, arg)); + pic_apply(pic, proc, i, arg_list); } while (1); return pic_undef_value(); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 1a1dad59..935a876f 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -784,49 +784,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) } pic_value -pic_call(pic_state *pic, struct pic_proc *proc, int n, ...) -{ - pic_value r; - va_list ap; - - va_start(ap, n); - r = pic_vcall(pic, proc, n, ap); - va_end(ap); - return r; -} - -pic_value -pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap) -{ - pic_value *args = pic_alloca(pic, sizeof(pic_value) * n); - int i; - - for (i = 0; i < n; ++i) { - args[i] = va_arg(ap, pic_value); - } - return pic_apply(pic, proc, n, args); -} - -pic_value -pic_apply_list(pic_state *pic, struct pic_proc *proc, pic_value list) -{ - int n, i = 0; - pic_vec *args; - pic_value x, it; - - n = pic_length(pic, list); - - args = pic_make_vec(pic, n); - - pic_for_each (x, list, it) { - args->data[i++] = x; - } - - return pic_apply(pic, proc, n, args->data); -} - -pic_value -pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args) +pic_applyk(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args) { pic_value *sp; pic_callinfo *ci; @@ -855,18 +813,27 @@ pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, int argc, pic_value } pic_value -pic_apply_trampoline_list(pic_state *pic, struct pic_proc *proc, pic_value args) +pic_call(pic_state *pic, struct pic_proc *proc, int n, ...) { - int i, argc = pic_length(pic, args); - pic_value val, it; - pic_vec *argv = pic_make_vec(pic, argc); + pic_value r; + va_list ap; - i = 0; - pic_for_each (val, args, it) { - argv->data[i++] = val; + va_start(ap, n); + r = pic_vcall(pic, proc, n, ap); + va_end(ap); + return r; +} + +pic_value +pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap) +{ + pic_value *args = pic_alloca(pic, sizeof(pic_value) * n); + int i; + + for (i = 0; i < n; ++i) { + args[i] = va_arg(ap, pic_value); } - - return pic_apply_trampoline(pic, proc, argc, argv->data); + return pic_apply(pic, proc, n, args); } void @@ -1043,9 +1010,8 @@ static pic_value pic_proc_apply(pic_state *pic) { struct pic_proc *proc; - pic_value *args; - int argc; - pic_value arg_list; + pic_value *args, *arg_list; + int argc, n, i; pic_get_args(pic, "l*", &proc, &argc, &args); @@ -1053,12 +1019,17 @@ pic_proc_apply(pic_state *pic) pic_errorf(pic, "apply: wrong number of arguments"); } - arg_list = args[--argc]; - while (argc--) { - arg_list = pic_cons(pic, args[argc], arg_list); - } + n = argc - 1 + pic_length(pic, args[argc - 1]); - return pic_apply_trampoline_list(pic, proc, arg_list); + arg_list = pic_alloca(pic, sizeof(pic_value) * n); + for (i = 0; i < argc - 1; ++i) { + arg_list[i] = args[i]; + } + while (i < n) { + arg_list[i] = pic_list_ref(pic, args[argc - 1], i - argc + 1); + i++; + } + return pic_applyk(pic, proc, n, arg_list); } void diff --git a/extlib/benz/string.c b/extlib/benz/string.c index fb3d3ae9..dc5dcb9e 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -578,7 +578,7 @@ pic_str_string_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } - val = pic_apply_list(pic, proc, vals); + val = pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); pic_assert_type(pic, val, char); buf[i] = pic_char(val); @@ -623,7 +623,7 @@ pic_str_string_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } - pic_apply_list(pic, proc, vals); + pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); } return pic_undef_value(); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index c06d0023..09ed95fb 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -240,7 +240,7 @@ pic_vec_vector_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } - vec->data[i] = pic_apply_list(pic, proc, vals); + vec->data[i] = pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); } return pic_obj_value(vec); @@ -269,7 +269,7 @@ pic_vec_vector_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } - pic_apply_list(pic, proc, vals); + pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); } return pic_undef_value(); From f70dd4d3767b2c552fb3c7edd7f0b8ea73528394 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 14 Feb 2016 22:23:14 +0900 Subject: [PATCH 010/119] add pic_closure_ref, pic_closure_set, and pic_lambda --- contrib/10.callcc/callcc.c | 21 ++---- contrib/40.srfi/src/106.c | 2 +- extlib/benz/cont.c | 18 ++--- extlib/benz/error.c | 2 +- extlib/benz/gc.c | 5 +- extlib/benz/include/picrin.h | 8 ++- extlib/benz/include/picrin/error.h | 3 +- extlib/benz/include/picrin/proc.h | 10 +-- extlib/benz/port.c | 2 +- extlib/benz/proc.c | 101 +++++++++++++++++++---------- extlib/benz/var.c | 23 +++---- extlib/benz/weak.c | 9 +-- 12 files changed, 105 insertions(+), 99 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 60347935..9c7c76d3 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -218,14 +218,13 @@ restore_cont(pic_state *pic, struct pic_fullcont *cont) PIC_NORETURN static pic_value cont_call(pic_state *pic) { - struct pic_proc *self; int argc; pic_value *argv; struct pic_fullcont *cont; - pic_get_args(pic, "&*", &self, &argc, &argv); + pic_get_args(pic, "*", &argc, &argv); - cont = pic_data_ptr(pic_proc_env_ref(pic, self, "cont"))->data; + cont = pic_data_ptr(pic_closure_ref(pic, 0))->data; cont->results = pic_list_by_array(pic, argc, argv); /* execute guard handlers */ @@ -245,14 +244,9 @@ pic_callcc_full(pic_state *pic, struct pic_proc *proc) } else { struct pic_proc *c; - struct pic_data *dat; - - c = pic_make_proc(pic, cont_call); - - dat = pic_data_alloc(pic, &cont_type, cont); /* save the continuation object in proc */ - pic_proc_env_set(pic, c, "cont", pic_obj_value(dat)); + c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); return pic_call(pic, proc, 1, pic_obj_value(c)); } @@ -272,15 +266,10 @@ pic_callcc_callcc(pic_state *pic) } else { struct pic_proc *c; - struct pic_data *dat; pic_value args[1]; - c = pic_make_proc(pic, cont_call); - - dat = pic_data_alloc(pic, &cont_type, cont); - /* save the continuation object in proc */ - pic_proc_env_set(pic, c, "cont", pic_obj_value(dat)); + c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); args[0] = pic_obj_value(c); return pic_applyk(pic, proc, 1, args); @@ -288,7 +277,7 @@ pic_callcc_callcc(pic_state *pic) } #define pic_redefun(pic, lib, name, func) \ - pic_set(pic, lib, name, pic_obj_value(pic_make_proc(pic, func))) + pic_set(pic, lib, name, pic_obj_value(pic_make_proc(pic, func, 0, NULL))) void pic_init_callcc(pic_state *pic) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index e81a47e6..88921795 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -399,7 +399,7 @@ pic_socket_call_with_socket(pic_state *pic) void pic_init_srfi_106(pic_state *pic) { -#define pic_defun_(pic, name, f) pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f))) +#define pic_defun_(pic, name, f) pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))) #define pic_define_(pic, name, v) pic_define(pic, pic->lib, name, v) pic_deflibrary (pic, "(srfi 106)") { diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 5966a904..6b54c11a 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -82,18 +82,20 @@ pic_load_point(pic_state *pic, struct pic_cont *cont) pic->cc = cont->prev; } +#define CV_ID 0 +#define CV_ESCAPE 1 + static pic_value cont_call(pic_state *pic) { - struct pic_proc *self; int argc; pic_value *argv; int id; struct pic_cont *cc, *cont; - pic_get_args(pic, "&*", &self, &argc, &argv); + pic_get_args(pic, "*", &argc, &argv); - id = pic_int(pic_proc_env_ref(pic, self, "id")); + id = pic_int(pic_closure_ref(pic, CV_ID)); /* check if continuation is alive */ for (cc = pic->cc; cc != NULL; cc = cc->prev) { @@ -105,7 +107,7 @@ cont_call(pic_state *pic) pic_errorf(pic, "calling dead escape continuation"); } - cont = pic_data_ptr(pic_proc_env_ref(pic, self, "escape"))->data; + cont = pic_data_ptr(pic_closure_ref(pic, CV_ESCAPE))->data; cont->results = pic_list_by_array(pic, argc, argv); pic_load_point(pic, cont); @@ -120,15 +122,9 @@ pic_make_cont(pic_state *pic, struct pic_cont *cont) { static const pic_data_type cont_type = { "cont", NULL, NULL }; struct pic_proc *c; - struct pic_data *e; - - c = pic_make_proc(pic, cont_call); - - e = pic_data_alloc(pic, &cont_type, cont); /* save the escape continuation in proc */ - pic_proc_env_set(pic, c, "escape", pic_obj_value(e)); - pic_proc_env_set(pic, c, "id", pic_int_value(cont->id)); + c = pic_lambda(pic, cont_call, 2, pic_int_value(cont->id), pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); return c; } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 6286c8cc..2c68b5f1 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -56,7 +56,7 @@ pic_native_exception_handler(pic_state *pic) pic->err = err; - cont = pic_proc_ptr(pic_proc_env_ref(pic, self, "cont")); + cont = pic_proc_ptr(pic_closure_ref(pic, 0)); pic_call(pic, cont, 1, pic_false_value()); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 18906f40..4acea528 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -302,8 +302,9 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) LOOP(obj->u.proc.u.i.cxt); } } else { - if (obj->u.proc.u.f.env) { - LOOP(obj->u.proc.u.f.env); + int i; + for (i = 0; i < obj->u.proc.u.f.localc; ++i) { + gc_mark(pic, obj->u.proc.locals[i]); } } break; diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index adabd21e..20016a46 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -151,6 +151,8 @@ void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *); void pic_define(pic_state *, struct pic_lib *, const char *, pic_value); pic_value pic_ref(pic_state *, struct pic_lib *, const char *); void pic_set(pic_state *, struct pic_lib *, const char *, pic_value); +pic_value pic_closure_ref(pic_state *, int); +void pic_closure_set(pic_state *, int, pic_value); pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, int, ...); struct pic_lib *pic_make_library(pic_state *, pic_value); @@ -161,13 +163,13 @@ void pic_export(pic_state *, pic_sym *); PIC_NORETURN void pic_panic(pic_state *, const char *); PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); +struct pic_proc *pic_lambda(pic_state *, pic_func_t, int, ...); +struct pic_proc *pic_vlambda(pic_state *, pic_func_t, int, va_list); pic_value pic_call(pic_state *, struct pic_proc *, int, ...); pic_value pic_vcall(pic_state *, struct pic_proc *, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); pic_value pic_applyk(pic_state *, struct pic_proc *, int, pic_value *); -pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); - bool pic_eq_p(pic_value, pic_value); bool pic_eqv_p(pic_value, pic_value); bool pic_equal_p(pic_state *, pic_value, pic_value); @@ -208,6 +210,8 @@ pic_value pic_read_cstr(pic_state *, const char *); void pic_load(pic_state *, struct pic_port *); void pic_load_cstr(pic_state *, const char *); +pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); + struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); #define pic_deflibrary(pic, spec) \ diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h index ecf59dda..540cae65 100644 --- a/extlib/benz/include/picrin/error.h +++ b/extlib/benz/include/picrin/error.h @@ -35,8 +35,7 @@ struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_value if (PIC_SETJMP(pic, cont.jmp) == 0) { \ extern pic_value pic_native_exception_handler(pic_state *); \ struct pic_proc *handler; \ - handler = pic_make_proc(pic, pic_native_exception_handler); \ - pic_proc_env_set(pic, handler, "cont", pic_obj_value(pic_make_cont(pic, &cont))); \ + handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_obj_value(pic_make_cont(pic, &cont))); \ do { \ pic_push_handler(pic, handler); #define pic_catch_(label) \ diff --git a/extlib/benz/include/picrin/proc.h b/extlib/benz/include/picrin/proc.h index e5cc2bdb..b11f543d 100644 --- a/extlib/benz/include/picrin/proc.h +++ b/extlib/benz/include/picrin/proc.h @@ -26,13 +26,14 @@ struct pic_proc { union { struct { pic_func_t func; - struct pic_dict *env; + int localc; } f; struct { struct pic_irep *irep; struct pic_context *cxt; } i; } u; + pic_value locals[1]; }; #define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) @@ -44,14 +45,9 @@ struct pic_proc { #define pic_context_p(o) (pic_type(o) == PIC_TT_CXT) #define pic_context_ptr(o) ((struct pic_context *)pic_ptr(o)) -struct pic_proc *pic_make_proc(pic_state *, pic_func_t); +struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); -struct pic_dict *pic_proc_env(pic_state *, struct pic_proc *); -bool pic_proc_env_has(pic_state *, struct pic_proc *, const char *); -pic_value pic_proc_env_ref(pic_state *, struct pic_proc *, const char *); -void pic_proc_env_set(pic_state *, struct pic_proc *, const char *, pic_value); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 311e5698..504b48ad 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -163,7 +163,7 @@ pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) port->file = file; port->flags = dir | PIC_PORT_TEXT | PIC_PORT_OPEN; - pic_defvar(pic, name, pic_obj_value(port), pic_make_proc(pic, pic_assert_port)); + pic_defvar(pic, name, pic_obj_value(port), pic_make_proc(pic, pic_assert_port, 0, NULL)); } #define DEFINE_STANDARD_PORT_ACCESSOR(name, var) \ diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 935a876f..8d209418 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -200,7 +200,7 @@ vm_push_cxt(pic_state *pic) { pic_callinfo *ci = pic->ci; - ci->cxt = (struct pic_context *)pic_obj_alloc(pic, sizeof(struct pic_context) + sizeof(pic_value) * ci->regc, PIC_TT_CXT); + ci->cxt = (struct pic_context *)pic_obj_alloc(pic, offsetof(struct pic_context, storage) + sizeof(pic_value) * ci->regc, PIC_TT_CXT); ci->cxt->up = ci->up; ci->cxt->regc = ci->regc; ci->cxt->regs = ci->regs; @@ -836,6 +836,30 @@ pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap) return pic_apply(pic, proc, n, args); } +struct pic_proc * +pic_lambda(pic_state *pic, pic_func_t f, int n, ...) +{ + struct pic_proc *proc; + va_list ap; + + va_start(ap, n); + proc = pic_vlambda(pic, f, n, ap); + va_end(ap); + return proc; +} + +struct pic_proc * +pic_vlambda(pic_state *pic, pic_func_t f, int n, va_list ap) +{ + pic_value *env = pic_alloca(pic, sizeof(pic_value) * n); + int i; + + for (i = 0; i < n; ++i) { + env[i] = va_arg(ap, pic_value); + } + return pic_make_proc(pic, f, n, env); +} + void pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) { @@ -855,9 +879,9 @@ pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) } void -pic_defun(pic_state *pic, const char *name, pic_func_t cfunc) +pic_defun(pic_state *pic, const char *name, pic_func_t f) { - pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, cfunc))); + pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))); pic_export(pic, pic_intern_cstr(pic, name)); } @@ -896,6 +920,36 @@ pic_set(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) vm_gset(pic, uid, val); } +pic_value +pic_closure_ref(pic_state *pic, int n) +{ + struct pic_proc *self; + + self = pic_proc_ptr(GET_OPERAND(pic, 0)); + + assert(pic_proc_func_p(self)); + + if (n < 0 || self->u.f.localc <= n) { + pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); + } + return pic_proc_ptr(GET_OPERAND(pic, 0))->locals[n]; +} + +void +pic_closure_set(pic_state *pic, int n, pic_value v) +{ + struct pic_proc *self; + + self = pic_proc_ptr(GET_OPERAND(pic, 0)); + + assert(pic_proc_func_p(self)); + + if (n < 0 || self->u.f.localc <= n) { + pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); + } + pic_proc_ptr(GET_OPERAND(pic, 0))->locals[n] = v; +} + pic_value pic_funcall(pic_state *pic, struct pic_lib *lib, const char *name, int n, ...) { @@ -943,14 +997,18 @@ pic_irep_decref(pic_state *pic, struct pic_irep *irep) } struct pic_proc * -pic_make_proc(pic_state *pic, pic_func_t func) +pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) { struct pic_proc *proc; + int i; - proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC); + proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals) + sizeof(pic_value) * n, PIC_TT_PROC); proc->tag = PIC_PROC_TAG_FUNC; proc->u.f.func = func; - proc->u.f.env = NULL; + proc->u.f.localc = n; + for (i = 0; i < n; ++i) { + proc->locals[i] = env[i]; + } return proc; } @@ -959,7 +1017,7 @@ pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cx { struct pic_proc *proc; - proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC); + proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals), PIC_TT_PROC); proc->tag = PIC_PROC_TAG_IREP; proc->u.i.irep = irep; proc->u.i.cxt = cxt; @@ -967,35 +1025,6 @@ pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cx return proc; } -struct pic_dict * -pic_proc_env(pic_state *pic, struct pic_proc *proc) -{ - assert(pic_proc_func_p(proc)); - - if (! proc->u.f.env) { - proc->u.f.env = pic_make_dict(pic); - } - return proc->u.f.env; -} - -bool -pic_proc_env_has(pic_state *pic, struct pic_proc *proc, const char *key) -{ - return pic_dict_has(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key)); -} - -pic_value -pic_proc_env_ref(pic_state *pic, struct pic_proc *proc, const char *key) -{ - return pic_dict_ref(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key)); -} - -void -pic_proc_env_set(pic_state *pic, struct pic_proc *proc, const char *key, pic_value val) -{ - pic_dict_set(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key), val); -} - static pic_value pic_proc_proc_p(pic_state *pic) { diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 1c0ad304..77e6c233 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -4,15 +4,6 @@ #include "picrin.h" -static pic_value -var_conv(pic_state *pic, struct pic_proc *var, pic_value val) -{ - if (pic_proc_env_has(pic, var, "conv") != 0) { - return pic_call(pic, pic_proc_ptr(pic_proc_env_ref(pic, var, "conv")), 1, val); - } - return val; -} - static pic_value var_get(pic_state *pic, struct pic_proc *var) { @@ -52,7 +43,13 @@ var_call(pic_state *pic) if (n == 0) { return var_get(pic, self); } else { - return var_set(pic, self, var_conv(pic, self, val)); + pic_value conv; + + conv = pic_closure_ref(pic, 0); + if (! pic_false_p(conv)) { + val = pic_call(pic, pic_proc_ptr(conv), 1, val); + } + return var_set(pic, self, val); } } @@ -60,12 +57,12 @@ struct pic_proc * pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv) { struct pic_proc *var; - - var = pic_make_proc(pic, var_call); + pic_value c = pic_false_value(); if (conv != NULL) { - pic_proc_env_set(pic, var, "conv", pic_obj_value(conv)); + c = pic_obj_value(conv); } + var = pic_lambda(pic, var_call, 1, c); pic_call(pic, var, 1, init); diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 196846b8..635bd260 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -115,7 +115,7 @@ weak_call(pic_state *pic) pic_errorf(pic, "attempted to set a non-object key '~s' in an ephemeron", key); } - weak = pic_weak_ptr(pic_proc_env_ref(pic, self, "weak")); + weak = pic_weak_ptr(pic_closure_ref(pic, 0)); if (n == 1) { return weak_get(pic, weak, pic_obj_ptr(key)); @@ -127,16 +127,11 @@ weak_call(pic_state *pic) static pic_value pic_weak_make_ephemeron(pic_state *pic) { - struct pic_weak *weak; struct pic_proc *proc; pic_get_args(pic, ""); - weak = pic_make_weak(pic); - - proc = pic_make_proc(pic, weak_call); - - pic_proc_env_set(pic, proc, "weak", pic_obj_value(weak)); + proc = pic_lambda(pic, weak_call, 1, pic_obj_value(pic_make_weak(pic))); return pic_obj_value(proc); } From 780df6275b3f9d08292be3b337ce3e3e7d79598e Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 00:06:12 +0900 Subject: [PATCH 011/119] move more decls to picrin.h --- extlib/benz/include/picrin.h | 87 ++++++++++++++++++++++ extlib/benz/include/picrin/blob.h | 1 - extlib/benz/include/picrin/data.h | 1 - extlib/benz/include/picrin/dict.h | 9 --- extlib/benz/include/picrin/pair.h | 9 --- extlib/benz/include/picrin/proc.h | 1 - extlib/benz/include/picrin/string.h | 7 -- extlib/benz/include/picrin/symbol.h | 7 -- extlib/benz/include/picrin/type.h | 108 ++++++++-------------------- extlib/benz/include/picrin/vector.h | 3 - extlib/benz/include/picrin/weak.h | 8 --- extlib/benz/number.c | 53 ++++++++++++++ 12 files changed, 171 insertions(+), 123 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 20016a46..8d6f035c 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -156,6 +156,7 @@ void pic_closure_set(pic_state *, int, pic_value); pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, int, ...); struct pic_lib *pic_make_library(pic_state *, pic_value); +void pic_in_library(pic_state *, pic_value); struct pic_lib *pic_find_library(pic_state *, pic_value); void pic_import(pic_state *, struct pic_lib *); void pic_export(pic_state *, pic_sym *); @@ -170,10 +171,96 @@ pic_value pic_vcall(pic_state *, struct pic_proc *, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); pic_value pic_applyk(pic_state *, struct pic_proc *, int, pic_value *); +int pic_int(pic_value); +double pic_float(pic_value); +char pic_char(pic_value); +bool pic_bool(pic_value); +/* const char *pic_str(pic_state *, pic_value, int *len); */ +/* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ +void *pic_data(pic_state *, pic_value); + +pic_value pic_undef_value(); +pic_value pic_int_value(int); +pic_value pic_float_value(double); +pic_value pic_char_value(char c); +pic_value pic_true_value(); +pic_value pic_false_value(); +pic_value pic_bool_value(bool); + +#define pic_int_p(v) (pic_vtype(v) == PIC_VTYPE_INT) +#define pic_float_p(v) (pic_vtype(v) == PIC_VTYPE_FLOAT) +#define pic_char_p(v) (pic_vtype(v) == PIC_VTYPE_CHAR) +#define pic_true_p(v) (pic_vtype(v) == PIC_VTYPE_TRUE) +#define pic_false_p(v) (pic_vtype(v) == PIC_VTYPE_FALSE) +#define pic_str_p(v) (pic_type(v) == PIC_TT_STRING) +#define pic_blob_p(v) (pic_type(v) == PIC_TT_BLOB) +#define pic_proc_p(o) (pic_type(o) == PIC_TT_PROC) +#define pic_data_p(o) (pic_type(o) == PIC_TT_DATA) +#define pic_nil_p(v) (pic_vtype(v) == PIC_VTYPE_NIL) +#define pic_pair_p(v) (pic_type(v) == PIC_TT_PAIR) +#define pic_vec_p(v) (pic_type(v) == PIC_TT_VECTOR) +#define pic_dict_p(v) (pic_type(v) == PIC_TT_DICT) +#define pic_weak_p(v) (pic_type(v) == PIC_TT_WEAK) +#define pic_sym_p(v) (pic_type(v) == PIC_TT_SYMBOL) +#define pic_undef_p(v) (pic_vtype(v) == PIC_VTYPE_UNDEF) + +enum pic_tt pic_type(pic_value); +const char *pic_type_repr(enum pic_tt); + bool pic_eq_p(pic_value, pic_value); bool pic_eqv_p(pic_value, pic_value); bool pic_equal_p(pic_state *, pic_value, pic_value); +/* list */ +pic_value pic_nil_value(); +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_value); +pic_value pic_list(pic_state *, int n, ...); +pic_value pic_vlist(pic_state *, int n, va_list); +pic_value pic_list_ref(pic_state *, pic_value, int); +void pic_list_set(pic_state *, pic_value, int, pic_value); +int pic_length(pic_state *, pic_value); + +/* vector */ +pic_vec *pic_make_vec(pic_state *, int); +pic_value pic_vec_ref(pic_state *, pic_vec *, int); +void pic_vec_set(pic_state *, pic_vec *, int, pic_value); +int pic_vec_len(pic_state *, pic_vec *); + +/* dictionary */ +struct pic_dict *pic_make_dict(pic_state *); +pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_sym *); +void pic_dict_set(pic_state *, struct pic_dict *, pic_sym *, pic_value); +void pic_dict_del(pic_state *, struct pic_dict *, pic_sym *); +bool pic_dict_has(pic_state *, struct pic_dict *, pic_sym *); +int pic_dict_size(pic_state *, struct pic_dict *); + +/* ephemeron */ +struct pic_weak *pic_make_weak(pic_state *); +pic_value pic_weak_ref(pic_state *, struct pic_weak *, void *); +void pic_weak_set(pic_state *, struct pic_weak *, void *, pic_value); +void pic_weak_del(pic_state *, struct pic_weak *, void *); +bool pic_weak_has(pic_state *, struct pic_weak *, void *); + +/* symbol */ +pic_sym *pic_intern(pic_state *, pic_str *); +#define pic_intern_str(pic,s,i) pic_intern(pic, pic_make_str(pic, (s), (i))) +#define pic_intern_cstr(pic,s) pic_intern(pic, pic_make_cstr(pic, (s))) +#define pic_intern_lit(pic,lit) pic_intern(pic, pic_make_lit(pic, lit)) +const char *pic_symbol_name(pic_state *, pic_sym *); + +/* string */ +int pic_str_len(pic_str *); +char pic_str_ref(pic_state *, pic_str *, int); +pic_str *pic_str_cat(pic_state *, pic_str *, pic_str *); +pic_str *pic_str_sub(pic_state *, pic_str *, int, int); +int pic_str_cmp(pic_state *, pic_str *, pic_str *); +int pic_str_hash(pic_state *, pic_str *); + #include "picrin/blob.h" #include "picrin/cont.h" #include "picrin/data.h" diff --git a/extlib/benz/include/picrin/blob.h b/extlib/benz/include/picrin/blob.h index 6e6b5532..2440c27f 100644 --- a/extlib/benz/include/picrin/blob.h +++ b/extlib/benz/include/picrin/blob.h @@ -15,7 +15,6 @@ struct pic_blob { int len; }; -#define pic_blob_p(v) (pic_type(v) == PIC_TT_BLOB) #define pic_blob_ptr(v) ((struct pic_blob *)pic_ptr(v)) struct pic_blob *pic_make_blob(pic_state *, int); diff --git a/extlib/benz/include/picrin/data.h b/extlib/benz/include/picrin/data.h index 4b9b27d1..a7f03580 100644 --- a/extlib/benz/include/picrin/data.h +++ b/extlib/benz/include/picrin/data.h @@ -21,7 +21,6 @@ struct pic_data { void *data; }; -#define pic_data_p(o) (pic_type(o) == PIC_TT_DATA) #define pic_data_ptr(o) ((struct pic_data *)pic_ptr(o)) PIC_INLINE bool pic_data_type_p(const pic_value obj, const pic_data_type *type) { diff --git a/extlib/benz/include/picrin/dict.h b/extlib/benz/include/picrin/dict.h index be7c675e..0aaa11aa 100644 --- a/extlib/benz/include/picrin/dict.h +++ b/extlib/benz/include/picrin/dict.h @@ -16,23 +16,14 @@ struct pic_dict { khash_t(dict) hash; }; -#define pic_dict_p(v) (pic_type(v) == PIC_TT_DICT) #define pic_dict_ptr(v) ((struct pic_dict *)pic_ptr(v)) -struct pic_dict *pic_make_dict(pic_state *); - #define pic_dict_for_each(sym, dict, it) \ pic_dict_for_each_help(sym, (&(dict)->hash), it) #define pic_dict_for_each_help(sym, h, it) \ for (it = kh_begin(h); it != kh_end(h); ++it) \ if ((sym = kh_key(h, it)), kh_exist(h, it)) -pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_sym *); -void pic_dict_set(pic_state *, struct pic_dict *, pic_sym *, pic_value); -void pic_dict_del(pic_state *, struct pic_dict *, pic_sym *); -int pic_dict_size(pic_state *, struct pic_dict *); -bool pic_dict_has(pic_state *, struct pic_dict *, pic_sym *); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/pair.h b/extlib/benz/include/picrin/pair.h index d8f7a675..3cbf428f 100644 --- a/extlib/benz/include/picrin/pair.h +++ b/extlib/benz/include/picrin/pair.h @@ -15,7 +15,6 @@ struct pic_pair { pic_value cdr; }; -#define pic_pair_p(v) (pic_type(v) == PIC_TT_PAIR) #define pic_pair_ptr(o) ((struct pic_pair *)pic_ptr(o)) PIC_INLINE pic_value @@ -44,11 +43,6 @@ pic_cdr(pic_state *pic, pic_value obj) return pair->cdr; } -pic_value pic_cons(pic_state *, pic_value, 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_value); 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); @@ -66,7 +60,6 @@ pic_value pic_make_list(pic_state *, int, pic_value); #define pic_push(pic, item, place) (place = pic_cons(pic, item, place)) #define pic_pop(pic, place) (place = pic_cdr(pic, place)) -int pic_length(pic_state *, pic_value); pic_value pic_reverse(pic_state *, pic_value); pic_value pic_append(pic_state *, pic_value, pic_value); @@ -86,8 +79,6 @@ pic_value pic_cdar(pic_state *, pic_value); pic_value pic_cddr(pic_state *, pic_value); pic_value pic_list_tail(pic_state *, pic_value, int); -pic_value pic_list_ref(pic_state *, pic_value, int); -void pic_list_set(pic_state *, pic_value, int, pic_value); pic_value pic_list_copy(pic_state *, pic_value); #if defined(__cplusplus) diff --git a/extlib/benz/include/picrin/proc.h b/extlib/benz/include/picrin/proc.h index b11f543d..36baeba1 100644 --- a/extlib/benz/include/picrin/proc.h +++ b/extlib/benz/include/picrin/proc.h @@ -39,7 +39,6 @@ struct pic_proc { #define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) #define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) -#define pic_proc_p(o) (pic_type(o) == PIC_TT_PROC) #define pic_proc_ptr(o) ((struct pic_proc *)pic_ptr(o)) #define pic_context_p(o) (pic_type(o) == PIC_TT_CXT) diff --git a/extlib/benz/include/picrin/string.h b/extlib/benz/include/picrin/string.h index f3343e2d..f5c32314 100644 --- a/extlib/benz/include/picrin/string.h +++ b/extlib/benz/include/picrin/string.h @@ -17,19 +17,12 @@ struct pic_string { void pic_rope_incref(pic_state *, struct pic_rope *); void pic_rope_decref(pic_state *, struct pic_rope *); -#define pic_str_p(v) (pic_type(v) == PIC_TT_STRING) #define pic_str_ptr(o) ((struct pic_string *)pic_ptr(o)) pic_str *pic_make_str(pic_state *, const char *, int); #define pic_make_cstr(pic, cstr) pic_make_str(pic, (cstr), strlen(cstr)) #define pic_make_lit(pic, lit) pic_make_str(pic, "" lit, -((int)sizeof lit - 1)) -char pic_str_ref(pic_state *, pic_str *, int); -int pic_str_len(pic_str *); -pic_str *pic_str_cat(pic_state *, pic_str *, pic_str *); -pic_str *pic_str_sub(pic_state *, pic_str *, int, int); -int pic_str_cmp(pic_state *, pic_str *, pic_str *); -int pic_str_hash(pic_state *, pic_str *); const char *pic_str_cstr(pic_state *, pic_str *); pic_str *pic_format(pic_state *, const char *, ...); diff --git a/extlib/benz/include/picrin/symbol.h b/extlib/benz/include/picrin/symbol.h index 6581bbd5..b390a7cc 100644 --- a/extlib/benz/include/picrin/symbol.h +++ b/extlib/benz/include/picrin/symbol.h @@ -23,20 +23,13 @@ struct pic_id { } u; }; -#define pic_sym_p(v) (pic_type(v) == PIC_TT_SYMBOL) #define pic_sym_ptr(v) ((pic_sym *)pic_ptr(v)) #define pic_id_p(v) (pic_type(v) == PIC_TT_ID || pic_type(v) == PIC_TT_SYMBOL) #define pic_id_ptr(v) ((pic_id *)pic_ptr(v)) -pic_sym *pic_intern(pic_state *, pic_str *); -#define pic_intern_str(pic,s,i) pic_intern(pic, pic_make_str(pic, (s), (i))) -#define pic_intern_cstr(pic,s) pic_intern(pic, pic_make_cstr(pic, (s))) -#define pic_intern_lit(pic,lit) pic_intern(pic, pic_make_lit(pic, lit)) - pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); -const char *pic_symbol_name(pic_state *, pic_sym *); const char *pic_identifier_name(pic_state *, pic_id *); #if defined(__cplusplus) diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index fb99fc86..a3e9ed78 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -66,7 +66,11 @@ pic_int(pic_value v) return u.i; } -#define pic_char(v) ((v) & 0xfffffffful) +static inline char +pic_char(pic_value v) +{ + return v & 0xfffffffful; +} #elif PIC_WORD_BOXING @@ -124,9 +128,23 @@ typedef struct { #define pic_vtype(v) ((v).type) #define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) -#define pic_float(v) ((v).u.f) -#define pic_int(v) ((v).u.i) -#define pic_char(v) ((v).u.c) +PIC_INLINE double +pic_float(pic_value v) +{ + return v.u.f; +} + +PIC_INLINE int +pic_int(pic_value v) +{ + return v.u.i; +} + +PIC_INLINE char +pic_char(pic_value v) +{ + return v.u.c; +} #endif @@ -191,21 +209,11 @@ typedef struct pic_blob pic_blob; #define pic_obj_p(v) (pic_vtype(v) == PIC_VTYPE_HEAP) #define pic_obj_ptr(v) ((struct pic_object *)pic_ptr(v)) -#define pic_nil_p(v) (pic_vtype(v) == PIC_VTYPE_NIL) -#define pic_true_p(v) (pic_vtype(v) == PIC_VTYPE_TRUE) -#define pic_false_p(v) (pic_vtype(v) == PIC_VTYPE_FALSE) -#define pic_undef_p(v) (pic_vtype(v) == PIC_VTYPE_UNDEF) #define pic_invalid_p(v) (pic_vtype(v) == PIC_VTYPE_INVALID) -#define pic_float_p(v) (pic_vtype(v) == PIC_VTYPE_FLOAT) -#define pic_int_p(v) (pic_vtype(v) == PIC_VTYPE_INT) -#define pic_char_p(v) (pic_vtype(v) == PIC_VTYPE_CHAR) #define pic_eof_p(v) (pic_vtype(v) == PIC_VTYPE_EOF) #define pic_test(v) (! pic_false_p(v)) -PIC_INLINE enum pic_tt pic_type(pic_value); -PIC_INLINE const char *pic_type_repr(enum pic_tt); - #define pic_assert_type(pic, v, type) \ if (! pic_##type##_p(v)) { \ pic_errorf(pic, "expected " #type ", but got ~s", v); \ @@ -217,19 +225,8 @@ pic_valid_int(double v) return INT_MIN <= v && v <= INT_MAX; } -PIC_INLINE pic_value pic_nil_value(); -PIC_INLINE pic_value pic_true_value(); -PIC_INLINE pic_value pic_false_value(); -PIC_INLINE pic_value pic_bool_value(bool); -PIC_INLINE pic_value pic_undef_value(); PIC_INLINE pic_value pic_invalid_value(); PIC_INLINE pic_value pic_obj_value(void *); -PIC_INLINE pic_value pic_float_value(double); -PIC_INLINE pic_value pic_int_value(int); -PIC_INLINE pic_value pic_char_value(char c); - -PIC_INLINE bool pic_eq_p(pic_value, pic_value); -PIC_INLINE bool pic_eqv_p(pic_value, pic_value); PIC_INLINE enum pic_tt pic_type(pic_value v) @@ -538,58 +535,15 @@ pic_eqv_p(pic_value x, pic_value y) #endif -#define pic_define_aop(name, op, guard) \ - PIC_INLINE pic_value \ - name(pic_state *pic, pic_value a, pic_value b) \ - { \ - PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - double f; \ - if (pic_int_p(a) && pic_int_p(b)) { \ - f = (double)pic_int(a) op (double)pic_int(b); \ - return (INT_MIN <= f && f <= INT_MAX && guard) \ - ? pic_int_value((int)f) \ - : pic_float_value(f); \ - } else if (pic_float_p(a) && pic_float_p(b)) { \ - return pic_float_value(pic_float(a) op pic_float(b)); \ - } else if (pic_int_p(a) && pic_float_p(b)) { \ - return pic_float_value(pic_int(a) op pic_float(b)); \ - } else if (pic_float_p(a) && pic_int_p(b)) { \ - return pic_float_value(pic_float(a) op pic_int(b)); \ - } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ - } \ - PIC_UNREACHABLE(); \ - } - -pic_define_aop(pic_add, +, true) -pic_define_aop(pic_sub, -, true) -pic_define_aop(pic_mul, *, true) -pic_define_aop(pic_div, /, f == (int)f) - -#define pic_define_cmp(name, op) \ - PIC_INLINE bool \ - name(pic_state *pic, pic_value a, pic_value b) \ - { \ - PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - if (pic_int_p(a) && pic_int_p(b)) { \ - return pic_int(a) op pic_int(b); \ - } else if (pic_float_p(a) && pic_float_p(b)) { \ - return pic_float(a) op pic_float(b); \ - } else if (pic_int_p(a) && pic_float_p(b)) { \ - return pic_int(a) op pic_float(b); \ - } else if (pic_float_p(a) && pic_int_p(b)) { \ - return pic_float(a) op pic_int(b); \ - } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ - } \ - PIC_UNREACHABLE(); \ - } - -pic_define_cmp(pic_eq, ==) -pic_define_cmp(pic_lt, <) -pic_define_cmp(pic_le, <=) -pic_define_cmp(pic_gt, >) -pic_define_cmp(pic_ge, >=) +pic_value pic_add(pic_state *, pic_value, pic_value); +pic_value pic_sub(pic_state *, pic_value, pic_value); +pic_value pic_mul(pic_state *, pic_value, pic_value); +pic_value pic_div(pic_state *, pic_value, pic_value); +bool pic_eq(pic_state *, pic_value, pic_value); +bool pic_lt(pic_state *, pic_value, pic_value); +bool pic_le(pic_state *, pic_value, pic_value); +bool pic_gt(pic_state *, pic_value, pic_value); +bool pic_ge(pic_state *, pic_value, pic_value); #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/vector.h b/extlib/benz/include/picrin/vector.h index d18f16e2..e3ac6fd0 100644 --- a/extlib/benz/include/picrin/vector.h +++ b/extlib/benz/include/picrin/vector.h @@ -15,11 +15,8 @@ struct pic_vector { int len; }; -#define pic_vec_p(v) (pic_type(v) == PIC_TT_VECTOR) #define pic_vec_ptr(o) ((struct pic_vector *)pic_ptr(o)) -pic_vec *pic_make_vec(pic_state *, int); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/weak.h b/extlib/benz/include/picrin/weak.h index ac938c88..1b502365 100644 --- a/extlib/benz/include/picrin/weak.h +++ b/extlib/benz/include/picrin/weak.h @@ -17,16 +17,8 @@ struct pic_weak { struct pic_weak *prev; /* for GC */ }; -#define pic_weak_p(v) (pic_type(v) == PIC_TT_WEAK) #define pic_weak_ptr(v) ((struct pic_weak *)pic_ptr(v)) -struct pic_weak *pic_make_weak(pic_state *); - -pic_value pic_weak_ref(pic_state *, struct pic_weak *, void *); -void pic_weak_set(pic_state *, struct pic_weak *, void *, pic_value); -void pic_weak_del(pic_state *, struct pic_weak *, void *); -bool pic_weak_has(pic_state *, struct pic_weak *, void *); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 4f60a7ca..035528b3 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -54,6 +54,59 @@ pic_number_exact(pic_state *pic) return pic_int_value((int)f); } +#define pic_define_aop(name, op, guard) \ + pic_value \ + name(pic_state *pic, pic_value a, pic_value b) \ + { \ + PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ + double f; \ + if (pic_int_p(a) && pic_int_p(b)) { \ + f = (double)pic_int(a) op (double)pic_int(b); \ + return (INT_MIN <= f && f <= INT_MAX && guard) \ + ? pic_int_value((int)f) \ + : pic_float_value(f); \ + } else if (pic_float_p(a) && pic_float_p(b)) { \ + return pic_float_value(pic_float(a) op pic_float(b)); \ + } else if (pic_int_p(a) && pic_float_p(b)) { \ + return pic_float_value(pic_int(a) op pic_float(b)); \ + } else if (pic_float_p(a) && pic_int_p(b)) { \ + return pic_float_value(pic_float(a) op pic_int(b)); \ + } else { \ + pic_errorf(pic, #name ": non-number operand given"); \ + } \ + PIC_UNREACHABLE(); \ + } + +pic_define_aop(pic_add, +, true) +pic_define_aop(pic_sub, -, true) +pic_define_aop(pic_mul, *, true) +pic_define_aop(pic_div, /, f == (int)f) + +#define pic_define_cmp(name, op) \ + bool \ + name(pic_state *pic, pic_value a, pic_value b) \ + { \ + PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ + if (pic_int_p(a) && pic_int_p(b)) { \ + return pic_int(a) op pic_int(b); \ + } else if (pic_float_p(a) && pic_float_p(b)) { \ + return pic_float(a) op pic_float(b); \ + } else if (pic_int_p(a) && pic_float_p(b)) { \ + return pic_int(a) op pic_float(b); \ + } else if (pic_float_p(a) && pic_int_p(b)) { \ + return pic_float(a) op pic_int(b); \ + } else { \ + pic_errorf(pic, #name ": non-number operand given"); \ + } \ + PIC_UNREACHABLE(); \ + } + +pic_define_cmp(pic_eq, ==) +pic_define_cmp(pic_lt, <) +pic_define_cmp(pic_le, <=) +pic_define_cmp(pic_gt, >) +pic_define_cmp(pic_ge, >=) + #define DEFINE_CMP(op) \ static pic_value \ pic_number_##op(pic_state *pic) \ From a92e70a610fca3046e7dbca4ca0a43096ae66499 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 00:20:49 +0900 Subject: [PATCH 012/119] remove typedefs of struct pic_string and struct pic_blob --- contrib/20.r7rs/src/mutable-string.c | 10 +++--- contrib/20.r7rs/src/system.c | 2 +- contrib/30.regexp/src/regexp.c | 4 +-- extlib/benz/blob.c | 14 ++++---- extlib/benz/bool.c | 2 +- extlib/benz/debug.c | 4 +-- extlib/benz/error.c | 6 ++-- extlib/benz/include/picrin.h | 18 +++++----- extlib/benz/include/picrin/error.h | 4 +-- extlib/benz/include/picrin/macro.h | 4 +-- extlib/benz/include/picrin/string.h | 8 ++--- extlib/benz/include/picrin/symbol.h | 2 +- extlib/benz/include/picrin/type.h | 2 -- extlib/benz/lib.c | 2 +- extlib/benz/macro.c | 4 +-- extlib/benz/number.c | 2 +- extlib/benz/port.c | 6 ++-- extlib/benz/proc.c | 8 ++--- extlib/benz/read.c | 4 +-- extlib/benz/string.c | 50 ++++++++++++++-------------- extlib/benz/symbol.c | 6 ++-- extlib/benz/vector.c | 4 +-- extlib/benz/write.c | 6 ++-- 23 files changed, 85 insertions(+), 87 deletions(-) diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index 0f5817d7..9458ef15 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -1,9 +1,9 @@ #include "picrin.h" void -pic_str_set(pic_state *pic, pic_str *str, int i, char c) +pic_str_set(pic_state *pic, struct pic_string *str, int i, char c) { - pic_str *x, *y, *z, *tmp; + struct pic_string *x, *y, *z, *tmp; char buf[1]; if (pic_str_len(str) <= i) { @@ -26,7 +26,7 @@ pic_str_set(pic_state *pic, pic_str *str, int i, char c) static pic_value pic_str_string_set(pic_state *pic) { - pic_str *str; + struct pic_string *str; char c; int k; @@ -39,7 +39,7 @@ pic_str_string_set(pic_state *pic) static pic_value pic_str_string_copy_ip(pic_state *pic) { - pic_str *to, *from; + struct pic_string *to, *from; int n, at, start, end; n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end); @@ -63,7 +63,7 @@ pic_str_string_copy_ip(pic_state *pic) static pic_value pic_str_string_fill_ip(pic_state *pic) { - pic_str *str; + struct pic_string *str; char c; int n, start, end; diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 7cda6527..09ba6e76 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -105,7 +105,7 @@ pic_system_getenvs(pic_state *pic) } for (envp = picrin_envp; *envp; ++envp) { - pic_str *key, *val; + struct pic_string *key, *val; int i; for (i = 0; (*envp)[i] != '='; ++i) diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 2af663dd..481a703d 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -82,7 +82,7 @@ pic_regexp_regexp_match(pic_state *pic) const char *input; regmatch_t match[100]; pic_value matches, positions; - pic_str *str; + struct pic_string *str; int i, offset; pic_get_args(pic, "oz", ®, &input); @@ -157,7 +157,7 @@ pic_regexp_regexp_replace(pic_state *pic) pic_value reg; const char *input; regmatch_t match; - pic_str *txt, *output = pic_make_lit(pic, ""); + struct pic_string *txt, *output = pic_make_lit(pic, ""); pic_get_args(pic, "ozs", ®, &input, &txt); diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index b4acfc67..c7cbff9f 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -30,7 +30,7 @@ pic_blob_bytevector(pic_state *pic) { pic_value *argv; int argc, i; - pic_blob *blob; + struct pic_blob *blob; unsigned char *data; pic_get_args(pic, "*", &argc, &argv); @@ -55,7 +55,7 @@ pic_blob_bytevector(pic_state *pic) static pic_value pic_blob_make_bytevector(pic_state *pic) { - pic_blob *blob; + struct pic_blob *blob; int k, i, b = 0; pic_get_args(pic, "i|i", &k, &b); @@ -110,7 +110,7 @@ pic_blob_bytevector_u8_set(pic_state *pic) static pic_value pic_blob_bytevector_copy_i(pic_state *pic) { - pic_blob *to, *from; + struct pic_blob *to, *from; int n, at, start, end; n = pic_get_args(pic, "bib|ii", &to, &at, &from, &start, &end); @@ -141,7 +141,7 @@ pic_blob_bytevector_copy_i(pic_state *pic) static pic_value pic_blob_bytevector_copy(pic_state *pic) { - pic_blob *from, *to; + struct pic_blob *from, *to; int n, start, end, i = 0; n = pic_get_args(pic, "b|ii", &from, &start, &end); @@ -170,7 +170,7 @@ pic_blob_bytevector_append(pic_state *pic) { int argc, i, j, len; pic_value *argv; - pic_blob *blob; + struct pic_blob *blob; pic_get_args(pic, "*", &argc, &argv); @@ -196,7 +196,7 @@ pic_blob_bytevector_append(pic_state *pic) static pic_value pic_blob_list_to_bytevector(pic_state *pic) { - pic_blob *blob; + struct pic_blob *blob; unsigned char *data; pic_value list, e, it; @@ -220,7 +220,7 @@ pic_blob_list_to_bytevector(pic_state *pic) static pic_value pic_blob_bytevector_to_list(pic_state *pic) { - pic_blob *blob; + struct pic_blob *blob; pic_value list; int n, start, end, i; diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 6e5a39b7..e54a8988 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -54,7 +54,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) return pic_str_cmp(pic, pic_str_ptr(x), pic_str_ptr(y)) == 0; } case PIC_TT_BLOB: { - pic_blob *blob1, *blob2; + struct pic_blob *blob1, *blob2; int i; blob1 = pic_blob_ptr(x); diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index fb1cb197..106be269 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -4,12 +4,12 @@ #include "picrin.h" -pic_str * +struct pic_string * pic_get_backtrace(pic_state *pic) { size_t ai = pic_gc_arena_preserve(pic); pic_callinfo *ci; - pic_str *trace; + struct pic_string *trace; trace = pic_make_lit(pic, ""); diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 2c68b5f1..3a3af27e 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -21,7 +21,7 @@ void pic_warnf(pic_state *pic, const char *fmt, ...) { va_list ap; - pic_str *err; + struct pic_string *err; va_start(ap, fmt); err = pic_vformat(pic, fmt, ap); @@ -35,7 +35,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) { va_list ap; const char *msg; - pic_str *err; + struct pic_string *err; va_start(ap, fmt); err = pic_vformat(pic, fmt, ap); @@ -94,7 +94,7 @@ struct pic_error * pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs) { struct pic_error *e; - pic_str *stack; + struct pic_string *stack; stack = pic_get_backtrace(pic); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 8d6f035c..39d9d0f0 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -47,7 +47,7 @@ typedef void *(*pic_allocf)(void *, void *, size_t); #include "picrin/read.h" #include "picrin/gc.h" -KHASH_DECLARE(s, pic_str *, pic_sym *) +KHASH_DECLARE(s, struct pic_string *, pic_sym *) typedef struct pic_checkpoint { PIC_OBJECT_HEADER @@ -247,19 +247,19 @@ void pic_weak_del(pic_state *, struct pic_weak *, void *); bool pic_weak_has(pic_state *, struct pic_weak *, void *); /* symbol */ -pic_sym *pic_intern(pic_state *, pic_str *); +pic_sym *pic_intern(pic_state *, struct pic_string *); #define pic_intern_str(pic,s,i) pic_intern(pic, pic_make_str(pic, (s), (i))) #define pic_intern_cstr(pic,s) pic_intern(pic, pic_make_cstr(pic, (s))) #define pic_intern_lit(pic,lit) pic_intern(pic, pic_make_lit(pic, lit)) const char *pic_symbol_name(pic_state *, pic_sym *); /* string */ -int pic_str_len(pic_str *); -char pic_str_ref(pic_state *, pic_str *, int); -pic_str *pic_str_cat(pic_state *, pic_str *, pic_str *); -pic_str *pic_str_sub(pic_state *, pic_str *, int, int); -int pic_str_cmp(pic_state *, pic_str *, pic_str *); -int pic_str_hash(pic_state *, pic_str *); +int pic_str_len(struct pic_string *); +char pic_str_ref(pic_state *, struct pic_string *, int); +struct pic_string *pic_str_cat(pic_state *, struct pic_string *, struct pic_string *); +struct pic_string *pic_str_sub(pic_state *, struct pic_string *, int, int); +int pic_str_cmp(pic_state *, struct pic_string *, struct pic_string *); +int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/blob.h" #include "picrin/cont.h" @@ -313,7 +313,7 @@ struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); (pic->prev_lib = NULL))) void pic_warnf(pic_state *, const char *, ...); -pic_str *pic_get_backtrace(pic_state *); +struct pic_string *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); struct pic_port *pic_stdin(pic_state *); diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h index 540cae65..36e35409 100644 --- a/extlib/benz/include/picrin/error.h +++ b/extlib/benz/include/picrin/error.h @@ -12,9 +12,9 @@ extern "C" { struct pic_error { PIC_OBJECT_HEADER pic_sym *type; - pic_str *msg; + struct pic_string *msg; pic_value irrs; - pic_str *stack; + struct pic_string *stack; }; #define pic_error_p(v) (pic_type(v) == PIC_TT_ERROR) diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index 5076f367..e52196ff 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -15,13 +15,13 @@ struct pic_env { PIC_OBJECT_HEADER khash_t(env) map; struct pic_env *up; - pic_str *prefix; + struct pic_string *prefix; }; #define pic_env_p(v) (pic_type(v) == PIC_TT_ENV) #define pic_env_ptr(v) ((struct pic_env *)pic_ptr(v)) -struct pic_env *pic_make_topenv(pic_state *, pic_str *); +struct pic_env *pic_make_topenv(pic_state *, struct pic_string *); struct pic_env *pic_make_env(pic_state *, struct pic_env *); pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); diff --git a/extlib/benz/include/picrin/string.h b/extlib/benz/include/picrin/string.h index f5c32314..f9b44fbc 100644 --- a/extlib/benz/include/picrin/string.h +++ b/extlib/benz/include/picrin/string.h @@ -19,14 +19,14 @@ void pic_rope_decref(pic_state *, struct pic_rope *); #define pic_str_ptr(o) ((struct pic_string *)pic_ptr(o)) -pic_str *pic_make_str(pic_state *, const char *, int); +struct pic_string *pic_make_str(pic_state *, const char *, int); #define pic_make_cstr(pic, cstr) pic_make_str(pic, (cstr), strlen(cstr)) #define pic_make_lit(pic, lit) pic_make_str(pic, "" lit, -((int)sizeof lit - 1)) -const char *pic_str_cstr(pic_state *, pic_str *); +const char *pic_str_cstr(pic_state *, struct pic_string *); -pic_str *pic_format(pic_state *, const char *, ...); -pic_str *pic_vformat(pic_state *, const char *, va_list); +struct pic_string *pic_format(pic_state *, const char *, ...); +struct pic_string *pic_vformat(pic_state *, const char *, va_list); #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/symbol.h b/extlib/benz/include/picrin/symbol.h index b390a7cc..3c905cd9 100644 --- a/extlib/benz/include/picrin/symbol.h +++ b/extlib/benz/include/picrin/symbol.h @@ -13,7 +13,7 @@ struct pic_id { union { struct pic_symbol { PIC_OBJECT_HEADER - pic_str *str; + struct pic_string *str; } sym; struct { PIC_OBJECT_HEADER diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index a3e9ed78..f6cda486 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -202,9 +202,7 @@ struct pic_env; typedef struct pic_symbol pic_sym; typedef struct pic_id pic_id; typedef struct pic_pair pic_pair; -typedef struct pic_string pic_str; typedef struct pic_vector pic_vec; -typedef struct pic_blob pic_blob; #define pic_obj_p(v) (pic_vtype(v) == PIC_VTYPE_HEAP) #define pic_obj_ptr(v) ((struct pic_object *)pic_ptr(v)) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 0faac96b..1bf6444b 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -9,7 +9,7 @@ make_library_env(pic_state *pic, pic_value name) { struct pic_env *env; pic_value dir, it; - pic_str *prefix = NULL; + struct pic_string *prefix = NULL; pic_for_each (dir, name, it) { if (prefix == NULL) { diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 8646986d..27bd76e0 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -21,7 +21,7 @@ pic_make_env(pic_state *pic, struct pic_env *up) } struct pic_env * -pic_make_topenv(pic_state *pic, pic_str *prefix) +pic_make_topenv(pic_state *pic, struct pic_string *prefix) { struct pic_env *env; @@ -37,7 +37,7 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { const char *name; pic_sym *uid; - pic_str *str; + struct pic_string *str; name = pic_identifier_name(pic, id); diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 035528b3..32f86608 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -218,7 +218,7 @@ pic_number_number_to_string(pic_state *pic) double f; bool e; int radix = 10; - pic_str *str; + struct pic_string *str; pic_get_args(pic, "F|i", &f, &e, &radix); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 504b48ad..1cad0ed8 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -546,7 +546,7 @@ static pic_value pic_port_get_output_bytevector(pic_state *pic) { struct pic_port *port = pic_stdout(pic); - pic_blob *blob; + struct pic_blob *blob; struct strfile *s; pic_get_args(pic, "|p", &port); @@ -646,7 +646,7 @@ pic_port_char_ready_p(pic_state *pic) static pic_value pic_port_read_string(pic_state *pic){ struct pic_port *port = pic_stdin(pic), *buf; - pic_str *str; + struct pic_string *str; int k, i; int c; pic_value res = pic_eof_object(); @@ -725,7 +725,7 @@ static pic_value pic_port_read_blob(pic_state *pic) { struct pic_port *port = pic_stdin(pic); - pic_blob *blob; + struct pic_blob *blob; int k, i; pic_get_args(pic, "i|p", &k, &port); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 8d209418..62dd9ba3 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -19,10 +19,10 @@ * F double *, bool * float with exactness * c char * char * z char ** c string - * s pic_str ** string object * m pic_sym ** symbol * v pic_vec ** vector object - * b pic_blob ** bytevector object + * s struct pic_str ** string object + * b struct pic_blob ** bytevector object * l struct pic_proc ** lambda object * p struct pic_port ** port object * d struct pic_dict ** dictionary object @@ -146,10 +146,10 @@ pic_get_args(pic_state *pic, const char *format, ...) #define PTR_CASE(c, type, ctype) \ VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) - PTR_CASE('s', str, pic_str *) PTR_CASE('m', sym, pic_sym *) PTR_CASE('v', vec, pic_vec *) - PTR_CASE('b', blob, pic_blob *) + PTR_CASE('s', str, struct pic_string *) + PTR_CASE('b', blob, struct pic_blob *) PTR_CASE('l', proc, struct pic_proc *) PTR_CASE('p', port, struct pic_port *) PTR_CASE('d', dict, struct pic_dict *) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 1fb6a713..84522faa 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -419,7 +419,7 @@ read_string(pic_state *pic, struct pic_port *port, int c) { char *buf; int size, cnt; - pic_str *str; + struct pic_string *str; size = 256; buf = pic_malloc(pic, size); @@ -499,7 +499,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) int nbits, n; int len, i; unsigned char *dat; - pic_blob *blob; + struct pic_blob *blob; nbits = 0; diff --git a/extlib/benz/string.c b/extlib/benz/string.c index dc5dcb9e..c838fa6a 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -93,12 +93,12 @@ pic_make_rope(pic_state *pic, struct pic_chunk *c) return x; } -static pic_str * +static struct pic_string * pic_make_string(pic_state *pic, struct pic_rope *rope) { - pic_str *str; + struct pic_string *str; - str = (pic_str *)pic_obj_alloc(pic, sizeof(pic_str), PIC_TT_STRING); + str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TT_STRING); str->rope = rope; /* delegate ownership */ return str; } @@ -236,7 +236,7 @@ rope_cstr(pic_state *pic, struct pic_rope *x) return c->str; } -pic_str * +struct pic_string * pic_make_str(pic_state *pic, const char *str, int len) { struct pic_chunk *c; @@ -253,13 +253,13 @@ pic_make_str(pic_state *pic, const char *str, int len) } int -pic_str_len(pic_str *str) +pic_str_len(struct pic_string *str) { return rope_len(str->rope); } char -pic_str_ref(pic_state *pic, pic_str *str, int i) +pic_str_ref(pic_state *pic, struct pic_string *str, int i) { int c; @@ -270,26 +270,26 @@ pic_str_ref(pic_state *pic, pic_str *str, int i) return (char)c; } -pic_str * -pic_str_cat(pic_state *pic, pic_str *a, pic_str *b) +struct pic_string * +pic_str_cat(pic_state *pic, struct pic_string *a, struct pic_string *b) { return pic_make_string(pic, rope_cat(pic, a->rope, b->rope)); } -pic_str * -pic_str_sub(pic_state *pic, pic_str *str, int s, int e) +struct pic_string * +pic_str_sub(pic_state *pic, struct pic_string *str, int s, int e) { return pic_make_string(pic, rope_sub(pic, str->rope, s, e)); } int -pic_str_cmp(pic_state *pic, pic_str *str1, pic_str *str2) +pic_str_cmp(pic_state *pic, struct pic_string *str1, struct pic_string *str2) { return strcmp(pic_str_cstr(pic, str1), pic_str_cstr(pic, str2)); } int -pic_str_hash(pic_state *pic, pic_str *str) +pic_str_hash(pic_state *pic, struct pic_string *str) { const char *s; int h = 0; @@ -302,7 +302,7 @@ pic_str_hash(pic_state *pic, pic_str *str) } const char * -pic_str_cstr(pic_state *pic, pic_str *str) +pic_str_cstr(pic_state *pic, struct pic_string *str) { return rope_cstr(pic, str->rope); } @@ -373,11 +373,11 @@ pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) return; } -pic_str * +struct pic_string * pic_vformat(pic_state *pic, const char *fmt, va_list ap) { struct pic_port *port; - pic_str *str; + struct pic_string *str; port = pic_open_output_string(pic); @@ -388,11 +388,11 @@ pic_vformat(pic_state *pic, const char *fmt, va_list ap) return str; } -pic_str * +struct pic_string * pic_format(pic_state *pic, const char *fmt, ...) { va_list ap; - pic_str *str; + struct pic_string *str; va_start(ap, fmt); str = pic_vformat(pic, fmt, ap); @@ -416,7 +416,7 @@ pic_str_string(pic_state *pic) { int argc, i; pic_value *argv; - pic_str *str; + struct pic_string *str; char *buf; pic_get_args(pic, "*", &argc, &argv); @@ -456,7 +456,7 @@ pic_str_make_string(pic_state *pic) static pic_value pic_str_string_length(pic_state *pic) { - pic_str *str; + struct pic_string *str; pic_get_args(pic, "s", &str); @@ -466,7 +466,7 @@ pic_str_string_length(pic_state *pic) static pic_value pic_str_string_ref(pic_state *pic) { - pic_str *str; + struct pic_string *str; int k; pic_get_args(pic, "si", &str, &k); @@ -507,7 +507,7 @@ DEFINE_STRING_CMP(ge, >=) static pic_value pic_str_string_copy(pic_state *pic) { - pic_str *str; + struct pic_string *str; int n, start, end, len; n = pic_get_args(pic, "s|ii", &str, &start, &end); @@ -532,7 +532,7 @@ pic_str_string_append(pic_state *pic) { int argc, i; pic_value *argv; - pic_str *str; + struct pic_string *str; pic_get_args(pic, "*", &argc, &argv); @@ -552,7 +552,7 @@ pic_str_string_map(pic_state *pic) struct pic_proc *proc; pic_value *argv, vals, val; int argc, i, len, j; - pic_str *str; + struct pic_string *str; char *buf; pic_get_args(pic, "l*", &proc, &argc, &argv); @@ -632,7 +632,7 @@ pic_str_string_for_each(pic_state *pic) static pic_value pic_str_list_to_string(pic_state *pic) { - pic_str *str; + struct pic_string *str; pic_value list, e, it; int i; char *buf; @@ -667,7 +667,7 @@ pic_str_list_to_string(pic_state *pic) static pic_value pic_str_string_to_list(pic_state *pic) { - pic_str *str; + struct pic_string *str; pic_value list; int n, start, end, i; diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 0c81ddfa..32d533d2 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -7,10 +7,10 @@ #define kh_pic_str_hash(a) (pic_str_hash(pic, (a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, (a), (b)) == 0) -KHASH_DEFINE(s, pic_str *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) +KHASH_DEFINE(s, struct pic_string *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) pic_sym * -pic_intern(pic_state *pic, pic_str *str) +pic_intern(pic_state *pic, struct pic_string *str) { khash_t(s) *h = &pic->oblist; pic_sym *sym; @@ -102,7 +102,7 @@ pic_symbol_symbol_to_string(pic_state *pic) static pic_value pic_symbol_string_to_symbol(pic_state *pic) { - pic_str *str; + struct pic_string *str; pic_get_args(pic, "s", &str); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 09ed95fb..582c9d63 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -323,7 +323,7 @@ pic_vec_vector_to_string(pic_state *pic) pic_vec *vec; char *buf; int n, start, end, i; - pic_str *str; + struct pic_string *str; n = pic_get_args(pic, "v|ii", &vec, &start, &end); @@ -355,7 +355,7 @@ pic_vec_vector_to_string(pic_state *pic) static pic_value pic_vec_string_to_vector(pic_state *pic) { - pic_str *str; + struct pic_string *str; int n, start, end, i; pic_vec *vec; diff --git a/extlib/benz/write.c b/extlib/benz/write.c index e36a8a0b..0c86fcb8 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -47,7 +47,7 @@ writer_control_destroy(struct writer_control *p) } static void -write_blob(pic_state *pic, pic_blob *blob, xFILE *file) +write_blob(pic_state *pic, struct pic_blob *blob, xFILE *file) { int i; @@ -82,7 +82,7 @@ write_char(pic_state *pic, char c, xFILE *file, int mode) } static void -write_str(pic_state *pic, pic_str *str, xFILE *file, int mode) +write_str(pic_state *pic, struct pic_string *str, xFILE *file, int mode) { int i; const char *cstr = pic_str_cstr(pic, str); @@ -433,7 +433,7 @@ pic_printf(pic_state *pic, const char *fmt, ...) { xFILE *file = pic_stdout(pic)->file; va_list ap; - pic_str *str; + struct pic_string *str; va_start(ap, fmt); From 4a3104187e8539c0cde55fda55695f6698c27ec8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 00:29:39 +0900 Subject: [PATCH 013/119] cosmetic changes --- extlib/benz/include/picrin.h | 7 ++++--- extlib/benz/include/picrin/pair.h | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 39d9d0f0..54f0c768 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -175,9 +175,9 @@ int pic_int(pic_value); double pic_float(pic_value); char pic_char(pic_value); bool pic_bool(pic_value); -/* const char *pic_str(pic_state *, pic_value, int *len); */ +/* const char *pic_str(pic_state *, pic_value); */ /* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ -void *pic_data(pic_state *, pic_value); +/* void *pic_data(pic_state *, pic_value); */ pic_value pic_undef_value(); pic_value pic_int_value(int); @@ -187,6 +187,7 @@ pic_value pic_true_value(); pic_value pic_false_value(); pic_value pic_bool_value(bool); +#define pic_undef_p(v) (pic_vtype(v) == PIC_VTYPE_UNDEF) #define pic_int_p(v) (pic_vtype(v) == PIC_VTYPE_INT) #define pic_float_p(v) (pic_vtype(v) == PIC_VTYPE_FLOAT) #define pic_char_p(v) (pic_vtype(v) == PIC_VTYPE_CHAR) @@ -202,7 +203,6 @@ pic_value pic_bool_value(bool); #define pic_dict_p(v) (pic_type(v) == PIC_TT_DICT) #define pic_weak_p(v) (pic_type(v) == PIC_TT_WEAK) #define pic_sym_p(v) (pic_type(v) == PIC_TT_SYMBOL) -#define pic_undef_p(v) (pic_vtype(v) == PIC_VTYPE_UNDEF) enum pic_tt pic_type(pic_value); const char *pic_type_repr(enum pic_tt); @@ -222,6 +222,7 @@ bool pic_list_p(pic_value); pic_value pic_list(pic_state *, int n, ...); pic_value pic_vlist(pic_state *, int n, va_list); pic_value pic_list_ref(pic_state *, pic_value, int); +pic_value pic_list_tail(pic_state *, pic_value, int); void pic_list_set(pic_state *, pic_value, int, pic_value); int pic_length(pic_state *, pic_value); diff --git a/extlib/benz/include/picrin/pair.h b/extlib/benz/include/picrin/pair.h index 3cbf428f..ff6538ed 100644 --- a/extlib/benz/include/picrin/pair.h +++ b/extlib/benz/include/picrin/pair.h @@ -78,7 +78,6 @@ 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_tail(pic_state *, pic_value, int); pic_value pic_list_copy(pic_state *, pic_value); #if defined(__cplusplus) From d30cdf74099220335419bfa910ab74baa47f98ca Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 00:50:02 +0900 Subject: [PATCH 014/119] add picrin/setup.h and picrin/state.h --- extlib/benz/include/picrin.h | 95 ++------------------------ extlib/benz/include/picrin/setup.h | 104 +++++++++++++++++++++++++++++ extlib/benz/include/picrin/state.h | 101 ++++++++++++++++++++++++++++ extlib/benz/include/picrin/type.h | 1 + 4 files changed, 212 insertions(+), 89 deletions(-) create mode 100644 extlib/benz/include/picrin/setup.h create mode 100644 extlib/benz/include/picrin/state.h diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 54f0c768..ca13b709 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -32,98 +32,13 @@ extern "C" { #include #include -#include "picrin/config.h" - -#include "picrin/compat.h" -#include "picrin/khash.h" +#include "picrin/setup.h" typedef struct pic_state pic_state; -typedef void *(*pic_allocf)(void *, void *, size_t); - #include "picrin/type.h" -#include "picrin/irep.h" -#include "picrin/file.h" -#include "picrin/read.h" -#include "picrin/gc.h" -KHASH_DECLARE(s, struct pic_string *, pic_sym *) - -typedef struct pic_checkpoint { - PIC_OBJECT_HEADER - struct pic_proc *in; - struct pic_proc *out; - int depth; - struct pic_checkpoint *prev; -} pic_checkpoint; - -typedef struct { - int argc, retc; - pic_code *ip; - pic_value *fp; - struct pic_irep *irep; - struct pic_context *cxt; - int regc; - pic_value *regs; - struct pic_context *up; -} pic_callinfo; - -struct pic_state { - pic_allocf allocf; - void *userdata; - - pic_checkpoint *cp; - struct pic_cont *cc; - int ccnt; - - pic_value *sp; - pic_value *stbase, *stend; - - pic_callinfo *ci; - pic_callinfo *cibase, *ciend; - - struct pic_proc **xp; - struct pic_proc **xpbase, **xpend; - - pic_code *ip; - - pic_value ptable; /* list of ephemerons */ - - struct pic_lib *lib, *prev_lib; - - pic_sym *sDEFINE, *sDEFINE_MACRO, *sLAMBDA, *sIF, *sBEGIN, *sSETBANG; - pic_sym *sQUOTE, *sQUASIQUOTE, *sUNQUOTE, *sUNQUOTE_SPLICING; - pic_sym *sSYNTAX_QUOTE, *sSYNTAX_QUASIQUOTE; - pic_sym *sSYNTAX_UNQUOTE, *sSYNTAX_UNQUOTE_SPLICING; - pic_sym *sDEFINE_LIBRARY, *sIMPORT, *sEXPORT, *sCOND_EXPAND; - pic_sym *sCONS, *sCAR, *sCDR, *sNILP, *sSYMBOLP, *sPAIRP; - pic_sym *sADD, *sSUB, *sMUL, *sDIV, *sEQ, *sLT, *sLE, *sGT, *sGE, *sNOT; - - struct pic_lib *PICRIN_BASE; - struct pic_lib *PICRIN_USER; - - pic_value features; - - khash_t(s) oblist; /* string to symbol */ - int ucnt; - struct pic_weak *globals; - struct pic_weak *macros; - pic_value libs; - struct pic_list ireps; /* chain */ - - pic_reader reader; - xFILE files[XOPEN_MAX]; - pic_code iseq[2]; /* for pic_apply_trampoline */ - - bool gc_enable; - struct pic_heap *heap; - struct pic_object **arena; - size_t arena_size, arena_idx; - - pic_value err; - - char *native_stack_start; -}; +typedef void *(*pic_allocf)(void *, void *, size_t); pic_state *pic_open(pic_allocf, void *); void pic_close(pic_state *); @@ -262,6 +177,10 @@ struct pic_string *pic_str_sub(pic_state *, struct pic_string *, int, int); int pic_str_cmp(pic_state *, struct pic_string *, struct pic_string *); int pic_str_hash(pic_state *, struct pic_string *); +/* extra stuff */ + +#include "picrin/state.h" + #include "picrin/blob.h" #include "picrin/cont.h" #include "picrin/data.h" @@ -278,8 +197,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/vector.h" #include "picrin/weak.h" -/* extra stuff */ - void *pic_default_allocf(void *, void *, size_t); struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h new file mode 100644 index 00000000..ef10793f --- /dev/null +++ b/extlib/benz/include/picrin/setup.h @@ -0,0 +1,104 @@ +/** + * See Copyright Notice in picrin.h + */ + +#include "picrin/config.h" + +#ifndef PIC_DIRECT_THREADED_VM +# if (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 +# define PIC_DIRECT_THREADED_VM 1 +# endif +#endif + +#if PIC_NAN_BOXING && PIC_WORD_BOXING +# error cannot enable both PIC_NAN_BOXING and PIC_WORD_BOXING simultaneously +#endif + +#ifndef PIC_WORD_BOXING +# define PIC_WORD_BOXING 0 +#endif + +#if ! PIC_WORD_BOXING +# ifndef PIC_NAN_BOXING +# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 +# define PIC_NAN_BOXING 1 +# endif +# endif +#endif + +#ifndef PIC_ENABLE_LIBC +# define PIC_ENABLE_LIBC 1 +#endif + +#ifndef PIC_ENABLE_STDIO +# define PIC_ENABLE_STDIO 1 +#endif + +#ifndef PIC_JMPBUF +# include +# define PIC_JMPBUF jmp_buf +#endif + +#ifndef PIC_SETJMP +# include +# define PIC_SETJMP(pic, buf) setjmp(buf) +#endif + +#ifndef PIC_LONGJMP +# include +# define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val)) +#endif + +#ifndef PIC_ABORT +# define PIC_ABORT(pic) abort() +#endif + +#ifndef PIC_ARENA_SIZE +# define PIC_ARENA_SIZE (8 * 1024) +#endif + +#ifndef PIC_HEAP_PAGE_SIZE +# define PIC_HEAP_PAGE_SIZE (4 * 1024 * 1024) +#endif + +#ifndef PIC_PAGE_REQUEST_THRESHOLD +# define PIC_PAGE_REQUEST_THRESHOLD(total) ((total) * 77 / 100) +#endif + +#ifndef PIC_STACK_SIZE +# define PIC_STACK_SIZE 2048 +#endif + +#ifndef PIC_RESCUE_SIZE +# define PIC_RESCUE_SIZE 30 +#endif + +#ifndef PIC_SYM_POOL_SIZE +# define PIC_SYM_POOL_SIZE (2 * 1024) +#endif + +#ifndef PIC_IREP_SIZE +# define PIC_IREP_SIZE 8 +#endif + +#ifndef PIC_POOL_SIZE +# define PIC_POOL_SIZE 8 +#endif + +#ifndef PIC_SYMS_SIZE +# define PIC_SYMS_SIZE 32 +#endif + +#ifndef PIC_ISEQ_SIZE +# define PIC_ISEQ_SIZE 1024 +#endif + +#if DEBUG +# include +# define GC_STRESS 0 +# define VM_DEBUG 1 +# define GC_DEBUG 0 +# define GC_DEBUG_DETAIL 0 +#endif + +#include "picrin/compat.h" diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h new file mode 100644 index 00000000..59655643 --- /dev/null +++ b/extlib/benz/include/picrin/state.h @@ -0,0 +1,101 @@ +/** + * See Copyright Notice in picrin.h + */ + +#ifndef PICRIN_STATE_H +#define PICRIN_STATE_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#include "picrin/khash.h" + +#include "picrin/irep.h" +#include "picrin/file.h" +#include "picrin/read.h" +#include "picrin/gc.h" + +KHASH_DECLARE(s, struct pic_string *, pic_sym *) + +typedef struct pic_checkpoint { + PIC_OBJECT_HEADER + struct pic_proc *in; + struct pic_proc *out; + int depth; + struct pic_checkpoint *prev; +} pic_checkpoint; + +typedef struct { + int argc, retc; + pic_code *ip; + pic_value *fp; + struct pic_irep *irep; + struct pic_context *cxt; + int regc; + pic_value *regs; + struct pic_context *up; +} pic_callinfo; + +struct pic_state { + pic_allocf allocf; + void *userdata; + + pic_checkpoint *cp; + struct pic_cont *cc; + int ccnt; + + pic_value *sp; + pic_value *stbase, *stend; + + pic_callinfo *ci; + pic_callinfo *cibase, *ciend; + + struct pic_proc **xp; + struct pic_proc **xpbase, **xpend; + + pic_code *ip; + + pic_value ptable; /* list of ephemerons */ + + struct pic_lib *lib, *prev_lib; + + pic_sym *sDEFINE, *sDEFINE_MACRO, *sLAMBDA, *sIF, *sBEGIN, *sSETBANG; + pic_sym *sQUOTE, *sQUASIQUOTE, *sUNQUOTE, *sUNQUOTE_SPLICING; + pic_sym *sSYNTAX_QUOTE, *sSYNTAX_QUASIQUOTE; + pic_sym *sSYNTAX_UNQUOTE, *sSYNTAX_UNQUOTE_SPLICING; + pic_sym *sDEFINE_LIBRARY, *sIMPORT, *sEXPORT, *sCOND_EXPAND; + pic_sym *sCONS, *sCAR, *sCDR, *sNILP, *sSYMBOLP, *sPAIRP; + pic_sym *sADD, *sSUB, *sMUL, *sDIV, *sEQ, *sLT, *sLE, *sGT, *sGE, *sNOT; + + struct pic_lib *PICRIN_BASE; + struct pic_lib *PICRIN_USER; + + pic_value features; + + khash_t(s) oblist; /* string to symbol */ + int ucnt; + struct pic_weak *globals; + struct pic_weak *macros; + pic_value libs; + struct pic_list ireps; /* chain */ + + pic_reader reader; + xFILE files[XOPEN_MAX]; + pic_code iseq[2]; /* for pic_apply_trampoline */ + + bool gc_enable; + struct pic_heap *heap; + struct pic_object **arena; + size_t arena_size, arena_idx; + + pic_value err; + + char *native_stack_start; +}; + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index f6cda486..25bc9a4c 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -197,6 +197,7 @@ struct pic_proc; struct pic_port; struct pic_error; struct pic_env; +struct pic_lib; /* set aliases to basic types */ typedef struct pic_symbol pic_sym; From b084a2039050b3e351ec8b53ec23e83eb0d7364a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 00:51:22 +0900 Subject: [PATCH 015/119] fix regression --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index fa67bdad..78a4dcc2 100644 --- a/src/main.c +++ b/src/main.c @@ -56,7 +56,7 @@ main(int argc, char *argv[], char **envp) PICRIN_MAIN = pic_find_library(pic, pic_read_cstr(pic, "(picrin main)")); - pic_funcall(pic, PICRIN_MAIN, "main", pic_nil_value()); + pic_funcall(pic, PICRIN_MAIN, "main", 0); status = 0; } From 294477ff13ade29967d450c8f0a2937573151f2c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 12:59:58 +0900 Subject: [PATCH 016/119] kh_s_t -> kh_oblist_t --- extlib/benz/gc.c | 4 ++-- extlib/benz/include/picrin/state.h | 4 ++-- extlib/benz/state.c | 6 +++--- extlib/benz/symbol.c | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 4acea528..1305ea79 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -645,7 +645,7 @@ gc_sweep_phase(pic_state *pic) struct heap_page *page; khiter_t it; khash_t(weak) *h; - khash_t(s) *s = &pic->oblist; + khash_t(oblist) *s = &pic->oblist; pic_sym *sym; struct pic_object *obj; size_t total = 0, inuse = 0; @@ -670,7 +670,7 @@ gc_sweep_phase(pic_state *pic) continue; sym = kh_val(s, it); if (sym->gc_mark == PIC_GC_UNMARK) { - kh_del(s, s, it); + kh_del(oblist, s, it); } } diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 59655643..2cef1abc 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -16,7 +16,7 @@ extern "C" { #include "picrin/read.h" #include "picrin/gc.h" -KHASH_DECLARE(s, struct pic_string *, pic_sym *) +KHASH_DECLARE(oblist, struct pic_string *, pic_sym *) typedef struct pic_checkpoint { PIC_OBJECT_HEADER @@ -73,7 +73,7 @@ struct pic_state { pic_value features; - khash_t(s) oblist; /* string to symbol */ + khash_t(oblist) oblist; /* string to symbol */ int ucnt; struct pic_weak *globals; struct pic_weak *macros; diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 961b832b..d2ac63c4 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -252,7 +252,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->heap = pic_heap_open(pic); /* symbol table */ - kh_init(s, &pic->oblist); + kh_init(oblist, &pic->oblist); /* unique symbol count */ pic->ucnt = 0; @@ -376,7 +376,7 @@ pic_open(pic_allocf allocf, void *userdata) void pic_close(pic_state *pic) { - khash_t(s) *h = &pic->oblist; + khash_t(oblist) *h = &pic->oblist; pic_allocf allocf = pic->allocf; /* clear out root objects */ @@ -420,7 +420,7 @@ pic_close(pic_state *pic) allocf(pic->userdata, pic->xpbase, 0); /* free global stacks */ - kh_destroy(s, h); + kh_destroy(oblist, h); /* free GC arena */ allocf(pic->userdata, pic->arena, 0); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 32d533d2..c7ee0969 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -7,17 +7,17 @@ #define kh_pic_str_hash(a) (pic_str_hash(pic, (a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, (a), (b)) == 0) -KHASH_DEFINE(s, struct pic_string *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) +KHASH_DEFINE(oblist, struct pic_string *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) pic_sym * pic_intern(pic_state *pic, struct pic_string *str) { - khash_t(s) *h = &pic->oblist; + khash_t(oblist) *h = &pic->oblist; pic_sym *sym; khiter_t it; int ret; - it = kh_put(s, h, str, &ret); + it = kh_put(oblist, h, str, &ret); if (ret == 0) { /* if exists */ sym = kh_val(h, it); pic_gc_protect(pic, pic_obj_value(sym)); From 8610f5090d61c57d0583b7dd7c7e2d6298571e5a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 15 Feb 2016 13:20:26 +0900 Subject: [PATCH 017/119] change the behavior of pic_deflibrary fix regression --- contrib/10.callcc/callcc.c | 8 +- contrib/10.math/math.c | 44 +++++------ contrib/20.r7rs/src/file.c | 16 ++-- contrib/20.r7rs/src/load.c | 6 +- contrib/20.r7rs/src/mutable-string.c | 10 +-- contrib/20.r7rs/src/system.c | 14 ++-- contrib/20.r7rs/src/time.c | 10 +-- contrib/30.random/src/random.c | 6 +- contrib/30.readline/src/readline.c | 52 +++++++------ contrib/30.regexp/src/regexp.c | 16 ++-- contrib/40.srfi/src/106.c | 106 +++++++++++++------------- contrib/60.repl/repl.c | 6 +- extlib/benz/include/picrin.h | 17 ++--- extlib/benz/include/picrin/state.h | 2 +- extlib/benz/lib.c | 11 +++ extlib/benz/proc.c | 5 -- extlib/benz/state.c | 110 +++++++++++++-------------- src/main.c | 5 +- 18 files changed, 224 insertions(+), 220 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 9c7c76d3..f4a82670 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -282,8 +282,8 @@ pic_callcc_callcc(pic_state *pic) void pic_init_callcc(pic_state *pic) { - pic_deflibrary (pic, "(scheme base)") { - pic_redefun(pic, pic->PICRIN_BASE, "call-with-current-continuation", pic_callcc_callcc); - pic_redefun(pic, pic->PICRIN_BASE, "call/cc", pic_callcc_callcc); - } + pic_deflibrary(pic, "(scheme base)"); + + pic_redefun(pic, pic->PICRIN_BASE, "call-with-current-continuation", pic_callcc_callcc); + pic_redefun(pic, pic->PICRIN_BASE, "call/cc", pic_callcc_callcc); } diff --git a/contrib/10.math/math.c b/contrib/10.math/math.c index f2b9d7f5..84584caf 100644 --- a/contrib/10.math/math.c +++ b/contrib/10.math/math.c @@ -284,27 +284,27 @@ pic_number_expt(pic_state *pic) void pic_init_math(pic_state *pic) { - pic_deflibrary (pic, "(picrin math)") { - pic_defun(pic, "floor/", pic_number_floor2); - pic_defun(pic, "truncate/", pic_number_trunc2); - pic_defun(pic, "floor", pic_number_floor); - pic_defun(pic, "ceiling", pic_number_ceil); - pic_defun(pic, "truncate", pic_number_trunc); - pic_defun(pic, "round", pic_number_round); + pic_deflibrary(pic, "(picrin math)"); - pic_defun(pic, "finite?", pic_number_finite_p); - pic_defun(pic, "infinite?", pic_number_infinite_p); - pic_defun(pic, "nan?", pic_number_nan_p); - pic_defun(pic, "sqrt", pic_number_sqrt); - pic_defun(pic, "exp", pic_number_exp); - pic_defun(pic, "log", pic_number_log); - pic_defun(pic, "sin", pic_number_sin); - pic_defun(pic, "cos", pic_number_cos); - pic_defun(pic, "tan", pic_number_tan); - pic_defun(pic, "acos", pic_number_acos); - pic_defun(pic, "asin", pic_number_asin); - pic_defun(pic, "atan", pic_number_atan); - pic_defun(pic, "abs", pic_number_abs); - pic_defun(pic, "expt", pic_number_expt); - } + pic_defun(pic, "floor/", pic_number_floor2); + pic_defun(pic, "truncate/", pic_number_trunc2); + pic_defun(pic, "floor", pic_number_floor); + pic_defun(pic, "ceiling", pic_number_ceil); + pic_defun(pic, "truncate", pic_number_trunc); + pic_defun(pic, "round", pic_number_round); + + pic_defun(pic, "finite?", pic_number_finite_p); + pic_defun(pic, "infinite?", pic_number_infinite_p); + pic_defun(pic, "nan?", pic_number_nan_p); + pic_defun(pic, "sqrt", pic_number_sqrt); + pic_defun(pic, "exp", pic_number_exp); + pic_defun(pic, "log", pic_number_log); + pic_defun(pic, "sin", pic_number_sin); + pic_defun(pic, "cos", pic_number_cos); + pic_defun(pic, "tan", pic_number_tan); + pic_defun(pic, "acos", pic_number_acos); + pic_defun(pic, "asin", pic_number_asin); + pic_defun(pic, "atan", pic_number_atan); + pic_defun(pic, "abs", pic_number_abs); + pic_defun(pic, "expt", pic_number_expt); } diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index d13f77b2..609592ea 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -93,12 +93,12 @@ pic_file_delete(pic_state *pic) void pic_init_file(pic_state *pic) { - pic_deflibrary (pic, "(scheme file)") { - pic_defun(pic, "open-input-file", pic_file_open_input_file); - pic_defun(pic, "open-binary-input-file", pic_file_open_binary_input_file); - pic_defun(pic, "open-output-file", pic_file_open_output_file); - pic_defun(pic, "open-binary-output-file", pic_file_open_binary_output_file); - pic_defun(pic, "file-exists?", pic_file_exists_p); - pic_defun(pic, "delete-file", pic_file_delete); - } + pic_deflibrary(pic, "(scheme file)"); + + pic_defun(pic, "open-input-file", pic_file_open_input_file); + pic_defun(pic, "open-binary-input-file", pic_file_open_binary_input_file); + pic_defun(pic, "open-output-file", pic_file_open_output_file); + pic_defun(pic, "open-binary-output-file", pic_file_open_binary_output_file); + pic_defun(pic, "file-exists?", pic_file_exists_p); + pic_defun(pic, "delete-file", pic_file_delete); } diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index 15cc6cae..58a48c3c 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -25,7 +25,7 @@ pic_load_load(pic_state *pic) void pic_init_load(pic_state *pic) { - pic_deflibrary (pic, "(scheme load)") { - pic_defun(pic, "load", pic_load_load); - } + pic_deflibrary(pic, "(scheme load)"); + + pic_defun(pic, "load", pic_load_load); } diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index 9458ef15..6d12deea 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -85,9 +85,9 @@ pic_str_string_fill_ip(pic_state *pic) void pic_init_mutable_string(pic_state *pic) { - pic_deflibrary (pic, "(picrin string)") { - pic_defun(pic, "string-set!", pic_str_string_set); - pic_defun(pic, "string-copy!", pic_str_string_copy_ip); - pic_defun(pic, "string-fill!", pic_str_string_fill_ip); - } + pic_deflibrary(pic, "(picrin string)"); + + pic_defun(pic, "string-set!", pic_str_string_set); + pic_defun(pic, "string-copy!", pic_str_string_copy_ip); + pic_defun(pic, "string-fill!", pic_str_string_fill_ip); } diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 09ba6e76..0ec818fd 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -127,11 +127,11 @@ pic_system_getenvs(pic_state *pic) void pic_init_system(pic_state *pic) { - pic_deflibrary (pic, "(scheme process-context)") { - pic_defun(pic, "command-line", pic_system_cmdline); - pic_defun(pic, "exit", pic_system_exit); - pic_defun(pic, "emergency-exit", pic_system_emergency_exit); - pic_defun(pic, "get-environment-variable", pic_system_getenv); - pic_defun(pic, "get-environment-variables", pic_system_getenvs); - } + pic_deflibrary(pic, "(scheme process-context)"); + + pic_defun(pic, "command-line", pic_system_cmdline); + pic_defun(pic, "exit", pic_system_exit); + pic_defun(pic, "emergency-exit", pic_system_emergency_exit); + pic_defun(pic, "get-environment-variable", pic_system_getenv); + pic_defun(pic, "get-environment-variables", pic_system_getenvs); } diff --git a/contrib/20.r7rs/src/time.c b/contrib/20.r7rs/src/time.c index 6ed8420d..4cab8aca 100644 --- a/contrib/20.r7rs/src/time.c +++ b/contrib/20.r7rs/src/time.c @@ -41,9 +41,9 @@ pic_jiffies_per_second(pic_state *pic) void pic_init_time(pic_state *pic) { - pic_deflibrary (pic, "(scheme time)") { - pic_defun(pic, "current-second", pic_current_second); - pic_defun(pic, "current-jiffy", pic_current_jiffy); - pic_defun(pic, "jiffies-per-second", pic_jiffies_per_second); - } + pic_deflibrary(pic, "(scheme time)"); + + pic_defun(pic, "current-second", pic_current_second); + pic_defun(pic, "current-jiffy", pic_current_jiffy); + pic_defun(pic, "jiffies-per-second", pic_jiffies_per_second); } diff --git a/contrib/30.random/src/random.c b/contrib/30.random/src/random.c index a750f92b..61e33633 100644 --- a/contrib/30.random/src/random.c +++ b/contrib/30.random/src/random.c @@ -13,7 +13,7 @@ pic_random_real(pic_state *pic) void pic_init_random(pic_state *pic) { - pic_deflibrary (pic, "(srfi 27)") { - pic_defun(pic, "random-real", pic_random_real); - } + pic_deflibrary(pic, "(srfi 27)"); + + pic_defun(pic, "random-real", pic_random_real); } diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index d6e71d6a..26df7c82 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -246,29 +246,31 @@ pic_rl_history_expand(pic_state *pic) void pic_init_readline(pic_state *pic){ using_history(); - pic_deflibrary (pic, "(picrin readline)") { - pic_defun(pic, "readline", pic_rl_readline); - } - pic_deflibrary (pic, "(picrin readline history)") { - /* pic_defun(pic, "history-offset", pic_rl_history_offset); */ - pic_defun(pic, "history-length", pic_rl_history_length); - pic_defun(pic, "add-history", pic_rl_add_history); - pic_defun(pic, "stifle-history", pic_rl_stifle_history); - pic_defun(pic, "unstifle-history", pic_rl_unstifle_history); - pic_defun(pic, "history-stifled?", pic_rl_history_is_stifled); - pic_defun(pic, "where-history", pic_rl_where_history); - pic_defun(pic, "current-history", pic_rl_current_history); - pic_defun(pic, "history-get", pic_rl_history_get); - pic_defun(pic, "clear-history", pic_rl_clear_history); - pic_defun(pic, "remove-history", pic_rl_remove_history); - pic_defun(pic, "history-set-pos", pic_rl_history_set_pos); - pic_defun(pic, "previous-history", pic_rl_previous_history); - pic_defun(pic, "next-history", pic_rl_next_history); - pic_defun(pic, "history-search", pic_rl_history_search); - pic_defun(pic, "history-search-prefix", pic_rl_history_search_prefix); - pic_defun(pic, "read-history", pic_rl_read_history); - pic_defun(pic, "write-history", pic_rl_write_history); - pic_defun(pic, "truncate-file", pic_rl_truncate_file); - pic_defun(pic, "history-expand", pic_rl_history_expand); - } + + pic_deflibrary(pic, "(picrin readline)"); + + pic_defun(pic, "readline", pic_rl_readline); + + pic_deflibrary(pic, "(picrin readline history)"); + + /* pic_defun(pic, "history-offset", pic_rl_history_offset); */ + pic_defun(pic, "history-length", pic_rl_history_length); + pic_defun(pic, "add-history", pic_rl_add_history); + pic_defun(pic, "stifle-history", pic_rl_stifle_history); + pic_defun(pic, "unstifle-history", pic_rl_unstifle_history); + pic_defun(pic, "history-stifled?", pic_rl_history_is_stifled); + pic_defun(pic, "where-history", pic_rl_where_history); + pic_defun(pic, "current-history", pic_rl_current_history); + pic_defun(pic, "history-get", pic_rl_history_get); + pic_defun(pic, "clear-history", pic_rl_clear_history); + pic_defun(pic, "remove-history", pic_rl_remove_history); + pic_defun(pic, "history-set-pos", pic_rl_history_set_pos); + pic_defun(pic, "previous-history", pic_rl_previous_history); + pic_defun(pic, "next-history", pic_rl_next_history); + pic_defun(pic, "history-search", pic_rl_history_search); + pic_defun(pic, "history-search-prefix", pic_rl_history_search_prefix); + pic_defun(pic, "read-history", pic_rl_read_history); + pic_defun(pic, "write-history", pic_rl_write_history); + pic_defun(pic, "truncate-file", pic_rl_truncate_file); + pic_defun(pic, "history-expand", pic_rl_history_expand); } diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 481a703d..fd88e290 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -178,12 +178,12 @@ pic_regexp_regexp_replace(pic_state *pic) void pic_init_regexp(pic_state *pic) { - pic_deflibrary (pic, "(picrin regexp)") { - pic_defun(pic, "regexp", pic_regexp_regexp); - pic_defun(pic, "regexp?", pic_regexp_regexp_p); - pic_defun(pic, "regexp-match", pic_regexp_regexp_match); - /* pic_defun(pic, "regexp-search", pic_regexp_regexp_search); */ - pic_defun(pic, "regexp-split", pic_regexp_regexp_split); - pic_defun(pic, "regexp-replace", pic_regexp_regexp_replace); - } + pic_deflibrary(pic, "(picrin regexp)"); + + pic_defun(pic, "regexp", pic_regexp_regexp); + pic_defun(pic, "regexp?", pic_regexp_regexp_p); + pic_defun(pic, "regexp-match", pic_regexp_regexp_match); + /* pic_defun(pic, "regexp-search", pic_regexp_regexp_search); */ + pic_defun(pic, "regexp-split", pic_regexp_regexp_split); + pic_defun(pic, "regexp-replace", pic_regexp_regexp_replace); } diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 88921795..42349d68 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -402,123 +402,123 @@ pic_init_srfi_106(pic_state *pic) #define pic_defun_(pic, name, f) pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))) #define pic_define_(pic, name, v) pic_define(pic, pic->lib, name, v) - pic_deflibrary (pic, "(srfi 106)") { - pic_defun_(pic, "socket?", pic_socket_socket_p); - pic_defun_(pic, "make-socket", pic_socket_make_socket); - pic_defun_(pic, "socket-accept", pic_socket_socket_accept); - pic_defun_(pic, "socket-send", pic_socket_socket_send); - pic_defun_(pic, "socket-recv", pic_socket_socket_recv); - pic_defun_(pic, "socket-shutdown", pic_socket_socket_shutdown); - pic_defun_(pic, "socket-close", pic_socket_socket_close); - pic_defun_(pic, "socket-input-port", pic_socket_socket_input_port); - pic_defun_(pic, "socket-output-port", pic_socket_socket_output_port); - pic_defun_(pic, "call-with-socket", pic_socket_call_with_socket); + pic_deflibrary(pic, "(srfi 106)"); + + pic_defun_(pic, "socket?", pic_socket_socket_p); + pic_defun_(pic, "make-socket", pic_socket_make_socket); + pic_defun_(pic, "socket-accept", pic_socket_socket_accept); + pic_defun_(pic, "socket-send", pic_socket_socket_send); + pic_defun_(pic, "socket-recv", pic_socket_socket_recv); + pic_defun_(pic, "socket-shutdown", pic_socket_socket_shutdown); + pic_defun_(pic, "socket-close", pic_socket_socket_close); + pic_defun_(pic, "socket-input-port", pic_socket_socket_input_port); + pic_defun_(pic, "socket-output-port", pic_socket_socket_output_port); + pic_defun_(pic, "call-with-socket", pic_socket_call_with_socket); #ifdef AF_INET - pic_define_(pic, "*af-inet*", pic_int_value(AF_INET)); + pic_define_(pic, "*af-inet*", pic_int_value(AF_INET)); #else - pic_define_(pic, "*af-inet*", pic_false_value()); + pic_define_(pic, "*af-inet*", pic_false_value()); #endif #ifdef AF_INET6 - pic_define_(pic, "*af-inet6*", pic_int_value(AF_INET6)); + pic_define_(pic, "*af-inet6*", pic_int_value(AF_INET6)); #else - pic_define_(pic, "*af-inet6*", pic_false_value()); + pic_define_(pic, "*af-inet6*", pic_false_value()); #endif #ifdef AF_UNSPEC - pic_define_(pic, "*af-unspec*", pic_int_value(AF_UNSPEC)); + pic_define_(pic, "*af-unspec*", pic_int_value(AF_UNSPEC)); #else - pic_define_(pic, "*af-unspec*", pic_false_value()); + pic_define_(pic, "*af-unspec*", pic_false_value()); #endif #ifdef SOCK_STREAM - pic_define_(pic, "*sock-stream*", pic_int_value(SOCK_STREAM)); + pic_define_(pic, "*sock-stream*", pic_int_value(SOCK_STREAM)); #else - pic_define_(pic, "*sock-stream*", pic_false_value()); + pic_define_(pic, "*sock-stream*", pic_false_value()); #endif #ifdef SOCK_DGRAM - pic_define_(pic, "*sock-dgram*", pic_int_value(SOCK_DGRAM)); + pic_define_(pic, "*sock-dgram*", pic_int_value(SOCK_DGRAM)); #else - pic_define_(pic, "*sock-dgram*", pic_false_value()); + pic_define_(pic, "*sock-dgram*", pic_false_value()); #endif #ifdef AI_CANONNAME - pic_define_(pic, "*ai-canonname*", pic_int_value(AI_CANONNAME)); + pic_define_(pic, "*ai-canonname*", pic_int_value(AI_CANONNAME)); #else - pic_define_(pic, "*ai-canonname*", pic_false_value()); + pic_define_(pic, "*ai-canonname*", pic_false_value()); #endif #ifdef AI_NUMERICHOST - pic_define_(pic, "*ai-numerichost*", pic_int_value(AI_NUMERICHOST)); + pic_define_(pic, "*ai-numerichost*", pic_int_value(AI_NUMERICHOST)); #else - pic_define_(pic, "*ai-numerichost*", pic_false_value()); + pic_define_(pic, "*ai-numerichost*", pic_false_value()); #endif -/* AI_V4MAPPED and AI_ALL are not supported by *BSDs, even though they are defined in netdb.h. */ + /* AI_V4MAPPED and AI_ALL are not supported by *BSDs, even though they are defined in netdb.h. */ #if defined(AI_V4MAPPED) && !defined(BSD) - pic_define_(pic, "*ai-v4mapped*", pic_int_value(AI_V4MAPPED)); + pic_define_(pic, "*ai-v4mapped*", pic_int_value(AI_V4MAPPED)); #else - pic_define_(pic, "*ai-v4mapped*", pic_false_value()); + pic_define_(pic, "*ai-v4mapped*", pic_false_value()); #endif #if defined(AI_ALL) && !defined(BSD) - pic_define_(pic, "*ai-all*", pic_int_value(AI_ALL)); + pic_define_(pic, "*ai-all*", pic_int_value(AI_ALL)); #else - pic_define_(pic, "*ai-all*", pic_false_value()); + pic_define_(pic, "*ai-all*", pic_false_value()); #endif #ifdef AI_ADDRCONFIG - pic_define_(pic, "*ai-addrconfig*", pic_int_value(AI_ADDRCONFIG)); + pic_define_(pic, "*ai-addrconfig*", pic_int_value(AI_ADDRCONFIG)); #else - pic_define_(pic, "*ai-addrconfig*", pic_false_value()); + pic_define_(pic, "*ai-addrconfig*", pic_false_value()); #endif #ifdef AI_PASSIVE - pic_define_(pic, "*ai-passive*", pic_int_value(AI_PASSIVE)); + pic_define_(pic, "*ai-passive*", pic_int_value(AI_PASSIVE)); #else - pic_define_(pic, "*ai-passive*", pic_false_value()); + pic_define_(pic, "*ai-passive*", pic_false_value()); #endif #ifdef IPPROTO_IP - pic_define_(pic, "*ipproto-ip*", pic_int_value(IPPROTO_IP)); + pic_define_(pic, "*ipproto-ip*", pic_int_value(IPPROTO_IP)); #else - pic_define_(pic, "*ipproto-ip*", pic_false_value()); + pic_define_(pic, "*ipproto-ip*", pic_false_value()); #endif #ifdef IPPROTO_TCP - pic_define_(pic, "*ipproto-tcp*", pic_int_value(IPPROTO_TCP)); + pic_define_(pic, "*ipproto-tcp*", pic_int_value(IPPROTO_TCP)); #else - pic_define_(pic, "*ipproto-tcp*", pic_false_value()); + pic_define_(pic, "*ipproto-tcp*", pic_false_value()); #endif #ifdef IPPROTO_UDP - pic_define_(pic, "*ipproto-udp*", pic_int_value(IPPROTO_UDP)); + pic_define_(pic, "*ipproto-udp*", pic_int_value(IPPROTO_UDP)); #else - pic_define_(pic, "*ipproto-udp*", pic_false_value()); + pic_define_(pic, "*ipproto-udp*", pic_false_value()); #endif #ifdef MSG_PEEK - pic_define_(pic, "*msg-peek*", pic_int_value(MSG_PEEK)); + pic_define_(pic, "*msg-peek*", pic_int_value(MSG_PEEK)); #else - pic_define_(pic, "*msg-peek*", pic_false_value()); + pic_define_(pic, "*msg-peek*", pic_false_value()); #endif #ifdef MSG_OOB - pic_define_(pic, "*msg-oob*", pic_int_value(MSG_OOB)); + pic_define_(pic, "*msg-oob*", pic_int_value(MSG_OOB)); #else - pic_define_(pic, "*msg-oob*", pic_false_value()); + pic_define_(pic, "*msg-oob*", pic_false_value()); #endif #ifdef MSG_WAITALL - pic_define_(pic, "*msg-waitall*", pic_int_value(MSG_WAITALL)); + pic_define_(pic, "*msg-waitall*", pic_int_value(MSG_WAITALL)); #else - pic_define_(pic, "*msg-waitall*", pic_false_value()); + pic_define_(pic, "*msg-waitall*", pic_false_value()); #endif #ifdef SHUT_RD - pic_define_(pic, "*shut-rd*", pic_int_value(SHUT_RD)); + pic_define_(pic, "*shut-rd*", pic_int_value(SHUT_RD)); #else - pic_define_(pic, "*shut-rd*", pic_false_value()); + pic_define_(pic, "*shut-rd*", pic_false_value()); #endif #ifdef SHUT_WR - pic_define_(pic, "*shut-wr*", pic_int_value(SHUT_WR)); + pic_define_(pic, "*shut-wr*", pic_int_value(SHUT_WR)); #else - pic_define_(pic, "*shut-wr*", pic_false_value()); + pic_define_(pic, "*shut-wr*", pic_false_value()); #endif #ifdef SHUT_RDWR - pic_define_(pic, "*shut-rdwr*", pic_int_value(SHUT_RDWR)); + pic_define_(pic, "*shut-rdwr*", pic_int_value(SHUT_RDWR)); #else - pic_define_(pic, "*shut-rdwr*", pic_false_value()); + pic_define_(pic, "*shut-rdwr*", pic_false_value()); #endif - } } diff --git a/contrib/60.repl/repl.c b/contrib/60.repl/repl.c index 1398a202..1084618b 100644 --- a/contrib/60.repl/repl.c +++ b/contrib/60.repl/repl.c @@ -15,7 +15,7 @@ pic_repl_tty_p(pic_state *pic) void pic_init_repl(pic_state *pic) { - pic_deflibrary (pic, "(picrin repl)") { - pic_defun(pic, "tty?", pic_repl_tty_p); - } + pic_deflibrary(pic, "(picrin repl)"); + + pic_defun(pic, "tty?", pic_repl_tty_p); } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index ca13b709..632418d2 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -219,16 +219,13 @@ pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); -#define pic_deflibrary(pic, spec) \ - for (((assert(pic->prev_lib == NULL)), \ - (pic->prev_lib = pic->lib), \ - (pic->lib = pic_find_library(pic, pic_read_cstr(pic, (spec)))), \ - (pic->lib = pic->lib \ - ? pic->lib \ - : pic_make_library(pic, pic_read_cstr(pic, (spec))))); \ - pic->prev_lib != NULL; \ - ((pic->lib = pic->prev_lib), \ - (pic->prev_lib = NULL))) +#define pic_deflibrary(pic, spec) do { \ + pic_value libname = pic_read_cstr(pic, spec); \ + if (pic_find_library(pic, libname) == NULL) { \ + pic_make_library(pic, libname); \ + } \ + pic_in_library(pic, libname); \ + } while (0) void pic_warnf(pic_state *, const char *, ...); struct pic_string *pic_get_backtrace(pic_state *); diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 2cef1abc..1778b8ed 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -58,7 +58,7 @@ struct pic_state { pic_value ptable; /* list of ephemerons */ - struct pic_lib *lib, *prev_lib; + struct pic_lib *lib; pic_sym *sDEFINE, *sDEFINE_MACRO, *sLAMBDA, *sIF, *sBEGIN, *sSETBANG; pic_sym *sQUOTE, *sQUASIQUOTE, *sUNQUOTE, *sUNQUOTE_SPLICING; diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 1bf6444b..deae6cab 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -55,6 +55,17 @@ pic_make_library(pic_state *pic, pic_value name) return lib; } +void +pic_in_library(pic_state *pic, pic_value name) +{ + struct pic_lib *lib; + + if ((lib = pic_find_library(pic, name)) == NULL) { + pic_errorf(pic, "library not found ~s", name); + } + pic->lib = lib; +} + struct pic_lib * pic_find_library(pic_state *pic, pic_value spec) { diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 62dd9ba3..55a770eb 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -179,13 +179,8 @@ static pic_value vm_gref(pic_state *pic, pic_sym *uid) { if (! pic_weak_has(pic, pic->globals, uid)) { - pic_weak_set(pic, pic->globals, uid, pic_invalid_value()); - pic_errorf(pic, "uninitialized global variable: %s", pic_symbol_name(pic, uid)); - - return pic_invalid_value(); } - return pic_weak_ref(pic, pic->globals, uid); } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index d2ac63c4..c3ce33b3 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -116,71 +116,72 @@ static void pic_init_core(pic_state *pic) { struct pic_box *pic_vm_gref_slot(pic_state *, pic_sym *); + size_t ai; pic_init_features(pic); - pic_deflibrary (pic, "(picrin base)") { - size_t ai = pic_gc_arena_preserve(pic); + pic_deflibrary(pic, "(picrin base)"); + + ai = pic_gc_arena_preserve(pic); #define DONE pic_gc_arena_restore(pic, ai); - import_builtin_syntax("define"); - import_builtin_syntax("set!"); - import_builtin_syntax("quote"); - import_builtin_syntax("lambda"); - import_builtin_syntax("if"); - import_builtin_syntax("begin"); - import_builtin_syntax("define-macro"); + import_builtin_syntax("define"); + import_builtin_syntax("set!"); + import_builtin_syntax("quote"); + import_builtin_syntax("lambda"); + import_builtin_syntax("if"); + import_builtin_syntax("begin"); + import_builtin_syntax("define-macro"); - declare_vm_procedure("cons"); - declare_vm_procedure("car"); - declare_vm_procedure("cdr"); - declare_vm_procedure("null?"); - declare_vm_procedure("symbol?"); - declare_vm_procedure("pair?"); - declare_vm_procedure("+"); - declare_vm_procedure("-"); - declare_vm_procedure("*"); - declare_vm_procedure("/"); - declare_vm_procedure("="); - declare_vm_procedure("<"); - declare_vm_procedure(">"); - declare_vm_procedure("<="); - declare_vm_procedure(">="); - declare_vm_procedure("not"); + declare_vm_procedure("cons"); + declare_vm_procedure("car"); + declare_vm_procedure("cdr"); + declare_vm_procedure("null?"); + declare_vm_procedure("symbol?"); + declare_vm_procedure("pair?"); + declare_vm_procedure("+"); + declare_vm_procedure("-"); + declare_vm_procedure("*"); + declare_vm_procedure("/"); + declare_vm_procedure("="); + declare_vm_procedure("<"); + declare_vm_procedure(">"); + declare_vm_procedure("<="); + declare_vm_procedure(">="); + declare_vm_procedure("not"); - DONE; + DONE; - pic_init_bool(pic); DONE; - pic_init_pair(pic); DONE; - pic_init_port(pic); DONE; - pic_init_number(pic); DONE; - pic_init_proc(pic); DONE; - pic_init_symbol(pic); DONE; - pic_init_vector(pic); DONE; - pic_init_blob(pic); DONE; - pic_init_cont(pic); DONE; - pic_init_char(pic); DONE; - pic_init_error(pic); DONE; - pic_init_str(pic); DONE; - pic_init_var(pic); DONE; - pic_init_write(pic); DONE; - pic_init_read(pic); DONE; - pic_init_dict(pic); DONE; - pic_init_record(pic); DONE; - pic_init_eval(pic); DONE; - pic_init_lib(pic); DONE; - pic_init_weak(pic); DONE; + pic_init_bool(pic); DONE; + pic_init_pair(pic); DONE; + pic_init_port(pic); DONE; + pic_init_number(pic); DONE; + pic_init_proc(pic); DONE; + pic_init_symbol(pic); DONE; + pic_init_vector(pic); DONE; + pic_init_blob(pic); DONE; + pic_init_cont(pic); DONE; + pic_init_char(pic); DONE; + pic_init_error(pic); DONE; + pic_init_str(pic); DONE; + pic_init_var(pic); DONE; + pic_init_write(pic); DONE; + pic_init_read(pic); DONE; + pic_init_dict(pic); DONE; + pic_init_record(pic); DONE; + pic_init_eval(pic); DONE; + pic_init_lib(pic); DONE; + pic_init_weak(pic); DONE; - pic_defun(pic, "features", pic_features); + pic_defun(pic, "features", pic_features); - pic_try { - pic_load_cstr(pic, &pic_boot[0][0]); - } - pic_catch { - pic_print_backtrace(pic, xstdout); - pic_panic(pic, ""); - } + pic_try { + pic_load_cstr(pic, &pic_boot[0][0]); + } + pic_catch { + pic_print_backtrace(pic, xstdout); + pic_panic(pic, ""); } } @@ -348,7 +349,6 @@ pic_open(pic_allocf allocf, void *userdata) pic->PICRIN_BASE = pic_make_library(pic, pic_read_cstr(pic, "(picrin base)")); pic->PICRIN_USER = pic_make_library(pic, pic_read_cstr(pic, "(picrin user)")); pic->lib = pic->PICRIN_USER; - pic->prev_lib = NULL; pic_gc_arena_restore(pic, ai); diff --git a/src/main.c b/src/main.c index 78a4dcc2..6b69accd 100644 --- a/src/main.c +++ b/src/main.c @@ -26,9 +26,8 @@ pic_init_picrin(pic_state *pic) { pic_add_feature(pic, "r7rs"); - pic_deflibrary (pic, "(picrin library)") { - pic_defun(pic, "libraries", pic_libraries); - } + pic_deflibrary(pic, "(picrin library)"); + pic_defun(pic, "libraries", pic_libraries); pic_init_contrib(pic); pic_load_piclib(pic); From 95740b86a0eb26bb8c6df074e65ff6d59b39954f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 16:29:13 +0900 Subject: [PATCH 018/119] remove libraries procedure --- contrib/20.r7rs/src/r7rs.c | 2 ++ src/main.c | 23 ++--------------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/contrib/20.r7rs/src/r7rs.c b/contrib/20.r7rs/src/r7rs.c index ad3090aa..301f9152 100644 --- a/contrib/20.r7rs/src/r7rs.c +++ b/contrib/20.r7rs/src/r7rs.c @@ -18,4 +18,6 @@ pic_init_r7rs(pic_state *pic) pic_init_mutable_string(pic); pic_init_system(pic); pic_init_time(pic); + + pic_add_feature(pic, "r7rs"); } diff --git a/src/main.c b/src/main.c index 6b69accd..5168b502 100644 --- a/src/main.c +++ b/src/main.c @@ -4,30 +4,11 @@ #include "picrin.h" -void pic_init_contrib(pic_state *); -void pic_load_piclib(pic_state *); - -static pic_value -pic_libraries(pic_state *pic) -{ - pic_value libs = pic_nil_value(), lib, it; - - pic_get_args(pic, ""); - - pic_for_each (lib, pic->libs, it) { - libs = pic_cons(pic, pic_car(pic, lib), libs); - } - - return libs; -} - void pic_init_picrin(pic_state *pic) { - pic_add_feature(pic, "r7rs"); - - pic_deflibrary(pic, "(picrin library)"); - pic_defun(pic, "libraries", pic_libraries); + void pic_init_contrib(pic_state *); + void pic_load_piclib(pic_state *); pic_init_contrib(pic); pic_load_piclib(pic); From 7816be80c1716a61b85db06923e636874fa4afeb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 16:38:59 +0900 Subject: [PATCH 019/119] pic_lookup_identifier -> pic_find_identifier --- extlib/benz/bool.c | 4 ++-- extlib/benz/include/picrin/macro.h | 1 - extlib/benz/macro.c | 18 +++++++++--------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index e54a8988..aa4a888f 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -45,8 +45,8 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) id1 = pic_id_ptr(x); id2 = pic_id_ptr(y); - s1 = pic_lookup_identifier(pic, id1->u.id.id, id1->u.id.env); - s2 = pic_lookup_identifier(pic, id2->u.id.id, id2->u.id.env); + s1 = pic_find_identifier(pic, id1->u.id.id, id1->u.id.env); + s2 = pic_find_identifier(pic, id2->u.id.id, id2->u.id.env); return s1 == s2; } diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index e52196ff..8a79ecae 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -27,7 +27,6 @@ struct pic_env *pic_make_env(pic_state *, struct pic_env *); pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); -pic_sym *pic_lookup_identifier(pic_state *, pic_id *, struct pic_env *); pic_value pic_expand(pic_state *, pic_value, struct pic_env *); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 27bd76e0..17cad2c2 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -64,7 +64,7 @@ pic_put_identifier(pic_state *pic, pic_id *id, pic_sym *uid, struct pic_env *env } pic_sym * -pic_find_identifier(pic_state PIC_UNUSED(*pic), pic_id *id, struct pic_env *env) +search_scope(pic_state *pic, pic_id *id, struct pic_env *env) { khiter_t it; @@ -76,12 +76,12 @@ pic_find_identifier(pic_state PIC_UNUSED(*pic), pic_id *id, struct pic_env *env) } static pic_sym * -lookup(pic_state *pic, pic_id *id, struct pic_env *env) +search(pic_state *pic, pic_id *id, struct pic_env *env) { pic_sym *uid = NULL; while (env != NULL) { - uid = pic_find_identifier(pic, id, env); + uid = search_scope(pic, id, env); if (uid != NULL) { break; } @@ -91,11 +91,11 @@ lookup(pic_state *pic, pic_id *id, struct pic_env *env) } pic_sym * -pic_lookup_identifier(pic_state *pic, pic_id *id, struct pic_env *env) +pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { pic_sym *uid; - while ((uid = lookup(pic, id, env)) == NULL) { + while ((uid = search(pic, id, env)) == NULL) { if (pic_sym_p(pic_obj_value(id))) { break; } @@ -152,7 +152,7 @@ expand_var(pic_state *pic, pic_id *id, struct pic_env *env, pic_value deferred) struct pic_proc *mac; pic_sym *functor; - functor = pic_lookup_identifier(pic, id, env); + functor = pic_find_identifier(pic, id, env); if ((mac = find_macro(pic, functor)) != NULL) { return expand(pic, pic_call(pic, mac, 2, pic_obj_value(id), pic_obj_value(env)), env, deferred); @@ -248,7 +248,7 @@ expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value def pic_value val; id = pic_id_ptr(pic_cadr(pic, expr)); - if ((uid = pic_find_identifier(pic, id, env)) == NULL) { + if ((uid = search_scope(pic, id, env)) == NULL) { uid = pic_add_identifier(pic, id, env); } else { shadow_macro(pic, uid); @@ -267,7 +267,7 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) pic_sym *uid; id = pic_id_ptr(pic_cadr(pic, expr)); - if ((uid = pic_find_identifier(pic, id, env)) == NULL) { + if ((uid = search_scope(pic, id, env)) == NULL) { uid = pic_add_identifier(pic, id, env); } @@ -299,7 +299,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer if (pic_id_p(pic_car(pic, expr))) { pic_sym *functor; - functor = pic_lookup_identifier(pic, pic_id_ptr(pic_car(pic, expr)), env); + functor = pic_find_identifier(pic, pic_id_ptr(pic_car(pic, expr)), env); if (functor == pic->sDEFINE_MACRO) { return expand_defmacro(pic, expr, env); From 561c350a1239bbe29ed90bfbd980b143599e4a84 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 17:39:32 +0900 Subject: [PATCH 020/119] library is now a non-first-class object --- contrib/10.callcc/callcc.c | 6 +- contrib/10.math/math.c | 2 +- contrib/20.r7rs/scheme/eval.scm | 11 +- contrib/20.r7rs/scheme/r5rs.scm | 4 +- contrib/20.r7rs/src/file.c | 2 +- contrib/20.r7rs/src/load.c | 2 +- contrib/20.r7rs/src/mutable-string.c | 2 +- contrib/20.r7rs/src/system.c | 2 +- contrib/20.r7rs/src/time.c | 2 +- contrib/30.random/src/random.c | 2 +- contrib/30.readline/src/readline.c | 4 +- contrib/30.regexp/src/regexp.c | 2 +- contrib/40.srfi/src/106.c | 6 +- contrib/60.repl/repl.c | 2 +- contrib/60.repl/repl.scm | 4 +- contrib/70.main/main.scm | 2 +- extlib/benz/boot.c | 126 +++++++++++-------- extlib/benz/eval.c | 24 ++-- extlib/benz/gc.c | 25 ++-- extlib/benz/include/picrin.h | 33 ++--- extlib/benz/include/picrin/khash.h | 9 ++ extlib/benz/include/picrin/lib.h | 26 ---- extlib/benz/include/picrin/macro.h | 2 +- extlib/benz/include/picrin/state.h | 14 ++- extlib/benz/include/picrin/type.h | 4 - extlib/benz/lib.c | 176 +++++++++++++++------------ extlib/benz/load.c | 2 +- extlib/benz/macro.c | 8 +- extlib/benz/port.c | 2 +- extlib/benz/proc.c | 54 ++++---- extlib/benz/state.c | 17 +-- extlib/benz/string.c | 4 +- extlib/benz/vector.c | 4 +- src/main.c | 5 +- 34 files changed, 308 insertions(+), 282 deletions(-) delete mode 100644 extlib/benz/include/picrin/lib.h diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index f4a82670..51327757 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -282,8 +282,6 @@ pic_callcc_callcc(pic_state *pic) void pic_init_callcc(pic_state *pic) { - pic_deflibrary(pic, "(scheme base)"); - - pic_redefun(pic, pic->PICRIN_BASE, "call-with-current-continuation", pic_callcc_callcc); - pic_redefun(pic, pic->PICRIN_BASE, "call/cc", pic_callcc_callcc); + pic_redefun(pic, "picrin.base", "call-with-current-continuation", pic_callcc_callcc); + pic_redefun(pic, "picrin.base", "call/cc", pic_callcc_callcc); } diff --git a/contrib/10.math/math.c b/contrib/10.math/math.c index 84584caf..81c04927 100644 --- a/contrib/10.math/math.c +++ b/contrib/10.math/math.c @@ -284,7 +284,7 @@ pic_number_expt(pic_state *pic) void pic_init_math(pic_state *pic) { - pic_deflibrary(pic, "(picrin math)"); + pic_deflibrary(pic, "picrin.math"); pic_defun(pic, "floor/", pic_number_floor2); pic_defun(pic, "truncate/", pic_number_trunc2); diff --git a/contrib/20.r7rs/scheme/eval.scm b/contrib/20.r7rs/scheme/eval.scm index c914ad7d..598d99b8 100644 --- a/contrib/20.r7rs/scheme/eval.scm +++ b/contrib/20.r7rs/scheme/eval.scm @@ -6,14 +6,11 @@ (define-syntax (inc! n) #`(set! #,n (+ #,n 1))) - (define (number->symbol n) - (string->symbol (number->string n))) - (define (environment . specs) - (let ((library-name `(picrin @@my-environment ,(number->symbol counter)))) + (let ((lib (string-append "picrin.@@my-environment." (number->string counter)))) (inc! counter) - (let ((lib (make-library library-name))) - (eval `(import ,@specs) lib) - lib))) + (make-library lib) + (eval `(import ,@specs) lib) + lib)) (export environment eval)) diff --git a/contrib/20.r7rs/scheme/r5rs.scm b/contrib/20.r7rs/scheme/r5rs.scm index 7d557027..a9f20eb2 100644 --- a/contrib/20.r7rs/scheme/r5rs.scm +++ b/contrib/20.r7rs/scheme/r5rs.scm @@ -28,12 +28,12 @@ (define (null-environment n) (if (not (= n 5)) (error "unsupported environment version" n) - (find-library '(scheme null)))) + "scheme.null")) (define (scheme-report-environment n) (if (not (= n 5)) (error "unsupported environment version" n) - (find-library '(scheme r5rs)))) + "scheme.r5rs")) (export * + - / < <= = > >= abs acos and diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 609592ea..d6a1135b 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -93,7 +93,7 @@ pic_file_delete(pic_state *pic) void pic_init_file(pic_state *pic) { - pic_deflibrary(pic, "(scheme file)"); + pic_deflibrary(pic, "scheme.file"); pic_defun(pic, "open-input-file", pic_file_open_input_file); pic_defun(pic, "open-binary-input-file", pic_file_open_binary_input_file); diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index 58a48c3c..aed45506 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -25,7 +25,7 @@ pic_load_load(pic_state *pic) void pic_init_load(pic_state *pic) { - pic_deflibrary(pic, "(scheme load)"); + pic_deflibrary(pic, "scheme.load"); pic_defun(pic, "load", pic_load_load); } diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index 6d12deea..2d360c6c 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -85,7 +85,7 @@ pic_str_string_fill_ip(pic_state *pic) void pic_init_mutable_string(pic_state *pic) { - pic_deflibrary(pic, "(picrin string)"); + pic_deflibrary(pic, "picrin.string"); pic_defun(pic, "string-set!", pic_str_string_set); pic_defun(pic, "string-copy!", pic_str_string_copy_ip); diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 0ec818fd..53acc81f 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -127,7 +127,7 @@ pic_system_getenvs(pic_state *pic) void pic_init_system(pic_state *pic) { - pic_deflibrary(pic, "(scheme process-context)"); + pic_deflibrary(pic, "scheme.process-context"); pic_defun(pic, "command-line", pic_system_cmdline); pic_defun(pic, "exit", pic_system_exit); diff --git a/contrib/20.r7rs/src/time.c b/contrib/20.r7rs/src/time.c index 4cab8aca..ba34d4eb 100644 --- a/contrib/20.r7rs/src/time.c +++ b/contrib/20.r7rs/src/time.c @@ -41,7 +41,7 @@ pic_jiffies_per_second(pic_state *pic) void pic_init_time(pic_state *pic) { - pic_deflibrary(pic, "(scheme time)"); + pic_deflibrary(pic, "scheme.time"); pic_defun(pic, "current-second", pic_current_second); pic_defun(pic, "current-jiffy", pic_current_jiffy); diff --git a/contrib/30.random/src/random.c b/contrib/30.random/src/random.c index 61e33633..6eb2ee11 100644 --- a/contrib/30.random/src/random.c +++ b/contrib/30.random/src/random.c @@ -13,7 +13,7 @@ pic_random_real(pic_state *pic) void pic_init_random(pic_state *pic) { - pic_deflibrary(pic, "(srfi 27)"); + pic_deflibrary(pic, "srfi.27"); pic_defun(pic, "random-real", pic_random_real); } diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index 26df7c82..9b95e2ad 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -247,11 +247,11 @@ void pic_init_readline(pic_state *pic){ using_history(); - pic_deflibrary(pic, "(picrin readline)"); + pic_deflibrary(pic, "picrin.readline"); pic_defun(pic, "readline", pic_rl_readline); - pic_deflibrary(pic, "(picrin readline history)"); + pic_deflibrary(pic, "picrin.readline.history"); /* pic_defun(pic, "history-offset", pic_rl_history_offset); */ pic_defun(pic, "history-length", pic_rl_history_length); diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index fd88e290..5cfc1ccb 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -178,7 +178,7 @@ pic_regexp_regexp_replace(pic_state *pic) void pic_init_regexp(pic_state *pic) { - pic_deflibrary(pic, "(picrin regexp)"); + pic_deflibrary(pic, "picrin.regexp"); pic_defun(pic, "regexp", pic_regexp_regexp); pic_defun(pic, "regexp?", pic_regexp_regexp_p); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 42349d68..d6598d9a 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -399,10 +399,10 @@ pic_socket_call_with_socket(pic_state *pic) void pic_init_srfi_106(pic_state *pic) { -#define pic_defun_(pic, name, f) pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))) -#define pic_define_(pic, name, v) pic_define(pic, pic->lib, name, v) + pic_deflibrary(pic, "srfi.106"); - pic_deflibrary(pic, "(srfi 106)"); +#define pic_defun_(pic, name, f) pic_define(pic, "srfi.106", name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))) +#define pic_define_(pic, name, v) pic_define(pic, "srfi.106", name, v) pic_defun_(pic, "socket?", pic_socket_socket_p); pic_defun_(pic, "make-socket", pic_socket_make_socket); diff --git a/contrib/60.repl/repl.c b/contrib/60.repl/repl.c index 1084618b..cea0ed22 100644 --- a/contrib/60.repl/repl.c +++ b/contrib/60.repl/repl.c @@ -15,7 +15,7 @@ pic_repl_tty_p(pic_state *pic) void pic_init_repl(pic_state *pic) { - pic_deflibrary(pic, "(picrin repl)"); + pic_deflibrary(pic, "picrin.repl"); pic_defun(pic, "tty?", pic_repl_tty_p); } diff --git a/contrib/60.repl/repl.scm b/contrib/60.repl/repl.scm index 742bdaa7..698c77c5 100644 --- a/contrib/60.repl/repl.scm +++ b/contrib/60.repl/repl.scm @@ -34,7 +34,7 @@ (scheme eval) (scheme r5rs) (picrin macro)) - (find-library '(picrin user)))) + "picrin.user")) (define (repl) (init-env) @@ -65,7 +65,7 @@ (lambda (port) (let next ((expr (read port))) (unless (eof-object? expr) - (write (eval expr (find-library '(picrin user)))) + (write (eval expr "picrin.user")) (newline) (set! str "") (next (read port)))))))))) diff --git a/contrib/70.main/main.scm b/contrib/70.main/main.scm index f0e48e9c..27e800b3 100644 --- a/contrib/70.main/main.scm +++ b/contrib/70.main/main.scm @@ -41,7 +41,7 @@ (lambda (in) (let loop ((expr (read in))) (unless (eof-object? expr) - (eval expr (find-library '(picrin user))) + (eval expr (find-library "picrin.user")) (loop (read in))))))) (define (main) diff --git a/extlib/benz/boot.c b/extlib/benz/boot.c index 8dff52fe..9ad798e3 100644 --- a/extlib/benz/boot.c +++ b/extlib/benz/boot.c @@ -542,12 +542,24 @@ my $src = <<'EOL'; ;;; library primitives +(define (mangle name) + (define (->string n) + (if (symbol? n) + (symbol->string n) + (number->string n))) + (define (join strs delim) + (let loop ((res (car strs)) (strs (cdr strs))) + (if (null? strs) + res + (loop (string-append res delim (car strs)) (cdr strs))))) + (join (map ->string name) ".")) + (define-macro define-library (lambda (form _) - (let ((name (cadr form)) + (let ((lib (mangle (cadr form))) (body (cddr form))) - (let ((new-library (or (find-library name) (make-library name)))) - (for-each (lambda (expr) (eval expr new-library)) body))))) + (or (find-library lib) (make-library lib)) + (for-each (lambda (expr) (eval expr lib)) body)))) (define-macro cond-expand (lambda (form _) @@ -559,7 +571,7 @@ my $src = <<'EOL'; (memq form (features))) (and (pair? form) (case (car form) - ((library) (find-library (cadr form))) + ((library) (find-library (mangle (cadr form)))) ((not) (not (test (cadr form)))) ((and) (let loop ((form (cdr form))) (or (null? form) @@ -584,7 +596,13 @@ my $src = <<'EOL'; (string->symbol (string-append (symbol->string prefix) - (symbol->string symbol)))))) + (symbol->string symbol))))) + (getlib + (lambda (name) + (let ((lib (mangle name))) + (if (find-library lib) + lib + (error "library not found" name)))))) (letrec ((extract (lambda (spec) @@ -592,7 +610,7 @@ my $src = <<'EOL'; ((only rename prefix except) (extract (cadr spec))) (else - (or (find-library spec) (error "library not found" spec)))))) + (getlib spec))))) (collect (lambda (spec) (case (car spec) @@ -615,8 +633,7 @@ my $src = <<'EOL'; (loop (cdr alist)) (cons (car alist) (loop (cdr alist)))))))) (else - (let ((lib (or (find-library spec) (error "library not found" spec)))) - (map (lambda (x) (cons x x)) (library-exports lib)))))))) + (map (lambda (x) (cons x x)) (library-exports (getlib spec)))))))) (letrec ((import (lambda (spec) @@ -948,31 +965,37 @@ const char pic_boot[][80] = { "rm))))\n `(let ()\n ,@(map (lambda (x)\n `(,(the 'def", "ine-syntax) ,(car x) ,(cadr x)))\n formal)\n ,@body))))\n\n(d", "efine-macro let-syntax\n (lambda (form env)\n `(,(the 'letrec-syntax) ,@(cdr f", -"orm))))\n\n\n;;; library primitives\n\n(define-macro define-library\n (lambda (form _", -")\n (let ((name (cadr form))\n (body (cddr form)))\n (let ((new-li", -"brary (or (find-library name) (make-library name))))\n (for-each (lambda (", -"expr) (eval expr new-library)) body)))))\n\n(define-macro cond-expand\n (lambda (f", -"orm _)\n (letrec\n ((test (lambda (form)\n (or\n ", -" (eq? form 'else)\n (and (symbol? form)\n ", -" (memq form (features)))\n (and (pair? form)\n ", -" (case (car form)\n ((library) (find-library (cad", -"r form)))\n ((not) (not (test (cadr form))))\n ", -" ((and) (let loop ((form (cdr form)))\n ", -" (or (null? form)\n (and (test (car form)", -") (loop (cdr form))))))\n ((or) (let loop ((form (cdr for", -"m)))\n (and (pair? form)\n ", -" (or (test (car form)) (loop (cdr form))))))\n ", -" (else #f)))))))\n (let loop ((clauses (cdr form)))\n (if (null? cla", -"uses)\n #undefined\n (if (test (caar clauses))\n ", -" `(,the-begin ,@(cdar clauses))\n (loop (cdr clauses))))))))\n\n(d", -"efine-macro import\n (lambda (form _)\n (let ((caddr\n (lambda (x) (c", -"ar (cdr (cdr x)))))\n (prefix\n (lambda (prefix symbol)\n ", -" (string->symbol\n (string-append\n (symbol->strin", -"g prefix)\n (symbol->string symbol))))))\n (letrec\n ((", -"extract\n (lambda (spec)\n (case (car spec)\n ", -" ((only rename prefix except)\n (extract (cadr spec)))\n ", -" (else\n (or (find-library spec) (error \"library not found\"", -" spec))))))\n (collect\n (lambda (spec)\n (case (", +"orm))))\n\n\n;;; library primitives\n\n(define (mangle name)\n (define (->string n)\n ", +" (if (symbol? n)\n (symbol->string n)\n (number->string n)))\n (de", +"fine (join strs delim)\n (let loop ((res (car strs)) (strs (cdr strs)))\n ", +"(if (null? strs)\n res\n (loop (string-append res delim (car str", +"s)) (cdr strs)))))\n (join (map ->string name) \".\"))\n\n(define-macro define-libra", +"ry\n (lambda (form _)\n (let ((lib (mangle (cadr form)))\n (body (cddr", +" form)))\n (or (find-library lib) (make-library lib))\n (for-each (lambd", +"a (expr) (eval expr lib)) body))))\n\n(define-macro cond-expand\n (lambda (form _)", +"\n (letrec\n ((test (lambda (form)\n (or\n ", +" (eq? form 'else)\n (and (symbol? form)\n ", +"(memq form (features)))\n (and (pair? form)\n ", +" (case (car form)\n ((library) (find-library (mangle (c", +"adr form))))\n ((not) (not (test (cadr form))))\n ", +" ((and) (let loop ((form (cdr form)))\n ", +" (or (null? form)\n (and (test (car fo", +"rm)) (loop (cdr form))))))\n ((or) (let loop ((form (cdr ", +"form)))\n (and (pair? form)\n ", +" (or (test (car form)) (loop (cdr form))))))\n ", +" (else #f)))))))\n (let loop ((clauses (cdr form)))\n (if (null? ", +"clauses)\n #undefined\n (if (test (caar clauses))\n ", +" `(,the-begin ,@(cdar clauses))\n (loop (cdr clauses))))))))\n", +"\n(define-macro import\n (lambda (form _)\n (let ((caddr\n (lambda (x)", +" (car (cdr (cdr x)))))\n (prefix\n (lambda (prefix symbol)\n ", +" (string->symbol\n (string-append\n (symbol->st", +"ring prefix)\n (symbol->string symbol)))))\n (getlib\n ", +" (lambda (name)\n (let ((lib (mangle name)))\n (if (", +"find-library lib)\n lib\n (error \"library not ", +"found\" name))))))\n (letrec\n ((extract\n (lambda (spec)\n ", +" (case (car spec)\n ((only rename prefix except)\n ", +" (extract (cadr spec)))\n (else\n (getli", +"b spec)))))\n (collect\n (lambda (spec)\n (case (", "car spec)\n ((only)\n (let ((alist (collect (cadr s", "pec))))\n (map (lambda (var) (assq var alist)) (cddr spec))))\n ", " ((rename)\n (let ((alist (collect (cadr spec)))\n ", @@ -985,25 +1008,24 @@ const char pic_boot[][80] = { "f (null? alist)\n '()\n (if (memq ", "(caar alist) (cddr spec))\n (loop (cdr alist))\n ", " (cons (car alist) (loop (cdr alist))))))))\n ", -" (else\n (let ((lib (or (find-library spec) (error \"library not ", -"found\" spec))))\n (map (lambda (x) (cons x x)) (library-exports", -" lib))))))))\n (letrec\n ((import\n (lambda (spec)\n", -" (let ((lib (extract spec))\n (alist (colle", -"ct spec)))\n (for-each\n (lambda (slot)\n ", -" (library-import lib (cdr slot) (car slot)))\n ", -" alist)))))\n (for-each import (cdr form)))))))\n\n(define-macro export\n ", -"(lambda (form _)\n (letrec\n ((collect\n (lambda (spec)\n ", -" (cond\n ((symbol? spec)\n `(,spec . ,spec))\n ", -" ((and (list? spec) (= (length spec) 3) (eq? (car spec) 'rename))\n ", -" `(,(list-ref spec 1) . ,(list-ref spec 2)))\n (else\n ", -"(error \"malformed export\")))))\n (export\n (lambda (spec)\n ", -" (let ((slot (collect spec)))\n (library-export (car slot) (c", -"dr slot))))))\n (for-each export (cdr form)))))\n\n(export define lambda quote", -" set! if begin define-macro\n let let* letrec letrec*\n let-values l", -"et*-values define-values\n quasiquote unquote unquote-splicing\n and", -" or\n cond case else =>\n do when unless\n parameterize\n ", -" define-syntax\n syntax-quote syntax-unquote\n syntax-quasiquote sy", -"ntax-unquote-splicing\n let-syntax letrec-syntax\n syntax-error)\n\n\n", +" (else\n (map (lambda (x) (cons x x)) (library-exports (getlib s", +"pec))))))))\n (letrec\n ((import\n (lambda (spec)\n ", +" (let ((lib (extract spec))\n (alist (collec", +"t spec)))\n (for-each\n (lambda (slot)\n ", +" (library-import lib (cdr slot) (car slot)))\n ", +"alist)))))\n (for-each import (cdr form)))))))\n\n(define-macro export\n (", +"lambda (form _)\n (letrec\n ((collect\n (lambda (spec)\n ", +" (cond\n ((symbol? spec)\n `(,spec . ,spec))\n ", +" ((and (list? spec) (= (length spec) 3) (eq? (car spec) 'rename))\n ", +" `(,(list-ref spec 1) . ,(list-ref spec 2)))\n (else\n (", +"error \"malformed export\")))))\n (export\n (lambda (spec)\n ", +" (let ((slot (collect spec)))\n (library-export (car slot) (cd", +"r slot))))))\n (for-each export (cdr form)))))\n\n(export define lambda quote ", +"set! if begin define-macro\n let let* letrec letrec*\n let-values le", +"t*-values define-values\n quasiquote unquote unquote-splicing\n and ", +"or\n cond case else =>\n do when unless\n parameterize\n ", +" define-syntax\n syntax-quote syntax-unquote\n syntax-quasiquote syn", +"tax-unquote-splicing\n let-syntax letrec-syntax\n syntax-error)\n\n\n", "", "" }; diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index b825e077..80799d47 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -885,20 +885,23 @@ pic_compile(pic_state *pic, pic_value obj) } pic_value -pic_eval(pic_state *pic, pic_value program, struct pic_lib *lib) +pic_eval(pic_state *pic, pic_value program, const char *lib) { - struct pic_lib *prev_lib = pic->lib; + const char *prev_lib = pic_current_library(pic); + struct pic_env *env; pic_value r; - pic->lib = lib; + env = pic_library_environment(pic, lib); + + pic_in_library(pic, lib); pic_try { - r = pic_call(pic, pic_compile(pic, pic_expand(pic, program, lib->env)), 0); + r = pic_call(pic, pic_compile(pic, pic_expand(pic, program, env)), 0); } pic_catch { - pic->lib = prev_lib; + pic_in_library(pic, prev_lib); pic_raise(pic, pic->err); } - pic->lib = prev_lib; + pic_in_library(pic, prev_lib); return r; } @@ -906,13 +909,12 @@ pic_eval(pic_state *pic, pic_value program, struct pic_lib *lib) static pic_value pic_eval_eval(pic_state *pic) { - pic_value program, lib; + pic_value program; + const char *str; - pic_get_args(pic, "oo", &program, &lib); + pic_get_args(pic, "oz", &program, &str); - pic_assert_type(pic, lib, lib); - - return pic_eval(pic, program, pic_lib_ptr(lib)); + return pic_eval(pic, program, str); } void diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 1305ea79..558bcf82 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -34,7 +34,6 @@ struct pic_object { struct pic_context cxt; struct pic_port port; struct pic_error err; - struct pic_lib lib; struct pic_checkpoint cp; } u; }; @@ -347,20 +346,11 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) gc_mark_object(pic, (struct pic_object *)kh_val(h, it)); } } - if (obj->u.env.prefix) { - gc_mark_object(pic, (struct pic_object *)obj->u.env.prefix); - } if (obj->u.env.up) { LOOP(obj->u.env.up); } break; } - case PIC_TT_LIB: { - gc_mark(pic, obj->u.lib.name); - gc_mark_object(pic, (struct pic_object *)obj->u.lib.env); - LOOP(obj->u.lib.exports); - break; - } case PIC_TT_DATA: { if (obj->u.data.type->mark) { obj->u.data.type->mark(pic, obj->u.data.data, gc_mark); @@ -428,6 +418,7 @@ gc_mark_phase(pic_state *pic) pic_callinfo *ci; struct pic_proc **xhandler; struct pic_list *list; + khiter_t it; size_t j; assert(pic->heap->weaks == NULL); @@ -492,12 +483,19 @@ gc_mark_phase(pic_state *pic) /* features */ gc_mark(pic, pic->features); - /* library table */ - gc_mark(pic, pic->libs); - /* parameter table */ gc_mark(pic, pic->ptable); + /* library table */ + for (it = kh_begin(&pic->ltable); it != kh_end(&pic->ltable); ++it) { + if (! kh_exist(&pic->ltable, it)) { + continue; + } + gc_mark_object(pic, (struct pic_object *)kh_val(&pic->ltable, it).name); + gc_mark_object(pic, (struct pic_object *)kh_val(&pic->ltable, it).env); + gc_mark_object(pic, (struct pic_object *)kh_val(&pic->ltable, it).exports); + } + /* weak maps */ do { struct pic_object *key; @@ -580,7 +578,6 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) case PIC_TT_PORT: case PIC_TT_ERROR: case PIC_TT_ID: - case PIC_TT_LIB: case PIC_TT_RECORD: case PIC_TT_CP: break; diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 632418d2..b9931736 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -63,17 +63,18 @@ void pic_add_feature(pic_state *, const char *); void pic_defun(pic_state *, const char *, pic_func_t); void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *); -void pic_define(pic_state *, struct pic_lib *, const char *, pic_value); -pic_value pic_ref(pic_state *, struct pic_lib *, const char *); -void pic_set(pic_state *, struct pic_lib *, const char *, pic_value); +void pic_define(pic_state *, const char *, const char *, pic_value); +pic_value pic_ref(pic_state *, const char *, const char *); +void pic_set(pic_state *, const char *, const char *, pic_value); pic_value pic_closure_ref(pic_state *, int); void pic_closure_set(pic_state *, int, pic_value); -pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, int, ...); +pic_value pic_funcall(pic_state *pic, const char *, const char *, int, ...); -struct pic_lib *pic_make_library(pic_state *, pic_value); -void pic_in_library(pic_state *, pic_value); -struct pic_lib *pic_find_library(pic_state *, pic_value); -void pic_import(pic_state *, struct pic_lib *); +void pic_make_library(pic_state *, const char *); +void pic_in_library(pic_state *, const char *); +bool pic_find_library(pic_state *, const char *); +const char *pic_current_library(pic_state *); +void pic_import(pic_state *, const char *); void pic_export(pic_state *, pic_sym *); PIC_NORETURN void pic_panic(pic_state *, const char *); @@ -186,7 +187,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/data.h" #include "picrin/dict.h" #include "picrin/error.h" -#include "picrin/lib.h" #include "picrin/macro.h" #include "picrin/pair.h" #include "picrin/port.h" @@ -215,16 +215,15 @@ pic_value pic_read_cstr(pic_state *, const char *); void pic_load(pic_state *, struct pic_port *); void pic_load_cstr(pic_state *, const char *); -pic_value pic_eval(pic_state *, pic_value, struct pic_lib *); +pic_value pic_eval(pic_state *, pic_value, const char *); struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); -#define pic_deflibrary(pic, spec) do { \ - pic_value libname = pic_read_cstr(pic, spec); \ - if (pic_find_library(pic, libname) == NULL) { \ - pic_make_library(pic, libname); \ - } \ - pic_in_library(pic, libname); \ +#define pic_deflibrary(pic, lib) do { \ + if (! pic_find_library(pic, lib)) { \ + pic_make_library(pic, lib); \ + } \ + pic_in_library(pic, lib); \ } while (0) void pic_warnf(pic_state *, const char *, ...); @@ -242,6 +241,8 @@ void pic_fprintf(pic_state *, struct pic_port *, const char *, ...); pic_value pic_display(pic_state *, pic_value); pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); +struct pic_env *pic_library_environment(pic_state *, const char *); + #if DEBUG # define pic_debug(pic,obj) pic_fwrite(pic,obj,xstderr) # define pic_fdebug(pic,obj,file) pic_fwrite(pic,obj,file) diff --git a/extlib/benz/include/picrin/khash.h b/extlib/benz/include/picrin/khash.h index 157926ee..a75a5cec 100644 --- a/extlib/benz/include/picrin/khash.h +++ b/extlib/benz/include/picrin/khash.h @@ -206,6 +206,15 @@ typedef khint_t khiter_t; #define kh_ptr_hash_equal(a, b) ((a) == (b)) #define kh_int_hash_func(key) (int)(key) #define kh_int_hash_equal(a, b) ((a) == (b)) +PIC_INLINE int kh_str_hash_func(const char *s) { + int h = 0; + while (*s) { + h = (h << 5) - h + *s++; + } + return h; +} +#define kh_str_cmp_func(a, b) (strcmp((a), (b)) == 0) + /* --- END OF HASH FUNCTIONS --- */ diff --git a/extlib/benz/include/picrin/lib.h b/extlib/benz/include/picrin/lib.h deleted file mode 100644 index 50cd45fe..00000000 --- a/extlib/benz/include/picrin/lib.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_LIB_H -#define PICRIN_LIB_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_lib { - PIC_OBJECT_HEADER - pic_value name; - struct pic_env *env; - struct pic_dict *exports; -}; - -#define pic_lib_p(o) (pic_type(o) == PIC_TT_LIB) -#define pic_lib_ptr(o) ((struct pic_lib *)pic_ptr(o)) - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index 8a79ecae..0d4c6a40 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -15,7 +15,7 @@ struct pic_env { PIC_OBJECT_HEADER khash_t(env) map; struct pic_env *up; - struct pic_string *prefix; + struct pic_string *lib; }; #define pic_env_p(v) (pic_type(v) == PIC_TT_ENV) diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 1778b8ed..c5590788 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -16,7 +16,11 @@ extern "C" { #include "picrin/read.h" #include "picrin/gc.h" -KHASH_DECLARE(oblist, struct pic_string *, pic_sym *) +struct pic_lib { + struct pic_string *name; + struct pic_env *env; + struct pic_dict *exports; +}; typedef struct pic_checkpoint { PIC_OBJECT_HEADER @@ -37,6 +41,9 @@ typedef struct { struct pic_context *up; } pic_callinfo; +KHASH_DECLARE(oblist, struct pic_string *, pic_sym *) +KHASH_DECLARE(ltable, const char *, struct pic_lib) + struct pic_state { pic_allocf allocf; void *userdata; @@ -68,16 +75,13 @@ struct pic_state { pic_sym *sCONS, *sCAR, *sCDR, *sNILP, *sSYMBOLP, *sPAIRP; pic_sym *sADD, *sSUB, *sMUL, *sDIV, *sEQ, *sLT, *sLE, *sGT, *sGE, *sNOT; - struct pic_lib *PICRIN_BASE; - struct pic_lib *PICRIN_USER; - pic_value features; khash_t(oblist) oblist; /* string to symbol */ int ucnt; struct pic_weak *globals; struct pic_weak *macros; - pic_value libs; + khash_t(ltable) ltable; struct pic_list ireps; /* chain */ pic_reader reader; diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index 25bc9a4c..de8e174f 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -169,7 +169,6 @@ enum pic_tt { PIC_TT_ERROR, PIC_TT_ID, PIC_TT_ENV, - PIC_TT_LIB, PIC_TT_DATA, PIC_TT_DICT, PIC_TT_WEAK, @@ -197,7 +196,6 @@ struct pic_proc; struct pic_port; struct pic_error; struct pic_env; -struct pic_lib; /* set aliases to basic types */ typedef struct pic_symbol pic_sym; @@ -298,8 +296,6 @@ pic_type_repr(enum pic_tt tt) return "proc"; case PIC_TT_ENV: return "env"; - case PIC_TT_LIB: - return "lib"; case PIC_TT_DATA: return "data"; case PIC_TT_DICT: diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index deae6cab..ffccffd0 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -4,22 +4,38 @@ #include "picrin.h" +KHASH_DEFINE(ltable, const char *, struct pic_lib, kh_str_hash_func, kh_str_cmp_func) + +static struct pic_lib * +get_library_opt(pic_state *pic, const char *lib) +{ + khash_t(ltable) *h = &pic->ltable; + khiter_t it; + + it = kh_get(ltable, h, lib); + if (it == kh_end(h)) { + return NULL; + } + return &kh_val(h, it); +} + +static struct pic_lib * +get_library(pic_state *pic, const char *lib) +{ + struct pic_lib *libp; + + if ((libp = get_library_opt(pic, lib)) == NULL) { + pic_errorf(pic, "library not found: %s", lib); + } + return libp; +} + static struct pic_env * -make_library_env(pic_state *pic, pic_value name) +make_library_env(pic_state *pic, struct pic_string *name) { struct pic_env *env; - pic_value dir, it; - struct pic_string *prefix = NULL; - pic_for_each (dir, name, it) { - if (prefix == NULL) { - prefix = pic_format(pic, "~a", dir); - } else { - prefix = pic_format(pic, "~a.~a", pic_obj_value(prefix), dir); - } - } - - env = pic_make_topenv(pic, prefix); + env = pic_make_topenv(pic, name); /* set up default environment */ pic_put_identifier(pic, (pic_id *)pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, env); @@ -30,64 +46,76 @@ make_library_env(pic_state *pic, pic_value name) return env; } -struct pic_lib * -pic_make_library(pic_state *pic, pic_value name) +void +pic_make_library(pic_state *pic, const char *lib) { - struct pic_lib *lib; + khash_t(ltable) *h = &pic->ltable; + const char *old_lib; + struct pic_string *name; struct pic_env *env; struct pic_dict *exports; + khiter_t it; + int ret; - if ((lib = pic_find_library(pic, name)) != NULL) { - pic_errorf(pic, "library name already in use: ~s", name); + if (pic->lib) { + old_lib = pic_current_library(pic); } + name = pic_make_cstr(pic, lib); env = make_library_env(pic, name); exports = pic_make_dict(pic); - lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB); - lib->name = name; - lib->env = env; - lib->exports = exports; + it = kh_put(ltable, h, pic_str_cstr(pic, name), &ret); + if (ret == 0) { /* if exists */ + pic_errorf(pic, "library name already in use: %s", lib); + } - /* register! */ - pic->libs = pic_acons(pic, name, pic_obj_value(lib), pic->libs); + kh_val(h, it).name = name; + kh_val(h, it).env = env; + kh_val(h, it).exports = exports; - return lib; + if (pic->lib) { + pic->lib = get_library(pic, old_lib); /* ltable might be rehashed */ + } } void -pic_in_library(pic_state *pic, pic_value name) +pic_in_library(pic_state *pic, const char *lib) { - struct pic_lib *lib; - - if ((lib = pic_find_library(pic, name)) == NULL) { - pic_errorf(pic, "library not found ~s", name); - } - pic->lib = lib; + pic->lib = get_library(pic, lib); } -struct pic_lib * -pic_find_library(pic_state *pic, pic_value spec) +bool +pic_find_library(pic_state *pic, const char *lib) { - pic_value v; + return get_library_opt(pic, lib) != NULL; +} - v = pic_assoc(pic, spec, pic->libs, NULL); - if (pic_false_p(v)) { - return NULL; - } - return pic_lib_ptr(pic_cdr(pic, v)); +const char * +pic_current_library(pic_state *pic) +{ + return pic_str_cstr(pic, pic->lib->name); +} + +struct pic_env * +pic_library_environment(pic_state *pic, const char *lib) +{ + return get_library(pic, lib)->env; } void -pic_import(pic_state *pic, struct pic_lib *lib) +pic_import(pic_state *pic, const char *lib) { pic_sym *name, *realname, *uid; khiter_t it; + struct pic_lib *libp; - pic_dict_for_each (name, lib->exports, it) { - realname = pic_sym_ptr(pic_dict_ref(pic, lib->exports, name)); + libp = get_library(pic, lib); - if ((uid = pic_find_identifier(pic, (pic_id *)realname, lib->env)) == NULL) { + pic_dict_for_each (name, libp->exports, it) { + realname = pic_sym_ptr(pic_dict_ref(pic, libp->exports, name)); + + if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname)); } pic_put_identifier(pic, (pic_id *)name, uid, pic->lib->env); @@ -103,42 +131,38 @@ pic_export(pic_state *pic, pic_sym *name) static pic_value pic_lib_make_library(pic_state *pic) { - pic_value name; + const char *lib; - pic_get_args(pic, "o", &name); + pic_get_args(pic, "z", &lib); - return pic_obj_value(pic_make_library(pic, name)); + pic_make_library(pic, lib); + + return pic_undef_value(); } static pic_value pic_lib_find_library(pic_state *pic) { - pic_value name; - struct pic_lib *lib; + const char *lib; - pic_get_args(pic, "o", &name); + pic_get_args(pic, "z", &lib); - if ((lib = pic_find_library(pic, name)) == NULL) { - return pic_false_value(); - } - return pic_obj_value(lib); + return pic_bool_value(pic_find_library(pic, lib)); } static pic_value pic_lib_current_library(pic_state *pic) { - pic_value lib; + const char *lib; int n; - n = pic_get_args(pic, "|o", &lib); + n = pic_get_args(pic, "|z", &lib); if (n == 0) { - return pic_obj_value(pic->lib); + return pic_obj_value(pic->lib->name); } else { - pic_assert_type(pic, lib, lib); - - pic->lib = pic_lib_ptr(lib); + pic_in_library(pic, lib); return pic_undef_value(); } @@ -147,27 +171,25 @@ pic_lib_current_library(pic_state *pic) static pic_value pic_lib_library_import(pic_state *pic) { - pic_value lib_opt; + const char *lib; pic_sym *name, *realname, *uid, *alias = NULL; - struct pic_lib *lib; + struct pic_lib *libp; - pic_get_args(pic, "om|m", &lib_opt, &name, &alias); - - pic_assert_type(pic, lib_opt, lib); + pic_get_args(pic, "zm|m", &lib, &name, &alias); if (alias == NULL) { alias = name; } - lib = pic_lib_ptr(lib_opt); + libp = get_library(pic, lib); - if (! pic_dict_has(pic, lib->exports, name)) { + if (! pic_dict_has(pic, libp->exports, name)) { pic_errorf(pic, "attempted to import undefined variable '~s'", pic_obj_value(name)); } else { - realname = pic_sym_ptr(pic_dict_ref(pic, lib->exports, name)); + realname = pic_sym_ptr(pic_dict_ref(pic, libp->exports, name)); } - if ((uid = pic_find_identifier(pic, (pic_id *)realname, lib->env)) == NULL) { + if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname)); } else { pic_put_identifier(pic, (pic_id *)alias, uid, pic->lib->env); @@ -195,15 +217,17 @@ pic_lib_library_export(pic_state *pic) static pic_value pic_lib_library_exports(pic_state *pic) { - pic_value lib, exports = pic_nil_value(); + const char *lib; + pic_value exports = pic_nil_value(); pic_sym *sym; khiter_t it; + struct pic_lib *libp; - pic_get_args(pic, "o", &lib); + pic_get_args(pic, "z", &lib); - pic_assert_type(pic, lib, lib); + libp = get_library(pic, lib); - pic_dict_for_each (sym, pic_lib_ptr(lib)->exports, it) { + pic_dict_for_each (sym, libp->exports, it) { pic_push(pic, pic_obj_value(sym), exports); } @@ -213,13 +237,11 @@ pic_lib_library_exports(pic_state *pic) static pic_value pic_lib_library_environment(pic_state *pic) { - pic_value lib; + const char *lib; - pic_get_args(pic, "o", &lib); + pic_get_args(pic, "z", &lib); - pic_assert_type(pic, lib, lib); - - return pic_obj_value(pic_lib_ptr(lib)->env); + return pic_obj_value(get_library(pic, lib)->env); } void diff --git a/extlib/benz/load.c b/extlib/benz/load.c index e07b70d3..f58ce1be 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -11,7 +11,7 @@ pic_load(pic_state *pic, struct pic_port *port) size_t ai = pic_gc_arena_preserve(pic); while (! pic_eof_p(form = pic_read(pic, port))) { - pic_eval(pic, form, pic->lib); + pic_eval(pic, form, pic_current_library(pic)); pic_gc_arena_restore(pic, ai); } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 17cad2c2..50a6b9ac 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -15,19 +15,19 @@ pic_make_env(pic_state *pic, struct pic_env *up) env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV); env->up = up; - env->prefix = NULL; + env->lib = NULL; kh_init(env, &env->map); return env; } struct pic_env * -pic_make_topenv(pic_state *pic, struct pic_string *prefix) +pic_make_topenv(pic_state *pic, struct pic_string *lib) { struct pic_env *env; env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV); env->up = NULL; - env->prefix = prefix; + env->lib = lib; kh_init(env, &env->map); return env; } @@ -42,7 +42,7 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) name = pic_identifier_name(pic, id); if (env->up == NULL && pic_sym_p(pic_obj_value(id))) { /* toplevel & public */ - str = pic_format(pic, "%s/%s", pic_str_cstr(pic, env->prefix), name); + str = pic_format(pic, "%s/%s", pic_str_cstr(pic, env->lib), name); } else { str = pic_format(pic, ".%s.%d", name, pic->ucnt++); } diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 1cad0ed8..ed92381b 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -172,7 +172,7 @@ pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) { \ pic_value obj; \ \ - obj = pic_funcall(pic, pic->PICRIN_BASE, var, 0); \ + obj = pic_funcall(pic, "picrin.base", var, 0); \ \ return pic_port_ptr(obj); \ } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 55a770eb..4fc209ca 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -856,14 +856,30 @@ pic_vlambda(pic_state *pic, pic_func_t f, int n, va_list ap) } void -pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) +pic_defun(pic_state *pic, const char *name, pic_func_t f) +{ + pic_define(pic, pic_current_library(pic), name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))); + pic_export(pic, pic_intern_cstr(pic, name)); +} + +void +pic_defvar(pic_state *pic, const char *name, pic_value init, struct pic_proc *conv) +{ + pic_define(pic, pic_current_library(pic), name, pic_obj_value(pic_make_var(pic, init, conv))); + pic_export(pic, pic_intern_cstr(pic, name)); +} + +void +pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) { pic_sym *sym, *uid; + struct pic_env *env; sym = pic_intern_cstr(pic, name); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) { - uid = pic_add_identifier(pic, (pic_id *)sym, lib->env); + env = pic_library_environment(pic, lib); + if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { + uid = pic_add_identifier(pic, (pic_id *)sym, env); } else { if (pic_weak_has(pic, pic->globals, uid)) { pic_warnf(pic, "redefining variable: ~s", pic_obj_value(uid)); @@ -873,43 +889,33 @@ pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) pic_set(pic, lib, name, val); } -void -pic_defun(pic_state *pic, const char *name, pic_func_t f) -{ - pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))); - pic_export(pic, pic_intern_cstr(pic, name)); -} - -void -pic_defvar(pic_state *pic, const char *name, pic_value init, struct pic_proc *conv) -{ - pic_define(pic, pic->lib, name, pic_obj_value(pic_make_var(pic, init, conv))); - pic_export(pic, pic_intern_cstr(pic, name)); -} - pic_value -pic_ref(pic_state *pic, struct pic_lib *lib, const char *name) +pic_ref(pic_state *pic, const char *lib, const char *name) { pic_sym *sym, *uid; + struct pic_env *env; sym = pic_intern_cstr(pic, name); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) { - pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name); + env = pic_library_environment(pic, lib); + if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { + pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); } return vm_gref(pic, uid); } void -pic_set(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) +pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) { pic_sym *sym, *uid; + struct pic_env *env; sym = pic_intern_cstr(pic, name); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) { - pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name); + env = pic_library_environment(pic, lib); + if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { + pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); } vm_gset(pic, uid, val); @@ -946,7 +952,7 @@ pic_closure_set(pic_state *pic, int n, pic_value v) } pic_value -pic_funcall(pic_state *pic, struct pic_lib *lib, const char *name, int n, ...) +pic_funcall(pic_state *pic, const char *lib, const char *name, int n, ...) { pic_value proc, r; va_list ap; diff --git a/extlib/benz/state.c b/extlib/benz/state.c index c3ce33b3..72c0604c 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -120,7 +120,7 @@ pic_init_core(pic_state *pic) pic_init_features(pic); - pic_deflibrary(pic, "(picrin base)"); + pic_deflibrary(pic, "picrin.base"); ai = pic_gc_arena_preserve(pic); @@ -268,7 +268,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->features = pic_nil_value(); /* libraries */ - pic->libs = pic_nil_value(); + kh_init(ltable, &pic->ltable); pic->lib = NULL; /* ireps */ @@ -346,9 +346,8 @@ pic_open(pic_allocf allocf, void *userdata) pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable); /* standard libraries */ - pic->PICRIN_BASE = pic_make_library(pic, pic_read_cstr(pic, "(picrin base)")); - pic->PICRIN_USER = pic_make_library(pic, pic_read_cstr(pic, "(picrin user)")); - pic->lib = pic->PICRIN_USER; + pic_make_library(pic, "picrin.user"); + pic_in_library(pic, "picrin.user"); pic_gc_arena_restore(pic, ai); @@ -376,7 +375,6 @@ pic_open(pic_allocf allocf, void *userdata) void pic_close(pic_state *pic) { - khash_t(oblist) *h = &pic->oblist; pic_allocf allocf = pic->allocf; /* clear out root objects */ @@ -388,7 +386,9 @@ pic_close(pic_state *pic) pic->globals = NULL; pic->macros = NULL; pic->features = pic_nil_value(); - pic->libs = pic_nil_value(); + + /* free all libraries */ + kh_clear(ltable, &pic->ltable); /* free all heap objects */ pic_gc(pic); @@ -420,7 +420,8 @@ pic_close(pic_state *pic) allocf(pic->userdata, pic->xpbase, 0); /* free global stacks */ - kh_destroy(oblist, h); + kh_destroy(oblist, &pic->oblist); + kh_destroy(ltable, &pic->ltable); /* free GC arena */ allocf(pic->userdata, pic->arena, 0); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index c838fa6a..0a836f24 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -578,7 +578,7 @@ pic_str_string_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } - val = pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); + val = pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); pic_assert_type(pic, val, char); buf[i] = pic_char(val); @@ -623,7 +623,7 @@ pic_str_string_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } - pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); + pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); } return pic_undef_value(); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 582c9d63..4e986ae3 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -240,7 +240,7 @@ pic_vec_vector_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } - vec->data[i] = pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); + vec->data[i] = pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); } return pic_obj_value(vec); @@ -269,7 +269,7 @@ pic_vec_vector_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } - pic_funcall(pic, pic->PICRIN_BASE, "apply", 2, pic_obj_value(proc), vals); + pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); } return pic_undef_value(); diff --git a/src/main.c b/src/main.c index 5168b502..35d627dd 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,6 @@ int main(int argc, char *argv[], char **envp) { pic_state *pic; - struct pic_lib *PICRIN_MAIN; int status; pic = pic_open(pic_default_allocf, NULL); @@ -34,9 +33,7 @@ main(int argc, char *argv[], char **envp) pic_try { pic_init_picrin(pic); - PICRIN_MAIN = pic_find_library(pic, pic_read_cstr(pic, "(picrin main)")); - - pic_funcall(pic, PICRIN_MAIN, "main", 0); + pic_funcall(pic, "picrin.main", "main", 0); status = 0; } From a3f9a3be68a4757a89f9678842f49a8e297f15f7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 18:58:32 +0900 Subject: [PATCH 021/119] remove WORD_BOXING mode --- extlib/benz/include/picrin/config.h | 100 ---------------------------- extlib/benz/include/picrin/setup.h | 16 +---- extlib/benz/include/picrin/type.h | 62 +---------------- 3 files changed, 4 insertions(+), 174 deletions(-) diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index e90684d0..2bbb920a 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -8,9 +8,6 @@ /** switch internal value representation */ /* #define PIC_NAN_BOXING 1 */ -/** enable word boxing */ -/* #define PIC_WORD_BOXING 0 */ - /** no dependency on libc */ /* #define PIC_ENABLE_LIBC 1 */ @@ -54,100 +51,3 @@ /* #define VM_DEBUG 1 */ /* #define GC_DEBUG 1 */ /* #define GC_DEBUG_DETAIL 1 */ - -#ifndef PIC_DIRECT_THREADED_VM -# if (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 -# define PIC_DIRECT_THREADED_VM 1 -# endif -#endif - -#if PIC_NAN_BOXING && PIC_WORD_BOXING -# error cannot enable both PIC_NAN_BOXING and PIC_WORD_BOXING simultaneously -#endif - -#ifndef PIC_WORD_BOXING -# define PIC_WORD_BOXING 0 -#endif - -#if ! PIC_WORD_BOXING -# ifndef PIC_NAN_BOXING -# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 -# define PIC_NAN_BOXING 1 -# endif -# endif -#endif - -#ifndef PIC_ENABLE_LIBC -# define PIC_ENABLE_LIBC 1 -#endif - -#ifndef PIC_ENABLE_STDIO -# define PIC_ENABLE_STDIO 1 -#endif - -#ifndef PIC_JMPBUF -# include -# define PIC_JMPBUF jmp_buf -#endif - -#ifndef PIC_SETJMP -# include -# define PIC_SETJMP(pic, buf) setjmp(buf) -#endif - -#ifndef PIC_LONGJMP -# include -# define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val)) -#endif - -#ifndef PIC_ABORT -# define PIC_ABORT(pic) abort() -#endif - -#ifndef PIC_ARENA_SIZE -# define PIC_ARENA_SIZE (8 * 1024) -#endif - -#ifndef PIC_HEAP_PAGE_SIZE -# define PIC_HEAP_PAGE_SIZE (4 * 1024 * 1024) -#endif - -#ifndef PIC_PAGE_REQUEST_THRESHOLD -# define PIC_PAGE_REQUEST_THRESHOLD(total) ((total) * 77 / 100) -#endif - -#ifndef PIC_STACK_SIZE -# define PIC_STACK_SIZE 2048 -#endif - -#ifndef PIC_RESCUE_SIZE -# define PIC_RESCUE_SIZE 30 -#endif - -#ifndef PIC_SYM_POOL_SIZE -# define PIC_SYM_POOL_SIZE (2 * 1024) -#endif - -#ifndef PIC_IREP_SIZE -# define PIC_IREP_SIZE 8 -#endif - -#ifndef PIC_POOL_SIZE -# define PIC_POOL_SIZE 8 -#endif - -#ifndef PIC_SYMS_SIZE -# define PIC_SYMS_SIZE 32 -#endif - -#ifndef PIC_ISEQ_SIZE -# define PIC_ISEQ_SIZE 1024 -#endif - -#if DEBUG -# include -# define GC_STRESS 0 -# define VM_DEBUG 1 -# define GC_DEBUG 0 -# define GC_DEBUG_DETAIL 0 -#endif diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index ef10793f..47b97638 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -10,19 +10,9 @@ # endif #endif -#if PIC_NAN_BOXING && PIC_WORD_BOXING -# error cannot enable both PIC_NAN_BOXING and PIC_WORD_BOXING simultaneously -#endif - -#ifndef PIC_WORD_BOXING -# define PIC_WORD_BOXING 0 -#endif - -#if ! PIC_WORD_BOXING -# ifndef PIC_NAN_BOXING -# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 -# define PIC_NAN_BOXING 1 -# endif +#ifndef PIC_NAN_BOXING +# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 +# define PIC_NAN_BOXING 1 # endif #endif diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index de8e174f..1e93c372 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -72,46 +72,6 @@ pic_char(pic_value v) return v & 0xfffffffful; } -#elif PIC_WORD_BOXING - -typedef unsigned long pic_value; - -#define pic_ptr(v) ((void *)(v)) -#define pic_init_value(v,vtype) do { \ - v = (vtype << 3) + 7; \ - } while (0) - -PIC_INLINE enum pic_vtype -pic_vtype(pic_value v) -{ - if ((v & 1) == 0) { - return PIC_VTYPE_HEAP; - } - if ((v & 2) == 0) { - return PIC_VTYPE_INT; - } - if ((v & 4) == 0) { - return PIC_VTYPE_CHAR; - } - return v >> 3; -} - -PIC_INLINE int -pic_int(pic_value v) -{ - v >>= 2; - if ((v & ((ULONG_MAX >> 3) + 1)) != 0) { - v |= ULONG_MAX - (ULONG_MAX >> 2); - } - return v; -} - -PIC_INLINE char -pic_char(pic_value v) -{ - return v >> 3; -} - #else typedef struct { @@ -394,26 +354,6 @@ pic_char_value(char c) return v; } -#elif PIC_WORD_BOXING - -PIC_INLINE pic_value -pic_obj_value(void *ptr) -{ - return (pic_value)ptr; -} - -PIC_INLINE pic_value -pic_int_value(int i) -{ - return (i << 2) + 1; -} - -PIC_INLINE pic_value -pic_char_value(char c) -{ - return (c << 3) + 3; -} - #else PIC_INLINE pic_value @@ -476,7 +416,7 @@ pic_invalid_value() return v; } -#if PIC_NAN_BOXING || PIC_WORD_BOXING +#if PIC_NAN_BOXING PIC_INLINE bool pic_eq_p(pic_value x, pic_value y) From 615bdff61a8aaf85e22e43c985cb2c21ad0c598d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 20:15:42 +0900 Subject: [PATCH 022/119] add pic_state parameter (to be used by copy gc) --- contrib/10.callcc/callcc.c | 2 +- contrib/10.math/math.c | 76 ++++++++-------- contrib/20.r7rs/src/file.c | 8 +- contrib/20.r7rs/src/load.c | 2 +- contrib/20.r7rs/src/mutable-string.c | 14 +-- contrib/20.r7rs/src/system.c | 20 ++--- contrib/20.r7rs/src/time.c | 6 +- contrib/30.random/src/random.c | 2 +- contrib/30.readline/src/readline.c | 36 ++++---- contrib/30.regexp/src/regexp.c | 20 ++--- contrib/40.srfi/src/106.c | 96 ++++++++++----------- contrib/60.repl/repl.c | 2 +- docs/capi.rst | 2 +- extlib/benz/README.md | 2 +- extlib/benz/blob.c | 24 +++--- extlib/benz/bool.c | 40 ++++----- extlib/benz/char.c | 16 ++-- extlib/benz/cont.c | 10 +-- extlib/benz/debug.c | 4 +- extlib/benz/dict.c | 20 ++--- extlib/benz/error.c | 6 +- extlib/benz/eval.c | 52 +++++------ extlib/benz/gc.c | 10 +-- extlib/benz/include/picrin.h | 124 ++++++++++++++------------- extlib/benz/include/picrin/data.h | 4 +- extlib/benz/include/picrin/error.h | 2 +- extlib/benz/include/picrin/macro.h | 2 +- extlib/benz/include/picrin/pair.h | 8 +- extlib/benz/include/picrin/port.h | 1 - extlib/benz/include/picrin/proc.h | 2 +- extlib/benz/include/picrin/record.h | 2 +- extlib/benz/include/picrin/symbol.h | 2 +- extlib/benz/include/picrin/type.h | 81 ++++++++--------- extlib/benz/lib.c | 12 +-- extlib/benz/load.c | 2 +- extlib/benz/macro.c | 24 +++--- extlib/benz/number.c | 70 +++++++-------- extlib/benz/pair.c | 88 +++++++++---------- extlib/benz/port.c | 71 +++++++-------- extlib/benz/proc.c | 62 +++++++------- extlib/benz/read.c | 90 +++++++++---------- extlib/benz/record.c | 2 +- extlib/benz/state.c | 6 +- extlib/benz/string.c | 58 ++++++------- extlib/benz/symbol.c | 28 +++--- extlib/benz/var.c | 6 +- extlib/benz/vector.c | 28 +++--- extlib/benz/weak.c | 10 +-- extlib/benz/write.c | 38 ++++---- 49 files changed, 644 insertions(+), 649 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 51327757..0d48b6d3 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -158,7 +158,7 @@ save_cont(pic_state *pic, struct pic_fullcont **c) cont->arena = pic_malloc(pic, sizeof(struct pic_object *) * pic->arena_size); memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * pic->arena_size); - cont->results = pic_undef_value(); + cont->results = pic_undef_value(pic); } static void diff --git a/contrib/10.math/math.c b/contrib/10.math/math.c index 81c04927..0fc2ff92 100644 --- a/contrib/10.math/math.c +++ b/contrib/10.math/math.c @@ -17,13 +17,13 @@ pic_number_floor2(pic_state *pic) ? i / j : (i / j) - 1; - return pic_values2(pic, pic_int_value(k), pic_int_value(i - k * j)); + return pic_values2(pic, pic_int_value(pic, k), pic_int_value(pic, i - k * j)); } else { double q, r; q = floor((double)i/j); r = i - j * q; - return pic_values2(pic, pic_float_value(q), pic_float_value(r)); + return pic_values2(pic, pic_float_value(pic, q), pic_float_value(pic, r)); } } @@ -36,14 +36,14 @@ pic_number_trunc2(pic_state *pic) pic_get_args(pic, "II", &i, &e1, &j, &e2); if (e1 && e2) { - return pic_values2(pic, pic_int_value(i/j), pic_int_value(i - (i/j) * j)); + return pic_values2(pic, pic_int_value(pic, i/j), pic_int_value(pic, i - (i/j) * j)); } else { double q, r; q = trunc((double)i/j); r = i - j * q; - return pic_values2(pic, pic_float_value(q), pic_float_value(r)); + return pic_values2(pic, pic_float_value(pic, q), pic_float_value(pic, r)); } } @@ -56,9 +56,9 @@ pic_number_floor(pic_state *pic) pic_get_args(pic, "F", &f, &e); if (e) { - return pic_int_value((int)f); + return pic_int_value(pic, (int)f); } else { - return pic_float_value(floor(f)); + return pic_float_value(pic, floor(f)); } } @@ -71,9 +71,9 @@ pic_number_ceil(pic_state *pic) pic_get_args(pic, "F", &f, &e); if (e) { - return pic_int_value((int)f); + return pic_int_value(pic, (int)f); } else { - return pic_float_value(ceil(f)); + return pic_float_value(pic, ceil(f)); } } @@ -86,9 +86,9 @@ pic_number_trunc(pic_state *pic) pic_get_args(pic, "F", &f, &e); if (e) { - return pic_int_value((int)f); + return pic_int_value(pic, (int)f); } else { - return pic_float_value(trunc(f)); + return pic_float_value(pic, trunc(f)); } } @@ -101,9 +101,9 @@ pic_number_round(pic_state *pic) pic_get_args(pic, "F", &f, &e); if (e) { - return pic_int_value((int)f); + return pic_int_value(pic, (int)f); } else { - return pic_float_value(round(f)); + return pic_float_value(pic, round(f)); } } @@ -114,12 +114,12 @@ pic_number_finite_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_int_p(v)) - return pic_true_value(); - if (pic_float_p(v) && ! (isinf(pic_float(v)) || isnan(pic_float(v)))) - return pic_true_value(); + if (pic_int_p(pic, v)) + return pic_true_value(pic); + if (pic_float_p(pic, v) && ! (isinf(pic_float(pic, v)) || isnan(pic_float(pic, v)))) + return pic_true_value(pic); else - return pic_false_value(); + return pic_false_value(pic); } static pic_value @@ -129,10 +129,10 @@ pic_number_infinite_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_float_p(v) && isinf(pic_float(v))) - return pic_true_value(); + if (pic_float_p(pic, v) && isinf(pic_float(pic, v))) + return pic_true_value(pic); else - return pic_false_value(); + return pic_false_value(pic); } static pic_value @@ -142,10 +142,10 @@ pic_number_nan_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_float_p(v) && isnan(pic_float(v))) - return pic_true_value(); + if (pic_float_p(pic, v) && isnan(pic_float(pic, v))) + return pic_true_value(pic); else - return pic_false_value(); + return pic_false_value(pic); } static pic_value @@ -154,7 +154,7 @@ pic_number_exp(pic_state *pic) double f; pic_get_args(pic, "f", &f); - return pic_float_value(exp(f)); + return pic_float_value(pic, exp(f)); } static pic_value @@ -165,10 +165,10 @@ pic_number_log(pic_state *pic) argc = pic_get_args(pic, "f|f", &f, &g); if (argc == 1) { - return pic_float_value(log(f)); + return pic_float_value(pic, log(f)); } else { - return pic_float_value(log(f) / log(g)); + return pic_float_value(pic, log(f) / log(g)); } } @@ -179,7 +179,7 @@ pic_number_sin(pic_state *pic) pic_get_args(pic, "f", &f); f = sin(f); - return pic_float_value(f); + return pic_float_value(pic, f); } static pic_value @@ -189,7 +189,7 @@ pic_number_cos(pic_state *pic) pic_get_args(pic, "f", &f); f = cos(f); - return pic_float_value(f); + return pic_float_value(pic, f); } static pic_value @@ -199,7 +199,7 @@ pic_number_tan(pic_state *pic) pic_get_args(pic, "f", &f); f = tan(f); - return pic_float_value(f); + return pic_float_value(pic, f); } static pic_value @@ -209,7 +209,7 @@ pic_number_acos(pic_state *pic) pic_get_args(pic, "f", &f); f = acos(f); - return pic_float_value(f); + return pic_float_value(pic, f); } static pic_value @@ -219,7 +219,7 @@ pic_number_asin(pic_state *pic) pic_get_args(pic, "f", &f); f = asin(f); - return pic_float_value(f); + return pic_float_value(pic, f); } static pic_value @@ -231,10 +231,10 @@ pic_number_atan(pic_state *pic) argc = pic_get_args(pic, "f|f", &f, &g); if (argc == 1) { f = atan(f); - return pic_float_value(f); + return pic_float_value(pic, f); } else { - return pic_float_value(atan2(f,g)); + return pic_float_value(pic, atan2(f,g)); } } @@ -245,7 +245,7 @@ pic_number_sqrt(pic_state *pic) pic_get_args(pic, "f", &f); - return pic_float_value(sqrt(f)); + return pic_float_value(pic, sqrt(f)); } static pic_value @@ -257,10 +257,10 @@ pic_number_abs(pic_state *pic) pic_get_args(pic, "F", &f, &e); if (e) { - return pic_int_value(f < 0 ? -f : f); + return pic_int_value(pic, f < 0 ? -f : f); } else { - return pic_float_value(fabs(f)); + return pic_float_value(pic, fabs(f)); } } @@ -275,10 +275,10 @@ pic_number_expt(pic_state *pic) h = pow(f, g); if (e1 && e2) { if (h <= INT_MAX) { - return pic_int_value((int)h); + return pic_int_value(pic, (int)h); } } - return pic_float_value(h); + return pic_float_value(pic, h); } void diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index d6a1135b..270260db 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -11,7 +11,7 @@ file_error(pic_state *pic, const char *msg) { struct pic_error *e; - e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value()); + e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value(pic)); pic_raise(pic, pic_obj_value(e)); } @@ -71,9 +71,9 @@ pic_file_exists_p(pic_state *pic) fp = fopen(fname, "r"); if (fp) { fclose(fp); - return pic_true_value(); + return pic_true_value(pic); } else { - return pic_false_value(); + return pic_false_value(pic); } } @@ -87,7 +87,7 @@ pic_file_delete(pic_state *pic) if (remove(fname) != 0) { file_error(pic, "file cannot be deleted"); } - return pic_undef_value(); + return pic_undef_value(pic); } void diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index aed45506..7b4c9e8a 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -19,7 +19,7 @@ pic_load_load(pic_state *pic) pic_close_port(pic, port); - return pic_undef_value(); + return pic_undef_value(pic); } void diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index 2d360c6c..db58687e 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -6,7 +6,7 @@ pic_str_set(pic_state *pic, struct pic_string *str, int i, char c) struct pic_string *x, *y, *z, *tmp; char buf[1]; - if (pic_str_len(str) <= i) { + if (pic_str_len(pic, str) <= i) { pic_errorf(pic, "index out of range %d", i); } @@ -14,7 +14,7 @@ pic_str_set(pic_state *pic, struct pic_string *str, int i, char c) x = pic_str_sub(pic, str, 0, i); y = pic_make_str(pic, buf, 1); - z = pic_str_sub(pic, str, i + 1, pic_str_len(str)); + z = pic_str_sub(pic, str, i + 1, pic_str_len(pic, str)); tmp = pic_str_cat(pic, x, pic_str_cat(pic, y, z)); @@ -33,7 +33,7 @@ pic_str_string_set(pic_state *pic) pic_get_args(pic, "sic", &str, &k, &c); pic_str_set(pic, str, k, c); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -48,7 +48,7 @@ pic_str_string_copy_ip(pic_state *pic) case 3: start = 0; case 4: - end = pic_str_len(from); + end = pic_str_len(pic, from); } if (to == from) { from = pic_str_sub(pic, from, 0, end); @@ -57,7 +57,7 @@ pic_str_string_copy_ip(pic_state *pic) while (start < end) { pic_str_set(pic, to, at++, pic_str_ref(pic, from, start++)); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -73,13 +73,13 @@ pic_str_string_fill_ip(pic_state *pic) case 2: start = 0; case 3: - end = pic_str_len(str); + end = pic_str_len(pic, str); } while (start < end) { pic_str_set(pic, str, start++, c); } - return pic_undef_value(); + return pic_undef_value(pic); } void diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 53acc81f..26ffbfe1 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -13,7 +13,7 @@ extern char **picrin_envp; static pic_value pic_system_cmdline(pic_state *pic) { - pic_value v = pic_nil_value(); + pic_value v = pic_nil_value(pic); int i; pic_get_args(pic, ""); @@ -36,12 +36,12 @@ pic_system_exit(pic_state *pic) argc = pic_get_args(pic, "|o", &v); if (argc == 1) { - switch (pic_type(v)) { + switch (pic_type(pic, v)) { case PIC_TT_FLOAT: - status = (int)pic_float(v); + status = (int)pic_float(pic, v); break; case PIC_TT_INT: - status = pic_int(v); + status = pic_int(pic, v); break; default: break; @@ -61,12 +61,12 @@ pic_system_emergency_exit(pic_state *pic) argc = pic_get_args(pic, "|o", &v); if (argc == 1) { - switch (pic_type(v)) { + switch (pic_type(pic, v)) { case PIC_TT_FLOAT: - status = (int)pic_float(v); + status = (int)pic_float(pic, v); break; case PIC_TT_INT: - status = pic_int(v); + status = pic_int(pic, v); break; default: break; @@ -86,7 +86,7 @@ pic_system_getenv(pic_state *pic) val = getenv(str); if (val == NULL) - return pic_nil_value(); + return pic_nil_value(pic); else return pic_obj_value(pic_make_cstr(pic, val)); } @@ -95,13 +95,13 @@ static pic_value pic_system_getenvs(pic_state *pic) { char **envp; - pic_value data = pic_nil_value(); + pic_value data = pic_nil_value(pic); size_t ai = pic_gc_arena_preserve(pic); pic_get_args(pic, ""); if (! picrin_envp) { - return pic_nil_value(); + return pic_nil_value(pic); } for (envp = picrin_envp; *envp; ++envp) { diff --git a/contrib/20.r7rs/src/time.c b/contrib/20.r7rs/src/time.c index ba34d4eb..ac8585d3 100644 --- a/contrib/20.r7rs/src/time.c +++ b/contrib/20.r7rs/src/time.c @@ -16,7 +16,7 @@ pic_current_second(pic_state *pic) pic_get_args(pic, ""); time(&t); - return pic_float_value((double)t + UTC_TAI_DIFF); + return pic_float_value(pic, (double)t + UTC_TAI_DIFF); } static pic_value @@ -27,7 +27,7 @@ pic_current_jiffy(pic_state *pic) pic_get_args(pic, ""); c = clock(); - return pic_int_value((int)c); /* The year 2038 problem :-| */ + return pic_int_value(pic, (int)c); /* The year 2038 problem :-| */ } static pic_value @@ -35,7 +35,7 @@ pic_jiffies_per_second(pic_state *pic) { pic_get_args(pic, ""); - return pic_int_value(CLOCKS_PER_SEC); + return pic_int_value(pic, CLOCKS_PER_SEC); } void diff --git a/contrib/30.random/src/random.c b/contrib/30.random/src/random.c index 6eb2ee11..95fb7a03 100644 --- a/contrib/30.random/src/random.c +++ b/contrib/30.random/src/random.c @@ -7,7 +7,7 @@ pic_random_real(pic_state *pic) { pic_get_args(pic, ""); - return pic_float_value(genrand_real3()); + return pic_float_value(pic, genrand_real3()); } void diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index 9b95e2ad..a7542af5 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -29,7 +29,7 @@ pic_rl_history_length(pic_state *pic) { pic_get_args(pic, ""); - return pic_int_value(history_get_history_state()->length); + return pic_int_value(pic, history_get_history_state()->length); } static pic_value @@ -41,7 +41,7 @@ pic_rl_add_history(pic_state *pic) add_history(line); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -53,7 +53,7 @@ pic_rl_stifle_history(pic_state *pic) stifle_history(i); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -63,7 +63,7 @@ pic_rl_unstifle_history(pic_state *pic) unstifle_history(); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -71,7 +71,7 @@ pic_rl_history_is_stifled(pic_state *pic) { pic_get_args(pic, ""); - return pic_bool_value(history_is_stifled()); + return pic_bool_value(pic, history_is_stifled()); } static pic_value @@ -79,7 +79,7 @@ pic_rl_where_history(pic_state *pic) { pic_get_args(pic, ""); - return pic_int_value(where_history()); + return pic_int_value(pic, where_history()); } static pic_value @@ -101,7 +101,7 @@ pic_rl_history_get(pic_state *pic) e = history_get(i); return e ? pic_obj_value(pic_make_cstr(pic, e->line)) - : pic_false_value(); + : pic_false_value(pic); } static pic_value @@ -115,7 +115,7 @@ pic_rl_remove_history(pic_state *pic) e = remove_history(i); return e ? pic_obj_value(pic_make_cstr(pic, e->line)) - : pic_false_value(); + : pic_false_value(pic); } static pic_value @@ -125,7 +125,7 @@ pic_rl_clear_history(pic_state *pic) clear_history(); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -136,7 +136,7 @@ pic_rl_history_set_pos(pic_state *pic) pic_get_args(pic, "i", &i); - return pic_int_value(history_set_pos(i)); + return pic_int_value(pic, history_set_pos(i)); } static pic_value @@ -149,7 +149,7 @@ pic_rl_previous_history(pic_state *pic) e = previous_history(); return e ? pic_obj_value(pic_make_cstr(pic, e->line)) - : pic_false_value(); + : pic_false_value(pic); } static pic_value @@ -162,7 +162,7 @@ pic_rl_next_history(pic_state *pic) e = next_history(); return e ? pic_obj_value(pic_make_cstr(pic, e->line)) - : pic_false_value(); + : pic_false_value(pic); } static pic_value @@ -173,9 +173,9 @@ pic_rl_history_search(pic_state *pic) argc = pic_get_args(pic, "zi|i", &key, &direction, &pos); if(argc == 2) - return pic_int_value(history_search(key, direction)); + return pic_int_value(pic, history_search(key, direction)); else - return pic_int_value(history_search_pos(key, direction, pos)); + return pic_int_value(pic, history_search_pos(key, direction, pos)); } static pic_value @@ -186,7 +186,7 @@ pic_rl_history_search_prefix(pic_state *pic) pic_get_args(pic, "zi", &key, &direction); - return pic_int_value(history_search_prefix(key, direction)); + return pic_int_value(pic, history_search_prefix(key, direction)); } static pic_value @@ -199,7 +199,7 @@ pic_rl_read_history(pic_state *pic) if(read_history(filename)) pic_errorf(pic, "cannot read history file : %s", filename); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -212,7 +212,7 @@ pic_rl_write_history(pic_state *pic) if(write_history(filename)) pic_errorf(pic, "cannot write history file: %s", filename); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -225,7 +225,7 @@ pic_rl_truncate_file(pic_state *pic) history_truncate_file(filename, i); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 5cfc1ccb..64740f8a 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -19,7 +19,7 @@ regexp_dtor(pic_state *pic, void *data) static const pic_data_type regexp_type = { "regexp", regexp_dtor, NULL }; -#define pic_regexp_p(o) (pic_data_type_p((o), ®exp_type)) +#define pic_regexp_p(pic, o) (pic_data_type_p(pic, (o), ®exp_type)) #define pic_regexp_data_ptr(o) ((struct pic_regexp_t *)pic_data_ptr(o)->data) static pic_value @@ -72,7 +72,7 @@ pic_regexp_regexp_p(pic_state *pic) pic_get_args(pic, "o", &obj); - return pic_bool_value(pic_regexp_p(obj)); + return pic_bool_value(pic, pic_regexp_p(pic, obj)); } static pic_value @@ -89,8 +89,8 @@ pic_regexp_regexp_match(pic_state *pic) pic_assert_type(pic, reg, regexp); - matches = pic_nil_value(); - positions = pic_nil_value(); + matches = pic_nil_value(pic); + positions = pic_nil_value(pic); if (strchr(pic_regexp_data_ptr(reg)->flags, 'g') != NULL) { /* global search */ @@ -98,7 +98,7 @@ pic_regexp_regexp_match(pic_state *pic) offset = 0; while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, match, 0) != REG_NOMATCH) { pic_push(pic, pic_obj_value(pic_make_str(pic, input, match[0].rm_eo - match[0].rm_so)), matches); - pic_push(pic, pic_int_value(offset), positions); + pic_push(pic, pic_int_value(pic, offset), positions); offset += match[0].rm_eo; input += match[0].rm_eo; @@ -113,14 +113,14 @@ pic_regexp_regexp_match(pic_state *pic) } str = pic_make_str(pic, input + match[i].rm_so, match[i].rm_eo - match[i].rm_so); pic_push(pic, pic_obj_value(str), matches); - pic_push(pic, pic_int_value(match[i].rm_so), positions); + pic_push(pic, pic_int_value(pic, match[i].rm_so), positions); } } } - if (pic_nil_p(matches)) { - matches = pic_false_value(); - positions = pic_false_value(); + if (pic_nil_p(pic, matches)) { + matches = pic_false_value(pic); + positions = pic_false_value(pic); } else { matches = pic_reverse(pic, matches); positions = pic_reverse(pic, positions); @@ -134,7 +134,7 @@ pic_regexp_regexp_split(pic_state *pic) pic_value reg; const char *input; regmatch_t match; - pic_value output = pic_nil_value(); + pic_value output = pic_nil_value(pic); pic_get_args(pic, "oz", ®, &input); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index d6598d9a..b1aac0ce 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -46,13 +46,13 @@ socket_dtor(pic_state *pic, void *data) static const pic_data_type socket_type = { "socket", socket_dtor, NULL }; -#define pic_socket_p(o) (pic_data_type_p((o), &socket_type)) +#define pic_socket_p(pic, o) (pic_data_type_p(pic, (o), &socket_type)) #define pic_socket_data_ptr(o) ((struct pic_socket_t *)pic_data_ptr(o)->data) PIC_INLINE void validate_socket_object(pic_state *pic, pic_value v) { - if (! pic_socket_p(v)) { + if (! pic_socket_p(pic, v)) { pic_errorf(pic, "~s is not a socket object", v); } } @@ -63,7 +63,7 @@ pic_socket_socket_p(pic_state *pic) pic_value obj; pic_get_args(pic, "o", &obj); - return pic_bool_value(pic_socket_p(obj)); + return pic_bool_value(pic, pic_socket_p(pic, obj)); } static pic_value @@ -79,10 +79,10 @@ pic_socket_make_socket(pic_state *pic) pic_get_args(pic, "ooiiii", &n, &s, &family, &socktype, &flags, &protocol); node = service = NULL; - if (pic_str_p(n)) { + if (pic_str_p(pic, n)) { node = pic_str_cstr(pic, pic_str_ptr(n)); } - if (pic_str_p(s)) { + if (pic_str_p(pic, s)) { service = pic_str_cstr(pic, pic_str_ptr(s)); } @@ -224,7 +224,7 @@ pic_socket_socket_send(pic_state *pic) written += len; } - return pic_int_value(written); + return pic_int_value(pic, written); } static pic_value @@ -286,7 +286,7 @@ pic_socket_socket_shutdown(pic_state *pic) sock->fd = -1; } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -299,7 +299,7 @@ pic_socket_socket_close(pic_state *pic) socket_close(pic_socket_data_ptr(obj)); - return pic_undef_value(); + return pic_undef_value(pic); } static int @@ -416,109 +416,109 @@ pic_init_srfi_106(pic_state *pic) pic_defun_(pic, "call-with-socket", pic_socket_call_with_socket); #ifdef AF_INET - pic_define_(pic, "*af-inet*", pic_int_value(AF_INET)); + pic_define_(pic, "*af-inet*", pic_int_value(pic, AF_INET)); #else - pic_define_(pic, "*af-inet*", pic_false_value()); + pic_define_(pic, "*af-inet*", pic_false_value(pic)); #endif #ifdef AF_INET6 - pic_define_(pic, "*af-inet6*", pic_int_value(AF_INET6)); + pic_define_(pic, "*af-inet6*", pic_int_value(pic, AF_INET6)); #else - pic_define_(pic, "*af-inet6*", pic_false_value()); + pic_define_(pic, "*af-inet6*", pic_false_value(pic)); #endif #ifdef AF_UNSPEC - pic_define_(pic, "*af-unspec*", pic_int_value(AF_UNSPEC)); + pic_define_(pic, "*af-unspec*", pic_int_value(pic, AF_UNSPEC)); #else - pic_define_(pic, "*af-unspec*", pic_false_value()); + pic_define_(pic, "*af-unspec*", pic_false_value(pic)); #endif #ifdef SOCK_STREAM - pic_define_(pic, "*sock-stream*", pic_int_value(SOCK_STREAM)); + pic_define_(pic, "*sock-stream*", pic_int_value(pic, SOCK_STREAM)); #else - pic_define_(pic, "*sock-stream*", pic_false_value()); + pic_define_(pic, "*sock-stream*", pic_false_value(pic)); #endif #ifdef SOCK_DGRAM - pic_define_(pic, "*sock-dgram*", pic_int_value(SOCK_DGRAM)); + pic_define_(pic, "*sock-dgram*", pic_int_value(pic, SOCK_DGRAM)); #else - pic_define_(pic, "*sock-dgram*", pic_false_value()); + pic_define_(pic, "*sock-dgram*", pic_false_value(pic)); #endif #ifdef AI_CANONNAME - pic_define_(pic, "*ai-canonname*", pic_int_value(AI_CANONNAME)); + pic_define_(pic, "*ai-canonname*", pic_int_value(pic, AI_CANONNAME)); #else - pic_define_(pic, "*ai-canonname*", pic_false_value()); + pic_define_(pic, "*ai-canonname*", pic_false_value(pic)); #endif #ifdef AI_NUMERICHOST - pic_define_(pic, "*ai-numerichost*", pic_int_value(AI_NUMERICHOST)); + pic_define_(pic, "*ai-numerichost*", pic_int_value(pic, AI_NUMERICHOST)); #else - pic_define_(pic, "*ai-numerichost*", pic_false_value()); + pic_define_(pic, "*ai-numerichost*", pic_false_value(pic)); #endif /* AI_V4MAPPED and AI_ALL are not supported by *BSDs, even though they are defined in netdb.h. */ #if defined(AI_V4MAPPED) && !defined(BSD) - pic_define_(pic, "*ai-v4mapped*", pic_int_value(AI_V4MAPPED)); + pic_define_(pic, "*ai-v4mapped*", pic_int_value(pic, AI_V4MAPPED)); #else - pic_define_(pic, "*ai-v4mapped*", pic_false_value()); + pic_define_(pic, "*ai-v4mapped*", pic_false_value(pic)); #endif #if defined(AI_ALL) && !defined(BSD) - pic_define_(pic, "*ai-all*", pic_int_value(AI_ALL)); + pic_define_(pic, "*ai-all*", pic_int_value(pic, AI_ALL)); #else - pic_define_(pic, "*ai-all*", pic_false_value()); + pic_define_(pic, "*ai-all*", pic_false_value(pic)); #endif #ifdef AI_ADDRCONFIG - pic_define_(pic, "*ai-addrconfig*", pic_int_value(AI_ADDRCONFIG)); + pic_define_(pic, "*ai-addrconfig*", pic_int_value(pic, AI_ADDRCONFIG)); #else - pic_define_(pic, "*ai-addrconfig*", pic_false_value()); + pic_define_(pic, "*ai-addrconfig*", pic_false_value(pic)); #endif #ifdef AI_PASSIVE - pic_define_(pic, "*ai-passive*", pic_int_value(AI_PASSIVE)); + pic_define_(pic, "*ai-passive*", pic_int_value(pic, AI_PASSIVE)); #else - pic_define_(pic, "*ai-passive*", pic_false_value()); + pic_define_(pic, "*ai-passive*", pic_false_value(pic)); #endif #ifdef IPPROTO_IP - pic_define_(pic, "*ipproto-ip*", pic_int_value(IPPROTO_IP)); + pic_define_(pic, "*ipproto-ip*", pic_int_value(pic, IPPROTO_IP)); #else - pic_define_(pic, "*ipproto-ip*", pic_false_value()); + pic_define_(pic, "*ipproto-ip*", pic_false_value(pic)); #endif #ifdef IPPROTO_TCP - pic_define_(pic, "*ipproto-tcp*", pic_int_value(IPPROTO_TCP)); + pic_define_(pic, "*ipproto-tcp*", pic_int_value(pic, IPPROTO_TCP)); #else - pic_define_(pic, "*ipproto-tcp*", pic_false_value()); + pic_define_(pic, "*ipproto-tcp*", pic_false_value(pic)); #endif #ifdef IPPROTO_UDP - pic_define_(pic, "*ipproto-udp*", pic_int_value(IPPROTO_UDP)); + pic_define_(pic, "*ipproto-udp*", pic_int_value(pic, IPPROTO_UDP)); #else - pic_define_(pic, "*ipproto-udp*", pic_false_value()); + pic_define_(pic, "*ipproto-udp*", pic_false_value(pic)); #endif #ifdef MSG_PEEK - pic_define_(pic, "*msg-peek*", pic_int_value(MSG_PEEK)); + pic_define_(pic, "*msg-peek*", pic_int_value(pic, MSG_PEEK)); #else - pic_define_(pic, "*msg-peek*", pic_false_value()); + pic_define_(pic, "*msg-peek*", pic_false_value(pic)); #endif #ifdef MSG_OOB - pic_define_(pic, "*msg-oob*", pic_int_value(MSG_OOB)); + pic_define_(pic, "*msg-oob*", pic_int_value(pic, MSG_OOB)); #else - pic_define_(pic, "*msg-oob*", pic_false_value()); + pic_define_(pic, "*msg-oob*", pic_false_value(pic)); #endif #ifdef MSG_WAITALL - pic_define_(pic, "*msg-waitall*", pic_int_value(MSG_WAITALL)); + pic_define_(pic, "*msg-waitall*", pic_int_value(pic, MSG_WAITALL)); #else - pic_define_(pic, "*msg-waitall*", pic_false_value()); + pic_define_(pic, "*msg-waitall*", pic_false_value(pic)); #endif #ifdef SHUT_RD - pic_define_(pic, "*shut-rd*", pic_int_value(SHUT_RD)); + pic_define_(pic, "*shut-rd*", pic_int_value(pic, SHUT_RD)); #else - pic_define_(pic, "*shut-rd*", pic_false_value()); + pic_define_(pic, "*shut-rd*", pic_false_value(pic)); #endif #ifdef SHUT_WR - pic_define_(pic, "*shut-wr*", pic_int_value(SHUT_WR)); + pic_define_(pic, "*shut-wr*", pic_int_value(pic, SHUT_WR)); #else - pic_define_(pic, "*shut-wr*", pic_false_value()); + pic_define_(pic, "*shut-wr*", pic_false_value(pic)); #endif #ifdef SHUT_RDWR - pic_define_(pic, "*shut-rdwr*", pic_int_value(SHUT_RDWR)); + pic_define_(pic, "*shut-rdwr*", pic_int_value(pic, SHUT_RDWR)); #else - pic_define_(pic, "*shut-rdwr*", pic_false_value()); + pic_define_(pic, "*shut-rdwr*", pic_false_value(pic)); #endif } diff --git a/contrib/60.repl/repl.c b/contrib/60.repl/repl.c index cea0ed22..e6e371de 100644 --- a/contrib/60.repl/repl.c +++ b/contrib/60.repl/repl.c @@ -9,7 +9,7 @@ pic_repl_tty_p(pic_state *pic) pic_get_args(pic, ""); - return pic_bool_value((isatty(STDIN_FILENO))); + return pic_bool_value(pic, (isatty(STDIN_FILENO))); } void diff --git a/docs/capi.rst b/docs/capi.rst index 9297989b..c427515e 100644 --- a/docs/capi.rst +++ b/docs/capi.rst @@ -28,7 +28,7 @@ If you want to create a contribution library with C, the only thing you need to pic_get_args(pic, "ff", &a, &b); - return pic_float_value(a + b); + return pic_float_value(pic, a + b); } void diff --git a/extlib/benz/README.md b/extlib/benz/README.md index 67c7a32d..71b1e62b 100644 --- a/extlib/benz/README.md +++ b/extlib/benz/README.md @@ -55,7 +55,7 @@ pic_value factorial(pic_state *pic) { pic_get_args(pic, "i", &i); - return pic_int_value(fact(i)); + return pic_int_value(pic, fact(i)); } int diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index c7cbff9f..fc464aa0 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -22,7 +22,7 @@ pic_blob_bytevector_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_blob_p(v)); + return pic_bool_value(pic, pic_blob_p(pic, v)); } static pic_value @@ -42,11 +42,11 @@ pic_blob_bytevector(pic_state *pic) for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], int); - if (pic_int(argv[i]) < 0 || pic_int(argv[i]) > 255) { + if (pic_int(pic, argv[i]) < 0 || pic_int(pic, argv[i]) > 255) { pic_errorf(pic, "byte out of range"); } - *data++ = (unsigned char)pic_int(argv[i]); + *data++ = (unsigned char)pic_int(pic, argv[i]); } return pic_obj_value(blob); @@ -78,7 +78,7 @@ pic_blob_bytevector_length(pic_state *pic) pic_get_args(pic, "b", &bv); - return pic_int_value(bv->len); + return pic_int_value(pic, bv->len); } static pic_value @@ -89,7 +89,7 @@ pic_blob_bytevector_u8_ref(pic_state *pic) pic_get_args(pic, "bi", &bv, &k); - return pic_int_value(bv->data[k]); + return pic_int_value(pic, bv->data[k]); } static pic_value @@ -104,7 +104,7 @@ pic_blob_bytevector_u8_set(pic_state *pic) pic_errorf(pic, "byte out of range"); bv->data[k] = (unsigned char)v; - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -128,14 +128,14 @@ pic_blob_bytevector_copy_i(pic_state *pic) while (start < end) { to->data[--at] = from->data[--end]; } - return pic_undef_value(); + return pic_undef_value(pic); } while (start < end) { to->data[at++] = from->data[start++]; } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -209,10 +209,10 @@ pic_blob_list_to_bytevector(pic_state *pic) pic_for_each (e, list, it) { pic_assert_type(pic, e, int); - if (pic_int(e) < 0 || pic_int(e) > 255) + if (pic_int(pic, e) < 0 || pic_int(pic, e) > 255) pic_errorf(pic, "byte out of range"); - *data++ = (unsigned char)pic_int(e); + *data++ = (unsigned char)pic_int(pic, e); } return pic_obj_value(blob); } @@ -233,10 +233,10 @@ pic_blob_bytevector_to_list(pic_state *pic) end = blob->len; } - list = pic_nil_value(); + list = pic_nil_value(pic); for (i = start; i < end; ++i) { - pic_push(pic, pic_int_value(blob->data[i]), list); + pic_push(pic, pic_int_value(pic, blob->data[i]), list); } return pic_reverse(pic, list); } diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index aa4a888f..cf72b27c 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -10,8 +10,8 @@ KHASH_DEFINE2(m, void *, int, 0, kh_ptr_hash_func, kh_ptr_hash_equal) static bool internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) *h) { - pic_value localx = pic_nil_value(); - pic_value localy = pic_nil_value(); + pic_value localx = pic_nil_value(pic); + pic_value localy = pic_nil_value(pic); int cx = 0; int cy = 0; @@ -19,7 +19,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) if (depth > 200) { pic_errorf(pic, "Stack overflow in equal\n"); } - if (pic_pair_p(x) || pic_vec_p(x)) { + if (pic_pair_p(pic, x) || pic_vec_p(pic, x)) { int ret; kh_put(m, h, pic_obj_ptr(x), &ret); if (ret != 0) { @@ -30,14 +30,14 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) LOOP: - if (pic_eqv_p(x, y)) { + if (pic_eqv_p(pic, x, y)) { return true; } - if (pic_type(x) != pic_type(y)) { + if (pic_type(pic, x) != pic_type(pic, y)) { return false; } - switch (pic_type(x)) { + switch (pic_type(pic, x)) { case PIC_TT_ID: { struct pic_id *id1, *id2; pic_sym *s1, *s2; @@ -74,12 +74,12 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) return false; /* Floyd's cycle-finding algorithm */ - if (pic_nil_p(localx)) { + if (pic_nil_p(pic, localx)) { localx = x; } x = pic_cdr(pic, x); cx++; - if (pic_nil_p(localy)) { + if (pic_nil_p(pic, localy)) { localy = y; } y = pic_cdr(pic, y); @@ -87,7 +87,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) if (cx == 2) { cx = 0; localx = pic_cdr(pic, localx); - if (pic_eq_p(localx, x)) { + if (pic_eq_p(pic, localx, x)) { if (cy < 0 ) return true; /* both lists circular */ cx = INT_MIN; /* found a cycle on x */ } @@ -95,7 +95,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) if (cy == 2) { cy = 0; localy = pic_cdr(pic, localy); - if (pic_eq_p(localy, y)) { + if (pic_eq_p(pic, localy, y)) { if (cx < 0 ) return true; /* both lists circular */ cy = INT_MIN; /* found a cycle on y */ } @@ -143,7 +143,7 @@ pic_bool_eq_p(pic_state *pic) pic_get_args(pic, "oo", &x, &y); - return pic_bool_value(pic_eq_p(x, y)); + return pic_bool_value(pic, pic_eq_p(pic, x, y)); } static pic_value @@ -153,7 +153,7 @@ pic_bool_eqv_p(pic_state *pic) pic_get_args(pic, "oo", &x, &y); - return pic_bool_value(pic_eqv_p(x, y)); + return pic_bool_value(pic, pic_eqv_p(pic, x, y)); } static pic_value @@ -163,7 +163,7 @@ pic_bool_equal_p(pic_state *pic) pic_get_args(pic, "oo", &x, &y); - return pic_bool_value(pic_equal_p(pic, x, y)); + return pic_bool_value(pic, pic_equal_p(pic, x, y)); } static pic_value @@ -173,7 +173,7 @@ pic_bool_not(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_false_p(v) ? pic_true_value() : pic_false_value(); + return pic_false_p(pic, v) ? pic_true_value(pic) : pic_false_value(pic); } static pic_value @@ -183,7 +183,7 @@ pic_bool_boolean_p(pic_state *pic) pic_get_args(pic, "o", &v); - return (pic_true_p(v) || pic_false_p(v)) ? pic_true_value() : pic_false_value(); + return (pic_true_p(pic, v) || pic_false_p(pic, v)) ? pic_true_value(pic) : pic_false_value(pic); } static pic_value @@ -195,14 +195,14 @@ pic_bool_boolean_eq_p(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); for (i = 0; i < argc; ++i) { - if (! (pic_true_p(argv[i]) || pic_false_p(argv[i]))) { - return pic_false_value(); + if (! (pic_true_p(pic, argv[i]) || pic_false_p(pic, argv[i]))) { + return pic_false_value(pic); } - if (! pic_eq_p(argv[i], argv[0])) { - return pic_false_value(); + if (! pic_eq_p(pic, argv[i], argv[0])) { + return pic_false_value(pic); } } - return pic_true_value(); + return pic_true_value(pic); } void diff --git a/extlib/benz/char.c b/extlib/benz/char.c index 8db6f41a..709787fb 100644 --- a/extlib/benz/char.c +++ b/extlib/benz/char.c @@ -11,7 +11,7 @@ pic_char_char_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_char_p(v) ? pic_true_value() : pic_false_value(); + return pic_char_p(pic, v) ? pic_true_value(pic) : pic_false_value(pic); } static pic_value @@ -21,7 +21,7 @@ pic_char_char_to_integer(pic_state *pic) pic_get_args(pic, "c", &c); - return pic_int_value(c); + return pic_int_value(pic, c); } static pic_value @@ -35,7 +35,7 @@ pic_char_integer_to_char(pic_state *pic) pic_errorf(pic, "integer->char: integer out of char range: %d", i); } - return pic_char_value((char)i); + return pic_char_value(pic, (char)i); } #define DEFINE_CHAR_CMP(op, name) \ @@ -49,20 +49,20 @@ pic_char_integer_to_char(pic_state *pic) pic_get_args(pic, "cc*", &c, &d, &argc, &argv); \ \ if (! (c op d)) \ - return pic_false_value(); \ + return pic_false_value(pic); \ \ for (i = 0; i < argc; ++i) { \ c = d; \ - if (pic_char_p(argv[i])) \ - d = pic_char(argv[i]); \ + if (pic_char_p(pic, argv[i])) \ + d = pic_char(pic, argv[i]); \ else \ pic_errorf(pic, #op ": char required"); \ \ if (! (c op d)) \ - return pic_false_value(); \ + return pic_false_value(pic); \ } \ \ - return pic_true_value(); \ + return pic_true_value(pic); \ } DEFINE_CHAR_CMP(==, eq) diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 6b54c11a..4ee65e09 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -60,7 +60,7 @@ pic_save_point(pic_state *pic, struct pic_cont *cont) cont->ip = pic->ip; cont->ptable = pic->ptable; cont->prev = pic->cc; - cont->results = pic_undef_value(); + cont->results = pic_undef_value(pic); cont->id = pic->ccnt++; pic->cc = cont; @@ -95,7 +95,7 @@ cont_call(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - id = pic_int(pic_closure_ref(pic, CV_ID)); + id = pic_int(pic, pic_closure_ref(pic, CV_ID)); /* check if continuation is alive */ for (cc = pic->cc; cc != NULL; cc = cc->prev) { @@ -124,7 +124,7 @@ pic_make_cont(pic_state *pic, struct pic_cont *cont) struct pic_proc *c; /* save the escape continuation in proc */ - c = pic_lambda(pic, cont_call, 2, pic_int_value(cont->id), pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); + c = pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); return c; } @@ -214,7 +214,7 @@ pic_values(pic_state *pic, int argc, pic_value *argv) } pic->ci->retc = (int)argc; - return argc == 0 ? pic_undef_value() : pic->sp[0]; + return argc == 0 ? pic_undef_value(pic) : pic->sp[0]; } pic_value @@ -229,7 +229,7 @@ pic_values_by_list(pic_state *pic, pic_value list) } pic->ci->retc = i; - return pic_nil_p(list) ? pic_undef_value() : pic->sp[0]; + return pic_nil_p(pic, list) ? pic_undef_value(pic) : pic->sp[0]; } int diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 106be269..6ef57678 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -35,9 +35,9 @@ pic_get_backtrace(pic_state *pic) void pic_print_backtrace(pic_state *pic, xFILE *file) { - assert(! pic_invalid_p(pic->err)); + assert(! pic_invalid_p(pic, pic->err)); - if (! pic_error_p(pic->err)) { + if (! pic_error_p(pic, pic->err)) { xfprintf(pic, file, "raise: "); pic_fwrite(pic, pic->err, file); } else { diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 12cae194..3583d5e3 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -104,7 +104,7 @@ pic_dict_dictionary_p(pic_state *pic) pic_get_args(pic, "o", &obj); - return pic_bool_value(pic_dict_p(obj)); + return pic_bool_value(pic, pic_dict_p(pic, obj)); } static pic_value @@ -116,7 +116,7 @@ pic_dict_dictionary_ref(pic_state *pic) pic_get_args(pic, "dm", &dict, &key); if (! pic_dict_has(pic, dict, key)) { - return pic_false_value(); + return pic_false_value(pic); } return pic_cons(pic, pic_obj_value(key), pic_dict_ref(pic, dict, key)); } @@ -130,7 +130,7 @@ pic_dict_dictionary_set(pic_state *pic) pic_get_args(pic, "dmo", &dict, &key, &val); - if (pic_undef_p(val)) { + if (pic_undef_p(pic, val)) { if (pic_dict_has(pic, dict, key)) { pic_dict_del(pic, dict, key); } @@ -138,7 +138,7 @@ pic_dict_dictionary_set(pic_state *pic) else { pic_dict_set(pic, dict, key, val); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -148,7 +148,7 @@ pic_dict_dictionary_size(pic_state *pic) pic_get_args(pic, "d", &dict); - return pic_int_value(pic_dict_size(pic, dict)); + return pic_int_value(pic, pic_dict_size(pic, dict)); } static pic_value @@ -158,7 +158,7 @@ pic_dict_dictionary_map(pic_state *pic) struct pic_dict *dict; khiter_t it; khash_t(dict) *kh; - pic_value ret = pic_nil_value(); + pic_value ret = pic_nil_value(pic); pic_get_args(pic, "ld", &proc, &dict); @@ -191,14 +191,14 @@ pic_dict_dictionary_for_each(pic_state *pic) } } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value pic_dict_dictionary_to_alist(pic_state *pic) { struct pic_dict *dict; - pic_value item, alist = pic_nil_value(); + pic_value item, alist = pic_nil_value(pic); pic_sym *sym; khiter_t it; @@ -234,7 +234,7 @@ static pic_value pic_dict_dictionary_to_plist(pic_state *pic) { struct pic_dict *dict; - pic_value plist = pic_nil_value(); + pic_value plist = pic_nil_value(pic); pic_sym *sym; khiter_t it; @@ -258,7 +258,7 @@ pic_dict_plist_to_dictionary(pic_state *pic) dict = pic_make_dict(pic); - for (e = pic_reverse(pic, plist); ! pic_nil_p(e); e = pic_cddr(pic, e)) { + for (e = pic_reverse(pic, plist); ! pic_nil_p(pic, e); e = pic_cddr(pic, e)) { pic_assert_type(pic, pic_cadr(pic, e), sym); pic_dict_set(pic, dict, pic_sym_ptr(pic_cadr(pic, e)), pic_car(pic, e)); } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 3a3af27e..306b69c2 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -43,7 +43,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) msg = pic_str_cstr(pic, err); - pic_error(pic, msg, pic_nil_value()); + pic_error(pic, msg, pic_nil_value(pic)); } pic_value @@ -58,7 +58,7 @@ pic_native_exception_handler(pic_state *pic) cont = pic_proc_ptr(pic_closure_ref(pic, 0)); - pic_call(pic, cont, 1, pic_false_value()); + pic_call(pic, cont, 1, pic_false_value(pic)); PIC_UNREACHABLE(); } @@ -202,7 +202,7 @@ pic_error_error_object_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_error_p(v)); + return pic_bool_value(pic, pic_error_p(pic, v)); } static pic_value diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 80799d47..a87c8f75 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -11,13 +11,13 @@ optimize_beta(pic_state *pic, pic_value expr) size_t ai = pic_gc_arena_preserve(pic); pic_value functor, formals, args, tmp, val, it, defs; - if (! pic_list_p(expr)) + if (! pic_list_p(pic, expr)) return expr; - if (pic_nil_p(expr)) + if (pic_nil_p(pic, expr)) return expr; - if (pic_sym_p(pic_list_ref(pic, expr, 0))) { + if (pic_sym_p(pic, pic_list_ref(pic, expr, 0))) { pic_sym *sym = pic_sym_ptr(pic_list_ref(pic, expr, 0)); if (sym == pic->sQUOTE) { @@ -27,7 +27,7 @@ optimize_beta(pic_state *pic, pic_value expr) } } - tmp = pic_nil_value(); + tmp = pic_nil_value(pic); pic_for_each (val, expr, it) { pic_push(pic, optimize_beta(pic, val), tmp); } @@ -37,14 +37,14 @@ optimize_beta(pic_state *pic, pic_value expr) pic_gc_protect(pic, expr); functor = pic_list_ref(pic, expr, 0); - if (pic_pair_p(functor) && pic_eq_p(pic_car(pic, functor), pic_obj_value(pic->sLAMBDA))) { + if (pic_pair_p(pic, functor) && pic_eq_p(pic, pic_car(pic, functor), pic_obj_value(pic->sLAMBDA))) { formals = pic_list_ref(pic, functor, 1); - if (! pic_list_p(formals)) + if (! pic_list_p(pic, formals)) goto exit; /* TODO: support ((lambda args x) 1 2) */ args = pic_cdr(pic, expr); if (pic_length(pic, formals) != pic_length(pic, args)) goto exit; - defs = pic_nil_value(); + defs = pic_nil_value(pic); pic_for_each (val, args, it) { pic_push(pic, pic_list3(pic, pic_obj_value(pic->sDEFINE), pic_car(pic, formals), val), defs); formals = pic_cdr(pic, formals); @@ -92,10 +92,10 @@ analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, anal kh_init(a, &scope->captures); /* analyze formal */ - for (; pic_pair_p(formal); formal = pic_cdr(pic, formal)) { + for (; pic_pair_p(pic, formal); formal = pic_cdr(pic, formal)) { kh_put(a, &scope->args, pic_sym_ptr(pic_car(pic, formal)), &ret); } - if (pic_nil_p(formal)) { + if (pic_nil_p(pic, formal)) { scope->rest = NULL; } else { @@ -105,7 +105,7 @@ analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, anal scope->up = up; scope->depth = up ? up->depth + 1 : 0; - scope->defer = pic_list1(pic, pic_nil_value()); + scope->defer = pic_list1(pic, pic_nil_value(pic)); } static void @@ -177,7 +177,7 @@ analyze_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) } else if (depth == 0) { return pic_list2(pic, pic_obj_value(LREF), pic_obj_value(sym)); } else { - return pic_list3(pic, pic_obj_value(CREF), pic_int_value(depth), pic_obj_value(sym)); + return pic_list3(pic, pic_obj_value(CREF), pic_int_value(pic, depth), pic_obj_value(sym)); } } @@ -215,7 +215,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) { analyze_scope s, *scope = &s; pic_value formals, body; - pic_value rest = pic_undef_value(); + pic_value rest = pic_undef_value(pic); pic_vec *args, *locals, *captures; int i, j; khiter_t it; @@ -230,7 +230,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) analyze_deferred(pic, scope); args = pic_make_vec(pic, kh_size(&scope->args)); - for (i = 0; pic_pair_p(formals); formals = pic_cdr(pic, formals), i++) { + for (i = 0; pic_pair_p(pic, formals); formals = pic_cdr(pic, formals), i++) { args->data[i] = pic_car(pic, formals); } @@ -266,7 +266,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) static pic_value analyze_list(pic_state *pic, analyze_scope *scope, pic_value obj) { - pic_value seq = pic_nil_value(), val, it; + pic_value seq = pic_nil_value(pic), val, it; pic_for_each (val, obj, it) { pic_push(pic, analyze(pic, scope, val), seq); @@ -292,19 +292,19 @@ analyze_call(pic_state *pic, analyze_scope *scope, pic_value obj) static pic_value analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) { - switch (pic_type(obj)) { + switch (pic_type(pic, obj)) { case PIC_TT_SYMBOL: { return analyze_var(pic, scope, pic_sym_ptr(obj)); } case PIC_TT_PAIR: { pic_value proc; - if (! pic_list_p(obj)) { + if (! pic_list_p(pic, obj)) { pic_errorf(pic, "invalid expression given: ~s", obj); } proc = pic_list_ref(pic, obj, 0); - if (pic_sym_p(proc)) { + if (pic_sym_p(pic, proc)) { pic_sym *sym = pic_sym_ptr(proc); if (sym == pic->sDEFINE) { @@ -346,7 +346,7 @@ pic_analyze(pic_state *pic, pic_value obj) { analyze_scope s, *scope = &s; - analyzer_scope_init(pic, scope, pic_nil_value(), NULL); + analyzer_scope_init(pic, scope, pic_nil_value(pic), NULL); obj = analyze(pic, scope, obj); @@ -562,7 +562,7 @@ codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) pic_sym *name; int depth; - depth = pic_int(pic_list_ref(pic, obj, 1)); + depth = pic_int(pic, pic_list_ref(pic, obj, 1)); name = pic_sym_ptr(pic_list_ref(pic, obj, 2)); emit_r(pic, cxt, OP_CREF, depth, index_capture(cxt, name, depth)); emit_ret(pic, cxt, tailpos); @@ -604,7 +604,7 @@ codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) pic_sym *name; int depth; - depth = pic_int(pic_list_ref(pic, var, 1)); + depth = pic_int(pic, pic_list_ref(pic, var, 1)); name = pic_sym_ptr(pic_list_ref(pic, var, 2)); emit_r(pic, cxt, OP_CSET, depth, index_capture(cxt, name, depth)); emit_ret(pic, cxt, tailpos); @@ -636,7 +636,7 @@ codegen_lambda(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos /* extract arguments */ rest_opt = pic_list_ref(pic, obj, 1); - if (pic_sym_p(rest_opt)) { + if (pic_sym_p(pic, rest_opt)) { rest = pic_sym_ptr(rest_opt); } args = pic_vec_ptr(pic_list_ref(pic, obj, 2)); @@ -693,23 +693,23 @@ codegen_quote(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) int pidx; obj = pic_list_ref(pic, obj, 1); - switch (pic_type(obj)) { + switch (pic_type(pic, obj)) { case PIC_TT_UNDEF: emit_n(pic, cxt, OP_PUSHUNDEF); break; case PIC_TT_BOOL: - emit_n(pic, cxt, (pic_true_p(obj) ? OP_PUSHTRUE : OP_PUSHFALSE)); + emit_n(pic, cxt, (pic_true_p(pic, obj) ? OP_PUSHTRUE : OP_PUSHFALSE)); break; case PIC_TT_INT: check_ints_size(pic, cxt); pidx = (int)cxt->klen++; - cxt->ints[pidx] = pic_int(obj); + cxt->ints[pidx] = pic_int(pic, obj); emit_i(pic, cxt, OP_PUSHINT, pidx); break; case PIC_TT_FLOAT: check_nums_size(pic, cxt); pidx = (int)cxt->flen++; - cxt->nums[pidx] = pic_float(obj); + cxt->nums[pidx] = pic_float(pic, obj); emit_i(pic, cxt, OP_PUSHFLOAT, pidx); break; case PIC_TT_NIL: @@ -721,7 +721,7 @@ codegen_quote(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) case PIC_TT_CHAR: check_ints_size(pic, cxt); pidx = (int)cxt->klen++; - cxt->ints[pidx] = pic_char(obj); + cxt->ints[pidx] = pic_char(pic, obj); emit_i(pic, cxt, OP_PUSHCHAR, pidx); break; default: diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 558bcf82..d91f6d31 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -143,7 +143,7 @@ gc_protect(pic_state *pic, struct pic_object *obj) pic_value pic_gc_protect(pic_state *pic, pic_value v) { - if (! pic_obj_p(v)) + if (! pic_obj_p(pic, v)) return v; gc_protect(pic, pic_obj_ptr(v)); @@ -258,7 +258,7 @@ static void gc_mark_object(pic_state *, struct pic_object *); static void gc_mark(pic_state *pic, pic_value v) { - if (! pic_obj_p(v)) + if (! pic_obj_p(pic, v)) return; gc_mark_object(pic, pic_obj_ptr(v)); @@ -279,7 +279,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) switch (obj->u.basic.tt) { case PIC_TT_PAIR: { gc_mark(pic, obj->u.pair.car); - if (pic_obj_p(obj->u.pair.cdr)) { + if (pic_obj_p(pic, obj->u.pair.cdr)) { LOOP(pic_obj_ptr(obj->u.pair.cdr)); } break; @@ -369,7 +369,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } case PIC_TT_RECORD: { gc_mark(pic, obj->u.rec.type); - if (pic_obj_p(obj->u.rec.datum)) { + if (pic_obj_p(pic, obj->u.rec.datum)) { LOOP(pic_obj_ptr(obj->u.rec.datum)); } break; @@ -515,7 +515,7 @@ gc_mark_phase(pic_state *pic) key = kh_key(h, it); val = kh_val(h, it); if (key->u.basic.gc_mark == PIC_GC_MARK) { - if (pic_obj_p(val) && pic_obj_ptr(val)->u.basic.gc_mark == PIC_GC_UNMARK) { + if (pic_obj_p(pic, val) && pic_obj_ptr(val)->u.basic.gc_mark == PIC_GC_UNMARK) { gc_mark(pic, val); ++j; } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index b9931736..bdd3649b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -38,12 +38,12 @@ typedef struct pic_state pic_state; #include "picrin/type.h" -typedef void *(*pic_allocf)(void *, void *, size_t); +typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); -pic_state *pic_open(pic_allocf, void *); +pic_state *pic_open(pic_allocf f, void *userdata); void pic_close(pic_state *); -int pic_get_args(pic_state *, const char *, ...); +int pic_get_args(pic_state *, const char *fmt, ...); void *pic_malloc(pic_state *, size_t); void *pic_realloc(pic_state *, void *, size_t); @@ -58,83 +58,84 @@ size_t pic_gc_arena_preserve(pic_state *); void pic_gc_arena_restore(pic_state *, size_t); void pic_gc(pic_state *); -void pic_add_feature(pic_state *, const char *); +void pic_add_feature(pic_state *, const char *feature); -void pic_defun(pic_state *, const char *, pic_func_t); -void pic_defvar(pic_state *, const char *, pic_value, struct pic_proc *); +void pic_defun(pic_state *, const char *name, pic_func_t f); +void pic_defvar(pic_state *, const char *name, pic_value v, struct pic_proc *conv); -void pic_define(pic_state *, const char *, const char *, pic_value); -pic_value pic_ref(pic_state *, const char *, const char *); -void pic_set(pic_state *, const char *, const char *, pic_value); -pic_value pic_closure_ref(pic_state *, int); -void pic_closure_set(pic_state *, int, pic_value); -pic_value pic_funcall(pic_state *pic, const char *, const char *, int, ...); +void pic_define(pic_state *, const char *lib, const char *name, pic_value v); +pic_value pic_ref(pic_state *, const char *lib, const char *name); +void pic_set(pic_state *, const char *lib, const char *name, pic_value v); +pic_value pic_closure_ref(pic_state *, int i); +void pic_closure_set(pic_state *, int i, pic_value v); +pic_value pic_funcall(pic_state *, const char *lib, const char *name, int n, ...); -void pic_make_library(pic_state *, const char *); -void pic_in_library(pic_state *, const char *); -bool pic_find_library(pic_state *, const char *); +void pic_make_library(pic_state *, const char *lib); +void pic_in_library(pic_state *, const char *lib); +bool pic_find_library(pic_state *, const char *lib); const char *pic_current_library(pic_state *); -void pic_import(pic_state *, const char *); -void pic_export(pic_state *, pic_sym *); +void pic_import(pic_state *, const char *lib); +void pic_export(pic_state *, pic_sym *sym); -PIC_NORETURN void pic_panic(pic_state *, const char *); -PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); +PIC_NORETURN void pic_panic(pic_state *, const char *msg); +PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); -struct pic_proc *pic_lambda(pic_state *, pic_func_t, int, ...); -struct pic_proc *pic_vlambda(pic_state *, pic_func_t, int, va_list); -pic_value pic_call(pic_state *, struct pic_proc *, int, ...); -pic_value pic_vcall(pic_state *, struct pic_proc *, int, va_list); -pic_value pic_apply(pic_state *, struct pic_proc *, int, pic_value *); -pic_value pic_applyk(pic_state *, struct pic_proc *, int, pic_value *); +struct pic_proc *pic_lambda(pic_state *, pic_func_t f, int n, ...); +struct pic_proc *pic_vlambda(pic_state *, pic_func_t f, int n, va_list); +pic_value pic_call(pic_state *, struct pic_proc *proc, int, ...); +pic_value pic_vcall(pic_state *, struct pic_proc *proc, int, va_list); +pic_value pic_apply(pic_state *, struct pic_proc *proc, int n, pic_value *argv); +pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv); -int pic_int(pic_value); -double pic_float(pic_value); -char pic_char(pic_value); -bool pic_bool(pic_value); +int pic_int(pic_state *, pic_value); +double pic_float(pic_state *, pic_value); +char pic_char(pic_state *, pic_value); +bool pic_bool(pic_state *, pic_value); /* const char *pic_str(pic_state *, pic_value); */ /* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ /* void *pic_data(pic_state *, pic_value); */ -pic_value pic_undef_value(); -pic_value pic_int_value(int); -pic_value pic_float_value(double); -pic_value pic_char_value(char c); -pic_value pic_true_value(); -pic_value pic_false_value(); -pic_value pic_bool_value(bool); +pic_value pic_undef_value(pic_state *); +pic_value pic_int_value(pic_state *, int); +pic_value pic_float_value(pic_state *, double); +pic_value pic_char_value(pic_state *, char); +pic_value pic_true_value(pic_state *); +pic_value pic_false_value(pic_state *); +pic_value pic_bool_value(pic_state *, bool); -#define pic_undef_p(v) (pic_vtype(v) == PIC_VTYPE_UNDEF) -#define pic_int_p(v) (pic_vtype(v) == PIC_VTYPE_INT) -#define pic_float_p(v) (pic_vtype(v) == PIC_VTYPE_FLOAT) -#define pic_char_p(v) (pic_vtype(v) == PIC_VTYPE_CHAR) -#define pic_true_p(v) (pic_vtype(v) == PIC_VTYPE_TRUE) -#define pic_false_p(v) (pic_vtype(v) == PIC_VTYPE_FALSE) -#define pic_str_p(v) (pic_type(v) == PIC_TT_STRING) -#define pic_blob_p(v) (pic_type(v) == PIC_TT_BLOB) -#define pic_proc_p(o) (pic_type(o) == PIC_TT_PROC) -#define pic_data_p(o) (pic_type(o) == PIC_TT_DATA) -#define pic_nil_p(v) (pic_vtype(v) == PIC_VTYPE_NIL) -#define pic_pair_p(v) (pic_type(v) == PIC_TT_PAIR) -#define pic_vec_p(v) (pic_type(v) == PIC_TT_VECTOR) -#define pic_dict_p(v) (pic_type(v) == PIC_TT_DICT) -#define pic_weak_p(v) (pic_type(v) == PIC_TT_WEAK) -#define pic_sym_p(v) (pic_type(v) == PIC_TT_SYMBOL) +#define pic_undef_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_UNDEF) +#define pic_int_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_INT) +#define pic_float_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_FLOAT) +#define pic_char_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_CHAR) +#define pic_true_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_TRUE) +#define pic_false_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_FALSE) +#define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TT_STRING) +#define pic_blob_p(pic,v) (pic_type(pic,v) == PIC_TT_BLOB) +#define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TT_PROC) +#define pic_data_p(pic,v) (pic_type(pic,v) == PIC_TT_DATA) +#define pic_nil_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_NIL) +#define pic_pair_p(pic,v) (pic_type(pic,v) == PIC_TT_PAIR) +#define pic_vec_p(pic,v) (pic_type(pic,v) == PIC_TT_VECTOR) +#define pic_dict_p(pic,v) (pic_type(pic,v) == PIC_TT_DICT) +#define pic_weak_p(pic,v) (pic_type(pic,v) == PIC_TT_WEAK) +#define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TT_PORT) +#define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TT_SYMBOL) -enum pic_tt pic_type(pic_value); -const char *pic_type_repr(enum pic_tt); +enum pic_tt pic_type(pic_state *, pic_value); +const char *pic_type_repr(pic_state *, enum pic_tt); -bool pic_eq_p(pic_value, pic_value); -bool pic_eqv_p(pic_value, pic_value); +bool pic_eq_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); /* list */ -pic_value pic_nil_value(); +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_value); +bool pic_list_p(pic_state *, pic_value); pic_value pic_list(pic_state *, int n, ...); pic_value pic_vlist(pic_state *, int n, va_list); pic_value pic_list_ref(pic_state *, pic_value, int); @@ -171,7 +172,7 @@ pic_sym *pic_intern(pic_state *, struct pic_string *); const char *pic_symbol_name(pic_state *, pic_sym *); /* string */ -int pic_str_len(struct pic_string *); +int pic_str_len(pic_state *, struct pic_string *); char pic_str_ref(pic_state *, struct pic_string *, int); struct pic_string *pic_str_cat(pic_state *, struct pic_string *, struct pic_string *); struct pic_string *pic_str_sub(pic_state *, struct pic_string *, int, int); @@ -199,6 +200,11 @@ int pic_str_hash(pic_state *, struct pic_string *); void *pic_default_allocf(void *, void *, size_t); +#define pic_assert_type(pic, v, type) \ + if (! pic_##type##_p(pic, v)) { \ + pic_errorf(pic, "expected " #type ", but got ~s", v); \ + } + struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); #define pic_void(exec) \ diff --git a/extlib/benz/include/picrin/data.h b/extlib/benz/include/picrin/data.h index a7f03580..fdd88008 100644 --- a/extlib/benz/include/picrin/data.h +++ b/extlib/benz/include/picrin/data.h @@ -23,8 +23,8 @@ struct pic_data { #define pic_data_ptr(o) ((struct pic_data *)pic_ptr(o)) -PIC_INLINE bool pic_data_type_p(const pic_value obj, const pic_data_type *type) { - return pic_data_p(obj) && pic_data_ptr(obj)->type == type; +PIC_INLINE bool pic_data_type_p(pic_state *pic, const pic_value obj, const pic_data_type *type) { + return pic_data_p(pic, obj) && pic_data_ptr(obj)->type == type; } struct pic_data *pic_data_alloc(pic_state *, const pic_data_type *, void *); diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h index 36e35409..235a9d87 100644 --- a/extlib/benz/include/picrin/error.h +++ b/extlib/benz/include/picrin/error.h @@ -17,7 +17,7 @@ struct pic_error { struct pic_string *stack; }; -#define pic_error_p(v) (pic_type(v) == PIC_TT_ERROR) +#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TT_ERROR) #define pic_error_ptr(v) ((struct pic_error *)pic_ptr(v)) struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_value); diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index 0d4c6a40..a8fab6ea 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -18,7 +18,7 @@ struct pic_env { struct pic_string *lib; }; -#define pic_env_p(v) (pic_type(v) == PIC_TT_ENV) +#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TT_ENV) #define pic_env_ptr(v) ((struct pic_env *)pic_ptr(v)) struct pic_env *pic_make_topenv(pic_state *, struct pic_string *); diff --git a/extlib/benz/include/picrin/pair.h b/extlib/benz/include/picrin/pair.h index ff6538ed..ddd5a706 100644 --- a/extlib/benz/include/picrin/pair.h +++ b/extlib/benz/include/picrin/pair.h @@ -22,7 +22,7 @@ pic_car(pic_state *pic, pic_value obj) { struct pic_pair *pair; - if (! pic_pair_p(obj)) { + if (! pic_pair_p(pic, obj)) { pic_errorf(pic, "car: pair required, but got ~s", obj); } pair = pic_pair_ptr(obj); @@ -35,7 +35,7 @@ pic_cdr(pic_state *pic, pic_value obj) { struct pic_pair *pair; - if (! pic_pair_p(obj)) { + if (! pic_pair_p(pic, obj)) { pic_errorf(pic, "cdr: pair required, but got ~s", obj); } pair = pic_pair_ptr(obj); @@ -53,8 +53,8 @@ pic_value pic_list7(pic_state *, pic_value, pic_value, pic_value, pic_value, pic 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(it); it = pic_cdr(pic, it)) \ +#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)) diff --git a/extlib/benz/include/picrin/port.h b/extlib/benz/include/picrin/port.h index c806ba8e..22674b33 100644 --- a/extlib/benz/include/picrin/port.h +++ b/extlib/benz/include/picrin/port.h @@ -23,7 +23,6 @@ struct pic_port { int flags; }; -#define pic_port_p(v) (pic_type(v) == PIC_TT_PORT) #define pic_port_ptr(v) ((struct pic_port *)pic_ptr(v)) pic_value pic_eof_object(); diff --git a/extlib/benz/include/picrin/proc.h b/extlib/benz/include/picrin/proc.h index 36baeba1..b536868e 100644 --- a/extlib/benz/include/picrin/proc.h +++ b/extlib/benz/include/picrin/proc.h @@ -41,7 +41,7 @@ struct pic_proc { #define pic_proc_ptr(o) ((struct pic_proc *)pic_ptr(o)) -#define pic_context_p(o) (pic_type(o) == PIC_TT_CXT) +#define pic_context_p(o) (pic_type(pic, o) == PIC_TT_CXT) #define pic_context_ptr(o) ((struct pic_context *)pic_ptr(o)) struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); diff --git a/extlib/benz/include/picrin/record.h b/extlib/benz/include/picrin/record.h index 2ccf2669..d45cef27 100644 --- a/extlib/benz/include/picrin/record.h +++ b/extlib/benz/include/picrin/record.h @@ -15,7 +15,7 @@ struct pic_record { pic_value datum; }; -#define pic_rec_p(v) (pic_type(v) == PIC_TT_RECORD) +#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TT_RECORD) #define pic_rec_ptr(v) ((struct pic_record *)pic_ptr(v)) struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); diff --git a/extlib/benz/include/picrin/symbol.h b/extlib/benz/include/picrin/symbol.h index 3c905cd9..0d1ff11c 100644 --- a/extlib/benz/include/picrin/symbol.h +++ b/extlib/benz/include/picrin/symbol.h @@ -25,7 +25,7 @@ struct pic_id { #define pic_sym_ptr(v) ((pic_sym *)pic_ptr(v)) -#define pic_id_p(v) (pic_type(v) == PIC_TT_ID || pic_type(v) == PIC_TT_SYMBOL) +#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TT_ID || pic_type(pic, v) == PIC_TT_SYMBOL) #define pic_id_ptr(v) ((pic_id *)pic_ptr(v)) pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index 1e93c372..5e6f92d6 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -45,13 +45,13 @@ typedef uint64_t pic_value; #define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48))) static inline enum pic_vtype -pic_vtype(pic_value v) +pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) { return 0xfff0 >= (v >> 48) ? PIC_VTYPE_FLOAT : ((v >> 48) & 0xf); } static inline double -pic_float(pic_value v) +pic_float(pic_state PIC_UNUSED(*pic), pic_value v) { union { double f; uint64_t i; } u; u.i = v; @@ -59,7 +59,7 @@ pic_float(pic_value v) } static inline int -pic_int(pic_value v) +pic_int(pic_state PIC_UNUSED(*pic), pic_value v) { union { int i; unsigned u; } u; u.u = v & 0xfffffffful; @@ -67,7 +67,7 @@ pic_int(pic_value v) } static inline char -pic_char(pic_value v) +pic_char(pic_state PIC_UNUSED(*pic), pic_value v) { return v & 0xfffffffful; } @@ -85,23 +85,23 @@ typedef struct { } pic_value; #define pic_ptr(v) ((v).u.data) -#define pic_vtype(v) ((v).type) +#define pic_vtype(pic,v) ((v).type) #define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) PIC_INLINE double -pic_float(pic_value v) +pic_float(pic_state PIC_UNUSED(*pic), pic_value v) { return v.u.f; } PIC_INLINE int -pic_int(pic_value v) +pic_int(pic_state PIC_UNUSED(*pic), pic_value v) { return v.u.i; } PIC_INLINE char -pic_char(pic_value v) +pic_char(pic_state PIC_UNUSED(*pic), pic_value v) { return v.u.c; } @@ -163,18 +163,13 @@ typedef struct pic_id pic_id; typedef struct pic_pair pic_pair; typedef struct pic_vector pic_vec; -#define pic_obj_p(v) (pic_vtype(v) == PIC_VTYPE_HEAP) +#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_HEAP) #define pic_obj_ptr(v) ((struct pic_object *)pic_ptr(v)) -#define pic_invalid_p(v) (pic_vtype(v) == PIC_VTYPE_INVALID) -#define pic_eof_p(v) (pic_vtype(v) == PIC_VTYPE_EOF) +#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_VTYPE_INVALID) +#define pic_eof_p(pic, v) (pic_vtype(pic, v) == PIC_VTYPE_EOF) -#define pic_test(v) (! pic_false_p(v)) - -#define pic_assert_type(pic, v, type) \ - if (! pic_##type##_p(v)) { \ - pic_errorf(pic, "expected " #type ", but got ~s", v); \ - } +#define pic_test(pic, v) (! pic_false_p(pic, v)) PIC_INLINE bool pic_valid_int(double v) @@ -186,9 +181,9 @@ PIC_INLINE pic_value pic_invalid_value(); PIC_INLINE pic_value pic_obj_value(void *); PIC_INLINE enum pic_tt -pic_type(pic_value v) +pic_type(pic_state PIC_UNUSED(*pic), pic_value v) { - switch (pic_vtype(v)) { + switch (pic_vtype(pic, v)) { case PIC_VTYPE_NIL: return PIC_TT_NIL; case PIC_VTYPE_TRUE: @@ -215,7 +210,7 @@ pic_type(pic_value v) } PIC_INLINE const char * -pic_type_repr(enum pic_tt tt) +pic_type_repr(pic_state PIC_UNUSED(*pic), enum pic_tt tt) { switch (tt) { case PIC_TT_NIL: @@ -271,7 +266,7 @@ pic_type_repr(enum pic_tt tt) } PIC_INLINE pic_value -pic_nil_value() +pic_nil_value(pic_state PIC_UNUSED(*pic)) { pic_value v; @@ -280,7 +275,7 @@ pic_nil_value() } PIC_INLINE pic_value -pic_true_value() +pic_true_value(pic_state PIC_UNUSED(*pic)) { pic_value v; @@ -289,7 +284,7 @@ pic_true_value() } PIC_INLINE pic_value -pic_false_value() +pic_false_value(pic_state PIC_UNUSED(*pic)) { pic_value v; @@ -298,7 +293,7 @@ pic_false_value() } PIC_INLINE pic_value -pic_bool_value(bool b) +pic_bool_value(pic_state PIC_UNUSED(*pic), bool b) { pic_value v; @@ -319,7 +314,7 @@ pic_obj_value(void *ptr) } PIC_INLINE pic_value -pic_float_value(double f) +pic_float_value(pic_state PIC_UNUSED(*pic), double f) { union { double f; uint64_t i; } u; @@ -332,7 +327,7 @@ pic_float_value(double f) } PIC_INLINE pic_value -pic_int_value(int i) +pic_int_value(pic_state PIC_UNUSED(*pic), int i) { union { int i; unsigned u; } u; pic_value v; @@ -345,7 +340,7 @@ pic_int_value(int i) } PIC_INLINE pic_value -pic_char_value(char c) +pic_char_value(pic_state PIC_UNUSED(*pic), char c) { pic_value v; @@ -367,7 +362,7 @@ pic_obj_value(void *ptr) } PIC_INLINE pic_value -pic_float_value(double f) +pic_float_value(pic_state PIC_UNUSED(*pic), double f) { pic_value v; @@ -377,7 +372,7 @@ pic_float_value(double f) } PIC_INLINE pic_value -pic_int_value(int i) +pic_int_value(pic_state PIC_UNUSED(*pic), int i) { pic_value v; @@ -387,7 +382,7 @@ pic_int_value(int i) } PIC_INLINE pic_value -pic_char_value(char c) +pic_char_value(pic_state PIC_UNUSED(*pic), char c) { pic_value v; @@ -399,7 +394,7 @@ pic_char_value(char c) #endif PIC_INLINE pic_value -pic_undef_value() +pic_undef_value(pic_state PIC_UNUSED(*pic)) { pic_value v; @@ -419,13 +414,13 @@ pic_invalid_value() #if PIC_NAN_BOXING PIC_INLINE bool -pic_eq_p(pic_value x, pic_value y) +pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) { return x == y; } PIC_INLINE bool -pic_eqv_p(pic_value x, pic_value y) +pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) { return x == y; } @@ -433,36 +428,36 @@ pic_eqv_p(pic_value x, pic_value y) #else PIC_INLINE bool -pic_eq_p(pic_value x, pic_value y) +pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) { - if (pic_type(x) != pic_type(y)) + if (pic_type(pic, x) != pic_type(pic, y)) return false; - switch (pic_type(x)) { + switch (pic_type(pic, x)) { case PIC_TT_NIL: return true; case PIC_TT_BOOL: - return pic_vtype(x) == pic_vtype(y); + return pic_vtype(pic, x) == pic_vtype(pic, y); default: return pic_ptr(x) == pic_ptr(y); } } PIC_INLINE bool -pic_eqv_p(pic_value x, pic_value y) +pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) { - if (pic_type(x) != pic_type(y)) + if (pic_type(pic, x) != pic_type(pic, y)) return false; - switch (pic_type(x)) { + switch (pic_type(pic, x)) { case PIC_TT_NIL: return true; case PIC_TT_BOOL: - return pic_vtype(x) == pic_vtype(y); + return pic_vtype(pic, x) == pic_vtype(pic, y); case PIC_TT_FLOAT: - return pic_float(x) == pic_float(y); + return pic_float(pic, x) == pic_float(pic, y); case PIC_TT_INT: - return pic_int(x) == pic_int(y); + return pic_int(pic, x) == pic_int(pic, y); default: return pic_ptr(x) == pic_ptr(y); } diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index ffccffd0..d47051f5 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -137,7 +137,7 @@ pic_lib_make_library(pic_state *pic) pic_make_library(pic, lib); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -147,7 +147,7 @@ pic_lib_find_library(pic_state *pic) pic_get_args(pic, "z", &lib); - return pic_bool_value(pic_find_library(pic, lib)); + return pic_bool_value(pic, pic_find_library(pic, lib)); } static pic_value @@ -164,7 +164,7 @@ pic_lib_current_library(pic_state *pic) else { pic_in_library(pic, lib); - return pic_undef_value(); + return pic_undef_value(pic); } } @@ -195,7 +195,7 @@ pic_lib_library_import(pic_state *pic) pic_put_identifier(pic, (pic_id *)alias, uid, pic->lib->env); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -211,14 +211,14 @@ pic_lib_library_export(pic_state *pic) pic_dict_set(pic, pic->lib->exports, alias, pic_obj_value(name)); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value pic_lib_library_exports(pic_state *pic) { const char *lib; - pic_value exports = pic_nil_value(); + pic_value exports = pic_nil_value(pic); pic_sym *sym; khiter_t it; struct pic_lib *libp; diff --git a/extlib/benz/load.c b/extlib/benz/load.c index f58ce1be..f1a8f26c 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -10,7 +10,7 @@ pic_load(pic_state *pic, struct pic_port *port) pic_value form; size_t ai = pic_gc_arena_preserve(pic); - while (! pic_eof_p(form = pic_read(pic, port))) { + while (! pic_eof_p(pic, form = pic_read(pic, port))) { pic_eval(pic, form, pic_current_library(pic)); pic_gc_arena_restore(pic, ai); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 50a6b9ac..176c39bf 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -41,7 +41,7 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) name = pic_identifier_name(pic, id); - if (env->up == NULL && pic_sym_p(pic_obj_value(id))) { /* toplevel & public */ + if (env->up == NULL && pic_sym_p(pic, pic_obj_value(id))) { /* toplevel & public */ str = pic_format(pic, "%s/%s", pic_str_cstr(pic, env->lib), name); } else { str = pic_format(pic, ".%s.%d", name, pic->ucnt++); @@ -96,7 +96,7 @@ pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) pic_sym *uid; while ((uid = search(pic, id, env)) == NULL) { - if (pic_sym_p(pic_obj_value(id))) { + if (pic_sym_p(pic, pic_obj_value(id))) { break; } env = id->u.id.env; /* do not overwrite id first */ @@ -172,7 +172,7 @@ expand_list(pic_state *pic, pic_value obj, struct pic_env *env, pic_value deferr size_t ai = pic_gc_arena_preserve(pic); pic_value x, head, tail; - if (pic_pair_p(obj)) { + if (pic_pair_p(pic, obj)) { head = expand(pic, pic_car(pic, obj), env, deferred); tail = expand_list(pic, pic_cdr(pic, obj), env, deferred); x = pic_cons(pic, head, tail); @@ -223,14 +223,14 @@ expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env) in = pic_make_env(pic, env); - for (a = pic_cadr(pic, expr); pic_pair_p(a); a = pic_cdr(pic, a)) { + for (a = pic_cadr(pic, expr); pic_pair_p(pic, a); a = pic_cdr(pic, a)) { pic_add_identifier(pic, pic_id_ptr(pic_car(pic, a)), in); } - if (pic_id_p(a)) { + if (pic_id_p(pic, a)) { pic_add_identifier(pic, pic_id_ptr(a), in); } - deferred = pic_list1(pic, pic_nil_value()); + deferred = pic_list1(pic, pic_nil_value(pic)); formal = expand_list(pic, pic_list_ref(pic, expr, 1), in, deferred); body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred); @@ -272,19 +272,19 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) } val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); - if (! pic_proc_p(val)) { + if (! pic_proc_p(pic, val)) { pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_identifier_name(pic, id)); } define_macro(pic, uid, pic_proc_ptr(val)); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) { - switch (pic_type(expr)) { + switch (pic_type(pic, expr)) { case PIC_TT_ID: case PIC_TT_SYMBOL: { return expand_var(pic, pic_id_ptr(expr), env, deferred); @@ -292,11 +292,11 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer case PIC_TT_PAIR: { struct pic_proc *mac; - if (! pic_list_p(expr)) { + if (! pic_list_p(pic, expr)) { pic_errorf(pic, "cannot expand improper list: ~s", expr); } - if (pic_id_p(pic_car(pic, expr))) { + if (pic_id_p(pic, pic_car(pic, expr))) { pic_sym *functor; functor = pic_find_identifier(pic, pic_id_ptr(pic_car(pic, expr)), env); @@ -349,7 +349,7 @@ pic_expand(pic_state *pic, pic_value expr, struct pic_env *env) puts(""); #endif - deferred = pic_list1(pic, pic_nil_value()); + deferred = pic_list1(pic, pic_nil_value(pic)); v = expand(pic, expr, env, deferred); diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 32f86608..580e481e 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -11,7 +11,7 @@ pic_number_number_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_float_p(v) || pic_int_p(v)); + return pic_bool_value(pic, pic_float_p(pic, v) || pic_int_p(pic, v)); } static pic_value @@ -21,7 +21,7 @@ pic_number_exact_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_int_p(v)); + return pic_bool_value(pic, pic_int_p(pic, v)); } static pic_value @@ -31,7 +31,7 @@ pic_number_inexact_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_float_p(v)); + return pic_bool_value(pic, pic_float_p(pic, v)); } static pic_value @@ -41,7 +41,7 @@ pic_number_inexact(pic_state *pic) pic_get_args(pic, "f", &f); - return pic_float_value(f); + return pic_float_value(pic, f); } static pic_value @@ -51,7 +51,7 @@ pic_number_exact(pic_state *pic) pic_get_args(pic, "f", &f); - return pic_int_value((int)f); + return pic_int_value(pic, (int)f); } #define pic_define_aop(name, op, guard) \ @@ -60,17 +60,17 @@ pic_number_exact(pic_state *pic) { \ PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ double f; \ - if (pic_int_p(a) && pic_int_p(b)) { \ - f = (double)pic_int(a) op (double)pic_int(b); \ + if (pic_int_p(pic, a) && pic_int_p(pic, b)) { \ + f = (double)pic_int(pic, a) op (double)pic_int(pic, b); \ return (INT_MIN <= f && f <= INT_MAX && guard) \ - ? pic_int_value((int)f) \ - : pic_float_value(f); \ - } else if (pic_float_p(a) && pic_float_p(b)) { \ - return pic_float_value(pic_float(a) op pic_float(b)); \ - } else if (pic_int_p(a) && pic_float_p(b)) { \ - return pic_float_value(pic_int(a) op pic_float(b)); \ - } else if (pic_float_p(a) && pic_int_p(b)) { \ - return pic_float_value(pic_float(a) op pic_int(b)); \ + ? pic_int_value(pic, (int)f) \ + : pic_float_value(pic, f); \ + } else if (pic_float_p(pic, a) && pic_float_p(pic, b)) { \ + return pic_float_value(pic, pic_float(pic, a) op pic_float(pic, b)); \ + } else if (pic_int_p(pic, a) && pic_float_p(pic, b)) { \ + return pic_float_value(pic, pic_int(pic, a) op pic_float(pic, b)); \ + } else if (pic_float_p(pic, a) && pic_int_p(pic, b)) { \ + return pic_float_value(pic, pic_float(pic, a) op pic_int(pic, b)); \ } else { \ pic_errorf(pic, #name ": non-number operand given"); \ } \ @@ -87,14 +87,14 @@ pic_define_aop(pic_div, /, f == (int)f) name(pic_state *pic, pic_value a, pic_value b) \ { \ PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ - if (pic_int_p(a) && pic_int_p(b)) { \ - return pic_int(a) op pic_int(b); \ - } else if (pic_float_p(a) && pic_float_p(b)) { \ - return pic_float(a) op pic_float(b); \ - } else if (pic_int_p(a) && pic_float_p(b)) { \ - return pic_int(a) op pic_float(b); \ - } else if (pic_float_p(a) && pic_int_p(b)) { \ - return pic_float(a) op pic_int(b); \ + if (pic_int_p(pic, a) && pic_int_p(pic, b)) { \ + return pic_int(pic, a) op pic_int(pic, b); \ + } else if (pic_float_p(pic, a) && pic_float_p(pic, b)) { \ + return pic_float(pic, a) op pic_float(pic, b); \ + } else if (pic_int_p(pic, a) && pic_float_p(pic, b)) { \ + return pic_int(pic, a) op pic_float(pic, b); \ + } else if (pic_float_p(pic, a) && pic_int_p(pic, b)) { \ + return pic_float(pic, a) op pic_int(pic, b); \ } else { \ pic_errorf(pic, #name ": non-number operand given"); \ } \ @@ -117,15 +117,15 @@ pic_define_cmp(pic_ge, >=) pic_get_args(pic, "*", &argc, &argv); \ \ if (argc < 2) { \ - return pic_true_value(); \ + return pic_true_value(pic); \ } \ \ for (i = 1; i < argc; ++i) { \ if (! pic_##op(pic, argv[i - 1], argv[i])) { \ - return pic_false_value(); \ + return pic_false_value(pic); \ } \ } \ - return pic_true_value(); \ + return pic_true_value(pic); \ } DEFINE_CMP(eq) @@ -158,15 +158,15 @@ DEFINE_CMP(ge) } DEFINE_AOP(add, argv[0], do { - return pic_int_value(0); + return pic_int_value(pic, 0); } while (0)) DEFINE_AOP(mul, argv[0], do { - return pic_int_value(1); + return pic_int_value(pic, 1); } while (0)) -DEFINE_AOP(sub, pic_sub(pic, pic_int_value(0), argv[0]), do { +DEFINE_AOP(sub, pic_sub(pic, pic_int_value(pic, 0), argv[0]), do { pic_errorf(pic, "-: at least one argument required"); } while (0)) -DEFINE_AOP(div, pic_div(pic, pic_int_value(1), argv[0]), do { +DEFINE_AOP(div, pic_div(pic, pic_int_value(pic, 1), argv[0]), do { pic_errorf(pic, "/: at least one argument required"); } while (0)) @@ -265,8 +265,8 @@ pic_number_string_to_number(pic_state *pic) num = strtol(str, &eptr, radix); if (*eptr == '\0') { return pic_valid_int(num) - ? pic_int_value((int)num) - : pic_float_value(num); + ? pic_int_value(pic, (int)num) + : pic_float_value(pic, num); } pic_try { @@ -274,14 +274,14 @@ pic_number_string_to_number(pic_state *pic) } pic_catch { /* swallow error */ - flo = pic_false_value(); + flo = pic_false_value(pic); } - if (pic_int_p(flo) || pic_float_p(flo)) { + if (pic_int_p(pic, flo) || pic_float_p(pic, flo)) { return flo; } - return pic_false_value(); + return pic_false_value(pic); } void diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 09138a80..80c95121 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -21,7 +21,7 @@ pic_set_car(pic_state *pic, pic_value obj, pic_value val) { struct pic_pair *pair; - if (! pic_pair_p(obj)) { + if (! pic_pair_p(pic, obj)) { pic_errorf(pic, "pair required"); } pair = pic_pair_ptr(obj); @@ -34,7 +34,7 @@ pic_set_cdr(pic_state *pic, pic_value obj, pic_value val) { struct pic_pair *pair; - if (! pic_pair_p(obj)) { + if (! pic_pair_p(pic, obj)) { pic_errorf(pic, "pair required"); } pair = pic_pair_ptr(obj); @@ -43,7 +43,7 @@ pic_set_cdr(pic_state *pic, pic_value obj, pic_value val) } bool -pic_list_p(pic_value obj) +pic_list_p(pic_state *pic, pic_value obj) { pic_value local, rapid; int i; @@ -55,18 +55,18 @@ pic_list_p(pic_value obj) /* advance rapid fast-forward; runs 2x faster than local */ for (i = 0; i < 2; ++i) { - if (pic_pair_p(rapid)) { + if (pic_pair_p(pic, rapid)) { rapid = pic_pair_ptr(rapid)->cdr; } else { - return pic_nil_p(rapid); + return pic_nil_p(pic, rapid); } } /* advance local */ local = pic_pair_ptr(local)->cdr; - if (pic_eq_p(local, rapid)) { + if (pic_eq_p(pic, local, rapid)) { return false; } } @@ -75,7 +75,7 @@ pic_list_p(pic_value obj) pic_value pic_list1(pic_state *pic, pic_value obj1) { - return pic_cons(pic, obj1, pic_nil_value()); + return pic_cons(pic, obj1, pic_nil_value(pic)); } pic_value @@ -161,7 +161,7 @@ pic_list_by_array(pic_state *pic, int c, pic_value *vs) { pic_value v; - v = pic_nil_value(); + v = pic_nil_value(pic); while (c--) { v = pic_cons(pic, vs[c], v); } @@ -174,7 +174,7 @@ pic_make_list(pic_state *pic, int k, pic_value fill) pic_value list; int i; - list = pic_nil_value(); + list = pic_nil_value(pic); for (i = 0; i < k; ++i) { list = pic_cons(pic, fill, list); } @@ -187,11 +187,11 @@ pic_length(pic_state *pic, pic_value obj) { int c = 0; - if (! pic_list_p(obj)) { + if (! pic_list_p(pic, obj)) { pic_errorf(pic, "length: expected list, but got ~s", obj); } - while (! pic_nil_p(obj)) { + while (! pic_nil_p(pic, obj)) { obj = pic_cdr(pic, obj); ++c; } @@ -205,7 +205,7 @@ pic_reverse(pic_state *pic, pic_value list) size_t ai = pic_gc_arena_preserve(pic); pic_value v, acc, it; - acc = pic_nil_value(); + acc = pic_nil_value(pic); pic_for_each(v, list, it) { acc = pic_cons(pic, v, acc); @@ -237,10 +237,10 @@ pic_memq(pic_state *pic, pic_value key, pic_value list) { enter: - if (pic_nil_p(list)) - return pic_false_value(); + if (pic_nil_p(pic, list)) + return pic_false_value(pic); - if (pic_eq_p(key, pic_car(pic, list))) + if (pic_eq_p(pic, key, pic_car(pic, list))) return list; list = pic_cdr(pic, list); @@ -252,10 +252,10 @@ pic_memv(pic_state *pic, pic_value key, pic_value list) { enter: - if (pic_nil_p(list)) - return pic_false_value(); + if (pic_nil_p(pic, list)) + return pic_false_value(pic); - if (pic_eqv_p(key, pic_car(pic, list))) + if (pic_eqv_p(pic, key, pic_car(pic, list))) return list; list = pic_cdr(pic, list); @@ -267,14 +267,14 @@ pic_member(pic_state *pic, pic_value key, pic_value list, struct pic_proc *compa { enter: - if (pic_nil_p(list)) - return pic_false_value(); + 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_call(pic, compar, 2, key, pic_car(pic, list)))) + if (pic_test(pic, pic_call(pic, compar, 2, key, pic_car(pic, list)))) return list; } @@ -289,11 +289,11 @@ pic_assq(pic_state *pic, pic_value key, pic_value assoc) enter: - if (pic_nil_p(assoc)) - return pic_false_value(); + if (pic_nil_p(pic, assoc)) + return pic_false_value(pic); cell = pic_car(pic, assoc); - if (pic_eq_p(key, pic_car(pic, cell))) + if (pic_eq_p(pic, key, pic_car(pic, cell))) return cell; assoc = pic_cdr(pic, assoc); @@ -307,11 +307,11 @@ pic_assv(pic_state *pic, pic_value key, pic_value assoc) enter: - if (pic_nil_p(assoc)) - return pic_false_value(); + if (pic_nil_p(pic, assoc)) + return pic_false_value(pic); cell = pic_car(pic, assoc); - if (pic_eqv_p(key, pic_car(pic, cell))) + if (pic_eqv_p(pic, key, pic_car(pic, cell))) return cell; assoc = pic_cdr(pic, assoc); @@ -325,15 +325,15 @@ pic_assoc(pic_state *pic, pic_value key, pic_value assoc, struct pic_proc *compa enter: - if (pic_nil_p(assoc)) - return pic_false_value(); + 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_call(pic, compar, 2, key, pic_car(pic, cell)))) + if (pic_test(pic, pic_call(pic, compar, 2, key, pic_car(pic, cell)))) return cell; } @@ -395,7 +395,7 @@ pic_list_set(pic_state *pic, pic_value list, int i, pic_value obj) pic_value pic_list_copy(pic_state *pic, pic_value obj) { - if (pic_pair_p(obj)) { + if (pic_pair_p(pic, obj)) { return pic_cons(pic, pic_car(pic, obj), pic_list_copy(pic, pic_cdr(pic, obj))); } else { @@ -410,7 +410,7 @@ pic_pair_pair_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_pair_p(v)); + return pic_bool_value(pic, pic_pair_p(pic, v)); } static pic_value @@ -492,7 +492,7 @@ pic_pair_set_car(pic_state *pic) pic_set_car(pic, v, w); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -504,7 +504,7 @@ pic_pair_set_cdr(pic_state *pic) pic_set_cdr(pic, v, w); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -514,7 +514,7 @@ pic_pair_null_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_nil_p(v)); + return pic_bool_value(pic, pic_nil_p(pic, v)); } static pic_value @@ -524,14 +524,14 @@ pic_pair_list_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_list_p(v)); + return pic_bool_value(pic, pic_list_p(pic, v)); } static pic_value pic_pair_make_list(pic_state *pic) { int i; - pic_value fill = pic_undef_value(); + pic_value fill = pic_undef_value(pic); pic_get_args(pic, "i|o", &i, &fill); @@ -556,7 +556,7 @@ pic_pair_length(pic_state *pic) pic_get_args(pic, "o", &list); - return pic_int_value(pic_length(pic, list)); + return pic_int_value(pic, pic_length(pic, list)); } static pic_value @@ -568,7 +568,7 @@ pic_pair_append(pic_state *pic) pic_get_args(pic, "*", &argc, &args); if (argc == 0) { - return pic_nil_value(); + return pic_nil_value(pic); } list = args[--argc]; @@ -621,7 +621,7 @@ pic_pair_list_set(pic_state *pic) pic_list_set(pic, list, i, obj); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -648,10 +648,10 @@ pic_pair_map(pic_state *pic) arg_list = pic_alloca(pic, sizeof(pic_value) * argc); - ret = pic_nil_value(); + ret = pic_nil_value(pic); do { for (i = 0; i < argc; ++i) { - if (! pic_pair_p(args[i])) { + if (! pic_pair_p(pic, args[i])) { break; } arg_list[i] = pic_car(pic, args[i]); @@ -680,7 +680,7 @@ pic_pair_for_each(pic_state *pic) do { for (i = 0; i < argc; ++i) { - if (! pic_pair_p(args[i])) { + if (! pic_pair_p(pic, args[i])) { break; } arg_list[i] = pic_car(pic, args[i]); @@ -692,7 +692,7 @@ pic_pair_for_each(pic_state *pic) pic_apply(pic, proc, i, arg_list); } while (1); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value diff --git a/extlib/benz/port.c b/extlib/benz/port.c index ed92381b..84b27e40 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -103,7 +103,7 @@ file_error(pic_state *pic, const char *msg) { struct pic_error *e; - e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value()); + e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value(pic)); pic_raise(pic, pic_obj_value(e)); } @@ -266,7 +266,7 @@ string_open(pic_state *pic, const char *data, size_t size) if (file == NULL) { string_close(pic, m); - pic_error(pic, "could not open new output string/bytevector port", pic_nil_value()); + pic_error(pic, "could not open new output string/bytevector port", pic_nil_value(pic)); } return file; } @@ -346,11 +346,11 @@ pic_port_input_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_IN) != 0) { - return pic_true_value(); + if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_IN) != 0) { + return pic_true_value(pic); } else { - return pic_false_value(); + return pic_false_value(pic); } } @@ -361,11 +361,11 @@ pic_port_output_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_OUT) != 0) { - return pic_true_value(); + if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_OUT) != 0) { + return pic_true_value(pic); } else { - return pic_false_value(); + return pic_false_value(pic); } } @@ -376,11 +376,11 @@ pic_port_textual_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_TEXT) != 0) { - return pic_true_value(); + if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_TEXT) != 0) { + return pic_true_value(pic); } else { - return pic_false_value(); + return pic_false_value(pic); } } @@ -391,11 +391,11 @@ pic_port_binary_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_BINARY) != 0) { - return pic_true_value(); + if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_BINARY) != 0) { + return pic_true_value(pic); } else { - return pic_false_value(); + return pic_false_value(pic); } } @@ -406,7 +406,7 @@ pic_port_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_port_p(v)); + return pic_bool_value(pic, pic_port_p(pic, v)); } static pic_value @@ -416,12 +416,7 @@ pic_port_eof_object_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_vtype(v) == PIC_VTYPE_EOF) { - return pic_true_value(); - } - else { - return pic_false_value(); - } + return pic_bool_value(pic, pic_eof_p(pic, v)); } static pic_value @@ -439,7 +434,7 @@ pic_port_port_open_p(pic_state *pic) pic_get_args(pic, "p", &port); - return pic_bool_value(port->flags & PIC_PORT_OPEN); + return pic_bool_value(pic, port->flags & PIC_PORT_OPEN); } static pic_value @@ -451,7 +446,7 @@ pic_port_close_port(pic_state *pic) pic_close_port(pic, port); - return pic_undef_value(); + return pic_undef_value(pic); } #define assert_port_profile(port, flgs, caller) do { \ @@ -581,7 +576,7 @@ pic_port_read_char(pic_state *pic) return pic_eof_object(); } else { - return pic_char_value((char)c); + return pic_char_value(pic, (char)c); } } @@ -600,7 +595,7 @@ pic_port_peek_char(pic_state *pic) } else { xungetc(c, port->file); - return pic_char_value((char)c); + return pic_char_value(pic, (char)c); } } @@ -622,7 +617,7 @@ pic_port_read_line(pic_state *pic) } str = pic_get_output_string(pic, buf); - if (pic_str_len(str) == 0 && c == EOF) { + if (pic_str_len(pic, str) == 0 && c == EOF) { /* EOF */ } else { res = pic_obj_value(str); @@ -640,7 +635,7 @@ pic_port_char_ready_p(pic_state *pic) pic_get_args(pic, "|p", &port); - return pic_true_value(); /* FIXME: always returns #t */ + return pic_true_value(pic); /* FIXME: always returns #t */ } static pic_value @@ -665,7 +660,7 @@ pic_port_read_string(pic_state *pic){ } str = pic_get_output_string(pic, buf); - if (pic_str_len(str) == 0 && c == EOF) { + if (pic_str_len(pic, str) == 0 && c == EOF) { /* EOF */ } else { res = pic_obj_value(str); @@ -685,7 +680,7 @@ pic_port_read_byte(pic_state *pic){ return pic_eof_object(); } - return pic_int_value(c); + return pic_int_value(pic, c); } static pic_value @@ -704,7 +699,7 @@ pic_port_peek_byte(pic_state *pic) } else { xungetc(c, port->file); - return pic_int_value(c); + return pic_int_value(pic, c); } } @@ -717,7 +712,7 @@ pic_port_byte_ready_p(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "u8-ready?"); - return pic_true_value(); /* FIXME: always returns #t */ + return pic_true_value(pic); /* FIXME: always returns #t */ } @@ -780,7 +775,7 @@ pic_port_read_blob_ip(pic_state *pic) return pic_eof_object(); } else { - return pic_int_value(i); + return pic_int_value(pic, i); } } @@ -794,7 +789,7 @@ pic_port_newline(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "newline"); xfputs(pic, "\n", port->file); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -808,7 +803,7 @@ pic_port_write_char(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-char"); xfputc(pic, c, port->file); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -833,7 +828,7 @@ pic_port_write_string(pic_state *pic) for (i = start; i < end && str[i] != '\0'; ++i) { xfputc(pic, str[i], port->file); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -847,7 +842,7 @@ pic_port_write_byte(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-u8"); xfputc(pic, i, port->file); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -872,7 +867,7 @@ pic_port_write_blob(pic_state *pic) for (i = start; i < end; ++i) { xfputc(pic, blob->data[i], port->file); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -885,7 +880,7 @@ pic_port_flush(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT, "flush-output-port"); xfflush(pic, port->file); - return pic_undef_value(); + return pic_undef_value(pic); } void diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 4fc209ca..699474d4 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -106,13 +106,13 @@ pic_get_args(pic_state *pic, const char *format, ...) e = (c == c2 ? va_arg(ap, bool *) : &dummy); \ \ v = GET_OPERAND(pic, i); \ - switch (pic_type(v)) { \ + switch (pic_type(pic, v)) { \ case PIC_TT_FLOAT: \ - *n = pic_float(v); \ + *n = pic_float(pic, v); \ *e = false; \ break; \ case PIC_TT_INT: \ - *n = pic_int(v); \ + *n = pic_int(pic, v); \ *e = true; \ break; \ default: \ @@ -131,7 +131,7 @@ pic_get_args(pic_state *pic, const char *format, ...) \ ptr = va_arg(ap, ctype *); \ v = GET_OPERAND(pic, i); \ - if (pic_## type ##_p(v)) { \ + if (pic_## type ##_p(pic, v)) { \ *ptr = conv; \ } \ else { \ @@ -140,7 +140,7 @@ pic_get_args(pic_state *pic, const char *format, ...) break; \ } - VAL_CASE('c', char, char, pic_char(v)) + VAL_CASE('c', char, char, pic_char(pic, v)) VAL_CASE('z', str, const char *, pic_str_cstr(pic, pic_str_ptr(v))) #define PTR_CASE(c, type, ctype) \ @@ -371,31 +371,31 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) NEXT; } CASE(OP_PUSHUNDEF) { - PUSH(pic_undef_value()); + PUSH(pic_undef_value(pic)); NEXT; } CASE(OP_PUSHNIL) { - PUSH(pic_nil_value()); + PUSH(pic_nil_value(pic)); NEXT; } CASE(OP_PUSHTRUE) { - PUSH(pic_true_value()); + PUSH(pic_true_value(pic)); NEXT; } CASE(OP_PUSHFALSE) { - PUSH(pic_false_value()); + PUSH(pic_false_value(pic)); NEXT; } CASE(OP_PUSHINT) { - PUSH(pic_int_value(pic->ci->irep->ints[c.a])); + PUSH(pic_int_value(pic, pic->ci->irep->ints[c.a])); NEXT; } CASE(OP_PUSHFLOAT) { - PUSH(pic_float_value(pic->ci->irep->nums[c.a])); + PUSH(pic_float_value(pic, pic->ci->irep->nums[c.a])); NEXT; } CASE(OP_PUSHCHAR) { - PUSH(pic_char_value(pic->ci->irep->ints[c.a])); + PUSH(pic_char_value(pic, pic->ci->irep->ints[c.a])); NEXT; } CASE(OP_PUSHEOF) { @@ -412,7 +412,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) } CASE(OP_GSET) { vm_gset(pic, (pic_sym *)pic->ci->irep->pool[c.a], POP()); - PUSH(pic_undef_value()); + PUSH(pic_undef_value(pic)); NEXT; } CASE(OP_LREF) { @@ -435,12 +435,12 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) { if (c.a >= irep->argc + irep->localc) { ci->cxt->regs[c.a - (ci->regs - ci->fp)] = POP(); - PUSH(pic_undef_value()); + PUSH(pic_undef_value(pic)); NEXT; } } pic->ci->fp[c.a] = POP(); - PUSH(pic_undef_value()); + PUSH(pic_undef_value(pic)); NEXT; } CASE(OP_CREF) { @@ -463,7 +463,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) cxt = cxt->up; } cxt->regs[c.b] = POP(); - PUSH(pic_undef_value()); + PUSH(pic_undef_value(pic)); NEXT; } CASE(OP_JMP) { @@ -474,7 +474,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) pic_value v; v = POP(); - if (! pic_false_p(v)) { + if (! pic_false_p(pic, v)) { pic->ip += c.a; JUMP; } @@ -491,7 +491,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) L_CALL: x = pic->sp[-c.a]; - if (! pic_proc_p(x)) { + if (! pic_proc_p(pic, x)) { pic_errorf(pic, "invalid application: ~s", x); } proc = pic_proc_ptr(x); @@ -532,7 +532,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) } /* prepare rest args */ if (irep->varg) { - rest = pic_nil_value(); + rest = pic_nil_value(pic); for (i = 0; i < ci->argc - irep->argc; ++i) { pic_gc_protect(pic, v = POP()); rest = pic_cons(pic, v, rest); @@ -546,7 +546,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) --l; } for (i = 0; i < l; ++i) { - PUSH(pic_undef_value()); + PUSH(pic_undef_value(pic)); } } @@ -659,7 +659,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) check_condition(NILP, 1); p = POP(); (void)POP(); - PUSH(pic_bool_value(pic_nil_p(p))); + PUSH(pic_bool_value(pic, pic_nil_p(pic, p))); NEXT; } CASE(OP_SYMBOLP) { @@ -667,7 +667,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) check_condition(SYMBOLP, 1); p = POP(); (void)POP(); - PUSH(pic_bool_value(pic_sym_p(p))); + PUSH(pic_bool_value(pic, pic_sym_p(pic, p))); NEXT; } CASE(OP_PAIRP) { @@ -675,13 +675,13 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) check_condition(PAIRP, 1); p = POP(); (void)POP(); - PUSH(pic_bool_value(pic_pair_p(p))); + PUSH(pic_bool_value(pic, pic_pair_p(pic, p))); NEXT; } CASE(OP_NOT) { pic_value v; check_condition(NOT, 1); - v = pic_false_p(POP()) ? pic_true_value() : pic_false_value(); + v = pic_false_p(pic, POP()) ? pic_true_value(pic) : pic_false_value(pic); (void)POP(); PUSH(v); NEXT; @@ -729,7 +729,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) b = POP(); a = POP(); (void)POP(); - PUSH(pic_bool_value(pic_eq(pic, a, b))); + PUSH(pic_bool_value(pic, pic_eq(pic, a, b))); NEXT; } CASE(OP_LE) { @@ -738,7 +738,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) b = POP(); a = POP(); (void)POP(); - PUSH(pic_bool_value(pic_le(pic, a, b))); + PUSH(pic_bool_value(pic, pic_le(pic, a, b))); NEXT; } CASE(OP_LT) { @@ -747,7 +747,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) b = POP(); a = POP(); (void)POP(); - PUSH(pic_bool_value(pic_lt(pic, a, b))); + PUSH(pic_bool_value(pic, pic_lt(pic, a, b))); NEXT; } CASE(OP_GE) { @@ -756,7 +756,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) b = POP(); a = POP(); (void)POP(); - PUSH(pic_bool_value(pic_ge(pic, a, b))); + PUSH(pic_bool_value(pic, pic_ge(pic, a, b))); NEXT; } CASE(OP_GT) { @@ -765,7 +765,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) b = POP(); a = POP(); (void)POP(); - PUSH(pic_bool_value(pic_gt(pic, a, b))); + PUSH(pic_bool_value(pic, pic_gt(pic, a, b))); NEXT; } @@ -801,7 +801,7 @@ pic_applyk(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args) ci->retc = (int)argc; if (ci->retc == 0) { - return pic_undef_value(); + return pic_undef_value(pic); } else { return args[0]; } @@ -1033,7 +1033,7 @@ pic_proc_proc_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_proc_p(v)); + return pic_bool_value(pic, pic_proc_p(pic, v)); } static pic_value diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 84522faa..3819d9e5 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -226,7 +226,7 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c) unsigned u = 0; if (! isdigit(c)) { - read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(c))); + read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(pic, c))); } u = c - '0'; @@ -247,7 +247,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) int dpe = 0; /* the number of '.' or 'e' characters seen */ if (! isdigit(c)) { - read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(c))); + read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(pic, c))); } buf[idx++] = (char )c; while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) { @@ -274,7 +274,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) break; } if (! isdigit(peek(pic, port))) { - read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(c))); + read_error(pic, "expected one or more digits", pic_list1(pic, pic_char_value(pic, c))); } while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) { buf[idx++] = (char )next(pic, port); @@ -285,14 +285,14 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) pic_obj_value(pic_make_str(pic, (const char *)buf, ATOF_BUF_SIZE))); if (! isdelim(c)) - read_error(pic, "non-delimiter character given after number", pic_list1(pic, pic_char_value(c))); + read_error(pic, "non-delimiter character given after number", pic_list1(pic, pic_char_value(pic, c))); buf[idx] = 0; flt = PIC_CSTRING_TO_DOUBLE(buf); if (dpe == 0 && pic_valid_int(flt)) - return pic_int_value((int )flt); - return pic_float_value(flt); + return pic_int_value(pic, (int )flt); + return pic_float_value(pic, flt); } static pic_value @@ -302,12 +302,12 @@ read_number(pic_state *pic, struct pic_port *port, int c) } static pic_value -negate(pic_value n) +negate(pic_state *pic, pic_value n) { - if (pic_int_p(n) && (INT_MIN != pic_int(n))) { - return pic_int_value(-pic_int(n)); + if (pic_int_p(pic, n) && (INT_MIN != pic_int(pic, n))) { + return pic_int_value(pic, -pic_int(pic, n)); } else { - return pic_float_value(-pic_float(n)); + return pic_float_value(pic, -pic_float(pic, n)); } } @@ -317,15 +317,15 @@ read_minus(pic_state *pic, struct pic_port *port, int c) pic_value sym; if (isdigit(peek(pic, port))) { - return negate(read_unsigned(pic, port, next(pic, port))); + return negate(pic, read_unsigned(pic, port, next(pic, port))); } else { sym = read_symbol(pic, port, c); if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "-inf.0")) { - return pic_float_value(-(1.0 / 0.0)); + return pic_float_value(pic, -(1.0 / 0.0)); } if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "-nan.0")) { - return pic_float_value(-(0.0 / 0.0)); + return pic_float_value(pic, -(0.0 / 0.0)); } return sym; } @@ -342,10 +342,10 @@ read_plus(pic_state *pic, struct pic_port *port, int c) else { sym = read_symbol(pic, port, c); if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "+inf.0")) { - return pic_float_value(1.0 / 0.0); + return pic_float_value(pic, 1.0 / 0.0); } if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "+nan.0")) { - return pic_float_value(0.0 / 0.0); + return pic_float_value(pic, 0.0 / 0.0); } return sym; } @@ -356,13 +356,13 @@ read_true(pic_state *pic, struct pic_port *port, int c) { if ((c = peek(pic, port)) == 'r') { if (! expect(pic, port, "rue")) { - read_error(pic, "unexpected character while reading #true", pic_nil_value()); + read_error(pic, "unexpected character while reading #true", pic_nil_value(pic)); } } else if (! isdelim(c)) { - read_error(pic, "non-delimiter character given after #t", pic_list1(pic, pic_char_value(c))); + read_error(pic, "non-delimiter character given after #t", pic_list1(pic, pic_char_value(pic, c))); } - return pic_true_value(); + return pic_true_value(pic); } static pic_value @@ -370,13 +370,13 @@ read_false(pic_state *pic, struct pic_port *port, int c) { if ((c = peek(pic, port)) == 'a') { if (! expect(pic, port, "alse")) { - read_error(pic, "unexpected character while reading #false", pic_nil_value()); + read_error(pic, "unexpected character while reading #false", pic_nil_value(pic)); } } else if (! isdelim(c)) { - read_error(pic, "non-delimiter character given after #f", pic_list1(pic, pic_char_value(c))); + read_error(pic, "non-delimiter character given after #f", pic_list1(pic, pic_char_value(pic, c))); } - return pic_false_value(); + return pic_false_value(pic); } static pic_value @@ -386,7 +386,7 @@ read_char(pic_state *pic, struct pic_port *port, int c) if (! isdelim(peek(pic, port))) { switch (c) { - default: read_error(pic, "unexpected character after char literal", pic_list1(pic, pic_char_value(c))); + default: read_error(pic, "unexpected character after char literal", pic_list1(pic, pic_char_value(pic, c))); 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 'd': c = 0x7F; if (! expect(pic, port, "elete")) goto fail; break; @@ -408,10 +408,10 @@ read_char(pic_state *pic, struct pic_port *port, int c) } } - return pic_char_value((char)c); + return pic_char_value(pic, (char)c); fail: - read_error(pic, "unexpected character while reading character literal", pic_list1(pic, pic_char_value(c))); + read_error(pic, "unexpected character while reading character literal", pic_list1(pic, pic_char_value(pic, c))); } static pic_value @@ -474,7 +474,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) i = 0; while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') { if (i >= sizeof HEX_BUF) - read_error(pic, "expected ';'", pic_list1(pic, pic_char_value(HEX_BUF[sizeof(HEX_BUF) - 1]))); + read_error(pic, "expected ';'", pic_list1(pic, pic_char_value(pic, HEX_BUF[sizeof(HEX_BUF) - 1]))); } c = (char)strtol(HEX_BUF, NULL, 16); break; @@ -508,11 +508,11 @@ read_blob(pic_state *pic, struct pic_port *port, int c) } if (nbits != 8) { - read_error(pic, "unsupported bytevector bit width", pic_list1(pic, pic_int_value(nbits))); + read_error(pic, "unsupported bytevector bit width", pic_list1(pic, pic_int_value(pic, nbits))); } if (c != '(') { - read_error(pic, "expected '(' character", pic_list1(pic, pic_char_value(c))); + read_error(pic, "expected '(' character", pic_list1(pic, pic_char_value(pic, c))); } len = 0; @@ -521,7 +521,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) while ((c = skip(pic, port, c)) != ')') { n = read_uinteger(pic, port, c); if (n < 0 || (1 << nbits) <= n) { - read_error(pic, "invalid element in bytevector literal", pic_list1(pic, pic_int_value(n))); + read_error(pic, "invalid element in bytevector literal", pic_list1(pic, pic_int_value(pic, n))); } len += 1; dat = pic_realloc(pic, dat, len); @@ -543,12 +543,12 @@ read_undef_or_blob(pic_state *pic, struct pic_port *port, int c) { if ((c = peek(pic, port)) == 'n') { if (! expect(pic, port, "ndefined")) { - read_error(pic, "unexpected character while reading #undefined", pic_nil_value()); + read_error(pic, "unexpected character while reading #undefined", pic_nil_value(pic)); } - return pic_undef_value(); + return pic_undef_value(pic); } if (! isdigit(c)) { - read_error(pic, "expect #undefined or #u8(...), but illegal character given", pic_list1(pic, pic_char_value(c))); + read_error(pic, "expect #undefined or #u8(...), but illegal character given", pic_list1(pic, pic_char_value(pic, c))); } return read_blob(pic, port, 'u'); } @@ -564,24 +564,24 @@ read_pair(pic_state *pic, struct pic_port *port, int c) c = skip(pic, port, ' '); if (c == tCLOSE) { - return pic_nil_value(); + return pic_nil_value(pic); } if (c == '.' && isdelim(peek(pic, port))) { cdr = read(pic, port, next(pic, port)); closing: if ((c = skip(pic, port, ' ')) != tCLOSE) { - if (pic_invalid_p(read_nullable(pic, port, c))) { + if (pic_invalid_p(pic, read_nullable(pic, port, c))) { goto closing; } - read_error(pic, "unmatched parenthesis", pic_nil_value()); + read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); } return cdr; } else { car = read_nullable(pic, port, c); - if (pic_invalid_p(car)) { + if (pic_invalid_p(pic, car)) { goto retry; } @@ -623,7 +623,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) { pic_value tmp; - kh_val(h, it) = val = pic_cons(pic, pic_undef_value(), pic_undef_value()); + kh_val(h, it) = val = pic_cons(pic, pic_undef_value(pic), pic_undef_value(pic)); tmp = read(pic, port, c); pic_pair_ptr(val)->car = pic_car(pic, tmp); @@ -672,7 +672,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i) it = kh_get(read, h, i); if (it == kh_end(h)) { - read_error(pic, "label of given index not defined", pic_list1(pic, pic_int_value(i))); + read_error(pic, "label of given index not defined", pic_list1(pic, pic_int_value(pic, i))); } return kh_val(h, it); } @@ -693,13 +693,13 @@ read_label(pic_state *pic, struct pic_port *port, int c) if (c == '#') { return read_label_ref(pic, port, i); } - read_error(pic, "broken label expression", pic_nil_value()); + read_error(pic, "broken label expression", pic_nil_value(pic)); } static pic_value read_unmatch(pic_state *pic, struct pic_port PIC_UNUSED(*port), int PIC_UNUSED(c)) { - read_error(pic, "unmatched parenthesis", pic_nil_value()); + read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); } static pic_value @@ -708,11 +708,11 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c) c = next(pic, port); if (c == EOF) { - read_error(pic, "unexpected EOF", pic_nil_value()); + read_error(pic, "unexpected EOF", pic_nil_value(pic)); } if (pic->reader.dispatch[c] == NULL) { - read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(c))); + read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(pic, c))); } return pic->reader.dispatch[c](pic, port, c); @@ -724,11 +724,11 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) c = skip(pic, port, c); if (c == EOF) { - read_error(pic, "unexpected EOF", pic_nil_value()); + read_error(pic, "unexpected EOF", pic_nil_value(pic)); } if (pic->reader.table[c] == NULL) { - read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(c))); + read_error(pic, "invalid character at the seeker head", pic_list1(pic, pic_char_value(pic, c))); } return pic->reader.table[c](pic, port, c); @@ -742,7 +742,7 @@ read(pic_state *pic, struct pic_port *port, int c) retry: val = read_nullable(pic, port, c); - if (pic_invalid_p(val)) { + if (pic_invalid_p(pic, val)) { c = next(pic, port); goto retry; } @@ -832,7 +832,7 @@ pic_read(pic_state *pic, struct pic_port *port) while ((c = skip(pic, port, next(pic, port))) != EOF) { val = read_nullable(pic, port, c); - if (! pic_invalid_p(val)) { + if (! pic_invalid_p(pic, val)) { break; } pic_gc_arena_restore(pic, ai); diff --git a/extlib/benz/record.c b/extlib/benz/record.c index 301b9a12..ded83ab2 100644 --- a/extlib/benz/record.c +++ b/extlib/benz/record.c @@ -45,7 +45,7 @@ pic_rec_record_p(pic_state *pic) pic_get_args(pic, "o", &rec); - return pic_bool_value(pic_rec_p(rec)); + return pic_bool_value(pic, pic_rec_p(pic, rec)); } static pic_value diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 72c0604c..917bd59f 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -265,7 +265,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->macros = NULL; /* features */ - pic->features = pic_nil_value(); + pic->features = pic_nil_value(pic); /* libraries */ kh_init(ltable, &pic->ltable); @@ -282,7 +282,7 @@ pic_open(pic_allocf allocf, void *userdata) memset(pic->files, 0, sizeof pic->files); /* parameter table */ - pic->ptable = pic_nil_value(); + pic->ptable = pic_nil_value(pic); /* native stack marker */ pic->native_stack_start = &t; @@ -385,7 +385,7 @@ pic_close(pic_state *pic) pic->err = pic_invalid_value(); pic->globals = NULL; pic->macros = NULL; - pic->features = pic_nil_value(); + pic->features = pic_nil_value(pic); /* free all libraries */ kh_clear(ltable, &pic->ltable); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 0a836f24..bd0ac8b4 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -253,7 +253,7 @@ pic_make_str(pic_state *pic, const char *str, int len) } int -pic_str_len(struct pic_string *str) +pic_str_len(pic_state PIC_UNUSED(*pic), struct pic_string *str) { return rope_len(str->rope); } @@ -408,7 +408,7 @@ pic_str_string_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_str_p(v)); + return pic_bool_value(pic, pic_str_p(pic, v)); } static pic_value @@ -425,7 +425,7 @@ pic_str_string(pic_state *pic) for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], char); - buf[i] = pic_char(argv[i]); + buf[i] = pic_char(pic, argv[i]); } str = pic_make_str(pic, buf, argc); @@ -460,7 +460,7 @@ pic_str_string_length(pic_state *pic) pic_get_args(pic, "s", &str); - return pic_int_value(pic_str_len(str)); + return pic_int_value(pic, pic_str_len(pic, str)); } static pic_value @@ -471,7 +471,7 @@ pic_str_string_ref(pic_state *pic) pic_get_args(pic, "si", &str, &k); - return pic_char_value(pic_str_ref(pic, str, k)); + return pic_char_value(pic, pic_str_ref(pic, str, k)); } #define DEFINE_STRING_CMP(name, op) \ @@ -483,19 +483,19 @@ pic_str_string_ref(pic_state *pic) \ pic_get_args(pic, "*", &argc, &argv); \ \ - if (argc < 1 || ! pic_str_p(argv[0])) { \ - return pic_false_value(); \ + if (argc < 1 || ! pic_str_p(pic, argv[0])) { \ + return pic_false_value(pic); \ } \ \ for (i = 1; i < argc; ++i) { \ - if (! pic_str_p(argv[i])) { \ - return pic_false_value(); \ + if (! pic_str_p(pic, argv[i])) { \ + return pic_false_value(pic); \ } \ if (! (pic_str_cmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \ - return pic_false_value(); \ + return pic_false_value(pic); \ } \ } \ - return pic_true_value(); \ + return pic_true_value(pic); \ } DEFINE_STRING_CMP(eq, ==) @@ -512,7 +512,7 @@ pic_str_string_copy(pic_state *pic) n = pic_get_args(pic, "s|ii", &str, &start, &end); - len = pic_str_len(str); + len = pic_str_len(pic, str); switch (n) { case 1: @@ -538,7 +538,7 @@ pic_str_string_append(pic_state *pic) str = pic_make_lit(pic, ""); for (i = 0; i < argc; ++i) { - if (! pic_str_p(argv[i])) { + if (! pic_str_p(pic, argv[i])) { pic_errorf(pic, "type error"); } str = pic_str_cat(pic, str, pic_str_ptr(argv[i])); @@ -561,27 +561,27 @@ pic_str_string_map(pic_state *pic) pic_errorf(pic, "string-map: one or more strings expected, but got zero"); } else { pic_assert_type(pic, argv[0], str); - len = pic_str_len(pic_str_ptr(argv[0])); + len = pic_str_len(pic, pic_str_ptr(argv[0])); } for (i = 1; i < argc; ++i) { pic_assert_type(pic, argv[i], str); - len = len < pic_str_len(pic_str_ptr(argv[i])) + len = len < pic_str_len(pic, pic_str_ptr(argv[i])) ? len - : pic_str_len(pic_str_ptr(argv[i])); + : pic_str_len(pic, pic_str_ptr(argv[i])); } buf = pic_malloc(pic, len); pic_try { for (i = 0; i < len; ++i) { - vals = pic_nil_value(); + vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { - pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); + pic_push(pic, pic_char_value(pic, pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } val = pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); pic_assert_type(pic, val, char); - buf[i] = pic_char(val); + buf[i] = pic_char(pic, val); } str = pic_make_str(pic, buf, len); } @@ -608,25 +608,25 @@ pic_str_string_for_each(pic_state *pic) pic_errorf(pic, "string-map: one or more strings expected, but got zero"); } else { pic_assert_type(pic, argv[0], str); - len = pic_str_len(pic_str_ptr(argv[0])); + len = pic_str_len(pic, pic_str_ptr(argv[0])); } for (i = 1; i < argc; ++i) { pic_assert_type(pic, argv[i], str); - len = len < pic_str_len(pic_str_ptr(argv[i])) + len = len < pic_str_len(pic, pic_str_ptr(argv[i])) ? len - : pic_str_len(pic_str_ptr(argv[i])); + : pic_str_len(pic, pic_str_ptr(argv[i])); } for (i = 0; i < len; ++i) { - vals = pic_nil_value(); + vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { - pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); + pic_push(pic, pic_char_value(pic, pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -650,7 +650,7 @@ pic_str_list_to_string(pic_state *pic) pic_for_each (e, list, it) { pic_assert_type(pic, e, char); - buf[i++] = pic_char(e); + buf[i++] = pic_char(pic, e); } str = pic_make_str(pic, buf, i); @@ -677,13 +677,13 @@ pic_str_string_to_list(pic_state *pic) case 1: start = 0; case 2: - end = pic_str_len(str); + end = pic_str_len(pic, str); } - list = pic_nil_value(); + list = pic_nil_value(pic); for (i = start; i < end; ++i) { - pic_push(pic, pic_char_value(pic_str_ref(pic, str, i)), list); + pic_push(pic, pic_char_value(pic, pic_str_ref(pic, str, i)), list); } return pic_reverse(pic, list); } diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index c7ee0969..0e185dec 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -53,7 +53,7 @@ pic_symbol_name(pic_state *pic, pic_sym *sym) const char * pic_identifier_name(pic_state *pic, pic_id *id) { - while (! pic_sym_p(pic_obj_value(id))) { + while (! pic_sym_p(pic, pic_obj_value(id))) { id = id->u.id.id; } @@ -67,7 +67,7 @@ pic_symbol_symbol_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_sym_p(v)); + return pic_bool_value(pic, pic_sym_p(pic, v)); } static pic_value @@ -79,14 +79,14 @@ pic_symbol_symbol_eq_p(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); for (i = 0; i < argc; ++i) { - if (! pic_sym_p(argv[i])) { - return pic_false_value(); + if (! pic_sym_p(pic, argv[i])) { + return pic_false_value(pic); } - if (! pic_eq_p(argv[i], argv[0])) { - return pic_false_value(); + if (! pic_eq_p(pic, argv[i], argv[0])) { + return pic_false_value(pic); } } - return pic_true_value(); + return pic_true_value(pic); } static pic_value @@ -116,7 +116,7 @@ pic_symbol_identifier_p(pic_state *pic) pic_get_args(pic, "o", &obj); - return pic_bool_value(pic_id_p(obj)); + return pic_bool_value(pic, pic_id_p(pic, obj)); } static pic_value @@ -141,7 +141,7 @@ pic_symbol_identifier_variable(pic_state *pic) pic_assert_type(pic, id, id); - if (pic_sym_p(id)) { + if (pic_sym_p(pic, id)) { pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); } @@ -157,7 +157,7 @@ pic_symbol_identifier_environment(pic_state *pic) pic_assert_type(pic, id, id); - if (pic_sym_p(id)) { + if (pic_sym_p(pic, id)) { pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); } @@ -173,14 +173,14 @@ pic_symbol_identifier_eq_p(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); for (i = 0; i < argc; ++i) { - if (! pic_id_p(argv[i])) { - return pic_false_value(); + if (! pic_id_p(pic, argv[i])) { + return pic_false_value(pic); } if (! pic_equal_p(pic, argv[i], argv[0])) { - return pic_false_value(); + return pic_false_value(pic); } } - return pic_true_value(); + return pic_true_value(pic); } void diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 77e6c233..1965db92 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -28,7 +28,7 @@ var_set(pic_state *pic, struct pic_proc *var, pic_value val) pic_weak_set(pic, weak, var, val); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -46,7 +46,7 @@ var_call(pic_state *pic) pic_value conv; conv = pic_closure_ref(pic, 0); - if (! pic_false_p(conv)) { + if (! pic_false_p(pic, conv)) { val = pic_call(pic, pic_proc_ptr(conv), 1, val); } return var_set(pic, self, val); @@ -57,7 +57,7 @@ struct pic_proc * pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv) { struct pic_proc *var; - pic_value c = pic_false_value(); + pic_value c = pic_false_value(pic); if (conv != NULL) { c = pic_obj_value(conv); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 4e986ae3..af273b9b 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -14,7 +14,7 @@ pic_make_vec(pic_state *pic, int len) vec->len = len; vec->data = (pic_value *)pic_malloc(pic, sizeof(pic_value) * len); for (i = 0; i < len; ++i) { - vec->data[i] = pic_undef_value(); + vec->data[i] = pic_undef_value(pic); } return vec; } @@ -26,7 +26,7 @@ pic_vec_vector_p(pic_state *pic) pic_get_args(pic, "o", &v); - return pic_bool_value(pic_vec_p(v)); + return pic_bool_value(pic, pic_vec_p(pic, v)); } static pic_value @@ -72,7 +72,7 @@ pic_vec_vector_length(pic_state *pic) pic_get_args(pic, "v", &v); - return pic_int_value(v->len); + return pic_int_value(pic, v->len); } static pic_value @@ -102,7 +102,7 @@ pic_vec_vector_set(pic_state *pic) pic_errorf(pic, "vector-set!: index out of range"); } v->data[k] = o; - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -126,14 +126,14 @@ pic_vec_vector_copy_i(pic_state *pic) while (start < end) { to->data[--at] = from->data[--end]; } - return pic_undef_value(); + return pic_undef_value(pic); } while (start < end) { to->data[at++] = from->data[start++]; } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -211,7 +211,7 @@ pic_vec_vector_fill_i(pic_state *pic) vec->data[start++] = obj; } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -236,7 +236,7 @@ pic_vec_vector_map(pic_state *pic) vec = pic_make_vec(pic, len); for (i = 0; i < len; ++i) { - vals = pic_nil_value(); + vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } @@ -265,14 +265,14 @@ pic_vec_vector_for_each(pic_state *pic) } for (i = 0; i < len; ++i) { - vals = pic_nil_value(); + vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -309,7 +309,7 @@ pic_vec_vector_to_list(pic_state *pic) end = vec->len; } - list = pic_nil_value(); + list = pic_nil_value(pic); for (i = start; i < end; ++i) { pic_push(pic, vec->data[i], list); @@ -343,7 +343,7 @@ pic_vec_vector_to_string(pic_state *pic) for (i = start; i < end; ++i) { pic_assert_type(pic, vec->data[i], char); - buf[i - start] = pic_char(vec->data[i]); + buf[i - start] = pic_char(pic, vec->data[i]); } str = pic_make_str(pic, buf, end - start); @@ -365,7 +365,7 @@ pic_vec_string_to_vector(pic_state *pic) case 1: start = 0; case 2: - end = pic_str_len(str); + end = pic_str_len(pic, str); } if (end < start) { @@ -375,7 +375,7 @@ pic_vec_string_to_vector(pic_state *pic) vec = pic_make_vec(pic, end - start); for (i = 0; i < end - start; ++i) { - vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + start)); + vec->data[i] = pic_char_value(pic, pic_str_ref(pic, str, i + start)); } return pic_obj_value(vec); } diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 635bd260..6dda9cd8 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -38,7 +38,7 @@ pic_weak_rev_ref(pic_state *pic, struct pic_weak *weak, pic_value val) if (h->n_buckets) { khint_t i = 0; - while ((i < h->n_buckets) && (ac_iseither(h->flags, i) || !pic_eq_p(h->vals[i], val))) { + while ((i < h->n_buckets) && (ac_iseither(h->flags, i) || !pic_eq_p(pic, h->vals[i], val))) { i += 1; } if (i < h->n_buckets) return kh_key(h, i); @@ -82,7 +82,7 @@ static pic_value weak_get(pic_state *pic, struct pic_weak *weak, void *key) { if (! pic_weak_has(pic, weak, key)) { - return pic_false_value(); + return pic_false_value(pic); } return pic_cons(pic, pic_obj_value(key), pic_weak_ref(pic, weak, key)); } @@ -90,7 +90,7 @@ weak_get(pic_state *pic, struct pic_weak *weak, void *key) static pic_value weak_set(pic_state *pic, struct pic_weak *weak, void *key, pic_value val) { - if (pic_undef_p(val)) { + if (pic_undef_p(pic, val)) { if (pic_weak_has(pic, weak, key)) { pic_weak_del(pic, weak, key); } @@ -98,7 +98,7 @@ weak_set(pic_state *pic, struct pic_weak *weak, void *key, pic_value val) pic_weak_set(pic, weak, key, val); } - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -111,7 +111,7 @@ weak_call(pic_state *pic) n = pic_get_args(pic, "&o|o", &self, &key, &val); - if (! pic_obj_p(key)) { + if (! pic_obj_p(pic, key)) { pic_errorf(pic, "attempted to set a non-object key '~s' in an ephemeron", key); } diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 0c86fcb8..4cd4da89 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -92,7 +92,7 @@ write_str(pic_state *pic, struct pic_string *str, xFILE *file, int mode) return; } xfprintf(pic, file, "\""); - for (i = 0; i < pic_str_len(str); ++i) { + for (i = 0; i < pic_str_len(pic, str); ++i) { if (cstr[i] == '"' || cstr[i] == '\\') { xfputc(pic, '\\', file); } @@ -128,10 +128,10 @@ write_pair_help(struct writer_control *p, struct pic_pair *pair) write_core(p, pair->car); - if (pic_nil_p(pair->cdr)) { + if (pic_nil_p(pic, pair->cdr)) { return; } - else if (pic_pair_p(pair->cdr)) { + else if (pic_pair_p(pic, pair->cdr)) { /* shared objects */ if ((it = kh_get(l, lh, pic_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { @@ -171,7 +171,7 @@ write_pair(struct writer_control *p, struct pic_pair *pair) xFILE *file = p->file; pic_sym *tag; - if (pic_pair_p(pair->cdr) && pic_nil_p(pic_cdr(pic, pair->cdr)) && pic_sym_p(pair->car)) { + if (pic_pair_p(pic, pair->cdr) && pic_nil_p(pic, pic_cdr(pic, pair->cdr)) && pic_sym_p(pic, pair->car)) { tag = pic_sym_ptr(pair->car); if (tag == pic->sQUOTE) { xfprintf(pic, file, "'"); @@ -263,7 +263,7 @@ write_core(struct writer_control *p, pic_value obj) int ret; /* shared objects */ - if (pic_vtype(obj) == PIC_VTYPE_HEAP && ((it = kh_get(l, lh, pic_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { + if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { kh_put(v, vh, pic_ptr(obj), &ret); if (ret == 0) { /* if exists */ xfprintf(pic, file, "#%d#", kh_val(lh, it)); @@ -272,7 +272,7 @@ write_core(struct writer_control *p, pic_value obj) xfprintf(pic, file, "#%d=", kh_val(lh, it)); } - switch (pic_type(obj)) { + switch (pic_type(pic, obj)) { case PIC_TT_UNDEF: xfprintf(pic, file, "#undefined"); break; @@ -280,7 +280,7 @@ write_core(struct writer_control *p, pic_value obj) xfprintf(pic, file, "()"); break; case PIC_TT_BOOL: - xfprintf(pic, file, pic_true_p(obj) ? "#t" : "#f"); + xfprintf(pic, file, pic_true_p(pic, obj) ? "#t" : "#f"); break; case PIC_TT_ID: xfprintf(pic, file, "#", pic_identifier_name(pic, pic_id_ptr(obj))); @@ -289,10 +289,10 @@ write_core(struct writer_control *p, pic_value obj) xfprintf(pic, file, "#.(eof-object)"); break; case PIC_TT_INT: - xfprintf(pic, file, "%d", pic_int(obj)); + xfprintf(pic, file, "%d", pic_int(pic, obj)); break; case PIC_TT_FLOAT: - write_float(pic, pic_float(obj), file); + write_float(pic, pic_float(pic, obj), file); break; case PIC_TT_SYMBOL: xfprintf(pic, file, "%s", pic_symbol_name(pic, pic_sym_ptr(obj))); @@ -301,7 +301,7 @@ write_core(struct writer_control *p, pic_value obj) write_blob(pic, pic_blob_ptr(obj), file); break; case PIC_TT_CHAR: - write_char(pic, pic_char(obj), file, p->mode); + write_char(pic, pic_char(pic, obj), file, p->mode); break; case PIC_TT_STRING: write_str(pic, pic_str_ptr(obj), file, p->mode); @@ -316,12 +316,12 @@ write_core(struct writer_control *p, pic_value obj) write_dict(p, pic_dict_ptr(obj)); break; default: - xfprintf(pic, file, "#<%s %p>", pic_type_repr(pic_type(obj)), pic_ptr(obj)); + xfprintf(pic, file, "#<%s %p>", pic_type_repr(pic, pic_type(pic, obj)), pic_ptr(obj)); break; } if (p->op == OP_WRITE) { - if (pic_obj_p(obj) && ((it = kh_get(l, lh, pic_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { + if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { it = kh_get(v, vh, pic_ptr(obj)); kh_del(v, vh, it); } @@ -337,7 +337,7 @@ traverse(struct writer_control *p, pic_value obj) return; } - switch (pic_type(obj)) { + switch (pic_type(pic, obj)) { case PIC_TT_PAIR: case PIC_TT_VECTOR: case PIC_TT_DICT: { @@ -350,11 +350,11 @@ traverse(struct writer_control *p, pic_value obj) /* first time */ kh_val(h, it) = -1; - if (pic_pair_p(obj)) { + if (pic_pair_p(pic, obj)) { /* pair */ traverse(p, pic_car(pic, obj)); traverse(p, pic_cdr(pic, obj)); - } else if (pic_vec_p(obj)) { + } else if (pic_vec_p(pic, obj)) { /* vector */ int i; for (i = 0; i < pic_vec_ptr(obj)->len; ++i) { @@ -453,7 +453,7 @@ pic_write_write(pic_state *pic) pic_get_args(pic, "o|p", &v, &port); write(pic, v, port->file, WRITE_MODE, OP_WRITE); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -464,7 +464,7 @@ pic_write_write_simple(pic_state *pic) pic_get_args(pic, "o|p", &v, &port); write(pic, v, port->file, WRITE_MODE, OP_WRITE_SIMPLE); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -475,7 +475,7 @@ pic_write_write_shared(pic_state *pic) pic_get_args(pic, "o|p", &v, &port); write(pic, v, port->file, WRITE_MODE, OP_WRITE_SHARED); - return pic_undef_value(); + return pic_undef_value(pic); } static pic_value @@ -486,7 +486,7 @@ pic_write_display(pic_state *pic) pic_get_args(pic, "o|p", &v, &port); write(pic, v, port->file, DISPLAY_MODE, OP_WRITE); - return pic_undef_value(); + return pic_undef_value(pic); } void From 08652df612a77aa51be058c382bee7585c59ff5f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 23:25:45 +0900 Subject: [PATCH 023/119] don't include type.h at the beginning of picrin.h --- contrib/20.r7rs/src/system.c | 8 +- contrib/30.readline/src/readline.c | 2 +- contrib/40.srfi/src/106.c | 2 +- extlib/benz/blob.c | 2 +- extlib/benz/bool.c | 66 +++++- extlib/benz/cont.c | 2 +- extlib/benz/data.c | 2 +- extlib/benz/dict.c | 2 +- extlib/benz/error.c | 2 +- extlib/benz/eval.c | 25 +- extlib/benz/gc.c | 106 ++++----- extlib/benz/include/picrin.h | 117 +++++++--- extlib/benz/include/picrin/blob.h | 2 +- extlib/benz/include/picrin/data.h | 2 +- extlib/benz/include/picrin/dict.h | 2 +- extlib/benz/include/picrin/error.h | 4 +- extlib/benz/include/picrin/macro.h | 4 +- extlib/benz/include/picrin/pair.h | 2 +- extlib/benz/include/picrin/port.h | 4 +- extlib/benz/include/picrin/proc.h | 6 +- extlib/benz/include/picrin/record.h | 4 +- extlib/benz/include/picrin/setup.h | 4 +- extlib/benz/include/picrin/string.h | 2 +- extlib/benz/include/picrin/symbol.h | 6 +- extlib/benz/include/picrin/type.h | 344 ++++++---------------------- extlib/benz/include/picrin/vector.h | 2 +- extlib/benz/include/picrin/weak.h | 2 +- extlib/benz/macro.c | 10 +- extlib/benz/pair.c | 2 +- extlib/benz/port.c | 40 ++-- extlib/benz/proc.c | 23 +- extlib/benz/read.c | 2 +- extlib/benz/record.c | 2 +- extlib/benz/state.c | 2 +- extlib/benz/string.c | 2 +- extlib/benz/symbol.c | 4 +- extlib/benz/value.c | 74 ++++++ extlib/benz/vector.c | 2 +- extlib/benz/weak.c | 2 +- extlib/benz/write.c | 61 ++--- 40 files changed, 470 insertions(+), 482 deletions(-) create mode 100644 extlib/benz/value.c diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 26ffbfe1..d53169aa 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -37,10 +37,10 @@ pic_system_exit(pic_state *pic) argc = pic_get_args(pic, "|o", &v); if (argc == 1) { switch (pic_type(pic, v)) { - case PIC_TT_FLOAT: + case PIC_TYPE_FLOAT: status = (int)pic_float(pic, v); break; - case PIC_TT_INT: + case PIC_TYPE_INT: status = pic_int(pic, v); break; default: @@ -62,10 +62,10 @@ pic_system_emergency_exit(pic_state *pic) argc = pic_get_args(pic, "|o", &v); if (argc == 1) { switch (pic_type(pic, v)) { - case PIC_TT_FLOAT: + case PIC_TYPE_FLOAT: status = (int)pic_float(pic, v); break; - case PIC_TT_INT: + case PIC_TYPE_INT: status = pic_int(pic, v); break; default: diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index a7542af5..50c77163 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -21,7 +21,7 @@ pic_rl_readline(pic_state *pic) if(result) return pic_obj_value(pic_make_cstr(pic, result)); else - return pic_eof_object(); + return pic_eof_object(pic); } static pic_value diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index b1aac0ce..f88504de 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -340,7 +340,7 @@ make_socket_port(pic_state *pic, struct pic_socket_t *sock, short dir) { struct pic_port *port; - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = xfunopen(pic, sock, xf_socket_read, xf_socket_write, xf_socket_seek, xf_socket_close); port->flags = dir | PIC_PORT_BINARY | PIC_PORT_OPEN; return port; diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index fc464aa0..9d1161f0 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -9,7 +9,7 @@ pic_make_blob(pic_state *pic, int len) { struct pic_blob *bv; - bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TT_BLOB); + bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TYPE_BLOB); bv->data = pic_malloc(pic, len); bv->len = len; return bv; diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index cf72b27c..ff7d4a31 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -4,6 +4,60 @@ #include "picrin.h" +#if PIC_NAN_BOXING + +bool +pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +{ + return x == y; +} + +bool +pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +{ + return x == y; +} + +#else + +bool +pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +{ + if (pic_type(pic, x) != pic_type(pic, y)) + return false; + + switch (pic_type(pic, x)) { + case PIC_TYPE_NIL: + return true; + case PIC_TYPE_TRUE: case PIC_TYPE_FALSE: + return pic_type(pic, x) == pic_type(pic, y); + default: + return pic_obj_ptr(x) == pic_obj_ptr(y); + } +} + +bool +pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +{ + if (pic_type(pic, x) != pic_type(pic, y)) + return false; + + switch (pic_type(pic, x)) { + case PIC_TYPE_NIL: + return true; + case PIC_TYPE_TRUE: case PIC_TYPE_FALSE: + return pic_type(pic, x) == pic_type(pic, y); + case PIC_TYPE_FLOAT: + return pic_float(pic, x) == pic_float(pic, y); + case PIC_TYPE_INT: + return pic_int(pic, x) == pic_int(pic, y); + default: + return pic_obj_ptr(x) == pic_obj_ptr(y); + } +} + +#endif + KHASH_DECLARE(m, void *, int) KHASH_DEFINE2(m, void *, int, 0, kh_ptr_hash_func, kh_ptr_hash_equal) @@ -38,7 +92,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) } switch (pic_type(pic, x)) { - case PIC_TT_ID: { + case PIC_TYPE_ID: { struct pic_id *id1, *id2; pic_sym *s1, *s2; @@ -50,10 +104,10 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) return s1 == s2; } - case PIC_TT_STRING: { + case PIC_TYPE_STRING: { return pic_str_cmp(pic, pic_str_ptr(x), pic_str_ptr(y)) == 0; } - case PIC_TT_BLOB: { + case PIC_TYPE_BLOB: { struct pic_blob *blob1, *blob2; int i; @@ -69,7 +123,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) } return true; } - case PIC_TT_PAIR: { + case PIC_TYPE_PAIR: { if (! internal_equal_p(pic, pic_car(pic, x), pic_car(pic, y), depth + 1, h)) return false; @@ -102,7 +156,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) } goto LOOP; /* tail-call optimization */ } - case PIC_TT_VECTOR: { + case PIC_TYPE_VECTOR: { int i; struct pic_vector *u, *v; @@ -118,7 +172,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) } return true; } - case PIC_TT_DATA: { + case PIC_TYPE_DATA: { return pic_data_ptr(x)->data == pic_data_ptr(y)->data; } default: diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 4ee65e09..8dac1bcd 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -31,7 +31,7 @@ pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, st } here = pic->cp; - pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TT_CP); + pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TYPE_CP); pic->cp->prev = here; pic->cp->depth = here->depth + 1; pic->cp->in = in; diff --git a/extlib/benz/data.c b/extlib/benz/data.c index 0df3ab06..a570b6be 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -5,7 +5,7 @@ pic_data_alloc(pic_state *pic, const pic_data_type *type, void *userdata) { struct pic_data *data; - data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TT_DATA); + data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TYPE_DATA); data->type = type; data->data = userdata; diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 3583d5e3..39dcacf6 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -11,7 +11,7 @@ pic_make_dict(pic_state *pic) { struct pic_dict *dict; - dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TT_DICT); + dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TYPE_DICT); kh_init(dict, &dict->hash); return dict; diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 306b69c2..b416ed18 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -98,7 +98,7 @@ pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs) stack = pic_get_backtrace(pic); - e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TT_ERROR); + e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); e->type = type; e->msg = pic_make_cstr(pic, msg); e->irrs = irrs; diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index a87c8f75..371651c6 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -293,10 +293,10 @@ static pic_value analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) { switch (pic_type(pic, obj)) { - case PIC_TT_SYMBOL: { + case PIC_TYPE_SYMBOL: { return analyze_var(pic, scope, pic_sym_ptr(obj)); } - case PIC_TT_PAIR: { + case PIC_TYPE_PAIR: { pic_value proc; if (! pic_list_p(pic, obj)) { @@ -694,38 +694,41 @@ codegen_quote(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) obj = pic_list_ref(pic, obj, 1); switch (pic_type(pic, obj)) { - case PIC_TT_UNDEF: + case PIC_TYPE_UNDEF: emit_n(pic, cxt, OP_PUSHUNDEF); break; - case PIC_TT_BOOL: - emit_n(pic, cxt, (pic_true_p(pic, obj) ? OP_PUSHTRUE : OP_PUSHFALSE)); + case PIC_TYPE_TRUE: + emit_n(pic, cxt, OP_PUSHTRUE); break; - case PIC_TT_INT: + case PIC_TYPE_FALSE: + emit_n(pic, cxt, OP_PUSHFALSE); + break; + case PIC_TYPE_INT: check_ints_size(pic, cxt); pidx = (int)cxt->klen++; cxt->ints[pidx] = pic_int(pic, obj); emit_i(pic, cxt, OP_PUSHINT, pidx); break; - case PIC_TT_FLOAT: + case PIC_TYPE_FLOAT: check_nums_size(pic, cxt); pidx = (int)cxt->flen++; cxt->nums[pidx] = pic_float(pic, obj); emit_i(pic, cxt, OP_PUSHFLOAT, pidx); break; - case PIC_TT_NIL: + case PIC_TYPE_NIL: emit_n(pic, cxt, OP_PUSHNIL); break; - case PIC_TT_EOF: + case PIC_TYPE_EOF: emit_n(pic, cxt, OP_PUSHEOF); break; - case PIC_TT_CHAR: + case PIC_TYPE_CHAR: check_ints_size(pic, cxt); pidx = (int)cxt->klen++; cxt->ints[pidx] = pic_char(pic, obj); emit_i(pic, cxt, OP_PUSHCHAR, pidx); break; default: - assert(pic_obj_p(obj)); + assert(pic_obj_p(pic,obj)); check_pool_size(pic, cxt); pidx = (int)cxt->plen++; cxt->pool[pidx] = pic_obj_ptr(obj); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index d91f6d31..e0c53e81 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -277,14 +277,14 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) #define LOOP(o) obj = (struct pic_object *)(o); goto loop switch (obj->u.basic.tt) { - case PIC_TT_PAIR: { + case PIC_TYPE_PAIR: { gc_mark(pic, obj->u.pair.car); if (pic_obj_p(pic, obj->u.pair.cdr)) { LOOP(pic_obj_ptr(obj->u.pair.cdr)); } break; } - case PIC_TT_CXT: { + case PIC_TYPE_CXT: { int i; for (i = 0; i < obj->u.cxt.regc; ++i) { @@ -295,7 +295,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } break; } - case PIC_TT_PROC: { + case PIC_TYPE_PROC: { if (pic_proc_irep_p(&obj->u.proc)) { if (obj->u.proc.u.i.cxt) { LOOP(obj->u.proc.u.i.cxt); @@ -308,35 +308,35 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } break; } - case PIC_TT_PORT: { + case PIC_TYPE_PORT: { break; } - case PIC_TT_ERROR: { + case PIC_TYPE_ERROR: { gc_mark_object(pic, (struct pic_object *)obj->u.err.type); gc_mark_object(pic, (struct pic_object *)obj->u.err.msg); gc_mark(pic, obj->u.err.irrs); LOOP(obj->u.err.stack); break; } - case PIC_TT_STRING: { + case PIC_TYPE_STRING: { break; } - case PIC_TT_VECTOR: { + case PIC_TYPE_VECTOR: { int i; for (i = 0; i < obj->u.vec.len; ++i) { gc_mark(pic, obj->u.vec.data[i]); } break; } - case PIC_TT_BLOB: { + case PIC_TYPE_BLOB: { break; } - case PIC_TT_ID: { + case PIC_TYPE_ID: { gc_mark_object(pic, (struct pic_object *)obj->u.id.u.id.id); LOOP(obj->u.id.u.id.env); break; } - case PIC_TT_ENV: { + case PIC_TYPE_ENV: { khash_t(env) *h = &obj->u.env.map; khiter_t it; @@ -351,13 +351,13 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } break; } - case PIC_TT_DATA: { + case PIC_TYPE_DATA: { if (obj->u.data.type->mark) { obj->u.data.type->mark(pic, obj->u.data.data, gc_mark); } break; } - case PIC_TT_DICT: { + case PIC_TYPE_DICT: { pic_sym *sym; khiter_t it; @@ -367,25 +367,25 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } break; } - case PIC_TT_RECORD: { + case PIC_TYPE_RECORD: { gc_mark(pic, obj->u.rec.type); if (pic_obj_p(pic, obj->u.rec.datum)) { LOOP(pic_obj_ptr(obj->u.rec.datum)); } break; } - case PIC_TT_SYMBOL: { + case PIC_TYPE_SYMBOL: { LOOP(obj->u.sym.str); break; } - case PIC_TT_WEAK: { + case PIC_TYPE_WEAK: { struct pic_weak *weak = (struct pic_weak *)obj; weak->prev = pic->heap->weaks; pic->heap->weaks = weak; break; } - case PIC_TT_CP: { + case PIC_TYPE_CP: { if (obj->u.cp.prev) { gc_mark_object(pic, (struct pic_object *)obj->u.cp.prev); } @@ -397,14 +397,15 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } break; } - case PIC_TT_NIL: - case PIC_TT_BOOL: - case PIC_TT_FLOAT: - case PIC_TT_INT: - case PIC_TT_CHAR: - case PIC_TT_EOF: - case PIC_TT_UNDEF: - case PIC_TT_INVALID: + case PIC_TYPE_NIL: + case PIC_TYPE_TRUE: + case PIC_TYPE_FALSE: + case PIC_TYPE_FLOAT: + case PIC_TYPE_INT: + case PIC_TYPE_CHAR: + case PIC_TYPE_EOF: + case PIC_TYPE_UNDEF: + case PIC_TYPE_INVALID: pic_panic(pic, "logic flaw"); } } @@ -532,64 +533,65 @@ static void gc_finalize_object(pic_state *pic, struct pic_object *obj) { switch (obj->u.basic.tt) { - case PIC_TT_VECTOR: { + case PIC_TYPE_VECTOR: { pic_free(pic, obj->u.vec.data); break; } - case PIC_TT_BLOB: { + case PIC_TYPE_BLOB: { pic_free(pic, obj->u.blob.data); break; } - case PIC_TT_STRING: { + case PIC_TYPE_STRING: { pic_rope_decref(pic, obj->u.str.rope); break; } - case PIC_TT_ENV: { + case PIC_TYPE_ENV: { kh_destroy(env, &obj->u.env.map); break; } - case PIC_TT_DATA: { + case PIC_TYPE_DATA: { if (obj->u.data.type->dtor) { obj->u.data.type->dtor(pic, obj->u.data.data); } break; } - case PIC_TT_DICT: { + case PIC_TYPE_DICT: { kh_destroy(dict, &obj->u.dict.hash); break; } - case PIC_TT_SYMBOL: { + case PIC_TYPE_SYMBOL: { /* TODO: remove this symbol's entry from pic->syms immediately */ break; } - case PIC_TT_WEAK: { + case PIC_TYPE_WEAK: { kh_destroy(weak, &obj->u.weak.hash); break; } - case PIC_TT_PROC: { + case PIC_TYPE_PROC: { if (pic_proc_irep_p(&obj->u.proc)) { pic_irep_decref(pic, obj->u.proc.u.i.irep); } break; } - case PIC_TT_PAIR: - case PIC_TT_CXT: - case PIC_TT_PORT: - case PIC_TT_ERROR: - case PIC_TT_ID: - case PIC_TT_RECORD: - case PIC_TT_CP: + case PIC_TYPE_PAIR: + case PIC_TYPE_CXT: + case PIC_TYPE_PORT: + case PIC_TYPE_ERROR: + case PIC_TYPE_ID: + case PIC_TYPE_RECORD: + case PIC_TYPE_CP: break; - case PIC_TT_NIL: - case PIC_TT_BOOL: - case PIC_TT_FLOAT: - case PIC_TT_INT: - case PIC_TT_CHAR: - case PIC_TT_EOF: - case PIC_TT_UNDEF: - case PIC_TT_INVALID: + case PIC_TYPE_NIL: + case PIC_TYPE_TRUE: + case PIC_TYPE_FALSE: + case PIC_TYPE_FLOAT: + case PIC_TYPE_INT: + case PIC_TYPE_CHAR: + case PIC_TYPE_EOF: + case PIC_TYPE_UNDEF: + case PIC_TYPE_INVALID: pic_panic(pic, "logic flaw"); } } @@ -704,7 +706,7 @@ pic_alloca(pic_state *pic, size_t n) } struct pic_object * -pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt) +pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) { struct pic_object *obj; @@ -724,17 +726,17 @@ pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt) } } obj->u.basic.gc_mark = PIC_GC_UNMARK; - obj->u.basic.tt = tt; + obj->u.basic.tt = type; return obj; } struct pic_object * -pic_obj_alloc(pic_state *pic, size_t size, enum pic_tt tt) +pic_obj_alloc(pic_state *pic, size_t size, int type) { struct pic_object *obj; - obj = pic_obj_alloc_unsafe(pic, size, tt); + obj = pic_obj_alloc_unsafe(pic, size, type); gc_protect(pic, obj); return obj; diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index bdd3649b..6515bac7 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -36,7 +36,36 @@ extern "C" { typedef struct pic_state pic_state; -#include "picrin/type.h" +#if PIC_NAN_BOXING +# include +typedef uint64_t pic_value; +#else +typedef struct { + unsigned char type; + union { + void *data; + double f; + int i; + char c; + } u; +} pic_value; +#endif + +struct pic_object; +struct pic_symbol; +struct pic_pair; +struct pic_string; +struct pic_vector; +struct pic_blob; +struct pic_proc; +struct pic_port; +struct pic_error; +struct pic_env; + +typedef struct pic_symbol pic_sym; +typedef struct pic_id pic_id; +typedef struct pic_pair pic_pair; +typedef struct pic_vector pic_vec; typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); @@ -87,13 +116,54 @@ pic_value pic_vcall(pic_state *, struct pic_proc *proc, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *proc, int n, pic_value *argv); pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv); -int pic_int(pic_state *, pic_value); -double pic_float(pic_state *, pic_value); -char pic_char(pic_state *, pic_value); -bool pic_bool(pic_state *, pic_value); -/* const char *pic_str(pic_state *, pic_value); */ -/* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ -/* void *pic_data(pic_state *, pic_value); */ +#define PIC_TYPE_INVALID 1 +#define PIC_TYPE_FLOAT 2 +#define PIC_TYPE_INT 3 +#define PIC_TYPE_CHAR 4 +#define PIC_TYPE_EOF 5 +#define PIC_TYPE_UNDEF 6 +#define PIC_TYPE_TRUE 8 +#define PIC_TYPE_NIL 7 +#define PIC_TYPE_FALSE 9 +#define PIC_IVAL_END 10 +/* --------------------- */ +#define PIC_TYPE_STRING 16 +#define PIC_TYPE_VECTOR 17 +#define PIC_TYPE_BLOB 18 +#define PIC_TYPE_PROC 19 +#define PIC_TYPE_PORT 20 +#define PIC_TYPE_ERROR 21 +#define PIC_TYPE_ID 22 +#define PIC_TYPE_ENV 23 +#define PIC_TYPE_DATA 24 +#define PIC_TYPE_DICT 25 +#define PIC_TYPE_WEAK 26 +#define PIC_TYPE_RECORD 27 +#define PIC_TYPE_SYMBOL 28 +#define PIC_TYPE_PAIR 29 +#define PIC_TYPE_CXT 30 +#define PIC_TYPE_CP 31 + +#include "picrin/type.h" + +#define pic_undef_p(pic,v) (pic_type(pic,v) == PIC_TYPE_UNDEF) +#define pic_int_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INT) +#define pic_float_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FLOAT) +#define pic_char_p(pic,v) (pic_type(pic,v) == PIC_TYPE_CHAR) +#define pic_eof_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_EOF) +#define pic_true_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE) +#define pic_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE) +#define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING) +#define pic_blob_p(pic,v) (pic_type(pic,v) == PIC_TYPE_BLOB) +#define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PROC) +#define pic_data_p(pic,v) (pic_type(pic,v) == PIC_TYPE_DATA) +#define pic_nil_p(pic,v) (pic_type(pic,v) == PIC_TYPE_NIL) +#define pic_pair_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PAIR) +#define pic_vec_p(pic,v) (pic_type(pic,v) == PIC_TYPE_VECTOR) +#define pic_dict_p(pic,v) (pic_type(pic,v) == PIC_TYPE_DICT) +#define pic_weak_p(pic,v) (pic_type(pic,v) == PIC_TYPE_WEAK) +#define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT) +#define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL) pic_value pic_undef_value(pic_state *); pic_value pic_int_value(pic_state *, int); @@ -102,27 +172,18 @@ pic_value pic_char_value(pic_state *, char); pic_value pic_true_value(pic_state *); pic_value pic_false_value(pic_state *); pic_value pic_bool_value(pic_state *, bool); +pic_value pic_eof_object(pic_state *); -#define pic_undef_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_UNDEF) -#define pic_int_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_INT) -#define pic_float_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_FLOAT) -#define pic_char_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_CHAR) -#define pic_true_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_TRUE) -#define pic_false_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_FALSE) -#define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TT_STRING) -#define pic_blob_p(pic,v) (pic_type(pic,v) == PIC_TT_BLOB) -#define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TT_PROC) -#define pic_data_p(pic,v) (pic_type(pic,v) == PIC_TT_DATA) -#define pic_nil_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_NIL) -#define pic_pair_p(pic,v) (pic_type(pic,v) == PIC_TT_PAIR) -#define pic_vec_p(pic,v) (pic_type(pic,v) == PIC_TT_VECTOR) -#define pic_dict_p(pic,v) (pic_type(pic,v) == PIC_TT_DICT) -#define pic_weak_p(pic,v) (pic_type(pic,v) == PIC_TT_WEAK) -#define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TT_PORT) -#define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TT_SYMBOL) +int pic_int(pic_state *, pic_value); +double pic_float(pic_state *, pic_value); +char pic_char(pic_state *, pic_value); +bool pic_bool(pic_state *, pic_value); +/* const char *pic_str(pic_state *, pic_value); */ +/* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ +/* void *pic_data(pic_state *, pic_value); */ -enum pic_tt pic_type(pic_state *, pic_value); -const char *pic_type_repr(pic_state *, enum pic_tt); +int pic_type(pic_state *, pic_value); +const char *pic_typename(pic_state *, int); bool pic_eq_p(pic_state *, pic_value, pic_value); bool pic_eqv_p(pic_state *, pic_value, pic_value); @@ -205,7 +266,7 @@ void *pic_default_allocf(void *, void *, size_t); pic_errorf(pic, "expected " #type ", but got ~s", v); \ } -struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); +struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); #define pic_void(exec) \ pic_void_(PIC_GENSYM(ai), exec) diff --git a/extlib/benz/include/picrin/blob.h b/extlib/benz/include/picrin/blob.h index 2440c27f..e75051d6 100644 --- a/extlib/benz/include/picrin/blob.h +++ b/extlib/benz/include/picrin/blob.h @@ -15,7 +15,7 @@ struct pic_blob { int len; }; -#define pic_blob_ptr(v) ((struct pic_blob *)pic_ptr(v)) +#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) struct pic_blob *pic_make_blob(pic_state *, int); diff --git a/extlib/benz/include/picrin/data.h b/extlib/benz/include/picrin/data.h index fdd88008..6a701faa 100644 --- a/extlib/benz/include/picrin/data.h +++ b/extlib/benz/include/picrin/data.h @@ -21,7 +21,7 @@ struct pic_data { void *data; }; -#define pic_data_ptr(o) ((struct pic_data *)pic_ptr(o)) +#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) PIC_INLINE bool pic_data_type_p(pic_state *pic, const pic_value obj, const pic_data_type *type) { return pic_data_p(pic, obj) && pic_data_ptr(obj)->type == type; diff --git a/extlib/benz/include/picrin/dict.h b/extlib/benz/include/picrin/dict.h index 0aaa11aa..d0ce786e 100644 --- a/extlib/benz/include/picrin/dict.h +++ b/extlib/benz/include/picrin/dict.h @@ -16,7 +16,7 @@ struct pic_dict { khash_t(dict) hash; }; -#define pic_dict_ptr(v) ((struct pic_dict *)pic_ptr(v)) +#define pic_dict_ptr(v) ((struct pic_dict *)pic_obj_ptr(v)) #define pic_dict_for_each(sym, dict, it) \ pic_dict_for_each_help(sym, (&(dict)->hash), it) diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h index 235a9d87..d09056c7 100644 --- a/extlib/benz/include/picrin/error.h +++ b/extlib/benz/include/picrin/error.h @@ -17,8 +17,8 @@ struct pic_error { struct pic_string *stack; }; -#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TT_ERROR) -#define pic_error_ptr(v) ((struct pic_error *)pic_ptr(v)) +#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) +#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_value); diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index a8fab6ea..3c2703b8 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -18,8 +18,8 @@ struct pic_env { struct pic_string *lib; }; -#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TT_ENV) -#define pic_env_ptr(v) ((struct pic_env *)pic_ptr(v)) +#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) +#define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) struct pic_env *pic_make_topenv(pic_state *, struct pic_string *); struct pic_env *pic_make_env(pic_state *, struct pic_env *); diff --git a/extlib/benz/include/picrin/pair.h b/extlib/benz/include/picrin/pair.h index ddd5a706..a9ae5933 100644 --- a/extlib/benz/include/picrin/pair.h +++ b/extlib/benz/include/picrin/pair.h @@ -15,7 +15,7 @@ struct pic_pair { pic_value cdr; }; -#define pic_pair_ptr(o) ((struct pic_pair *)pic_ptr(o)) +#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) PIC_INLINE pic_value pic_car(pic_state *pic, pic_value obj) diff --git a/extlib/benz/include/picrin/port.h b/extlib/benz/include/picrin/port.h index 22674b33..835d1988 100644 --- a/extlib/benz/include/picrin/port.h +++ b/extlib/benz/include/picrin/port.h @@ -23,9 +23,7 @@ struct pic_port { int flags; }; -#define pic_port_ptr(v) ((struct pic_port *)pic_ptr(v)) - -pic_value pic_eof_object(); +#define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) struct pic_port *pic_open_input_string(pic_state *, const char *); struct pic_port *pic_open_output_string(pic_state *); diff --git a/extlib/benz/include/picrin/proc.h b/extlib/benz/include/picrin/proc.h index b536868e..7fcbe509 100644 --- a/extlib/benz/include/picrin/proc.h +++ b/extlib/benz/include/picrin/proc.h @@ -39,10 +39,10 @@ struct pic_proc { #define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) #define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) -#define pic_proc_ptr(o) ((struct pic_proc *)pic_ptr(o)) +#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) -#define pic_context_p(o) (pic_type(pic, o) == PIC_TT_CXT) -#define pic_context_ptr(o) ((struct pic_context *)pic_ptr(o)) +#define pic_context_p(o) (pic_type(pic, o) == PIC_TYPE_CXT) +#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); diff --git a/extlib/benz/include/picrin/record.h b/extlib/benz/include/picrin/record.h index d45cef27..0549fa90 100644 --- a/extlib/benz/include/picrin/record.h +++ b/extlib/benz/include/picrin/record.h @@ -15,8 +15,8 @@ struct pic_record { pic_value datum; }; -#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TT_RECORD) -#define pic_rec_ptr(v) ((struct pic_record *)pic_ptr(v)) +#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) +#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 47b97638..2fa429a7 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -5,13 +5,13 @@ #include "picrin/config.h" #ifndef PIC_DIRECT_THREADED_VM -# if (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 +# if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__) # define PIC_DIRECT_THREADED_VM 1 # endif #endif #ifndef PIC_NAN_BOXING -# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && __STRICT_ANSI__ != 1 +# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__) # define PIC_NAN_BOXING 1 # endif #endif diff --git a/extlib/benz/include/picrin/string.h b/extlib/benz/include/picrin/string.h index f9b44fbc..117fc24e 100644 --- a/extlib/benz/include/picrin/string.h +++ b/extlib/benz/include/picrin/string.h @@ -17,7 +17,7 @@ struct pic_string { void pic_rope_incref(pic_state *, struct pic_rope *); void pic_rope_decref(pic_state *, struct pic_rope *); -#define pic_str_ptr(o) ((struct pic_string *)pic_ptr(o)) +#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) struct pic_string *pic_make_str(pic_state *, const char *, int); #define pic_make_cstr(pic, cstr) pic_make_str(pic, (cstr), strlen(cstr)) diff --git a/extlib/benz/include/picrin/symbol.h b/extlib/benz/include/picrin/symbol.h index 0d1ff11c..3104c363 100644 --- a/extlib/benz/include/picrin/symbol.h +++ b/extlib/benz/include/picrin/symbol.h @@ -23,10 +23,10 @@ struct pic_id { } u; }; -#define pic_sym_ptr(v) ((pic_sym *)pic_ptr(v)) +#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) -#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TT_ID || pic_type(pic, v) == PIC_TT_SYMBOL) -#define pic_id_ptr(v) ((pic_id *)pic_ptr(v)) +#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) +#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index 5e6f92d6..0f03314e 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -14,43 +14,25 @@ extern "C" { * it is only used for repsenting internal special state */ -enum pic_vtype { - PIC_VTYPE_NIL = 1, - PIC_VTYPE_TRUE, - PIC_VTYPE_FALSE, - PIC_VTYPE_UNDEF, - PIC_VTYPE_INVALID, - PIC_VTYPE_FLOAT, - PIC_VTYPE_INT, - PIC_VTYPE_CHAR, - PIC_VTYPE_EOF, - PIC_VTYPE_HEAP -}; - #if PIC_NAN_BOXING -#include - /** * value representation by nan-boxing: * float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF * ptr : 111111111111TTTT PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP - * int : 1111111111110110 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII - * char : 1111111111111000 0000000000000000 CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC + * int : 111111111111TTTT 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII + * char : 111111111111TTTT 0000000000000000 CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC */ -typedef uint64_t pic_value; - -#define pic_ptr(v) ((void *)(0xfffffffffffful & (v))) #define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48))) -static inline enum pic_vtype +PIC_INLINE int pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) { - return 0xfff0 >= (v >> 48) ? PIC_VTYPE_FLOAT : ((v >> 48) & 0xf); + return 0xfff0 >= (v >> 48) ? PIC_TYPE_FLOAT : ((v >> 48) & 0xf); } -static inline double +PIC_INLINE double pic_float(pic_state PIC_UNUSED(*pic), pic_value v) { union { double f; uint64_t i; } u; @@ -58,7 +40,7 @@ pic_float(pic_state PIC_UNUSED(*pic), pic_value v) return u.f; } -static inline int +PIC_INLINE int pic_int(pic_state PIC_UNUSED(*pic), pic_value v) { union { int i; unsigned u; } u; @@ -66,28 +48,28 @@ pic_int(pic_state PIC_UNUSED(*pic), pic_value v) return u.i; } -static inline char +PIC_INLINE char pic_char(pic_state PIC_UNUSED(*pic), pic_value v) { return v & 0xfffffffful; } +PIC_INLINE struct pic_object * +pic_obj_ptr(pic_value v) +{ + return (struct pic_object *)(0xfffffffffffful & v); +} + #else -typedef struct { - enum pic_vtype type; - union { - void *data; - double f; - int i; - char c; - } u; -} pic_value; - -#define pic_ptr(v) ((v).u.data) -#define pic_vtype(pic,v) ((v).type) #define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) +PIC_INLINE int +pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return (int)(v.type); +} + PIC_INLINE double pic_float(pic_state PIC_UNUSED(*pic), pic_value v) { @@ -106,68 +88,24 @@ pic_char(pic_state PIC_UNUSED(*pic), pic_value v) return v.u.c; } +PIC_INLINE struct pic_object * +pic_obj_ptr(pic_value v) +{ + return (struct pic_object *)(v.u.data); +} + #endif -enum pic_tt { - /* immediate */ - PIC_TT_NIL, - PIC_TT_BOOL, - PIC_TT_FLOAT, - PIC_TT_INT, - PIC_TT_CHAR, - PIC_TT_EOF, - PIC_TT_UNDEF, - PIC_TT_INVALID, - /* heap */ - PIC_TT_SYMBOL, - PIC_TT_PAIR, - PIC_TT_STRING, - PIC_TT_VECTOR, - PIC_TT_BLOB, - PIC_TT_PROC, - PIC_TT_PORT, - PIC_TT_ERROR, - PIC_TT_ID, - PIC_TT_ENV, - PIC_TT_DATA, - PIC_TT_DICT, - PIC_TT_WEAK, - PIC_TT_RECORD, - PIC_TT_CXT, - PIC_TT_CP -}; - #define PIC_OBJECT_HEADER \ - enum pic_tt tt; \ + unsigned char tt; \ char gc_mark; struct pic_basic { PIC_OBJECT_HEADER }; -struct pic_object; -struct pic_symbol; -struct pic_pair; -struct pic_string; -struct pic_vector; -struct pic_blob; - -struct pic_proc; -struct pic_port; -struct pic_error; -struct pic_env; - -/* set aliases to basic types */ -typedef struct pic_symbol pic_sym; -typedef struct pic_id pic_id; -typedef struct pic_pair pic_pair; -typedef struct pic_vector pic_vec; - -#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_VTYPE_HEAP) -#define pic_obj_ptr(v) ((struct pic_object *)pic_ptr(v)) - -#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_VTYPE_INVALID) -#define pic_eof_p(pic, v) (pic_vtype(pic, v) == PIC_VTYPE_EOF) +#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) +#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_INVALID) #define pic_test(pic, v) (! pic_false_p(pic, v)) @@ -177,100 +115,21 @@ pic_valid_int(double v) return INT_MIN <= v && v <= INT_MAX; } -PIC_INLINE pic_value pic_invalid_value(); -PIC_INLINE pic_value pic_obj_value(void *); - -PIC_INLINE enum pic_tt -pic_type(pic_state PIC_UNUSED(*pic), pic_value v) -{ - switch (pic_vtype(pic, v)) { - case PIC_VTYPE_NIL: - return PIC_TT_NIL; - case PIC_VTYPE_TRUE: - return PIC_TT_BOOL; - case PIC_VTYPE_FALSE: - return PIC_TT_BOOL; - case PIC_VTYPE_UNDEF: - return PIC_TT_UNDEF; - case PIC_VTYPE_INVALID: - return PIC_TT_INVALID; - case PIC_VTYPE_FLOAT: - return PIC_TT_FLOAT; - case PIC_VTYPE_INT: - return PIC_TT_INT; - case PIC_VTYPE_CHAR: - return PIC_TT_CHAR; - case PIC_VTYPE_EOF: - return PIC_TT_EOF; - case PIC_VTYPE_HEAP: - return ((struct pic_basic *)pic_ptr(v))->tt; - } - - PIC_UNREACHABLE(); -} - -PIC_INLINE const char * -pic_type_repr(pic_state PIC_UNUSED(*pic), enum pic_tt tt) -{ - switch (tt) { - case PIC_TT_NIL: - return "nil"; - case PIC_TT_BOOL: - return "boolean"; - case PIC_TT_FLOAT: - return "float"; - case PIC_TT_INT: - return "int"; - case PIC_TT_SYMBOL: - return "symbol"; - case PIC_TT_CHAR: - return "char"; - case PIC_TT_EOF: - return "eof"; - case PIC_TT_UNDEF: - return "undef"; - case PIC_TT_INVALID: - return "invalid"; - case PIC_TT_PAIR: - return "pair"; - case PIC_TT_STRING: - return "string"; - case PIC_TT_VECTOR: - return "vector"; - case PIC_TT_BLOB: - return "blob"; - case PIC_TT_PORT: - return "port"; - case PIC_TT_ERROR: - return "error"; - case PIC_TT_ID: - return "id"; - case PIC_TT_CXT: - return "cxt"; - case PIC_TT_PROC: - return "proc"; - case PIC_TT_ENV: - return "env"; - case PIC_TT_DATA: - return "data"; - case PIC_TT_DICT: - return "dict"; - case PIC_TT_WEAK: - return "weak"; - case PIC_TT_RECORD: - return "record"; - case PIC_TT_CP: - return "checkpoint"; - } - PIC_UNREACHABLE(); -} - PIC_INLINE pic_value pic_nil_value(pic_state PIC_UNUSED(*pic)) { pic_value v; - pic_init_value(v, PIC_VTYPE_NIL); + pic_init_value(v, PIC_TYPE_NIL); + return v; +} + +PIC_INLINE pic_value +pic_eof_object(pic_state PIC_UNUSED(*pic)) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_EOF); return v; } @@ -279,7 +138,7 @@ pic_true_value(pic_state PIC_UNUSED(*pic)) { pic_value v; - pic_init_value(v, PIC_VTYPE_TRUE); + pic_init_value(v, PIC_TYPE_TRUE); return v; } @@ -288,7 +147,7 @@ pic_false_value(pic_state PIC_UNUSED(*pic)) { pic_value v; - pic_init_value(v, PIC_VTYPE_FALSE); + pic_init_value(v, PIC_TYPE_FALSE); return v; } @@ -297,7 +156,25 @@ pic_bool_value(pic_state PIC_UNUSED(*pic), bool b) { pic_value v; - pic_init_value(v, b ? PIC_VTYPE_TRUE : PIC_VTYPE_FALSE); + pic_init_value(v, b ? PIC_TYPE_TRUE : PIC_TYPE_FALSE); + return v; +} + +PIC_INLINE pic_value +pic_undef_value(pic_state PIC_UNUSED(*pic)) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_UNDEF); + return v; +} + +PIC_INLINE pic_value +pic_invalid_value() +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_INVALID); return v; } @@ -308,7 +185,7 @@ pic_obj_value(void *ptr) { pic_value v; - pic_init_value(v, PIC_VTYPE_HEAP); + pic_init_value(v, PIC_IVAL_END); v |= 0xfffffffffffful & (uint64_t)ptr; return v; } @@ -329,13 +206,10 @@ pic_float_value(pic_state PIC_UNUSED(*pic), double f) PIC_INLINE pic_value pic_int_value(pic_state PIC_UNUSED(*pic), int i) { - union { int i; unsigned u; } u; pic_value v; - u.i = i; - - pic_init_value(v, PIC_VTYPE_INT); - v |= u.u; + pic_init_value(v, PIC_TYPE_INT); + v |= (unsigned)i; return v; } @@ -344,8 +218,8 @@ pic_char_value(pic_state PIC_UNUSED(*pic), char c) { pic_value v; - pic_init_value(v, PIC_VTYPE_CHAR); - v |= c; + pic_init_value(v, PIC_TYPE_CHAR); + v |= (unsigned char)c; return v; } @@ -356,7 +230,7 @@ pic_obj_value(void *ptr) { pic_value v; - pic_init_value(v, PIC_VTYPE_HEAP); + pic_init_value(v, PIC_IVAL_END); v.u.data = ptr; return v; } @@ -366,7 +240,7 @@ pic_float_value(pic_state PIC_UNUSED(*pic), double f) { pic_value v; - pic_init_value(v, PIC_VTYPE_FLOAT); + pic_init_value(v, PIC_TYPE_FLOAT); v.u.f = f; return v; } @@ -376,7 +250,7 @@ pic_int_value(pic_state PIC_UNUSED(*pic), int i) { pic_value v; - pic_init_value(v, PIC_VTYPE_INT); + pic_init_value(v, PIC_TYPE_INT); v.u.i = i; return v; } @@ -386,95 +260,13 @@ pic_char_value(pic_state PIC_UNUSED(*pic), char c) { pic_value v; - pic_init_value(v, PIC_VTYPE_CHAR); + pic_init_value(v, PIC_TYPE_CHAR); v.u.c = c; return v; } #endif -PIC_INLINE pic_value -pic_undef_value(pic_state PIC_UNUSED(*pic)) -{ - pic_value v; - - pic_init_value(v, PIC_VTYPE_UNDEF); - return v; -} - -PIC_INLINE pic_value -pic_invalid_value() -{ - pic_value v; - - pic_init_value(v, PIC_VTYPE_INVALID); - return v; -} - -#if PIC_NAN_BOXING - -PIC_INLINE bool -pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) -{ - return x == y; -} - -PIC_INLINE bool -pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) -{ - return x == y; -} - -#else - -PIC_INLINE bool -pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) -{ - if (pic_type(pic, x) != pic_type(pic, y)) - return false; - - switch (pic_type(pic, x)) { - case PIC_TT_NIL: - return true; - case PIC_TT_BOOL: - return pic_vtype(pic, x) == pic_vtype(pic, y); - default: - return pic_ptr(x) == pic_ptr(y); - } -} - -PIC_INLINE bool -pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) -{ - if (pic_type(pic, x) != pic_type(pic, y)) - return false; - - switch (pic_type(pic, x)) { - case PIC_TT_NIL: - return true; - case PIC_TT_BOOL: - return pic_vtype(pic, x) == pic_vtype(pic, y); - case PIC_TT_FLOAT: - return pic_float(pic, x) == pic_float(pic, y); - case PIC_TT_INT: - return pic_int(pic, x) == pic_int(pic, y); - default: - return pic_ptr(x) == pic_ptr(y); - } -} - -#endif - -pic_value pic_add(pic_state *, pic_value, pic_value); -pic_value pic_sub(pic_state *, pic_value, pic_value); -pic_value pic_mul(pic_state *, pic_value, pic_value); -pic_value pic_div(pic_state *, pic_value, pic_value); -bool pic_eq(pic_state *, pic_value, pic_value); -bool pic_lt(pic_state *, pic_value, pic_value); -bool pic_le(pic_state *, pic_value, pic_value); -bool pic_gt(pic_state *, pic_value, pic_value); -bool pic_ge(pic_state *, pic_value, pic_value); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/vector.h b/extlib/benz/include/picrin/vector.h index e3ac6fd0..bb5ddad1 100644 --- a/extlib/benz/include/picrin/vector.h +++ b/extlib/benz/include/picrin/vector.h @@ -15,7 +15,7 @@ struct pic_vector { int len; }; -#define pic_vec_ptr(o) ((struct pic_vector *)pic_ptr(o)) +#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/weak.h b/extlib/benz/include/picrin/weak.h index 1b502365..914865b8 100644 --- a/extlib/benz/include/picrin/weak.h +++ b/extlib/benz/include/picrin/weak.h @@ -17,7 +17,7 @@ struct pic_weak { struct pic_weak *prev; /* for GC */ }; -#define pic_weak_ptr(v) ((struct pic_weak *)pic_ptr(v)) +#define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) #if defined(__cplusplus) } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 176c39bf..1df08a07 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -13,7 +13,7 @@ pic_make_env(pic_state *pic, struct pic_env *up) assert(up != NULL); - env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV); + env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); env->up = up; env->lib = NULL; kh_init(env, &env->map); @@ -25,7 +25,7 @@ pic_make_topenv(pic_state *pic, struct pic_string *lib) { struct pic_env *env; - env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV); + env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); env->up = NULL; env->lib = lib; kh_init(env, &env->map); @@ -285,11 +285,11 @@ static pic_value expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) { switch (pic_type(pic, expr)) { - case PIC_TT_ID: - case PIC_TT_SYMBOL: { + case PIC_TYPE_ID: + case PIC_TYPE_SYMBOL: { return expand_var(pic, pic_id_ptr(expr), env, deferred); } - case PIC_TT_PAIR: { + case PIC_TYPE_PAIR: { struct pic_proc *mac; if (! pic_list_p(pic, expr)) { diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 80c95121..a853440c 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -9,7 +9,7 @@ pic_cons(pic_state *pic, pic_value car, pic_value cdr) { struct pic_pair *pair; - pair = (struct pic_pair *)pic_obj_alloc(pic, sizeof(struct pic_pair), PIC_TT_PAIR); + pair = (struct pic_pair *)pic_obj_alloc(pic, sizeof(struct pic_pair), PIC_TYPE_PAIR); pair->car = car; pair->cdr = cdr; diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 84b27e40..4ad2251c 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -4,16 +4,6 @@ #include "picrin.h" -pic_value -pic_eof_object() -{ - pic_value v; - - pic_init_value(v, PIC_VTYPE_EOF); - - return v; -} - static pic_value pic_assert_port(pic_state *pic) { @@ -121,7 +111,7 @@ pic_open_file(pic_state *pic, const char *name, int flags) { file_error(pic, pic_str_cstr(pic, pic_format(pic, "could not open file '%s'", name))); } - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = file; port->flags = flags | PIC_PORT_OPEN; @@ -159,7 +149,7 @@ pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) { struct pic_port *port; - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = file; port->flags = dir | PIC_PORT_TEXT | PIC_PORT_OPEN; @@ -276,7 +266,7 @@ pic_open_input_string(pic_state *pic, const char *str) { struct pic_port *port; - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, str, strlen(str)); port->flags = PIC_PORT_IN | PIC_PORT_TEXT | PIC_PORT_OPEN; @@ -288,7 +278,7 @@ pic_open_output_string(pic_state *pic) { struct pic_port *port; - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, NULL, 0); port->flags = PIC_PORT_OUT | PIC_PORT_TEXT | PIC_PORT_OPEN; @@ -424,7 +414,7 @@ pic_port_eof_object(pic_state *pic) { pic_get_args(pic, ""); - return pic_eof_object(); + return pic_eof_object(pic); } static pic_value @@ -516,7 +506,7 @@ pic_port_open_input_blob(pic_state *pic) pic_get_args(pic, "b", &blob); - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, (const char *)blob->data, blob->len); port->flags = PIC_PORT_IN | PIC_PORT_BINARY | PIC_PORT_OPEN; @@ -530,7 +520,7 @@ pic_port_open_output_bytevector(pic_state *pic) pic_get_args(pic, ""); - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, NULL, 0); port->flags = PIC_PORT_OUT | PIC_PORT_BINARY | PIC_PORT_OPEN; @@ -573,7 +563,7 @@ pic_port_read_char(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-char"); if ((c = xfgetc(pic, port->file)) == EOF) { - return pic_eof_object(); + return pic_eof_object(pic); } else { return pic_char_value(pic, (char)c); @@ -591,7 +581,7 @@ pic_port_peek_char(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "peek-char"); if ((c = xfgetc(pic, port->file)) == EOF) { - return pic_eof_object(); + return pic_eof_object(pic); } else { xungetc(c, port->file); @@ -605,7 +595,7 @@ pic_port_read_line(pic_state *pic) int c; struct pic_port *port = pic_stdin(pic), *buf; struct pic_string *str; - pic_value res = pic_eof_object(); + pic_value res = pic_eof_object(pic); pic_get_args(pic, "|p", &port); @@ -644,7 +634,7 @@ pic_port_read_string(pic_state *pic){ struct pic_string *str; int k, i; int c; - pic_value res = pic_eof_object(); + pic_value res = pic_eof_object(pic); pic_get_args(pic, "i|p", &k, &port); @@ -677,7 +667,7 @@ pic_port_read_byte(pic_state *pic){ assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-u8"); if ((c = xfgetc(pic, port->file)) == EOF) { - return pic_eof_object(); + return pic_eof_object(pic); } return pic_int_value(pic, c); @@ -695,7 +685,7 @@ pic_port_peek_byte(pic_state *pic) c = xfgetc(pic, port->file); if (c == EOF) { - return pic_eof_object(); + return pic_eof_object(pic); } else { xungetc(c, port->file); @@ -731,7 +721,7 @@ pic_port_read_blob(pic_state *pic) i = xfread(pic, blob->data, sizeof(char), k, port->file); if (i == 0) { - return pic_eof_object(); + return pic_eof_object(pic); } else { pic_realloc(pic, blob->data, i); @@ -772,7 +762,7 @@ pic_port_read_blob_ip(pic_state *pic) pic_free(pic, buf); if (i == 0) { - return pic_eof_object(); + return pic_eof_object(pic); } else { return pic_int_value(pic, i); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 699474d4..0430c661 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -107,11 +107,11 @@ pic_get_args(pic_state *pic, const char *format, ...) \ v = GET_OPERAND(pic, i); \ switch (pic_type(pic, v)) { \ - case PIC_TT_FLOAT: \ + case PIC_TYPE_FLOAT: \ *n = pic_float(pic, v); \ *e = false; \ break; \ - case PIC_TT_INT: \ + case PIC_TYPE_INT: \ *n = pic_int(pic, v); \ *e = true; \ break; \ @@ -195,7 +195,7 @@ vm_push_cxt(pic_state *pic) { pic_callinfo *ci = pic->ci; - ci->cxt = (struct pic_context *)pic_obj_alloc(pic, offsetof(struct pic_context, storage) + sizeof(pic_value) * ci->regc, PIC_TT_CXT); + ci->cxt = (struct pic_context *)pic_obj_alloc(pic, offsetof(struct pic_context, storage) + sizeof(pic_value) * ci->regc, PIC_TYPE_CXT); ci->cxt->up = ci->up; ci->cxt->regc = ci->regc; ci->cxt->regs = ci->regs; @@ -321,6 +321,17 @@ pic_vm_tear_off(pic_state *pic) # define VM_CALL_PRINT #endif +/* for arithmetic instructions */ +pic_value pic_add(pic_state *, pic_value, pic_value); +pic_value pic_sub(pic_state *, pic_value, pic_value); +pic_value pic_mul(pic_state *, pic_value, pic_value); +pic_value pic_div(pic_state *, pic_value, pic_value); +bool pic_eq(pic_state *, pic_value, pic_value); +bool pic_lt(pic_state *, pic_value, pic_value); +bool pic_le(pic_state *, pic_value, pic_value); +bool pic_gt(pic_state *, pic_value, pic_value); +bool pic_ge(pic_state *, pic_value, pic_value); + pic_value pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) { @@ -399,7 +410,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) NEXT; } CASE(OP_PUSHEOF) { - PUSH(pic_eof_object()); + PUSH(pic_eof_object(pic)); NEXT; } CASE(OP_PUSHCONST) { @@ -1003,7 +1014,7 @@ pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) struct pic_proc *proc; int i; - proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals) + sizeof(pic_value) * n, PIC_TT_PROC); + proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals) + sizeof(pic_value) * n, PIC_TYPE_PROC); proc->tag = PIC_PROC_TAG_FUNC; proc->u.f.func = func; proc->u.f.localc = n; @@ -1018,7 +1029,7 @@ pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cx { struct pic_proc *proc; - proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals), PIC_TT_PROC); + proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals), PIC_TYPE_PROC); proc->tag = PIC_PROC_TAG_IREP; proc->u.i.irep = irep; proc->u.i.cxt = cxt; diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 3819d9e5..59aee417 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -838,7 +838,7 @@ pic_read(pic_state *pic, struct pic_port *port) pic_gc_arena_restore(pic, ai); } if (c == EOF) { - return pic_eof_object(); + return pic_eof_object(pic); } pic_gc_arena_restore(pic, ai); diff --git a/extlib/benz/record.c b/extlib/benz/record.c index ded83ab2..c338989a 100644 --- a/extlib/benz/record.c +++ b/extlib/benz/record.c @@ -9,7 +9,7 @@ pic_make_rec(pic_state *pic, pic_value type, pic_value datum) { struct pic_record *rec; - rec = (struct pic_record *)pic_obj_alloc(pic, sizeof(struct pic_record), PIC_TT_RECORD); + rec = (struct pic_record *)pic_obj_alloc(pic, sizeof(struct pic_record), PIC_TYPE_RECORD); rec->type = type; rec->datum = datum; diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 917bd59f..a6e0ee97 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -334,7 +334,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->macros = pic_make_weak(pic); /* root block */ - pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TT_CP); + pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TYPE_CP); pic->cp->prev = NULL; pic->cp->depth = 0; pic->cp->in = pic->cp->out = NULL; diff --git a/extlib/benz/string.c b/extlib/benz/string.c index bd0ac8b4..e29ffb32 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -98,7 +98,7 @@ pic_make_string(pic_state *pic, struct pic_rope *rope) { struct pic_string *str; - str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TT_STRING); + str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TYPE_STRING); str->rope = rope; /* delegate ownership */ return str; } diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 0e185dec..51e9cfbc 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -26,7 +26,7 @@ pic_intern(pic_state *pic, struct pic_string *str) kh_val(h, it) = pic->sQUOTE; /* dummy */ - sym = (pic_sym *)pic_obj_alloc(pic, sizeof(pic_sym), PIC_TT_SYMBOL); + sym = (pic_sym *)pic_obj_alloc(pic, sizeof(pic_sym), PIC_TYPE_SYMBOL); sym->str = str; kh_val(h, it) = sym; @@ -38,7 +38,7 @@ pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { pic_id *nid; - nid = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TT_ID); + nid = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); nid->u.id.id = id; nid->u.id.env = env; return nid; diff --git a/extlib/benz/value.c b/extlib/benz/value.c new file mode 100644 index 00000000..e0857dfb --- /dev/null +++ b/extlib/benz/value.c @@ -0,0 +1,74 @@ +/** + * See Copyright Notice in picrin.h + */ + +#include "picrin.h" + +int +pic_type(pic_state PIC_UNUSED(*pic), pic_value v) +{ + int tt = pic_vtype(pic, v); + + if (tt < PIC_IVAL_END) { + return tt; + } + return ((struct pic_basic *)pic_obj_ptr(v))->tt; +} + +const char * +pic_typename(pic_state *pic, int type) +{ + switch (type) { + case PIC_TYPE_NIL: + return "null"; + case PIC_TYPE_TRUE: + case PIC_TYPE_FALSE: + return "boolean"; + case PIC_TYPE_FLOAT: + return "float"; + case PIC_TYPE_INT: + return "int"; + case PIC_TYPE_SYMBOL: + return "symbol"; + case PIC_TYPE_CHAR: + return "char"; + case PIC_TYPE_EOF: + return "eof-object"; + case PIC_TYPE_UNDEF: + return "undefined"; + case PIC_TYPE_INVALID: + return "invalid"; + case PIC_TYPE_PAIR: + return "pair"; + case PIC_TYPE_STRING: + return "string"; + case PIC_TYPE_VECTOR: + return "vector"; + case PIC_TYPE_BLOB: + return "bytevector"; + case PIC_TYPE_PORT: + return "port"; + case PIC_TYPE_ERROR: + return "error"; + case PIC_TYPE_ID: + return "identifier"; + case PIC_TYPE_CXT: + return "context"; + case PIC_TYPE_PROC: + return "procedure"; + case PIC_TYPE_ENV: + return "environment"; + case PIC_TYPE_DATA: + return "data"; + case PIC_TYPE_DICT: + return "dictionary"; + case PIC_TYPE_WEAK: + return "ephemeron"; + case PIC_TYPE_RECORD: + return "record"; + case PIC_TYPE_CP: + return "checkpoint"; + default: + pic_errorf(pic, "pic_typename: invalid type given %d", type); + } +} diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index af273b9b..1aa87e76 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -10,7 +10,7 @@ pic_make_vec(pic_state *pic, int len) struct pic_vector *vec; int i; - vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TT_VECTOR); + vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TYPE_VECTOR); vec->len = len; vec->data = (pic_value *)pic_malloc(pic, sizeof(pic_value) * len); for (i = 0; i < len; ++i) { diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 6dda9cd8..484bcbd8 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -11,7 +11,7 @@ pic_make_weak(pic_state *pic) { struct pic_weak *weak; - weak = (struct pic_weak *)pic_obj_alloc(pic, sizeof(struct pic_weak), PIC_TT_WEAK); + weak = (struct pic_weak *)pic_obj_alloc(pic, sizeof(struct pic_weak), PIC_TYPE_WEAK); weak->prev = NULL; kh_init(weak, &weak->hash); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 4cd4da89..426f9804 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -134,10 +134,10 @@ write_pair_help(struct writer_control *p, struct pic_pair *pair) else if (pic_pair_p(pic, pair->cdr)) { /* shared objects */ - if ((it = kh_get(l, lh, pic_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { + if ((it = kh_get(l, lh, pic_obj_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { xfprintf(pic, p->file, " . "); - kh_put(v, vh, pic_ptr(pair->cdr), &ret); + kh_put(v, vh, pic_obj_ptr(pair->cdr), &ret); if (ret == 0) { /* if exists */ xfprintf(pic, p->file, "#%d#", kh_val(lh, it)); return; @@ -151,8 +151,8 @@ write_pair_help(struct writer_control *p, struct pic_pair *pair) write_pair_help(p, pic_pair_ptr(pair->cdr)); if (p->op == OP_WRITE) { - if ((it = kh_get(l, lh, pic_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { - it = kh_get(v, vh, pic_ptr(pair->cdr)); + if ((it = kh_get(l, lh, pic_obj_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { + it = kh_get(v, vh, pic_obj_ptr(pair->cdr)); kh_del(v, vh, it); } } @@ -263,8 +263,8 @@ write_core(struct writer_control *p, pic_value obj) int ret; /* shared objects */ - if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { - kh_put(v, vh, pic_ptr(obj), &ret); + if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_obj_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { + kh_put(v, vh, pic_obj_ptr(obj), &ret); if (ret == 0) { /* if exists */ xfprintf(pic, file, "#%d#", kh_val(lh, it)); return; @@ -273,56 +273,59 @@ write_core(struct writer_control *p, pic_value obj) } switch (pic_type(pic, obj)) { - case PIC_TT_UNDEF: + case PIC_TYPE_UNDEF: xfprintf(pic, file, "#undefined"); break; - case PIC_TT_NIL: + case PIC_TYPE_NIL: xfprintf(pic, file, "()"); break; - case PIC_TT_BOOL: - xfprintf(pic, file, pic_true_p(pic, obj) ? "#t" : "#f"); + case PIC_TYPE_TRUE: + xfprintf(pic, file, "#t"); break; - case PIC_TT_ID: + case PIC_TYPE_FALSE: + xfprintf(pic, file, "#f"); + break; + case PIC_TYPE_ID: xfprintf(pic, file, "#", pic_identifier_name(pic, pic_id_ptr(obj))); break; - case PIC_TT_EOF: + case PIC_TYPE_EOF: xfprintf(pic, file, "#.(eof-object)"); break; - case PIC_TT_INT: + case PIC_TYPE_INT: xfprintf(pic, file, "%d", pic_int(pic, obj)); break; - case PIC_TT_FLOAT: + case PIC_TYPE_FLOAT: write_float(pic, pic_float(pic, obj), file); break; - case PIC_TT_SYMBOL: + case PIC_TYPE_SYMBOL: xfprintf(pic, file, "%s", pic_symbol_name(pic, pic_sym_ptr(obj))); break; - case PIC_TT_BLOB: + case PIC_TYPE_BLOB: write_blob(pic, pic_blob_ptr(obj), file); break; - case PIC_TT_CHAR: + case PIC_TYPE_CHAR: write_char(pic, pic_char(pic, obj), file, p->mode); break; - case PIC_TT_STRING: + case PIC_TYPE_STRING: write_str(pic, pic_str_ptr(obj), file, p->mode); break; - case PIC_TT_PAIR: + case PIC_TYPE_PAIR: write_pair(p, pic_pair_ptr(obj)); break; - case PIC_TT_VECTOR: + case PIC_TYPE_VECTOR: write_vec(p, pic_vec_ptr(obj)); break; - case PIC_TT_DICT: + case PIC_TYPE_DICT: write_dict(p, pic_dict_ptr(obj)); break; default: - xfprintf(pic, file, "#<%s %p>", pic_type_repr(pic, pic_type(pic, obj)), pic_ptr(obj)); + xfprintf(pic, file, "#<%s %p>", pic_typename(pic, pic_type(pic, obj)), pic_obj_ptr(obj)); break; } if (p->op == OP_WRITE) { - if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { - it = kh_get(v, vh, pic_ptr(obj)); + if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_obj_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { + it = kh_get(v, vh, pic_obj_ptr(obj)); kh_del(v, vh, it); } } @@ -338,14 +341,14 @@ traverse(struct writer_control *p, pic_value obj) } switch (pic_type(pic, obj)) { - case PIC_TT_PAIR: - case PIC_TT_VECTOR: - case PIC_TT_DICT: { + case PIC_TYPE_PAIR: + case PIC_TYPE_VECTOR: + case PIC_TYPE_DICT: { khash_t(l) *h = &p->labels; khiter_t it; int ret; - it = kh_put(l, h, pic_ptr(obj), &ret); + it = kh_put(l, h, pic_obj_ptr(obj), &ret); if (ret != 0) { /* first time */ kh_val(h, it) = -1; @@ -369,7 +372,7 @@ traverse(struct writer_control *p, pic_value obj) } if (p->op == OP_WRITE) { - it = kh_get(l, h, pic_ptr(obj)); + it = kh_get(l, h, pic_obj_ptr(obj)); if (kh_val(h, it) == -1) { kh_del(l, h, it); } From ada84f48d65a47aac1a493f6a3e98d8c914f88d5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 23:49:16 +0900 Subject: [PATCH 024/119] add some object constructors/destructors --- contrib/10.callcc/callcc.c | 4 +-- contrib/20.r7rs/src/mutable-string.c | 2 +- contrib/20.r7rs/src/system.c | 8 +++--- contrib/30.readline/src/readline.c | 14 +++++----- contrib/30.regexp/src/regexp.c | 16 ++++++------ contrib/40.srfi/src/106.c | 8 +++--- docs/capi.rst | 2 +- extlib/benz/cont.c | 2 +- extlib/benz/data.c | 11 -------- extlib/benz/debug.c | 12 ++++----- extlib/benz/error.c | 10 ++++---- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin.h | 38 +++++++++++++++++++--------- extlib/benz/include/picrin/data.h | 8 ------ extlib/benz/include/picrin/string.h | 9 ------- extlib/benz/lib.c | 6 ++--- extlib/benz/macro.c | 4 +-- extlib/benz/number.c | 2 +- extlib/benz/port.c | 4 +-- extlib/benz/proc.c | 8 +++--- extlib/benz/read.c | 12 ++++----- extlib/benz/string.c | 34 ++++++++++++------------- extlib/benz/symbol.c | 8 +++--- extlib/benz/value.c | 18 +++++++++++++ extlib/benz/vector.c | 2 +- extlib/benz/write.c | 12 ++++----- 26 files changed, 130 insertions(+), 126 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 0d48b6d3..7f102774 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -246,7 +246,7 @@ pic_callcc_full(pic_state *pic, struct pic_proc *proc) struct pic_proc *c; /* save the continuation object in proc */ - c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); + c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_value(pic, cont, &cont_type))); return pic_call(pic, proc, 1, pic_obj_value(c)); } @@ -269,7 +269,7 @@ pic_callcc_callcc(pic_state *pic) pic_value args[1]; /* save the continuation object in proc */ - c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); + c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_value(pic, cont, &cont_type))); args[0] = pic_obj_value(c); return pic_applyk(pic, proc, 1, args); diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index db58687e..b00842df 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -13,7 +13,7 @@ pic_str_set(pic_state *pic, struct pic_string *str, int i, char c) buf[0] = c; x = pic_str_sub(pic, str, 0, i); - y = pic_make_str(pic, buf, 1); + y = pic_str_value(pic, buf, 1); z = pic_str_sub(pic, str, i + 1, pic_str_len(pic, str)); tmp = pic_str_cat(pic, x, pic_str_cat(pic, y, z)); diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index d53169aa..abcda8be 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -21,7 +21,7 @@ pic_system_cmdline(pic_state *pic) for (i = 0; i < picrin_argc; ++i) { size_t ai = pic_gc_arena_preserve(pic); - v = pic_cons(pic, pic_obj_value(pic_make_cstr(pic, picrin_argv[i])), v); + v = pic_cons(pic, pic_obj_value(pic_cstr_value(pic, picrin_argv[i])), v); pic_gc_arena_restore(pic, ai); } @@ -88,7 +88,7 @@ pic_system_getenv(pic_state *pic) if (val == NULL) return pic_nil_value(pic); else - return pic_obj_value(pic_make_cstr(pic, val)); + return pic_obj_value(pic_cstr_value(pic, val)); } static pic_value @@ -111,8 +111,8 @@ pic_system_getenvs(pic_state *pic) for (i = 0; (*envp)[i] != '='; ++i) ; - key = pic_make_str(pic, *envp, i); - val = pic_make_cstr(pic, getenv(pic_str_cstr(pic, key))); + key = pic_str_value(pic, *envp, i); + val = pic_cstr_value(pic, getenv(pic_str(pic, key))); /* push */ data = pic_acons(pic, pic_obj_value(key), pic_obj_value(val), data); diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index 50c77163..b14fd482 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -19,7 +19,7 @@ pic_rl_readline(pic_state *pic) result = readline(prompt); if(result) - return pic_obj_value(pic_make_cstr(pic, result)); + return pic_obj_value(pic_cstr_value(pic, result)); else return pic_eof_object(pic); } @@ -87,7 +87,7 @@ pic_rl_current_history(pic_state *pic) { pic_get_args(pic, ""); - return pic_obj_value(pic_make_cstr(pic, current_history()->line)); + return pic_obj_value(pic_cstr_value(pic, current_history()->line)); } static pic_value @@ -100,7 +100,7 @@ pic_rl_history_get(pic_state *pic) e = history_get(i); - return e ? pic_obj_value(pic_make_cstr(pic, e->line)) + return e ? pic_obj_value(pic_cstr_value(pic, e->line)) : pic_false_value(pic); } @@ -114,7 +114,7 @@ pic_rl_remove_history(pic_state *pic) e = remove_history(i); - return e ? pic_obj_value(pic_make_cstr(pic, e->line)) + return e ? pic_obj_value(pic_cstr_value(pic, e->line)) : pic_false_value(pic); } @@ -148,7 +148,7 @@ pic_rl_previous_history(pic_state *pic) e = previous_history(); - return e ? pic_obj_value(pic_make_cstr(pic, e->line)) + return e ? pic_obj_value(pic_cstr_value(pic, e->line)) : pic_false_value(pic); } @@ -161,7 +161,7 @@ pic_rl_next_history(pic_state *pic) e = next_history(); - return e ? pic_obj_value(pic_make_cstr(pic, e->line)) + return e ? pic_obj_value(pic_cstr_value(pic, e->line)) : pic_false_value(pic); } @@ -240,7 +240,7 @@ pic_rl_history_expand(pic_state *pic) if(status == -1 || status == 2) pic_errorf(pic, "%s\n", result); - return pic_obj_value(pic_make_cstr(pic, result)); + return pic_obj_value(pic_cstr_value(pic, result)); } void diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 64740f8a..1a4ad678 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -62,7 +62,7 @@ pic_regexp_regexp(pic_state *pic) pic_errorf(pic, "regexp compilation error: %s", errbuf); } - return pic_obj_value(pic_data_alloc(pic, ®exp_type, reg)); + return pic_obj_value(pic_data_value(pic, reg, ®exp_type)); } static pic_value @@ -97,7 +97,7 @@ pic_regexp_regexp_match(pic_state *pic) offset = 0; while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, match, 0) != REG_NOMATCH) { - pic_push(pic, pic_obj_value(pic_make_str(pic, input, match[0].rm_eo - match[0].rm_so)), matches); + pic_push(pic, pic_obj_value(pic_str_value(pic, input, match[0].rm_eo - match[0].rm_so)), matches); pic_push(pic, pic_int_value(pic, offset), positions); offset += match[0].rm_eo; @@ -111,7 +111,7 @@ pic_regexp_regexp_match(pic_state *pic) if (match[i].rm_so == -1) { break; } - str = pic_make_str(pic, input + match[i].rm_so, match[i].rm_eo - match[i].rm_so); + str = pic_str_value(pic, input + match[i].rm_so, match[i].rm_eo - match[i].rm_so); pic_push(pic, pic_obj_value(str), matches); pic_push(pic, pic_int_value(pic, match[i].rm_so), positions); } @@ -141,12 +141,12 @@ pic_regexp_regexp_split(pic_state *pic) pic_assert_type(pic, reg, regexp); while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { - pic_push(pic, pic_obj_value(pic_make_str(pic, input, match.rm_so)), output); + pic_push(pic, pic_obj_value(pic_str_value(pic, input, match.rm_so)), output); input += match.rm_eo; } - pic_push(pic, pic_obj_value(pic_make_cstr(pic, input)), output); + pic_push(pic, pic_obj_value(pic_cstr_value(pic, input)), output); return pic_reverse(pic, output); } @@ -157,20 +157,20 @@ pic_regexp_regexp_replace(pic_state *pic) pic_value reg; const char *input; regmatch_t match; - struct pic_string *txt, *output = pic_make_lit(pic, ""); + struct pic_string *txt, *output = pic_lit_value(pic, ""); pic_get_args(pic, "ozs", ®, &input, &txt); pic_assert_type(pic, reg, regexp); while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { - output = pic_str_cat(pic, output, pic_make_str(pic, input, match.rm_so)); + output = pic_str_cat(pic, output, pic_str_value(pic, input, match.rm_so)); output = pic_str_cat(pic, output, txt); input += match.rm_eo; } - output = pic_str_cat(pic, output, pic_make_str(pic, input, strlen(input))); + output = pic_str_cat(pic, output, pic_str_value(pic, input, strlen(input))); return pic_obj_value(output); } diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index f88504de..fcd4e6f9 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -80,10 +80,10 @@ pic_socket_make_socket(pic_state *pic) node = service = NULL; if (pic_str_p(pic, n)) { - node = pic_str_cstr(pic, pic_str_ptr(n)); + node = pic_str(pic, pic_str_ptr(n)); } if (pic_str_p(pic, s)) { - service = pic_str_cstr(pic, pic_str_ptr(s)); + service = pic_str(pic, pic_str_ptr(s)); } sock = pic_malloc(pic, sizeof(struct pic_socket_t)); @@ -147,7 +147,7 @@ pic_socket_make_socket(pic_state *pic) pic_errorf(pic, "%s", strerror(errno)); } - return pic_obj_value(pic_data_alloc(pic, &socket_type, sock)); + return pic_obj_value(pic_data_value(pic, sock, &socket_type)); } static pic_value @@ -185,7 +185,7 @@ pic_socket_socket_accept(pic_state *pic) new_sock = pic_malloc(pic, sizeof(struct pic_socket_t)); new_sock->fd = fd; - return pic_obj_value(pic_data_alloc(pic, &socket_type, new_sock)); + return pic_obj_value(pic_data_value(pic, new_sock, &socket_type)); } static pic_value diff --git a/docs/capi.rst b/docs/capi.rst index c427515e..6e701fb9 100644 --- a/docs/capi.rst +++ b/docs/capi.rst @@ -89,7 +89,7 @@ When you use dynamic memory allocation inside C APIs, you must be caseful about f = create_foo(); - data = pic_data_alloc(pic, &foo_type, md); + data = pic_data_value(pic, md, &foo_type); return pic_obj_value(data); } diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 8dac1bcd..80ce6c54 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -124,7 +124,7 @@ pic_make_cont(pic_state *pic, struct pic_cont *cont) struct pic_proc *c; /* save the escape continuation in proc */ - c = pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_obj_value(pic_data_alloc(pic, &cont_type, cont))); + c = pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_obj_value(pic_data_value(pic, cont, &cont_type))); return c; } diff --git a/extlib/benz/data.c b/extlib/benz/data.c index a570b6be..e2402a2d 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -1,13 +1,2 @@ #include "picrin.h" -struct pic_data * -pic_data_alloc(pic_state *pic, const pic_data_type *type, void *userdata) -{ - struct pic_data *data; - - data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TYPE_DATA); - data->type = type; - data->data = userdata; - - return data; -} diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 6ef57678..7bde1990 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -11,18 +11,18 @@ pic_get_backtrace(pic_state *pic) pic_callinfo *ci; struct pic_string *trace; - trace = pic_make_lit(pic, ""); + trace = pic_lit_value(pic, ""); for (ci = pic->ci; ci != pic->cibase; --ci) { struct pic_proc *proc = pic_proc_ptr(ci->fp[0]); - trace = pic_str_cat(pic, trace, pic_make_lit(pic, " at ")); - trace = pic_str_cat(pic, trace, pic_make_lit(pic, "(anonymous lambda)")); + trace = pic_str_cat(pic, trace, pic_lit_value(pic, " at ")); + trace = pic_str_cat(pic, trace, pic_lit_value(pic, "(anonymous lambda)")); if (pic_proc_func_p(proc)) { - trace = pic_str_cat(pic, trace, pic_make_lit(pic, " (native function)\n")); + trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (native function)\n")); } else if (pic_proc_irep_p(proc)) { - trace = pic_str_cat(pic, trace, pic_make_lit(pic, " (unknown location)\n")); /* TODO */ + trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (unknown location)\n")); /* TODO */ } } @@ -58,6 +58,6 @@ pic_print_backtrace(pic_state *pic, xFILE *file) } xfprintf(pic, file, "\n"); - xfputs(pic, pic_str_cstr(pic, e->stack), file); + xfputs(pic, pic_str(pic, e->stack), file); } } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index b416ed18..39c2e9d9 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -24,10 +24,10 @@ pic_warnf(pic_state *pic, const char *fmt, ...) struct pic_string *err; va_start(ap, fmt); - err = pic_vformat(pic, fmt, ap); + err = pic_vstrf_value(pic, fmt, ap); va_end(ap); - xfprintf(pic, pic_stderr(pic)->file, "warn: %s\n", pic_str_cstr(pic, err)); + xfprintf(pic, pic_stderr(pic)->file, "warn: %s\n", pic_str(pic, err)); } void @@ -38,10 +38,10 @@ pic_errorf(pic_state *pic, const char *fmt, ...) struct pic_string *err; va_start(ap, fmt); - err = pic_vformat(pic, fmt, ap); + err = pic_vstrf_value(pic, fmt, ap); va_end(ap); - msg = pic_str_cstr(pic, err); + msg = pic_str(pic, err); pic_error(pic, msg, pic_nil_value(pic)); } @@ -100,7 +100,7 @@ pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs) e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); e->type = type; - e->msg = pic_make_cstr(pic, msg); + e->msg = pic_cstr_value(pic, msg); e->irrs = irrs; e->stack = stack; diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index e0c53e81..0e4ef903 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -702,7 +702,7 @@ pic_alloca(pic_state *pic, size_t n) static const pic_data_type t = { "pic_alloca", pic_free, 0 }; /* TODO: optimize */ - return pic_data_alloc(pic, &t, pic_malloc(pic, n))->data; + return pic_data_value(pic, pic_malloc(pic, n), &t)->data; } struct pic_object * diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 6515bac7..7744f27b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -61,6 +61,7 @@ struct pic_proc; struct pic_port; struct pic_error; struct pic_env; +struct pic_data; typedef struct pic_symbol pic_sym; typedef struct pic_id pic_id; @@ -165,6 +166,20 @@ pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv) #define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT) #define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL) +int pic_int(pic_state *, pic_value); +double pic_float(pic_state *, pic_value); +char pic_char(pic_state *, pic_value); +bool pic_bool(pic_state *, pic_value); +const char *pic_str(pic_state *, struct pic_string *); +unsigned char *pic_blob(pic_state *, struct pic_blob *, int *len); +void *pic_data(pic_state *, struct pic_data *); + +typedef struct { + const char *type_name; + void (*dtor)(pic_state *, void *); + void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value)); +} pic_data_type; + pic_value pic_undef_value(pic_state *); pic_value pic_int_value(pic_state *, int); pic_value pic_float_value(pic_state *, double); @@ -173,14 +188,13 @@ pic_value pic_true_value(pic_state *); pic_value pic_false_value(pic_state *); pic_value pic_bool_value(pic_state *, bool); pic_value pic_eof_object(pic_state *); - -int pic_int(pic_state *, pic_value); -double pic_float(pic_state *, pic_value); -char pic_char(pic_state *, pic_value); -bool pic_bool(pic_state *, pic_value); -/* const char *pic_str(pic_state *, pic_value); */ -/* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ -/* void *pic_data(pic_state *, pic_value); */ +struct pic_string *pic_str_value(pic_state *, const char *str, int len); +#define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr)) +#define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1)) +struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...); +struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); +struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len); +struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); int pic_type(pic_state *, pic_value); const char *pic_typename(pic_state *, int); @@ -227,10 +241,10 @@ bool pic_weak_has(pic_state *, struct pic_weak *, void *); /* symbol */ pic_sym *pic_intern(pic_state *, struct pic_string *); -#define pic_intern_str(pic,s,i) pic_intern(pic, pic_make_str(pic, (s), (i))) -#define pic_intern_cstr(pic,s) pic_intern(pic, pic_make_cstr(pic, (s))) -#define pic_intern_lit(pic,lit) pic_intern(pic, pic_make_lit(pic, lit)) -const char *pic_symbol_name(pic_state *, pic_sym *); +#define pic_intern_str(pic,s,i) pic_intern(pic, pic_str_value(pic, (s), (i))) +#define pic_intern_cstr(pic,s) pic_intern(pic, pic_cstr_value(pic, (s))) +#define pic_intern_lit(pic,lit) pic_intern(pic, pic_lit_value(pic, lit)) +struct pic_string *pic_sym_name(pic_state *, pic_sym *); /* string */ int pic_str_len(pic_state *, struct pic_string *); diff --git a/extlib/benz/include/picrin/data.h b/extlib/benz/include/picrin/data.h index 6a701faa..60fe6dc2 100644 --- a/extlib/benz/include/picrin/data.h +++ b/extlib/benz/include/picrin/data.h @@ -9,12 +9,6 @@ extern "C" { #endif -typedef struct { - const char *type_name; - void (*dtor)(pic_state *, void *); - void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value)); -} pic_data_type; - struct pic_data { PIC_OBJECT_HEADER const pic_data_type *type; @@ -27,8 +21,6 @@ PIC_INLINE bool pic_data_type_p(pic_state *pic, const pic_value obj, const pic_d return pic_data_p(pic, obj) && pic_data_ptr(obj)->type == type; } -struct pic_data *pic_data_alloc(pic_state *, const pic_data_type *, void *); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/string.h b/extlib/benz/include/picrin/string.h index 117fc24e..7d4eeab0 100644 --- a/extlib/benz/include/picrin/string.h +++ b/extlib/benz/include/picrin/string.h @@ -19,15 +19,6 @@ void pic_rope_decref(pic_state *, struct pic_rope *); #define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) -struct pic_string *pic_make_str(pic_state *, const char *, int); -#define pic_make_cstr(pic, cstr) pic_make_str(pic, (cstr), strlen(cstr)) -#define pic_make_lit(pic, lit) pic_make_str(pic, "" lit, -((int)sizeof lit - 1)) - -const char *pic_str_cstr(pic_state *, struct pic_string *); - -struct pic_string *pic_format(pic_state *, const char *, ...); -struct pic_string *pic_vformat(pic_state *, const char *, va_list); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index d47051f5..311f6ed5 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -61,11 +61,11 @@ pic_make_library(pic_state *pic, const char *lib) old_lib = pic_current_library(pic); } - name = pic_make_cstr(pic, lib); + name = pic_cstr_value(pic, lib); env = make_library_env(pic, name); exports = pic_make_dict(pic); - it = kh_put(ltable, h, pic_str_cstr(pic, name), &ret); + it = kh_put(ltable, h, pic_str(pic, name), &ret); if (ret == 0) { /* if exists */ pic_errorf(pic, "library name already in use: %s", lib); } @@ -94,7 +94,7 @@ pic_find_library(pic_state *pic, const char *lib) const char * pic_current_library(pic_state *pic) { - return pic_str_cstr(pic, pic->lib->name); + return pic_str(pic, pic->lib->name); } struct pic_env * diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 1df08a07..8a65f594 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -42,9 +42,9 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) name = pic_identifier_name(pic, id); if (env->up == NULL && pic_sym_p(pic, pic_obj_value(id))) { /* toplevel & public */ - str = pic_format(pic, "%s/%s", pic_str_cstr(pic, env->lib), name); + str = pic_strf_value(pic, "%s/%s", pic_str(pic, env->lib), name); } else { - str = pic_format(pic, ".%s.%d", name, pic->ucnt++); + str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); } uid = pic_intern(pic, str); diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 580e481e..423c8287 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -234,7 +234,7 @@ pic_number_number_to_string(pic_state *pic) number_string(ival, radix, ilen, buf); - str = pic_make_str(pic, buf, s - 1); + str = pic_str_value(pic, buf, s - 1); pic_free(pic, buf); } diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 4ad2251c..5647f76f 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -108,7 +108,7 @@ pic_open_file(pic_state *pic, const char *name, int flags) { mode = 'w'; } if ((file = file_open(pic, name, &mode)) == NULL) { - file_error(pic, pic_str_cstr(pic, pic_format(pic, "could not open file '%s'", name))); + file_error(pic, pic_str(pic, pic_strf_value(pic, "could not open file '%s'", name))); } port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); @@ -298,7 +298,7 @@ pic_get_output_string(pic_state *pic, struct pic_port *port) s = port->file->vtable.cookie; - return pic_make_str(pic, s->buf, s->end); + return pic_str_value(pic, s->buf, s->end); } void diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 0430c661..c77c9a9a 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -141,7 +141,7 @@ pic_get_args(pic_state *pic, const char *format, ...) } VAL_CASE('c', char, char, pic_char(pic, v)) - VAL_CASE('z', str, const char *, pic_str_cstr(pic, pic_str_ptr(v))) + VAL_CASE('z', str, const char *, pic_str(pic, pic_str_ptr(v))) #define PTR_CASE(c, type, ctype) \ VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) @@ -179,7 +179,7 @@ static pic_value vm_gref(pic_state *pic, pic_sym *uid) { if (! pic_weak_has(pic, pic->globals, uid)) { - pic_errorf(pic, "uninitialized global variable: %s", pic_symbol_name(pic, uid)); + pic_errorf(pic, "uninitialized global variable: %s", pic_str(pic, pic_sym_name(pic, uid))); } return pic_weak_ref(pic, pic->globals, uid); } @@ -308,12 +308,12 @@ pic_vm_tear_off(pic_state *pic) puts(")"); \ if (! pic_proc_func_p(proc)) { \ printf(" irep = %p\n", proc->u.i.irep); \ - printf(" name = %s\n", pic_symbol_name(pic, pic_proc_name(proc))); \ + printf(" name = %s\n", pic_str(pic, pic_sym_name(pic, pic_proc_name(proc)))); \ pic_dump_irep(proc->u.i.irep); \ } \ else { \ printf(" cfunc = %p\n", (void *)proc->u.f.func); \ - printf(" name = %s\n", pic_symbol_name(pic, pic_proc_name(proc))); \ + printf(" name = %s\n", pic_str(pic, pic_sym_name(pic, pic_proc_name(proc)))); \ } \ puts("== end\n"); \ } while (0) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 59aee417..c4982a0b 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -282,7 +282,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) } if (idx >= ATOF_BUF_SIZE) read_error(pic, "number too large", - pic_obj_value(pic_make_str(pic, (const char *)buf, ATOF_BUF_SIZE))); + pic_obj_value(pic_str_value(pic, (const char *)buf, ATOF_BUF_SIZE))); if (! isdelim(c)) read_error(pic, "non-delimiter character given after number", pic_list1(pic, pic_char_value(pic, c))); @@ -321,10 +321,10 @@ read_minus(pic_state *pic, struct pic_port *port, int c) } else { sym = read_symbol(pic, port, c); - if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "-inf.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-inf.0")) { return pic_float_value(pic, -(1.0 / 0.0)); } - if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "-nan.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-nan.0")) { return pic_float_value(pic, -(0.0 / 0.0)); } return sym; @@ -341,10 +341,10 @@ read_plus(pic_state *pic, struct pic_port *port, int c) } else { sym = read_symbol(pic, port, c); - if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "+inf.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+inf.0")) { return pic_float_value(pic, 1.0 / 0.0); } - if (strcaseeq(pic_symbol_name(pic, pic_sym_ptr(sym)), "+nan.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+nan.0")) { return pic_float_value(pic, 0.0 / 0.0); } return sym; @@ -444,7 +444,7 @@ read_string(pic_state *pic, struct pic_port *port, int c) } buf[cnt] = '\0'; - str = pic_make_str(pic, buf, cnt); + str = pic_str_value(pic, buf, cnt); pic_free(pic, buf); return pic_obj_value(str); } diff --git a/extlib/benz/string.c b/extlib/benz/string.c index e29ffb32..90103455 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -94,7 +94,7 @@ pic_make_rope(pic_state *pic, struct pic_chunk *c) } static struct pic_string * -pic_make_string(pic_state *pic, struct pic_rope *rope) +pic_str_valueing(pic_state *pic, struct pic_rope *rope) { struct pic_string *str; @@ -237,7 +237,7 @@ rope_cstr(pic_state *pic, struct pic_rope *x) } struct pic_string * -pic_make_str(pic_state *pic, const char *str, int len) +pic_str_value(pic_state *pic, const char *str, int len) { struct pic_chunk *c; @@ -249,7 +249,7 @@ pic_make_str(pic_state *pic, const char *str, int len) } c = pic_make_chunk_lit(pic, str, -len); } - return pic_make_string(pic, pic_make_rope(pic, c)); + return pic_str_valueing(pic, pic_make_rope(pic, c)); } int @@ -273,19 +273,19 @@ pic_str_ref(pic_state *pic, struct pic_string *str, int i) struct pic_string * pic_str_cat(pic_state *pic, struct pic_string *a, struct pic_string *b) { - return pic_make_string(pic, rope_cat(pic, a->rope, b->rope)); + return pic_str_valueing(pic, rope_cat(pic, a->rope, b->rope)); } struct pic_string * pic_str_sub(pic_state *pic, struct pic_string *str, int s, int e) { - return pic_make_string(pic, rope_sub(pic, str->rope, s, e)); + return pic_str_valueing(pic, rope_sub(pic, str->rope, s, e)); } int pic_str_cmp(pic_state *pic, struct pic_string *str1, struct pic_string *str2) { - return strcmp(pic_str_cstr(pic, str1), pic_str_cstr(pic, str2)); + return strcmp(pic_str(pic, str1), pic_str(pic, str2)); } int @@ -294,7 +294,7 @@ pic_str_hash(pic_state *pic, struct pic_string *str) const char *s; int h = 0; - s = pic_str_cstr(pic, str); + s = pic_str(pic, str); while (*s) { h = (h << 5) - h + *s++; } @@ -302,7 +302,7 @@ pic_str_hash(pic_state *pic, struct pic_string *str) } const char * -pic_str_cstr(pic_state *pic, struct pic_string *str) +pic_str(pic_state *pic, struct pic_string *str) { return rope_cstr(pic, str->rope); } @@ -374,7 +374,7 @@ pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) } struct pic_string * -pic_vformat(pic_state *pic, const char *fmt, va_list ap) +pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap) { struct pic_port *port; struct pic_string *str; @@ -389,13 +389,13 @@ pic_vformat(pic_state *pic, const char *fmt, va_list ap) } struct pic_string * -pic_format(pic_state *pic, const char *fmt, ...) +pic_strf_value(pic_state *pic, const char *fmt, ...) { va_list ap; struct pic_string *str; va_start(ap, fmt); - str = pic_vformat(pic, fmt, ap); + str = pic_vstrf_value(pic, fmt, ap); va_end(ap); return str; @@ -428,7 +428,7 @@ pic_str_string(pic_state *pic) buf[i] = pic_char(pic, argv[i]); } - str = pic_make_str(pic, buf, argc); + str = pic_str_value(pic, buf, argc); pic_free(pic, buf); return pic_obj_value(str); @@ -447,7 +447,7 @@ pic_str_make_string(pic_state *pic) buf = pic_malloc(pic, len); memset(buf, c, len); - ret = pic_obj_value(pic_make_str(pic, buf, len)); + ret = pic_obj_value(pic_str_value(pic, buf, len)); pic_free(pic, buf); return ret; @@ -536,7 +536,7 @@ pic_str_string_append(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - str = pic_make_lit(pic, ""); + str = pic_lit_value(pic, ""); for (i = 0; i < argc; ++i) { if (! pic_str_p(pic, argv[i])) { pic_errorf(pic, "type error"); @@ -583,7 +583,7 @@ pic_str_string_map(pic_state *pic) pic_assert_type(pic, val, char); buf[i] = pic_char(pic, val); } - str = pic_make_str(pic, buf, len); + str = pic_str_value(pic, buf, len); } pic_catch { pic_free(pic, buf); @@ -640,7 +640,7 @@ pic_str_list_to_string(pic_state *pic) pic_get_args(pic, "o", &list); if (pic_length(pic, list) == 0) { - return pic_obj_value(pic_make_lit(pic, "")); + return pic_obj_value(pic_lit_value(pic, "")); } buf = pic_malloc(pic, pic_length(pic, list)); @@ -653,7 +653,7 @@ pic_str_list_to_string(pic_state *pic) buf[i++] = pic_char(pic, e); } - str = pic_make_str(pic, buf, i); + str = pic_str_value(pic, buf, i); } pic_catch { pic_free(pic, buf); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 51e9cfbc..c1cc75cc 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -44,10 +44,10 @@ pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env) return nid; } -const char * -pic_symbol_name(pic_state *pic, pic_sym *sym) +struct pic_string * +pic_sym_name(pic_state PIC_UNUSED(*pic), pic_sym *sym) { - return pic_str_cstr(pic, sym->str); + return sym->str; } const char * @@ -57,7 +57,7 @@ pic_identifier_name(pic_state *pic, pic_id *id) id = id->u.id.id; } - return pic_symbol_name(pic, (pic_sym *)id); + return pic_str(pic, pic_sym_name(pic, (pic_sym *)id)); } static pic_value diff --git a/extlib/benz/value.c b/extlib/benz/value.c index e0857dfb..b0339702 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -72,3 +72,21 @@ pic_typename(pic_state *pic, int type) pic_errorf(pic, "pic_typename: invalid type given %d", type); } } + +void * +pic_data(pic_state PIC_UNUSED(*pic), struct pic_data *data) +{ + return data->data; +} + +struct pic_data * +pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type) +{ + struct pic_data *data; + + data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TYPE_DATA); + data->type = type; + data->data = userdata; + + return data; +} diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 1aa87e76..358ad54e 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -346,7 +346,7 @@ pic_vec_vector_to_string(pic_state *pic) buf[i - start] = pic_char(pic, vec->data[i]); } - str = pic_make_str(pic, buf, end - start); + str = pic_str_value(pic, buf, end - start); pic_free(pic, buf); return pic_obj_value(str); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 426f9804..73bba09e 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -85,10 +85,10 @@ static void write_str(pic_state *pic, struct pic_string *str, xFILE *file, int mode) { int i; - const char *cstr = pic_str_cstr(pic, str); + const char *cstr = pic_str(pic, str); if (mode == DISPLAY_MODE) { - xfprintf(pic, file, "%s", pic_str_cstr(pic, str)); + xfprintf(pic, file, "%s", pic_str(pic, str)); return; } xfprintf(pic, file, "\""); @@ -246,7 +246,7 @@ write_dict(struct writer_control *p, struct pic_dict *dict) xfprintf(pic, file, "#.(dictionary"); pic_dict_for_each (sym, dict, it) { - xfprintf(pic, file, " '%s ", pic_symbol_name(pic, sym)); + xfprintf(pic, file, " '%s ", pic_str(pic, pic_sym_name(pic, sym))); write_core(p, pic_dict_ref(pic, dict, sym)); } xfprintf(pic, file, ")"); @@ -298,7 +298,7 @@ write_core(struct writer_control *p, pic_value obj) write_float(pic, pic_float(pic, obj), file); break; case PIC_TYPE_SYMBOL: - xfprintf(pic, file, "%s", pic_symbol_name(pic, pic_sym_ptr(obj))); + xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, pic_sym_ptr(obj)))); break; case PIC_TYPE_BLOB: write_blob(pic, pic_blob_ptr(obj), file); @@ -440,11 +440,11 @@ pic_printf(pic_state *pic, const char *fmt, ...) va_start(ap, fmt); - str = pic_vformat(pic, fmt, ap); + str = pic_vstrf_value(pic, fmt, ap); va_end(ap); - xfprintf(pic, file, "%s", pic_str_cstr(pic, str)); + xfprintf(pic, file, "%s", pic_str(pic, str)); xfflush(pic, file); } From 126989e4ec274e4443a745422b5395c049caed2b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 23:52:20 +0900 Subject: [PATCH 025/119] cleanup --- extlib/benz/include/picrin.h | 62 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 7744f27b..a73bccd6 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -117,6 +117,36 @@ pic_value pic_vcall(pic_state *, struct pic_proc *proc, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *proc, int n, pic_value *argv); pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv); +PIC_INLINE int pic_int(pic_state *, pic_value); +PIC_INLINE double pic_float(pic_state *, pic_value); +PIC_INLINE char pic_char(pic_state *, pic_value); +#define pic_bool(pic,v) (! pic_false_p(pic, v)) +const char *pic_str(pic_state *, struct pic_string *); +unsigned char *pic_blob(pic_state *, struct pic_blob *, int *len); +void *pic_data(pic_state *, struct pic_data *); + +typedef struct { + const char *type_name; + void (*dtor)(pic_state *, void *); + void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value)); +} pic_data_type; + +PIC_INLINE pic_value pic_undef_value(pic_state *); +PIC_INLINE pic_value pic_int_value(pic_state *, int); +PIC_INLINE pic_value pic_float_value(pic_state *, double); +PIC_INLINE pic_value pic_char_value(pic_state *, char); +PIC_INLINE pic_value pic_true_value(pic_state *); +PIC_INLINE pic_value pic_false_value(pic_state *); +PIC_INLINE pic_value pic_bool_value(pic_state *, bool); +PIC_INLINE pic_value pic_eof_object(pic_state *); +struct pic_string *pic_str_value(pic_state *, const char *str, int len); +#define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr)) +#define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1)) +struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...); +struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); +struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len); +struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); + #define PIC_TYPE_INVALID 1 #define PIC_TYPE_FLOAT 2 #define PIC_TYPE_INT 3 @@ -145,8 +175,6 @@ pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv) #define PIC_TYPE_CXT 30 #define PIC_TYPE_CP 31 -#include "picrin/type.h" - #define pic_undef_p(pic,v) (pic_type(pic,v) == PIC_TYPE_UNDEF) #define pic_int_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INT) #define pic_float_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FLOAT) @@ -166,35 +194,7 @@ pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv) #define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT) #define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL) -int pic_int(pic_state *, pic_value); -double pic_float(pic_state *, pic_value); -char pic_char(pic_state *, pic_value); -bool pic_bool(pic_state *, pic_value); -const char *pic_str(pic_state *, struct pic_string *); -unsigned char *pic_blob(pic_state *, struct pic_blob *, int *len); -void *pic_data(pic_state *, struct pic_data *); - -typedef struct { - const char *type_name; - void (*dtor)(pic_state *, void *); - void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value)); -} pic_data_type; - -pic_value pic_undef_value(pic_state *); -pic_value pic_int_value(pic_state *, int); -pic_value pic_float_value(pic_state *, double); -pic_value pic_char_value(pic_state *, char); -pic_value pic_true_value(pic_state *); -pic_value pic_false_value(pic_state *); -pic_value pic_bool_value(pic_state *, bool); -pic_value pic_eof_object(pic_state *); -struct pic_string *pic_str_value(pic_state *, const char *str, int len); -#define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr)) -#define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1)) -struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...); -struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); -struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len); -struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); +#include "picrin/type.h" int pic_type(pic_state *, pic_value); const char *pic_typename(pic_state *, int); From 0a715e491661d943293064addf00264e11e7d22d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 23:59:33 +0900 Subject: [PATCH 026/119] use pic_blob_value --- contrib/40.srfi/src/106.c | 3 +-- extlib/benz/blob.c | 15 +++++++++------ extlib/benz/include/picrin/blob.h | 2 -- extlib/benz/port.c | 5 ++--- extlib/benz/read.c | 7 ++----- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index fcd4e6f9..2ccb0c21 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -263,8 +263,7 @@ pic_socket_socket_recv(pic_state *pic) pic_errorf(pic, "%s", strerror(errno)); } - bv = pic_make_blob(pic, len); - memcpy(bv->data, buf, len); + bv = pic_blob_value(pic, buf, len); free(buf); return pic_obj_value(bv); diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index 9d1161f0..a7c053f5 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -5,13 +5,16 @@ #include "picrin.h" struct pic_blob * -pic_make_blob(pic_state *pic, int len) +pic_blob_value(pic_state *pic, const unsigned char *buf, int len) { struct pic_blob *bv; bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TYPE_BLOB); bv->data = pic_malloc(pic, len); bv->len = len; + if (buf) { + memcpy(bv->data, buf, len); + } return bv; } @@ -35,7 +38,7 @@ pic_blob_bytevector(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - blob = pic_make_blob(pic, argc); + blob = pic_blob_value(pic, 0, argc); data = blob->data; @@ -63,7 +66,7 @@ pic_blob_make_bytevector(pic_state *pic) if (b < 0 || b > 255) pic_errorf(pic, "byte out of range"); - blob = pic_make_blob(pic, k); + blob = pic_blob_value(pic, 0, k); for (i = 0; i < k; ++i) { blob->data[i] = (unsigned char)b; } @@ -157,7 +160,7 @@ pic_blob_bytevector_copy(pic_state *pic) pic_errorf(pic, "make-bytevector: end index must not be less than start index"); } - to = pic_make_blob(pic, end - start); + to = pic_blob_value(pic, 0, end - start); while (start < end) { to->data[i++] = from->data[start++]; } @@ -180,7 +183,7 @@ pic_blob_bytevector_append(pic_state *pic) len += pic_blob_ptr(argv[i])->len; } - blob = pic_make_blob(pic, len); + blob = pic_blob_value(pic, 0, len); len = 0; for (i = 0; i < argc; ++i) { @@ -202,7 +205,7 @@ pic_blob_list_to_bytevector(pic_state *pic) pic_get_args(pic, "o", &list); - blob = pic_make_blob(pic, pic_length(pic, list)); + blob = pic_blob_value(pic, 0, pic_length(pic, list)); data = blob->data; diff --git a/extlib/benz/include/picrin/blob.h b/extlib/benz/include/picrin/blob.h index e75051d6..68281e82 100644 --- a/extlib/benz/include/picrin/blob.h +++ b/extlib/benz/include/picrin/blob.h @@ -17,8 +17,6 @@ struct pic_blob { #define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) -struct pic_blob *pic_make_blob(pic_state *, int); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 5647f76f..19886305 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -546,8 +546,7 @@ pic_port_get_output_bytevector(pic_state *pic) s = port->file->vtable.cookie; - blob = pic_make_blob(pic, s->end); - memcpy(blob->data, s->buf, s->end); + blob = pic_blob_value(pic, (unsigned char *)s->buf, s->end); return pic_obj_value(blob); } @@ -717,7 +716,7 @@ pic_port_read_blob(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-bytevector"); - blob = pic_make_blob(pic, k); + blob = pic_blob_value(pic, 0, k); i = xfread(pic, blob->data, sizeof(char), k, port->file); if (i == 0) { diff --git a/extlib/benz/read.c b/extlib/benz/read.c index c4982a0b..172e02db 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -497,7 +497,7 @@ static pic_value read_blob(pic_state *pic, struct pic_port *port, int c) { int nbits, n; - int len, i; + int len; unsigned char *dat; struct pic_blob *blob; @@ -529,10 +529,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) c = next(pic, port); } - blob = pic_make_blob(pic, len); - for (i = 0; i < len; ++i) { - blob->data[i] = dat[i]; - } + blob = pic_blob_value(pic, dat, len); pic_free(pic, dat); return pic_obj_value(blob); From 3dd8290dd30200d3364d7112106d730575ce10d3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:03:34 +0900 Subject: [PATCH 027/119] add picrin/object.h --- extlib/benz/gc.c | 1 + extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/{weak.h => object.h} | 5 +++-- extlib/benz/var.c | 1 + extlib/benz/weak.c | 1 + 5 files changed, 6 insertions(+), 3 deletions(-) rename extlib/benz/include/picrin/{weak.h => object.h} (87%) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 0e4ef903..922968d5 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" union header { struct { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index a73bccd6..47d5b16e 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -271,7 +271,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/string.h" #include "picrin/symbol.h" #include "picrin/vector.h" -#include "picrin/weak.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/weak.h b/extlib/benz/include/picrin/object.h similarity index 87% rename from extlib/benz/include/picrin/weak.h rename to extlib/benz/include/picrin/object.h index 914865b8..bef9590f 100644 --- a/extlib/benz/include/picrin/weak.h +++ b/extlib/benz/include/picrin/object.h @@ -2,8 +2,8 @@ * See Copyright Notice in picrin.h */ -#ifndef PICRIN_WEAK_H -#define PICRIN_WEAK_H +#ifndef PICRIN_OBJECT_H +#define PICRIN_OBJECT_H #if defined(__cplusplus) extern "C" { @@ -19,6 +19,7 @@ struct pic_weak { #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 1965db92..5b132a06 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" static pic_value var_get(pic_state *pic, struct pic_proc *var) diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 484bcbd8..86821dae 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" KHASH_DEFINE(weak, void *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) From 645e5bee3a35e554006ccc86110282511a16f7fb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:08:39 +0900 Subject: [PATCH 028/119] remove vector.h --- extlib/benz/bool.c | 1 + extlib/benz/cont.c | 1 + extlib/benz/eval.c | 1 + extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/object.h | 14 ++++++++++++++ extlib/benz/include/picrin/vector.h | 24 ------------------------ extlib/benz/proc.c | 1 + extlib/benz/read.c | 1 + extlib/benz/vector.c | 1 + extlib/benz/write.c | 1 + 10 files changed, 21 insertions(+), 25 deletions(-) delete mode 100644 extlib/benz/include/picrin/vector.h diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index ff7d4a31..19c856cb 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" #if PIC_NAN_BOXING diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 80ce6c54..4a43c9cf 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" void pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 371651c6..43992d2e 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" #include "picrin/opcode.h" static pic_value diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 47d5b16e..606af64f 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -270,7 +270,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/record.h" #include "picrin/string.h" #include "picrin/symbol.h" -#include "picrin/vector.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index bef9590f..b362f95c 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -9,6 +9,20 @@ extern "C" { #endif + +/* vector */ + +struct pic_vector { + PIC_OBJECT_HEADER + pic_value *data; + int len; +}; + +#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) + + +/* weak */ + KHASH_DECLARE(weak, void *, pic_value) struct pic_weak { diff --git a/extlib/benz/include/picrin/vector.h b/extlib/benz/include/picrin/vector.h deleted file mode 100644 index bb5ddad1..00000000 --- a/extlib/benz/include/picrin/vector.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_VECTOR_H -#define PICRIN_VECTOR_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_vector { - PIC_OBJECT_HEADER - pic_value *data; - int len; -}; - -#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index c77c9a9a..b8b54339 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" #include "picrin/opcode.h" #define MIN(x,y) ((x) < (y) ? (x) : (y)) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 172e02db..84738f2c 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" KHASH_DEFINE(read, int, pic_value, kh_int_hash_func, kh_int_hash_equal) diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 358ad54e..965d46e3 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" struct pic_vector * pic_make_vec(pic_state *pic, int len) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 73bba09e..9e3e2a50 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" KHASH_DECLARE(l, void *, int) KHASH_DECLARE(v, void *, int) From cfc32a1b152f8bd000b99b8683862e9d77fce546 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:14:50 +0900 Subject: [PATCH 029/119] remove string.h --- contrib/20.r7rs/src/mutable-string.c | 1 + contrib/40.srfi/src/106.c | 12 +++--------- contrib/40.srfi/srfi/106.scm | 2 +- extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/object.h | 13 +++++++++++++ extlib/benz/include/picrin/string.h | 10 ---------- extlib/benz/string.c | 1 + 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index b00842df..0f3bcfe6 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/object.h" void pic_str_set(pic_state *pic, struct pic_string *str, int i, char c) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 2ccb0c21..ef31ec43 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -69,22 +69,16 @@ pic_socket_socket_p(pic_state *pic) static pic_value pic_socket_make_socket(pic_state *pic) { - pic_value n, s; const char *node, *service; int family, socktype, flags, protocol; int result; struct addrinfo hints, *ai, *it; struct pic_socket_t *sock; - pic_get_args(pic, "ooiiii", &n, &s, &family, &socktype, &flags, &protocol); + pic_get_args(pic, "zziiii", &node, &service, &family, &socktype, &flags, &protocol); - node = service = NULL; - if (pic_str_p(pic, n)) { - node = pic_str(pic, pic_str_ptr(n)); - } - if (pic_str_p(pic, s)) { - service = pic_str(pic, pic_str_ptr(s)); - } + if (strlen(node) == 0) node = NULL; + if (strlen(service) == 0) service = NULL; sock = pic_malloc(pic, sizeof(struct pic_socket_t)); sock->fd = -1; diff --git a/contrib/40.srfi/srfi/106.scm b/contrib/40.srfi/srfi/106.scm index e224b603..a9ac0408 100644 --- a/contrib/40.srfi/srfi/106.scm +++ b/contrib/40.srfi/srfi/106.scm @@ -34,7 +34,7 @@ (type *sock-stream*) (flags *ai-passive*) (protocol *ipproto-ip*)) - (make-socket #f service family type flags protocol))) + (make-socket "" service family type flags protocol))) (define %address-family `((inet . ,*af-inet*) (inet6 . ,*af-inet6*) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 606af64f..9566cd7b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -268,7 +268,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/port.h" #include "picrin/proc.h" #include "picrin/record.h" -#include "picrin/string.h" #include "picrin/symbol.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index b362f95c..ac7615e5 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -10,6 +10,19 @@ extern "C" { #endif +/* string */ + +struct pic_string { + PIC_OBJECT_HEADER + struct pic_rope *rope; +}; + +void pic_rope_incref(pic_state *, struct pic_rope *); +void pic_rope_decref(pic_state *, struct pic_rope *); + +#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) + + /* vector */ struct pic_vector { diff --git a/extlib/benz/include/picrin/string.h b/extlib/benz/include/picrin/string.h index 7d4eeab0..ac5d2ce8 100644 --- a/extlib/benz/include/picrin/string.h +++ b/extlib/benz/include/picrin/string.h @@ -9,16 +9,6 @@ extern "C" { #endif -struct pic_string { - PIC_OBJECT_HEADER - struct pic_rope *rope; -}; - -void pic_rope_incref(pic_state *, struct pic_rope *); -void pic_rope_decref(pic_state *, struct pic_rope *); - -#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 90103455..8027a36e 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" struct pic_chunk { char *str; From aa2121b61c96635e461260890b26cabf5eff3a9a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:20:15 +0900 Subject: [PATCH 030/119] remove blob.h --- contrib/40.srfi/src/106.c | 6 ++---- extlib/benz/blob.c | 8 ++++++++ extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/blob.h | 8 -------- extlib/benz/include/picrin/object.h | 10 ++++++++++ extlib/benz/port.c | 1 + 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index ef31ec43..2c3e045d 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -188,8 +188,7 @@ pic_socket_socket_send(pic_state *pic) pic_value obj; struct pic_blob *bv; const unsigned char *cursor; - int flags = 0; - size_t remain, written; + int flags = 0, remain, written; struct pic_socket_t *sock; pic_get_args(pic, "ob|i", &obj, &bv, &flags); @@ -198,8 +197,7 @@ pic_socket_socket_send(pic_state *pic) sock = pic_socket_data_ptr(obj); ensure_socket_is_open(pic, sock); - cursor = bv->data; - remain = bv->len; + cursor = pic_blob(pic, bv, &remain); written = 0; errno = 0; while (remain > 0) { diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index a7c053f5..6cc1eaac 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" struct pic_blob * pic_blob_value(pic_state *pic, const unsigned char *buf, int len) @@ -18,6 +19,13 @@ pic_blob_value(pic_state *pic, const unsigned char *buf, int len) return bv; } +unsigned char * +pic_blob(pic_state PIC_UNUSED(*pic), struct pic_blob *blob, int *len) +{ + *len = blob->len; + return blob->data; +} + static pic_value pic_blob_bytevector_p(pic_state *pic) { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 9566cd7b..ab3c9796 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -258,7 +258,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/state.h" -#include "picrin/blob.h" #include "picrin/cont.h" #include "picrin/data.h" #include "picrin/dict.h" diff --git a/extlib/benz/include/picrin/blob.h b/extlib/benz/include/picrin/blob.h index 68281e82..24ca9e2c 100644 --- a/extlib/benz/include/picrin/blob.h +++ b/extlib/benz/include/picrin/blob.h @@ -9,14 +9,6 @@ extern "C" { #endif -struct pic_blob { - PIC_OBJECT_HEADER - unsigned char *data; - int len; -}; - -#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index ac7615e5..6a5d5b16 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -10,6 +10,16 @@ extern "C" { #endif +/* blob */ + +struct pic_blob { + PIC_OBJECT_HEADER + unsigned char *data; + int len; +}; + +#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) + /* string */ struct pic_string { diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 19886305..5169978b 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" static pic_value pic_assert_port(pic_state *pic) From 387ba469c8030b8c3019a484db7ab19f176d5337 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:39:13 +0900 Subject: [PATCH 031/119] add pic_dict_next --- extlib/benz/dict.c | 34 +++++++++++++++++++++++-------- extlib/benz/gc.c | 7 ++++--- extlib/benz/include/picrin.h | 1 + extlib/benz/include/picrin/dict.h | 6 ------ extlib/benz/lib.c | 11 +++++----- extlib/benz/write.c | 18 ++++++++-------- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 39dcacf6..80aed488 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -66,6 +66,23 @@ pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym *key) kh_del(dict, h, it); } +bool +pic_dict_next(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, int *iter, pic_sym **key, pic_value *val) +{ + khash_t(dict) *h = &dict->hash; + int it = *iter; + + for (it = *iter; it != kh_end(h); ++it) { + if (kh_exist(h, it)) { + if (key) *key = kh_key(h, it); + if (val) *val = kh_val(h, it); + *iter = ++it; + return true; + } + } + return false; +} + static pic_value pic_dict_make_dictionary(pic_state *pic) { @@ -198,15 +215,14 @@ static pic_value pic_dict_dictionary_to_alist(pic_state *pic) { struct pic_dict *dict; - pic_value item, alist = pic_nil_value(pic); + pic_value val, alist = pic_nil_value(pic); pic_sym *sym; - khiter_t it; + int it = 0; pic_get_args(pic, "d", &dict); - pic_dict_for_each (sym, dict, it) { - item = pic_cons(pic, pic_obj_value(sym), pic_dict_ref(pic, dict, sym)); - pic_push(pic, item, alist); + while (pic_dict_next(pic, dict, &it, &sym, &val)) { + pic_push(pic, pic_cons(pic, pic_obj_value(sym), val), alist); } return alist; @@ -234,14 +250,14 @@ static pic_value pic_dict_dictionary_to_plist(pic_state *pic) { struct pic_dict *dict; - pic_value plist = pic_nil_value(pic); + pic_value val, plist = pic_nil_value(pic); pic_sym *sym; - khiter_t it; + int it = 0; pic_get_args(pic, "d", &dict); - pic_dict_for_each (sym, dict, it) { - pic_push(pic, pic_dict_ref(pic, dict, sym), plist); + while (pic_dict_next(pic, dict, &it, &sym, &val)) { + pic_push(pic, val, plist); pic_push(pic, pic_obj_value(sym), plist); } diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 922968d5..cf32889d 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -360,11 +360,12 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } case PIC_TYPE_DICT: { pic_sym *sym; - khiter_t it; + pic_value val; + int it = 0; - pic_dict_for_each (sym, &obj->u.dict, it) { + while (pic_dict_next(pic, &obj->u.dict, &it, &sym, &val)) { gc_mark_object(pic, (struct pic_object *)sym); - gc_mark(pic, pic_dict_ref(pic, &obj->u.dict, sym)); + gc_mark(pic, val); } break; } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index ab3c9796..177b877b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -231,6 +231,7 @@ void pic_dict_set(pic_state *, struct pic_dict *, pic_sym *, pic_value); void pic_dict_del(pic_state *, struct pic_dict *, pic_sym *); bool pic_dict_has(pic_state *, struct pic_dict *, pic_sym *); int pic_dict_size(pic_state *, struct pic_dict *); +bool pic_dict_next(pic_state *, struct pic_dict *, int *iter, pic_sym **key, pic_value *val); /* ephemeron */ struct pic_weak *pic_make_weak(pic_state *); diff --git a/extlib/benz/include/picrin/dict.h b/extlib/benz/include/picrin/dict.h index d0ce786e..bcb3a427 100644 --- a/extlib/benz/include/picrin/dict.h +++ b/extlib/benz/include/picrin/dict.h @@ -18,12 +18,6 @@ struct pic_dict { #define pic_dict_ptr(v) ((struct pic_dict *)pic_obj_ptr(v)) -#define pic_dict_for_each(sym, dict, it) \ - pic_dict_for_each_help(sym, (&(dict)->hash), it) -#define pic_dict_for_each_help(sym, h, it) \ - for (it = kh_begin(h); it != kh_end(h); ++it) \ - if ((sym = kh_key(h, it)), kh_exist(h, it)) - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 311f6ed5..b0784941 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -107,13 +107,14 @@ void pic_import(pic_state *pic, const char *lib) { pic_sym *name, *realname, *uid; - khiter_t it; + int it = 0; + pic_value val; struct pic_lib *libp; libp = get_library(pic, lib); - pic_dict_for_each (name, libp->exports, it) { - realname = pic_sym_ptr(pic_dict_ref(pic, libp->exports, name)); + while (pic_dict_next(pic, libp->exports, &it, &name, &val)) { + realname = pic_sym_ptr(val); if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname)); @@ -220,14 +221,14 @@ pic_lib_library_exports(pic_state *pic) const char *lib; pic_value exports = pic_nil_value(pic); pic_sym *sym; - khiter_t it; + int it = 0; struct pic_lib *libp; pic_get_args(pic, "z", &lib); libp = get_library(pic, lib); - pic_dict_for_each (sym, libp->exports, it) { + while (pic_dict_next(pic, libp->exports, &it, &sym, NULL)) { pic_push(pic, pic_obj_value(sym), exports); } diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 9e3e2a50..6cf79c7e 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -242,13 +242,14 @@ write_dict(struct writer_control *p, struct pic_dict *dict) { pic_state *pic = p->pic; xFILE *file = p->file; - pic_sym *sym; - khiter_t it; + pic_sym *key; + pic_value val; + int it = 0; xfprintf(pic, file, "#.(dictionary"); - pic_dict_for_each (sym, dict, it) { - xfprintf(pic, file, " '%s ", pic_str(pic, pic_sym_name(pic, sym))); - write_core(p, pic_dict_ref(pic, dict, sym)); + while (pic_dict_next(pic, dict, &it, &key, &val)) { + xfprintf(pic, file, " '%s ", pic_str(pic, pic_sym_name(pic, key))); + write_core(p, val); } xfprintf(pic, file, ")"); } @@ -366,9 +367,10 @@ traverse(struct writer_control *p, pic_value obj) } } else { /* dictionary */ - pic_sym *sym; - pic_dict_for_each (sym, pic_dict_ptr(obj), it) { - traverse(p, pic_dict_ref(pic, pic_dict_ptr(obj), sym)); + int it = 0; + pic_value val; + while (pic_dict_next(pic, pic_dict_ptr(obj), &it, NULL, &val)) { + traverse(p, val); } } From 0243c52b143cebbbd16e642cc368d92a4130accb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:40:35 +0900 Subject: [PATCH 032/119] remove dict.h --- extlib/benz/dict.c | 1 + extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/dict.h | 25 ------------------------- extlib/benz/include/picrin/object.h | 12 ++++++++++++ 4 files changed, 13 insertions(+), 26 deletions(-) delete mode 100644 extlib/benz/include/picrin/dict.h diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 80aed488..656547d1 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" KHASH_DEFINE(dict, pic_sym *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 177b877b..27fdc140 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -261,7 +261,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/cont.h" #include "picrin/data.h" -#include "picrin/dict.h" #include "picrin/error.h" #include "picrin/macro.h" #include "picrin/pair.h" diff --git a/extlib/benz/include/picrin/dict.h b/extlib/benz/include/picrin/dict.h deleted file mode 100644 index bcb3a427..00000000 --- a/extlib/benz/include/picrin/dict.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_DICT_H -#define PICRIN_DICT_H - -#if defined(__cplusplus) -extern "C" { -#endif - -KHASH_DECLARE(dict, pic_sym *, pic_value) - -struct pic_dict { - PIC_OBJECT_HEADER - khash_t(dict) hash; -}; - -#define pic_dict_ptr(v) ((struct pic_dict *)pic_obj_ptr(v)) - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 6a5d5b16..2a4c7caf 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -44,6 +44,18 @@ struct pic_vector { #define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) +/* dictionary */ + +KHASH_DECLARE(dict, pic_sym *, pic_value) + +struct pic_dict { + PIC_OBJECT_HEADER + khash_t(dict) hash; +}; + +#define pic_dict_ptr(v) ((struct pic_dict *)pic_obj_ptr(v)) + + /* weak */ KHASH_DECLARE(weak, void *, pic_value) From 8979b65b0c73b3b17b02810daedb1c16bd26687d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:50:13 +0900 Subject: [PATCH 033/119] remove data.h --- contrib/10.callcc/callcc.c | 2 +- contrib/30.regexp/src/regexp.c | 12 ++++++------ contrib/40.srfi/src/106.c | 18 +++++++++--------- extlib/benz/data.c | 26 ++++++++++++++++++++++++++ extlib/benz/include/picrin.h | 5 +++-- extlib/benz/include/picrin/data.h | 28 ---------------------------- extlib/benz/include/picrin/object.h | 11 +++++++++++ extlib/benz/value.c | 18 ------------------ 8 files changed, 56 insertions(+), 64 deletions(-) delete mode 100644 extlib/benz/include/picrin/data.h diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 7f102774..4388e8bb 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -224,7 +224,7 @@ cont_call(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - cont = pic_data_ptr(pic_closure_ref(pic, 0))->data; + cont = pic_data(pic, pic_closure_ref(pic, 0)); cont->results = pic_list_by_array(pic, argc, argv); /* execute guard handlers */ diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 1a4ad678..fd8bca76 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -20,7 +20,7 @@ regexp_dtor(pic_state *pic, void *data) static const pic_data_type regexp_type = { "regexp", regexp_dtor, NULL }; #define pic_regexp_p(pic, o) (pic_data_type_p(pic, (o), ®exp_type)) -#define pic_regexp_data_ptr(o) ((struct pic_regexp_t *)pic_data_ptr(o)->data) +#define pic_regexp_data(pic, v) ((struct pic_regexp_t *)pic_data(pic, v)) static pic_value pic_regexp_regexp(pic_state *pic) @@ -92,11 +92,11 @@ pic_regexp_regexp_match(pic_state *pic) matches = pic_nil_value(pic); positions = pic_nil_value(pic); - if (strchr(pic_regexp_data_ptr(reg)->flags, 'g') != NULL) { + if (strchr(pic_regexp_data(pic, reg)->flags, 'g') != NULL) { /* global search */ offset = 0; - while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, match, 0) != REG_NOMATCH) { + while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, match, 0) != REG_NOMATCH) { pic_push(pic, pic_obj_value(pic_str_value(pic, input, match[0].rm_eo - match[0].rm_so)), matches); pic_push(pic, pic_int_value(pic, offset), positions); @@ -106,7 +106,7 @@ pic_regexp_regexp_match(pic_state *pic) } else { /* local search */ - if (regexec(&pic_regexp_data_ptr(reg)->reg, input, 100, match, 0) == 0) { + if (regexec(&pic_regexp_data(pic, reg)->reg, input, 100, match, 0) == 0) { for (i = 0; i < 100; ++i) { if (match[i].rm_so == -1) { break; @@ -140,7 +140,7 @@ pic_regexp_regexp_split(pic_state *pic) pic_assert_type(pic, reg, regexp); - while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { + while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { pic_push(pic, pic_obj_value(pic_str_value(pic, input, match.rm_so)), output); input += match.rm_eo; @@ -163,7 +163,7 @@ pic_regexp_regexp_replace(pic_state *pic) pic_assert_type(pic, reg, regexp); - while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { + while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { output = pic_str_cat(pic, output, pic_str_value(pic, input, match.rm_so)); output = pic_str_cat(pic, output, txt); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 2c3e045d..ee26620e 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -47,7 +47,7 @@ socket_dtor(pic_state *pic, void *data) static const pic_data_type socket_type = { "socket", socket_dtor, NULL }; #define pic_socket_p(pic, o) (pic_data_type_p(pic, (o), &socket_type)) -#define pic_socket_data_ptr(o) ((struct pic_socket_t *)pic_data_ptr(o)->data) +#define pic_socket_data(pic, o) ((struct pic_socket_t *)pic_data(pic, o)) PIC_INLINE void validate_socket_object(pic_state *pic, pic_value v) @@ -154,7 +154,7 @@ pic_socket_socket_accept(pic_state *pic) pic_get_args(pic, "o", &obj); validate_socket_object(pic, obj); - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); errno = 0; @@ -194,7 +194,7 @@ pic_socket_socket_send(pic_state *pic) pic_get_args(pic, "ob|i", &obj, &bv, &flags); validate_socket_object(pic, obj); - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); cursor = pic_blob(pic, bv, &remain); @@ -236,7 +236,7 @@ pic_socket_socket_recv(pic_state *pic) pic_errorf(pic, "size must not be negative"); } - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); buf = malloc(size); @@ -271,7 +271,7 @@ pic_socket_socket_shutdown(pic_state *pic) pic_get_args(pic, "oi", &obj, &how); validate_socket_object(pic, obj); - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); if (sock->fd != -1) { shutdown(sock->fd, how); sock->fd = -1; @@ -288,7 +288,7 @@ pic_socket_socket_close(pic_state *pic) pic_get_args(pic, "o", &obj); validate_socket_object(pic, obj); - socket_close(pic_socket_data_ptr(obj)); + socket_close(pic_socket_data(pic, obj)); return pic_undef_value(pic); } @@ -346,7 +346,7 @@ pic_socket_socket_input_port(pic_state *pic) pic_get_args(pic, "o", &obj); validate_socket_object(pic, obj); - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); return pic_obj_value(make_socket_port(pic, sock, PIC_PORT_IN)); @@ -361,7 +361,7 @@ pic_socket_socket_output_port(pic_state *pic) pic_get_args(pic, "o", &obj); validate_socket_object(pic, obj); - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); return pic_obj_value(make_socket_port(pic, sock, PIC_PORT_OUT)); @@ -377,7 +377,7 @@ pic_socket_call_with_socket(pic_state *pic) pic_get_args(pic, "ol", &obj, &proc); validate_socket_object(pic, obj); - sock = pic_socket_data_ptr(obj); + sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); result = pic_call(pic, proc, 1, obj); diff --git a/extlib/benz/data.c b/extlib/benz/data.c index e2402a2d..da8b7d6d 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -1,2 +1,28 @@ #include "picrin.h" +#include "picrin/object.h" +bool +pic_data_type_p(pic_state *pic, pic_value obj, const pic_data_type *type) +{ + return pic_data_p(pic, obj) && pic_data_ptr(obj)->type == type; +} + +void * +pic_data(pic_state *pic, pic_value data) +{ + pic_assert_type(pic, data, data); + + return pic_data_ptr(data)->data; +} + +struct pic_data * +pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type) +{ + struct pic_data *data; + + data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TYPE_DATA); + data->type = type; + data->data = userdata; + + return data; +} diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 27fdc140..f0c85c9e 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -123,7 +123,7 @@ PIC_INLINE char pic_char(pic_state *, pic_value); #define pic_bool(pic,v) (! pic_false_p(pic, v)) const char *pic_str(pic_state *, struct pic_string *); unsigned char *pic_blob(pic_state *, struct pic_blob *, int *len); -void *pic_data(pic_state *, struct pic_data *); +void *pic_data(pic_state *, pic_value); typedef struct { const char *type_name; @@ -260,7 +260,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/state.h" #include "picrin/cont.h" -#include "picrin/data.h" #include "picrin/error.h" #include "picrin/macro.h" #include "picrin/pair.h" @@ -296,6 +295,8 @@ pic_value pic_eval(pic_state *, pic_value, const char *); struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); +bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); + #define pic_deflibrary(pic, lib) do { \ if (! pic_find_library(pic, lib)) { \ pic_make_library(pic, lib); \ diff --git a/extlib/benz/include/picrin/data.h b/extlib/benz/include/picrin/data.h deleted file mode 100644 index 60fe6dc2..00000000 --- a/extlib/benz/include/picrin/data.h +++ /dev/null @@ -1,28 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_DATA_H -#define PICRIN_DATA_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_data { - PIC_OBJECT_HEADER - const pic_data_type *type; - void *data; -}; - -#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) - -PIC_INLINE bool pic_data_type_p(pic_state *pic, const pic_value obj, const pic_data_type *type) { - return pic_data_p(pic, obj) && pic_data_ptr(obj)->type == type; -} - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 2a4c7caf..411d4d35 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -69,6 +69,17 @@ struct pic_weak { #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) +/* data */ + +struct pic_data { + PIC_OBJECT_HEADER + const pic_data_type *type; + void *data; +}; + +#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) + + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/value.c b/extlib/benz/value.c index b0339702..e0857dfb 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -72,21 +72,3 @@ pic_typename(pic_state *pic, int type) pic_errorf(pic, "pic_typename: invalid type given %d", type); } } - -void * -pic_data(pic_state PIC_UNUSED(*pic), struct pic_data *data) -{ - return data->data; -} - -struct pic_data * -pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type) -{ - struct pic_data *data; - - data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TYPE_DATA); - data->type = type; - data->data = userdata; - - return data; -} From 18b873f72350f9ba3dae733287fd0505b8b10103 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:56:56 +0900 Subject: [PATCH 034/119] remove proc.h --- contrib/10.callcc/callcc.c | 4 +-- contrib/40.srfi/src/106.c | 2 +- extlib/benz/debug.c | 1 + extlib/benz/error.c | 1 + extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/object.h | 42 ++++++++++++++++++++++ extlib/benz/include/picrin/proc.h | 54 ----------------------------- extlib/benz/macro.c | 1 + 8 files changed, 48 insertions(+), 58 deletions(-) delete mode 100644 extlib/benz/include/picrin/proc.h diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 4388e8bb..68cbd4d9 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -276,8 +276,8 @@ pic_callcc_callcc(pic_state *pic) } } -#define pic_redefun(pic, lib, name, func) \ - pic_set(pic, lib, name, pic_obj_value(pic_make_proc(pic, func, 0, NULL))) +#define pic_redefun(pic, lib, name, func) \ + pic_set(pic, lib, name, pic_obj_value(pic_lambda(pic, func, 0))) void pic_init_callcc(pic_state *pic) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index ee26620e..e8029bc6 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -392,7 +392,7 @@ pic_init_srfi_106(pic_state *pic) { pic_deflibrary(pic, "srfi.106"); -#define pic_defun_(pic, name, f) pic_define(pic, "srfi.106", name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))) +#define pic_defun_(pic, name, f) pic_define(pic, "srfi.106", name, pic_obj_value(pic_lambda(pic, f, 0))) #define pic_define_(pic, name, v) pic_define(pic, "srfi.106", name, v) pic_defun_(pic, "socket?", pic_socket_socket_p); diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 7bde1990..c9c8c2af 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" struct pic_string * pic_get_backtrace(pic_state *pic) diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 39c2e9d9..658d85af 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" void pic_panic(pic_state PIC_UNUSED(*pic), const char *msg) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index f0c85c9e..5f0c58f0 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -264,7 +264,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/macro.h" #include "picrin/pair.h" #include "picrin/port.h" -#include "picrin/proc.h" #include "picrin/record.h" #include "picrin/symbol.h" diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 411d4d35..63473828 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -80,6 +80,48 @@ struct pic_data { #define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) +/* context */ + +struct pic_context { + PIC_OBJECT_HEADER + pic_value *regs; + int regc; + struct pic_context *up; + pic_value storage[1]; +}; + +#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) + + +/* procedure */ + +struct pic_proc { + PIC_OBJECT_HEADER + enum { + PIC_PROC_TAG_IREP, + PIC_PROC_TAG_FUNC + } tag; + union { + struct { + pic_func_t func; + int localc; + } f; + struct { + struct pic_irep *irep; + struct pic_context *cxt; + } i; + } u; + pic_value locals[1]; +}; + +#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) + +#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) +#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) + +struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); +struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/proc.h b/extlib/benz/include/picrin/proc.h deleted file mode 100644 index 7fcbe509..00000000 --- a/extlib/benz/include/picrin/proc.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_PROC_H -#define PICRIN_PROC_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_context { - PIC_OBJECT_HEADER - pic_value *regs; - int regc; - struct pic_context *up; - pic_value storage[1]; -}; - -struct pic_proc { - PIC_OBJECT_HEADER - enum { - PIC_PROC_TAG_IREP, - PIC_PROC_TAG_FUNC - } tag; - union { - struct { - pic_func_t func; - int localc; - } f; - struct { - struct pic_irep *irep; - struct pic_context *cxt; - } i; - } u; - pic_value locals[1]; -}; - -#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) -#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) - -#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) - -#define pic_context_p(o) (pic_type(pic, o) == PIC_TYPE_CXT) -#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) - -struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); -struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 8a65f594..c6572765 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal) From 665eda1d927fa5bc24edc4a0d2c7363779fd0df4 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 00:59:45 +0900 Subject: [PATCH 035/119] remove record.h --- extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/blob.h | 16 --------------- extlib/benz/include/picrin/object.h | 15 +++++++++++++++ extlib/benz/include/picrin/record.h | 30 ----------------------------- extlib/benz/include/picrin/string.h | 16 --------------- extlib/benz/record.c | 17 +++------------- 6 files changed, 18 insertions(+), 77 deletions(-) delete mode 100644 extlib/benz/include/picrin/blob.h delete mode 100644 extlib/benz/include/picrin/record.h delete mode 100644 extlib/benz/include/picrin/string.h diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 5f0c58f0..d0efcc9b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -264,7 +264,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/macro.h" #include "picrin/pair.h" #include "picrin/port.h" -#include "picrin/record.h" #include "picrin/symbol.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/blob.h b/extlib/benz/include/picrin/blob.h deleted file mode 100644 index 24ca9e2c..00000000 --- a/extlib/benz/include/picrin/blob.h +++ /dev/null @@ -1,16 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_BLOB_H -#define PICRIN_BLOB_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 63473828..831b2c9b 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -122,6 +122,21 @@ struct pic_proc { struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); + +/* record */ + +struct pic_record { + PIC_OBJECT_HEADER + pic_value type; + pic_value datum; +}; + +#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) +#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) + +struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); + + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/record.h b/extlib/benz/include/picrin/record.h deleted file mode 100644 index 0549fa90..00000000 --- a/extlib/benz/include/picrin/record.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_RECORD_H -#define PICRIN_RECORD_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_record { - PIC_OBJECT_HEADER - pic_value type; - pic_value datum; -}; - -#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) -#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) - -struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); - -pic_value pic_rec_type(pic_state *, struct pic_record *); -pic_value pic_rec_datum(pic_state *, struct pic_record *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/string.h b/extlib/benz/include/picrin/string.h deleted file mode 100644 index ac5d2ce8..00000000 --- a/extlib/benz/include/picrin/string.h +++ /dev/null @@ -1,16 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_STRING_H -#define PICRIN_STRING_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/record.c b/extlib/benz/record.c index c338989a..82d58fd8 100644 --- a/extlib/benz/record.c +++ b/extlib/benz/record.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" struct pic_record * pic_make_rec(pic_state *pic, pic_value type, pic_value datum) @@ -16,18 +17,6 @@ pic_make_rec(pic_state *pic, pic_value type, pic_value datum) return rec; } -pic_value -pic_rec_type(pic_state PIC_UNUSED(*pic), struct pic_record *rec) -{ - return rec->type; -} - -pic_value -pic_rec_datum(pic_state PIC_UNUSED(*pic), struct pic_record *rec) -{ - return rec->datum; -} - static pic_value pic_rec_make_record(pic_state *pic) { @@ -55,7 +44,7 @@ pic_rec_record_type(pic_state *pic) pic_get_args(pic, "r", &rec); - return pic_rec_type(pic, rec); + return rec->type; } static pic_value @@ -65,7 +54,7 @@ pic_rec_record_datum(pic_state *pic) pic_get_args(pic, "r", &rec); - return pic_rec_datum(pic, rec); + return rec->datum; } void From 3198e77ac180be97710740c2d7e5522e434430fb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 01:05:28 +0900 Subject: [PATCH 036/119] remove symbol.h --- extlib/benz/include/picrin.h | 1 - extlib/benz/include/picrin/object.h | 26 +++++++++++++++++++ extlib/benz/include/picrin/symbol.h | 39 ----------------------------- extlib/benz/lib.c | 1 + extlib/benz/macro.c | 4 +-- extlib/benz/symbol.c | 7 +++--- extlib/benz/write.c | 2 +- 7 files changed, 34 insertions(+), 46 deletions(-) delete mode 100644 extlib/benz/include/picrin/symbol.h diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index d0efcc9b..319f01fb 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -264,7 +264,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/macro.h" #include "picrin/pair.h" #include "picrin/port.h" -#include "picrin/symbol.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 831b2c9b..1e70892a 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -10,6 +10,32 @@ extern "C" { #endif +/* symbol & identifier */ + +struct pic_id { + union { + struct pic_symbol { + PIC_OBJECT_HEADER + struct pic_string *str; + } sym; + struct { + PIC_OBJECT_HEADER + struct pic_id *id; + struct pic_env *env; + } id; + } u; +}; + +#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) + +#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) +#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) + +pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); + +struct pic_string *pic_id_name(pic_state *, pic_id *); + + /* blob */ struct pic_blob { diff --git a/extlib/benz/include/picrin/symbol.h b/extlib/benz/include/picrin/symbol.h deleted file mode 100644 index 3104c363..00000000 --- a/extlib/benz/include/picrin/symbol.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_SYMBOL_H -#define PICRIN_SYMBOL_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_id { - union { - struct pic_symbol { - PIC_OBJECT_HEADER - struct pic_string *str; - } sym; - struct { - PIC_OBJECT_HEADER - struct pic_id *id; - struct pic_env *env; - } id; - } u; -}; - -#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) - -#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) -#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) - -pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); - -const char *pic_identifier_name(pic_state *, pic_id *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index b0784941..c5b4dd28 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" KHASH_DEFINE(ltable, const char *, struct pic_lib, kh_str_hash_func, kh_str_cmp_func) diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index c6572765..42524ca0 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -40,7 +40,7 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) pic_sym *uid; struct pic_string *str; - name = pic_identifier_name(pic, id); + name = pic_str(pic, pic_id_name(pic, id)); if (env->up == NULL && pic_sym_p(pic, pic_obj_value(id))) { /* toplevel & public */ str = pic_strf_value(pic, "%s/%s", pic_str(pic, env->lib), name); @@ -274,7 +274,7 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); if (! pic_proc_p(pic, val)) { - pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_identifier_name(pic, id)); + pic_errorf(pic, "macro definition \"%s\" evaluates to non-procedure object", pic_str(pic, pic_id_name(pic, id))); } define_macro(pic, uid, pic_proc_ptr(val)); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index c1cc75cc..ed6bea48 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" #define kh_pic_str_hash(a) (pic_str_hash(pic, (a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, (a), (b)) == 0) @@ -50,14 +51,14 @@ pic_sym_name(pic_state PIC_UNUSED(*pic), pic_sym *sym) return sym->str; } -const char * -pic_identifier_name(pic_state *pic, pic_id *id) +struct pic_string * +pic_id_name(pic_state *pic, pic_id *id) { while (! pic_sym_p(pic, pic_obj_value(id))) { id = id->u.id.id; } - return pic_str(pic, pic_sym_name(pic, (pic_sym *)id)); + return pic_sym_name(pic, (pic_sym *)id); } static pic_value diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 6cf79c7e..e23bcedc 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -288,7 +288,7 @@ write_core(struct writer_control *p, pic_value obj) xfprintf(pic, file, "#f"); break; case PIC_TYPE_ID: - xfprintf(pic, file, "#", pic_identifier_name(pic, pic_id_ptr(obj))); + xfprintf(pic, file, "#", pic_str(pic, pic_id_name(pic, pic_id_ptr(obj)))); break; case PIC_TYPE_EOF: xfprintf(pic, file, "#.(eof-object)"); From 9ae6f0cbe98434a2412729c7170ca2203dac9e09 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 01:34:13 +0900 Subject: [PATCH 037/119] remove error.h --- contrib/20.r7rs/src/file.c | 6 +-- extlib/benz/error.c | 13 +++--- extlib/benz/include/picrin.h | 32 ++++++++++++++- extlib/benz/include/picrin/error.h | 63 ----------------------------- extlib/benz/include/picrin/object.h | 16 ++++++++ extlib/benz/port.c | 4 +- extlib/benz/read.c | 8 +--- 7 files changed, 59 insertions(+), 83 deletions(-) delete mode 100644 extlib/benz/include/picrin/error.h diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 270260db..f0da4f49 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -9,11 +9,7 @@ PIC_NORETURN static void file_error(pic_state *pic, const char *msg) { - struct pic_error *e; - - e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value(pic)); - - pic_raise(pic, pic_obj_value(e)); + pic_error(pic, "file", msg, pic_nil_value(pic)); } pic_value diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 658d85af..b86a42b5 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -44,7 +44,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) msg = pic_str(pic, err); - pic_error(pic, msg, pic_nil_value(pic)); + pic_error(pic, "", msg, pic_nil_value(pic)); } pic_value @@ -92,15 +92,16 @@ pic_pop_handler(pic_state *pic) } struct pic_error * -pic_make_error(pic_state *pic, pic_sym *type, const char *msg, pic_value irrs) +pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; struct pic_string *stack; + pic_sym *ty = pic_intern_cstr(pic, type); stack = pic_get_backtrace(pic); e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); - e->type = type; + e->type = ty; e->msg = pic_cstr_value(pic, msg); e->irrs = irrs; e->stack = stack; @@ -138,11 +139,11 @@ pic_raise(pic_state *pic, pic_value err) } void -pic_error(pic_state *pic, const char *msg, pic_value irrs) +pic_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; - e = pic_make_error(pic, pic_intern_lit(pic, ""), msg, irrs); + e = pic_make_error(pic, type, msg, irrs); pic_raise(pic, pic_obj_value(e)); } @@ -193,7 +194,7 @@ pic_error_error(pic_state *pic) pic_get_args(pic, "z*", &str, &argc, &argv); - pic_error(pic, str, pic_list_by_array(pic, argc, argv)); + pic_error(pic, "", str, pic_list_by_array(pic, argc, argv)); } static pic_value diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 319f01fb..f19f419d 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -109,6 +109,8 @@ void pic_export(pic_state *, pic_sym *sym); PIC_NORETURN void pic_panic(pic_state *, const char *msg); PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); +PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); +PIC_NORETURN void pic_raise(pic_state *, pic_value v); struct pic_proc *pic_lambda(pic_state *, pic_func_t f, int n, ...); struct pic_proc *pic_vlambda(pic_state *, pic_func_t f, int n, va_list); @@ -260,7 +262,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/state.h" #include "picrin/cont.h" -#include "picrin/error.h" #include "picrin/macro.h" #include "picrin/pair.h" #include "picrin/port.h" @@ -301,6 +302,35 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); pic_in_library(pic, lib); \ } while (0) +/* do not return from try block! */ + +#define pic_try \ + pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) +#define pic_catch \ + pic_catch_(PIC_GENSYM(label)) +#define pic_try_(cont, handler) \ + do { \ + extern void pic_push_handler(pic_state *, struct pic_proc *); \ + extern struct pic_proc *pic_pop_handler(pic_state *); \ + extern pic_value pic_native_exception_handler(pic_state *); \ + struct pic_cont cont; \ + pic_save_point(pic, &cont); \ + if (PIC_SETJMP(pic, cont.jmp) == 0) { \ + struct pic_proc *handler; \ + handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_obj_value(pic_make_cont(pic, &cont))); \ + do { \ + pic_push_handler(pic, handler); +#define pic_catch_(label) \ + pic_pop_handler(pic); \ + } while (0); \ + pic->cc = pic->cc->prev; \ + } else { \ + goto label; \ + } \ + } while (0); \ + if (0) \ + label: + void pic_warnf(pic_state *, const char *, ...); struct pic_string *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); diff --git a/extlib/benz/include/picrin/error.h b/extlib/benz/include/picrin/error.h deleted file mode 100644 index d09056c7..00000000 --- a/extlib/benz/include/picrin/error.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_ERROR_H -#define PICRIN_ERROR_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_error { - PIC_OBJECT_HEADER - pic_sym *type; - struct pic_string *msg; - pic_value irrs; - struct pic_string *stack; -}; - -#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) -#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) - -struct pic_error *pic_make_error(pic_state *, pic_sym *, const char *, pic_value); - -/* do not return from try block! */ - -#define pic_try \ - pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) -#define pic_catch \ - pic_catch_(PIC_GENSYM(label)) -#define pic_try_(cont, handler) \ - do { \ - struct pic_cont cont; \ - pic_save_point(pic, &cont); \ - if (PIC_SETJMP(pic, cont.jmp) == 0) { \ - extern pic_value pic_native_exception_handler(pic_state *); \ - struct pic_proc *handler; \ - handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_obj_value(pic_make_cont(pic, &cont))); \ - do { \ - pic_push_handler(pic, handler); -#define pic_catch_(label) \ - pic_pop_handler(pic); \ - } while (0); \ - pic->cc = pic->cc->prev; \ - } else { \ - goto label; \ - } \ - } while (0); \ - if (0) \ - label: - -void pic_push_handler(pic_state *, struct pic_proc *); -struct pic_proc *pic_pop_handler(pic_state *); - -pic_value pic_raise_continuable(pic_state *, pic_value); -PIC_NORETURN void pic_raise(pic_state *, pic_value); -PIC_NORETURN void pic_error(pic_state *, const char *, pic_value); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 1e70892a..1866820c 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -163,6 +163,22 @@ struct pic_record { struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); +/* error */ + +struct pic_error { + PIC_OBJECT_HEADER + pic_sym *type; + struct pic_string *msg; + pic_value irrs; + struct pic_string *stack; +}; + +#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) +#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) + +struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); + + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 5169978b..0c88e230 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -94,7 +94,7 @@ file_error(pic_state *pic, const char *msg) { struct pic_error *e; - e = pic_make_error(pic, pic_intern_lit(pic, "file"), msg, pic_nil_value(pic)); + e = pic_make_error(pic, "file", msg, pic_nil_value(pic)); pic_raise(pic, pic_obj_value(e)); } @@ -257,7 +257,7 @@ string_open(pic_state *pic, const char *data, size_t size) if (file == NULL) { string_close(pic, m); - pic_error(pic, "could not open new output string/bytevector port", pic_nil_value(pic)); + pic_error(pic, "", "could not open new output string/bytevector port", pic_nil_value(pic)); } return file; } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 84738f2c..1ddfcb29 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -11,13 +11,9 @@ static pic_value read(pic_state *pic, struct pic_port *port, int c); static pic_value read_nullable(pic_state *pic, struct pic_port *port, int c); PIC_NORETURN static void -read_error(pic_state *pic, const char *msg, pic_value irritant) +read_error(pic_state *pic, const char *msg, pic_value irritants) { - struct pic_error *e; - - e = pic_make_error(pic, pic_intern_lit(pic, "read"), msg, irritant); - - pic_raise(pic, pic_obj_value(e)); + pic_error(pic, "read", msg, irritants); } static int From 00e98548d72f6a009fc7a27a4f4226a2166db8fc Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 02:29:40 +0900 Subject: [PATCH 038/119] remove pair.h --- contrib/10.callcc/callcc.c | 2 +- contrib/20.r7rs/src/system.c | 2 +- extlib/benz/cont.c | 2 +- extlib/benz/error.c | 2 +- extlib/benz/eval.c | 20 +- extlib/benz/include/picrin.h | 35 +- extlib/benz/include/picrin/object.h | 11 + extlib/benz/include/picrin/pair.h | 87 ----- extlib/benz/macro.c | 10 +- extlib/benz/pair.c | 476 +++++++++++----------------- extlib/benz/read.c | 44 +-- 11 files changed, 263 insertions(+), 428 deletions(-) delete mode 100644 extlib/benz/include/picrin/pair.h diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 68cbd4d9..00c35a8c 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -225,7 +225,7 @@ cont_call(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); 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 */ pic_wind(pic, pic->cp, cont->cp); diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index abcda8be..5d2d1c5b 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -115,7 +115,7 @@ pic_system_getenvs(pic_state *pic) val = pic_cstr_value(pic, getenv(pic_str(pic, key))); /* 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_protect(pic, data); diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 4a43c9cf..7bce50f8 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -109,7 +109,7 @@ cont_call(pic_state *pic) } 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); diff --git a/extlib/benz/error.c b/extlib/benz/error.c index b86a42b5..16c259d6 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -194,7 +194,7 @@ pic_error_error(pic_state *pic) 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 diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 43992d2e..72f30482 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -24,7 +24,7 @@ optimize_beta(pic_state *pic, pic_value expr) if (sym == pic->sQUOTE) { return expr; } 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; defs = pic_nil_value(pic); 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); } expr = pic_list_ref(pic, functor, 2); 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: @@ -106,7 +106,7 @@ analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, anal scope->up = up; 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 @@ -174,11 +174,11 @@ analyze_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) depth = find_var(pic, scope, sym); 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) { - 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 { - 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_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; } @@ -261,7 +261,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) 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 @@ -325,7 +325,7 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) return analyze_call(pic, scope, obj); } default: - return pic_list2(pic, pic_obj_value(pic->sQUOTE), obj); + return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), obj); } } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index f19f419d..71d5ed2e 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -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_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 */ 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); +pic_value pic_make_list(pic_state *, int n, pic_value *argv); pic_value pic_list(pic_state *, int n, ...); pic_value pic_vlist(pic_state *, int n, va_list); -pic_value pic_list_ref(pic_state *, pic_value, int); -pic_value pic_list_tail(pic_state *, pic_value, int); -void pic_list_set(pic_state *, pic_value, int, pic_value); -int pic_length(pic_state *, pic_value); +pic_value pic_list_ref(pic_state *, pic_value list, int i); +void pic_list_set(pic_state *, pic_value list, int i, pic_value v); +pic_value pic_list_tail(pic_state *, pic_value list, int i); +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 */ 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/macro.h" -#include "picrin/pair.h" #include "picrin/port.h" 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) \ 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 *, ...); struct pic_string *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 1866820c..b5d0c8b4 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -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 *); +/* 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 */ struct pic_blob { diff --git a/extlib/benz/include/picrin/pair.h b/extlib/benz/include/picrin/pair.h deleted file mode 100644 index a9ae5933..00000000 --- a/extlib/benz/include/picrin/pair.h +++ /dev/null @@ -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 diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 42524ca0..c2442ad8 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -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_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; } @@ -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); } - 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); body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred); 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 @@ -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); - 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 @@ -350,7 +350,7 @@ pic_expand(pic_state *pic, pic_value expr, struct pic_env *env) puts(""); #endif - deferred = pic_list1(pic, pic_nil_value(pic)); + deferred = pic_list(pic, 1, pic_nil_value(pic)); v = expand(pic, expr, env, deferred); diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index a853440c..c49489c0 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" pic_value 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); } +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 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; } +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 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_list1(pic_state *pic, pic_value obj1) -{ - 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_make_list(pic_state *pic, int n, pic_value *argv) { pic_value list; int i; list = pic_nil_value(pic); - for (i = 0; i < k; ++i) { - list = pic_cons(pic, fill, list); + for (i = n - 1; i >= 0; --i) { + 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; } @@ -232,177 +231,6 @@ pic_append(pic_state *pic, pic_value xs, pic_value 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 pic_pair_pair_p(pic_state *pic) { @@ -530,12 +358,16 @@ pic_pair_list_p(pic_state *pic) static pic_value pic_pair_make_list(pic_state *pic) { - int i; - pic_value fill = pic_undef_value(pic); + int k, i; + 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 @@ -546,7 +378,7 @@ pic_pair_list(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - return pic_list_by_array(pic, argc, argv); + return pic_make_list(pic, argc, argv); } static pic_value @@ -627,11 +459,28 @@ pic_pair_list_set(pic_state *pic) static pic_value 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 @@ -702,7 +551,13 @@ pic_pair_memq(pic_state *pic) 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 @@ -712,7 +567,13 @@ pic_pair_memv(pic_state *pic) 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 @@ -723,38 +584,73 @@ pic_pair_member(pic_state *pic) 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 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 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 pic_pair_assoc(pic_state *pic) { 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 diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 1ddfcb29..f096564b 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -146,13 +146,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c) static pic_value 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 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 @@ -164,19 +164,19 @@ read_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) tag = pic->sUNQUOTE_SPLICING; 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 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 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 @@ -188,7 +188,7 @@ read_syntax_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) tag = pic->sSYNTAX_UNQUOTE_SPLICING; 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 @@ -223,7 +223,7 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c) unsigned u = 0; 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'; @@ -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 */ 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; 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; } 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) { 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))); 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; 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)); } } 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); @@ -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)); } } 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); @@ -383,7 +383,7 @@ read_char(pic_state *pic, struct pic_port *port, int c) if (! isdelim(peek(pic, port))) { 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 'b': c = '\b'; if (! expect(pic, port, "ackspace")) 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); 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 @@ -471,7 +471,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) i = 0; while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') { 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); break; @@ -505,11 +505,11 @@ read_blob(pic_state *pic, struct pic_port *port, int c) } 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 != '(') { - 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; @@ -518,7 +518,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) while ((c = skip(pic, port, c)) != ')') { n = read_uinteger(pic, port, c); 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; 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); } 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'); } @@ -666,7 +666,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i) it = kh_get(read, h, i); 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); } @@ -706,7 +706,7 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c) } 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); @@ -722,7 +722,7 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) } 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); From bb2f9c03671a7d1d0d9bd47f6053cddec72c2fe6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 03:03:29 +0900 Subject: [PATCH 039/119] don't distinguish textual and binary ports --- contrib/20.r7rs/scheme/base.scm | 16 ++++-- contrib/20.r7rs/src/file.c | 8 +-- contrib/20.r7rs/src/load.c | 2 +- contrib/40.srfi/src/106.c | 2 +- extlib/benz/include/picrin/file.h | 2 - extlib/benz/include/picrin/port.h | 2 - extlib/benz/port.c | 84 ++++++++----------------------- 7 files changed, 40 insertions(+), 76 deletions(-) diff --git a/contrib/20.r7rs/scheme/base.scm b/contrib/20.r7rs/scheme/base.scm index 1d722289..e23ba379 100644 --- a/contrib/20.r7rs/scheme/base.scm +++ b/contrib/20.r7rs/scheme/base.scm @@ -776,6 +776,14 @@ ;; 6.13. Input and output + (define (const-true _) #t) + + (define (input-port-open? port) + (and (input-port? port) (port-open? port))) + + (define (output-port-open? port) + (and (output-port? port) (port-open? port))) + (export current-input-port current-output-port current-error-port @@ -785,11 +793,11 @@ port? input-port? output-port? - textual-port? - binary-port? + (rename const-true textual-port?) + (rename const-true binary-port?) - (rename port-open? input-port-open?) - (rename port-open? output-port-open?) + input-port-open? + output-port-open? close-port (rename close-port close-input-port) (rename close-port close-output-port) diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index f0da4f49..619f8dba 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -15,7 +15,7 @@ file_error(pic_state *pic, const char *msg) pic_value pic_file_open_input_file(pic_state *pic) { - static const short flags = PIC_PORT_IN | PIC_PORT_TEXT; + static const short flags = PIC_PORT_IN; char *fname; pic_get_args(pic, "z", &fname); @@ -26,7 +26,7 @@ pic_file_open_input_file(pic_state *pic) pic_value pic_file_open_binary_input_file(pic_state *pic) { - static const short flags = PIC_PORT_IN | PIC_PORT_BINARY; + static const short flags = PIC_PORT_IN; char *fname; pic_get_args(pic, "z", &fname); @@ -37,7 +37,7 @@ pic_file_open_binary_input_file(pic_state *pic) pic_value pic_file_open_output_file(pic_state *pic) { - static const short flags = PIC_PORT_OUT | PIC_PORT_TEXT; + static const short flags = PIC_PORT_OUT; char *fname; pic_get_args(pic, "z", &fname); @@ -48,7 +48,7 @@ pic_file_open_output_file(pic_state *pic) pic_value pic_file_open_binary_output_file(pic_state *pic) { - static const short flags = PIC_PORT_OUT | PIC_PORT_BINARY; + static const short flags = PIC_PORT_OUT; char *fname; pic_get_args(pic, "z", &fname); diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index 7b4c9e8a..1f39e0b3 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -13,7 +13,7 @@ pic_load_load(pic_state *pic) pic_get_args(pic, "z|o", &fn, &envid); - port = pic_open_file(pic, fn, PIC_PORT_IN | PIC_PORT_TEXT); + port = pic_open_file(pic, fn, PIC_PORT_IN); pic_load(pic, port); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index e8029bc6..a6cd8da6 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -333,7 +333,7 @@ make_socket_port(pic_state *pic, struct pic_socket_t *sock, short dir) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = xfunopen(pic, sock, xf_socket_read, xf_socket_write, xf_socket_seek, xf_socket_close); - port->flags = dir | PIC_PORT_BINARY | PIC_PORT_OPEN; + port->flags = dir | PIC_PORT_OPEN; return port; } diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index b07e1a27..55a123b1 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -33,8 +33,6 @@ typedef struct { #define xstdout (&pic->files[1]) #define xstderr (&pic->files[2]) -extern const xFILE x_iob[XOPEN_MAX]; - enum _flags { X_READ = 01, X_WRITE = 02, diff --git a/extlib/benz/include/picrin/port.h b/extlib/benz/include/picrin/port.h index 835d1988..c1bc8225 100644 --- a/extlib/benz/include/picrin/port.h +++ b/extlib/benz/include/picrin/port.h @@ -12,8 +12,6 @@ extern "C" { enum pic_port_flag { PIC_PORT_IN = 1, PIC_PORT_OUT = 2, - PIC_PORT_TEXT = 4, - PIC_PORT_BINARY = 8, PIC_PORT_OPEN = 16 }; diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 0c88e230..94a57ef1 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -152,7 +152,7 @@ pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = file; - port->flags = dir | PIC_PORT_TEXT | PIC_PORT_OPEN; + port->flags = dir | PIC_PORT_OPEN; pic_defvar(pic, name, pic_obj_value(port), pic_make_proc(pic, pic_assert_port, 0, NULL)); } @@ -269,7 +269,7 @@ pic_open_input_string(pic_state *pic, const char *str) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, str, strlen(str)); - port->flags = PIC_PORT_IN | PIC_PORT_TEXT | PIC_PORT_OPEN; + port->flags = PIC_PORT_IN | PIC_PORT_OPEN; return port; } @@ -281,7 +281,7 @@ pic_open_output_string(pic_state *pic) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, NULL, 0); - port->flags = PIC_PORT_OUT | PIC_PORT_TEXT | PIC_PORT_OPEN; + port->flags = PIC_PORT_OUT | PIC_PORT_OPEN; return port; } @@ -360,36 +360,6 @@ pic_port_output_port_p(pic_state *pic) } } -static pic_value -pic_port_textual_port_p(pic_state *pic) -{ - pic_value v; - - pic_get_args(pic, "o", &v); - - if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_TEXT) != 0) { - return pic_true_value(pic); - } - else { - return pic_false_value(pic); - } -} - -static pic_value -pic_port_binary_port_p(pic_state *pic) -{ - pic_value v; - - pic_get_args(pic, "o", &v); - - if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_BINARY) != 0) { - return pic_true_value(pic); - } - else { - return pic_false_value(pic); - } -} - static pic_value pic_port_port_p(pic_state *pic) { @@ -447,14 +417,6 @@ pic_port_close_port(pic_state *pic) pic_errorf(pic, caller ": expected output port"); \ case PIC_PORT_OUT: \ pic_errorf(pic, caller ": expected input port"); \ - case PIC_PORT_IN | PIC_PORT_TEXT: \ - pic_errorf(pic, caller ": expected input/textual port"); \ - case PIC_PORT_IN | PIC_PORT_BINARY: \ - pic_errorf(pic, caller ": expected input/binary port"); \ - case PIC_PORT_OUT | PIC_PORT_TEXT: \ - pic_errorf(pic, caller ": expected output/textual port"); \ - case PIC_PORT_OUT | PIC_PORT_BINARY: \ - pic_errorf(pic, caller ": expected output/binary port"); \ } \ } \ if ((port->flags & PIC_PORT_OPEN) == 0) { \ @@ -494,7 +456,7 @@ pic_port_get_output_string(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "get-output-string"); + assert_port_profile(port, PIC_PORT_OUT, "get-output-string"); return pic_obj_value(pic_get_output_string(pic, port)); } @@ -509,7 +471,7 @@ pic_port_open_input_blob(pic_state *pic) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, (const char *)blob->data, blob->len); - port->flags = PIC_PORT_IN | PIC_PORT_BINARY | PIC_PORT_OPEN; + port->flags = PIC_PORT_IN | PIC_PORT_OPEN; return pic_obj_value(port); } @@ -523,7 +485,7 @@ pic_port_open_output_bytevector(pic_state *pic) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = string_open(pic, NULL, 0); - port->flags = PIC_PORT_OUT | PIC_PORT_BINARY | PIC_PORT_OPEN; + port->flags = PIC_PORT_OUT | PIC_PORT_OPEN; return pic_obj_value(port); } @@ -537,7 +499,7 @@ pic_port_get_output_bytevector(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "get-output-bytevector"); + assert_port_profile(port, PIC_PORT_OUT, "get-output-bytevector"); if (port->file->vtable.write != string_write) { pic_errorf(pic, "get-output-bytevector: port is not made by open-output-bytevector"); @@ -560,7 +522,7 @@ pic_port_read_char(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-char"); + assert_port_profile(port, PIC_PORT_IN, "read-char"); if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(pic); @@ -578,7 +540,7 @@ pic_port_peek_char(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "peek-char"); + assert_port_profile(port, PIC_PORT_IN, "peek-char"); if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(pic); @@ -599,7 +561,7 @@ pic_port_read_line(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-line"); + assert_port_profile(port, PIC_PORT_IN, "read-line"); buf = pic_open_output_string(pic); while ((c = xfgetc(pic, port->file)) != EOF && c != '\n') { @@ -621,7 +583,7 @@ pic_port_char_ready_p(pic_state *pic) { struct pic_port *port = pic_stdin(pic); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "char-ready?"); + assert_port_profile(port, PIC_PORT_IN, "char-ready?"); pic_get_args(pic, "|p", &port); @@ -638,7 +600,7 @@ pic_port_read_string(pic_state *pic){ pic_get_args(pic, "i|p", &k, &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-stritg"); + assert_port_profile(port, PIC_PORT_IN, "read-stritg"); c = EOF; buf = pic_open_output_string(pic); @@ -665,7 +627,7 @@ pic_port_read_byte(pic_state *pic){ int c; pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-u8"); + assert_port_profile(port, PIC_PORT_IN, "read-u8"); if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(pic); } @@ -681,7 +643,7 @@ pic_port_peek_byte(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "peek-u8"); + assert_port_profile(port, PIC_PORT_IN, "peek-u8"); c = xfgetc(pic, port->file); if (c == EOF) { @@ -700,7 +662,7 @@ pic_port_byte_ready_p(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "u8-ready?"); + assert_port_profile(port, PIC_PORT_IN, "u8-ready?"); return pic_true_value(pic); /* FIXME: always returns #t */ } @@ -715,7 +677,7 @@ pic_port_read_blob(pic_state *pic) pic_get_args(pic, "i|p", &k, &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-bytevector"); + assert_port_profile(port, PIC_PORT_IN, "read-bytevector"); blob = pic_blob_value(pic, 0, k); @@ -748,7 +710,7 @@ pic_port_read_blob_ip(pic_state *pic) end = bv->len; } - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-bytevector!"); + assert_port_profile(port, PIC_PORT_IN, "read-bytevector!"); if (end < start) { pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index"); @@ -776,7 +738,7 @@ pic_port_newline(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "newline"); + assert_port_profile(port, PIC_PORT_OUT, "newline"); xfputs(pic, "\n", port->file); return pic_undef_value(pic); @@ -790,7 +752,7 @@ pic_port_write_char(pic_state *pic) pic_get_args(pic, "c|p", &c, &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-char"); + assert_port_profile(port, PIC_PORT_OUT, "write-char"); xfputc(pic, c, port->file); return pic_undef_value(pic); @@ -813,7 +775,7 @@ pic_port_write_string(pic_state *pic) end = INT_MAX; } - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-string"); + assert_port_profile(port, PIC_PORT_OUT, "write-string"); for (i = start; i < end && str[i] != '\0'; ++i) { xfputc(pic, str[i], port->file); @@ -829,7 +791,7 @@ pic_port_write_byte(pic_state *pic) pic_get_args(pic, "i|p", &i, &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-u8"); + assert_port_profile(port, PIC_PORT_OUT, "write-u8"); xfputc(pic, i, port->file); return pic_undef_value(pic); @@ -852,7 +814,7 @@ pic_port_write_blob(pic_state *pic) end = blob->len; } - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-bytevector"); + assert_port_profile(port, PIC_PORT_OUT, "write-bytevector"); for (i = start; i < end; ++i) { xfputc(pic, blob->data[i], port->file); @@ -906,8 +868,6 @@ pic_init_port(pic_state *pic) pic_defun(pic, "input-port?", pic_port_input_port_p); pic_defun(pic, "output-port?", pic_port_output_port_p); - pic_defun(pic, "textual-port?", pic_port_textual_port_p); - pic_defun(pic, "binary-port?", pic_port_binary_port_p); pic_defun(pic, "port?", pic_port_port_p); pic_defun(pic, "port-open?", pic_port_port_open_p); From ec9c0e8841b985b262a85f81929842d69a93a5e4 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 03:57:15 +0900 Subject: [PATCH 040/119] [bugfix] rope_at --- extlib/benz/string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 8027a36e..0472fae3 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -120,8 +120,8 @@ rope_at(struct pic_rope *x, size_t i) if (i < x->left->weight) { x = x->left; } else { - x = x->right; i -= x->left->weight; + x = x->right; } } return -1; From 8ec052c09f05bec836850a1f872427921ce6b9c3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 03:58:09 +0900 Subject: [PATCH 041/119] reimplement string-io procedures in scheme --- contrib/20.r7rs/scheme/base.scm | 62 +++++++- extlib/benz/port.c | 245 ++------------------------------ 2 files changed, 68 insertions(+), 239 deletions(-) diff --git a/contrib/20.r7rs/scheme/base.scm b/contrib/20.r7rs/scheme/base.scm index e23ba379..d8d487d1 100644 --- a/contrib/20.r7rs/scheme/base.scm +++ b/contrib/20.r7rs/scheme/base.scm @@ -776,14 +776,68 @@ ;; 6.13. Input and output - (define (const-true _) #t) - (define (input-port-open? port) (and (input-port? port) (port-open? port))) (define (output-port-open? port) (and (output-port? port) (port-open? port))) + (define (call-with-port port handler) + (let ((res (handler port))) + (close-port port) + res)) + + (define (open-input-string str) + (open-input-bytevector (list->bytevector (map char->integer (string->list str))))) + + (define (open-output-string) + (open-output-bytevector)) + + (define (get-output-string port) + (list->string (map integer->char (bytevector->list (get-output-bytevector port))))) + + (define (read-char . opt) + (let ((b (apply read-u8 opt))) + (if (eof-object? b) + b + (integer->char b)))) + + (define (peek-char . opt) + (let ((b (apply peek-u8 opt))) + (if (eof-object? b) + b + (integer->char b)))) + + (define (char-ready? . opt) + (apply u8-ready? opt)) + + (define (newline . opt) + (apply write-u8 (char->integer #\newline) opt)) + + (define (write-char c . opt) + (apply write-u8 (char->integer c) opt)) + + (define (write-string s . opt) + (apply write-bytevector (list->bytevector (map char->integer (string->list s))) opt)) + + (define (read-line . opt) + (if (eof-object? (apply peek-char opt)) + (eof-object) + (let loop ((str "") (c (apply read-char opt))) + (if (or (eof-object? c) + (char=? c #\newline)) + str + (loop (string-append str (string c)) (apply read-char opt)))))) + + (define (read-string k . opt) + (if (eof-object? (apply peek-char opt)) + (eof-object) + (let loop ((k k) (str "") (c (apply read-char opt))) + (if (or (eof-object? c) + (zero? k)) + str + (loop (- k 1) (string-append str (string c)) (apply read-char opt)))))) + (export current-input-port current-output-port current-error-port @@ -793,8 +847,8 @@ port? input-port? output-port? - (rename const-true textual-port?) - (rename const-true binary-port?) + (rename port? textual-port?) + (rename port? binary-port?) input-port-open? output-port-open? diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 94a57ef1..875ccf99 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -314,22 +314,6 @@ pic_close_port(pic_state *pic, struct pic_port *port) port->flags &= ~PIC_PORT_OPEN; } -static pic_value -pic_port_call_with_port(pic_state *pic) -{ - struct pic_port *port; - struct pic_proc *proc; - pic_value value; - - pic_get_args(pic, "pl", &port, &proc); - - value = pic_call(pic, proc, 1, pic_obj_value(port)); - - pic_close_port(pic, port); - - return value; -} - static pic_value pic_port_input_port_p(pic_state *pic) { @@ -424,43 +408,6 @@ pic_port_close_port(pic_state *pic) } \ } while (0) -static pic_value -pic_port_open_input_string(pic_state *pic) -{ - struct pic_port *port; - char *str; - - pic_get_args(pic, "z", &str); - - port = pic_open_input_string(pic, str); - - return pic_obj_value(port); -} - -static pic_value -pic_port_open_output_string(pic_state *pic) -{ - struct pic_port *port; - - pic_get_args(pic, ""); - - port = pic_open_output_string(pic); - - return pic_obj_value(port); -} - -static pic_value -pic_port_get_output_string(pic_state *pic) -{ - struct pic_port *port = pic_stdout(pic); - - pic_get_args(pic, "|p", &port); - - assert_port_profile(port, PIC_PORT_OUT, "get-output-string"); - - return pic_obj_value(pic_get_output_string(pic, port)); -} - static pic_value pic_port_open_input_blob(pic_state *pic) { @@ -514,113 +461,6 @@ pic_port_get_output_bytevector(pic_state *pic) return pic_obj_value(blob); } -static pic_value -pic_port_read_char(pic_state *pic) -{ - int c; - struct pic_port *port = pic_stdin(pic); - - pic_get_args(pic, "|p", &port); - - assert_port_profile(port, PIC_PORT_IN, "read-char"); - - if ((c = xfgetc(pic, port->file)) == EOF) { - return pic_eof_object(pic); - } - else { - return pic_char_value(pic, (char)c); - } -} - -static pic_value -pic_port_peek_char(pic_state *pic) -{ - int c; - struct pic_port *port = pic_stdin(pic); - - pic_get_args(pic, "|p", &port); - - assert_port_profile(port, PIC_PORT_IN, "peek-char"); - - if ((c = xfgetc(pic, port->file)) == EOF) { - return pic_eof_object(pic); - } - else { - xungetc(c, port->file); - return pic_char_value(pic, (char)c); - } -} - -static pic_value -pic_port_read_line(pic_state *pic) -{ - int c; - struct pic_port *port = pic_stdin(pic), *buf; - struct pic_string *str; - pic_value res = pic_eof_object(pic); - - pic_get_args(pic, "|p", &port); - - assert_port_profile(port, PIC_PORT_IN, "read-line"); - - buf = pic_open_output_string(pic); - while ((c = xfgetc(pic, port->file)) != EOF && c != '\n') { - xfputc(pic, c, buf->file); - } - - str = pic_get_output_string(pic, buf); - if (pic_str_len(pic, str) == 0 && c == EOF) { - /* EOF */ - } else { - res = pic_obj_value(str); - } - pic_close_port(pic, buf); - return res; -} - -static pic_value -pic_port_char_ready_p(pic_state *pic) -{ - struct pic_port *port = pic_stdin(pic); - - assert_port_profile(port, PIC_PORT_IN, "char-ready?"); - - pic_get_args(pic, "|p", &port); - - return pic_true_value(pic); /* FIXME: always returns #t */ -} - -static pic_value -pic_port_read_string(pic_state *pic){ - struct pic_port *port = pic_stdin(pic), *buf; - struct pic_string *str; - int k, i; - int c; - pic_value res = pic_eof_object(pic); - - pic_get_args(pic, "i|p", &k, &port); - - assert_port_profile(port, PIC_PORT_IN, "read-stritg"); - - c = EOF; - buf = pic_open_output_string(pic); - for(i = 0; i < k; ++i) { - if((c = xfgetc(pic, port->file)) == EOF){ - break; - } - xfputc(pic, c, buf->file); - } - - str = pic_get_output_string(pic, buf); - if (pic_str_len(pic, str) == 0 && c == EOF) { - /* EOF */ - } else { - res = pic_obj_value(str); - } - pic_close_port(pic, buf); - return res; -} - static pic_value pic_port_read_byte(pic_state *pic){ struct pic_port *port = pic_stdin(pic); @@ -669,7 +509,7 @@ pic_port_byte_ready_p(pic_state *pic) static pic_value -pic_port_read_blob(pic_state *pic) +pic_port_read_bytevector(pic_state *pic) { struct pic_port *port = pic_stdin(pic); struct pic_blob *blob; @@ -693,7 +533,7 @@ pic_port_read_blob(pic_state *pic) } static pic_value -pic_port_read_blob_ip(pic_state *pic) +pic_port_read_bytevector_ip(pic_state *pic) { struct pic_port *port; struct pic_blob *bv; @@ -731,58 +571,6 @@ pic_port_read_blob_ip(pic_state *pic) } } -static pic_value -pic_port_newline(pic_state *pic) -{ - struct pic_port *port = pic_stdout(pic); - - pic_get_args(pic, "|p", &port); - - assert_port_profile(port, PIC_PORT_OUT, "newline"); - - xfputs(pic, "\n", port->file); - return pic_undef_value(pic); -} - -static pic_value -pic_port_write_char(pic_state *pic) -{ - char c; - struct pic_port *port = pic_stdout(pic); - - pic_get_args(pic, "c|p", &c, &port); - - assert_port_profile(port, PIC_PORT_OUT, "write-char"); - - xfputc(pic, c, port->file); - return pic_undef_value(pic); -} - -static pic_value -pic_port_write_string(pic_state *pic) -{ - char *str; - struct pic_port *port; - int start, end, n, i; - - n = pic_get_args(pic, "z|pii", &str, &port, &start, &end); - switch (n) { - case 1: - port = pic_stdout(pic); - case 2: - start = 0; - case 3: - end = INT_MAX; - } - - assert_port_profile(port, PIC_PORT_OUT, "write-string"); - - for (i = start; i < end && str[i] != '\0'; ++i) { - xfputc(pic, str[i], port->file); - } - return pic_undef_value(pic); -} - static pic_value pic_port_write_byte(pic_state *pic) { @@ -798,7 +586,7 @@ pic_port_write_byte(pic_state *pic) } static pic_value -pic_port_write_blob(pic_state *pic) +pic_port_write_bytevector(pic_state *pic) { struct pic_blob *blob; struct pic_port *port; @@ -864,42 +652,29 @@ pic_init_port(pic_state *pic) pic_define_standard_port(pic, "current-output-port", xstdout, PIC_PORT_OUT); pic_define_standard_port(pic, "current-error-port", xstderr, PIC_PORT_OUT); - pic_defun(pic, "call-with-port", pic_port_call_with_port); - + pic_defun(pic, "port?", pic_port_port_p); pic_defun(pic, "input-port?", pic_port_input_port_p); pic_defun(pic, "output-port?", pic_port_output_port_p); - pic_defun(pic, "port?", pic_port_port_p); - pic_defun(pic, "port-open?", pic_port_port_open_p); pic_defun(pic, "close-port", pic_port_close_port); + pic_defun(pic, "eof-object?", pic_port_eof_object_p); + pic_defun(pic, "eof-object", pic_port_eof_object); + /* string I/O */ - pic_defun(pic, "open-input-string", pic_port_open_input_string); - pic_defun(pic, "open-output-string", pic_port_open_output_string); - pic_defun(pic, "get-output-string", pic_port_get_output_string); pic_defun(pic, "open-input-bytevector", pic_port_open_input_blob); pic_defun(pic, "open-output-bytevector", pic_port_open_output_bytevector); pic_defun(pic, "get-output-bytevector", pic_port_get_output_bytevector); /* input */ - pic_defun(pic, "read-char", pic_port_read_char); - pic_defun(pic, "peek-char", pic_port_peek_char); - pic_defun(pic, "read-line", pic_port_read_line); - pic_defun(pic, "eof-object?", pic_port_eof_object_p); - pic_defun(pic, "eof-object", pic_port_eof_object); - pic_defun(pic, "char-ready?", pic_port_char_ready_p); - pic_defun(pic, "read-string", pic_port_read_string); pic_defun(pic, "read-u8", pic_port_read_byte); pic_defun(pic, "peek-u8", pic_port_peek_byte); pic_defun(pic, "u8-ready?", pic_port_byte_ready_p); - pic_defun(pic, "read-bytevector", pic_port_read_blob); - pic_defun(pic, "read-bytevector!", pic_port_read_blob_ip); + pic_defun(pic, "read-bytevector", pic_port_read_bytevector); + pic_defun(pic, "read-bytevector!", pic_port_read_bytevector_ip); /* output */ - pic_defun(pic, "newline", pic_port_newline); - pic_defun(pic, "write-char", pic_port_write_char); - pic_defun(pic, "write-string", pic_port_write_string); pic_defun(pic, "write-u8", pic_port_write_byte); - pic_defun(pic, "write-bytevector", pic_port_write_blob); + pic_defun(pic, "write-bytevector", pic_port_write_bytevector); pic_defun(pic, "flush-output-port", pic_port_flush); } From 84c2866b2baf9cb5205290f3f33f0e2983ca2832 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 05:54:50 +0900 Subject: [PATCH 042/119] cleanup port API --- contrib/20.r7rs/src/file.c | 37 +-- contrib/20.r7rs/src/load.c | 10 +- contrib/20.r7rs/t/r7rs.scm | 2 + contrib/40.srfi/src/106.c | 19 +- extlib/benz/char.c | 2 +- extlib/benz/file.c | 192 +++++++++++++ extlib/benz/include/picrin.h | 6 +- extlib/benz/include/picrin/file.h | 7 + extlib/benz/include/picrin/port.h | 15 +- extlib/benz/load.c | 4 +- extlib/benz/number.c | 13 +- extlib/benz/port.c | 435 +++++------------------------- extlib/benz/read.c | 2 +- extlib/benz/state.c | 12 +- extlib/benz/string.c | 16 +- 15 files changed, 331 insertions(+), 441 deletions(-) diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 619f8dba..83fd231f 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -12,48 +12,35 @@ file_error(pic_state *pic, const char *msg) pic_error(pic, "file", msg, pic_nil_value(pic)); } -pic_value -pic_file_open_input_file(pic_state *pic) +static struct pic_port * +open_file(pic_state *pic, const char *fname, const char *mode) { - static const short flags = PIC_PORT_IN; - char *fname; + FILE *fp; - pic_get_args(pic, "z", &fname); - - return pic_obj_value(pic_open_file(pic, fname, flags)); + if ((fp = fopen(fname, mode)) == NULL) { + file_error(pic, "could not open file..."); + } + return pic_make_port(pic, xfopen_file(pic, fp, mode)); } pic_value -pic_file_open_binary_input_file(pic_state *pic) +pic_file_open_input_file(pic_state *pic) { - static const short flags = PIC_PORT_IN; char *fname; pic_get_args(pic, "z", &fname); - return pic_obj_value(pic_open_file(pic, fname, flags)); + return pic_obj_value(open_file(pic, fname, "r")); } pic_value pic_file_open_output_file(pic_state *pic) { - static const short flags = PIC_PORT_OUT; char *fname; pic_get_args(pic, "z", &fname); - return pic_obj_value(pic_open_file(pic, fname, flags)); -} - -pic_value -pic_file_open_binary_output_file(pic_state *pic) -{ - static const short flags = PIC_PORT_OUT; - char *fname; - - pic_get_args(pic, "z", &fname); - - return pic_obj_value(pic_open_file(pic, fname, flags)); + return pic_obj_value(open_file(pic, fname, "w")); } pic_value @@ -92,9 +79,9 @@ pic_init_file(pic_state *pic) pic_deflibrary(pic, "scheme.file"); pic_defun(pic, "open-input-file", pic_file_open_input_file); - pic_defun(pic, "open-binary-input-file", pic_file_open_binary_input_file); + pic_defun(pic, "open-binary-input-file", pic_file_open_input_file); pic_defun(pic, "open-output-file", pic_file_open_output_file); - pic_defun(pic, "open-binary-output-file", pic_file_open_binary_output_file); + pic_defun(pic, "open-binary-output-file", pic_file_open_output_file); pic_defun(pic, "file-exists?", pic_file_exists_p); pic_defun(pic, "delete-file", pic_file_delete); } diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index 1f39e0b3..15004dc0 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -4,16 +4,24 @@ #include "picrin.h" +#include + static pic_value pic_load_load(pic_state *pic) { pic_value envid; char *fn; struct pic_port *port; + FILE *fp; pic_get_args(pic, "z|o", &fn, &envid); - port = pic_open_file(pic, fn, PIC_PORT_IN); + fp = fopen(fn, "r"); + if (fp == NULL) { + pic_errorf(pic, "load: could not open file %s", fn); + } + + port = pic_make_port(pic, xfopen_file(pic, fp, "r")); pic_load(pic, port); diff --git a/contrib/20.r7rs/t/r7rs.scm b/contrib/20.r7rs/t/r7rs.scm index 4185724d..f329d781 100644 --- a/contrib/20.r7rs/t/r7rs.scm +++ b/contrib/20.r7rs/t/r7rs.scm @@ -1766,6 +1766,8 @@ (test 'exception value) (test "condition: an-error!" (get-output-string out))) +(flush-output-port) + (define (test-exception-handler-4 v out) (call-with-current-continuation (lambda (k) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index a6cd8da6..c8efc474 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -327,14 +327,17 @@ xf_socket_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) } static struct pic_port * -make_socket_port(pic_state *pic, struct pic_socket_t *sock, short dir) +make_socket_port(pic_state *pic, struct pic_socket_t *sock, const char *mode) { - struct pic_port *port; + xFILE *fp; - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); - port->file = xfunopen(pic, sock, xf_socket_read, xf_socket_write, xf_socket_seek, xf_socket_close); - port->flags = dir | PIC_PORT_OPEN; - return port; + if (*mode == 'r') { + fp = xfunopen(pic, sock, xf_socket_read, 0, xf_socket_seek, xf_socket_close); + } else { + fp = xfunopen(pic, sock, 0, xf_socket_write, xf_socket_seek, xf_socket_close); + } + + return pic_make_port(pic, fp); } static pic_value @@ -349,7 +352,7 @@ pic_socket_socket_input_port(pic_state *pic) sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); - return pic_obj_value(make_socket_port(pic, sock, PIC_PORT_IN)); + return pic_obj_value(make_socket_port(pic, sock, "r")); } static pic_value @@ -364,7 +367,7 @@ pic_socket_socket_output_port(pic_state *pic) sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); - return pic_obj_value(make_socket_port(pic, sock, PIC_PORT_OUT)); + return pic_obj_value(make_socket_port(pic, sock, "w")); } static pic_value diff --git a/extlib/benz/char.c b/extlib/benz/char.c index 709787fb..d4d4b499 100644 --- a/extlib/benz/char.c +++ b/extlib/benz/char.c @@ -31,7 +31,7 @@ pic_char_integer_to_char(pic_state *pic) pic_get_args(pic, "i", &i); - if (i < 0 || i > 127) { + if (i < 0 || i > 255) { pic_errorf(pic, "integer->char: integer out of char range: %d", i); } diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 334a4315..4b4b274e 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -378,6 +378,198 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { return cnt; } +#if PIC_ENABLE_STDIO + +static int +file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { + FILE *file = cookie; + int r; + + size = 1; /* override size */ + + r = (int)fread(ptr, 1, (size_t)size, file); + if (r < size && ferror(file)) { + return -1; + } + if (r == 0 && feof(file)) { + clearerr(file); + } + return r; +} + +static int +file_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) { + FILE *file = cookie; + int r; + + r = (int)fwrite(ptr, 1, (size_t)size, file); + if (r < size) { + return -1; + } + fflush(cookie); + return r; +} + +static long +file_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { + switch (whence) { + case XSEEK_CUR: + whence = SEEK_CUR; + break; + case XSEEK_SET: + whence = SEEK_SET; + break; + case XSEEK_END: + whence = SEEK_END; + break; + } + if (fseek(cookie, pos, whence) == 0) { + return ftell(cookie); + } + return -1; +} + +static int +file_close(pic_state PIC_UNUSED(*pic), void *cookie) { + return fclose(cookie); +} + +xFILE *xfopen_file(pic_state *pic, FILE *fp, const char *mode) { + switch (*mode) { + case 'r': + return xfunopen(pic, fp, file_read, 0, file_seek, file_close); + default: + return xfunopen(pic, fp, 0, file_write, file_seek, file_close); + } +} + +#endif + +typedef struct { char *buf; long pos, end, capa; } xbuf_t; + +static int +string_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) +{ + xbuf_t *m = cookie; + + if (size > (int)(m->end - m->pos)) + size = (int)(m->end - m->pos); + memcpy(ptr, m->buf + m->pos, size); + m->pos += size; + return size; +} + +static int +string_write(pic_state *pic, void *cookie, const char *ptr, int size) +{ + xbuf_t *m = cookie; + + if (m->pos + size >= m->capa) { + m->capa = (m->pos + size) * 2; + m->buf = pic_realloc(pic, m->buf, m->capa); + } + memcpy(m->buf + m->pos, ptr, size); + m->pos += size; + if (m->end < m->pos) + m->end = m->pos; + return size; +} + +static long +string_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) +{ + xbuf_t *m = cookie; + + switch (whence) { + case XSEEK_SET: + m->pos = pos; + break; + case XSEEK_CUR: + m->pos += pos; + break; + case XSEEK_END: + m->pos = m->end + pos; + break; + } + + return m->pos; +} + +static int +string_close(pic_state *pic, void *cookie) +{ + xbuf_t *m = cookie; + + pic_free(pic, m->buf); + pic_free(pic, m); + return 0; +} + +xFILE *xfopen_buf(pic_state *pic, const char *data, int size, const char *mode) { + xbuf_t *m; + xFILE *file; + + m = pic_malloc(pic, sizeof(xbuf_t)); + m->buf = pic_malloc(pic, size); + m->pos = 0; + m->end = size; + m->capa = size; + + if (*mode == 'r') { + memcpy(m->buf, data, size); + file = xfunopen(pic, m, string_read, NULL, string_seek, string_close); + } else { + file = xfunopen(pic, m, NULL, string_write, string_seek, string_close); + } + if (file == NULL) { + string_close(pic, m); + } + return file; +} + +int xfget_buf(pic_state *pic, xFILE *file, const char **buf, int *len) { + xbuf_t *s; + + xfflush(pic, file); + + if (file->vtable.write != string_write) { + return -1; + } + s = file->vtable.cookie; + *len = s->end; + *buf = s->buf; + return 0; +} + +static int +null_read(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), char PIC_UNUSED(*ptr), int PIC_UNUSED(size)) { + return 0; +} + +static int +null_write(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), const char PIC_UNUSED(*ptr), int size) { + return size; +} + +static long +null_seek(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) { + return 0; +} + +static int +null_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) { + return 0; +} + +xFILE *xfopen_null(pic_state PIC_UNUSED(*pic), const char *mode) { + switch (*mode) { + case 'r': + return xfunopen(pic, 0, null_read, 0, null_seek, null_close); + default: + return xfunopen(pic, 0, 0, null_write, null_seek, null_close); + } +} + #if 0 int main() { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 71d5ed2e..a2944b44 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -350,9 +350,9 @@ void pic_warnf(pic_state *, const char *, ...); struct pic_string *pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); -struct pic_port *pic_stdin(pic_state *); -struct pic_port *pic_stdout(pic_state *); -struct pic_port *pic_stderr(pic_state *); +#define pic_stdin(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-input-port", 0)) +#define pic_stdout(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-output-port", 0)) +#define pic_stderr(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-error-port", 0)) pic_value pic_write(pic_state *, pic_value); /* returns given obj */ pic_value pic_fwrite(pic_state *, pic_value, xFILE *); diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 55a123b1..d23479ad 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -62,6 +62,13 @@ enum _flags { xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); int xfclose(pic_state *, xFILE *); +#if PIC_ENABLE_STDIO +xFILE *xfopen_file(pic_state *, FILE *, const char *mode); +#endif +xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); +int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len); +xFILE *xfopen_null(pic_state *, const char *mode); + /* buffer management */ int x_fillbuf(pic_state *, xFILE *); int x_flushbuf(pic_state *, int, xFILE *); diff --git a/extlib/benz/include/picrin/port.h b/extlib/benz/include/picrin/port.h index c1bc8225..95ddc487 100644 --- a/extlib/benz/include/picrin/port.h +++ b/extlib/benz/include/picrin/port.h @@ -9,26 +9,15 @@ extern "C" { #endif -enum pic_port_flag { - PIC_PORT_IN = 1, - PIC_PORT_OUT = 2, - PIC_PORT_OPEN = 16 -}; - struct pic_port { PIC_OBJECT_HEADER xFILE *file; - int flags; }; #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) -struct pic_port *pic_open_input_string(pic_state *, const char *); -struct pic_port *pic_open_output_string(pic_state *); -struct pic_string *pic_get_output_string(pic_state *, struct pic_port *); - -struct pic_port *pic_open_file(pic_state *, const char *, int); -void pic_close_port(pic_state *pic, struct pic_port *); +struct pic_port *pic_make_port(pic_state *, xFILE *file); +void pic_close_port(pic_state *, struct pic_port *port); #if defined(__cplusplus) } diff --git a/extlib/benz/load.c b/extlib/benz/load.c index f1a8f26c..f055a2d1 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -18,9 +18,9 @@ pic_load(pic_state *pic, struct pic_port *port) } void -pic_load_cstr(pic_state *pic, const char *src) +pic_load_cstr(pic_state *pic, const char *str) { - struct pic_port *port = pic_open_input_string(pic, src); + struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); pic_try { pic_load(pic, port); diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 423c8287..7821db08 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -239,13 +239,14 @@ pic_number_number_to_string(pic_state *pic) pic_free(pic, buf); } else { - struct pic_port *port = pic_open_output_string(pic); + xFILE *file = xfopen_buf(pic, NULL, 0, "w"); + const char *buf; + int len; - xfprintf(pic, port->file, "%f", f); - - str = pic_get_output_string(pic, port); - - pic_close_port(pic, port); + xfprintf(pic, file, "%f", f); + xfget_buf(pic, file, &buf, &len); + str = pic_str_value(pic, buf, len); + xfclose(pic, file); } return pic_obj_value(str); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 875ccf99..86373553 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -5,313 +5,25 @@ #include "picrin.h" #include "picrin/object.h" -static pic_value -pic_assert_port(pic_state *pic) -{ - struct pic_port *port; - - pic_get_args(pic, "p", &port); - - return pic_obj_value(port); -} - -/* current-(input|output|error)-port */ - -#if PIC_ENABLE_STDIO - -static int -file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { - FILE *file = cookie; - int r; - - size = 1; /* override size */ - - r = (int)fread(ptr, 1, (size_t)size, file); - if (r < size && ferror(file)) { - return -1; - } - if (r == 0 && feof(file)) { - clearerr(file); - } - return r; -} - -static int -file_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) { - FILE *file = cookie; - int r; - - r = (int)fwrite(ptr, 1, (size_t)size, file); - if (r < size) { - return -1; - } - fflush(cookie); - return r; -} - -static long -file_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { - switch (whence) { - case XSEEK_CUR: - whence = SEEK_CUR; - break; - case XSEEK_SET: - whence = SEEK_SET; - break; - case XSEEK_END: - whence = SEEK_END; - break; - } - if (fseek(cookie, pos, whence) == 0) { - return ftell(cookie); - } - return -1; -} - -static int -file_close(pic_state PIC_UNUSED(*pic), void *cookie) { - return fclose(cookie); -} - -static xFILE * -file_open(pic_state *pic, const char *name, const char *mode) { - FILE *fp; - - if ((fp = fopen(name, mode)) == NULL) { - return NULL; - } - - switch (*mode) { - case 'r': - return xfunopen(pic, fp, file_read, NULL, file_seek, file_close); - default: - return xfunopen(pic, fp, NULL, file_write, file_seek, file_close); - } -} - -PIC_NORETURN static void -file_error(pic_state *pic, const char *msg) -{ - struct pic_error *e; - - e = pic_make_error(pic, "file", msg, pic_nil_value(pic)); - - pic_raise(pic, pic_obj_value(e)); -} - struct pic_port * -pic_open_file(pic_state *pic, const char *name, int flags) { - struct pic_port *port; - xFILE *file; - char mode = 'r'; - - if ((flags & PIC_PORT_IN) == 0) { - mode = 'w'; - } - if ((file = file_open(pic, name, &mode)) == NULL) { - file_error(pic, pic_str(pic, pic_strf_value(pic, "could not open file '%s'", name))); - } - - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); - port->file = file; - port->flags = flags | PIC_PORT_OPEN; - - return port; -} - -#else - -/* null file */ - -static int -null_read(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), char PIC_UNUSED(*ptr), int PIC_UNUSED(size)) { - return 0; -} - -static int -null_write(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), const char PIC_UNUSED(*ptr), int size) { - return size; -} - -static long -null_seek(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) { - return 0; -} - -static int -null_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) { - return 0; -} - -#endif - -static void -pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) +pic_make_port(pic_state *pic, xFILE *file) { struct pic_port *port; port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = file; - port->flags = dir | PIC_PORT_OPEN; - - pic_defvar(pic, name, pic_obj_value(port), pic_make_proc(pic, pic_assert_port, 0, NULL)); -} - -#define DEFINE_STANDARD_PORT_ACCESSOR(name, var) \ - struct pic_port * \ - name(pic_state *pic) \ - { \ - pic_value obj; \ - \ - obj = pic_funcall(pic, "picrin.base", var, 0); \ - \ - return pic_port_ptr(obj); \ - } - -DEFINE_STANDARD_PORT_ACCESSOR(pic_stdin, "current-input-port") -DEFINE_STANDARD_PORT_ACCESSOR(pic_stdout, "current-output-port") -DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port") - -struct strfile { - char *buf; - long pos, end, capa; -}; - -static int -string_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) -{ - struct strfile *m = cookie; - - if (size > (int)(m->end - m->pos)) - size = (int)(m->end - m->pos); - memcpy(ptr, m->buf + m->pos, size); - m->pos += size; - return size; -} - -static int -string_write(pic_state *pic, void *cookie, const char *ptr, int size) -{ - struct strfile *m = cookie; - - if (m->pos + size >= m->capa) { - m->capa = (m->pos + size) * 2; - m->buf = pic_realloc(pic, m->buf, m->capa); - } - memcpy(m->buf + m->pos, ptr, size); - m->pos += size; - if (m->end < m->pos) - m->end = m->pos; - return size; -} - -static long -string_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) -{ - struct strfile *m = cookie; - - switch (whence) { - case XSEEK_SET: - m->pos = pos; - break; - case XSEEK_CUR: - m->pos += pos; - break; - case XSEEK_END: - m->pos = m->end + pos; - break; - } - - return m->pos; -} - -static int -string_close(pic_state *pic, void *cookie) -{ - struct strfile *m = cookie; - - pic_free(pic, m->buf); - pic_free(pic, m); - return 0; -} - -static xFILE * -string_open(pic_state *pic, const char *data, size_t size) -{ - struct strfile *m; - xFILE *file; - - m = pic_malloc(pic, sizeof(struct strfile)); - m->buf = pic_malloc(pic, size); - m->pos = 0; - m->end = size; - m->capa = size; - - - if (data != NULL) { - memcpy(m->buf, data, size); - file = xfunopen(pic, m, string_read, NULL, string_seek, string_close); - } else { - file = xfunopen(pic, m, NULL, string_write, string_seek, string_close); - } - - if (file == NULL) { - string_close(pic, m); - pic_error(pic, "", "could not open new output string/bytevector port", pic_nil_value(pic)); - } - return file; -} - -struct pic_port * -pic_open_input_string(pic_state *pic, const char *str) -{ - struct pic_port *port; - - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); - port->file = string_open(pic, str, strlen(str)); - port->flags = PIC_PORT_IN | PIC_PORT_OPEN; - return port; } -struct pic_port * -pic_open_output_string(pic_state *pic) -{ - struct pic_port *port; - - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); - port->file = string_open(pic, NULL, 0); - port->flags = PIC_PORT_OUT | PIC_PORT_OPEN; - - return port; -} - -struct pic_string * -pic_get_output_string(pic_state *pic, struct pic_port *port) -{ - struct strfile *s; - - if (port->file->vtable.write != string_write) { - pic_errorf(pic, "get-output-string: port is not made by open-output-string"); - } - - xfflush(pic, port->file); - - s = port->file->vtable.cookie; - - return pic_str_value(pic, s->buf, s->end); -} - void pic_close_port(pic_state *pic, struct pic_port *port) { - if ((port->flags & PIC_PORT_OPEN) == 0) { + if (port->file->flag == 0) { return; } if (xfclose(pic, port->file) == EOF) { pic_errorf(pic, "close-port: failure"); } - port->flags &= ~PIC_PORT_OPEN; } static pic_value @@ -321,10 +33,9 @@ pic_port_input_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_IN) != 0) { + if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_READ) != 0) { return pic_true_value(pic); - } - else { + } else { return pic_false_value(pic); } } @@ -336,7 +47,7 @@ pic_port_output_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(pic, v) && (pic_port_ptr(v)->flags & PIC_PORT_OUT) != 0) { + if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_WRITE) != 0) { return pic_true_value(pic); } else { @@ -379,7 +90,7 @@ pic_port_port_open_p(pic_state *pic) pic_get_args(pic, "p", &port); - return pic_bool_value(pic, port->flags & PIC_PORT_OPEN); + return pic_bool_value(pic, port->file->flag != 0); } static pic_value @@ -394,80 +105,65 @@ pic_port_close_port(pic_state *pic) return pic_undef_value(pic); } -#define assert_port_profile(port, flgs, caller) do { \ - if ((port->flags & (flgs)) != (flgs)) { \ - switch (flgs) { \ - case PIC_PORT_IN: \ +#define assert_port_profile(port, flags, caller) do { \ + if ((port->file->flag & (flags)) != (flags)) { \ + switch (flags) { \ + case X_WRITE: \ pic_errorf(pic, caller ": expected output port"); \ - case PIC_PORT_OUT: \ + case X_READ: \ pic_errorf(pic, caller ": expected input port"); \ } \ } \ - if ((port->flags & PIC_PORT_OPEN) == 0) { \ + if (port->file->flag == 0) { \ pic_errorf(pic, caller ": expected open port"); \ } \ } while (0) static pic_value -pic_port_open_input_blob(pic_state *pic) +pic_port_open_input_bytevector(pic_state *pic) { - struct pic_port *port; struct pic_blob *blob; + xFILE *file; pic_get_args(pic, "b", &blob); - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); - port->file = string_open(pic, (const char *)blob->data, blob->len); - port->flags = PIC_PORT_IN | PIC_PORT_OPEN; + file = xfopen_buf(pic, (const char *)blob->data, blob->len, "r"); - return pic_obj_value(port); + return pic_obj_value(pic_make_port(pic, file)); } static pic_value pic_port_open_output_bytevector(pic_state *pic) { - struct pic_port *port; - pic_get_args(pic, ""); - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); - port->file = string_open(pic, NULL, 0); - port->flags = PIC_PORT_OUT | PIC_PORT_OPEN; - - return pic_obj_value(port); + return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w"))); } static pic_value pic_port_get_output_bytevector(pic_state *pic) { struct pic_port *port = pic_stdout(pic); - struct pic_blob *blob; - struct strfile *s; + const char *buf; + int len; pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT, "get-output-bytevector"); + assert_port_profile(port, X_WRITE, "get-output-bytevector"); - if (port->file->vtable.write != string_write) { - pic_errorf(pic, "get-output-bytevector: port is not made by open-output-bytevector"); + if (xfget_buf(pic, port->file, &buf, &len) < 0) { + pic_errorf(pic, "port was not created by open-output-bytevector"); } - - xfflush(pic, port->file); - - s = port->file->vtable.cookie; - - blob = pic_blob_value(pic, (unsigned char *)s->buf, s->end); - - return pic_obj_value(blob); + return pic_obj_value(pic_blob_value(pic, (unsigned char *)buf, len)); } static pic_value -pic_port_read_byte(pic_state *pic){ +pic_port_read_u8(pic_state *pic){ struct pic_port *port = pic_stdin(pic); int c; pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN, "read-u8"); + assert_port_profile(port, X_READ, "read-u8"); if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(pic); } @@ -476,14 +172,14 @@ pic_port_read_byte(pic_state *pic){ } static pic_value -pic_port_peek_byte(pic_state *pic) +pic_port_peek_u8(pic_state *pic) { int c; struct pic_port *port = pic_stdin(pic); pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN, "peek-u8"); + assert_port_profile(port, X_READ, "peek-u8"); c = xfgetc(pic, port->file); if (c == EOF) { @@ -496,15 +192,15 @@ pic_port_peek_byte(pic_state *pic) } static pic_value -pic_port_byte_ready_p(pic_state *pic) +pic_port_u8_ready_p(pic_state *pic) { struct pic_port *port = pic_stdin(pic); pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN, "u8-ready?"); + assert_port_profile(port, X_READ, "u8-ready?"); - return pic_true_value(pic); /* FIXME: always returns #t */ + return pic_true_value(pic); /* FIXME: always returns #t */ } @@ -517,7 +213,7 @@ pic_port_read_bytevector(pic_state *pic) pic_get_args(pic, "i|p", &k, &port); - assert_port_profile(port, PIC_PORT_IN, "read-bytevector"); + assert_port_profile(port, X_READ, "read-bytevector"); blob = pic_blob_value(pic, 0, k); @@ -550,7 +246,7 @@ pic_port_read_bytevector_ip(pic_state *pic) end = bv->len; } - assert_port_profile(port, PIC_PORT_IN, "read-bytevector!"); + assert_port_profile(port, X_READ, "read-bytevector!"); if (end < start) { pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index"); @@ -572,14 +268,14 @@ pic_port_read_bytevector_ip(pic_state *pic) } static pic_value -pic_port_write_byte(pic_state *pic) +pic_port_write_u8(pic_state *pic) { int i; struct pic_port *port = pic_stdout(pic); pic_get_args(pic, "i|p", &i, &port); - assert_port_profile(port, PIC_PORT_OUT, "write-u8"); + assert_port_profile(port, X_WRITE, "write-u8"); xfputc(pic, i, port->file); return pic_undef_value(pic); @@ -602,7 +298,7 @@ pic_port_write_bytevector(pic_state *pic) end = blob->len; } - assert_port_profile(port, PIC_PORT_OUT, "write-bytevector"); + assert_port_profile(port, X_WRITE, "write-bytevector"); for (i = start; i < end; ++i) { xfputc(pic, blob->data[i], port->file); @@ -617,40 +313,33 @@ pic_port_flush(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT, "flush-output-port"); + assert_port_profile(port, X_WRITE, "flush-output-port"); xfflush(pic, port->file); return pic_undef_value(pic); } +static pic_value +coerce_port(pic_state *pic) +{ + struct pic_port *port; + + pic_get_args(pic, "p", &port); + + return pic_obj_value(port); +} + +#define DEFINE_PORT(pic, name, file) \ + pic_defvar(pic, name, pic_obj_value(pic_make_port(pic, file)), coerce) + void pic_init_port(pic_state *pic) { -#if PIC_ENABLE_STDIO -# define FILE_VTABLE { 0, file_read, file_write, file_seek, file_close } -#else -# define FILE_VTABLE { 0, null_read, null_write, null_seek, null_close } -#endif + struct pic_proc *coerce = pic_lambda(pic, coerce_port, 0); - static const xFILE skel[3] = { - { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_READ }, - { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_LNBUF }, - { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_UNBUF } - }; - - pic->files[0] = skel[0]; - pic->files[1] = skel[1]; - pic->files[2] = skel[2]; - -#if PIC_ENABLE_STDIO - pic->files[0].vtable.cookie = stdin; - pic->files[1].vtable.cookie = stdout; - pic->files[2].vtable.cookie = stderr; -#endif - - pic_define_standard_port(pic, "current-input-port", xstdin, PIC_PORT_IN); - pic_define_standard_port(pic, "current-output-port", xstdout, PIC_PORT_OUT); - pic_define_standard_port(pic, "current-error-port", xstderr, PIC_PORT_OUT); + DEFINE_PORT(pic, "current-input-port", xstdin); + DEFINE_PORT(pic, "current-output-port", xstdout); + DEFINE_PORT(pic, "current-error-port", xstderr); pic_defun(pic, "port?", pic_port_port_p); pic_defun(pic, "input-port?", pic_port_input_port_p); @@ -661,20 +350,20 @@ pic_init_port(pic_state *pic) pic_defun(pic, "eof-object?", pic_port_eof_object_p); pic_defun(pic, "eof-object", pic_port_eof_object); - /* string I/O */ - pic_defun(pic, "open-input-bytevector", pic_port_open_input_blob); - pic_defun(pic, "open-output-bytevector", pic_port_open_output_bytevector); - pic_defun(pic, "get-output-bytevector", pic_port_get_output_bytevector); - /* input */ - pic_defun(pic, "read-u8", pic_port_read_byte); - pic_defun(pic, "peek-u8", pic_port_peek_byte); - pic_defun(pic, "u8-ready?", pic_port_byte_ready_p); + pic_defun(pic, "read-u8", pic_port_read_u8); + pic_defun(pic, "peek-u8", pic_port_peek_u8); + pic_defun(pic, "u8-ready?", pic_port_u8_ready_p); pic_defun(pic, "read-bytevector", pic_port_read_bytevector); pic_defun(pic, "read-bytevector!", pic_port_read_bytevector_ip); /* output */ - pic_defun(pic, "write-u8", pic_port_write_byte); + pic_defun(pic, "write-u8", pic_port_write_u8); pic_defun(pic, "write-bytevector", pic_port_write_bytevector); pic_defun(pic, "flush-output-port", pic_port_flush); + + /* string I/O */ + pic_defun(pic, "open-input-bytevector", pic_port_open_input_bytevector); + pic_defun(pic, "open-output-bytevector", pic_port_open_output_bytevector); + pic_defun(pic, "get-output-bytevector", pic_port_get_output_bytevector); } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index f096564b..fbad22f7 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -842,7 +842,7 @@ pic_read(pic_state *pic, struct pic_port *port) pic_value pic_read_cstr(pic_state *pic, const char *str) { - struct pic_port *port = pic_open_input_string(pic, str); + struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); pic_value form; pic_try { diff --git a/extlib/benz/state.c b/extlib/benz/state.c index a6e0ee97..1cd7c969 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -188,7 +188,6 @@ pic_init_core(pic_state *pic) pic_state * pic_open(pic_allocf allocf, void *userdata) { - struct pic_port *pic_make_standard_port(pic_state *, xFILE *, short); char t; pic_state *pic; @@ -280,6 +279,17 @@ pic_open(pic_allocf allocf, void *userdata) /* file pool */ memset(pic->files, 0, sizeof pic->files); +#if PIC_ENABLE_STDIO + xfopen_file(pic, stdin, "r"); + xfopen_file(pic, stdout, "w"); + xfopen_file(pic, stderr, "w"); + pic->files[1].flag |= X_LNBUF; + pic->files[2].flag |= X_UNBUF; +#else + xfopen_null(pic, "r"); + xfopen_null(pic, "w"); + xfopen_null(pic, "w"); +#endif /* parameter table */ pic->ptable = pic_nil_value(pic); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 0472fae3..d449953b 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -309,7 +309,7 @@ pic_str(pic_state *pic, struct pic_string *str) } static void -pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) +vfstrf(pic_state *pic, xFILE *file, const char *fmt, va_list ap) { char c; @@ -377,15 +377,17 @@ pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) struct pic_string * pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap) { - struct pic_port *port; struct pic_string *str; + xFILE *file; + const char *buf; + int len; - port = pic_open_output_string(pic); + file = xfopen_buf(pic, NULL, 0, "w"); - pic_vfformat(pic, port->file, fmt, ap); - str = pic_get_output_string(pic, port); - - pic_close_port(pic, port); + vfstrf(pic, file, fmt, ap); + xfget_buf(pic, file, &buf, &len); + str = pic_str_value(pic, buf, len); + xfclose(pic, file); return str; } From 540261132339110c29589e143d4760ed680d5171 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 05:58:34 +0900 Subject: [PATCH 043/119] remove port.h --- extlib/benz/include/picrin.h | 4 +++- extlib/benz/include/picrin/object.h | 10 ++++++++++ extlib/benz/include/picrin/port.h | 26 -------------------------- 3 files changed, 13 insertions(+), 27 deletions(-) delete mode 100644 extlib/benz/include/picrin/port.h diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index a2944b44..0a4ea451 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -272,7 +272,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/cont.h" #include "picrin/macro.h" -#include "picrin/port.h" void *pic_default_allocf(void *, void *, size_t); @@ -283,6 +282,9 @@ void *pic_default_allocf(void *, void *, size_t); struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); +struct pic_port *pic_make_port(pic_state *, xFILE *file); +void pic_close_port(pic_state *, struct pic_port *port); + #define pic_void(exec) \ pic_void_(PIC_GENSYM(ai), exec) #define pic_void_(ai,exec) do { \ diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index b5d0c8b4..a15b1def 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -190,6 +190,16 @@ struct pic_error { struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); +/* port */ + +struct pic_port { + PIC_OBJECT_HEADER + xFILE *file; +}; + +#define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) + + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/port.h b/extlib/benz/include/picrin/port.h deleted file mode 100644 index 95ddc487..00000000 --- a/extlib/benz/include/picrin/port.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_PORT_H -#define PICRIN_PORT_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_port { - PIC_OBJECT_HEADER - xFILE *file; -}; - -#define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) - -struct pic_port *pic_make_port(pic_state *, xFILE *file); -void pic_close_port(pic_state *, struct pic_port *port); - -#if defined(__cplusplus) -} -#endif - -#endif From fe54b1315ba4fa5507d84c21df35d64c5cbd9f46 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 13:58:45 +0900 Subject: [PATCH 044/119] pic_obj_alloc is not a public api --- extlib/benz/include/picrin.h | 9 ++++----- extlib/benz/include/picrin/object.h | 3 +++ extlib/benz/state.c | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 0a4ea451..03d5b6f0 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -196,8 +196,6 @@ struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *typ #define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT) #define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL) -#include "picrin/type.h" - int pic_type(pic_state *, pic_value); const char *pic_typename(pic_state *, int); @@ -217,7 +215,7 @@ pic_value pic_cdar(pic_state *, pic_value); pic_value pic_cddr(pic_state *, pic_value); /* list */ -pic_value pic_nil_value(pic_state *); +PIC_INLINE pic_value pic_nil_value(pic_state *); 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, ...); @@ -266,8 +264,11 @@ struct pic_string *pic_str_sub(pic_state *, struct pic_string *, int, int); int pic_str_cmp(pic_state *, struct pic_string *, struct pic_string *); int pic_str_hash(pic_state *, struct pic_string *); + /* extra stuff */ + +#include "picrin/type.h" #include "picrin/state.h" #include "picrin/cont.h" @@ -280,8 +281,6 @@ void *pic_default_allocf(void *, void *, size_t); pic_errorf(pic, "expected " #type ", but got ~s", v); \ } -struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); - struct pic_port *pic_make_port(pic_state *, xFILE *file); void pic_close_port(pic_state *, struct pic_port *port); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index a15b1def..c4762df4 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -10,6 +10,9 @@ extern "C" { #endif +struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); + + /* symbol & identifier */ struct pic_id { diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 1cd7c969..d8dc6378 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" static void pic_init_features(pic_state *pic) From f0386657be8ec72a4b816aaecd0cb310298d20d6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 14:50:12 +0900 Subject: [PATCH 045/119] cosmetic changes --- extlib/benz/file.c | 72 ++++++++----------------------- extlib/benz/include/picrin.h | 56 ++++++++++++------------ extlib/benz/include/picrin/file.h | 71 +++++++++--------------------- extlib/benz/port.c | 3 ++ extlib/benz/read.c | 3 ++ 5 files changed, 74 insertions(+), 131 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 4b4b274e..d5dc60b9 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -1,5 +1,9 @@ #include "picrin.h" +#ifndef EOF +# define EOF (-1) +#endif + xFILE *xfunopen(pic_state *pic, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)) { xFILE *fp; @@ -137,6 +141,15 @@ int xfflush(pic_state *pic, xFILE *f) { return retval; } +#define xgetc(pic, p) \ + ((--(p)->cnt >= 0) \ + ? (unsigned char) *(p)->ptr++ \ + : x_fillbuf((pic), p)) +#define xputc(pic, x, p) \ + ((--(p)->cnt >= 0 && !(((p)->flag & X_LNBUF) && (x) == '\n')) \ + ? *(p)->ptr++ = (x) \ + : x_flushbuf((pic), (x), (p))) + int xfputc(pic_state *pic, int x, xFILE *fp) { return xputc(pic, x, fp); } @@ -174,35 +187,6 @@ char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { return (c == EOF && buf == s) ? NULL : s; } -int xputs(pic_state *pic, const char *s) { - int i = 1; - - while(*s != '\0') { - if (xputchar(pic, *s++) == EOF) - return EOF; - i++; - } - if (xputchar(pic, '\n') == EOF) { - return EOF; - } - return i; -} - -char *xgets(pic_state *pic, char *s) { - int c; - char *buf; - - xfflush(pic, NULL); - - buf = s; - while ((c = xgetchar(pic)) != EOF && c != '\n') { - *buf++ = c; - } - *buf = '\0'; - - return (c == EOF && buf == s) ? NULL : s; -} - int xungetc(int c, xFILE *fp) { unsigned char uc = c; @@ -270,25 +254,6 @@ long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { return 0; } -long xftell(pic_state *pic, xFILE *fp) { - return xfseek(pic, fp, 0, XSEEK_CUR); -} - -void xrewind(pic_state *pic, xFILE *fp) { - xfseek(pic, fp, 0, XSEEK_SET); - xclearerr(fp); -} - -int xprintf(pic_state *pic, const char *fmt, ...) { - va_list ap; - int n; - - va_start(ap, fmt); - n = xvfprintf(pic, xstdout, fmt, ap); - va_end(ap); - return n; -} - int xfprintf(pic_state *pic, xFILE *stream, const char *fmt, ...) { va_list ap; int n; @@ -435,12 +400,13 @@ file_close(pic_state PIC_UNUSED(*pic), void *cookie) { } xFILE *xfopen_file(pic_state *pic, FILE *fp, const char *mode) { - switch (*mode) { - case 'r': - return xfunopen(pic, fp, file_read, 0, file_seek, file_close); - default: - return xfunopen(pic, fp, 0, file_write, file_seek, file_close); + xFILE *f; + if (*mode == 'r') { + f = xfunopen(pic, fp, file_read, 0, file_seek, file_close); + } else { + f = xfunopen(pic, fp, 0, file_write, file_seek, file_close); } + return f; } #endif diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 03d5b6f0..b32f641c 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -149,33 +149,35 @@ struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len); struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); -#define PIC_TYPE_INVALID 1 -#define PIC_TYPE_FLOAT 2 -#define PIC_TYPE_INT 3 -#define PIC_TYPE_CHAR 4 -#define PIC_TYPE_EOF 5 -#define PIC_TYPE_UNDEF 6 -#define PIC_TYPE_TRUE 8 -#define PIC_TYPE_NIL 7 -#define PIC_TYPE_FALSE 9 -#define PIC_IVAL_END 10 -/* --------------------- */ -#define PIC_TYPE_STRING 16 -#define PIC_TYPE_VECTOR 17 -#define PIC_TYPE_BLOB 18 -#define PIC_TYPE_PROC 19 -#define PIC_TYPE_PORT 20 -#define PIC_TYPE_ERROR 21 -#define PIC_TYPE_ID 22 -#define PIC_TYPE_ENV 23 -#define PIC_TYPE_DATA 24 -#define PIC_TYPE_DICT 25 -#define PIC_TYPE_WEAK 26 -#define PIC_TYPE_RECORD 27 -#define PIC_TYPE_SYMBOL 28 -#define PIC_TYPE_PAIR 29 -#define PIC_TYPE_CXT 30 -#define PIC_TYPE_CP 31 +enum { + PIC_TYPE_INVALID = 1, + PIC_TYPE_FLOAT = 2, + PIC_TYPE_INT = 3, + PIC_TYPE_CHAR = 4, + PIC_TYPE_EOF = 5, + PIC_TYPE_UNDEF = 6, + PIC_TYPE_TRUE = 8, + PIC_TYPE_NIL = 7, + PIC_TYPE_FALSE = 9, + PIC_IVAL_END = 10, +/* -------------------- */ + PIC_TYPE_STRING = 16, + PIC_TYPE_VECTOR = 17, + PIC_TYPE_BLOB = 18, + PIC_TYPE_PROC = 19, + PIC_TYPE_PORT = 20, + PIC_TYPE_ERROR = 21, + PIC_TYPE_ID = 22, + PIC_TYPE_ENV = 23, + PIC_TYPE_DATA = 24, + PIC_TYPE_DICT = 25, + PIC_TYPE_WEAK = 26, + PIC_TYPE_RECORD = 27, + PIC_TYPE_SYMBOL = 28, + PIC_TYPE_PAIR = 29, + PIC_TYPE_CXT = 30, + PIC_TYPE_CP = 31 +}; #define pic_undef_p(pic,v) (pic_type(pic,v) == PIC_TYPE_UNDEF) #define pic_int_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INT) diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index d23479ad..2118934c 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -5,10 +5,6 @@ extern "C" { #endif -#ifndef EOF -# define EOF (-1) -#endif - #define XBUFSIZ 1024 #define XOPEN_MAX 1024 @@ -33,7 +29,7 @@ typedef struct { #define xstdout (&pic->files[1]) #define xstderr (&pic->files[2]) -enum _flags { +enum { X_READ = 01, X_WRITE = 02, X_UNBUF = 04, @@ -45,23 +41,29 @@ enum _flags { #define xclearerr(p) ((p)->flag &= ~(X_EOF | X_ERR)) #define xfeof(p) (((p)->flag & X_EOF) != 0) #define xferror(p) (((p)->flag & X_ERR) != 0) -#define xfileno(p) ((p)->fd) -#define xgetc(pic, p) \ - ((--(p)->cnt >= 0) \ - ? (unsigned char) *(p)->ptr++ \ - : x_fillbuf((pic), p)) -#define xputc(pic, x, p) \ - ((--(p)->cnt >= 0 && !(((p)->flag & X_LNBUF) && (x) == '\n')) \ - ? *(p)->ptr++ = (x) \ - : x_flushbuf((pic), (x), (p))) -#define xgetchar(pic) xgetc((pic), xstdin) -#define xputchar(pic, x) xputc((pic), (x), xstdout) - -/* resource aquisition */ xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); + +enum { + XSEEK_CUR, + XSEEK_END, + XSEEK_SET +}; + +size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); +size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); +long xfseek(pic_state *, xFILE *, long, int); +int xfflush(pic_state *, xFILE *); int xfclose(pic_state *, xFILE *); +int xfputc(pic_state *, int, xFILE *); +int xfgetc(pic_state *, xFILE *); +int xfputs(pic_state *, const char *, xFILE *); +char *xfgets(pic_state *, char *, int, xFILE *); +int xungetc(int, xFILE *); +int xfprintf(pic_state *, xFILE *, const char *, ...); +int xvfprintf(pic_state *, xFILE *, const char *, va_list); + #if PIC_ENABLE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); #endif @@ -69,39 +71,6 @@ xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len); xFILE *xfopen_null(pic_state *, const char *mode); -/* buffer management */ -int x_fillbuf(pic_state *, xFILE *); -int x_flushbuf(pic_state *, int, xFILE *); -int xfflush(pic_state *, xFILE *); - -/* direct IO */ -size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); -size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); - -enum { - XSEEK_CUR, - XSEEK_END, - XSEEK_SET -}; - -/* indicator positioning */ -long xfseek(pic_state *, xFILE *, long, int); -long xftell(pic_state *, xFILE *); -void xrewind(pic_state *, xFILE *); - -/* character IO */ -int xfputc(pic_state *, int, xFILE *); -int xfgetc(pic_state *, xFILE *); -int xfputs(pic_state *, const char *, xFILE *); -char *xfgets(pic_state *, char *, int, xFILE *); -int xputs(pic_state *, const char *); -int xungetc(int, xFILE *); - -/* formatted I/O */ -int xprintf(pic_state *, const char *, ...); -int xfprintf(pic_state *, xFILE *, const char *, ...); -int xvfprintf(pic_state *, xFILE *, const char *, va_list); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 86373553..136de293 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -5,6 +5,9 @@ #include "picrin.h" #include "picrin/object.h" +#undef EOF +#define EOF (-1) + struct pic_port * pic_make_port(pic_state *pic, xFILE *file) { diff --git a/extlib/benz/read.c b/extlib/benz/read.c index fbad22f7..11d46184 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -5,6 +5,9 @@ #include "picrin.h" #include "picrin/object.h" +#undef EOF +#define EOF (-1) + KHASH_DEFINE(read, int, pic_value, kh_int_hash_func, kh_int_hash_equal) static pic_value read(pic_state *pic, struct pic_port *port, int c); From 4d38e08c985e82391735c5de46b540a93858e70b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 16:17:13 +0900 Subject: [PATCH 046/119] pic_gc_arena_preserve -> pic_enter --- contrib/20.r7rs/src/system.c | 10 +++++----- extlib/benz/debug.c | 6 +++--- extlib/benz/error.c | 2 +- extlib/benz/eval.c | 28 ++++++++++++++-------------- extlib/benz/gc.c | 6 +++--- extlib/benz/include/picrin.h | 10 +++++----- extlib/benz/load.c | 4 ++-- extlib/benz/macro.c | 12 ++++++------ extlib/benz/number.c | 14 +++++++------- extlib/benz/pair.c | 14 +++++++------- extlib/benz/proc.c | 18 +++++++++--------- extlib/benz/read.c | 8 ++++---- extlib/benz/state.c | 12 ++++++------ extlib/benz/symbol.c | 2 +- 14 files changed, 73 insertions(+), 73 deletions(-) diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 5d2d1c5b..8346e3b3 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -19,10 +19,10 @@ pic_system_cmdline(pic_state *pic) pic_get_args(pic, ""); for (i = 0; i < picrin_argc; ++i) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); v = pic_cons(pic, pic_obj_value(pic_cstr_value(pic, picrin_argv[i])), v); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); } return pic_reverse(pic, v); @@ -96,7 +96,7 @@ pic_system_getenvs(pic_state *pic) { char **envp; pic_value data = pic_nil_value(pic); - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_get_args(pic, ""); @@ -117,8 +117,8 @@ pic_system_getenvs(pic_state *pic) /* push */ data = pic_cons(pic, pic_cons(pic, pic_obj_value(key), pic_obj_value(val)), data); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, data); + pic_leave(pic, ai); + pic_protect(pic, data); } return data; diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index c9c8c2af..b8e5e215 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -8,7 +8,7 @@ struct pic_string * pic_get_backtrace(pic_state *pic) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_callinfo *ci; struct pic_string *trace; @@ -27,8 +27,8 @@ pic_get_backtrace(pic_state *pic) } } - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, pic_obj_value(trace)); + pic_leave(pic, ai); + pic_protect(pic, pic_obj_value(trace)); return trace; } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 16c259d6..2b203ed1 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -117,7 +117,7 @@ pic_raise_continuable(pic_state *pic, pic_value err) handler = pic_pop_handler(pic); - pic_gc_protect(pic, pic_obj_value(handler)); + pic_protect(pic, pic_obj_value(handler)); v = pic_call(pic, handler, 1, err); diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 72f30482..2d112e74 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -9,7 +9,7 @@ static pic_value optimize_beta(pic_state *pic, pic_value expr) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value functor, formals, args, tmp, val, it, defs; if (! pic_list_p(pic, expr)) @@ -34,8 +34,8 @@ optimize_beta(pic_state *pic, pic_value expr) } expr = pic_reverse(pic, tmp); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, expr); + pic_leave(pic, ai); + pic_protect(pic, expr); functor = pic_list_ref(pic, expr, 0); if (pic_pair_p(pic, functor) && pic_eq_p(pic, pic_car(pic, functor), pic_obj_value(pic->sLAMBDA))) { @@ -57,8 +57,8 @@ optimize_beta(pic_state *pic, pic_value expr) } exit: - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, expr); + pic_leave(pic, ai); + pic_protect(pic, expr); return expr; } @@ -332,13 +332,13 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) static pic_value analyze(pic_state *pic, analyze_scope *scope, pic_value obj) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value res; res = analyze_node(pic, scope, obj); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, res); + pic_leave(pic, ai); + pic_protect(pic, res); return res; } @@ -828,23 +828,23 @@ pic_codegen(pic_state *pic, pic_value obj) return codegen_context_destroy(pic, cxt); } -#define SAVE(pic, ai, obj) pic_gc_arena_restore(pic, ai); pic_gc_protect(pic, obj) +#define SAVE(pic, ai, obj) pic_leave(pic, ai); pic_protect(pic, obj) struct pic_proc * pic_compile(pic_state *pic, pic_value obj) { struct pic_irep *irep; struct pic_proc *proc; - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); #if DEBUG - fprintf(stdout, "ai = %zu\n", pic_gc_arena_preserve(pic)); + fprintf(stdout, "ai = %zu\n", pic_enter(pic)); fprintf(stdout, "# input expression\n"); pic_write(pic, obj); fprintf(stdout, "\n"); - fprintf(stdout, "ai = %zu\n", pic_gc_arena_preserve(pic)); + fprintf(stdout, "ai = %zu\n", pic_enter(pic)); #endif /* optimize */ @@ -853,7 +853,7 @@ pic_compile(pic_state *pic, pic_value obj) fprintf(stdout, "## optimize completed\n"); pic_write(pic, obj); fprintf(stdout, "\n"); - fprintf(stdout, "ai = %zu\n", pic_gc_arena_preserve(pic)); + fprintf(stdout, "ai = %zu\n", pic_enter(pic)); #endif SAVE(pic, ai, obj); @@ -864,7 +864,7 @@ pic_compile(pic_state *pic, pic_value obj) fprintf(stdout, "## analyzer completed\n"); pic_write(pic, obj); fprintf(stdout, "\n"); - fprintf(stdout, "ai = %zu\n", pic_gc_arena_preserve(pic)); + fprintf(stdout, "ai = %zu\n", pic_enter(pic)); #endif SAVE(pic, ai, obj); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index cf32889d..b6958e31 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -142,7 +142,7 @@ gc_protect(pic_state *pic, struct pic_object *obj) } pic_value -pic_gc_protect(pic_state *pic, pic_value v) +pic_protect(pic_state *pic, pic_value v) { if (! pic_obj_p(pic, v)) return v; @@ -153,13 +153,13 @@ pic_gc_protect(pic_state *pic, pic_value v) } size_t -pic_gc_arena_preserve(pic_state *pic) +pic_enter(pic_state *pic) { return pic->arena_idx; } void -pic_gc_arena_restore(pic_state *pic, size_t state) +pic_leave(pic_state *pic, size_t state) { pic->arena_idx = state; } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index b32f641c..f0a05e86 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -83,9 +83,9 @@ void pic_free(pic_state *, void *); typedef pic_value (*pic_func_t)(pic_state *); void *pic_alloca(pic_state *, size_t); -pic_value pic_gc_protect(pic_state *, pic_value); -size_t pic_gc_arena_preserve(pic_state *); -void pic_gc_arena_restore(pic_state *, size_t); +size_t pic_enter(pic_state *); +void pic_leave(pic_state *, size_t); +pic_value pic_protect(pic_state *, pic_value); void pic_gc(pic_state *); void pic_add_feature(pic_state *, const char *feature); @@ -289,9 +289,9 @@ void pic_close_port(pic_state *, struct pic_port *port); #define pic_void(exec) \ pic_void_(PIC_GENSYM(ai), exec) #define pic_void_(ai,exec) do { \ - size_t ai = pic_gc_arena_preserve(pic); \ + size_t ai = pic_enter(pic); \ exec; \ - pic_gc_arena_restore(pic, ai); \ + pic_leave(pic, ai); \ } while (0) pic_value pic_read(pic_state *, struct pic_port *); diff --git a/extlib/benz/load.c b/extlib/benz/load.c index f055a2d1..465ef80f 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -8,12 +8,12 @@ void pic_load(pic_state *pic, struct pic_port *port) { pic_value form; - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); while (! pic_eof_p(pic, form = pic_read(pic, port))) { pic_eval(pic, form, pic_current_library(pic)); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); } } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index c2442ad8..f8bae3c2 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -170,7 +170,7 @@ expand_quote(pic_state *pic, pic_value expr) static pic_value expand_list(pic_state *pic, pic_value obj, struct pic_env *env, pic_value deferred) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value x, head, tail; if (pic_pair_p(pic, obj)) { @@ -181,8 +181,8 @@ expand_list(pic_state *pic, pic_value obj, struct pic_env *env, pic_value deferr x = expand(pic, obj, env, deferred); } - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, x); + pic_leave(pic, ai); + pic_protect(pic, x); return x; } @@ -329,13 +329,13 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer static pic_value expand(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value v; v = expand_node(pic, expr, env, deferred); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, v); + pic_leave(pic, ai); + pic_protect(pic, v); return v; } diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 7821db08..fe25d20c 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -288,33 +288,33 @@ pic_number_string_to_number(pic_state *pic) void pic_init_number(pic_state *pic) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_defun(pic, "number?", pic_number_number_p); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); pic_defun(pic, "exact?", pic_number_exact_p); pic_defun(pic, "inexact?", pic_number_inexact_p); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); pic_defun(pic, "inexact", pic_number_inexact); pic_defun(pic, "exact", pic_number_exact); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); pic_defun(pic, "=", pic_number_eq); pic_defun(pic, "<", pic_number_lt); pic_defun(pic, ">", pic_number_gt); pic_defun(pic, "<=", pic_number_le); pic_defun(pic, ">=", pic_number_ge); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); pic_defun(pic, "+", pic_number_add); pic_defun(pic, "-", pic_number_sub); pic_defun(pic, "*", pic_number_mul); pic_defun(pic, "/", pic_number_div); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); pic_defun(pic, "number->string", pic_number_number_to_string); pic_defun(pic, "string->number", pic_number_string_to_number); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); } diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index c49489c0..6507c49e 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -201,15 +201,15 @@ pic_length(pic_state *pic, pic_value obj) pic_value pic_reverse(pic_state *pic, pic_value list) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value v, acc, it; acc = pic_nil_value(pic); pic_for_each(v, list, it) { acc = pic_cons(pic, v, acc); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, acc); + pic_leave(pic, ai); + pic_protect(pic, acc); } return acc; } @@ -217,16 +217,16 @@ pic_reverse(pic_state *pic, pic_value list) pic_value pic_append(pic_state *pic, pic_value xs, pic_value ys) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value x, it; xs = pic_reverse(pic, xs); pic_for_each (x, xs, it) { ys = pic_cons(pic, x, ys); - pic_gc_arena_restore(pic, ai); - pic_gc_protect(pic, xs); - pic_gc_protect(pic, ys); + pic_leave(pic, ai); + pic_protect(pic, xs); + pic_protect(pic, ys); } return ys; } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index b8b54339..9ce9dc70 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -337,7 +337,7 @@ pic_value pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) { pic_code c; - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_code boot[2]; int i; @@ -528,7 +528,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) pic->sp[0] = v; pic->sp += pic->ci->retc; - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); goto L_RET; } else { @@ -546,7 +546,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) if (irep->varg) { rest = pic_nil_value(pic); for (i = 0; i < ci->argc - irep->argc; ++i) { - pic_gc_protect(pic, v = POP()); + pic_protect(pic, v = POP()); rest = pic_cons(pic, v, rest); } PUSH(rest); @@ -568,7 +568,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) ci->regs = ci->fp + irep->argc + irep->localc; pic->ip = irep->code; - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); JUMP; } } @@ -631,7 +631,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) proc = pic_make_proc_irep(pic, pic->ci->irep->irep[c.a], pic->ci->cxt); PUSH(pic_obj_value(proc)); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); NEXT; } @@ -643,11 +643,11 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) CASE(OP_CONS) { pic_value a, b; check_condition(CONS, 2); - pic_gc_protect(pic, b = POP()); - pic_gc_protect(pic, a = POP()); + pic_protect(pic, b = POP()); + pic_protect(pic, a = POP()); (void)POP(); PUSH(pic_cons(pic, a, b)); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); NEXT; } CASE(OP_CAR) { @@ -785,7 +785,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) VM_END_PRINT; - return pic_gc_protect(pic, POP()); + return pic_protect(pic, POP()); } } VM_LOOP_END; } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 11d46184..ef0bfd71 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -822,7 +822,7 @@ pic_reader_destroy(pic_state *pic) pic_value pic_read(pic_state *pic, struct pic_port *port) { - size_t ai = pic_gc_arena_preserve(pic); + size_t ai = pic_enter(pic); pic_value val; int c; @@ -832,14 +832,14 @@ pic_read(pic_state *pic, struct pic_port *port) if (! pic_invalid_p(pic, val)) { break; } - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); } if (c == EOF) { return pic_eof_object(pic); } - pic_gc_arena_restore(pic, ai); - return pic_gc_protect(pic, val); + pic_leave(pic, ai); + return pic_protect(pic, val); } pic_value diff --git a/extlib/benz/state.c b/extlib/benz/state.c index d8dc6378..77763c6a 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -123,9 +123,9 @@ pic_init_core(pic_state *pic) pic_deflibrary(pic, "picrin.base"); - ai = pic_gc_arena_preserve(pic); + ai = pic_enter(pic); -#define DONE pic_gc_arena_restore(pic, ai); +#define DONE pic_leave(pic, ai); import_builtin_syntax("define"); import_builtin_syntax("set!"); @@ -298,7 +298,7 @@ pic_open(pic_allocf allocf, void *userdata) /* native stack marker */ pic->native_stack_start = &t; - ai = pic_gc_arena_preserve(pic); + ai = pic_enter(pic); #define S(slot,name) pic->slot = pic_intern_lit(pic, name) @@ -338,7 +338,7 @@ pic_open(pic_allocf allocf, void *userdata) S(sGE, ">="); S(sNOT, "not"); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); /* root tables */ pic->globals = pic_make_weak(pic); @@ -360,14 +360,14 @@ pic_open(pic_allocf allocf, void *userdata) pic_make_library(pic, "picrin.user"); pic_in_library(pic, "picrin.user"); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); /* turn on GC */ pic->gc_enable = true; pic_init_core(pic); - pic_gc_arena_restore(pic, ai); + pic_leave(pic, ai); return pic; diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index ed6bea48..2ccdb340 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -21,7 +21,7 @@ pic_intern(pic_state *pic, struct pic_string *str) it = kh_put(oblist, h, str, &ret); if (ret == 0) { /* if exists */ sym = kh_val(h, it); - pic_gc_protect(pic, pic_obj_value(sym)); + pic_protect(pic, pic_obj_value(sym)); return sym; } From 935199ea5eccae01c5416fe44e33f224979d9af3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 16:51:45 +0900 Subject: [PATCH 047/119] reader uses xFILE* directly --- extlib/benz/include/picrin/read.h | 2 +- extlib/benz/read.c | 277 +++++++++++++++--------------- 2 files changed, 140 insertions(+), 139 deletions(-) diff --git a/extlib/benz/include/picrin/read.h b/extlib/benz/include/picrin/read.h index 27c715bb..4b1e0baa 100644 --- a/extlib/benz/include/picrin/read.h +++ b/extlib/benz/include/picrin/read.h @@ -11,7 +11,7 @@ extern "C" { KHASH_DECLARE(read, int, pic_value) -typedef pic_value (*pic_reader_t)(pic_state *, struct pic_port *port, int c); +typedef pic_value (*pic_reader_t)(pic_state *, xFILE *file, int c); typedef struct { enum pic_typecase { diff --git a/extlib/benz/read.c b/extlib/benz/read.c index ef0bfd71..2441f677 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -10,8 +10,8 @@ KHASH_DEFINE(read, int, pic_value, kh_int_hash_func, kh_int_hash_equal) -static pic_value read(pic_state *pic, struct pic_port *port, int c); -static pic_value read_nullable(pic_state *pic, struct pic_port *port, int c); +static pic_value read(pic_state *pic, xFILE *file, int c); +static pic_value read_nullable(pic_state *pic, xFILE *file, int c); PIC_NORETURN static void read_error(pic_state *pic, const char *msg, pic_value irritants) @@ -20,39 +20,39 @@ read_error(pic_state *pic, const char *msg, pic_value irritants) } static int -skip(pic_state *pic, struct pic_port *port, int c) +skip(pic_state *pic, xFILE *file, int c) { while (isspace(c)) { - c = xfgetc(pic, port->file); + c = xfgetc(pic, file); } return c; } static int -next(pic_state *pic, struct pic_port *port) +next(pic_state *pic, xFILE *file) { - return xfgetc(pic, port->file); + return xfgetc(pic, file); } static int -peek(pic_state *pic, struct pic_port *port) +peek(pic_state *pic, xFILE *file) { int c; - xungetc((c = xfgetc(pic, port->file)), port->file); + xungetc((c = xfgetc(pic, file)), file); return c; } static bool -expect(pic_state *pic, struct pic_port *port, const char *str) +expect(pic_state *pic, xFILE *file, const char *str) { int c; while ((c = (int)*str++) != 0) { - if (c != peek(pic, port)) + if (c != peek(pic, file)) return false; - next(pic, port); + next(pic, file); } return true; @@ -86,26 +86,26 @@ case_fold(pic_state *pic, int c) } static pic_value -read_comment(pic_state PIC_UNUSED(*pic), struct pic_port *port, int c) +read_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int c) { do { - c = next(pic, port); + c = next(pic, file); } while (! (c == EOF || c == '\n')); return pic_invalid_value(); } static pic_value -read_block_comment(pic_state PIC_UNUSED(*pic), struct pic_port *port, int PIC_UNUSED(c)) +read_block_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int PIC_UNUSED(c)) { int x, y; int i = 1; - y = next(pic, port); + y = next(pic, file); while (y != EOF && i > 0) { x = y; - y = next(pic, port); + y = next(pic, file); if (x == '|' && y == '#') { i--; } @@ -118,84 +118,84 @@ read_block_comment(pic_state PIC_UNUSED(*pic), struct pic_port *port, int PIC_UN } static pic_value -read_datum_comment(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_datum_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - read(pic, port, next(pic, port)); + read(pic, file, next(pic, file)); return pic_invalid_value(); } static pic_value -read_directive(pic_state *pic, struct pic_port *port, int c) +read_directive(pic_state *pic, xFILE *file, int c) { - switch (peek(pic, port)) { + switch (peek(pic, file)) { case 'n': - if (expect(pic, port, "no-fold-case")) { + if (expect(pic, file, "no-fold-case")) { pic->reader.typecase = PIC_CASE_DEFAULT; return pic_invalid_value(); } break; case 'f': - if (expect(pic, port, "fold-case")) { + if (expect(pic, file, "fold-case")) { pic->reader.typecase = PIC_CASE_FOLD; return pic_invalid_value(); } break; } - return read_comment(pic, port, c); + return read_comment(pic, file, c); } static pic_value -read_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), read(pic, port, next(pic, port))); + return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), read(pic, file, next(pic, file))); } static pic_value -read_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic_obj_value(pic->sQUASIQUOTE), read(pic, port, next(pic, port))); + return pic_list(pic, 2, pic_obj_value(pic->sQUASIQUOTE), read(pic, file, next(pic, file))); } static pic_value -read_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { pic_sym *tag = pic->sUNQUOTE; - if (peek(pic, port) == '@') { + if (peek(pic, file) == '@') { tag = pic->sUNQUOTE_SPLICING; - next(pic, port); + next(pic, file); } - return pic_list(pic, 2, pic_obj_value(tag), read(pic, port, next(pic, port))); + return pic_list(pic, 2, pic_obj_value(tag), read(pic, file, next(pic, file))); } static pic_value -read_syntax_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_syntax_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, 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, file, next(pic, file))); } static pic_value -read_syntax_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_syntax_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, 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, file, next(pic, file))); } static pic_value -read_syntax_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) +read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { pic_sym *tag = pic->sSYNTAX_UNQUOTE; - if (peek(pic, port) == '@') { + if (peek(pic, file) == '@') { tag = pic->sSYNTAX_UNQUOTE_SPLICING; - next(pic, port); + next(pic, file); } - return pic_list(pic, 2, pic_obj_value(tag), read(pic, port, next(pic, port))); + return pic_list(pic, 2, pic_obj_value(tag), read(pic, file, next(pic, file))); } static pic_value -read_symbol(pic_state *pic, struct pic_port *port, int c) +read_symbol(pic_state *pic, xFILE *file, int c) { int len; char *buf; @@ -206,8 +206,8 @@ read_symbol(pic_state *pic, struct pic_port *port, int c) buf[0] = case_fold(pic, c); buf[1] = 0; - while (! isdelim(peek(pic, port))) { - c = next(pic, port); + while (! isdelim(peek(pic, file))) { + c = next(pic, file); len += 1; buf = pic_realloc(pic, buf, len + 1); buf[len - 1] = case_fold(pic, c); @@ -221,7 +221,7 @@ read_symbol(pic_state *pic, struct pic_port *port, int c) } static unsigned -read_uinteger(pic_state *pic, struct pic_port *port, int c) +read_uinteger(pic_state *pic, xFILE *file, int c) { unsigned u = 0; @@ -230,15 +230,15 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c) } u = c - '0'; - while (isdigit(c = peek(pic, port))) { - u = u * 10 + next(pic, port) - '0'; + while (isdigit(c = peek(pic, file))) { + u = u * 10 + next(pic, file) - '0'; } return u; } static pic_value -read_unsigned(pic_state *pic, struct pic_port *port, int c) +read_unsigned(pic_state *pic, xFILE *file, int c) { #define ATOF_BUF_SIZE (64) char buf[ATOF_BUF_SIZE]; @@ -250,34 +250,34 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) read_error(pic, "expected one or more digits", pic_list(pic, 1, pic_char_value(pic, c))); } buf[idx++] = (char )c; - while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) { - buf[idx++] = (char )next(pic, port); + while (isdigit(c = peek(pic, file)) && idx < ATOF_BUF_SIZE) { + buf[idx++] = (char )next(pic, file); } - if ('.' == peek(pic, port) && idx < ATOF_BUF_SIZE) { + if ('.' == peek(pic, file) && idx < ATOF_BUF_SIZE) { dpe++; - buf[idx++] = (char )next(pic, port); - while (isdigit(c = peek(pic, port)) && idx < ATOF_BUF_SIZE) { - buf[idx++] = (char )next(pic, port); + buf[idx++] = (char )next(pic, file); + while (isdigit(c = peek(pic, file)) && idx < ATOF_BUF_SIZE) { + buf[idx++] = (char )next(pic, file); } } - c = peek(pic, port); + c = peek(pic, file); if ((c == 'e' || c == 'E') && idx < (ATOF_BUF_SIZE - 2)) { dpe++; - buf[idx++] = (char )next(pic, port); - switch ((c = peek(pic, port))) { + buf[idx++] = (char )next(pic, file); + switch ((c = peek(pic, file))) { case '-': case '+': - buf[idx++] = (char )next(pic, port); + buf[idx++] = (char )next(pic, file); break; default: break; } - if (! isdigit(peek(pic, port))) { + if (! isdigit(peek(pic, file))) { 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) { - buf[idx++] = (char )next(pic, port); + while (isdigit(c = peek(pic, file)) && idx < ATOF_BUF_SIZE) { + buf[idx++] = (char )next(pic, file); } } if (idx >= ATOF_BUF_SIZE) @@ -296,9 +296,9 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_number(pic_state *pic, struct pic_port *port, int c) +read_number(pic_state *pic, xFILE *file, int c) { - return read_unsigned(pic, port, c); + return read_unsigned(pic, file, c); } static pic_value @@ -312,15 +312,15 @@ negate(pic_state *pic, pic_value n) } static pic_value -read_minus(pic_state *pic, struct pic_port *port, int c) +read_minus(pic_state *pic, xFILE *file, int c) { pic_value sym; - if (isdigit(peek(pic, port))) { - return negate(pic, read_unsigned(pic, port, next(pic, port))); + if (isdigit(peek(pic, file))) { + return negate(pic, read_unsigned(pic, file, next(pic, file))); } else { - sym = read_symbol(pic, port, c); + sym = read_symbol(pic, file, c); if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-inf.0")) { return pic_float_value(pic, -(1.0 / 0.0)); } @@ -332,15 +332,15 @@ read_minus(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_plus(pic_state *pic, struct pic_port *port, int c) +read_plus(pic_state *pic, xFILE *file, int c) { pic_value sym; - if (isdigit(peek(pic, port))) { - return read_unsigned(pic, port, next(pic, port)); + if (isdigit(peek(pic, file))) { + return read_unsigned(pic, file, next(pic, file)); } else { - sym = read_symbol(pic, port, c); + sym = read_symbol(pic, file, c); if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+inf.0")) { return pic_float_value(pic, 1.0 / 0.0); } @@ -352,10 +352,10 @@ read_plus(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_true(pic_state *pic, struct pic_port *port, int c) +read_true(pic_state *pic, xFILE *file, int c) { - if ((c = peek(pic, port)) == 'r') { - if (! expect(pic, port, "rue")) { + if ((c = peek(pic, file)) == 'r') { + if (! expect(pic, file, "rue")) { read_error(pic, "unexpected character while reading #true", pic_nil_value(pic)); } } else if (! isdelim(c)) { @@ -366,10 +366,10 @@ read_true(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_false(pic_state *pic, struct pic_port *port, int c) +read_false(pic_state *pic, xFILE *file, int c) { - if ((c = peek(pic, port)) == 'a') { - if (! expect(pic, port, "alse")) { + if ((c = peek(pic, file)) == 'a') { + if (! expect(pic, file, "alse")) { read_error(pic, "unexpected character while reading #false", pic_nil_value(pic)); } } else if (! isdelim(c)) { @@ -380,31 +380,31 @@ read_false(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_char(pic_state *pic, struct pic_port *port, int c) +read_char(pic_state *pic, xFILE *file, int c) { - c = next(pic, port); + c = next(pic, file); - if (! isdelim(peek(pic, port))) { + if (! isdelim(peek(pic, file))) { switch (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 'b': c = '\b'; if (! expect(pic, port, "ackspace")) goto fail; break; - case 'd': c = 0x7F; if (! expect(pic, port, "elete")) goto fail; break; - case 'e': c = 0x1B; if (! expect(pic, port, "scape")) goto fail; break; + case 'a': c = '\a'; if (! expect(pic, file, "larm")) goto fail; break; + case 'b': c = '\b'; if (! expect(pic, file, "ackspace")) goto fail; break; + case 'd': c = 0x7F; if (! expect(pic, file, "elete")) goto fail; break; + case 'e': c = 0x1B; if (! expect(pic, file, "scape")) goto fail; break; case 'n': - if ((c = peek(pic, port)) == 'e') { + if ((c = peek(pic, file)) == 'e') { c = '\n'; - if (! expect(pic, port, "ewline")) + if (! expect(pic, file, "ewline")) goto fail; } else { c = '\0'; - if (! expect(pic, port, "ull")) + if (! expect(pic, file, "ull")) goto fail; } break; - case 'r': c = '\r'; if (! expect(pic, port, "eturn")) goto fail; break; - case 's': c = ' '; if (! expect(pic, port, "pace")) goto fail; break; - case 't': c = '\t'; if (! expect(pic, port, "ab")) goto fail; break; + case 'r': c = '\r'; if (! expect(pic, file, "eturn")) goto fail; break; + case 's': c = ' '; if (! expect(pic, file, "pace")) goto fail; break; + case 't': c = '\t'; if (! expect(pic, file, "ab")) goto fail; break; } } @@ -415,7 +415,7 @@ read_char(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_string(pic_state *pic, struct pic_port *port, int c) +read_string(pic_state *pic, xFILE *file, int c) { char *buf; int size, cnt; @@ -427,9 +427,9 @@ read_string(pic_state *pic, struct pic_port *port, int c) /* TODO: intraline whitespaces */ - while ((c = next(pic, port)) != '"') { + while ((c = next(pic, file)) != '"') { if (c == '\\') { - switch (c = next(pic, port)) { + switch (c = next(pic, file)) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 't': c = '\t'; break; @@ -450,7 +450,7 @@ read_string(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_pipe(pic_state *pic, struct pic_port *port, int c) +read_pipe(pic_state *pic, xFILE *file, int c) { char *buf; int size, cnt; @@ -462,9 +462,9 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) size = 256; buf = pic_malloc(pic, size); cnt = 0; - while ((c = next(pic, port)) != '|') { + while ((c = next(pic, file)) != '|') { if (c == '\\') { - switch ((c = next(pic, port))) { + switch ((c = next(pic, file))) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 't': c = '\t'; break; @@ -472,7 +472,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) case 'r': c = '\r'; break; case 'x': i = 0; - while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') { + while ((HEX_BUF[i++] = (char)next(pic, file)) != ';') { if (i >= sizeof HEX_BUF) read_error(pic, "expected ';'", pic_list(pic, 1, pic_char_value(pic, HEX_BUF[sizeof(HEX_BUF) - 1]))); } @@ -494,7 +494,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_blob(pic_state *pic, struct pic_port *port, int c) +read_blob(pic_state *pic, xFILE *file, int c) { int nbits, n; int len; @@ -503,7 +503,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) nbits = 0; - while (isdigit(c = next(pic, port))) { + while (isdigit(c = next(pic, file))) { nbits = 10 * nbits + c - '0'; } @@ -517,16 +517,16 @@ read_blob(pic_state *pic, struct pic_port *port, int c) len = 0; dat = NULL; - c = next(pic, port); - while ((c = skip(pic, port, c)) != ')') { - n = read_uinteger(pic, port, c); + c = next(pic, file); + while ((c = skip(pic, file, c)) != ')') { + n = read_uinteger(pic, file, c); if (n < 0 || (1 << nbits) <= n) { read_error(pic, "invalid element in bytevector literal", pic_list(pic, 1, pic_int_value(pic, n))); } len += 1; dat = pic_realloc(pic, dat, len); dat[len - 1] = (unsigned char)n; - c = next(pic, port); + c = next(pic, file); } blob = pic_blob_value(pic, dat, len); @@ -536,10 +536,10 @@ read_blob(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_undef_or_blob(pic_state *pic, struct pic_port *port, int c) +read_undef_or_blob(pic_state *pic, xFILE *file, int c) { - if ((c = peek(pic, port)) == 'n') { - if (! expect(pic, port, "ndefined")) { + if ((c = peek(pic, file)) == 'n') { + if (! expect(pic, file, "ndefined")) { read_error(pic, "unexpected character while reading #undefined", pic_nil_value(pic)); } return pic_undef_value(pic); @@ -547,28 +547,28 @@ read_undef_or_blob(pic_state *pic, struct pic_port *port, int c) if (! isdigit(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, file, 'u'); } static pic_value -read_pair(pic_state *pic, struct pic_port *port, int c) +read_pair(pic_state *pic, xFILE *file, int c) { static const int tCLOSE = ')'; pic_value car, cdr; retry: - c = skip(pic, port, ' '); + c = skip(pic, file, ' '); if (c == tCLOSE) { return pic_nil_value(pic); } - if (c == '.' && isdelim(peek(pic, port))) { - cdr = read(pic, port, next(pic, port)); + if (c == '.' && isdelim(peek(pic, file))) { + cdr = read(pic, file, next(pic, file)); closing: - if ((c = skip(pic, port, ' ')) != tCLOSE) { - if (pic_invalid_p(pic, read_nullable(pic, port, c))) { + if ((c = skip(pic, file, ' ')) != tCLOSE) { + if (pic_invalid_p(pic, read_nullable(pic, file, c))) { goto closing; } read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); @@ -576,25 +576,25 @@ read_pair(pic_state *pic, struct pic_port *port, int c) return cdr; } else { - car = read_nullable(pic, port, c); + car = read_nullable(pic, file, c); if (pic_invalid_p(pic, car)) { goto retry; } - cdr = read_pair(pic, port, '('); + cdr = read_pair(pic, file, '('); return pic_cons(pic, car, cdr); } } static pic_value -read_vector(pic_state *pic, struct pic_port *port, int c) +read_vector(pic_state *pic, xFILE *file, int c) { pic_value list, it, elem; pic_vec *vec; int i = 0; - list = read(pic, port, c); + list = read(pic, file, c); vec = pic_make_vec(pic, pic_length(pic, list)); @@ -606,7 +606,7 @@ read_vector(pic_state *pic, struct pic_port *port, int c) } static pic_value -read_label_set(pic_state *pic, struct pic_port *port, int i) +read_label_set(pic_state *pic, xFILE *file, int i) { khash_t(read) *h = &pic->reader.labels; pic_value val; @@ -615,14 +615,14 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) it = kh_put(read, h, i, &ret); - switch ((c = skip(pic, port, ' '))) { + switch ((c = skip(pic, file, ' '))) { case '(': { pic_value tmp; kh_val(h, it) = val = pic_cons(pic, pic_undef_value(pic), pic_undef_value(pic)); - tmp = read(pic, port, c); + tmp = read(pic, file, c); pic_pair_ptr(val)->car = pic_car(pic, tmp); pic_pair_ptr(val)->cdr = pic_cdr(pic, tmp); @@ -632,7 +632,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) { bool vect; - if (peek(pic, port) == '(') { + if (peek(pic, file) == '(') { vect = true; } else { vect = false; @@ -643,7 +643,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) kh_val(h, it) = val = pic_obj_value(pic_make_vec(pic, 0)); - tmp = pic_vec_ptr(read(pic, port, c)); + tmp = pic_vec_ptr(read(pic, file, c)); PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); PIC_SWAP(int, tmp->len, pic_vec_ptr(val)->len); @@ -654,7 +654,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) } default: { - kh_val(h, it) = val = read(pic, port, c); + kh_val(h, it) = val = read(pic, file, c); return val; } @@ -662,7 +662,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) } static pic_value -read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i) +read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i) { khash_t(read) *h = &pic->reader.labels; khiter_t it; @@ -675,34 +675,34 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i) } static pic_value -read_label(pic_state *pic, struct pic_port *port, int c) +read_label(pic_state *pic, xFILE *file, int c) { int i; i = 0; do { i = i * 10 + c - '0'; - } while (isdigit(c = next(pic, port))); + } while (isdigit(c = next(pic, file))); if (c == '=') { - return read_label_set(pic, port, i); + return read_label_set(pic, file, i); } if (c == '#') { - return read_label_ref(pic, port, i); + return read_label_ref(pic, file, i); } read_error(pic, "broken label expression", pic_nil_value(pic)); } static pic_value -read_unmatch(pic_state *pic, struct pic_port PIC_UNUSED(*port), int PIC_UNUSED(c)) +read_unmatch(pic_state *pic, xFILE PIC_UNUSED(*file), int PIC_UNUSED(c)) { read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); } static pic_value -read_dispatch(pic_state *pic, struct pic_port *port, int c) +read_dispatch(pic_state *pic, xFILE *file, int c) { - c = next(pic, port); + c = next(pic, file); if (c == EOF) { read_error(pic, "unexpected EOF", pic_nil_value(pic)); @@ -712,13 +712,13 @@ read_dispatch(pic_state *pic, struct pic_port *port, int 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, file, c); } static pic_value -read_nullable(pic_state *pic, struct pic_port *port, int c) +read_nullable(pic_state *pic, xFILE *file, int c) { - c = skip(pic, port, c); + c = skip(pic, file, c); if (c == EOF) { read_error(pic, "unexpected EOF", pic_nil_value(pic)); @@ -728,19 +728,19 @@ read_nullable(pic_state *pic, struct pic_port *port, int 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, file, c); } static pic_value -read(pic_state *pic, struct pic_port *port, int c) +read(pic_state *pic, xFILE *file, int c) { pic_value val; retry: - val = read_nullable(pic, port, c); + val = read_nullable(pic, file, c); if (pic_invalid_p(pic, val)) { - c = next(pic, port); + c = next(pic, file); goto retry; } @@ -824,10 +824,11 @@ pic_read(pic_state *pic, struct pic_port *port) { size_t ai = pic_enter(pic); pic_value val; + xFILE *file = port->file; int c; - while ((c = skip(pic, port, next(pic, port))) != EOF) { - val = read_nullable(pic, port, c); + while ((c = skip(pic, file, next(pic, file))) != EOF) { + val = read_nullable(pic, file, c); if (! pic_invalid_p(pic, val)) { break; From d965a3da5a4b77051baf6c33383d851b4de85d6d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 16:58:39 +0900 Subject: [PATCH 048/119] remove macro.h --- extlib/benz/include/picrin.h | 11 ++++++--- extlib/benz/include/picrin/macro.h | 37 ----------------------------- extlib/benz/include/picrin/object.h | 17 +++++++++++++ extlib/benz/lib.c | 5 +++- extlib/benz/macro.c | 12 ---------- 5 files changed, 29 insertions(+), 53 deletions(-) delete mode 100644 extlib/benz/include/picrin/macro.h diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index f0a05e86..08637904 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -274,7 +274,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/state.h" #include "picrin/cont.h" -#include "picrin/macro.h" void *pic_default_allocf(void *, void *, size_t); @@ -294,14 +293,20 @@ void pic_close_port(pic_state *, struct pic_port *port); pic_leave(pic, ai); \ } while (0) +pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); +pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); +pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); + pic_value pic_read(pic_state *, struct pic_port *); pic_value pic_read_cstr(pic_state *, const char *); -void pic_load(pic_state *, struct pic_port *); -void pic_load_cstr(pic_state *, const char *); +pic_value pic_expand(pic_state *, pic_value, struct pic_env *); pic_value pic_eval(pic_state *, pic_value, const char *); +void pic_load(pic_state *, struct pic_port *); +void pic_load_cstr(pic_state *, const char *); + struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h deleted file mode 100644 index 3c2703b8..00000000 --- a/extlib/benz/include/picrin/macro.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_MACRO_H -#define PICRIN_MACRO_H - -#if defined(__cplusplus) -extern "C" { -#endif - -KHASH_DECLARE(env, pic_id *, pic_sym *) - -struct pic_env { - PIC_OBJECT_HEADER - khash_t(env) map; - struct pic_env *up; - struct pic_string *lib; -}; - -#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) -#define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) - -struct pic_env *pic_make_topenv(pic_state *, struct pic_string *); -struct pic_env *pic_make_env(pic_state *, struct pic_env *); - -pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); -pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); -pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); - -pic_value pic_expand(pic_state *, pic_value, struct pic_env *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index c4762df4..0a3310bd 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -203,6 +203,23 @@ struct pic_port { #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) +/* environment */ + +KHASH_DECLARE(env, pic_id *, pic_sym *) + +struct pic_env { + PIC_OBJECT_HEADER + khash_t(env) map; + struct pic_env *up; + struct pic_string *lib; +}; + +#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) +#define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) + +struct pic_env *pic_make_env(pic_state *, struct pic_env *); + + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index c5b4dd28..bf7a7246 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -36,7 +36,10 @@ make_library_env(pic_state *pic, struct pic_string *name) { struct pic_env *env; - env = pic_make_topenv(pic, name); + env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); + env->up = NULL; + env->lib = name; + kh_init(env, &env->map); /* set up default environment */ pic_put_identifier(pic, (pic_id *)pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, env); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index f8bae3c2..34266ed1 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -21,18 +21,6 @@ pic_make_env(pic_state *pic, struct pic_env *up) return env; } -struct pic_env * -pic_make_topenv(pic_state *pic, struct pic_string *lib) -{ - struct pic_env *env; - - env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); - env->up = NULL; - env->lib = lib; - kh_init(env, &env->map); - return env; -} - pic_sym * pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { From b070d9c1dc3c2597e57cef88331d5d0dc8ea4512 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 17:38:49 +0900 Subject: [PATCH 049/119] add pic_return --- contrib/10.callcc/callcc.c | 45 ++++----- contrib/10.math/math.c | 8 +- contrib/30.regexp/src/regexp.c | 2 +- extlib/benz/cont.c | 156 +++++++++++------------------- extlib/benz/eval.c | 8 +- extlib/benz/include/picrin.h | 8 +- extlib/benz/include/picrin/cont.h | 15 +-- extlib/benz/read.c | 4 +- extlib/benz/vector.c | 41 ++++---- 9 files changed, 109 insertions(+), 178 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 00c35a8c..8f6a2c15 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/object.h" struct pic_fullcont { jmp_buf jmp; @@ -29,7 +30,7 @@ struct pic_fullcont { struct pic_object **arena; size_t arena_size, arena_idx; - pic_value results; + pic_vec *results; }; static void @@ -91,7 +92,7 @@ cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value)) mark(pic, cont->ptable); /* result values */ - mark(pic, cont->results); + mark(pic, pic_obj_value(cont->results)); } static const pic_data_type cont_type = { "continuation", cont_dtor, cont_mark }; @@ -158,7 +159,7 @@ save_cont(pic_state *pic, struct pic_fullcont **c) cont->arena = pic_malloc(pic, sizeof(struct pic_object *) * pic->arena_size); memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * pic->arena_size); - cont->results = pic_undef_value(pic); + cont->results = pic_make_vec(pic, 0, NULL); } static void @@ -225,7 +226,7 @@ cont_call(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); cont = pic_data(pic, pic_closure_ref(pic, 0)); - cont->results = pic_make_list(pic, argc, argv); + cont->results = pic_make_vec(pic, argc, argv); /* execute guard handlers */ pic_wind(pic, pic->cp, cont->cp); @@ -233,36 +234,14 @@ cont_call(pic_state *pic) restore_cont(pic, cont); } -pic_value -pic_callcc_full(pic_state *pic, struct pic_proc *proc) -{ - struct pic_fullcont *cont; - - save_cont(pic, &cont); - if (setjmp(cont->jmp)) { - return pic_values_by_list(pic, cont->results); - } - else { - struct pic_proc *c; - - /* save the continuation object in proc */ - c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_value(pic, cont, &cont_type))); - - return pic_call(pic, proc, 1, pic_obj_value(c)); - } -} - static pic_value -pic_callcc_callcc(pic_state *pic) +pic_callcc(pic_state *pic, struct pic_proc *proc) { - struct pic_proc *proc; struct pic_fullcont *cont; - pic_get_args(pic, "l", &proc); - save_cont(pic, &cont); if (setjmp(cont->jmp)) { - return pic_values_by_list(pic, cont->results); + return pic_valuesk(pic, cont->results->len, cont->results->data); } else { struct pic_proc *c; @@ -276,6 +255,16 @@ pic_callcc_callcc(pic_state *pic) } } +static pic_value +pic_callcc_callcc(pic_state *pic) +{ + struct pic_proc *proc; + + pic_get_args(pic, "l", &proc); + + return pic_callcc(pic, proc); +} + #define pic_redefun(pic, lib, name, func) \ pic_set(pic, lib, name, pic_obj_value(pic_lambda(pic, func, 0))) diff --git a/contrib/10.math/math.c b/contrib/10.math/math.c index 0fc2ff92..cbaeec7a 100644 --- a/contrib/10.math/math.c +++ b/contrib/10.math/math.c @@ -17,13 +17,13 @@ pic_number_floor2(pic_state *pic) ? i / j : (i / j) - 1; - return pic_values2(pic, pic_int_value(pic, k), pic_int_value(pic, i - k * j)); + return pic_return(pic, 2, pic_int_value(pic, k), pic_int_value(pic, i - k * j)); } else { double q, r; q = floor((double)i/j); r = i - j * q; - return pic_values2(pic, pic_float_value(pic, q), pic_float_value(pic, r)); + return pic_return(pic, 2, pic_float_value(pic, q), pic_float_value(pic, r)); } } @@ -36,14 +36,14 @@ pic_number_trunc2(pic_state *pic) pic_get_args(pic, "II", &i, &e1, &j, &e2); if (e1 && e2) { - return pic_values2(pic, pic_int_value(pic, i/j), pic_int_value(pic, i - (i/j) * j)); + return pic_return(pic, 2, pic_int_value(pic, i/j), pic_int_value(pic, i - (i/j) * j)); } else { double q, r; q = trunc((double)i/j); r = i - j * q; - return pic_values2(pic, pic_float_value(pic, q), pic_float_value(pic, r)); + return pic_return(pic, 2, pic_float_value(pic, q), pic_float_value(pic, r)); } } diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index fd8bca76..dd611975 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -125,7 +125,7 @@ pic_regexp_regexp_match(pic_state *pic) matches = pic_reverse(pic, matches); positions = pic_reverse(pic, positions); } - return pic_values2(pic, matches, positions); + return pic_return(pic, 2, matches, positions); } static pic_value diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 7bce50f8..9eeb20bf 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -5,6 +5,40 @@ #include "picrin.h" #include "picrin/object.h" +void +pic_save_point(pic_state *pic, struct pic_cont *cont) +{ + /* save runtime context */ + cont->cp = pic->cp; + cont->sp_offset = pic->sp - pic->stbase; + cont->ci_offset = pic->ci - pic->cibase; + cont->xp_offset = pic->xp - pic->xpbase; + cont->arena_idx = pic->arena_idx; + cont->ip = pic->ip; + cont->ptable = pic->ptable; + cont->prev = pic->cc; + cont->results = pic_make_vec(pic, 0, NULL); + cont->id = pic->ccnt++; + + pic->cc = cont; +} + +void +pic_load_point(pic_state *pic, struct pic_cont *cont) +{ + pic_wind(pic, pic->cp, cont->cp); + + /* load runtime context */ + pic->cp = cont->cp; + pic->sp = pic->stbase + cont->sp_offset; + pic->ci = pic->cibase + cont->ci_offset; + pic->xp = pic->xpbase + cont->xp_offset; + pic->arena_idx = cont->arena_idx; + pic->ip = cont->ip; + pic->ptable = cont->ptable; + pic->cc = cont->prev; +} + void pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) { @@ -21,7 +55,7 @@ pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) } } -pic_value +static pic_value pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, struct pic_proc *out) { pic_checkpoint *here; @@ -49,40 +83,6 @@ pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, st return val; } -void -pic_save_point(pic_state *pic, struct pic_cont *cont) -{ - /* save runtime context */ - cont->cp = pic->cp; - cont->sp_offset = pic->sp - pic->stbase; - cont->ci_offset = pic->ci - pic->cibase; - cont->xp_offset = pic->xp - pic->xpbase; - cont->arena_idx = pic->arena_idx; - cont->ip = pic->ip; - cont->ptable = pic->ptable; - cont->prev = pic->cc; - cont->results = pic_undef_value(pic); - cont->id = pic->ccnt++; - - pic->cc = cont; -} - -void -pic_load_point(pic_state *pic, struct pic_cont *cont) -{ - pic_wind(pic, pic->cp, cont->cp); - - /* load runtime context */ - pic->cp = cont->cp; - pic->sp = pic->stbase + cont->sp_offset; - pic->ci = pic->cibase + cont->ci_offset; - pic->xp = pic->xpbase + cont->xp_offset; - pic->arena_idx = cont->arena_idx; - pic->ip = cont->ip; - pic->ptable = cont->ptable; - pic->cc = cont->prev; -} - #define CV_ID 0 #define CV_ESCAPE 1 @@ -109,7 +109,7 @@ cont_call(pic_state *pic) } cont = pic_data_ptr(pic_closure_ref(pic, CV_ESCAPE))->data; - cont->results = pic_make_list(pic, argc, argv); + cont->results = pic_make_vec(pic, argc, argv); pic_load_point(pic, cont); @@ -130,7 +130,7 @@ pic_make_cont(pic_state *pic, struct pic_cont *cont) return c; } -pic_value +static pic_value pic_callcc(pic_state *pic, struct pic_proc *proc) { struct pic_cont cont; @@ -138,7 +138,7 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) pic_save_point(pic, &cont); if (PIC_SETJMP(pic, cont.jmp)) { - return pic_values_by_list(pic, cont.results); + return pic_valuesk(pic, cont.results->len, cont.results->data); } else { pic_value val; @@ -151,88 +151,43 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) } } -static pic_value -pic_va_values(pic_state *pic, int n, ...) +pic_value +pic_return(pic_state *pic, int n, ...) { - pic_vec *args = pic_make_vec(pic, n); va_list ap; - int i = 0; + pic_value ret; va_start(ap, n); - - while (i < n) { - args->data[i++] = va_arg(ap, pic_value); - } - + ret = pic_vreturn(pic, n, ap); va_end(ap); - - return pic_values(pic, n, args->data); + return ret; } pic_value -pic_values0(pic_state *pic) +pic_vreturn(pic_state *pic, int n, va_list ap) { - return pic_va_values(pic, 0); + pic_value *retv = pic_alloca(pic, sizeof(pic_value) * n); + int i; + + for (i = 0; i < n; ++i) { + retv[i] = va_arg(ap, pic_value); + } + return pic_valuesk(pic, n, retv); } pic_value -pic_values1(pic_state *pic, pic_value arg1) -{ - return pic_va_values(pic, 1, arg1); -} - -pic_value -pic_values2(pic_state *pic, pic_value arg1, pic_value arg2) -{ - return pic_va_values(pic, 2, arg1, arg2); -} - -pic_value -pic_values3(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3) -{ - return pic_va_values(pic, 3, arg1, arg2, arg3); -} - -pic_value -pic_values4(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3, pic_value arg4) -{ - return pic_va_values(pic, 4, arg1, arg2, arg3, arg4); -} - -pic_value -pic_values5(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3, pic_value arg4, pic_value arg5) -{ - return pic_va_values(pic, 5, arg1, arg2, arg3, arg4, arg5); -} - -pic_value -pic_values(pic_state *pic, int argc, pic_value *argv) +pic_valuesk(pic_state *pic, int argc, pic_value *argv) { int i; for (i = 0; i < argc; ++i) { pic->sp[i] = argv[i]; } - pic->ci->retc = (int)argc; + pic->ci->retc = argc; return argc == 0 ? pic_undef_value(pic) : pic->sp[0]; } -pic_value -pic_values_by_list(pic_state *pic, pic_value list) -{ - pic_value v, it; - int i; - - i = 0; - pic_for_each (v, list, it) { - pic->sp[i++] = v; - } - pic->ci->retc = i; - - return pic_nil_p(pic, list) ? pic_undef_value(pic) : pic->sp[0]; -} - int pic_receive(pic_state *pic, int n, pic_value *argv) { @@ -246,7 +201,6 @@ pic_receive(pic_state *pic, int n, pic_value *argv) for (i = 0; i < retc && i < n; ++i) { argv[i] = ci->fp[i]; } - return retc; } @@ -278,7 +232,7 @@ pic_cont_values(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - return pic_values(pic, argc, argv); + return pic_valuesk(pic, argc, argv); } static pic_value @@ -293,7 +247,7 @@ pic_cont_call_with_values(pic_state *pic) pic_call(pic, producer, 0); argc = pic_receive(pic, 0, NULL); - args = pic_make_vec(pic, argc); + args = pic_make_vec(pic, argc, NULL); pic_receive(pic, argc, args->data); diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 2d112e74..264c3272 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -230,7 +230,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) body = analyze(pic, scope, body); analyze_deferred(pic, scope); - args = pic_make_vec(pic, kh_size(&scope->args)); + args = pic_make_vec(pic, kh_size(&scope->args), NULL); for (i = 0; pic_pair_p(pic, formals); formals = pic_cdr(pic, formals), i++) { args->data[i] = pic_car(pic, formals); } @@ -239,7 +239,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) rest = pic_obj_value(scope->rest); } - locals = pic_make_vec(pic, kh_size(&scope->locals)); + locals = pic_make_vec(pic, kh_size(&scope->locals), NULL); j = 0; if (scope->rest != NULL) { locals->data[j++] = pic_obj_value(scope->rest); @@ -252,7 +252,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) } } - captures = pic_make_vec(pic, kh_size(&scope->captures)); + captures = pic_make_vec(pic, kh_size(&scope->captures), NULL); for (it = kh_begin(&scope->captures), j = 0; it < kh_end(&scope->captures); ++it) { if (kh_exist(&scope->captures, it)) { captures->data[j++] = pic_obj_value(kh_key(&scope->captures, it)); @@ -818,7 +818,7 @@ codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) static struct pic_irep * pic_codegen(pic_state *pic, pic_value obj) { - pic_vec *empty = pic_make_vec(pic, 0); + pic_vec *empty = pic_make_vec(pic, 0, NULL); codegen_context c, *cxt = &c; codegen_context_init(pic, cxt, NULL, NULL, empty, empty, empty); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 08637904..907bd3dc 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -100,6 +100,11 @@ pic_value pic_closure_ref(pic_state *, int i); void pic_closure_set(pic_state *, int i, pic_value v); pic_value pic_funcall(pic_state *, const char *lib, const char *name, int n, ...); +pic_value pic_return(pic_state *, int n, ...); +pic_value pic_vreturn(pic_state *, int n, va_list); +pic_value pic_valuesk(pic_state *, int n, pic_value *retv); +int pic_receive(pic_state *, int n, pic_value *retv); + void pic_make_library(pic_state *, const char *lib); void pic_in_library(pic_state *, const char *lib); bool pic_find_library(pic_state *, const char *lib); @@ -230,7 +235,7 @@ pic_value pic_reverse(pic_state *, pic_value list); pic_value pic_append(pic_state *, pic_value xs, pic_value ys); /* vector */ -pic_vec *pic_make_vec(pic_state *, int); +pic_vec *pic_make_vec(pic_state *, int, pic_value *); pic_value pic_vec_ref(pic_state *, pic_vec *, int); void pic_vec_set(pic_state *, pic_vec *, int, pic_value); int pic_vec_len(pic_state *, pic_vec *); @@ -272,7 +277,6 @@ int pic_str_hash(pic_state *, struct pic_string *); #include "picrin/type.h" #include "picrin/state.h" - #include "picrin/cont.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/cont.h b/extlib/benz/include/picrin/cont.h index b224597d..6b6edcf0 100644 --- a/extlib/benz/include/picrin/cont.h +++ b/extlib/benz/include/picrin/cont.h @@ -22,7 +22,7 @@ struct pic_cont { pic_value ptable; pic_code *ip; - pic_value results; + pic_vec *results; struct pic_cont *prev; }; @@ -33,19 +33,6 @@ void pic_load_point(pic_state *, struct pic_cont *); struct pic_proc *pic_make_cont(pic_state *, struct pic_cont *); void pic_wind(pic_state *, pic_checkpoint *, pic_checkpoint *); -pic_value pic_dynamic_wind(pic_state *, struct pic_proc *, struct pic_proc *, struct pic_proc *); - -pic_value pic_values0(pic_state *); -pic_value pic_values1(pic_state *, pic_value); -pic_value pic_values2(pic_state *, pic_value, pic_value); -pic_value pic_values3(pic_state *, pic_value, pic_value, pic_value); -pic_value pic_values4(pic_state *, pic_value, pic_value, pic_value, pic_value); -pic_value pic_values5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value); -pic_value pic_values(pic_state *, int, pic_value *); -pic_value pic_values_by_list(pic_state *, pic_value); -int pic_receive(pic_state *, int, pic_value *); - -pic_value pic_callcc(pic_state *, struct pic_proc *); #if defined(__cplusplus) } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 2441f677..88e636b2 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -596,7 +596,7 @@ read_vector(pic_state *pic, xFILE *file, int c) list = read(pic, file, c); - vec = pic_make_vec(pic, pic_length(pic, list)); + vec = pic_make_vec(pic, pic_length(pic, list), NULL); pic_for_each (elem, list, it) { vec->data[i++] = elem; @@ -641,7 +641,7 @@ read_label_set(pic_state *pic, xFILE *file, int i) if (vect) { pic_vec *tmp; - kh_val(h, it) = val = pic_obj_value(pic_make_vec(pic, 0)); + kh_val(h, it) = val = pic_obj_value(pic_make_vec(pic, 0, NULL)); tmp = pic_vec_ptr(read(pic, file, c)); PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 965d46e3..339a346b 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -6,7 +6,7 @@ #include "picrin/object.h" struct pic_vector * -pic_make_vec(pic_state *pic, int len) +pic_make_vec(pic_state *pic, int len, pic_value *argv) { struct pic_vector *vec; int i; @@ -14,8 +14,12 @@ pic_make_vec(pic_state *pic, int len) vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TYPE_VECTOR); vec->len = len; vec->data = (pic_value *)pic_malloc(pic, sizeof(pic_value) * len); - for (i = 0; i < len; ++i) { - vec->data[i] = pic_undef_value(pic); + if (argv == NULL) { + for (i = 0; i < len; ++i) { + vec->data[i] = pic_undef_value(pic); + } + } else { + memcpy(vec->data, argv, sizeof(pic_value) * len); } return vec; } @@ -33,17 +37,13 @@ pic_vec_vector_p(pic_state *pic) static pic_value pic_vec_vector(pic_state *pic) { - int argc, i; + int argc; pic_value *argv; pic_vec *vec; pic_get_args(pic, "*", &argc, &argv); - vec = pic_make_vec(pic, argc); - - for (i = 0; i < argc; ++i) { - vec->data[i] = argv[i]; - } + vec = pic_make_vec(pic, argc, argv); return pic_obj_value(vec); } @@ -57,7 +57,7 @@ pic_vec_make_vector(pic_state *pic) n = pic_get_args(pic, "i|o", &k, &v); - vec = pic_make_vec(pic, k); + vec = pic_make_vec(pic, k, NULL); if (n == 2) { for (i = 0; i < k; ++i) { vec->data[i] = v; @@ -140,26 +140,23 @@ pic_vec_vector_copy_i(pic_state *pic) static pic_value pic_vec_vector_copy(pic_state *pic) { - pic_vec *vec, *to; - int n, start, end, i = 0; + pic_vec *from, *to; + int n, start, end; - n = pic_get_args(pic, "v|ii", &vec, &start, &end); + n = pic_get_args(pic, "v|ii", &from, &start, &end); switch (n) { case 1: start = 0; case 2: - end = vec->len; + end = from->len; } if (end < start) { pic_errorf(pic, "vector-copy: end index must not be less than start index"); } - to = pic_make_vec(pic, end - start); - while (start < end) { - to->data[i++] = vec->data[start++]; - } + to = pic_make_vec(pic, end - start, from->data + start); return pic_obj_value(to); } @@ -179,7 +176,7 @@ pic_vec_vector_append(pic_state *pic) len += pic_vec_ptr(argv[i])->len; } - vec = pic_make_vec(pic, len); + vec = pic_make_vec(pic, len, NULL); len = 0; for (i = 0; i < argc; ++i) { @@ -234,7 +231,7 @@ pic_vec_vector_map(pic_state *pic) : pic_vec_ptr(argv[i])->len; } - vec = pic_make_vec(pic, len); + vec = pic_make_vec(pic, len, NULL); for (i = 0; i < len; ++i) { vals = pic_nil_value(pic); @@ -284,7 +281,7 @@ pic_vec_list_to_vector(pic_state *pic) pic_get_args(pic, "o", &list); - vec = pic_make_vec(pic, pic_length(pic, list)); + vec = pic_make_vec(pic, pic_length(pic, list), NULL); data = vec->data; @@ -373,7 +370,7 @@ pic_vec_string_to_vector(pic_state *pic) pic_errorf(pic, "string->vector: end index must not be less than start index"); } - vec = pic_make_vec(pic, end - start); + vec = pic_make_vec(pic, end - start, NULL); for (i = 0; i < end - start; ++i) { vec->data[i] = pic_char_value(pic, pic_str_ref(pic, str, i + start)); From 03fed95b320068f0df5bab43097f9b6f5c12da98 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 17:58:42 +0900 Subject: [PATCH 050/119] don't allocate on continuation call --- extlib/benz/cont.c | 20 +++++++++++--------- extlib/benz/include/picrin.h | 4 ---- extlib/benz/include/picrin/cont.h | 3 ++- extlib/benz/include/picrin/object.h | 4 ++++ 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 9eeb20bf..f47347b6 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -17,7 +17,8 @@ pic_save_point(pic_state *pic, struct pic_cont *cont) cont->ip = pic->ip; cont->ptable = pic->ptable; cont->prev = pic->cc; - cont->results = pic_make_vec(pic, 0, NULL); + cont->retc = 0; + cont->retv = NULL; cont->id = pic->ccnt++; pic->cc = cont; @@ -109,7 +110,8 @@ cont_call(pic_state *pic) } cont = pic_data_ptr(pic_closure_ref(pic, CV_ESCAPE))->data; - cont->results = pic_make_vec(pic, argc, argv); + cont->retc = argc; + cont->retv = argv; pic_load_point(pic, cont); @@ -138,7 +140,7 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) pic_save_point(pic, &cont); if (PIC_SETJMP(pic, cont.jmp)) { - return pic_valuesk(pic, cont.results->len, cont.results->data); + return pic_valuesk(pic, cont.retc, cont.retv); } else { pic_value val; @@ -239,19 +241,19 @@ static pic_value pic_cont_call_with_values(pic_state *pic) { struct pic_proc *producer, *consumer; - int argc; - pic_vec *args; + int retc; + pic_value *retv; pic_get_args(pic, "ll", &producer, &consumer); pic_call(pic, producer, 0); - argc = pic_receive(pic, 0, NULL); - args = pic_make_vec(pic, argc, NULL); + retc = pic_receive(pic, 0, NULL); + retv = pic_alloca(pic, sizeof(pic_value) * retc); - pic_receive(pic, argc, args->data); + pic_receive(pic, retc, retv); - return pic_applyk(pic, consumer, argc, args->data); + return pic_applyk(pic, consumer, retc, retv); } void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 907bd3dc..94600879 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -297,10 +297,6 @@ void pic_close_port(pic_state *, struct pic_port *port); pic_leave(pic, ai); \ } while (0) -pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); -pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); -pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); - pic_value pic_read(pic_state *, struct pic_port *); pic_value pic_read_cstr(pic_state *, const char *); diff --git a/extlib/benz/include/picrin/cont.h b/extlib/benz/include/picrin/cont.h index 6b6edcf0..4e6649bf 100644 --- a/extlib/benz/include/picrin/cont.h +++ b/extlib/benz/include/picrin/cont.h @@ -22,7 +22,8 @@ struct pic_cont { pic_value ptable; pic_code *ip; - pic_vec *results; + int retc; + pic_value *retv; struct pic_cont *prev; }; diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 0a3310bd..2335cd59 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -36,6 +36,10 @@ struct pic_id { pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); +pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); +pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); +pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); + struct pic_string *pic_id_name(pic_state *, pic_id *); From 3e4a4755dc1e0beb5ef9743f937760a9829ed162 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 19:08:45 +0900 Subject: [PATCH 051/119] struct pic_dict * -> pic_value --- extlib/benz/dict.c | 94 +++++++--------- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin.h | 14 +-- extlib/benz/include/picrin/object.h | 162 +++++++++------------------- extlib/benz/lib.c | 16 +-- extlib/benz/proc.c | 7 +- extlib/benz/write.c | 6 +- 7 files changed, 112 insertions(+), 189 deletions(-) diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 656547d1..88c67e4c 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -7,21 +7,20 @@ KHASH_DEFINE(dict, pic_sym *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) -struct pic_dict * +pic_value pic_make_dict(pic_state *pic) { struct pic_dict *dict; dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TYPE_DICT); kh_init(dict, &dict->hash); - - return dict; + return pic_obj_value(dict); } pic_value -pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym *key) +pic_dict_ref(pic_state *pic, pic_value dict, pic_sym *key) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; khiter_t it; it = kh_get(dict, h, key); @@ -32,9 +31,9 @@ pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym *key) } void -pic_dict_set(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key, pic_value val) +pic_dict_set(pic_state *pic, pic_value dict, pic_sym *key, pic_value val) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int ret; khiter_t it; @@ -43,21 +42,23 @@ pic_dict_set(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key, pi } int -pic_dict_size(pic_state PIC_UNUSED(*pic), struct pic_dict *dict) +pic_dict_size(pic_state PIC_UNUSED(*pic), pic_value dict) { - return kh_size(&dict->hash); + return kh_size(&pic_dict_ptr(pic, dict)->hash); } bool -pic_dict_has(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key) +pic_dict_has(pic_state *pic, pic_value dict, pic_sym *key) { - return kh_get(dict, &dict->hash, key) != kh_end(&dict->hash); + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; + + return kh_get(dict, h, key) != kh_end(h); } void -pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym *key) +pic_dict_del(pic_state *pic, pic_value dict, pic_sym *key) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; khiter_t it; it = kh_get(dict, h, key); @@ -68,9 +69,9 @@ pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym *key) } bool -pic_dict_next(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, int *iter, pic_sym **key, pic_value *val) +pic_dict_next(pic_state PIC_UNUSED(*pic), pic_value dict, int *iter, pic_sym **key, pic_value *val) { - khash_t(dict) *h = &dict->hash; + khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int it = *iter; for (it = *iter; it != kh_end(h); ++it) { @@ -87,20 +88,15 @@ pic_dict_next(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, int *iter, pic_ static pic_value pic_dict_make_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_get_args(pic, ""); - dict = pic_make_dict(pic); - - return pic_obj_value(dict); + return pic_make_dict(pic); } static pic_value pic_dict_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_value *argv; + pic_value dict, *argv; int argc, i; pic_get_args(pic, "*", &argc, &argv); @@ -112,7 +108,7 @@ pic_dict_dictionary(pic_state *pic) pic_dict_set(pic, dict, pic_sym_ptr(argv[i]), argv[i+1]); } - return pic_obj_value(dict); + return dict; } static pic_value @@ -128,7 +124,7 @@ pic_dict_dictionary_p(pic_state *pic) static pic_value pic_dict_dictionary_ref(pic_state *pic) { - struct pic_dict *dict; + pic_value dict; pic_sym *key; pic_get_args(pic, "dm", &dict, &key); @@ -142,7 +138,7 @@ pic_dict_dictionary_ref(pic_state *pic) static pic_value pic_dict_dictionary_set(pic_state *pic) { - struct pic_dict *dict; + pic_value dict; pic_sym *key; pic_value val; @@ -162,7 +158,7 @@ pic_dict_dictionary_set(pic_state *pic) static pic_value pic_dict_dictionary_size(pic_state *pic) { - struct pic_dict *dict; + pic_value dict; pic_get_args(pic, "d", &dict); @@ -173,21 +169,15 @@ static pic_value pic_dict_dictionary_map(pic_state *pic) { struct pic_proc *proc; - struct pic_dict *dict; - khiter_t it; - khash_t(dict) *kh; - pic_value ret = pic_nil_value(pic); + pic_value dict, ret = pic_nil_value(pic); + pic_sym *key; + int it = 0; pic_get_args(pic, "ld", &proc, &dict); - kh = &dict->hash; - - for (it = kh_begin(kh); it != kh_end(kh); ++it) { - if (kh_exist(kh, it)) { - pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))), ret); - } + while (pic_dict_next(pic, dict, &it, &key, NULL)) { + pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(key)), ret); } - return pic_reverse(pic, ret); } @@ -195,18 +185,14 @@ static pic_value pic_dict_dictionary_for_each(pic_state *pic) { struct pic_proc *proc; - struct pic_dict *dict; - khiter_t it; - khash_t(dict) *kh; + pic_value dict; + pic_sym *key; + int it; pic_get_args(pic, "ld", &proc, &dict); - kh = &dict->hash; - - for (it = kh_begin(kh); it != kh_end(kh); ++it) { - if (kh_exist(kh, it)) { - pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))); - } + while (pic_dict_next(pic, dict, &it, &key, NULL)) { + pic_call(pic, proc, 1, pic_obj_value(key)); } return pic_undef_value(pic); @@ -215,8 +201,7 @@ pic_dict_dictionary_for_each(pic_state *pic) static pic_value pic_dict_dictionary_to_alist(pic_state *pic) { - struct pic_dict *dict; - pic_value val, alist = pic_nil_value(pic); + pic_value dict, val, alist = pic_nil_value(pic); pic_sym *sym; int it = 0; @@ -232,8 +217,7 @@ pic_dict_dictionary_to_alist(pic_state *pic) static pic_value pic_dict_alist_to_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_value alist, e, it; + pic_value dict, alist, e, it; pic_get_args(pic, "o", &alist); @@ -244,14 +228,13 @@ pic_dict_alist_to_dictionary(pic_state *pic) pic_dict_set(pic, dict, pic_sym_ptr(pic_car(pic, e)), pic_cdr(pic, e)); } - return pic_obj_value(dict); + return dict; } static pic_value pic_dict_dictionary_to_plist(pic_state *pic) { - struct pic_dict *dict; - pic_value val, plist = pic_nil_value(pic); + pic_value dict, val, plist = pic_nil_value(pic); pic_sym *sym; int it = 0; @@ -268,8 +251,7 @@ pic_dict_dictionary_to_plist(pic_state *pic) static pic_value pic_dict_plist_to_dictionary(pic_state *pic) { - struct pic_dict *dict; - pic_value plist, e; + pic_value dict, plist, e; pic_get_args(pic, "o", &plist); @@ -280,7 +262,7 @@ pic_dict_plist_to_dictionary(pic_state *pic) pic_dict_set(pic, dict, pic_sym_ptr(pic_cadr(pic, e)), pic_car(pic, e)); } - return pic_obj_value(dict); + return dict; } void diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index b6958e31..2a7a29a2 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -363,7 +363,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) pic_value val; int it = 0; - while (pic_dict_next(pic, &obj->u.dict, &it, &sym, &val)) { + while (pic_dict_next(pic, pic_obj_value(&obj->u.dict), &it, &sym, &val)) { gc_mark_object(pic, (struct pic_object *)sym); gc_mark(pic, val); } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 94600879..b2421e3b 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -241,13 +241,13 @@ void pic_vec_set(pic_state *, pic_vec *, int, pic_value); int pic_vec_len(pic_state *, pic_vec *); /* dictionary */ -struct pic_dict *pic_make_dict(pic_state *); -pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_sym *); -void pic_dict_set(pic_state *, struct pic_dict *, pic_sym *, pic_value); -void pic_dict_del(pic_state *, struct pic_dict *, pic_sym *); -bool pic_dict_has(pic_state *, struct pic_dict *, pic_sym *); -int pic_dict_size(pic_state *, struct pic_dict *); -bool pic_dict_next(pic_state *, struct pic_dict *, int *iter, pic_sym **key, pic_value *val); +pic_value pic_make_dict(pic_state *); +pic_value pic_dict_ref(pic_state *, pic_value dict, pic_sym *); +void pic_dict_set(pic_state *, pic_value dict, pic_sym *, pic_value); +void pic_dict_del(pic_state *, pic_value dict, pic_sym *); +bool pic_dict_has(pic_state *, pic_value dict, pic_sym *); +int pic_dict_size(pic_state *, pic_value dict); +bool pic_dict_next(pic_state *, pic_value dict, int *iter, pic_sym **key, pic_value *val); /* ephemeron */ struct pic_weak *pic_make_weak(pic_state *); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 2335cd59..4f5ddfe9 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -9,11 +9,9 @@ extern "C" { #endif - -struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); - - -/* symbol & identifier */ +KHASH_DECLARE(env, pic_id *, pic_sym *) +KHASH_DECLARE(dict, pic_sym *, pic_value) +KHASH_DECLARE(weak, void *, pic_value) struct pic_id { union { @@ -29,21 +27,12 @@ struct pic_id { } u; }; -#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) - -#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) -#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) - -pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); - -pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); -pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); -pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); - -struct pic_string *pic_id_name(pic_state *, pic_id *); - - -/* pair */ +struct pic_env { + PIC_OBJECT_HEADER + khash_t(env) map; + struct pic_env *up; + struct pic_string *lib; +}; struct pic_pair { PIC_OBJECT_HEADER @@ -51,69 +40,33 @@ struct pic_pair { pic_value cdr; }; -#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) - - -/* blob */ - struct pic_blob { PIC_OBJECT_HEADER unsigned char *data; int len; }; -#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) - -/* string */ - struct pic_string { PIC_OBJECT_HEADER struct pic_rope *rope; }; -void pic_rope_incref(pic_state *, struct pic_rope *); -void pic_rope_decref(pic_state *, struct pic_rope *); - -#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) - - -/* vector */ - -struct pic_vector { - PIC_OBJECT_HEADER - pic_value *data; - int len; -}; - -#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) - - -/* dictionary */ - -KHASH_DECLARE(dict, pic_sym *, pic_value) - struct pic_dict { PIC_OBJECT_HEADER khash_t(dict) hash; }; -#define pic_dict_ptr(v) ((struct pic_dict *)pic_obj_ptr(v)) - - -/* weak */ - -KHASH_DECLARE(weak, void *, pic_value) - struct pic_weak { PIC_OBJECT_HEADER khash_t(weak) hash; struct pic_weak *prev; /* for GC */ }; -#define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) - - -/* data */ +struct pic_vector { + PIC_OBJECT_HEADER + pic_value *data; + int len; +}; struct pic_data { PIC_OBJECT_HEADER @@ -121,11 +74,6 @@ struct pic_data { void *data; }; -#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) - - -/* context */ - struct pic_context { PIC_OBJECT_HEADER pic_value *regs; @@ -134,11 +82,6 @@ struct pic_context { pic_value storage[1]; }; -#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) - - -/* procedure */ - struct pic_proc { PIC_OBJECT_HEADER enum { @@ -158,31 +101,12 @@ struct pic_proc { pic_value locals[1]; }; -#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) - -#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) -#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) - -struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); -struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); - - -/* record */ - struct pic_record { PIC_OBJECT_HEADER pic_value type; pic_value datum; }; -#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) -#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) - -struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); - - -/* error */ - struct pic_error { PIC_OBJECT_HEADER pic_sym *type; @@ -191,38 +115,52 @@ struct pic_error { struct pic_string *stack; }; -#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) -#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) - -struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); - - -/* port */ - struct pic_port { PIC_OBJECT_HEADER xFILE *file; }; +#define pic_dict_ptr(pic, v) ((struct pic_dict *)pic_obj_ptr(v)) +#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) +#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) +#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) +#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) +#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) +#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) +#define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) +#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) +#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) +#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) +#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) +#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) - - -/* environment */ - -KHASH_DECLARE(env, pic_id *, pic_sym *) - -struct pic_env { - PIC_OBJECT_HEADER - khash_t(env) map; - struct pic_env *up; - struct pic_string *lib; -}; - -#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) #define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) +#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) +#define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) +#define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) +#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) + +struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); + +pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); +struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); +struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); +struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); +struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); struct pic_env *pic_make_env(pic_state *, struct pic_env *); +pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); +pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); +pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); +struct pic_string *pic_id_name(pic_state *, pic_id *); + +void pic_rope_incref(pic_state *, struct pic_rope *); +void pic_rope_decref(pic_state *, struct pic_rope *); + +#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) +#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) + #if defined(__cplusplus) } diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index bf7a7246..b97c49f0 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -57,7 +57,7 @@ pic_make_library(pic_state *pic, const char *lib) const char *old_lib; struct pic_string *name; struct pic_env *env; - struct pic_dict *exports; + pic_value exports; khiter_t it; int ret; @@ -76,7 +76,7 @@ pic_make_library(pic_state *pic, const char *lib) kh_val(h, it).name = name; kh_val(h, it).env = env; - kh_val(h, it).exports = exports; + kh_val(h, it).exports = pic_dict_ptr(pic, exports); if (pic->lib) { pic->lib = get_library(pic, old_lib); /* ltable might be rehashed */ @@ -117,7 +117,7 @@ pic_import(pic_state *pic, const char *lib) libp = get_library(pic, lib); - while (pic_dict_next(pic, libp->exports, &it, &name, &val)) { + while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &val)) { realname = pic_sym_ptr(val); if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { @@ -130,7 +130,7 @@ pic_import(pic_state *pic, const char *lib) void pic_export(pic_state *pic, pic_sym *name) { - pic_dict_set(pic, pic->lib->exports, name, pic_obj_value(name)); + pic_dict_set(pic, pic_obj_value(pic->lib->exports), name, pic_obj_value(name)); } static pic_value @@ -188,10 +188,10 @@ pic_lib_library_import(pic_state *pic) libp = get_library(pic, lib); - if (! pic_dict_has(pic, libp->exports, name)) { + if (! pic_dict_has(pic, pic_obj_value(libp->exports), name)) { pic_errorf(pic, "attempted to import undefined variable '~s'", pic_obj_value(name)); } else { - realname = pic_sym_ptr(pic_dict_ref(pic, libp->exports, name)); + realname = pic_sym_ptr(pic_dict_ref(pic, pic_obj_value(libp->exports), name)); } if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { @@ -214,7 +214,7 @@ pic_lib_library_export(pic_state *pic) alias = name; } - pic_dict_set(pic, pic->lib->exports, alias, pic_obj_value(name)); + pic_dict_set(pic, pic_obj_value(pic->lib->exports), alias, pic_obj_value(name)); return pic_undef_value(pic); } @@ -232,7 +232,7 @@ pic_lib_library_exports(pic_state *pic) libp = get_library(pic, lib); - while (pic_dict_next(pic, libp->exports, &it, &sym, NULL)) { + while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &sym, NULL)) { pic_push(pic, pic_obj_value(sym), exports); } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 9ce9dc70..b3a8970e 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -26,7 +26,7 @@ * b struct pic_blob ** bytevector object * l struct pic_proc ** lambda object * p struct pic_port ** port object - * d struct pic_dict ** dictionary object + * d pic_value * dictionary object * e struct pic_error ** error object * r struct pic_record ** record object * @@ -153,10 +153,13 @@ pic_get_args(pic_state *pic, const char *format, ...) PTR_CASE('b', blob, struct pic_blob *) PTR_CASE('l', proc, struct pic_proc *) PTR_CASE('p', port, struct pic_port *) - PTR_CASE('d', dict, struct pic_dict *) PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) +#define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + + OBJ_CASE('d', dict) + default: pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); } diff --git a/extlib/benz/write.c b/extlib/benz/write.c index e23bcedc..c80b7fc3 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -238,7 +238,7 @@ write_vec(struct writer_control *p, pic_vec *vec) } static void -write_dict(struct writer_control *p, struct pic_dict *dict) +write_dict(struct writer_control *p, pic_value dict) { pic_state *pic = p->pic; xFILE *file = p->file; @@ -318,7 +318,7 @@ write_core(struct writer_control *p, pic_value obj) write_vec(p, pic_vec_ptr(obj)); break; case PIC_TYPE_DICT: - write_dict(p, pic_dict_ptr(obj)); + write_dict(p, obj); break; default: xfprintf(pic, file, "#<%s %p>", pic_typename(pic, pic_type(pic, obj)), pic_obj_ptr(obj)); @@ -369,7 +369,7 @@ traverse(struct writer_control *p, pic_value obj) /* dictionary */ int it = 0; pic_value val; - while (pic_dict_next(pic, pic_dict_ptr(obj), &it, NULL, &val)) { + while (pic_dict_next(pic, obj, &it, NULL, &val)) { traverse(p, val); } } From 9f53b39a04fdda6be7f8d6151f879c8f965a3ae7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 19:25:16 +0900 Subject: [PATCH 052/119] don't use pic_vec and object.h in callcc.c --- contrib/10.callcc/callcc.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 8f6a2c15..80905d8e 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -1,5 +1,4 @@ #include "picrin.h" -#include "picrin/object.h" struct pic_fullcont { jmp_buf jmp; @@ -30,7 +29,8 @@ struct pic_fullcont { struct pic_object **arena; size_t arena_size, arena_idx; - pic_vec *results; + int retc; + pic_value *retv; }; static void @@ -90,9 +90,6 @@ cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value)) /* parameter table */ mark(pic, cont->ptable); - - /* result values */ - mark(pic, pic_obj_value(cont->results)); } static const pic_data_type cont_type = { "continuation", cont_dtor, cont_mark }; @@ -159,7 +156,8 @@ save_cont(pic_state *pic, struct pic_fullcont **c) cont->arena = pic_malloc(pic, sizeof(struct pic_object *) * pic->arena_size); memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * pic->arena_size); - cont->results = pic_make_vec(pic, 0, NULL); + cont->retc = 0; + cont->retv = NULL; } static void @@ -219,14 +217,20 @@ restore_cont(pic_state *pic, struct pic_fullcont *cont) PIC_NORETURN static pic_value cont_call(pic_state *pic) { - int argc; - pic_value *argv; + int argc, i; + pic_value *argv, *retv; struct pic_fullcont *cont; pic_get_args(pic, "*", &argc, &argv); + retv = pic_alloca(pic, sizeof(pic_value) * argc); + for (i = 0; i < argc; ++i) { + retv[i] = argv[i]; + } + cont = pic_data(pic, pic_closure_ref(pic, 0)); - cont->results = pic_make_vec(pic, argc, argv); + cont->retc = argc; + cont->retv = retv; /* execute guard handlers */ pic_wind(pic, pic->cp, cont->cp); @@ -241,7 +245,7 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) save_cont(pic, &cont); if (setjmp(cont->jmp)) { - return pic_valuesk(pic, cont->results->len, cont->results->data); + return pic_valuesk(pic, cont->retc, cont->retv); } else { struct pic_proc *c; From 25e19d4f007dc51194f6d21bcedd5900991cb8a5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 21:56:45 +0900 Subject: [PATCH 053/119] struct pic_vector * -> pic_value --- extlib/benz/bool.c | 13 +- extlib/benz/eval.c | 71 ++++----- extlib/benz/include/picrin.h | 10 +- extlib/benz/include/picrin/compat.h | 18 +++ extlib/benz/include/picrin/object.h | 15 +- extlib/benz/proc.c | 4 +- extlib/benz/read.c | 17 +-- extlib/benz/vector.c | 219 +++++++++++++++------------- extlib/benz/write.c | 18 +-- 9 files changed, 214 insertions(+), 171 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 19c856cb..c5d47823 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -158,17 +158,16 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) goto LOOP; /* tail-call optimization */ } case PIC_TYPE_VECTOR: { - int i; - struct pic_vector *u, *v; + int i, xlen, ylen; - u = pic_vec_ptr(x); - v = pic_vec_ptr(y); + xlen = pic_vec_len(pic, x); + ylen = pic_vec_len(pic, y); - if (u->len != v->len) { + if (xlen != ylen) { return false; } - for (i = 0; i < u->len; ++i) { - if (! internal_equal_p(pic, u->data[i], v->data[i], depth + 1, h)) + for (i = 0; i < xlen; ++i) { + if (! internal_equal_p(pic, pic_vec_ref(pic, x, i), pic_vec_ref(pic, y, i), depth + 1, h)) return false; } return true; diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 264c3272..64160c7f 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -217,7 +217,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) analyze_scope s, *scope = &s; pic_value formals, body; pic_value rest = pic_undef_value(pic); - pic_vec *args, *locals, *captures; + pic_value args, locals, captures; int i, j; khiter_t it; @@ -232,7 +232,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) args = pic_make_vec(pic, kh_size(&scope->args), NULL); for (i = 0; pic_pair_p(pic, formals); formals = pic_cdr(pic, formals), i++) { - args->data[i] = pic_car(pic, formals); + pic_vec_set(pic, args, i, pic_car(pic, formals)); } if (scope->rest != NULL) { @@ -242,26 +242,26 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) locals = pic_make_vec(pic, kh_size(&scope->locals), NULL); j = 0; if (scope->rest != NULL) { - locals->data[j++] = pic_obj_value(scope->rest); + pic_vec_set(pic, locals, j++, pic_obj_value(scope->rest)); } for (it = kh_begin(&scope->locals); it < kh_end(&scope->locals); ++it) { if (kh_exist(&scope->locals, it)) { if (scope->rest != NULL && kh_key(&scope->locals, it) == scope->rest) continue; - locals->data[j++] = pic_obj_value(kh_key(&scope->locals, it)); + pic_vec_set(pic, locals, j++, pic_obj_value(kh_key(&scope->locals, it))); } } captures = pic_make_vec(pic, kh_size(&scope->captures), NULL); for (it = kh_begin(&scope->captures), j = 0; it < kh_end(&scope->captures); ++it) { if (kh_exist(&scope->captures, it)) { - captures->data[j++] = pic_obj_value(kh_key(&scope->captures, it)); + pic_vec_set(pic, captures, j++, pic_obj_value(kh_key(&scope->captures, it))); } } analyzer_scope_destroy(pic, scope); - return pic_list(pic, 6, 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, args, locals, captures, body); } static pic_value @@ -360,7 +360,7 @@ pic_analyze(pic_state *pic, pic_value obj) typedef struct codegen_context { /* rest args variable is counted as a local */ pic_sym *rest; - pic_vec *args, *locals, *captures; + pic_value args, locals, captures; /* actual bit code sequence */ pic_code *code; size_t clen, ccapa; @@ -381,7 +381,7 @@ typedef struct codegen_context { static void create_activation(pic_state *, codegen_context *); static void -codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, pic_sym *rest, pic_vec *args, pic_vec *locals, pic_vec *captures) +codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, pic_sym *rest, pic_value args, pic_value locals, pic_value captures) { cxt->up = up; cxt->rest = rest; @@ -422,9 +422,9 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) irep = pic_malloc(pic, sizeof(struct pic_irep)); irep->refc = 1; irep->varg = cxt->rest != NULL; - irep->argc = (int)cxt->args->len + 1; - irep->localc = (int)cxt->locals->len; - irep->capturec = (int)cxt->captures->len; + irep->argc = pic_vec_len(pic, cxt->args) + 1; + irep->localc = pic_vec_len(pic, cxt->locals); + irep->capturec = pic_vec_len(pic, cxt->captures); irep->code = pic_realloc(pic, cxt->code, sizeof(pic_code) * cxt->clen); irep->irep = pic_realloc(pic, cxt->irep, sizeof(struct pic_irep *) * cxt->ilen); irep->ints = pic_realloc(pic, cxt->ints, sizeof(int) * cxt->klen); @@ -481,7 +481,7 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) #define emit_ret(pic, cxt, tailpos) if (tailpos) emit_n(pic, cxt, OP_RET) static int -index_capture(codegen_context *cxt, pic_sym *sym, int depth) +index_capture(pic_state *pic, codegen_context *cxt, pic_sym *sym, int depth) { int i; @@ -489,26 +489,26 @@ index_capture(codegen_context *cxt, pic_sym *sym, int depth) cxt = cxt->up; } - for (i = 0; i < cxt->captures->len; ++i) { - if (pic_sym_ptr(cxt->captures->data[i]) == sym) + for (i = 0; i < pic_vec_len(pic, cxt->captures); ++i) { + if (pic_sym_ptr(pic_vec_ref(pic, cxt->captures, i)) == sym) return i; } return -1; } static int -index_local(codegen_context *cxt, pic_sym *sym) +index_local(pic_state *pic, codegen_context *cxt, pic_sym *sym) { int i, offset; offset = 1; - for (i = 0; i < cxt->args->len; ++i) { - if (pic_sym_ptr(cxt->args->data[i]) == sym) + for (i = 0; i < pic_vec_len(pic, cxt->args); ++i) { + if (pic_sym_ptr(pic_vec_ref(pic, cxt->args, i)) == sym) return i + offset; } offset += i; - for (i = 0; i < cxt->locals->len; ++i) { - if (pic_sym_ptr(cxt->locals->data[i]) == sym) + for (i = 0; i < pic_vec_len(pic, cxt->locals); ++i) { + if (pic_sym_ptr(pic_vec_ref(pic, cxt->locals, i)) == sym) return i + offset; } return -1; @@ -531,10 +531,11 @@ create_activation(pic_state *pic, codegen_context *cxt) { int i, n; - for (i = 0; i < cxt->captures->len; ++i) { - n = index_local(cxt, pic_sym_ptr(cxt->captures->data[i])); + for (i = 0; i < pic_vec_len(pic, cxt->captures); ++i) { + pic_sym *sym = pic_sym_ptr(pic_vec_ref(pic, cxt->captures, i)); + n = index_local(pic, cxt, sym); assert(n != -1); - if (n <= cxt->args->len || cxt->rest == pic_sym_ptr(cxt->captures->data[i])) { + if (n <= pic_vec_len(pic, cxt->args) || cxt->rest == sym) { /* copy arguments to capture variable area */ emit_i(pic, cxt, OP_LREF, n); } else { @@ -565,7 +566,7 @@ codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) depth = pic_int(pic, pic_list_ref(pic, obj, 1)); name = pic_sym_ptr(pic_list_ref(pic, obj, 2)); - emit_r(pic, cxt, OP_CREF, depth, index_capture(cxt, name, depth)); + emit_r(pic, cxt, OP_CREF, depth, index_capture(pic, cxt, name, depth)); emit_ret(pic, cxt, tailpos); } else if (sym == LREF) { @@ -573,11 +574,11 @@ codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) int i; name = pic_sym_ptr(pic_list_ref(pic, obj, 1)); - if ((i = index_capture(cxt, name, 0)) != -1) { - emit_i(pic, cxt, OP_LREF, i + (int)cxt->args->len + (int)cxt->locals->len + 1); + if ((i = index_capture(pic, cxt, name, 0)) != -1) { + emit_i(pic, cxt, OP_LREF, i + pic_vec_len(pic, cxt->args) + pic_vec_len(pic, cxt->locals) + 1); emit_ret(pic, cxt, tailpos); } else { - emit_i(pic, cxt, OP_LREF, index_local(cxt, name)); + emit_i(pic, cxt, OP_LREF, index_local(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } } @@ -607,7 +608,7 @@ codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) depth = pic_int(pic, pic_list_ref(pic, var, 1)); name = pic_sym_ptr(pic_list_ref(pic, var, 2)); - emit_r(pic, cxt, OP_CSET, depth, index_capture(cxt, name, depth)); + emit_r(pic, cxt, OP_CSET, depth, index_capture(pic, cxt, name, depth)); emit_ret(pic, cxt, tailpos); } else if (type == LREF) { @@ -615,11 +616,11 @@ codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) int i; name = pic_sym_ptr(pic_list_ref(pic, var, 1)); - if ((i = index_capture(cxt, name, 0)) != -1) { - emit_i(pic, cxt, OP_LSET, i + (int)cxt->args->len + (int)cxt->locals->len + 1); + if ((i = index_capture(pic, cxt, name, 0)) != -1) { + emit_i(pic, cxt, OP_LSET, i + pic_vec_len(pic, cxt->args) + pic_vec_len(pic, cxt->locals) + 1); emit_ret(pic, cxt, tailpos); } else { - emit_i(pic, cxt, OP_LSET, index_local(cxt, name)); + emit_i(pic, cxt, OP_LSET, index_local(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } } @@ -631,7 +632,7 @@ codegen_lambda(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos codegen_context c, *inner_cxt = &c; pic_value rest_opt, body; pic_sym *rest = NULL; - pic_vec *args, *locals, *captures; + pic_value args, locals, captures; check_irep_size(pic, cxt); @@ -640,9 +641,9 @@ codegen_lambda(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos if (pic_sym_p(pic, rest_opt)) { rest = pic_sym_ptr(rest_opt); } - args = pic_vec_ptr(pic_list_ref(pic, obj, 2)); - locals = pic_vec_ptr(pic_list_ref(pic, obj, 3)); - captures = pic_vec_ptr(pic_list_ref(pic, obj, 4)); + args = pic_list_ref(pic, obj, 2); + locals = pic_list_ref(pic, obj, 3); + captures = pic_list_ref(pic, obj, 4); body = pic_list_ref(pic, obj, 5); /* emit irep */ @@ -818,7 +819,7 @@ codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) static struct pic_irep * pic_codegen(pic_state *pic, pic_value obj) { - pic_vec *empty = pic_make_vec(pic, 0, NULL); + pic_value empty = pic_make_vec(pic, 0, NULL); codegen_context c, *cxt = &c; codegen_context_init(pic, cxt, NULL, NULL, empty, empty, empty); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index b2421e3b..b1483dd3 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -55,7 +55,6 @@ struct pic_object; struct pic_symbol; struct pic_pair; struct pic_string; -struct pic_vector; struct pic_blob; struct pic_proc; struct pic_port; @@ -66,7 +65,6 @@ struct pic_data; typedef struct pic_symbol pic_sym; typedef struct pic_id pic_id; typedef struct pic_pair pic_pair; -typedef struct pic_vector pic_vec; typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); @@ -235,10 +233,10 @@ pic_value pic_reverse(pic_state *, pic_value list); pic_value pic_append(pic_state *, pic_value xs, pic_value ys); /* vector */ -pic_vec *pic_make_vec(pic_state *, int, pic_value *); -pic_value pic_vec_ref(pic_state *, pic_vec *, int); -void pic_vec_set(pic_state *, pic_vec *, int, pic_value); -int pic_vec_len(pic_state *, pic_vec *); +pic_value pic_make_vec(pic_state *, int n, pic_value *argv); +pic_value pic_vec_ref(pic_state *, pic_value vec, int i); +void pic_vec_set(pic_state *, pic_value vec, int i, pic_value v); +int pic_vec_len(pic_state *, pic_value vec); /* dictionary */ pic_value pic_make_dict(pic_state *); diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h index 8f2bb886..ba4d964e 100644 --- a/extlib/benz/include/picrin/compat.h +++ b/extlib/benz/include/picrin/compat.h @@ -203,6 +203,24 @@ memcpy(void *dst, const void *src, size_t n) return d; } +PIC_INLINE void * +memmove(void *dst, const void *src, size_t n) +{ + const char *s = src; + char *d = dst; + + if (d <= s || d >= s + n) { + memcpy(dst, src, n); + } else { + s += n; + d += n; + while (n-- > 0) { + *--d = *--s; + } + } + return d; +} + PIC_INLINE char * strcpy(char *dst, const char *src) { diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 4f5ddfe9..08acd36c 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -120,13 +120,13 @@ struct pic_port { xFILE *file; }; +#define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_dict_ptr(pic, v) ((struct pic_dict *)pic_obj_ptr(v)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) #define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) #define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) #define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) -#define pic_vec_ptr(o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) #define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) @@ -143,6 +143,19 @@ struct pic_port { struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); +#define VALID_INDEX(pic, len, i) do { \ + if (i < 0 || len <= i) pic_errorf(pic, "index out of range: %d", i); \ + } while (0) +#define VALID_RANGE(pic, len, s, e) do { \ + if (s < 0 || len < s) pic_errorf(pic, "invalid start index: %d", s); \ + if (e < s || len < e) pic_errorf(pic, "invalid end index: %d", e); \ + } while (0) +#define VALID_ATRANGE(pic, tolen, at, fromlen, s, e) do { \ + VALID_INDEX(pic, tolen, at); \ + VALID_RANGE(pic, fromlen, s, e); \ + if (tolen - at < e - s) pic_errorf(pic, "invalid range"); \ + } while (0) + pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index b3a8970e..712c26db 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -21,7 +21,7 @@ * c char * char * z char ** c string * m pic_sym ** symbol - * v pic_vec ** vector object + * v pic_value * vector object * s struct pic_str ** string object * b struct pic_blob ** bytevector object * l struct pic_proc ** lambda object @@ -148,7 +148,6 @@ pic_get_args(pic_state *pic, const char *format, ...) VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) PTR_CASE('m', sym, pic_sym *) - PTR_CASE('v', vec, pic_vec *) PTR_CASE('s', str, struct pic_string *) PTR_CASE('b', blob, struct pic_blob *) PTR_CASE('l', proc, struct pic_proc *) @@ -158,6 +157,7 @@ pic_get_args(pic_state *pic, const char *format, ...) #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + OBJ_CASE('v', vec) OBJ_CASE('d', dict) default: diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 88e636b2..2a7c99e5 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -590,8 +590,7 @@ read_pair(pic_state *pic, xFILE *file, int c) static pic_value read_vector(pic_state *pic, xFILE *file, int c) { - pic_value list, it, elem; - pic_vec *vec; + pic_value list, it, elem, vec; int i = 0; list = read(pic, file, c); @@ -599,10 +598,10 @@ read_vector(pic_state *pic, xFILE *file, int c) vec = pic_make_vec(pic, pic_length(pic, list), NULL); pic_for_each (elem, list, it) { - vec->data[i++] = elem; + pic_vec_set(pic, vec, i++, elem); } - return pic_obj_value(vec); + return vec; } static pic_value @@ -639,13 +638,13 @@ read_label_set(pic_state *pic, xFILE *file, int i) } if (vect) { - pic_vec *tmp; + pic_value tmp; - kh_val(h, it) = val = pic_obj_value(pic_make_vec(pic, 0, NULL)); + kh_val(h, it) = val = pic_make_vec(pic, 0, NULL); - tmp = pic_vec_ptr(read(pic, file, c)); - PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); - PIC_SWAP(int, tmp->len, pic_vec_ptr(val)->len); + tmp = read(pic, file, c); + PIC_SWAP(pic_value *, pic_vec_ptr(pic, tmp)->data, pic_vec_ptr(pic, val)->data); + PIC_SWAP(int, pic_vec_ptr(pic, tmp)->len, pic_vec_ptr(pic, val)->len); return val; } diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 339a346b..da75cd9c 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -5,7 +5,7 @@ #include "picrin.h" #include "picrin/object.h" -struct pic_vector * +pic_value pic_make_vec(pic_state *pic, int len, pic_value *argv) { struct pic_vector *vec; @@ -21,7 +21,25 @@ pic_make_vec(pic_state *pic, int len, pic_value *argv) } else { memcpy(vec->data, argv, sizeof(pic_value) * len); } - return vec; + return pic_obj_value(vec); +} + +pic_value +pic_vec_ref(pic_state PIC_UNUSED(*pic), pic_value vec, int k) +{ + return pic_vec_ptr(pic, vec)->data[k]; +} + +void +pic_vec_set(pic_state PIC_UNUSED(*pic), pic_value vec, int k, pic_value val) +{ + pic_vec_ptr(pic, vec)->data[k] = val; +} + +int +pic_vec_len(pic_state PIC_UNUSED(*pic), pic_value vec) +{ + return pic_vec_ptr(pic, vec)->len; } static pic_value @@ -39,100 +57,92 @@ pic_vec_vector(pic_state *pic) { int argc; pic_value *argv; - pic_vec *vec; pic_get_args(pic, "*", &argc, &argv); - vec = pic_make_vec(pic, argc, argv); - - return pic_obj_value(vec); + return pic_make_vec(pic, argc, argv); } static pic_value pic_vec_make_vector(pic_state *pic) { - pic_value v; + pic_value vec, init; int n, k, i; - struct pic_vector *vec; - n = pic_get_args(pic, "i|o", &k, &v); + n = pic_get_args(pic, "i|o", &k, &init); + + if (k < 0) { + pic_errorf(pic, "make-vector: negative length given %d", k); + } vec = pic_make_vec(pic, k, NULL); if (n == 2) { for (i = 0; i < k; ++i) { - vec->data[i] = v; + pic_vec_set(pic, vec, i, init); } } - return pic_obj_value(vec); + return vec; } static pic_value pic_vec_vector_length(pic_state *pic) { - struct pic_vector *v; + pic_value v; pic_get_args(pic, "v", &v); - return pic_int_value(pic, v->len); + return pic_int_value(pic, pic_vec_len(pic, v)); } static pic_value pic_vec_vector_ref(pic_state *pic) { - struct pic_vector *v; + pic_value v; int k; pic_get_args(pic, "vi", &v, &k); - if (v->len <= k) { - pic_errorf(pic, "vector-ref: index out of range"); - } - return v->data[k]; + VALID_INDEX(pic, pic_vec_len(pic, v), k); + + return pic_vec_ref(pic, v, k); } static pic_value pic_vec_vector_set(pic_state *pic) { - struct pic_vector *v; + pic_value v, o; int k; - pic_value o; pic_get_args(pic, "vio", &v, &k, &o); - if (v->len <= k) { - pic_errorf(pic, "vector-set!: index out of range"); - } - v->data[k] = o; + VALID_INDEX(pic, pic_vec_len(pic, v), k); + + pic_vec_set(pic, v, k, o); + return pic_undef_value(pic); } static pic_value pic_vec_vector_copy_i(pic_state *pic) { - pic_vec *to, *from; - int n, at, start, end; + pic_value to, from; + int n, at, start, end, tolen, fromlen; n = pic_get_args(pic, "viv|ii", &to, &at, &from, &start, &end); + tolen = pic_vec_len(pic, to); + fromlen = pic_vec_len(pic, from); + switch (n) { case 3: start = 0; case 4: - end = from->len; + end = fromlen; } - if (to == from && (start <= at && at < end)) { - /* copy in reversed order */ - at += end - start; - while (start < end) { - to->data[--at] = from->data[--end]; - } - return pic_undef_value(pic); - } + VALID_ATRANGE(pic, tolen, at, fromlen, start, end); - while (start < end) { - to->data[at++] = from->data[start++]; - } + memmove(pic_vec_ptr(pic, to)->data + at, pic_vec_ptr(pic, from)->data + start, sizeof(pic_value) * (end - start)); return pic_undef_value(pic); } @@ -140,73 +150,72 @@ pic_vec_vector_copy_i(pic_state *pic) static pic_value pic_vec_vector_copy(pic_state *pic) { - pic_vec *from, *to; - int n, start, end; + pic_value from; + int n, start, end, fromlen; n = pic_get_args(pic, "v|ii", &from, &start, &end); + fromlen = pic_vec_len(pic, from); + switch (n) { case 1: start = 0; case 2: - end = from->len; + end = fromlen; } - if (end < start) { - pic_errorf(pic, "vector-copy: end index must not be less than start index"); - } + VALID_RANGE(pic, fromlen, start, end); - to = pic_make_vec(pic, end - start, from->data + start); - - return pic_obj_value(to); + return pic_make_vec(pic, end - start, pic_vec_ptr(pic, from)->data + start); } static pic_value pic_vec_vector_append(pic_state *pic) { - pic_value *argv; - int argc, i, j, len; - pic_vec *vec; + pic_value *argv, vec; + int argc, i, len; pic_get_args(pic, "*", &argc, &argv); len = 0; for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], vec); - len += pic_vec_ptr(argv[i])->len; + len += pic_vec_len(pic, argv[i]); } vec = pic_make_vec(pic, len, NULL); len = 0; for (i = 0; i < argc; ++i) { - for (j = 0; j < pic_vec_ptr(argv[i])->len; ++j) { - vec->data[len + j] = pic_vec_ptr(argv[i])->data[j]; - } - len += pic_vec_ptr(argv[i])->len; + int l = pic_vec_len(pic, argv[i]); + memcpy(pic_vec_ptr(pic, vec)->data + len, pic_vec_ptr(pic, argv[i])->data, sizeof(pic_value) * l); + len += l; } - return pic_obj_value(vec); + return vec; } static pic_value pic_vec_vector_fill_i(pic_state *pic) { - pic_vec *vec; - pic_value obj; - int n, start, end; + pic_value vec, obj; + int n, start, end, len; n = pic_get_args(pic, "vo|ii", &vec, &obj, &start, &end); + len = pic_vec_len(pic, vec); + switch (n) { case 2: start = 0; case 3: - end = vec->len; + end = len; } + VALID_RANGE(pic, len, start, end); + while (start < end) { - vec->data[start++] = obj; + pic_vec_set(pic, vec, start++, obj); } return pic_undef_value(pic); @@ -217,18 +226,20 @@ pic_vec_vector_map(pic_state *pic) { struct pic_proc *proc; int argc, i, len, j; - pic_value *argv, vals; - pic_vec *vec; + pic_value *argv, vec, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); + if (argc == 0) { + pic_errorf(pic, "vector-map: wrong number of arguments (1 for at least 2)"); + } + len = INT_MAX; for (i = 0; i < argc; ++i) { + int l; pic_assert_type(pic, argv[i], vec); - - len = len < pic_vec_ptr(argv[i])->len - ? len - : pic_vec_ptr(argv[i])->len; + l = pic_vec_len(pic, argv[i]); + len = len < l ? len : l; } vec = pic_make_vec(pic, len, NULL); @@ -236,12 +247,12 @@ pic_vec_vector_map(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { - pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); + pic_push(pic, pic_vec_ref(pic, argv[j], i), vals); } - vec->data[i] = pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); + pic_vec_set(pic, vec, i, pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals)); } - return pic_obj_value(vec); + return vec; } static pic_value @@ -253,19 +264,22 @@ pic_vec_vector_for_each(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &argv); + if (argc == 0) { + pic_errorf(pic, "vector-for-each: wrong number of arguments (1 for at least 2)"); + } + len = INT_MAX; for (i = 0; i < argc; ++i) { + int l; pic_assert_type(pic, argv[i], vec); - - len = len < pic_vec_ptr(argv[i])->len - ? len - : pic_vec_ptr(argv[i])->len; + l = pic_vec_len(pic, argv[i]); + len = len < l ? len : l; } for (i = 0; i < len; ++i) { vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { - pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); + pic_push(pic, pic_vec_ref(pic, argv[j], i), vals); } pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); } @@ -276,41 +290,43 @@ pic_vec_vector_for_each(pic_state *pic) static pic_value pic_vec_list_to_vector(pic_state *pic) { - struct pic_vector *vec; - pic_value list, e, it, *data; + pic_value list, vec, e, it; + int len, i = 0; pic_get_args(pic, "o", &list); - vec = pic_make_vec(pic, pic_length(pic, list), NULL); - - data = vec->data; + len = pic_length(pic, list); + vec = pic_make_vec(pic, len, NULL); pic_for_each (e, list, it) { - *data++ = e; + pic_vec_set(pic, vec, i++, e); } - return pic_obj_value(vec); + return vec; } static pic_value pic_vec_vector_to_list(pic_state *pic) { - struct pic_vector *vec; + pic_value vec; pic_value list; - int n, start, end, i; + int n, start, end, i, len; n = pic_get_args(pic, "v|ii", &vec, &start, &end); + len = pic_vec_len(pic, vec); + switch (n) { case 1: start = 0; case 2: - end = vec->len; + end = len; } - list = pic_nil_value(pic); + VALID_RANGE(pic, len, start, end); + list = pic_nil_value(pic); for (i = start; i < end; ++i) { - pic_push(pic, vec->data[i], list); + pic_push(pic, pic_vec_ref(pic, vec, i), list); } return pic_reverse(pic, list); } @@ -318,30 +334,31 @@ pic_vec_vector_to_list(pic_state *pic) static pic_value pic_vec_vector_to_string(pic_state *pic) { - pic_vec *vec; + pic_value vec, t; char *buf; - int n, start, end, i; + int n, start, end, i, len; struct pic_string *str; n = pic_get_args(pic, "v|ii", &vec, &start, &end); + len = pic_vec_len(pic, vec); + switch (n) { case 1: start = 0; case 2: - end = vec->len; + end = len; } - if (end < start) { - pic_errorf(pic, "vector->string: end index must not be less than start index"); - } + VALID_RANGE(pic, len, start, end); buf = pic_malloc(pic, end - start); - for (i = start; i < end; ++i) { - pic_assert_type(pic, vec->data[i], char); + t = pic_vec_ref(pic, vec, i); - buf[i - start] = pic_char(pic, vec->data[i]); + pic_assert_type(pic, t, char); + + buf[i - start] = pic_char(pic, t); } str = pic_str_value(pic, buf, end - start); @@ -355,7 +372,7 @@ pic_vec_string_to_vector(pic_state *pic) { struct pic_string *str; int n, start, end, i; - pic_vec *vec; + pic_value vec; n = pic_get_args(pic, "s|ii", &str, &start, &end); @@ -366,16 +383,14 @@ pic_vec_string_to_vector(pic_state *pic) end = pic_str_len(pic, str); } - if (end < start) { - pic_errorf(pic, "string->vector: end index must not be less than start index"); - } + VALID_RANGE(pic, pic_str_len(pic, str), start, end); vec = pic_make_vec(pic, end - start, NULL); for (i = 0; i < end - start; ++i) { - vec->data[i] = pic_char_value(pic, pic_str_ref(pic, str, i + start)); + pic_vec_set(pic, vec, i, pic_char_value(pic, pic_str_ref(pic, str, i + start))); } - return pic_obj_value(vec); + return vec; } void diff --git a/extlib/benz/write.c b/extlib/benz/write.c index c80b7fc3..a65dc804 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -221,16 +221,16 @@ write_pair(struct writer_control *p, struct pic_pair *pair) } static void -write_vec(struct writer_control *p, pic_vec *vec) +write_vec(struct writer_control *p, pic_value vec) { pic_state *pic = p->pic; xFILE *file = p->file; - int i; + int i, len = pic_vec_len(pic, vec); xfprintf(pic, file, "#("); - for (i = 0; i < vec->len; ++i) { - write_core(p, vec->data[i]); - if (i + 1 < vec->len) { + for (i = 0; i < len; ++i) { + write_core(p, pic_vec_ref(pic, vec, i)); + if (i + 1 < len) { xfprintf(pic, file, " "); } } @@ -315,7 +315,7 @@ write_core(struct writer_control *p, pic_value obj) write_pair(p, pic_pair_ptr(obj)); break; case PIC_TYPE_VECTOR: - write_vec(p, pic_vec_ptr(obj)); + write_vec(p, obj); break; case PIC_TYPE_DICT: write_dict(p, obj); @@ -361,9 +361,9 @@ traverse(struct writer_control *p, pic_value obj) traverse(p, pic_cdr(pic, obj)); } else if (pic_vec_p(pic, obj)) { /* vector */ - int i; - for (i = 0; i < pic_vec_ptr(obj)->len; ++i) { - traverse(p, pic_vec_ptr(obj)->data[i]); + int i, len = pic_vec_len(pic, obj); + for (i = 0; i < len; ++i) { + traverse(p, pic_vec_ref(pic, obj, i)); } } else { /* dictionary */ From 1e08a7f21ae7d861f2410107d0c1753cbded9416 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 22:09:06 +0900 Subject: [PATCH 054/119] struct pic_pair * -> pic_value --- extlib/benz/include/picrin.h | 2 -- extlib/benz/include/picrin/object.h | 2 +- extlib/benz/pair.c | 30 +++++--------------- extlib/benz/read.c | 4 +-- extlib/benz/write.c | 44 ++++++++++++++--------------- 5 files changed, 32 insertions(+), 50 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index b1483dd3..84df7381 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -53,7 +53,6 @@ typedef struct { struct pic_object; struct pic_symbol; -struct pic_pair; struct pic_string; struct pic_blob; struct pic_proc; @@ -64,7 +63,6 @@ struct pic_data; typedef struct pic_symbol pic_sym; typedef struct pic_id pic_id; -typedef struct pic_pair pic_pair; typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 08acd36c..7e71ef38 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -120,11 +120,11 @@ struct pic_port { xFILE *file; }; +#define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) #define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_dict_ptr(pic, v) ((struct pic_dict *)pic_obj_ptr(v)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) -#define pic_pair_ptr(o) ((struct pic_pair *)pic_obj_ptr(o)) #define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) #define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 6507c49e..a58b6cc4 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -20,53 +20,37 @@ pic_cons(pic_state *pic, pic_value car, pic_value cdr) 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; + return pic_pair_ptr(pic, obj)->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; + return pic_pair_ptr(pic, obj)->cdr; } void pic_set_car(pic_state *pic, pic_value obj, pic_value val) { - struct pic_pair *pair; - if (! pic_pair_p(pic, obj)) { pic_errorf(pic, "pair required"); } - pair = pic_pair_ptr(obj); - - pair->car = val; + pic_pair_ptr(pic, obj)->car = val; } void pic_set_cdr(pic_state *pic, pic_value obj, pic_value val) { - struct pic_pair *pair; - if (! pic_pair_p(pic, obj)) { pic_errorf(pic, "pair required"); } - pair = pic_pair_ptr(obj); - - pair->cdr = val; + pic_pair_ptr(pic, obj)->cdr = val; } pic_value @@ -107,7 +91,7 @@ pic_list_p(pic_state *pic, pic_value obj) /* advance rapid fast-forward; runs 2x faster than local */ for (i = 0; i < 2; ++i) { if (pic_pair_p(pic, rapid)) { - rapid = pic_pair_ptr(rapid)->cdr; + rapid = pic_pair_ptr(pic, rapid)->cdr; } else { return pic_nil_p(pic, rapid); @@ -115,7 +99,7 @@ pic_list_p(pic_state *pic, pic_value obj) } /* advance local */ - local = pic_pair_ptr(local)->cdr; + local = pic_pair_ptr(pic, local)->cdr; if (pic_eq_p(pic, local, rapid)) { return false; @@ -169,7 +153,7 @@ pic_list_ref(pic_state *pic, pic_value list, int 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_pair_ptr(pic, pic_list_tail(pic, list, i))->car = obj; } pic_value diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 2a7c99e5..4f26f86f 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -622,8 +622,8 @@ read_label_set(pic_state *pic, xFILE *file, int i) kh_val(h, it) = val = pic_cons(pic, pic_undef_value(pic), pic_undef_value(pic)); tmp = read(pic, file, c); - pic_pair_ptr(val)->car = pic_car(pic, tmp); - pic_pair_ptr(val)->cdr = pic_cdr(pic, tmp); + pic_pair_ptr(pic, val)->car = pic_car(pic, tmp); + pic_pair_ptr(pic, val)->cdr = pic_cdr(pic, tmp); return val; } diff --git a/extlib/benz/write.c b/extlib/benz/write.c index a65dc804..228bc3d9 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -119,7 +119,7 @@ write_float(pic_state *pic, double f, xFILE *file) static void write_core(struct writer_control *p, pic_value); static void -write_pair_help(struct writer_control *p, struct pic_pair *pair) +write_pair_help(struct writer_control *p, pic_value pair) { pic_state *pic = p->pic; khash_t(l) *lh = &p->labels; @@ -127,18 +127,18 @@ write_pair_help(struct writer_control *p, struct pic_pair *pair) khiter_t it; int ret; - write_core(p, pair->car); + write_core(p, pic_car(pic, pair)); - if (pic_nil_p(pic, pair->cdr)) { + if (pic_nil_p(pic, pic_cdr(pic, pair))) { return; } - else if (pic_pair_p(pic, pair->cdr)) { + else if (pic_pair_p(pic, pic_cdr(pic, pair))) { /* shared objects */ - if ((it = kh_get(l, lh, pic_obj_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { + if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) { xfprintf(pic, p->file, " . "); - kh_put(v, vh, pic_obj_ptr(pair->cdr), &ret); + kh_put(v, vh, pic_obj_ptr(pic_cdr(pic, pair)), &ret); if (ret == 0) { /* if exists */ xfprintf(pic, p->file, "#%d#", kh_val(lh, it)); return; @@ -149,11 +149,11 @@ write_pair_help(struct writer_control *p, struct pic_pair *pair) xfprintf(pic, p->file, " "); } - write_pair_help(p, pic_pair_ptr(pair->cdr)); + write_pair_help(p, pic_cdr(pic, pair)); if (p->op == OP_WRITE) { - if ((it = kh_get(l, lh, pic_obj_ptr(pair->cdr))) != kh_end(lh) && kh_val(lh, it) != -1) { - it = kh_get(v, vh, pic_obj_ptr(pair->cdr)); + if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) { + it = kh_get(v, vh, pic_obj_ptr(pic_cdr(pic, pair))); kh_del(v, vh, it); } } @@ -161,57 +161,57 @@ write_pair_help(struct writer_control *p, struct pic_pair *pair) } else { xfprintf(pic, p->file, " . "); - write_core(p, pair->cdr); + write_core(p, pic_cdr(pic, pair)); } } static void -write_pair(struct writer_control *p, struct pic_pair *pair) +write_pair(struct writer_control *p, pic_value pair) { pic_state *pic = p->pic; xFILE *file = p->file; pic_sym *tag; - if (pic_pair_p(pic, pair->cdr) && pic_nil_p(pic, pic_cdr(pic, pair->cdr)) && pic_sym_p(pic, pair->car)) { - tag = pic_sym_ptr(pair->car); + if (pic_pair_p(pic, pic_cdr(pic, pair)) && pic_nil_p(pic, pic_cddr(pic, pair)) && pic_sym_p(pic, pic_car(pic, pair))) { + tag = pic_sym_ptr(pic_car(pic, pair)); if (tag == pic->sQUOTE) { xfprintf(pic, file, "'"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sUNQUOTE) { xfprintf(pic, file, ","); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sUNQUOTE_SPLICING) { xfprintf(pic, file, ",@"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sQUASIQUOTE) { xfprintf(pic, file, "`"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sSYNTAX_QUOTE) { xfprintf(pic, file, "#'"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sSYNTAX_UNQUOTE) { xfprintf(pic, file, "#,"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sSYNTAX_UNQUOTE_SPLICING) { xfprintf(pic, file, "#,@"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } else if (tag == pic->sSYNTAX_QUASIQUOTE) { xfprintf(pic, file, "#`"); - write_core(p, pic_car(pic, pair->cdr)); + write_core(p, pic_cadr(pic, pair)); return; } } @@ -312,7 +312,7 @@ write_core(struct writer_control *p, pic_value obj) write_str(pic, pic_str_ptr(obj), file, p->mode); break; case PIC_TYPE_PAIR: - write_pair(p, pic_pair_ptr(obj)); + write_pair(p, obj); break; case PIC_TYPE_VECTOR: write_vec(p, obj); From 064eaff498962cd8165ab32167a8b3ce004dda9b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 23:22:41 +0900 Subject: [PATCH 055/119] struct pic_blob * -> pic_value --- contrib/40.srfi/src/106.c | 12 +-- extlib/benz/blob.c | 149 +++++++++++++++------------- extlib/benz/bool.c | 15 ++- extlib/benz/include/picrin.h | 15 ++- extlib/benz/include/picrin/compat.h | 12 +++ extlib/benz/include/picrin/object.h | 2 +- extlib/benz/port.c | 66 ++++++------ extlib/benz/proc.c | 4 +- extlib/benz/read.c | 4 +- extlib/benz/write.c | 15 +-- 10 files changed, 155 insertions(+), 139 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index c8efc474..a52d161d 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -185,8 +185,7 @@ pic_socket_socket_accept(pic_state *pic) static pic_value pic_socket_socket_send(pic_state *pic) { - pic_value obj; - struct pic_blob *bv; + pic_value obj, bv; const unsigned char *cursor; int flags = 0, remain, written; struct pic_socket_t *sock; @@ -223,7 +222,6 @@ static pic_value pic_socket_socket_recv(pic_state *pic) { pic_value obj; - struct pic_blob *bv; void *buf; int size; int flags = 0; @@ -239,7 +237,7 @@ pic_socket_socket_recv(pic_state *pic) sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); - buf = malloc(size); + buf = pic_blob(pic, pic_blob_value(pic, NULL, size), NULL); if (buf == NULL && size > 0) { /* XXX: Is it really OK? */ pic_panic(pic, "memory exhausted"); @@ -251,14 +249,10 @@ pic_socket_socket_recv(pic_state *pic) } while (len < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)); if (len < 0) { - free(buf); pic_errorf(pic, "%s", strerror(errno)); } - bv = pic_blob_value(pic, buf, len); - free(buf); - - return pic_obj_value(bv); + return pic_blob_value(pic, buf, len); } static pic_value diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index 6cc1eaac..125c3e74 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -5,7 +5,7 @@ #include "picrin.h" #include "picrin/object.h" -struct pic_blob * +pic_value pic_blob_value(pic_state *pic, const unsigned char *buf, int len) { struct pic_blob *bv; @@ -16,14 +16,16 @@ pic_blob_value(pic_state *pic, const unsigned char *buf, int len) if (buf) { memcpy(bv->data, buf, len); } - return bv; + return pic_obj_value(bv); } unsigned char * -pic_blob(pic_state PIC_UNUSED(*pic), struct pic_blob *blob, int *len) +pic_blob(pic_state PIC_UNUSED(*pic), pic_value blob, int *len) { - *len = blob->len; - return blob->data; + if (len) { + *len = pic_blob_ptr(pic, blob)->len; + } + return pic_blob_ptr(pic, blob)->data; } static pic_value @@ -39,16 +41,15 @@ pic_blob_bytevector_p(pic_state *pic) static pic_value pic_blob_bytevector(pic_state *pic) { - pic_value *argv; + pic_value *argv, blob; int argc, i; - struct pic_blob *blob; unsigned char *data; pic_get_args(pic, "*", &argc, &argv); blob = pic_blob_value(pic, 0, argc); - data = blob->data; + data = pic_blob(pic, blob, NULL); for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], int); @@ -60,91 +61,103 @@ pic_blob_bytevector(pic_state *pic) *data++ = (unsigned char)pic_int(pic, argv[i]); } - return pic_obj_value(blob); + return blob; } static pic_value pic_blob_make_bytevector(pic_state *pic) { - struct pic_blob *blob; - int k, i, b = 0; + pic_value blob; + int k, b = 0; pic_get_args(pic, "i|i", &k, &b); if (b < 0 || b > 255) pic_errorf(pic, "byte out of range"); - blob = pic_blob_value(pic, 0, k); - for (i = 0; i < k; ++i) { - blob->data[i] = (unsigned char)b; + if (k < 0) { + pic_errorf(pic, "make-bytevector: negative length given %d", k); } - return pic_obj_value(blob); + blob = pic_blob_value(pic, 0, k); + + memset(pic_blob(pic, blob, NULL), k, (unsigned char)b); + + return blob; } static pic_value pic_blob_bytevector_length(pic_state *pic) { - struct pic_blob *bv; + pic_value bv; + int len; pic_get_args(pic, "b", &bv); - return pic_int_value(pic, bv->len); + pic_blob(pic, bv, &len); + + return pic_int_value(pic, len); } static pic_value pic_blob_bytevector_u8_ref(pic_state *pic) { - struct pic_blob *bv; - int k; + pic_value bv; + unsigned char *buf; + int k, len; pic_get_args(pic, "bi", &bv, &k); - return pic_int_value(pic, bv->data[k]); + buf = pic_blob(pic, bv, &len); + + VALID_INDEX(pic, len, k); + + return pic_int_value(pic, buf[k]); } static pic_value pic_blob_bytevector_u8_set(pic_state *pic) { - struct pic_blob *bv; - int k, v; + pic_value bv; + unsigned char *buf; + int k, v, len; pic_get_args(pic, "bii", &bv, &k, &v); if (v < 0 || v > 255) pic_errorf(pic, "byte out of range"); - bv->data[k] = (unsigned char)v; + buf = pic_blob(pic, bv, &len); + + VALID_INDEX(pic, len, k); + + buf[k] = (unsigned char)v; + return pic_undef_value(pic); } static pic_value pic_blob_bytevector_copy_i(pic_state *pic) { - struct pic_blob *to, *from; - int n, at, start, end; + pic_value to, from; + unsigned char *tobuf, *frombuf; + int n, at, start, end, tolen, fromlen; n = pic_get_args(pic, "bib|ii", &to, &at, &from, &start, &end); + tobuf = pic_blob(pic, to, &tolen); + frombuf = pic_blob(pic, from, &fromlen); + switch (n) { case 3: start = 0; case 4: - end = from->len; + end = fromlen; } - if (to == from && (start <= at && at < end)) { - /* copy in reversed order */ - at += end - start; - while (start < end) { - to->data[--at] = from->data[--end]; - } - return pic_undef_value(pic); - } + VALID_ATRANGE(pic, tolen, at, fromlen, start, end); - while (start < end) { - to->data[at++] = from->data[start++]; - } + memmove(tobuf + at, frombuf + start, end - start); return pic_undef_value(pic); } @@ -152,62 +165,59 @@ pic_blob_bytevector_copy_i(pic_state *pic) static pic_value pic_blob_bytevector_copy(pic_state *pic) { - struct pic_blob *from, *to; - int n, start, end, i = 0; + pic_value from; + unsigned char *buf; + int n, start, end, len; n = pic_get_args(pic, "b|ii", &from, &start, &end); + buf = pic_blob(pic, from, &len); + switch (n) { case 1: start = 0; case 2: - end = from->len; + end = len; } - if (end < start) { - pic_errorf(pic, "make-bytevector: end index must not be less than start index"); - } + VALID_RANGE(pic, len, start, end); - to = pic_blob_value(pic, 0, end - start); - while (start < end) { - to->data[i++] = from->data[start++]; - } - - return pic_obj_value(to); + return pic_blob_value(pic, buf + start, end - start); } static pic_value pic_blob_bytevector_append(pic_state *pic) { - int argc, i, j, len; - pic_value *argv; - struct pic_blob *blob; + int argc, i, l, len; + unsigned char *buf, *dst; + pic_value *argv, blob; pic_get_args(pic, "*", &argc, &argv); len = 0; for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], blob); - len += pic_blob_ptr(argv[i])->len; + pic_blob(pic, argv[i], &l); + len += l; } - blob = pic_blob_value(pic, 0, len); + blob = pic_blob_value(pic, NULL, len); + dst = pic_blob(pic, blob, NULL); len = 0; for (i = 0; i < argc; ++i) { - for (j = 0; j < pic_blob_ptr(argv[i])->len; ++j) { - blob->data[len + j] = pic_blob_ptr(argv[i])->data[j]; - } - len += pic_blob_ptr(argv[i])->len; + buf = pic_blob(pic, argv[i], &l); + memcpy(dst + len, buf, l); + len += l; } - return pic_obj_value(blob); + return blob; } static pic_value pic_blob_list_to_bytevector(pic_state *pic) { - struct pic_blob *blob; + pic_value blob; unsigned char *data; pic_value list, e, it; @@ -215,7 +225,7 @@ pic_blob_list_to_bytevector(pic_state *pic) blob = pic_blob_value(pic, 0, pic_length(pic, list)); - data = blob->data; + data = pic_blob(pic, blob, NULL); pic_for_each (e, list, it) { pic_assert_type(pic, e, int); @@ -225,29 +235,32 @@ pic_blob_list_to_bytevector(pic_state *pic) *data++ = (unsigned char)pic_int(pic, e); } - return pic_obj_value(blob); + return blob; } static pic_value pic_blob_bytevector_to_list(pic_state *pic) { - struct pic_blob *blob; - pic_value list; - int n, start, end, i; + pic_value blob, list; + unsigned char *buf; + int n, len, start, end, i; n = pic_get_args(pic, "b|ii", &blob, &start, &end); + buf = pic_blob(pic, blob, &len); + switch (n) { case 1: start = 0; case 2: - end = blob->len; + end = len; } - list = pic_nil_value(pic); + VALID_RANGE(pic, len, start, end); + list = pic_nil_value(pic); for (i = start; i < end; ++i) { - pic_push(pic, pic_int_value(pic, blob->data[i]), list); + pic_push(pic, pic_int_value(pic, buf[i]), list); } return pic_reverse(pic, list); } diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index c5d47823..3e474e55 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -109,18 +109,17 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) return pic_str_cmp(pic, pic_str_ptr(x), pic_str_ptr(y)) == 0; } case PIC_TYPE_BLOB: { - struct pic_blob *blob1, *blob2; - int i; + int xlen, ylen; + const unsigned char *xbuf, *ybuf; - blob1 = pic_blob_ptr(x); - blob2 = pic_blob_ptr(y); + xbuf = pic_blob(pic, x, &xlen); + ybuf = pic_blob(pic, y, &ylen); - if (blob1->len != blob2->len) { + if (xlen != ylen) { return false; } - for (i = 0; i < blob1->len; ++i) { - if (blob1->data[i] != blob2->data[i]) - return false; + if (memcmp(xbuf, ybuf, xlen) != 0) { + return false; } return true; } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 84df7381..9e796dc3 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -54,7 +54,6 @@ typedef struct { struct pic_object; struct pic_symbol; struct pic_string; -struct pic_blob; struct pic_proc; struct pic_port; struct pic_error; @@ -120,13 +119,13 @@ pic_value pic_vcall(pic_state *, struct pic_proc *proc, int, va_list); pic_value pic_apply(pic_state *, struct pic_proc *proc, int n, pic_value *argv); pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv); -PIC_INLINE int pic_int(pic_state *, pic_value); -PIC_INLINE double pic_float(pic_state *, pic_value); -PIC_INLINE char pic_char(pic_state *, pic_value); -#define pic_bool(pic,v) (! pic_false_p(pic, v)) +PIC_INLINE int pic_int(pic_state *, pic_value i); +PIC_INLINE double pic_float(pic_state *, pic_value f); +PIC_INLINE char pic_char(pic_state *, pic_value c); +#define pic_bool(pic,b) (! pic_false_p(pic, b)) const char *pic_str(pic_state *, struct pic_string *); -unsigned char *pic_blob(pic_state *, struct pic_blob *, int *len); -void *pic_data(pic_state *, pic_value); +unsigned char *pic_blob(pic_state *, pic_value blob, int *len); +void *pic_data(pic_state *, pic_value str); typedef struct { const char *type_name; @@ -147,7 +146,7 @@ struct pic_string *pic_str_value(pic_state *, const char *str, int len); #define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1)) struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...); struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); -struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len); +pic_value pic_blob_value(pic_state *, const unsigned char *buf, int len); struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); enum { diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h index ba4d964e..e367a6ce 100644 --- a/extlib/benz/include/picrin/compat.h +++ b/extlib/benz/include/picrin/compat.h @@ -221,6 +221,18 @@ memmove(void *dst, const void *src, size_t n) return d; } +PIC_INLINE int +memcmp(const void *b1, const void *b2, size_t n) +{ + const char *s1 = b1, *s2 = b2; + + while (*s1 == *s2 && n-- > 0) { + s1++; + s2++; + } + return (unsigned)*s1 - (unsigned)*s2; +} + PIC_INLINE char * strcpy(char *dst, const char *src) { diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 7e71ef38..ff3cb20d 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -121,11 +121,11 @@ struct pic_port { }; #define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) +#define pic_blob_ptr(pic, v) ((struct pic_blob *)pic_obj_ptr(v)) #define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_dict_ptr(pic, v) ((struct pic_dict *)pic_obj_ptr(v)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) -#define pic_blob_ptr(v) ((struct pic_blob *)pic_obj_ptr(v)) #define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) #define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 136de293..039dbf91 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -125,14 +125,15 @@ pic_port_close_port(pic_state *pic) static pic_value pic_port_open_input_bytevector(pic_state *pic) { - struct pic_blob *blob; - xFILE *file; + pic_value blob; + unsigned char *buf; + int len; pic_get_args(pic, "b", &blob); - file = xfopen_buf(pic, (const char *)blob->data, blob->len, "r"); + buf = pic_blob(pic, blob, &len); - return pic_obj_value(pic_make_port(pic, file)); + return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r"))); } static pic_value @@ -157,7 +158,7 @@ pic_port_get_output_bytevector(pic_state *pic) if (xfget_buf(pic, port->file, &buf, &len) < 0) { pic_errorf(pic, "port was not created by open-output-bytevector"); } - return pic_obj_value(pic_blob_value(pic, (unsigned char *)buf, len)); + return pic_blob_value(pic, (unsigned char *)buf, len); } static pic_value @@ -211,63 +212,51 @@ static pic_value pic_port_read_bytevector(pic_state *pic) { struct pic_port *port = pic_stdin(pic); - struct pic_blob *blob; + unsigned char *buf; int k, i; pic_get_args(pic, "i|p", &k, &port); assert_port_profile(port, X_READ, "read-bytevector"); - blob = pic_blob_value(pic, 0, k); + buf = pic_blob(pic, pic_blob_value(pic, NULL, k), NULL); - i = xfread(pic, blob->data, sizeof(char), k, port->file); + i = xfread(pic, buf, sizeof(char), k, port->file); if (i == 0) { return pic_eof_object(pic); } - else { - pic_realloc(pic, blob->data, i); - blob->len = i; - return pic_obj_value(blob); - } + return pic_blob_value(pic, buf, i); } static pic_value pic_port_read_bytevector_ip(pic_state *pic) { struct pic_port *port; - struct pic_blob *bv; - char *buf; + pic_value bv; + unsigned char *buf; int n, start, end, i, len; n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end); + + buf = pic_blob(pic, bv, &len); + switch (n) { case 1: port = pic_stdin(pic); case 2: start = 0; case 3: - end = bv->len; + end = len; } + VALID_RANGE(pic, len, start, end); assert_port_profile(port, X_READ, "read-bytevector!"); - if (end < start) { - pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index"); - } - - len = end - start; - - buf = pic_calloc(pic, len, sizeof(char)); - i = xfread(pic, buf, sizeof(char), len, port->file); - memcpy(bv->data + start, buf, i); - pic_free(pic, buf); - + i = xfread(pic, buf + start, 1, end - start, port->file); if (i == 0) { return pic_eof_object(pic); } - else { - return pic_int_value(pic, i); - } + return pic_int_value(pic, i); } static pic_value @@ -287,24 +276,31 @@ pic_port_write_u8(pic_state *pic) static pic_value pic_port_write_bytevector(pic_state *pic) { - struct pic_blob *blob; + pic_value blob; struct pic_port *port; - int n, start, end, i; + unsigned char *buf; + int n, start, end, len, done; n = pic_get_args(pic, "b|pii", &blob, &port, &start, &end); + + buf = pic_blob(pic, blob, &len); + switch (n) { case 1: port = pic_stdout(pic); case 2: start = 0; case 3: - end = blob->len; + end = len; } + VALID_RANGE(pic, len, start, end); assert_port_profile(port, X_WRITE, "write-bytevector"); - for (i = start; i < end; ++i) { - xfputc(pic, blob->data[i], port->file); + done = 0; + while (done < end - start) { + done += xfwrite(pic, buf + start + done, 1, end - start - done, port->file); + /* FIXME: error check... */ } return pic_undef_value(pic); } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 712c26db..017d691f 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -23,7 +23,7 @@ * m pic_sym ** symbol * v pic_value * vector object * s struct pic_str ** string object - * b struct pic_blob ** bytevector object + * b pic_value * bytevector object * l struct pic_proc ** lambda object * p struct pic_port ** port object * d pic_value * dictionary object @@ -149,7 +149,6 @@ pic_get_args(pic_state *pic, const char *format, ...) PTR_CASE('m', sym, pic_sym *) PTR_CASE('s', str, struct pic_string *) - PTR_CASE('b', blob, struct pic_blob *) PTR_CASE('l', proc, struct pic_proc *) PTR_CASE('p', port, struct pic_port *) PTR_CASE('e', error, struct pic_error *) @@ -157,6 +156,7 @@ pic_get_args(pic_state *pic, const char *format, ...) #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + OBJ_CASE('b', blob) OBJ_CASE('v', vec) OBJ_CASE('d', dict) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 4f26f86f..3cefa8a3 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -499,7 +499,7 @@ read_blob(pic_state *pic, xFILE *file, int c) int nbits, n; int len; unsigned char *dat; - struct pic_blob *blob; + pic_value blob; nbits = 0; @@ -532,7 +532,7 @@ read_blob(pic_state *pic, xFILE *file, int c) blob = pic_blob_value(pic, dat, len); pic_free(pic, dat); - return pic_obj_value(blob); + return blob; } static pic_value diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 228bc3d9..f5f8ceb6 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -48,14 +48,17 @@ writer_control_destroy(struct writer_control *p) } static void -write_blob(pic_state *pic, struct pic_blob *blob, xFILE *file) +write_blob(pic_state *pic, pic_value blob, xFILE *file) { - int i; + const unsigned char *buf; + int len, i; + + buf = pic_blob(pic, blob, &len); xfprintf(pic, file, "#u8("); - for (i = 0; i < blob->len; ++i) { - xfprintf(pic, file, "%d", blob->data[i]); - if (i + 1 < blob->len) { + for (i = 0; i < len; ++i) { + xfprintf(pic, file, "%d", buf[i]); + if (i + 1 < len) { xfprintf(pic, file, " "); } } @@ -303,7 +306,7 @@ write_core(struct writer_control *p, pic_value obj) xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, pic_sym_ptr(obj)))); break; case PIC_TYPE_BLOB: - write_blob(pic, pic_blob_ptr(obj), file); + write_blob(pic, obj, file); break; case PIC_TYPE_CHAR: write_char(pic, pic_char(pic, obj), file, p->mode); From 0d8a45191aa449db73d52be4ef5b202e59f8c958 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 23:30:47 +0900 Subject: [PATCH 056/119] struct pic_data * -> pic_value --- contrib/10.callcc/callcc.c | 2 +- contrib/30.regexp/src/regexp.c | 2 +- contrib/40.srfi/src/106.c | 4 ++-- docs/capi.rst | 5 +---- extlib/benz/bool.c | 2 +- extlib/benz/cont.c | 4 ++-- extlib/benz/data.c | 12 +++++------- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin.h | 5 ++--- extlib/benz/include/picrin/object.h | 4 ++-- 10 files changed, 18 insertions(+), 24 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 80905d8e..b3d9a96c 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -252,7 +252,7 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) pic_value args[1]; /* save the continuation object in proc */ - c = pic_lambda(pic, cont_call, 1, pic_obj_value(pic_data_value(pic, cont, &cont_type))); + c = pic_lambda(pic, cont_call, 1, pic_data_value(pic, cont, &cont_type)); args[0] = pic_obj_value(c); return pic_applyk(pic, proc, 1, args); diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index dd611975..130f6ff2 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -62,7 +62,7 @@ pic_regexp_regexp(pic_state *pic) pic_errorf(pic, "regexp compilation error: %s", errbuf); } - return pic_obj_value(pic_data_value(pic, reg, ®exp_type)); + return pic_data_value(pic, reg, ®exp_type); } static pic_value diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index a52d161d..b82c0f6f 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -141,7 +141,7 @@ pic_socket_make_socket(pic_state *pic) pic_errorf(pic, "%s", strerror(errno)); } - return pic_obj_value(pic_data_value(pic, sock, &socket_type)); + return pic_data_value(pic, sock, &socket_type); } static pic_value @@ -179,7 +179,7 @@ pic_socket_socket_accept(pic_state *pic) new_sock = pic_malloc(pic, sizeof(struct pic_socket_t)); new_sock->fd = fd; - return pic_obj_value(pic_data_value(pic, new_sock, &socket_type)); + return pic_data_value(pic, new_sock, &socket_type); } static pic_value diff --git a/docs/capi.rst b/docs/capi.rst index 6e701fb9..03b5cc50 100644 --- a/docs/capi.rst +++ b/docs/capi.rst @@ -83,15 +83,12 @@ When you use dynamic memory allocation inside C APIs, you must be caseful about pic_create_foo(pic_state *pic) { struct foo *f; - struct pic_data *dat; pic_get_args(pic, ""); // no args here f = create_foo(); - data = pic_data_value(pic, md, &foo_type); - - return pic_obj_value(data); + return pic_data_value(pic, md, &foo_type); } void diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 3e474e55..826c4626 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -172,7 +172,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) return true; } case PIC_TYPE_DATA: { - return pic_data_ptr(x)->data == pic_data_ptr(y)->data; + return pic_data(pic, x) == pic_data(pic, y); } default: return false; diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index f47347b6..4007b558 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -109,7 +109,7 @@ cont_call(pic_state *pic) pic_errorf(pic, "calling dead escape continuation"); } - cont = pic_data_ptr(pic_closure_ref(pic, CV_ESCAPE))->data; + cont = pic_data(pic, pic_closure_ref(pic, CV_ESCAPE)); cont->retc = argc; cont->retv = argv; @@ -127,7 +127,7 @@ pic_make_cont(pic_state *pic, struct pic_cont *cont) struct pic_proc *c; /* save the escape continuation in proc */ - c = pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_obj_value(pic_data_value(pic, cont, &cont_type))); + c = pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_data_value(pic, cont, &cont_type)); return c; } diff --git a/extlib/benz/data.c b/extlib/benz/data.c index da8b7d6d..ce7c6530 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -4,18 +4,16 @@ bool pic_data_type_p(pic_state *pic, pic_value obj, const pic_data_type *type) { - return pic_data_p(pic, obj) && pic_data_ptr(obj)->type == type; + return pic_data_p(pic, obj) && pic_data_ptr(pic, obj)->type == type; } void * -pic_data(pic_state *pic, pic_value data) +pic_data(pic_state PIC_UNUSED(*pic), pic_value data) { - pic_assert_type(pic, data, data); - - return pic_data_ptr(data)->data; + return pic_data_ptr(pic, data)->data; } -struct pic_data * +pic_value pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type) { struct pic_data *data; @@ -24,5 +22,5 @@ pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type) data->type = type; data->data = userdata; - return data; + return pic_obj_value(data); } diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 2a7a29a2..8baa2239 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -704,7 +704,7 @@ pic_alloca(pic_state *pic, size_t n) static const pic_data_type t = { "pic_alloca", pic_free, 0 }; /* TODO: optimize */ - return pic_data_value(pic, pic_malloc(pic, n), &t)->data; + return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t)); } struct pic_object * diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 9e796dc3..45130110 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -58,7 +58,6 @@ struct pic_proc; struct pic_port; struct pic_error; struct pic_env; -struct pic_data; typedef struct pic_symbol pic_sym; typedef struct pic_id pic_id; @@ -125,7 +124,7 @@ PIC_INLINE char pic_char(pic_state *, pic_value c); #define pic_bool(pic,b) (! pic_false_p(pic, b)) const char *pic_str(pic_state *, struct pic_string *); unsigned char *pic_blob(pic_state *, pic_value blob, int *len); -void *pic_data(pic_state *, pic_value str); +void *pic_data(pic_state *, pic_value data); typedef struct { const char *type_name; @@ -147,7 +146,7 @@ struct pic_string *pic_str_value(pic_state *, const char *str, int len); struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...); struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); pic_value pic_blob_value(pic_state *, const unsigned char *buf, int len); -struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); +pic_value pic_data_value(pic_state *, void *ptr, const pic_data_type *type); enum { PIC_TYPE_INVALID = 1, diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index ff3cb20d..1c1ad585 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -123,12 +123,12 @@ struct pic_port { #define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) #define pic_blob_ptr(pic, v) ((struct pic_blob *)pic_obj_ptr(v)) #define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) -#define pic_dict_ptr(pic, v) ((struct pic_dict *)pic_obj_ptr(v)) +#define pic_dict_ptr(pic, o) ((struct pic_dict *)pic_obj_ptr(o)) +#define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) #define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) -#define pic_data_ptr(o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) From 5254e80932adadfc024fc24178e6b13a268ea467 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 00:03:16 +0900 Subject: [PATCH 057/119] struct pic_proc * -> pic_value --- contrib/10.callcc/callcc.c | 13 +++--- contrib/40.srfi/src/106.c | 5 +-- extlib/benz/cont.c | 40 +++++++----------- extlib/benz/debug.c | 6 +-- extlib/benz/dict.c | 6 +-- extlib/benz/error.c | 23 ++++------ extlib/benz/eval.c | 4 +- extlib/benz/include/picrin.h | 25 ++++++----- extlib/benz/include/picrin/cont.h | 2 +- extlib/benz/include/picrin/object.h | 6 +-- extlib/benz/macro.c | 22 +++++----- extlib/benz/pair.c | 16 +++---- extlib/benz/port.c | 2 +- extlib/benz/proc.c | 65 +++++++++++++---------------- extlib/benz/string.c | 10 ++--- extlib/benz/var.c | 35 +++++++--------- extlib/benz/vector.c | 10 ++--- extlib/benz/weak.c | 9 +--- 18 files changed, 128 insertions(+), 171 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index b3d9a96c..bacfee60 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -239,7 +239,7 @@ cont_call(pic_state *pic) } static pic_value -pic_callcc(pic_state *pic, struct pic_proc *proc) +pic_callcc(pic_state *pic, pic_value proc) { struct pic_fullcont *cont; @@ -248,13 +248,12 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) return pic_valuesk(pic, cont->retc, cont->retv); } else { - struct pic_proc *c; - pic_value args[1]; + pic_value c, args[1]; /* save the continuation object in proc */ c = pic_lambda(pic, cont_call, 1, pic_data_value(pic, cont, &cont_type)); - args[0] = pic_obj_value(c); + args[0] = c; return pic_applyk(pic, proc, 1, args); } } @@ -262,15 +261,15 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) static pic_value pic_callcc_callcc(pic_state *pic) { - struct pic_proc *proc; + pic_value proc; pic_get_args(pic, "l", &proc); return pic_callcc(pic, proc); } -#define pic_redefun(pic, lib, name, func) \ - pic_set(pic, lib, name, pic_obj_value(pic_lambda(pic, func, 0))) +#define pic_redefun(pic, lib, name, func) \ + pic_set(pic, lib, name, pic_lambda(pic, func, 0)) void pic_init_callcc(pic_state *pic) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index b82c0f6f..f673c088 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -367,8 +367,7 @@ pic_socket_socket_output_port(pic_state *pic) static pic_value pic_socket_call_with_socket(pic_state *pic) { - pic_value obj, result; - struct pic_proc *proc; + pic_value obj, proc, result; struct pic_socket_t *sock; pic_get_args(pic, "ol", &obj, &proc); @@ -389,7 +388,7 @@ pic_init_srfi_106(pic_state *pic) { pic_deflibrary(pic, "srfi.106"); -#define pic_defun_(pic, name, f) pic_define(pic, "srfi.106", name, pic_obj_value(pic_lambda(pic, f, 0))) +#define pic_defun_(pic, name, f) pic_define(pic, "srfi.106", name, pic_lambda(pic, f, 0)) #define pic_define_(pic, name, v) pic_define(pic, "srfi.106", name, v) pic_defun_(pic, "socket?", pic_socket_socket_p); diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 4007b558..755b02e7 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -48,38 +48,34 @@ pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) if (here->depth < there->depth) { pic_wind(pic, here, there->prev); - pic_call(pic, there->in, 0); + pic_call(pic, pic_obj_value(there->in), 0); } else { - pic_call(pic, there->out, 0); + pic_call(pic, pic_obj_value(there->out), 0); pic_wind(pic, here->prev, there); } } static pic_value -pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, struct pic_proc *out) +pic_dynamic_wind(pic_state *pic, pic_value in, pic_value thunk, pic_value out) { pic_checkpoint *here; pic_value val; - if (in != NULL) { - pic_call(pic, in, 0); /* enter */ - } + pic_call(pic, in, 0); /* enter */ here = pic->cp; pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TYPE_CP); pic->cp->prev = here; pic->cp->depth = here->depth + 1; - pic->cp->in = in; - pic->cp->out = out; + pic->cp->in = pic_proc_ptr(pic, in); + pic->cp->out = pic_proc_ptr(pic, out); val = pic_call(pic, thunk, 0); pic->cp = here; - if (out != NULL) { - pic_call(pic, out, 0); /* exit */ - } + pic_call(pic, out, 0); /* exit */ return val; } @@ -120,20 +116,17 @@ cont_call(pic_state *pic) PIC_UNREACHABLE(); } -struct pic_proc * +pic_value pic_make_cont(pic_state *pic, struct pic_cont *cont) { static const pic_data_type cont_type = { "cont", NULL, NULL }; - struct pic_proc *c; /* save the escape continuation in proc */ - c = pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_data_value(pic, cont, &cont_type)); - - return c; + return pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_data_value(pic, cont, &cont_type)); } static pic_value -pic_callcc(pic_state *pic, struct pic_proc *proc) +pic_callcc(pic_state *pic, pic_value proc) { struct pic_cont cont; @@ -145,7 +138,7 @@ pic_callcc(pic_state *pic, struct pic_proc *proc) else { pic_value val; - val = pic_call(pic, proc, 1, pic_obj_value(pic_make_cont(pic, &cont))); + val = pic_call(pic, proc, 1, pic_make_cont(pic, &cont)); pic->cc = pic->cc->prev; @@ -209,17 +202,17 @@ pic_receive(pic_state *pic, int n, pic_value *argv) static pic_value pic_cont_callcc(pic_state *pic) { - struct pic_proc *cb; + pic_value f; - pic_get_args(pic, "l", &cb); + pic_get_args(pic, "l", &f); - return pic_callcc(pic, cb); + return pic_callcc(pic, f); } static pic_value pic_cont_dynamic_wind(pic_state *pic) { - struct pic_proc *in, *thunk, *out; + pic_value in, thunk, out; pic_get_args(pic, "lll", &in, &thunk, &out); @@ -240,9 +233,8 @@ pic_cont_values(pic_state *pic) static pic_value pic_cont_call_with_values(pic_state *pic) { - struct pic_proc *producer, *consumer; + pic_value producer, consumer, *retv; int retc; - pic_value *retv; pic_get_args(pic, "ll", &producer, &consumer); diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index b8e5e215..c354c89f 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -15,14 +15,14 @@ pic_get_backtrace(pic_state *pic) trace = pic_lit_value(pic, ""); for (ci = pic->ci; ci != pic->cibase; --ci) { - struct pic_proc *proc = pic_proc_ptr(ci->fp[0]); + pic_value proc = ci->fp[0]; trace = pic_str_cat(pic, trace, pic_lit_value(pic, " at ")); trace = pic_str_cat(pic, trace, pic_lit_value(pic, "(anonymous lambda)")); - if (pic_proc_func_p(proc)) { + if (pic_proc_func_p(pic_proc_ptr(pic, proc))) { trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (native function)\n")); - } else if (pic_proc_irep_p(proc)) { + } else { trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (unknown location)\n")); /* TODO */ } } diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 88c67e4c..301d0c4b 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -168,8 +168,7 @@ pic_dict_dictionary_size(pic_state *pic) static pic_value pic_dict_dictionary_map(pic_state *pic) { - struct pic_proc *proc; - pic_value dict, ret = pic_nil_value(pic); + pic_value dict, proc, ret = pic_nil_value(pic); pic_sym *key; int it = 0; @@ -184,8 +183,7 @@ pic_dict_dictionary_map(pic_state *pic) static pic_value pic_dict_dictionary_for_each(pic_state *pic) { - struct pic_proc *proc; - pic_value dict; + pic_value dict, proc; pic_sym *key; int it; diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 2b203ed1..0686ff9e 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -51,21 +51,18 @@ pic_value pic_native_exception_handler(pic_state *pic) { pic_value err; - struct pic_proc *self, *cont; - pic_get_args(pic, "&o", &self, &err); + pic_get_args(pic, "o", &err); pic->err = err; - cont = pic_proc_ptr(pic_closure_ref(pic, 0)); - - pic_call(pic, cont, 1, pic_false_value(pic)); + pic_call(pic, pic_closure_ref(pic, 0), 1, pic_false_value(pic)); PIC_UNREACHABLE(); } void -pic_push_handler(pic_state *pic, struct pic_proc *handler) +pic_push_handler(pic_state *pic, pic_value handler) { size_t xp_len; ptrdiff_t xp_offset; @@ -78,17 +75,17 @@ pic_push_handler(pic_state *pic, struct pic_proc *handler) pic->xpend = pic->xpbase + xp_len; } - *pic->xp++ = handler; + *pic->xp++ = pic_proc_ptr(pic, handler); } -struct pic_proc * +pic_value pic_pop_handler(pic_state *pic) { if (pic->xp == pic->xpbase) { pic_panic(pic, "no exception handler registered"); } - return *--pic->xp; + return pic_obj_value(*--pic->xp); } struct pic_error * @@ -112,12 +109,11 @@ pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs pic_value pic_raise_continuable(pic_state *pic, pic_value err) { - struct pic_proc *handler; - pic_value v; + pic_value handler, v; handler = pic_pop_handler(pic); - pic_protect(pic, pic_obj_value(handler)); + pic_protect(pic, handler); v = pic_call(pic, handler, 1, err); @@ -151,8 +147,7 @@ pic_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) static pic_value pic_error_with_exception_handler(pic_state *pic) { - struct pic_proc *handler, *thunk; - pic_value val; + pic_value handler, thunk, val; pic_get_args(pic, "ll", &handler, &thunk); diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 64160c7f..eaf6a72a 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -831,11 +831,11 @@ pic_codegen(pic_state *pic, pic_value obj) #define SAVE(pic, ai, obj) pic_leave(pic, ai); pic_protect(pic, obj) -struct pic_proc * +pic_value pic_compile(pic_state *pic, pic_value obj) { struct pic_irep *irep; - struct pic_proc *proc; + pic_value proc; size_t ai = pic_enter(pic); #if DEBUG diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 45130110..d86e461c 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -54,7 +54,6 @@ typedef struct { struct pic_object; struct pic_symbol; struct pic_string; -struct pic_proc; struct pic_port; struct pic_error; struct pic_env; @@ -85,7 +84,7 @@ void pic_gc(pic_state *); void pic_add_feature(pic_state *, const char *feature); void pic_defun(pic_state *, const char *name, pic_func_t f); -void pic_defvar(pic_state *, const char *name, pic_value v, struct pic_proc *conv); +void pic_defvar(pic_state *, const char *name, pic_value v, pic_value conv); void pic_define(pic_state *, const char *lib, const char *name, pic_value v); pic_value pic_ref(pic_state *, const char *lib, const char *name); @@ -111,12 +110,12 @@ PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); PIC_NORETURN void pic_raise(pic_state *, pic_value v); -struct pic_proc *pic_lambda(pic_state *, pic_func_t f, int n, ...); -struct pic_proc *pic_vlambda(pic_state *, pic_func_t f, int n, va_list); -pic_value pic_call(pic_state *, struct pic_proc *proc, int, ...); -pic_value pic_vcall(pic_state *, struct pic_proc *proc, int, va_list); -pic_value pic_apply(pic_state *, struct pic_proc *proc, int n, pic_value *argv); -pic_value pic_applyk(pic_state *, struct pic_proc *proc, int n, pic_value *argv); +pic_value pic_lambda(pic_state *, pic_func_t f, int n, ...); +pic_value pic_vlambda(pic_state *, pic_func_t f, int n, va_list); +pic_value pic_call(pic_state *, pic_value proc, int, ...); +pic_value pic_vcall(pic_state *, pic_value proc, int, va_list); +pic_value pic_apply(pic_state *, pic_value proc, int n, pic_value *argv); +pic_value pic_applyk(pic_state *, pic_value proc, int n, pic_value *argv); PIC_INLINE int pic_int(pic_state *, pic_value i); PIC_INLINE double pic_float(pic_state *, pic_value f); @@ -301,7 +300,7 @@ pic_value pic_eval(pic_state *, pic_value, const char *); void pic_load(pic_state *, struct pic_port *); void pic_load_cstr(pic_state *, const char *); -struct pic_proc *pic_make_var(pic_state *, pic_value, struct pic_proc *); +pic_value pic_make_var(pic_state *, pic_value init, pic_value conv); bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); @@ -320,14 +319,14 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); pic_catch_(PIC_GENSYM(label)) #define pic_try_(cont, handler) \ do { \ - extern void pic_push_handler(pic_state *, struct pic_proc *); \ - extern struct pic_proc *pic_pop_handler(pic_state *); \ + extern void pic_push_handler(pic_state *, pic_value proc); \ + extern pic_value pic_pop_handler(pic_state *); \ extern pic_value pic_native_exception_handler(pic_state *); \ struct pic_cont cont; \ pic_save_point(pic, &cont); \ if (PIC_SETJMP(pic, cont.jmp) == 0) { \ - struct pic_proc *handler; \ - handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_obj_value(pic_make_cont(pic, &cont))); \ + pic_value handler; \ + handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_make_cont(pic, &cont)); \ do { \ pic_push_handler(pic, handler); #define pic_catch_(label) \ diff --git a/extlib/benz/include/picrin/cont.h b/extlib/benz/include/picrin/cont.h index 4e6649bf..46742b0c 100644 --- a/extlib/benz/include/picrin/cont.h +++ b/extlib/benz/include/picrin/cont.h @@ -31,7 +31,7 @@ struct pic_cont { void pic_save_point(pic_state *, struct pic_cont *); void pic_load_point(pic_state *, struct pic_cont *); -struct pic_proc *pic_make_cont(pic_state *, struct pic_cont *); +pic_value pic_make_cont(pic_state *, struct pic_cont *); void pic_wind(pic_state *, pic_checkpoint *, pic_checkpoint *); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 1c1ad585..8970c77f 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -125,12 +125,12 @@ struct pic_port { #define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_dict_ptr(pic, o) ((struct pic_dict *)pic_obj_ptr(o)) #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) +#define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) #define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) -#define pic_proc_ptr(o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) #define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) @@ -157,8 +157,8 @@ struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); } while (0) pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); -struct pic_proc *pic_make_proc(pic_state *, pic_func_t, int, pic_value *); -struct pic_proc *pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); +pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); +pic_value pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); struct pic_env *pic_make_env(pic_state *, struct pic_env *); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 34266ed1..9bb59afc 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -107,21 +107,21 @@ pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) static void -define_macro(pic_state *pic, pic_sym *uid, struct pic_proc *mac) +define_macro(pic_state *pic, pic_sym *uid, pic_value mac) { if (pic_weak_has(pic, pic->macros, uid)) { pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(uid)); } - pic_weak_set(pic, pic->macros, uid, pic_obj_value(mac)); + pic_weak_set(pic, pic->macros, uid, mac); } -static struct pic_proc * +static pic_value find_macro(pic_state *pic, pic_sym *uid) { if (! pic_weak_has(pic, pic->macros, uid)) { - return NULL; + return pic_false_value(pic); } - return pic_proc_ptr(pic_weak_ref(pic, pic->macros, uid)); + return pic_weak_ref(pic, pic->macros, uid); } static void @@ -138,12 +138,12 @@ static pic_value expand_lambda(pic_state *, pic_value, struct pic_env *); static pic_value expand_var(pic_state *pic, pic_id *id, struct pic_env *env, pic_value deferred) { - struct pic_proc *mac; + pic_value mac; pic_sym *functor; functor = pic_find_identifier(pic, id, env); - if ((mac = find_macro(pic, functor)) != NULL) { + if (! pic_false_p(pic, mac = find_macro(pic, functor))) { return expand(pic, pic_call(pic, mac, 2, pic_obj_value(id), pic_obj_value(env)), env, deferred); } return pic_obj_value(functor); @@ -250,7 +250,7 @@ expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value def static pic_value expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) { - struct pic_proc *pic_compile(pic_state *, pic_value); + pic_value pic_compile(pic_state *, pic_value); pic_id *id; pic_value val; pic_sym *uid; @@ -265,7 +265,7 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) pic_errorf(pic, "macro definition \"%s\" evaluates to non-procedure object", pic_str(pic, pic_id_name(pic, id))); } - define_macro(pic, uid, pic_proc_ptr(val)); + define_macro(pic, uid, val); return pic_undef_value(pic); } @@ -279,7 +279,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer return expand_var(pic, pic_id_ptr(expr), env, deferred); } case PIC_TYPE_PAIR: { - struct pic_proc *mac; + pic_value mac; if (! pic_list_p(pic, expr)) { pic_errorf(pic, "cannot expand improper list: ~s", expr); @@ -303,7 +303,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer return expand_quote(pic, expr); } - if ((mac = find_macro(pic, functor)) != NULL) { + if (! pic_false_p(pic, mac = find_macro(pic, functor))) { return expand(pic, pic_call(pic, mac, 2, expr, pic_obj_value(env)), env, deferred); } } diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index a58b6cc4..ff12994a 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -470,9 +470,8 @@ pic_pair_list_copy(pic_state *pic) static pic_value pic_pair_map(pic_state *pic) { - struct pic_proc *proc; int argc, i; - pic_value *args, *arg_list, ret; + pic_value proc, *args, *arg_list, ret; pic_get_args(pic, "l*", &proc, &argc, &args); @@ -503,9 +502,8 @@ pic_pair_map(pic_state *pic) static pic_value pic_pair_for_each(pic_state *pic) { - struct pic_proc *proc; int argc, i; - pic_value *args, *arg_list; + pic_value proc, *args, *arg_list; pic_get_args(pic, "l*", &proc, &argc, &args); @@ -563,13 +561,12 @@ pic_pair_memv(pic_state *pic) static pic_value pic_pair_member(pic_state *pic) { - struct pic_proc *proc = NULL; - pic_value key, list; + pic_value key, list, proc = pic_false_value(pic); pic_get_args(pic, "oo|l", &key, &list, &proc); while (! pic_nil_p(pic, list)) { - if (proc == NULL) { + if (pic_false_p(pic, proc)) { if (pic_equal_p(pic, key, pic_car(pic, list))) return list; } else { @@ -618,14 +615,13 @@ pic_pair_assv(pic_state *pic) static pic_value pic_pair_assoc(pic_state *pic) { - struct pic_proc *proc = NULL; - pic_value key, alist, cell; + pic_value key, alist, proc = pic_false_value(pic), cell; pic_get_args(pic, "oo|l", &key, &alist, &proc); while (! pic_nil_p(pic, alist)) { cell = pic_car(pic, alist); - if (proc == NULL) { + if (pic_false_p(pic, proc)) { if (pic_equal_p(pic, key, pic_car(pic, cell))) return cell; } else { diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 039dbf91..2e94e9f9 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -334,7 +334,7 @@ coerce_port(pic_state *pic) void pic_init_port(pic_state *pic) { - struct pic_proc *coerce = pic_lambda(pic, coerce_port, 0); + pic_value coerce = pic_lambda(pic, coerce_port, 0); DEFINE_PORT(pic, "current-input-port", xstdin); DEFINE_PORT(pic, "current-output-port", xstdout); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 017d691f..0aade676 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -24,7 +24,7 @@ * v pic_value * vector object * s struct pic_str ** string object * b pic_value * bytevector object - * l struct pic_proc ** lambda object + * l pic_value * lambda object * p struct pic_port ** port object * d pic_value * dictionary object * e struct pic_error ** error object @@ -76,10 +76,10 @@ pic_get_args(pic_state *pic, const char *format, ...) /* dispatch */ if (proc) { - struct pic_proc **proc; + pic_value *proc; - proc = va_arg(ap, struct pic_proc **); - *proc = pic_proc_ptr(GET_OPERAND(pic, 0)); + proc = va_arg(ap, pic_value *); + *proc = GET_OPERAND(pic, 0); } for (i = 1; i <= MIN(paramc + optc, argc); ++i) { @@ -149,13 +149,13 @@ pic_get_args(pic_state *pic, const char *format, ...) PTR_CASE('m', sym, pic_sym *) PTR_CASE('s', str, struct pic_string *) - PTR_CASE('l', proc, struct pic_proc *) PTR_CASE('p', port, struct pic_port *) PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + OBJ_CASE('l', proc) OBJ_CASE('b', blob) OBJ_CASE('v', vec) OBJ_CASE('d', dict) @@ -337,7 +337,7 @@ bool pic_gt(pic_state *, pic_value, pic_value); bool pic_ge(pic_state *, pic_value, pic_value); pic_value -pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) +pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) { pic_code c; size_t ai = pic_enter(pic); @@ -363,7 +363,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) pic_callinfo *cibase; #endif - PUSH(pic_obj_value(proc)); + PUSH(proc); for (i = 0; i < argc; ++i) { PUSH(argv[i]); @@ -498,6 +498,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) CASE(OP_CALL) { pic_value x, v; pic_callinfo *ci; + struct pic_proc *proc; if (c.a == -1) { pic->sp += pic->ci[1].retc - 1; @@ -509,7 +510,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) if (! pic_proc_p(pic, x)) { pic_errorf(pic, "invalid application: ~s", x); } - proc = pic_proc_ptr(x); + proc = pic_proc_ptr(pic, x); VM_CALL_PRINT; @@ -632,8 +633,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) vm_push_cxt(pic); } - proc = pic_make_proc_irep(pic, pic->ci->irep->irep[c.a], pic->ci->cxt); - PUSH(pic_obj_value(proc)); + PUSH(pic_make_proc_irep(pic, pic->ci->irep->irep[c.a], pic->ci->cxt)); pic_leave(pic, ai); NEXT; } @@ -794,7 +794,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv) } pic_value -pic_applyk(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args) +pic_applyk(pic_state *pic, pic_value proc, int argc, pic_value *args) { pic_value *sp; pic_callinfo *ci; @@ -803,7 +803,7 @@ pic_applyk(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args) PIC_INIT_CODE_I(pic->iseq[0], OP_NOP, 0); PIC_INIT_CODE_I(pic->iseq[1], OP_TAILCALL, -1); - *pic->sp++ = pic_obj_value(proc); + *pic->sp++ = proc; sp = pic->sp; for (i = 0; i < argc; ++i) { @@ -823,7 +823,7 @@ pic_applyk(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args) } pic_value -pic_call(pic_state *pic, struct pic_proc *proc, int n, ...) +pic_call(pic_state *pic, pic_value proc, int n, ...) { pic_value r; va_list ap; @@ -835,7 +835,7 @@ pic_call(pic_state *pic, struct pic_proc *proc, int n, ...) } pic_value -pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap) +pic_vcall(pic_state *pic, pic_value proc, int n, va_list ap) { pic_value *args = pic_alloca(pic, sizeof(pic_value) * n); int i; @@ -846,10 +846,10 @@ pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap) return pic_apply(pic, proc, n, args); } -struct pic_proc * +pic_value pic_lambda(pic_state *pic, pic_func_t f, int n, ...) { - struct pic_proc *proc; + pic_value proc; va_list ap; va_start(ap, n); @@ -858,7 +858,7 @@ pic_lambda(pic_state *pic, pic_func_t f, int n, ...) return proc; } -struct pic_proc * +pic_value pic_vlambda(pic_state *pic, pic_func_t f, int n, va_list ap) { pic_value *env = pic_alloca(pic, sizeof(pic_value) * n); @@ -873,14 +873,14 @@ pic_vlambda(pic_state *pic, pic_func_t f, int n, va_list ap) void pic_defun(pic_state *pic, const char *name, pic_func_t f) { - pic_define(pic, pic_current_library(pic), name, pic_obj_value(pic_make_proc(pic, f, 0, NULL))); + pic_define(pic, pic_current_library(pic), name, pic_make_proc(pic, f, 0, NULL)); pic_export(pic, pic_intern_cstr(pic, name)); } void -pic_defvar(pic_state *pic, const char *name, pic_value init, struct pic_proc *conv) +pic_defvar(pic_state *pic, const char *name, pic_value init, pic_value conv) { - pic_define(pic, pic_current_library(pic), name, pic_obj_value(pic_make_var(pic, init, conv))); + pic_define(pic, pic_current_library(pic), name, pic_make_var(pic, init, conv)); pic_export(pic, pic_intern_cstr(pic, name)); } @@ -939,31 +939,27 @@ pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) pic_value pic_closure_ref(pic_state *pic, int n) { - struct pic_proc *self; - - self = pic_proc_ptr(GET_OPERAND(pic, 0)); + struct pic_proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); assert(pic_proc_func_p(self)); if (n < 0 || self->u.f.localc <= n) { pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); } - return pic_proc_ptr(GET_OPERAND(pic, 0))->locals[n]; + return self->locals[n]; } void pic_closure_set(pic_state *pic, int n, pic_value v) { - struct pic_proc *self; - - self = pic_proc_ptr(GET_OPERAND(pic, 0)); + struct pic_proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); assert(pic_proc_func_p(self)); if (n < 0 || self->u.f.localc <= n) { pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); } - pic_proc_ptr(GET_OPERAND(pic, 0))->locals[n] = v; + self->locals[n] = v; } pic_value @@ -977,7 +973,7 @@ pic_funcall(pic_state *pic, const char *lib, const char *name, int n, ...) pic_assert_type(pic, proc, proc); va_start(ap, n); - r = pic_vcall(pic, pic_proc_ptr(proc), n, ap); + r = pic_vcall(pic, proc, n, ap); va_end(ap); return r; @@ -1012,7 +1008,7 @@ pic_irep_decref(pic_state *pic, struct pic_irep *irep) } } -struct pic_proc * +pic_value pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) { struct pic_proc *proc; @@ -1025,10 +1021,10 @@ pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) for (i = 0; i < n; ++i) { proc->locals[i] = env[i]; } - return proc; + return pic_obj_value(proc); } -struct pic_proc * +pic_value pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cxt) { struct pic_proc *proc; @@ -1038,7 +1034,7 @@ pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cx proc->u.i.irep = irep; proc->u.i.cxt = cxt; pic_irep_incref(pic, irep); - return proc; + return pic_obj_value(proc); } static pic_value @@ -1054,8 +1050,7 @@ pic_proc_proc_p(pic_state *pic) static pic_value pic_proc_apply(pic_state *pic) { - struct pic_proc *proc; - pic_value *args, *arg_list; + pic_value proc, *args, *arg_list; int argc, n, i; pic_get_args(pic, "l*", &proc, &argc, &args); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index d449953b..28d18426 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -552,8 +552,7 @@ pic_str_string_append(pic_state *pic) static pic_value pic_str_string_map(pic_state *pic) { - struct pic_proc *proc; - pic_value *argv, vals, val; + pic_value proc, *argv, vals, val; int argc, i, len, j; struct pic_string *str; char *buf; @@ -581,7 +580,7 @@ pic_str_string_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic, pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } - val = pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); + val = pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); pic_assert_type(pic, val, char); buf[i] = pic_char(pic, val); @@ -601,9 +600,8 @@ pic_str_string_map(pic_state *pic) static pic_value pic_str_string_for_each(pic_state *pic) { - struct pic_proc *proc; int argc, len, i, j; - pic_value *argv, vals; + pic_value proc, *argv, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); @@ -626,7 +624,7 @@ pic_str_string_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic, pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } - pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); + pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); } return pic_undef_value(pic); diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 5b132a06..04a07e5e 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -6,28 +6,28 @@ #include "picrin/object.h" static pic_value -var_get(pic_state *pic, struct pic_proc *var) +var_get(pic_state *pic, pic_value var) { pic_value elem, it; struct pic_weak *weak; pic_for_each (elem, pic->ptable, it) { weak = pic_weak_ptr(elem); - if (pic_weak_has(pic, weak, var)) { - return pic_weak_ref(pic, weak, var); + if (pic_weak_has(pic, weak, pic_obj_ptr(var))) { + return pic_weak_ref(pic, weak, pic_obj_ptr(var)); } } pic_panic(pic, "logic flaw"); } static pic_value -var_set(pic_state *pic, struct pic_proc *var, pic_value val) +var_set(pic_state *pic, pic_value var, pic_value val) { struct pic_weak *weak; weak = pic_weak_ptr(pic_car(pic, pic->ptable)); - pic_weak_set(pic, weak, var, val); + pic_weak_set(pic, weak, pic_obj_ptr(var), val); return pic_undef_value(pic); } @@ -35,8 +35,7 @@ var_set(pic_state *pic, struct pic_proc *var, pic_value val) static pic_value var_call(pic_state *pic) { - struct pic_proc *self; - pic_value val; + pic_value self, val; int n; n = pic_get_args(pic, "&|o", &self, &val); @@ -48,22 +47,18 @@ var_call(pic_state *pic) conv = pic_closure_ref(pic, 0); if (! pic_false_p(pic, conv)) { - val = pic_call(pic, pic_proc_ptr(conv), 1, val); + val = pic_call(pic, conv, 1, val); } return var_set(pic, self, val); } } -struct pic_proc * -pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv) +pic_value +pic_make_var(pic_state *pic, pic_value init, pic_value conv) { - struct pic_proc *var; - pic_value c = pic_false_value(pic); + pic_value var; - if (conv != NULL) { - c = pic_obj_value(conv); - } - var = pic_lambda(pic, var_call, 1, c); + var = pic_lambda(pic, var_call, 1, conv); pic_call(pic, var, 1, init); @@ -73,19 +68,17 @@ pic_make_var(pic_state *pic, pic_value init, struct pic_proc *conv) static pic_value pic_var_make_parameter(pic_state *pic) { - struct pic_proc *conv = NULL; - pic_value init; + pic_value init, conv = pic_false_value(pic); pic_get_args(pic, "o|l", &init, &conv); - return pic_obj_value(pic_make_var(pic, init, conv)); + return pic_make_var(pic, init, conv); } static pic_value pic_var_with_parameter(pic_state *pic) { - struct pic_proc *body; - pic_value val; + pic_value body, val; pic_get_args(pic, "l", &body); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index da75cd9c..8dce36f3 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -224,9 +224,8 @@ pic_vec_vector_fill_i(pic_state *pic) static pic_value pic_vec_vector_map(pic_state *pic) { - struct pic_proc *proc; int argc, i, len, j; - pic_value *argv, vec, vals; + pic_value proc, *argv, vec, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); @@ -249,7 +248,7 @@ pic_vec_vector_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ref(pic, argv[j], i), vals); } - pic_vec_set(pic, vec, i, pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals)); + pic_vec_set(pic, vec, i, pic_funcall(pic, "picrin.base", "apply", 2, proc, vals)); } return vec; @@ -258,9 +257,8 @@ pic_vec_vector_map(pic_state *pic) static pic_value pic_vec_vector_for_each(pic_state *pic) { - struct pic_proc *proc; int argc, i, len, j; - pic_value *argv, vals; + pic_value proc, *argv, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); @@ -281,7 +279,7 @@ pic_vec_vector_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ref(pic, argv[j], i), vals); } - pic_funcall(pic, "picrin.base", "apply", 2, pic_obj_value(proc), vals); + pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); } return pic_undef_value(pic); diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 86821dae..69effdcc 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -105,12 +105,11 @@ weak_set(pic_state *pic, struct pic_weak *weak, void *key, pic_value val) static pic_value weak_call(pic_state *pic) { - struct pic_proc *self; struct pic_weak *weak; pic_value key, val; int n; - n = pic_get_args(pic, "&o|o", &self, &key, &val); + n = pic_get_args(pic, "o|o", &key, &val); if (! pic_obj_p(pic, key)) { pic_errorf(pic, "attempted to set a non-object key '~s' in an ephemeron", key); @@ -128,13 +127,9 @@ weak_call(pic_state *pic) static pic_value pic_weak_make_ephemeron(pic_state *pic) { - struct pic_proc *proc; - pic_get_args(pic, ""); - proc = pic_lambda(pic, weak_call, 1, pic_obj_value(pic_make_weak(pic))); - - return pic_obj_value(proc); + return pic_lambda(pic, weak_call, 1, pic_obj_value(pic_make_weak(pic))); } void From 2d5fbc889ed88ec33caa744e9ec11575d4651920 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 03:26:52 +0900 Subject: [PATCH 058/119] struct pic_string * -> pic_value --- contrib/20.r7rs/src/mutable-string.c | 85 +++++----- contrib/20.r7rs/src/system.c | 12 +- contrib/30.readline/src/readline.c | 18 +- contrib/30.regexp/src/regexp.c | 17 +- extlib/benz/blob.c | 2 +- extlib/benz/bool.c | 2 +- extlib/benz/debug.c | 8 +- extlib/benz/error.c | 10 +- extlib/benz/include/picrin.h | 27 ++- extlib/benz/include/picrin/object.h | 6 +- extlib/benz/lib.c | 11 +- extlib/benz/macro.c | 4 +- extlib/benz/number.c | 11 +- extlib/benz/proc.c | 6 +- extlib/benz/read.c | 7 +- extlib/benz/string.c | 242 ++++++++++++--------------- extlib/benz/symbol.c | 18 +- extlib/benz/vector.c | 21 ++- extlib/benz/write.c | 6 +- t/issue/foo-map.scm | 42 +++++ 20 files changed, 280 insertions(+), 275 deletions(-) create mode 100644 t/issue/foo-map.scm diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index 0f3bcfe6..bc688e10 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -2,84 +2,93 @@ #include "picrin/object.h" void -pic_str_set(pic_state *pic, struct pic_string *str, int i, char c) +pic_str_update(pic_state *pic, pic_value dst, pic_value src) { - struct pic_string *x, *y, *z, *tmp; - char buf[1]; - - if (pic_str_len(pic, str) <= i) { - pic_errorf(pic, "index out of range %d", i); - } - - buf[0] = c; - - x = pic_str_sub(pic, str, 0, i); - y = pic_str_value(pic, buf, 1); - z = pic_str_sub(pic, str, i + 1, pic_str_len(pic, str)); - - tmp = pic_str_cat(pic, x, pic_str_cat(pic, y, z)); - - pic_rope_incref(pic, tmp->rope); - pic_rope_decref(pic, str->rope); - str->rope = tmp->rope; + pic_rope_incref(pic, pic_str_ptr(pic, src)->rope); + pic_rope_decref(pic, pic_str_ptr(pic, dst)->rope); + pic_str_ptr(pic, dst)->rope = pic_str_ptr(pic, src)->rope; } static pic_value pic_str_string_set(pic_state *pic) { - struct pic_string *str; + pic_value str, x, y, z; char c; - int k; + int k, len; pic_get_args(pic, "sic", &str, &k, &c); - pic_str_set(pic, str, k, c); + len = pic_str_len(pic, str); + + VALID_INDEX(pic, len, k); + + x = pic_str_sub(pic, str, 0, k); + y = pic_str_value(pic, &c, 1); + z = pic_str_sub(pic, str, k + 1, len); + + pic_str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); + return pic_undef_value(pic); } static pic_value pic_str_string_copy_ip(pic_state *pic) { - struct pic_string *to, *from; - int n, at, start, end; + pic_value to, from, x, y, z; + int n, at, start, end, tolen, fromlen; n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end); + tolen = pic_str_len(pic, to); + fromlen = pic_str_len(pic, from); + switch (n) { case 3: start = 0; case 4: - end = pic_str_len(pic, from); - } - if (to == from) { - from = pic_str_sub(pic, from, 0, end); + end = fromlen; } - while (start < end) { - pic_str_set(pic, to, at++, pic_str_ref(pic, from, start++)); - } + VALID_ATRANGE(pic, tolen, at, fromlen, start, end); + + x = pic_str_sub(pic, to, 0, at); + y = pic_str_sub(pic, from, start, end); + z = pic_str_sub(pic, to, at + end - start, tolen); + + pic_str_update(pic, to, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); + return pic_undef_value(pic); } static pic_value pic_str_string_fill_ip(pic_state *pic) { - struct pic_string *str; - char c; - int n, start, end; + pic_value str, x, y, z; + char c, *buf; + int n, start, end, len; n = pic_get_args(pic, "sc|ii", &str, &c, &start, &end); + len = pic_str_len(pic, str); + switch (n) { case 2: start = 0; case 3: - end = pic_str_len(pic, str); + end = len; } - while (start < end) { - pic_str_set(pic, str, start++, c); - } + VALID_RANGE(pic, len, start, end); + + buf = pic_alloca(pic, end - start); + memset(buf, c, end - start); + + x = pic_str_sub(pic, str, 0, start); + y = pic_str_value(pic, buf, end - start); + z = pic_str_sub(pic, str, end, len); + + pic_str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); + return pic_undef_value(pic); } diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index 8346e3b3..fc5d2ecf 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -19,12 +19,8 @@ pic_system_cmdline(pic_state *pic) pic_get_args(pic, ""); for (i = 0; i < picrin_argc; ++i) { - size_t ai = pic_enter(pic); - - v = pic_cons(pic, pic_obj_value(pic_cstr_value(pic, picrin_argv[i])), v); - pic_leave(pic, ai); + pic_push(pic, pic_cstr_value(pic, picrin_argv[i]), v); } - return pic_reverse(pic, v); } @@ -88,7 +84,7 @@ pic_system_getenv(pic_state *pic) if (val == NULL) return pic_nil_value(pic); else - return pic_obj_value(pic_cstr_value(pic, val)); + return pic_cstr_value(pic, val); } static pic_value @@ -105,7 +101,7 @@ pic_system_getenvs(pic_state *pic) } for (envp = picrin_envp; *envp; ++envp) { - struct pic_string *key, *val; + pic_value key, val; int i; for (i = 0; (*envp)[i] != '='; ++i) @@ -115,7 +111,7 @@ pic_system_getenvs(pic_state *pic) val = pic_cstr_value(pic, getenv(pic_str(pic, key))); /* push */ - data = pic_cons(pic, pic_cons(pic, pic_obj_value(key), pic_obj_value(val)), data); + data = pic_cons(pic, pic_cons(pic, key, val), data); pic_leave(pic, ai); pic_protect(pic, data); diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index b14fd482..1438b0a7 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -19,7 +19,7 @@ pic_rl_readline(pic_state *pic) result = readline(prompt); if(result) - return pic_obj_value(pic_cstr_value(pic, result)); + return pic_cstr_value(pic, result); else return pic_eof_object(pic); } @@ -87,7 +87,7 @@ pic_rl_current_history(pic_state *pic) { pic_get_args(pic, ""); - return pic_obj_value(pic_cstr_value(pic, current_history()->line)); + return pic_cstr_value(pic, current_history()->line); } static pic_value @@ -100,8 +100,7 @@ pic_rl_history_get(pic_state *pic) e = history_get(i); - return e ? pic_obj_value(pic_cstr_value(pic, e->line)) - : pic_false_value(pic); + return e ? pic_cstr_value(pic, e->line) : pic_false_value(pic); } static pic_value @@ -114,8 +113,7 @@ pic_rl_remove_history(pic_state *pic) e = remove_history(i); - return e ? pic_obj_value(pic_cstr_value(pic, e->line)) - : pic_false_value(pic); + return e ? pic_cstr_value(pic, e->line) : pic_false_value(pic); } static pic_value @@ -148,8 +146,7 @@ pic_rl_previous_history(pic_state *pic) e = previous_history(); - return e ? pic_obj_value(pic_cstr_value(pic, e->line)) - : pic_false_value(pic); + return e ? pic_cstr_value(pic, e->line) : pic_false_value(pic); } static pic_value @@ -161,8 +158,7 @@ pic_rl_next_history(pic_state *pic) e = next_history(); - return e ? pic_obj_value(pic_cstr_value(pic, e->line)) - : pic_false_value(pic); + return e ? pic_cstr_value(pic, e->line) : pic_false_value(pic); } static pic_value @@ -240,7 +236,7 @@ pic_rl_history_expand(pic_state *pic) if(status == -1 || status == 2) pic_errorf(pic, "%s\n", result); - return pic_obj_value(pic_cstr_value(pic, result)); + return pic_cstr_value(pic, result); } void diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 130f6ff2..3253e449 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -81,8 +81,7 @@ pic_regexp_regexp_match(pic_state *pic) pic_value reg; const char *input; regmatch_t match[100]; - pic_value matches, positions; - struct pic_string *str; + pic_value str, matches, positions; int i, offset; pic_get_args(pic, "oz", ®, &input); @@ -97,7 +96,7 @@ pic_regexp_regexp_match(pic_state *pic) offset = 0; while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, match, 0) != REG_NOMATCH) { - pic_push(pic, pic_obj_value(pic_str_value(pic, input, match[0].rm_eo - match[0].rm_so)), matches); + pic_push(pic, pic_str_value(pic, input, match[0].rm_eo - match[0].rm_so), matches); pic_push(pic, pic_int_value(pic, offset), positions); offset += match[0].rm_eo; @@ -112,7 +111,7 @@ pic_regexp_regexp_match(pic_state *pic) break; } str = pic_str_value(pic, input + match[i].rm_so, match[i].rm_eo - match[i].rm_so); - pic_push(pic, pic_obj_value(str), matches); + pic_push(pic, str, matches); pic_push(pic, pic_int_value(pic, match[i].rm_so), positions); } } @@ -141,12 +140,12 @@ pic_regexp_regexp_split(pic_state *pic) pic_assert_type(pic, reg, regexp); while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { - pic_push(pic, pic_obj_value(pic_str_value(pic, input, match.rm_so)), output); + pic_push(pic, pic_str_value(pic, input, match.rm_so), output); input += match.rm_eo; } - pic_push(pic, pic_obj_value(pic_cstr_value(pic, input)), output); + pic_push(pic, pic_cstr_value(pic, input), output); return pic_reverse(pic, output); } @@ -157,7 +156,7 @@ pic_regexp_regexp_replace(pic_state *pic) pic_value reg; const char *input; regmatch_t match; - struct pic_string *txt, *output = pic_lit_value(pic, ""); + pic_value txt, output = pic_lit_value(pic, ""); pic_get_args(pic, "ozs", ®, &input, &txt); @@ -170,9 +169,7 @@ pic_regexp_regexp_replace(pic_state *pic) input += match.rm_eo; } - output = pic_str_cat(pic, output, pic_str_value(pic, input, strlen(input))); - - return pic_obj_value(output); + return pic_str_cat(pic, output, pic_str_value(pic, input, strlen(input))); } void diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index 125c3e74..ec96e4fe 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -81,7 +81,7 @@ pic_blob_make_bytevector(pic_state *pic) blob = pic_blob_value(pic, 0, k); - memset(pic_blob(pic, blob, NULL), k, (unsigned char)b); + memset(pic_blob(pic, blob, NULL), (unsigned char)b, k); return blob; } diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 826c4626..87bd6269 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -106,7 +106,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) return s1 == s2; } case PIC_TYPE_STRING: { - return pic_str_cmp(pic, pic_str_ptr(x), pic_str_ptr(y)) == 0; + return pic_str_cmp(pic, x, y) == 0; } case PIC_TYPE_BLOB: { int xlen, ylen; diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index c354c89f..85389535 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -5,12 +5,12 @@ #include "picrin.h" #include "picrin/object.h" -struct pic_string * +pic_value pic_get_backtrace(pic_state *pic) { size_t ai = pic_enter(pic); pic_callinfo *ci; - struct pic_string *trace; + pic_value trace; trace = pic_lit_value(pic, ""); @@ -28,7 +28,7 @@ pic_get_backtrace(pic_state *pic) } pic_leave(pic, ai); - pic_protect(pic, pic_obj_value(trace)); + pic_protect(pic, trace); return trace; } @@ -59,6 +59,6 @@ pic_print_backtrace(pic_state *pic, xFILE *file) } xfprintf(pic, file, "\n"); - xfputs(pic, pic_str(pic, e->stack), file); + xfputs(pic, pic_str(pic, pic_obj_value(e->stack)), file); } } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 0686ff9e..e17c41a5 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -22,7 +22,7 @@ void pic_warnf(pic_state *pic, const char *fmt, ...) { va_list ap; - struct pic_string *err; + pic_value err; va_start(ap, fmt); err = pic_vstrf_value(pic, fmt, ap); @@ -36,7 +36,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) { va_list ap; const char *msg; - struct pic_string *err; + pic_value err; va_start(ap, fmt); err = pic_vstrf_value(pic, fmt, ap); @@ -92,16 +92,16 @@ struct pic_error * pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; - struct pic_string *stack; + pic_value stack; pic_sym *ty = pic_intern_cstr(pic, type); stack = pic_get_backtrace(pic); e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); e->type = ty; - e->msg = pic_cstr_value(pic, msg); + e->msg = pic_str_ptr(pic, pic_cstr_value(pic, msg)); e->irrs = irrs; - e->stack = stack; + e->stack = pic_str_ptr(pic, stack); return e; } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index d86e461c..20934ef8 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -53,7 +53,6 @@ typedef struct { struct pic_object; struct pic_symbol; -struct pic_string; struct pic_port; struct pic_error; struct pic_env; @@ -121,7 +120,7 @@ PIC_INLINE int pic_int(pic_state *, pic_value i); PIC_INLINE double pic_float(pic_state *, pic_value f); PIC_INLINE char pic_char(pic_state *, pic_value c); #define pic_bool(pic,b) (! pic_false_p(pic, b)) -const char *pic_str(pic_state *, struct pic_string *); +const char *pic_str(pic_state *, pic_value str); unsigned char *pic_blob(pic_state *, pic_value blob, int *len); void *pic_data(pic_state *, pic_value data); @@ -139,11 +138,11 @@ PIC_INLINE pic_value pic_true_value(pic_state *); PIC_INLINE pic_value pic_false_value(pic_state *); PIC_INLINE pic_value pic_bool_value(pic_state *, bool); PIC_INLINE pic_value pic_eof_object(pic_state *); -struct pic_string *pic_str_value(pic_state *, const char *str, int len); +pic_value pic_str_value(pic_state *, const char *str, int len); #define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr)) #define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1)) -struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...); -struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); +pic_value pic_strf_value(pic_state *, const char *fmt, ...); +pic_value pic_vstrf_value(pic_state *, const char *fmt, va_list ap); pic_value pic_blob_value(pic_state *, const unsigned char *buf, int len); pic_value pic_data_value(pic_state *, void *ptr, const pic_data_type *type); @@ -250,19 +249,19 @@ void pic_weak_del(pic_state *, struct pic_weak *, void *); bool pic_weak_has(pic_state *, struct pic_weak *, void *); /* symbol */ -pic_sym *pic_intern(pic_state *, struct pic_string *); +pic_sym *pic_intern(pic_state *, pic_value str); #define pic_intern_str(pic,s,i) pic_intern(pic, pic_str_value(pic, (s), (i))) #define pic_intern_cstr(pic,s) pic_intern(pic, pic_cstr_value(pic, (s))) #define pic_intern_lit(pic,lit) pic_intern(pic, pic_lit_value(pic, lit)) -struct pic_string *pic_sym_name(pic_state *, pic_sym *); +pic_value pic_sym_name(pic_state *, pic_sym *); /* string */ -int pic_str_len(pic_state *, struct pic_string *); -char pic_str_ref(pic_state *, struct pic_string *, int); -struct pic_string *pic_str_cat(pic_state *, struct pic_string *, struct pic_string *); -struct pic_string *pic_str_sub(pic_state *, struct pic_string *, int, int); -int pic_str_cmp(pic_state *, struct pic_string *, struct pic_string *); -int pic_str_hash(pic_state *, struct pic_string *); +int pic_str_len(pic_state *, pic_value str); +char pic_str_ref(pic_state *, pic_value str, int i); +pic_value pic_str_cat(pic_state *, pic_value str1, pic_value str2); +pic_value pic_str_sub(pic_state *, pic_value str, int i, int j); +int pic_str_cmp(pic_state *, pic_value str1, pic_value str2); +int pic_str_hash(pic_state *, pic_value str); /* extra stuff */ @@ -348,7 +347,7 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); #define pic_pop(pic, place) (place = pic_cdr(pic, place)) void pic_warnf(pic_state *, const char *, ...); -struct pic_string *pic_get_backtrace(pic_state *); +pic_value pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); #define pic_stdin(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-input-port", 0)) diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 8970c77f..22953098 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -120,15 +120,15 @@ struct pic_port { xFILE *file; }; +#define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o)) +#define pic_blob_ptr(pic, o) ((struct pic_blob *)pic_obj_ptr(o)) #define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) -#define pic_blob_ptr(pic, v) ((struct pic_blob *)pic_obj_ptr(v)) #define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_dict_ptr(pic, o) ((struct pic_dict *)pic_obj_ptr(o)) #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) -#define pic_str_ptr(o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) @@ -166,7 +166,7 @@ struct pic_env *pic_make_env(pic_state *, struct pic_env *); pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); -struct pic_string *pic_id_name(pic_state *, pic_id *); +pic_value pic_id_name(pic_state *, pic_id *); void pic_rope_incref(pic_state *, struct pic_rope *); void pic_rope_decref(pic_state *, struct pic_rope *); diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index b97c49f0..bd315e2d 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -32,13 +32,13 @@ get_library(pic_state *pic, const char *lib) } static struct pic_env * -make_library_env(pic_state *pic, struct pic_string *name) +make_library_env(pic_state *pic, pic_value name) { struct pic_env *env; env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); env->up = NULL; - env->lib = name; + env->lib = pic_str_ptr(pic, name); kh_init(env, &env->map); /* set up default environment */ @@ -55,9 +55,8 @@ pic_make_library(pic_state *pic, const char *lib) { khash_t(ltable) *h = &pic->ltable; const char *old_lib; - struct pic_string *name; struct pic_env *env; - pic_value exports; + pic_value name, exports; khiter_t it; int ret; @@ -74,7 +73,7 @@ pic_make_library(pic_state *pic, const char *lib) pic_errorf(pic, "library name already in use: %s", lib); } - kh_val(h, it).name = name; + kh_val(h, it).name = pic_str_ptr(pic, name); kh_val(h, it).env = env; kh_val(h, it).exports = pic_dict_ptr(pic, exports); @@ -98,7 +97,7 @@ pic_find_library(pic_state *pic, const char *lib) const char * pic_current_library(pic_state *pic) { - return pic_str(pic, pic->lib->name); + return pic_str(pic, pic_obj_value(pic->lib->name)); } struct pic_env * diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 9bb59afc..436df18f 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -26,12 +26,12 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { const char *name; pic_sym *uid; - struct pic_string *str; + pic_value str; name = pic_str(pic, pic_id_name(pic, id)); if (env->up == NULL && pic_sym_p(pic, pic_obj_value(id))) { /* toplevel & public */ - str = pic_strf_value(pic, "%s/%s", pic_str(pic, env->lib), name); + str = pic_strf_value(pic, "%s/%s", pic_str(pic, pic_obj_value(env->lib)), name); } else { str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); } diff --git a/extlib/benz/number.c b/extlib/benz/number.c index fe25d20c..9cd69ad0 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -218,7 +218,7 @@ pic_number_number_to_string(pic_state *pic) double f; bool e; int radix = 10; - struct pic_string *str; + pic_value str; pic_get_args(pic, "F|i", &f, &e, &radix); @@ -229,14 +229,11 @@ pic_number_number_to_string(pic_state *pic) if (e) { int ival = (int) f; int ilen = number_string_length(ival, radix); - int s = ilen + 1; - char *buf = pic_malloc(pic, s); + char *buf = pic_alloca(pic, ilen + 1); number_string(ival, radix, ilen, buf); - str = pic_str_value(pic, buf, s - 1); - - pic_free(pic, buf); + str = pic_str_value(pic, buf, ilen); } else { xFILE *file = xfopen_buf(pic, NULL, 0, "w"); @@ -249,7 +246,7 @@ pic_number_number_to_string(pic_state *pic) xfclose(pic, file); } - return pic_obj_value(str); + return str; } static pic_value diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 0aade676..aa7a4ee0 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -22,7 +22,7 @@ * z char ** c string * m pic_sym ** symbol * v pic_value * vector object - * s struct pic_str ** string object + * s pic_value * string object * b pic_value * bytevector object * l pic_value * lambda object * p struct pic_port ** port object @@ -142,19 +142,19 @@ pic_get_args(pic_state *pic, const char *format, ...) } VAL_CASE('c', char, char, pic_char(pic, v)) - VAL_CASE('z', str, const char *, pic_str(pic, pic_str_ptr(v))) + VAL_CASE('z', str, const char *, pic_str(pic, v)) #define PTR_CASE(c, type, ctype) \ VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) PTR_CASE('m', sym, pic_sym *) - PTR_CASE('s', str, struct pic_string *) PTR_CASE('p', port, struct pic_port *) PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + OBJ_CASE('s', str) OBJ_CASE('l', proc) OBJ_CASE('b', blob) OBJ_CASE('v', vec) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 3cefa8a3..92fa0b60 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -281,8 +281,7 @@ read_unsigned(pic_state *pic, xFILE *file, int c) } } if (idx >= ATOF_BUF_SIZE) - read_error(pic, "number too large", - pic_obj_value(pic_str_value(pic, (const char *)buf, ATOF_BUF_SIZE))); + read_error(pic, "number too large", pic_list(pic, 1, pic_str_value(pic, (const char *)buf, ATOF_BUF_SIZE))); if (! isdelim(c)) read_error(pic, "non-delimiter character given after number", pic_list(pic, 1, pic_char_value(pic, c))); @@ -419,7 +418,7 @@ read_string(pic_state *pic, xFILE *file, int c) { char *buf; int size, cnt; - struct pic_string *str; + pic_value str; size = 256; buf = pic_malloc(pic, size); @@ -446,7 +445,7 @@ read_string(pic_state *pic, xFILE *file, int c) str = pic_str_value(pic, buf, cnt); pic_free(pic, buf); - return pic_obj_value(str); + return str; } static pic_value diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 28d18426..ea84ae4b 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -94,14 +94,15 @@ pic_make_rope(pic_state *pic, struct pic_chunk *c) return x; } -static struct pic_string * -pic_str_valueing(pic_state *pic, struct pic_rope *rope) +static pic_value +pic_make_str(pic_state *pic, struct pic_rope *rope) { struct pic_string *str; str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TYPE_STRING); str->rope = rope; /* delegate ownership */ - return str; + + return pic_obj_value(str); } static size_t @@ -237,7 +238,7 @@ rope_cstr(pic_state *pic, struct pic_rope *x) return c->str; } -struct pic_string * +pic_value pic_str_value(pic_state *pic, const char *str, int len) { struct pic_chunk *c; @@ -250,47 +251,47 @@ pic_str_value(pic_state *pic, const char *str, int len) } c = pic_make_chunk_lit(pic, str, -len); } - return pic_str_valueing(pic, pic_make_rope(pic, c)); + return pic_make_str(pic, pic_make_rope(pic, c)); } int -pic_str_len(pic_state PIC_UNUSED(*pic), struct pic_string *str) +pic_str_len(pic_state PIC_UNUSED(*pic), pic_value str) { - return rope_len(str->rope); + return rope_len(pic_str_ptr(pic, str)->rope); } char -pic_str_ref(pic_state *pic, struct pic_string *str, int i) +pic_str_ref(pic_state *pic, pic_value str, int i) { int c; - c = rope_at(str->rope, i); + c = rope_at(pic_str_ptr(pic, str)->rope, i); if (c == -1) { pic_errorf(pic, "index out of range %d", i); } return (char)c; } -struct pic_string * -pic_str_cat(pic_state *pic, struct pic_string *a, struct pic_string *b) +pic_value +pic_str_cat(pic_state *pic, pic_value a, pic_value b) { - return pic_str_valueing(pic, rope_cat(pic, a->rope, b->rope)); + return pic_make_str(pic, rope_cat(pic, pic_str_ptr(pic, a)->rope, pic_str_ptr(pic, b)->rope)); } -struct pic_string * -pic_str_sub(pic_state *pic, struct pic_string *str, int s, int e) +pic_value +pic_str_sub(pic_state *pic, pic_value str, int s, int e) { - return pic_str_valueing(pic, rope_sub(pic, str->rope, s, e)); + return pic_make_str(pic, rope_sub(pic, pic_str_ptr(pic, str)->rope, s, e)); } int -pic_str_cmp(pic_state *pic, struct pic_string *str1, struct pic_string *str2) +pic_str_cmp(pic_state *pic, pic_value str1, pic_value str2) { return strcmp(pic_str(pic, str1), pic_str(pic, str2)); } int -pic_str_hash(pic_state *pic, struct pic_string *str) +pic_str_hash(pic_state *pic, pic_value str) { const char *s; int h = 0; @@ -303,9 +304,9 @@ pic_str_hash(pic_state *pic, struct pic_string *str) } const char * -pic_str(pic_state *pic, struct pic_string *str) +pic_str(pic_state *pic, pic_value str) { - return rope_cstr(pic, str->rope); + return rope_cstr(pic, pic_str_ptr(pic, str)->rope); } static void @@ -374,10 +375,10 @@ vfstrf(pic_state *pic, xFILE *file, const char *fmt, va_list ap) return; } -struct pic_string * +pic_value pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap) { - struct pic_string *str; + pic_value str; xFILE *file; const char *buf; int len; @@ -391,11 +392,11 @@ pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap) return str; } -struct pic_string * +pic_value pic_strf_value(pic_state *pic, const char *fmt, ...) { va_list ap; - struct pic_string *str; + pic_value str; va_start(ap, fmt); str = pic_vstrf_value(pic, fmt, ap); @@ -419,22 +420,18 @@ pic_str_string(pic_state *pic) { int argc, i; pic_value *argv; - struct pic_string *str; char *buf; pic_get_args(pic, "*", &argc, &argv); - buf = pic_malloc(pic, argc); + buf = pic_alloca(pic, argc); for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], char); buf[i] = pic_char(pic, argv[i]); } - str = pic_str_value(pic, buf, argc); - pic_free(pic, buf); - - return pic_obj_value(str); + return pic_str_value(pic, buf, argc); } static pic_value @@ -443,23 +440,24 @@ pic_str_make_string(pic_state *pic) int len; char c = ' '; char *buf; - pic_value ret; pic_get_args(pic, "i|c", &len, &c); - buf = pic_malloc(pic, len); + if (len < 0) { + pic_errorf(pic, "make-string: negative length given %d", len); + } + + buf = pic_alloca(pic, len); + memset(buf, c, len); - ret = pic_obj_value(pic_str_value(pic, buf, len)); - - pic_free(pic, buf); - return ret; + return pic_str_value(pic, buf, len); } static pic_value pic_str_string_length(pic_state *pic) { - struct pic_string *str; + pic_value str; pic_get_args(pic, "s", &str); @@ -469,36 +467,38 @@ pic_str_string_length(pic_state *pic) static pic_value pic_str_string_ref(pic_state *pic) { - struct pic_string *str; + pic_value str; int k; pic_get_args(pic, "si", &str, &k); + VALID_INDEX(pic, pic_str_len(pic, str), k); + return pic_char_value(pic, pic_str_ref(pic, str, k)); } -#define DEFINE_STRING_CMP(name, op) \ - static pic_value \ - pic_str_string_##name(pic_state *pic) \ - { \ - int argc, i; \ - pic_value *argv; \ - \ - pic_get_args(pic, "*", &argc, &argv); \ - \ - if (argc < 1 || ! pic_str_p(pic, argv[0])) { \ - return pic_false_value(pic); \ - } \ - \ - for (i = 1; i < argc; ++i) { \ - if (! pic_str_p(pic, argv[i])) { \ - return pic_false_value(pic); \ - } \ - if (! (pic_str_cmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \ - return pic_false_value(pic); \ - } \ - } \ - return pic_true_value(pic); \ +#define DEFINE_STRING_CMP(name, op) \ + static pic_value \ + pic_str_string_##name(pic_state *pic) \ + { \ + int argc, i; \ + pic_value *argv; \ + \ + pic_get_args(pic, "*", &argc, &argv); \ + \ + if (argc < 1 || ! pic_str_p(pic, argv[0])) { \ + return pic_false_value(pic); \ + } \ + \ + for (i = 1; i < argc; ++i) { \ + if (! pic_str_p(pic, argv[i])) { \ + return pic_false_value(pic); \ + } \ + if (! (pic_str_cmp(pic, argv[i-1], argv[i]) op 0)) { \ + return pic_false_value(pic); \ + } \ + } \ + return pic_true_value(pic); \ } DEFINE_STRING_CMP(eq, ==) @@ -510,7 +510,7 @@ DEFINE_STRING_CMP(ge, >=) static pic_value pic_str_string_copy(pic_state *pic) { - struct pic_string *str; + pic_value str; int n, start, end, len; n = pic_get_args(pic, "s|ii", &str, &start, &end); @@ -524,10 +524,9 @@ pic_str_string_copy(pic_state *pic) end = len; } - if (start < 0 || end > len || end < start) - pic_errorf(pic, "string-copy: invalid index"); + VALID_RANGE(pic, len, start, end); - return pic_obj_value(pic_str_sub(pic, str, start, end)); + return pic_str_sub(pic, str, start, end); } static pic_value @@ -535,18 +534,15 @@ pic_str_string_append(pic_state *pic) { int argc, i; pic_value *argv; - struct pic_string *str; + pic_value str = pic_lit_value(pic, ""); pic_get_args(pic, "*", &argc, &argv); - str = pic_lit_value(pic, ""); for (i = 0; i < argc; ++i) { - if (! pic_str_p(pic, argv[i])) { - pic_errorf(pic, "type error"); - } - str = pic_str_cat(pic, str, pic_str_ptr(argv[i])); + pic_assert_type(pic, argv[i], str); + str = pic_str_cat(pic, str, argv[i]); } - return pic_obj_value(str); + return str; } static pic_value @@ -554,135 +550,111 @@ pic_str_string_map(pic_state *pic) { pic_value proc, *argv, vals, val; int argc, i, len, j; - struct pic_string *str; char *buf; pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { pic_errorf(pic, "string-map: one or more strings expected, but got zero"); - } else { - pic_assert_type(pic, argv[0], str); - len = pic_str_len(pic, pic_str_ptr(argv[0])); } - for (i = 1; i < argc; ++i) { + + len = INT_MAX; + for (i = 0; i < argc; ++i) { + int l; pic_assert_type(pic, argv[i], str); - - len = len < pic_str_len(pic, pic_str_ptr(argv[i])) - ? len - : pic_str_len(pic, pic_str_ptr(argv[i])); + l = pic_str_len(pic, argv[i]); + len = len < l ? len : l; } - buf = pic_malloc(pic, len); - pic_try { - for (i = 0; i < len; ++i) { - vals = pic_nil_value(pic); - for (j = 0; j < argc; ++j) { - pic_push(pic, pic_char_value(pic, pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); - } - val = pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); + buf = pic_alloca(pic, len); - pic_assert_type(pic, val, char); - buf[i] = pic_char(pic, val); + for (i = 0; i < len; ++i) { + vals = pic_nil_value(pic); + for (j = 0; j < argc; ++j) { + pic_push(pic, pic_char_value(pic, pic_str_ref(pic, argv[j], i)), vals); } - str = pic_str_value(pic, buf, len); - } - pic_catch { - pic_free(pic, buf); - pic_raise(pic, pic->err); - } + vals = pic_reverse(pic, vals); + val = pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); - pic_free(pic, buf); + pic_assert_type(pic, val, char); - return pic_obj_value(str); + buf[i] = pic_char(pic, val); + } + return pic_str_value(pic, buf, len); } static pic_value pic_str_string_for_each(pic_state *pic) { - int argc, len, i, j; pic_value proc, *argv, vals; + int argc, i, len, j; pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { pic_errorf(pic, "string-map: one or more strings expected, but got zero"); - } else { - pic_assert_type(pic, argv[0], str); - len = pic_str_len(pic, pic_str_ptr(argv[0])); } - for (i = 1; i < argc; ++i) { - pic_assert_type(pic, argv[i], str); - len = len < pic_str_len(pic, pic_str_ptr(argv[i])) - ? len - : pic_str_len(pic, pic_str_ptr(argv[i])); + len = INT_MAX; + for (i = 0; i < argc; ++i) { + int l; + pic_assert_type(pic, argv[i], str); + l = pic_str_len(pic, argv[i]); + len = len < l ? len : l; } for (i = 0; i < len; ++i) { vals = pic_nil_value(pic); for (j = 0; j < argc; ++j) { - pic_push(pic, pic_char_value(pic, pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); + pic_push(pic, pic_char_value(pic, pic_str_ref(pic, argv[j], i)), vals); } + vals = pic_reverse(pic, vals); pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); } - return pic_undef_value(pic); } static pic_value pic_str_list_to_string(pic_state *pic) { - struct pic_string *str; pic_value list, e, it; int i; char *buf; pic_get_args(pic, "o", &list); - if (pic_length(pic, list) == 0) { - return pic_obj_value(pic_lit_value(pic, "")); + buf = pic_alloca(pic, pic_length(pic, list)); + + i = 0; + pic_for_each (e, list, it) { + pic_assert_type(pic, e, char); + + buf[i++] = pic_char(pic, e); } - buf = pic_malloc(pic, pic_length(pic, list)); - - pic_try { - i = 0; - pic_for_each (e, list, it) { - pic_assert_type(pic, e, char); - - buf[i++] = pic_char(pic, e); - } - - str = pic_str_value(pic, buf, i); - } - pic_catch { - pic_free(pic, buf); - pic_raise(pic, pic->err); - } - pic_free(pic, buf); - - return pic_obj_value(str); + return pic_str_value(pic, buf, i); } static pic_value pic_str_string_to_list(pic_state *pic) { - struct pic_string *str; - pic_value list; - int n, start, end, i; + pic_value str, list; + int n, start, end, len, i; n = pic_get_args(pic, "s|ii", &str, &start, &end); + len = pic_str_len(pic, str); + switch (n) { case 1: start = 0; case 2: - end = pic_str_len(pic, str); + end = len; } - list = pic_nil_value(pic); + VALID_RANGE(pic, len, start, end); + list = pic_nil_value(pic); for (i = start; i < end; ++i) { pic_push(pic, pic_char_value(pic, pic_str_ref(pic, str, i)), list); } diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 2ccdb340..25d513ac 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -5,20 +5,20 @@ #include "picrin.h" #include "picrin/object.h" -#define kh_pic_str_hash(a) (pic_str_hash(pic, (a))) -#define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, (a), (b)) == 0) +#define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a))) +#define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0) KHASH_DEFINE(oblist, struct pic_string *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) pic_sym * -pic_intern(pic_state *pic, struct pic_string *str) +pic_intern(pic_state *pic, pic_value str) { khash_t(oblist) *h = &pic->oblist; pic_sym *sym; khiter_t it; int ret; - it = kh_put(oblist, h, str, &ret); + it = kh_put(oblist, h, pic_str_ptr(pic, str), &ret); if (ret == 0) { /* if exists */ sym = kh_val(h, it); pic_protect(pic, pic_obj_value(sym)); @@ -28,7 +28,7 @@ pic_intern(pic_state *pic, struct pic_string *str) kh_val(h, it) = pic->sQUOTE; /* dummy */ sym = (pic_sym *)pic_obj_alloc(pic, sizeof(pic_sym), PIC_TYPE_SYMBOL); - sym->str = str; + sym->str = pic_str_ptr(pic, str); kh_val(h, it) = sym; return sym; @@ -45,13 +45,13 @@ pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env) return nid; } -struct pic_string * +pic_value pic_sym_name(pic_state PIC_UNUSED(*pic), pic_sym *sym) { - return sym->str; + return pic_obj_value(sym->str); } -struct pic_string * +pic_value pic_id_name(pic_state *pic, pic_id *id) { while (! pic_sym_p(pic, pic_obj_value(id))) { @@ -103,7 +103,7 @@ pic_symbol_symbol_to_string(pic_state *pic) static pic_value pic_symbol_string_to_symbol(pic_state *pic) { - struct pic_string *str; + pic_value str; pic_get_args(pic, "s", &str); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 8dce36f3..7ae1d824 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -248,6 +248,7 @@ pic_vec_vector_map(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ref(pic, argv[j], i), vals); } + vals = pic_reverse(pic, vals); pic_vec_set(pic, vec, i, pic_funcall(pic, "picrin.base", "apply", 2, proc, vals)); } @@ -279,6 +280,7 @@ pic_vec_vector_for_each(pic_state *pic) for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ref(pic, argv[j], i), vals); } + vals = pic_reverse(pic, vals); pic_funcall(pic, "picrin.base", "apply", 2, proc, vals); } @@ -335,7 +337,6 @@ pic_vec_vector_to_string(pic_state *pic) pic_value vec, t; char *buf; int n, start, end, i, len; - struct pic_string *str; n = pic_get_args(pic, "v|ii", &vec, &start, &end); @@ -350,7 +351,7 @@ pic_vec_vector_to_string(pic_state *pic) VALID_RANGE(pic, len, start, end); - buf = pic_malloc(pic, end - start); + buf = pic_alloca(pic, end - start); for (i = start; i < end; ++i) { t = pic_vec_ref(pic, vec, i); @@ -359,29 +360,27 @@ pic_vec_vector_to_string(pic_state *pic) buf[i - start] = pic_char(pic, t); } - str = pic_str_value(pic, buf, end - start); - pic_free(pic, buf); - - return pic_obj_value(str); + return pic_str_value(pic, buf, end - start); } static pic_value pic_vec_string_to_vector(pic_state *pic) { - struct pic_string *str; - int n, start, end, i; - pic_value vec; + pic_value str, vec; + int n, start, end, len, i; n = pic_get_args(pic, "s|ii", &str, &start, &end); + len = pic_str_len(pic, str); + switch (n) { case 1: start = 0; case 2: - end = pic_str_len(pic, str); + end = len; } - VALID_RANGE(pic, pic_str_len(pic, str), start, end); + VALID_RANGE(pic, len, start, end); vec = pic_make_vec(pic, end - start, NULL); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index f5f8ceb6..11de3960 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -86,7 +86,7 @@ write_char(pic_state *pic, char c, xFILE *file, int mode) } static void -write_str(pic_state *pic, struct pic_string *str, xFILE *file, int mode) +write_str(pic_state *pic, pic_value str, xFILE *file, int mode) { int i; const char *cstr = pic_str(pic, str); @@ -312,7 +312,7 @@ write_core(struct writer_control *p, pic_value obj) write_char(pic, pic_char(pic, obj), file, p->mode); break; case PIC_TYPE_STRING: - write_str(pic, pic_str_ptr(obj), file, p->mode); + write_str(pic, obj, file, p->mode); break; case PIC_TYPE_PAIR: write_pair(p, obj); @@ -442,7 +442,7 @@ pic_printf(pic_state *pic, const char *fmt, ...) { xFILE *file = pic_stdout(pic)->file; va_list ap; - struct pic_string *str; + pic_value str; va_start(ap, fmt); diff --git a/t/issue/foo-map.scm b/t/issue/foo-map.scm new file mode 100644 index 00000000..e52fa3e5 --- /dev/null +++ b/t/issue/foo-map.scm @@ -0,0 +1,42 @@ +(import (scheme base) + (picrin test)) + +(test-begin) + +(define (char-inc c) + (integer->char (+ (char->integer c) 1))) + +(define (char-dec c) + (integer->char (- (char->integer c) 1))) + +(test "tsvcmxdmqr" + (string-map (lambda (c k) + ((if (eqv? k #\+) char-inc char-dec) c)) + "studlycnps xxx" + "+-+-+-+-+-")) + +(test "abcdefgh" + (begin + (define s "") + (string-for-each + (lambda (a b) + (set! s (string-append s (string a b)))) + "aceg hij" + "bdfh") + s)) + +(test #(#(1 6 9) #(2 7 10) #(3 8 11)) + (vector-map vector #(1 2 3 4 5) #(6 7 8) #(9 10 11 12))) + +(test "(1 4 1)(2 5 1)" + (call-with-port (open-output-string) + (lambda (port) + (parameterize ((current-output-port port)) + (vector-for-each + (lambda args (display args)) + #(1 2 3) + #(4 5) + #(1 1)) + (get-output-string port))))) + +(test-end) From ef26a75d4557fa19a33ae2243128c5ff13e1ef9c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 04:35:15 +0900 Subject: [PATCH 059/119] struct pic_weak * -> pic_value --- extlib/benz/eval.c | 4 +- extlib/benz/gc.c | 8 +-- extlib/benz/include/picrin.h | 10 +-- extlib/benz/include/picrin/object.h | 4 +- extlib/benz/include/picrin/state.h | 4 +- extlib/benz/macro.c | 12 ++-- extlib/benz/proc.c | 8 +-- extlib/benz/state.c | 10 +-- extlib/benz/var.c | 18 +++--- extlib/benz/weak.c | 94 ++++++++++------------------- 10 files changed, 69 insertions(+), 103 deletions(-) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index eaf6a72a..248b67f9 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -147,13 +147,13 @@ define_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) int ret; if (search_scope(pic, scope, sym)) { - if (scope->depth > 0 || pic_weak_has(pic, pic->globals, sym)) { + if (scope->depth > 0 || pic_weak_has(pic, pic->globals, pic_obj_value(sym))) { pic_warnf(pic, "redefining variable: ~s", pic_obj_value(sym)); } return; } - pic_weak_set(pic, pic->globals, sym, pic_invalid_value()); + pic_weak_set(pic, pic->globals, pic_obj_value(sym), pic_invalid_value()); kh_put(a, &scope->locals, sym, &ret); } diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 8baa2239..b903f048 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -471,14 +471,10 @@ gc_mark_phase(pic_state *pic) M(sADD); M(sSUB); M(sMUL); M(sDIV); M(sEQ); M(sLT); M(sLE); M(sGT); M(sGE); M(sNOT); /* global variables */ - if (pic->globals) { - gc_mark_object(pic, (struct pic_object *)pic->globals); - } + gc_mark(pic, pic->globals); /* macro objects */ - if (pic->macros) { - gc_mark_object(pic, (struct pic_object *)pic->macros); - } + gc_mark(pic, pic->macros); /* error object */ gc_mark(pic, pic->err); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 20934ef8..2b092037 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -242,11 +242,11 @@ int pic_dict_size(pic_state *, pic_value dict); bool pic_dict_next(pic_state *, pic_value dict, int *iter, pic_sym **key, pic_value *val); /* ephemeron */ -struct pic_weak *pic_make_weak(pic_state *); -pic_value pic_weak_ref(pic_state *, struct pic_weak *, void *); -void pic_weak_set(pic_state *, struct pic_weak *, void *, pic_value); -void pic_weak_del(pic_state *, struct pic_weak *, void *); -bool pic_weak_has(pic_state *, struct pic_weak *, void *); +pic_value pic_make_weak(pic_state *); +pic_value pic_weak_ref(pic_state *, pic_value weak, pic_value key); +void pic_weak_set(pic_state *, pic_value weak, pic_value key, pic_value val); +void pic_weak_del(pic_state *, pic_value weak, pic_value key); +bool pic_weak_has(pic_state *, pic_value weak, pic_value key); /* symbol */ pic_sym *pic_intern(pic_state *, pic_value str); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 22953098..cbe3e264 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -11,7 +11,7 @@ extern "C" { KHASH_DECLARE(env, pic_id *, pic_sym *) KHASH_DECLARE(dict, pic_sym *, pic_value) -KHASH_DECLARE(weak, void *, pic_value) +KHASH_DECLARE(weak, struct pic_object *, pic_value) struct pic_id { union { @@ -125,11 +125,11 @@ struct pic_port { #define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) #define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) #define pic_dict_ptr(pic, o) ((struct pic_dict *)pic_obj_ptr(o)) +#define pic_weak_ptr(pic, o) ((struct pic_weak *)pic_obj_ptr(o)) #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) -#define pic_weak_ptr(v) ((struct pic_weak *)pic_obj_ptr(v)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) #define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index c5590788..30220021 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -79,8 +79,8 @@ struct pic_state { khash_t(oblist) oblist; /* string to symbol */ int ucnt; - struct pic_weak *globals; - struct pic_weak *macros; + pic_value globals; /* weak */ + pic_value macros; /* weak */ khash_t(ltable) ltable; struct pic_list ireps; /* chain */ diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 436df18f..486362a1 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -109,26 +109,26 @@ pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) static void define_macro(pic_state *pic, pic_sym *uid, pic_value mac) { - if (pic_weak_has(pic, pic->macros, uid)) { + if (pic_weak_has(pic, pic->macros, pic_obj_value(uid))) { pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(uid)); } - pic_weak_set(pic, pic->macros, uid, mac); + pic_weak_set(pic, pic->macros, pic_obj_value(uid), mac); } static pic_value find_macro(pic_state *pic, pic_sym *uid) { - if (! pic_weak_has(pic, pic->macros, uid)) { + if (! pic_weak_has(pic, pic->macros, pic_obj_value(uid))) { return pic_false_value(pic); } - return pic_weak_ref(pic, pic->macros, uid); + return pic_weak_ref(pic, pic->macros, pic_obj_value(uid)); } static void shadow_macro(pic_state *pic, pic_sym *uid) { - if (pic_weak_has(pic, pic->macros, uid)) { - pic_weak_del(pic, pic->macros, uid); + if (pic_weak_has(pic, pic->macros, pic_obj_value(uid))) { + pic_weak_del(pic, pic->macros, pic_obj_value(uid)); } } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index aa7a4ee0..69d7d34d 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -182,16 +182,16 @@ pic_get_args(pic_state *pic, const char *format, ...) static pic_value vm_gref(pic_state *pic, pic_sym *uid) { - if (! pic_weak_has(pic, pic->globals, uid)) { + if (! pic_weak_has(pic, pic->globals, pic_obj_value(uid))) { pic_errorf(pic, "uninitialized global variable: %s", pic_str(pic, pic_sym_name(pic, uid))); } - return pic_weak_ref(pic, pic->globals, uid); + return pic_weak_ref(pic, pic->globals, pic_obj_value(uid)); } static void vm_gset(pic_state *pic, pic_sym *uid, pic_value value) { - pic_weak_set(pic, pic->globals, uid, value); + pic_weak_set(pic, pic->globals, pic_obj_value(uid), value); } static void @@ -896,7 +896,7 @@ pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { uid = pic_add_identifier(pic, (pic_id *)sym, env); } else { - if (pic_weak_has(pic, pic->globals, uid)) { + if (pic_weak_has(pic, pic->globals, pic_obj_value(uid))) { pic_warnf(pic, "redefining variable: ~s", pic_obj_value(uid)); } } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 77763c6a..60ed6d9a 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -259,10 +259,10 @@ pic_open(pic_allocf allocf, void *userdata) pic->ucnt = 0; /* global variables */ - pic->globals = NULL; + pic->globals = pic_make_weak(pic); /* macros */ - pic->macros = NULL; + pic->macros = pic_make_weak(pic); /* features */ pic->features = pic_nil_value(pic); @@ -354,7 +354,7 @@ pic_open(pic_allocf allocf, void *userdata) pic_reader_init(pic); /* parameter table */ - pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable); + pic->ptable = pic_cons(pic, pic_make_weak(pic), pic->ptable); /* standard libraries */ pic_make_library(pic, "picrin.user"); @@ -394,8 +394,8 @@ pic_close(pic_state *pic) pic->xp = pic->xpbase; pic->arena_idx = 0; pic->err = pic_invalid_value(); - pic->globals = NULL; - pic->macros = NULL; + pic->globals = pic_invalid_value(); + pic->macros = pic_invalid_value(); pic->features = pic_nil_value(pic); /* free all libraries */ diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 04a07e5e..6f59c0c7 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -8,13 +8,11 @@ static pic_value var_get(pic_state *pic, pic_value var) { - pic_value elem, it; - struct pic_weak *weak; + pic_value weak, it; - pic_for_each (elem, pic->ptable, it) { - weak = pic_weak_ptr(elem); - if (pic_weak_has(pic, weak, pic_obj_ptr(var))) { - return pic_weak_ref(pic, weak, pic_obj_ptr(var)); + pic_for_each (weak, pic->ptable, it) { + if (pic_weak_has(pic, weak, var)) { + return pic_weak_ref(pic, weak, var); } } pic_panic(pic, "logic flaw"); @@ -23,11 +21,11 @@ var_get(pic_state *pic, pic_value var) static pic_value var_set(pic_state *pic, pic_value var, pic_value val) { - struct pic_weak *weak; + pic_value weak; - weak = pic_weak_ptr(pic_car(pic, pic->ptable)); + weak = pic_car(pic, pic->ptable); - pic_weak_set(pic, weak, pic_obj_ptr(var), val); + pic_weak_set(pic, weak, var, val); return pic_undef_value(pic); } @@ -82,7 +80,7 @@ pic_var_with_parameter(pic_state *pic) pic_get_args(pic, "l", &body); - pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable); + pic->ptable = pic_cons(pic, pic_make_weak(pic), pic->ptable); val = pic_call(pic, body, 0); diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 69effdcc..1ba978b1 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -5,9 +5,9 @@ #include "picrin.h" #include "picrin/object.h" -KHASH_DEFINE(weak, void *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) +KHASH_DEFINE(weak, struct pic_object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) -struct pic_weak * +pic_value pic_make_weak(pic_state *pic) { struct pic_weak *weak; @@ -16,97 +16,59 @@ pic_make_weak(pic_state *pic) weak->prev = NULL; kh_init(weak, &weak->hash); - return weak; + return pic_obj_value(weak); } pic_value -pic_weak_ref(pic_state *pic, struct pic_weak *weak, void *key) +pic_weak_ref(pic_state *pic, pic_value weak, pic_value key) { - khash_t(weak) *h = &weak->hash; + khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; khiter_t it; - it = kh_get(weak, h, key); + it = kh_get(weak, h, pic_obj_ptr(key)); if (it == kh_end(h)) { - pic_errorf(pic, "element not found for a key: ~s", pic_obj_value(key)); + pic_errorf(pic, "element not found for a key: ~s", key); } return kh_val(h, it); } -void * -pic_weak_rev_ref(pic_state *pic, struct pic_weak *weak, pic_value val) -{ - khash_t(weak) *h = &weak->hash; - - if (h->n_buckets) { - khint_t i = 0; - while ((i < h->n_buckets) && (ac_iseither(h->flags, i) || !pic_eq_p(pic, h->vals[i], val))) { - i += 1; - } - if (i < h->n_buckets) return kh_key(h, i); - } - pic_errorf(pic, "key not found for an element: ~s", val); - return NULL; -} - void -pic_weak_set(pic_state PIC_UNUSED(*pic), struct pic_weak *weak, void *key, pic_value val) +pic_weak_set(pic_state *pic, pic_value weak, pic_value key, pic_value val) { - khash_t(weak) *h = &weak->hash; + khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; int ret; khiter_t it; - it = kh_put(weak, h, key, &ret); + it = kh_put(weak, h, pic_obj_ptr(key), &ret); kh_val(h, it) = val; } bool -pic_weak_has(pic_state PIC_UNUSED(*pic), struct pic_weak *weak, void *key) +pic_weak_has(pic_state *pic, pic_value weak, pic_value key) { - return kh_get(weak, &weak->hash, key) != kh_end(&weak->hash); + khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; + + return kh_get(weak, h, pic_obj_ptr(key)) != kh_end(h); } void -pic_weak_del(pic_state *pic, struct pic_weak *weak, void *key) +pic_weak_del(pic_state *pic, pic_value weak, pic_value key) { - khash_t(weak) *h = &weak->hash; + khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; khiter_t it; - it = kh_get(weak, h, key); + it = kh_get(weak, h, pic_obj_ptr(key)); if (it == kh_end(h)) { - pic_errorf(pic, "no slot named ~s found in ephemeron", pic_obj_value(key)); + pic_errorf(pic, "no slot named ~s found in ephemeron", key); } kh_del(weak, h, it); } -static pic_value -weak_get(pic_state *pic, struct pic_weak *weak, void *key) -{ - if (! pic_weak_has(pic, weak, key)) { - return pic_false_value(pic); - } - return pic_cons(pic, pic_obj_value(key), pic_weak_ref(pic, weak, key)); -} - -static pic_value -weak_set(pic_state *pic, struct pic_weak *weak, void *key, pic_value val) -{ - if (pic_undef_p(pic, val)) { - if (pic_weak_has(pic, weak, key)) { - pic_weak_del(pic, weak, key); - } - } else { - pic_weak_set(pic, weak, key, val); - } - - return pic_undef_value(pic); -} - static pic_value weak_call(pic_state *pic) { - struct pic_weak *weak; - pic_value key, val; + pic_value key, val, weak; int n; n = pic_get_args(pic, "o|o", &key, &val); @@ -115,12 +77,22 @@ weak_call(pic_state *pic) pic_errorf(pic, "attempted to set a non-object key '~s' in an ephemeron", key); } - weak = pic_weak_ptr(pic_closure_ref(pic, 0)); + weak = pic_closure_ref(pic, 0); if (n == 1) { - return weak_get(pic, weak, pic_obj_ptr(key)); + if (! pic_weak_has(pic, weak, key)) { + return pic_false_value(pic); + } + return pic_cons(pic, key, pic_weak_ref(pic, weak, key)); } else { - return weak_set(pic, weak, pic_obj_ptr(key), val); + if (pic_undef_p(pic, val)) { + if (pic_weak_has(pic, weak, key)) { + pic_weak_del(pic, weak, key); + } + } else { + pic_weak_set(pic, weak, key, val); + } + return pic_undef_value(pic); } } @@ -129,7 +101,7 @@ pic_weak_make_ephemeron(pic_state *pic) { pic_get_args(pic, ""); - return pic_lambda(pic, weak_call, 1, pic_obj_value(pic_make_weak(pic))); + return pic_lambda(pic, weak_call, 1, pic_make_weak(pic)); } void From 1a316a7a6916c5f060eaf72b19eb3a31232fa33a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 05:18:13 +0900 Subject: [PATCH 060/119] change irep of symbol and identifier --- extlib/benz/bool.c | 6 +++--- extlib/benz/gc.c | 9 ++++----- extlib/benz/include/picrin.h | 4 ++-- extlib/benz/include/picrin/object.h | 15 +++++---------- extlib/benz/macro.c | 4 ++-- extlib/benz/symbol.c | 18 +++++++++--------- 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 87bd6269..5bf320c1 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -94,14 +94,14 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) switch (pic_type(pic, x)) { case PIC_TYPE_ID: { - struct pic_id *id1, *id2; + pic_id *id1, *id2; pic_sym *s1, *s2; id1 = pic_id_ptr(x); id2 = pic_id_ptr(y); - s1 = pic_find_identifier(pic, id1->u.id.id, id1->u.id.env); - s2 = pic_find_identifier(pic, id2->u.id.id, id2->u.id.env); + s1 = pic_find_identifier(pic, id1->u.id, id1->env); + s2 = pic_find_identifier(pic, id2->u.id, id2->env); return s1 == s2; } diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index b903f048..d1e2070e 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -20,7 +20,7 @@ struct heap_page { struct pic_object { union { struct pic_basic basic; - struct pic_symbol sym; + struct pic_identifier id; struct pic_string str; struct pic_blob blob; struct pic_pair pair; @@ -29,7 +29,6 @@ struct pic_object { struct pic_weak weak; struct pic_data data; struct pic_record rec; - struct pic_id id; struct pic_env env; struct pic_proc proc; struct pic_context cxt; @@ -333,8 +332,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TYPE_ID: { - gc_mark_object(pic, (struct pic_object *)obj->u.id.u.id.id); - LOOP(obj->u.id.u.id.env); + gc_mark_object(pic, (struct pic_object *)obj->u.id.u.id); + LOOP(obj->u.id.env); break; } case PIC_TYPE_ENV: { @@ -377,7 +376,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TYPE_SYMBOL: { - LOOP(obj->u.sym.str); + LOOP(obj->u.id.u.str); break; } case PIC_TYPE_WEAK: { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 2b092037..5f9850c2 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -57,8 +57,8 @@ struct pic_port; struct pic_error; struct pic_env; -typedef struct pic_symbol pic_sym; -typedef struct pic_id pic_id; +typedef struct pic_identifier pic_id; +typedef pic_id pic_sym; typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index cbe3e264..f8d18457 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -13,18 +13,13 @@ KHASH_DECLARE(env, pic_id *, pic_sym *) KHASH_DECLARE(dict, pic_sym *, pic_value) KHASH_DECLARE(weak, struct pic_object *, pic_value) -struct pic_id { +struct pic_identifier { + PIC_OBJECT_HEADER union { - struct pic_symbol { - PIC_OBJECT_HEADER - struct pic_string *str; - } sym; - struct { - PIC_OBJECT_HEADER - struct pic_id *id; - struct pic_env *env; - } id; + struct pic_string *str; + struct pic_identifier *id; } u; + struct pic_env *env; }; struct pic_env { diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 486362a1..faad8f45 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -88,8 +88,8 @@ pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) if (pic_sym_p(pic, pic_obj_value(id))) { break; } - env = id->u.id.env; /* do not overwrite id first */ - id = id->u.id.id; + env = id->env; /* do not overwrite id first */ + id = id->u.id; } if (uid == NULL) { while (env->up != NULL) { diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 25d513ac..2db1da56 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -27,8 +27,8 @@ pic_intern(pic_state *pic, pic_value str) kh_val(h, it) = pic->sQUOTE; /* dummy */ - sym = (pic_sym *)pic_obj_alloc(pic, sizeof(pic_sym), PIC_TYPE_SYMBOL); - sym->str = pic_str_ptr(pic, str); + sym = (pic_sym *)pic_obj_alloc(pic, offsetof(pic_sym, env), PIC_TYPE_SYMBOL); + sym->u.str = pic_str_ptr(pic, str); kh_val(h, it) = sym; return sym; @@ -40,22 +40,22 @@ pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env) pic_id *nid; nid = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); - nid->u.id.id = id; - nid->u.id.env = env; + nid->u.id = id; + nid->env = env; return nid; } pic_value pic_sym_name(pic_state PIC_UNUSED(*pic), pic_sym *sym) { - return pic_obj_value(sym->str); + return pic_obj_value(sym->u.str); } pic_value pic_id_name(pic_state *pic, pic_id *id) { while (! pic_sym_p(pic, pic_obj_value(id))) { - id = id->u.id.id; + id = id->u.id; } return pic_sym_name(pic, (pic_sym *)id); @@ -97,7 +97,7 @@ pic_symbol_symbol_to_string(pic_state *pic) pic_get_args(pic, "m", &sym); - return pic_obj_value(sym->str); + return pic_obj_value(sym->u.str); } static pic_value @@ -146,7 +146,7 @@ pic_symbol_identifier_variable(pic_state *pic) pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); } - return pic_obj_value(pic_id_ptr(id)->u.id.id); + return pic_obj_value(pic_id_ptr(id)->u.id); } static pic_value @@ -162,7 +162,7 @@ pic_symbol_identifier_environment(pic_state *pic) pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); } - return pic_obj_value(pic_id_ptr(id)->u.id.env); + return pic_obj_value(pic_id_ptr(id)->env); } static pic_value From f4efaf5dc042d5261f5fc4a0eae9113002cc93e6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 15:31:14 +0900 Subject: [PATCH 061/119] pic_sym * -> pic_value --- extlib/benz/bool.c | 4 +- extlib/benz/debug.c | 2 +- extlib/benz/dict.c | 63 ++++--- extlib/benz/error.c | 5 +- extlib/benz/eval.c | 245 +++++++++++++--------------- extlib/benz/gc.c | 9 +- extlib/benz/include/picrin.h | 17 +- extlib/benz/include/picrin/object.h | 8 +- extlib/benz/include/picrin/state.h | 14 +- extlib/benz/lib.c | 54 +++--- extlib/benz/macro.c | 110 ++++++------- extlib/benz/proc.c | 50 +++--- extlib/benz/read.c | 32 ++-- extlib/benz/state.c | 11 +- extlib/benz/symbol.c | 20 +-- extlib/benz/write.c | 25 ++- 16 files changed, 317 insertions(+), 352 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 5bf320c1..1a1a4310 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -95,7 +95,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) switch (pic_type(pic, x)) { case PIC_TYPE_ID: { pic_id *id1, *id2; - pic_sym *s1, *s2; + pic_value s1, s2; id1 = pic_id_ptr(x); id2 = pic_id_ptr(y); @@ -103,7 +103,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) s1 = pic_find_identifier(pic, id1->u.id, id1->env); s2 = pic_find_identifier(pic, id2->u.id, id2->env); - return s1 == s2; + return pic_eq_p(pic, s1, s2); } case PIC_TYPE_STRING: { return pic_str_cmp(pic, x, y) == 0; diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 85389535..6ba134c9 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -46,7 +46,7 @@ pic_print_backtrace(pic_state *pic, xFILE *file) pic_value elem, it; e = pic_error_ptr(pic->err); - if (e->type != pic_intern_lit(pic, "")) { + if (! pic_eq_p(pic, pic_obj_value(e->type), pic_intern_lit(pic, ""))) { pic_fwrite(pic, pic_obj_value(e->type), file); xfprintf(pic, file, " "); } diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 301d0c4b..8675d5e9 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -18,26 +18,26 @@ pic_make_dict(pic_state *pic) } pic_value -pic_dict_ref(pic_state *pic, pic_value dict, pic_sym *key) +pic_dict_ref(pic_state *pic, pic_value dict, pic_value key) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; khiter_t it; - it = kh_get(dict, h, key); + it = kh_get(dict, h, pic_sym_ptr(pic, key)); if (it == kh_end(h)) { - pic_errorf(pic, "element not found for a key: ~s", pic_obj_value(key)); + pic_errorf(pic, "element not found for a key: ~s", key); } return kh_val(h, it); } void -pic_dict_set(pic_state *pic, pic_value dict, pic_sym *key, pic_value val) +pic_dict_set(pic_state *pic, pic_value dict, pic_value key, pic_value val) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int ret; khiter_t it; - it = kh_put(dict, h, key, &ret); + it = kh_put(dict, h, pic_sym_ptr(pic, key), &ret); kh_val(h, it) = val; } @@ -48,35 +48,35 @@ pic_dict_size(pic_state PIC_UNUSED(*pic), pic_value dict) } bool -pic_dict_has(pic_state *pic, pic_value dict, pic_sym *key) +pic_dict_has(pic_state *pic, pic_value dict, pic_value key) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; - return kh_get(dict, h, key) != kh_end(h); + return kh_get(dict, h, pic_sym_ptr(pic, key)) != kh_end(h); } void -pic_dict_del(pic_state *pic, pic_value dict, pic_sym *key) +pic_dict_del(pic_state *pic, pic_value dict, pic_value key) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; khiter_t it; - it = kh_get(dict, h, key); + it = kh_get(dict, h, pic_sym_ptr(pic, key)); if (it == kh_end(h)) { - pic_errorf(pic, "no slot named ~s found in dictionary", pic_obj_value(key)); + pic_errorf(pic, "no slot named ~s found in dictionary", key); } kh_del(dict, h, it); } bool -pic_dict_next(pic_state PIC_UNUSED(*pic), pic_value dict, int *iter, pic_sym **key, pic_value *val) +pic_dict_next(pic_state PIC_UNUSED(*pic), pic_value dict, int *iter, pic_value *key, pic_value *val) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int it = *iter; for (it = *iter; it != kh_end(h); ++it) { if (kh_exist(h, it)) { - if (key) *key = kh_key(h, it); + if (key) *key = pic_obj_value(kh_key(h, it)); if (val) *val = kh_val(h, it); *iter = ++it; return true; @@ -105,7 +105,7 @@ pic_dict_dictionary(pic_state *pic) for (i = 0; i < argc; i += 2) { pic_assert_type(pic, argv[i], sym); - pic_dict_set(pic, dict, pic_sym_ptr(argv[i]), argv[i+1]); + pic_dict_set(pic, dict, argv[i], argv[i+1]); } return dict; @@ -124,23 +124,20 @@ pic_dict_dictionary_p(pic_state *pic) static pic_value pic_dict_dictionary_ref(pic_state *pic) { - pic_value dict; - pic_sym *key; + pic_value dict, key; pic_get_args(pic, "dm", &dict, &key); if (! pic_dict_has(pic, dict, key)) { return pic_false_value(pic); } - return pic_cons(pic, pic_obj_value(key), pic_dict_ref(pic, dict, key)); + return pic_cons(pic, key, pic_dict_ref(pic, dict, key)); } static pic_value pic_dict_dictionary_set(pic_state *pic) { - pic_value dict; - pic_sym *key; - pic_value val; + pic_value dict, key, val; pic_get_args(pic, "dmo", &dict, &key, &val); @@ -168,14 +165,13 @@ pic_dict_dictionary_size(pic_state *pic) static pic_value pic_dict_dictionary_map(pic_state *pic) { - pic_value dict, proc, ret = pic_nil_value(pic); - pic_sym *key; + pic_value dict, proc, key, ret = pic_nil_value(pic); int it = 0; pic_get_args(pic, "ld", &proc, &dict); while (pic_dict_next(pic, dict, &it, &key, NULL)) { - pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(key)), ret); + pic_push(pic, pic_call(pic, proc, 1, key), ret); } return pic_reverse(pic, ret); } @@ -183,14 +179,13 @@ pic_dict_dictionary_map(pic_state *pic) static pic_value pic_dict_dictionary_for_each(pic_state *pic) { - pic_value dict, proc; - pic_sym *key; + pic_value dict, proc, key; int it; pic_get_args(pic, "ld", &proc, &dict); while (pic_dict_next(pic, dict, &it, &key, NULL)) { - pic_call(pic, proc, 1, pic_obj_value(key)); + pic_call(pic, proc, 1, key); } return pic_undef_value(pic); @@ -199,14 +194,13 @@ pic_dict_dictionary_for_each(pic_state *pic) static pic_value pic_dict_dictionary_to_alist(pic_state *pic) { - pic_value dict, val, alist = pic_nil_value(pic); - pic_sym *sym; + pic_value dict, key, val, alist = pic_nil_value(pic); int it = 0; pic_get_args(pic, "d", &dict); - while (pic_dict_next(pic, dict, &it, &sym, &val)) { - pic_push(pic, pic_cons(pic, pic_obj_value(sym), val), alist); + while (pic_dict_next(pic, dict, &it, &key, &val)) { + pic_push(pic, pic_cons(pic, key, val), alist); } return alist; @@ -223,7 +217,7 @@ pic_dict_alist_to_dictionary(pic_state *pic) pic_for_each (e, pic_reverse(pic, alist), it) { pic_assert_type(pic, pic_car(pic, e), sym); - pic_dict_set(pic, dict, pic_sym_ptr(pic_car(pic, e)), pic_cdr(pic, e)); + pic_dict_set(pic, dict, pic_car(pic, e), pic_cdr(pic, e)); } return dict; @@ -232,15 +226,14 @@ pic_dict_alist_to_dictionary(pic_state *pic) static pic_value pic_dict_dictionary_to_plist(pic_state *pic) { - pic_value dict, val, plist = pic_nil_value(pic); - pic_sym *sym; + pic_value dict, key, val, plist = pic_nil_value(pic); int it = 0; pic_get_args(pic, "d", &dict); - while (pic_dict_next(pic, dict, &it, &sym, &val)) { + while (pic_dict_next(pic, dict, &it, &key, &val)) { pic_push(pic, val, plist); - pic_push(pic, pic_obj_value(sym), plist); + pic_push(pic, key, plist); } return plist; @@ -257,7 +250,7 @@ pic_dict_plist_to_dictionary(pic_state *pic) for (e = pic_reverse(pic, plist); ! pic_nil_p(pic, e); e = pic_cddr(pic, e)) { pic_assert_type(pic, pic_cadr(pic, e), sym); - pic_dict_set(pic, dict, pic_sym_ptr(pic_cadr(pic, e)), pic_car(pic, e)); + pic_dict_set(pic, dict, pic_cadr(pic, e), pic_car(pic, e)); } return dict; diff --git a/extlib/benz/error.c b/extlib/benz/error.c index e17c41a5..dff5d0e5 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -92,13 +92,12 @@ struct pic_error * pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; - pic_value stack; - pic_sym *ty = pic_intern_cstr(pic, type); + pic_value stack, ty = pic_intern_cstr(pic, type); stack = pic_get_backtrace(pic); e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); - e->type = ty; + e->type = pic_sym_ptr(pic, ty); e->msg = pic_str_ptr(pic, pic_cstr_value(pic, msg)); e->irrs = irrs; e->stack = pic_str_ptr(pic, stack); diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 248b67f9..d8b86146 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -19,12 +19,12 @@ optimize_beta(pic_state *pic, pic_value expr) return expr; if (pic_sym_p(pic, pic_list_ref(pic, expr, 0))) { - pic_sym *sym = pic_sym_ptr(pic_list_ref(pic, expr, 0)); + pic_value sym = pic_list_ref(pic, expr, 0); - if (sym == pic->sQUOTE) { + if (pic_eq_p(pic, sym, pic->sQUOTE)) { return expr; - } else if (sym == pic->sLAMBDA) { - 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))); + } else if (pic_eq_p(pic, sym, pic->sLAMBDA)) { + return pic_list(pic, 3, pic->sLAMBDA, pic_list_ref(pic, expr, 1), optimize_beta(pic, pic_list_ref(pic, expr, 2))); } } @@ -38,7 +38,7 @@ optimize_beta(pic_state *pic, pic_value expr) pic_protect(pic, expr); functor = pic_list_ref(pic, expr, 0); - if (pic_pair_p(pic, functor) && pic_eq_p(pic, pic_car(pic, functor), pic_obj_value(pic->sLAMBDA))) { + if (pic_pair_p(pic, functor) && pic_eq_p(pic, pic_car(pic, functor), pic->sLAMBDA)) { formals = pic_list_ref(pic, functor, 1); if (! pic_list_p(pic, formals)) goto exit; /* TODO: support ((lambda args x) 1 2) */ @@ -47,12 +47,12 @@ optimize_beta(pic_state *pic, pic_value expr) goto exit; defs = pic_nil_value(pic); pic_for_each (val, args, it) { - pic_push(pic, pic_list(pic, 3, pic_obj_value(pic->sDEFINE), pic_car(pic, formals), val), defs); + pic_push(pic, pic_list(pic, 3, pic->sDEFINE, pic_car(pic, formals), val), defs); formals = pic_cdr(pic, formals); } expr = pic_list_ref(pic, functor, 2); pic_for_each (val, defs, it) { - expr = pic_list(pic, 3, pic_obj_value(pic->sBEGIN), val, expr); + expr = pic_list(pic, 3, pic->sBEGIN, val, expr); } } exit: @@ -68,17 +68,14 @@ pic_optimize(pic_state *pic, pic_value expr) return optimize_beta(pic, expr); } -KHASH_DECLARE(a, pic_sym *, int) -KHASH_DEFINE2(a, pic_sym *, int, 0, kh_ptr_hash_func, kh_ptr_hash_equal) - /** * TODO: don't use khash_t, use kvec_t instead */ typedef struct analyze_scope { int depth; - pic_sym *rest; /* Nullable */ - khash_t(a) args, locals, captures; /* rest args variable is counted as a local */ + pic_value rest; /* Nullable */ + pic_value args, locals, captures; /* rest args variable is counted as a local */ pic_value defer; struct analyze_scope *up; } analyze_scope; @@ -86,22 +83,20 @@ typedef struct analyze_scope { static void analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, analyze_scope *up) { - int ret; - - kh_init(a, &scope->args); - kh_init(a, &scope->locals); - kh_init(a, &scope->captures); + scope->args = pic_make_dict(pic); + scope->locals = pic_make_dict(pic); + scope->captures = pic_make_dict(pic); /* analyze formal */ for (; pic_pair_p(pic, formal); formal = pic_cdr(pic, formal)) { - kh_put(a, &scope->args, pic_sym_ptr(pic_car(pic, formal)), &ret); + pic_dict_set(pic, scope->args, pic_car(pic, formal), pic_true_value(pic)); } if (pic_nil_p(pic, formal)) { - scope->rest = NULL; + scope->rest = pic_false_value(pic); } else { - scope->rest = pic_sym_ptr(formal); - kh_put(a, &scope->locals, pic_sym_ptr(formal), &ret); + scope->rest = formal; + pic_dict_set(pic, scope->locals, formal, pic_true_value(pic)); } scope->up = up; @@ -110,28 +105,26 @@ analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, anal } static void -analyzer_scope_destroy(pic_state *pic, analyze_scope *scope) +analyzer_scope_destroy(pic_state PIC_UNUSED(*pic), analyze_scope PIC_UNUSED(*scope)) { - kh_destroy(a, &scope->args); - kh_destroy(a, &scope->locals); - kh_destroy(a, &scope->captures); + /* nothing here */ } static bool -search_scope(pic_state *pic, analyze_scope *scope, pic_sym *sym) +search_scope(pic_state *pic, analyze_scope *scope, pic_value sym) { - return kh_get(a, &scope->args, sym) != kh_end(&scope->args) || kh_get(a, &scope->locals, sym) != kh_end(&scope->locals) || scope->depth == 0; + return pic_dict_has(pic, scope->args, sym) || pic_dict_has(pic, scope->locals, sym) || scope->depth == 0; } static int -find_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) +find_var(pic_state *pic, analyze_scope *scope, pic_value sym) { - int depth = 0, ret; + int depth = 0; while (scope) { if (search_scope(pic, scope, sym)) { if (depth > 0) { - kh_put(a, &scope->captures, sym, &ret); /* capture! */ + pic_dict_set(pic, scope->captures, sym, pic_true_value(pic)); /* capture! */ } return depth; } @@ -142,20 +135,18 @@ find_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) } static void -define_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) +define_var(pic_state *pic, analyze_scope *scope, pic_value sym) { - int ret; - if (search_scope(pic, scope, sym)) { - if (scope->depth > 0 || pic_weak_has(pic, pic->globals, pic_obj_value(sym))) { - pic_warnf(pic, "redefining variable: ~s", pic_obj_value(sym)); + if (scope->depth > 0 || pic_weak_has(pic, pic->globals, sym)) { + pic_warnf(pic, "redefining variable: ~s", sym); } return; } - pic_weak_set(pic, pic->globals, pic_obj_value(sym), pic_invalid_value()); + pic_weak_set(pic, pic->globals, sym, pic_invalid_value()); - kh_put(a, &scope->locals, sym, &ret); + pic_dict_set(pic, scope->locals, sym, pic_true_value(pic)); } static pic_value analyze(pic_state *, analyze_scope *, pic_value); @@ -167,18 +158,18 @@ static pic_value analyze_lambda(pic_state *, analyze_scope *, pic_value); #define CALL pic_intern_lit(pic, "call") static pic_value -analyze_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) +analyze_var(pic_state *pic, analyze_scope *scope, pic_value sym) { int depth; depth = find_var(pic, scope, sym); if (depth == scope->depth) { - return pic_list(pic, 2, pic_obj_value(GREF), pic_obj_value(sym)); + return pic_list(pic, 2, GREF, sym); } else if (depth == 0) { - return pic_list(pic, 2, pic_obj_value(LREF), pic_obj_value(sym)); + return pic_list(pic, 2, LREF, sym); } else { - return pic_list(pic, 3, pic_obj_value(CREF), pic_int_value(pic, depth), pic_obj_value(sym)); + return pic_list(pic, 3, CREF, pic_int_value(pic, depth), sym); } } @@ -216,10 +207,9 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) { analyze_scope s, *scope = &s; pic_value formals, body; - pic_value rest = pic_undef_value(pic); - pic_value args, locals, captures; - int i, j; - khiter_t it; + pic_value rest; + pic_value args, locals, captures, key; + int i, j, it; formals = pic_list_ref(pic, form, 1); body = pic_list_ref(pic, form, 2); @@ -230,38 +220,35 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) body = analyze(pic, scope, body); analyze_deferred(pic, scope); - args = pic_make_vec(pic, kh_size(&scope->args), NULL); + args = pic_make_vec(pic, pic_dict_size(pic, scope->args), NULL); for (i = 0; pic_pair_p(pic, formals); formals = pic_cdr(pic, formals), i++) { pic_vec_set(pic, args, i, pic_car(pic, formals)); } - if (scope->rest != NULL) { - rest = pic_obj_value(scope->rest); - } + rest = scope->rest; - locals = pic_make_vec(pic, kh_size(&scope->locals), NULL); + locals = pic_make_vec(pic, pic_dict_size(pic, scope->locals), NULL); j = 0; - if (scope->rest != NULL) { - pic_vec_set(pic, locals, j++, pic_obj_value(scope->rest)); + if (pic_sym_p(pic, scope->rest)) { + pic_vec_set(pic, locals, j++, scope->rest); } - for (it = kh_begin(&scope->locals); it < kh_end(&scope->locals); ++it) { - if (kh_exist(&scope->locals, it)) { - if (scope->rest != NULL && kh_key(&scope->locals, it) == scope->rest) - continue; - pic_vec_set(pic, locals, j++, pic_obj_value(kh_key(&scope->locals, it))); - } + it = 0; + while (pic_dict_next(pic, scope->locals, &it, &key, NULL)) { + if (pic_eq_p(pic, key, rest)) + continue; + pic_vec_set(pic, locals, j++, key); } - captures = pic_make_vec(pic, kh_size(&scope->captures), NULL); - for (it = kh_begin(&scope->captures), j = 0; it < kh_end(&scope->captures); ++it) { - if (kh_exist(&scope->captures, it)) { - pic_vec_set(pic, captures, j++, pic_obj_value(kh_key(&scope->captures, it))); - } + captures = pic_make_vec(pic, pic_dict_size(pic, scope->captures), NULL); + it = 0; + j = 0; + while (pic_dict_next(pic, scope->captures, &it, &key, NULL)) { + pic_vec_set(pic, captures, j++, key); } analyzer_scope_destroy(pic, scope); - return pic_list(pic, 6, pic_obj_value(pic->sLAMBDA), rest, args, locals, captures, body); + return pic_list(pic, 6, pic->sLAMBDA, rest, args, locals, captures, body); } static pic_value @@ -279,7 +266,7 @@ analyze_list(pic_state *pic, analyze_scope *scope, pic_value obj) static pic_value analyze_define(pic_state *pic, analyze_scope *scope, pic_value obj) { - define_var(pic, scope, pic_sym_ptr(pic_list_ref(pic, obj, 1))); + define_var(pic, scope, pic_list_ref(pic, obj, 1)); return pic_cons(pic, pic_car(pic, obj), analyze_list(pic, scope, pic_cdr(pic, obj))); } @@ -287,7 +274,7 @@ analyze_define(pic_state *pic, analyze_scope *scope, pic_value obj) static pic_value analyze_call(pic_state *pic, analyze_scope *scope, pic_value obj) { - return pic_cons(pic, pic_obj_value(CALL), analyze_list(pic, scope, obj)); + return pic_cons(pic, CALL, analyze_list(pic, scope, obj)); } static pic_value @@ -295,7 +282,7 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) { switch (pic_type(pic, obj)) { case PIC_TYPE_SYMBOL: { - return analyze_var(pic, scope, pic_sym_ptr(obj)); + return analyze_var(pic, scope, obj); } case PIC_TYPE_PAIR: { pic_value proc; @@ -306,18 +293,18 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) proc = pic_list_ref(pic, obj, 0); if (pic_sym_p(pic, proc)) { - pic_sym *sym = pic_sym_ptr(proc); + pic_value sym = proc; - if (sym == pic->sDEFINE) { + if (pic_eq_p(pic, sym, pic->sDEFINE)) { return analyze_define(pic, scope, obj); } - else if (sym == pic->sLAMBDA) { + else if (pic_eq_p(pic, sym, pic->sLAMBDA)) { return analyze_defer(pic, scope, obj); } - else if (sym == pic->sQUOTE) { + else if (pic_eq_p(pic, sym, pic->sQUOTE)) { return obj; } - else if (sym == pic->sBEGIN || sym == pic->sSETBANG || sym == pic->sIF) { + else if (pic_eq_p(pic, sym, pic->sBEGIN) || pic_eq_p(pic, sym, pic->sSETBANG) || pic_eq_p(pic, sym, pic->sIF)) { return pic_cons(pic, pic_car(pic, obj), analyze_list(pic, scope, pic_cdr(pic, obj))); } } @@ -325,7 +312,7 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) return analyze_call(pic, scope, obj); } default: - return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), obj); + return pic_list(pic, 2, pic->sQUOTE, obj); } } @@ -359,7 +346,7 @@ pic_analyze(pic_state *pic, pic_value obj) typedef struct codegen_context { /* rest args variable is counted as a local */ - pic_sym *rest; + pic_value rest; pic_value args, locals, captures; /* actual bit code sequence */ pic_code *code; @@ -381,7 +368,7 @@ typedef struct codegen_context { static void create_activation(pic_state *, codegen_context *); static void -codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, pic_sym *rest, pic_value args, pic_value locals, pic_value captures) +codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, pic_value rest, pic_value args, pic_value locals, pic_value captures) { cxt->up = up; cxt->rest = rest; @@ -421,7 +408,7 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) /* create irep */ irep = pic_malloc(pic, sizeof(struct pic_irep)); irep->refc = 1; - irep->varg = cxt->rest != NULL; + irep->varg = pic_sym_p(pic, cxt->rest); irep->argc = pic_vec_len(pic, cxt->args) + 1; irep->localc = pic_vec_len(pic, cxt->locals); irep->capturec = pic_vec_len(pic, cxt->captures); @@ -481,7 +468,7 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) #define emit_ret(pic, cxt, tailpos) if (tailpos) emit_n(pic, cxt, OP_RET) static int -index_capture(pic_state *pic, codegen_context *cxt, pic_sym *sym, int depth) +index_capture(pic_state *pic, codegen_context *cxt, pic_value sym, int depth) { int i; @@ -490,38 +477,38 @@ index_capture(pic_state *pic, codegen_context *cxt, pic_sym *sym, int depth) } for (i = 0; i < pic_vec_len(pic, cxt->captures); ++i) { - if (pic_sym_ptr(pic_vec_ref(pic, cxt->captures, i)) == sym) + if (pic_eq_p(pic, sym, pic_vec_ref(pic, cxt->captures, i))) return i; } return -1; } static int -index_local(pic_state *pic, codegen_context *cxt, pic_sym *sym) +index_local(pic_state *pic, codegen_context *cxt, pic_value sym) { int i, offset; offset = 1; for (i = 0; i < pic_vec_len(pic, cxt->args); ++i) { - if (pic_sym_ptr(pic_vec_ref(pic, cxt->args, i)) == sym) + if (pic_eq_p(pic, sym, pic_vec_ref(pic, cxt->args, i))) return i + offset; } offset += i; for (i = 0; i < pic_vec_len(pic, cxt->locals); ++i) { - if (pic_sym_ptr(pic_vec_ref(pic, cxt->locals, i)) == sym) + if (pic_eq_p(pic, sym, pic_vec_ref(pic, cxt->locals, i))) return i + offset; } return -1; } static int -index_global(pic_state *pic, codegen_context *cxt, pic_sym *name) +index_global(pic_state *pic, codegen_context *cxt, pic_value name) { int pidx; check_pool_size(pic, cxt); pidx = (int)cxt->plen++; - cxt->pool[pidx] = (struct pic_object *)name; + cxt->pool[pidx] = (struct pic_object *)pic_sym_ptr(pic, name); return pidx; } @@ -532,10 +519,10 @@ create_activation(pic_state *pic, codegen_context *cxt) int i, n; for (i = 0; i < pic_vec_len(pic, cxt->captures); ++i) { - pic_sym *sym = pic_sym_ptr(pic_vec_ref(pic, cxt->captures, i)); + pic_value sym = pic_vec_ref(pic, cxt->captures, i); n = index_local(pic, cxt, sym); assert(n != -1); - if (n <= pic_vec_len(pic, cxt->args) || cxt->rest == sym) { + if (n <= pic_vec_len(pic, cxt->args) || pic_eq_p(pic, sym, cxt->rest)) { /* copy arguments to capture variable area */ emit_i(pic, cxt, OP_LREF, n); } else { @@ -550,30 +537,30 @@ static void codegen(pic_state *, codegen_context *, pic_value, bool); static void codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) { - pic_sym *sym; + pic_value sym; - sym = pic_sym_ptr(pic_car(pic, obj)); - if (sym == GREF) { - pic_sym *name; + sym = pic_car(pic, obj); + if (pic_eq_p(pic, sym, GREF)) { + pic_value name; - name = pic_sym_ptr(pic_list_ref(pic, obj, 1)); + name = pic_list_ref(pic, obj, 1); emit_i(pic, cxt, OP_GREF, index_global(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } - else if (sym == CREF) { - pic_sym *name; + else if (pic_eq_p(pic, sym, CREF)) { + pic_value name; int depth; depth = pic_int(pic, pic_list_ref(pic, obj, 1)); - name = pic_sym_ptr(pic_list_ref(pic, obj, 2)); + name = pic_list_ref(pic, obj, 2); emit_r(pic, cxt, OP_CREF, depth, index_capture(pic, cxt, name, depth)); emit_ret(pic, cxt, tailpos); } - else if (sym == LREF) { - pic_sym *name; + else if (pic_eq_p(pic, sym, LREF)) { + pic_value name; int i; - name = pic_sym_ptr(pic_list_ref(pic, obj, 1)); + name = pic_list_ref(pic, obj, 1); if ((i = index_capture(pic, cxt, name, 0)) != -1) { emit_i(pic, cxt, OP_LREF, i + pic_vec_len(pic, cxt->args) + pic_vec_len(pic, cxt->locals) + 1); emit_ret(pic, cxt, tailpos); @@ -588,34 +575,34 @@ static void codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) { pic_value var, val; - pic_sym *type; + pic_value type; val = pic_list_ref(pic, obj, 2); codegen(pic, cxt, val, false); var = pic_list_ref(pic, obj, 1); - type = pic_sym_ptr(pic_list_ref(pic, var, 0)); - if (type == GREF) { - pic_sym *name; + type = pic_list_ref(pic, var, 0); + if (pic_eq_p(pic, type, GREF)) { + pic_value name; - name = pic_sym_ptr(pic_list_ref(pic, var, 1)); + name = pic_list_ref(pic, var, 1); emit_i(pic, cxt, OP_GSET, index_global(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } - else if (type == CREF) { - pic_sym *name; + else if (pic_eq_p(pic, type, CREF)) { + pic_value name; int depth; depth = pic_int(pic, pic_list_ref(pic, var, 1)); - name = pic_sym_ptr(pic_list_ref(pic, var, 2)); + name = pic_list_ref(pic, var, 2); emit_r(pic, cxt, OP_CSET, depth, index_capture(pic, cxt, name, depth)); emit_ret(pic, cxt, tailpos); } - else if (type == LREF) { - pic_sym *name; + else if (pic_eq_p(pic, type, LREF)) { + pic_value name; int i; - name = pic_sym_ptr(pic_list_ref(pic, var, 1)); + name = pic_list_ref(pic, var, 1); if ((i = index_capture(pic, cxt, name, 0)) != -1) { emit_i(pic, cxt, OP_LSET, i + pic_vec_len(pic, cxt->args) + pic_vec_len(pic, cxt->locals) + 1); emit_ret(pic, cxt, tailpos); @@ -630,17 +617,13 @@ static void codegen_lambda(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) { codegen_context c, *inner_cxt = &c; - pic_value rest_opt, body; - pic_sym *rest = NULL; + pic_value rest, body; pic_value args, locals, captures; check_irep_size(pic, cxt); /* extract arguments */ - rest_opt = pic_list_ref(pic, obj, 1); - if (pic_sym_p(pic, rest_opt)) { - rest = pic_sym_ptr(rest_opt); - } + rest = pic_list_ref(pic, obj, 1); args = pic_list_ref(pic, obj, 2); locals = pic_list_ref(pic, obj, 3); captures = pic_list_ref(pic, obj, 4); @@ -741,11 +724,11 @@ codegen_quote(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) } #define VM(uid, op) \ - if (sym == uid) { \ - emit_i(pic, cxt, op, len - 1); \ - emit_ret(pic, cxt, tailpos); \ - return; \ - } + if (pic_eq_p(pic, sym, uid)) { \ + emit_i(pic, cxt, op, len - 1); \ + emit_ret(pic, cxt, tailpos); \ + return; \ + } static void codegen_call(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) @@ -758,10 +741,10 @@ codegen_call(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) } functor = pic_list_ref(pic, obj, 1); - if (pic_sym_ptr(pic_list_ref(pic, functor, 0)) == GREF) { - pic_sym *sym; + if (pic_eq_p(pic, pic_list_ref(pic, functor, 0), GREF)) { + pic_value sym; - sym = pic_sym_ptr(pic_list_ref(pic, functor, 1)); + sym = pic_list_ref(pic, functor, 1); VM(pic->sCONS, OP_CONS) VM(pic->sCAR, OP_CAR) @@ -787,28 +770,28 @@ codegen_call(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) static void codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) { - pic_sym *sym; + pic_value sym; - sym = pic_sym_ptr(pic_car(pic, obj)); - if (sym == GREF || sym == CREF || sym == LREF) { + sym = pic_car(pic, obj); + if (pic_eq_p(pic, sym, GREF) || pic_eq_p(pic, sym, CREF) || pic_eq_p(pic, sym, LREF)) { codegen_ref(pic, cxt, obj, tailpos); } - else if (sym == pic->sSETBANG || sym == pic->sDEFINE) { + else if (pic_eq_p(pic, sym, pic->sSETBANG) || pic_eq_p(pic, sym, pic->sDEFINE)) { codegen_set(pic, cxt, obj, tailpos); } - else if (sym == pic->sLAMBDA) { + else if (pic_eq_p(pic, sym, pic->sLAMBDA)) { codegen_lambda(pic, cxt, obj, tailpos); } - else if (sym == pic->sIF) { + else if (pic_eq_p(pic, sym, pic->sIF)) { codegen_if(pic, cxt, obj, tailpos); } - else if (sym == pic->sBEGIN) { + else if (pic_eq_p(pic, sym, pic->sBEGIN)) { codegen_begin(pic, cxt, obj, tailpos); } - else if (sym == pic->sQUOTE) { + else if (pic_eq_p(pic, sym, pic->sQUOTE)) { codegen_quote(pic, cxt, obj, tailpos); } - else if (sym == CALL) { + else if (pic_eq_p(pic, sym, CALL)) { codegen_call(pic, cxt, obj, tailpos); } else { @@ -822,7 +805,7 @@ pic_codegen(pic_state *pic, pic_value obj) pic_value empty = pic_make_vec(pic, 0, NULL); codegen_context c, *cxt = &c; - codegen_context_init(pic, cxt, NULL, NULL, empty, empty, empty); + codegen_context_init(pic, cxt, NULL, pic_false_value(pic), empty, empty, empty); codegen(pic, cxt, obj, true); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index d1e2070e..fa35d5be 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -358,12 +358,11 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TYPE_DICT: { - pic_sym *sym; - pic_value val; + pic_value key, val; int it = 0; - while (pic_dict_next(pic, pic_obj_value(&obj->u.dict), &it, &sym, &val)) { - gc_mark_object(pic, (struct pic_object *)sym); + while (pic_dict_next(pic, pic_obj_value(&obj->u.dict), &it, &key, &val)) { + gc_mark(pic, key); gc_mark(pic, val); } break; @@ -411,7 +410,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } } -#define M(x) gc_mark_object(pic, (struct pic_object *)pic->x) +#define M(x) gc_mark(pic, pic->x) static void gc_mark_phase(pic_state *pic) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 5f9850c2..51b0c8f5 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -52,7 +52,6 @@ typedef struct { #endif struct pic_object; -struct pic_symbol; struct pic_port; struct pic_error; struct pic_env; @@ -102,7 +101,7 @@ void pic_in_library(pic_state *, const char *lib); bool pic_find_library(pic_state *, const char *lib); const char *pic_current_library(pic_state *); void pic_import(pic_state *, const char *lib); -void pic_export(pic_state *, pic_sym *sym); +void pic_export(pic_state *, pic_value sym); PIC_NORETURN void pic_panic(pic_state *, const char *msg); PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); @@ -234,12 +233,12 @@ int pic_vec_len(pic_state *, pic_value vec); /* dictionary */ pic_value pic_make_dict(pic_state *); -pic_value pic_dict_ref(pic_state *, pic_value dict, pic_sym *); -void pic_dict_set(pic_state *, pic_value dict, pic_sym *, pic_value); -void pic_dict_del(pic_state *, pic_value dict, pic_sym *); -bool pic_dict_has(pic_state *, pic_value dict, pic_sym *); +pic_value pic_dict_ref(pic_state *, pic_value dict, pic_value key); +void pic_dict_set(pic_state *, pic_value dict, pic_value key, pic_value); +void pic_dict_del(pic_state *, pic_value dict, pic_value key); +bool pic_dict_has(pic_state *, pic_value dict, pic_value key); int pic_dict_size(pic_state *, pic_value dict); -bool pic_dict_next(pic_state *, pic_value dict, int *iter, pic_sym **key, pic_value *val); +bool pic_dict_next(pic_state *, pic_value dict, int *iter, pic_value *key, pic_value *val); /* ephemeron */ pic_value pic_make_weak(pic_state *); @@ -249,11 +248,11 @@ void pic_weak_del(pic_state *, pic_value weak, pic_value key); bool pic_weak_has(pic_state *, pic_value weak, pic_value key); /* symbol */ -pic_sym *pic_intern(pic_state *, pic_value str); +pic_value pic_intern(pic_state *, pic_value str); #define pic_intern_str(pic,s,i) pic_intern(pic, pic_str_value(pic, (s), (i))) #define pic_intern_cstr(pic,s) pic_intern(pic, pic_cstr_value(pic, (s))) #define pic_intern_lit(pic,lit) pic_intern(pic, pic_lit_value(pic, lit)) -pic_value pic_sym_name(pic_state *, pic_sym *); +pic_value pic_sym_name(pic_state *, pic_value sym); /* string */ int pic_str_len(pic_state *, pic_value str); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index f8d18457..953b73d5 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -115,6 +115,7 @@ struct pic_port { xFILE *file; }; +#define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o)) #define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_blob_ptr(pic, o) ((struct pic_blob *)pic_obj_ptr(o)) #define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) @@ -123,7 +124,6 @@ struct pic_port { #define pic_weak_ptr(pic, o) ((struct pic_weak *)pic_obj_ptr(o)) #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) -#define pic_sym_ptr(v) ((pic_sym *)pic_obj_ptr(v)) #define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) @@ -158,9 +158,9 @@ struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); struct pic_env *pic_make_env(pic_state *, struct pic_env *); -pic_sym *pic_add_identifier(pic_state *, pic_id *, struct pic_env *); -pic_sym *pic_put_identifier(pic_state *, pic_id *, pic_sym *, struct pic_env *); -pic_sym *pic_find_identifier(pic_state *, pic_id *, struct pic_env *); +pic_value pic_add_identifier(pic_state *, pic_id *, struct pic_env *); +pic_value pic_put_identifier(pic_state *, pic_id *, pic_value uid, struct pic_env *); +pic_value pic_find_identifier(pic_state *, pic_id *, struct pic_env *); pic_value pic_id_name(pic_state *, pic_id *); void pic_rope_incref(pic_state *, struct pic_rope *); diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 30220021..a3e9ce88 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -67,13 +67,13 @@ struct pic_state { struct pic_lib *lib; - pic_sym *sDEFINE, *sDEFINE_MACRO, *sLAMBDA, *sIF, *sBEGIN, *sSETBANG; - pic_sym *sQUOTE, *sQUASIQUOTE, *sUNQUOTE, *sUNQUOTE_SPLICING; - pic_sym *sSYNTAX_QUOTE, *sSYNTAX_QUASIQUOTE; - pic_sym *sSYNTAX_UNQUOTE, *sSYNTAX_UNQUOTE_SPLICING; - pic_sym *sDEFINE_LIBRARY, *sIMPORT, *sEXPORT, *sCOND_EXPAND; - pic_sym *sCONS, *sCAR, *sCDR, *sNILP, *sSYMBOLP, *sPAIRP; - pic_sym *sADD, *sSUB, *sMUL, *sDIV, *sEQ, *sLT, *sLE, *sGT, *sGE, *sNOT; + pic_value sDEFINE, sDEFINE_MACRO, sLAMBDA, sIF, sBEGIN, sSETBANG; + pic_value sQUOTE, sQUASIQUOTE, sUNQUOTE, sUNQUOTE_SPLICING; + pic_value sSYNTAX_QUOTE, sSYNTAX_QUASIQUOTE; + pic_value sSYNTAX_UNQUOTE, sSYNTAX_UNQUOTE_SPLICING; + pic_value sDEFINE_LIBRARY, sIMPORT, sEXPORT, sCOND_EXPAND; + pic_value sCONS, sCAR, sCDR, sNILP, sSYMBOLP, sPAIRP; + pic_value sADD, sSUB, sMUL, sDIV, sEQ, sLT, sLE, sGT, sGE, sNOT; pic_value features; diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index bd315e2d..73e29c11 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -42,10 +42,10 @@ make_library_env(pic_state *pic, pic_value name) kh_init(env, &env->map); /* set up default environment */ - pic_put_identifier(pic, (pic_id *)pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, env); - pic_put_identifier(pic, (pic_id *)pic->sIMPORT, pic->sIMPORT, env); - pic_put_identifier(pic, (pic_id *)pic->sEXPORT, pic->sEXPORT, env); - pic_put_identifier(pic, (pic_id *)pic->sCOND_EXPAND, pic->sCOND_EXPAND, env); + pic_put_identifier(pic, pic_id_ptr(pic->sDEFINE_LIBRARY), pic->sDEFINE_LIBRARY, env); + pic_put_identifier(pic, pic_id_ptr(pic->sIMPORT), pic->sIMPORT, env); + pic_put_identifier(pic, pic_id_ptr(pic->sEXPORT), pic->sEXPORT, env); + pic_put_identifier(pic, pic_id_ptr(pic->sCOND_EXPAND), pic->sCOND_EXPAND, env); return env; } @@ -109,27 +109,25 @@ pic_library_environment(pic_state *pic, const char *lib) void pic_import(pic_state *pic, const char *lib) { - pic_sym *name, *realname, *uid; + pic_value name, realname, uid; int it = 0; - pic_value val; struct pic_lib *libp; libp = get_library(pic, lib); - while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &val)) { - realname = pic_sym_ptr(val); - - if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { - pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname)); + while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &realname)) { + uid = pic_find_identifier(pic, pic_id_ptr(realname), libp->env); + if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { + pic_errorf(pic, "attempted to export undefined variable '~s'", realname); } - pic_put_identifier(pic, (pic_id *)name, uid, pic->lib->env); + pic_put_identifier(pic, pic_id_ptr(name), uid, pic->lib->env); } } void -pic_export(pic_state *pic, pic_sym *name) +pic_export(pic_state *pic, pic_value name) { - pic_dict_set(pic, pic_obj_value(pic->lib->exports), name, pic_obj_value(name)); + pic_dict_set(pic, pic_obj_value(pic->lib->exports), name, name); } static pic_value @@ -176,44 +174,45 @@ static pic_value pic_lib_library_import(pic_state *pic) { const char *lib; - pic_sym *name, *realname, *uid, *alias = NULL; + pic_value name, alias = pic_false_value(pic), realname, uid; struct pic_lib *libp; pic_get_args(pic, "zm|m", &lib, &name, &alias); - if (alias == NULL) { + if (pic_false_p(pic, alias)) { alias = name; } libp = get_library(pic, lib); if (! pic_dict_has(pic, pic_obj_value(libp->exports), name)) { - pic_errorf(pic, "attempted to import undefined variable '~s'", pic_obj_value(name)); + pic_errorf(pic, "library-import: variable is not exported '~s'", name); } else { - realname = pic_sym_ptr(pic_dict_ref(pic, pic_obj_value(libp->exports), name)); + realname = pic_dict_ref(pic, pic_obj_value(libp->exports), name); } - if ((uid = pic_find_identifier(pic, (pic_id *)realname, libp->env)) == NULL) { - pic_errorf(pic, "attempted to export undefined variable '~s'", pic_obj_value(realname)); - } else { - pic_put_identifier(pic, (pic_id *)alias, uid, pic->lib->env); + uid = pic_find_identifier(pic, pic_id_ptr(realname), libp->env); + if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { + pic_errorf(pic, "attempted to export undefined variable '~s'", realname); } + pic_put_identifier(pic, pic_id_ptr(alias), uid, pic->lib->env); + return pic_undef_value(pic); } static pic_value pic_lib_library_export(pic_state *pic) { - pic_sym *name, *alias = NULL; + pic_value name, alias = pic_false_value(pic); pic_get_args(pic, "m|m", &name, &alias); - if (alias == NULL) { + if (pic_false_p(pic, alias)) { alias = name; } - pic_dict_set(pic, pic_obj_value(pic->lib->exports), alias, pic_obj_value(name)); + pic_dict_set(pic, pic_obj_value(pic->lib->exports), alias, name); return pic_undef_value(pic); } @@ -222,8 +221,7 @@ static pic_value pic_lib_library_exports(pic_state *pic) { const char *lib; - pic_value exports = pic_nil_value(pic); - pic_sym *sym; + pic_value sym, exports = pic_nil_value(pic); int it = 0; struct pic_lib *libp; @@ -232,7 +230,7 @@ pic_lib_library_exports(pic_state *pic) libp = get_library(pic, lib); while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &sym, NULL)) { - pic_push(pic, pic_obj_value(sym), exports); + pic_push(pic, sym, exports); } return exports; diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index faad8f45..aaf463ae 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -21,12 +21,11 @@ pic_make_env(pic_state *pic, struct pic_env *up) return env; } -pic_sym * +pic_value pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { const char *name; - pic_sym *uid; - pic_value str; + pic_value uid, str; name = pic_str(pic, pic_id_name(pic, id)); @@ -40,63 +39,58 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) return pic_put_identifier(pic, id, uid, env); } -pic_sym * -pic_put_identifier(pic_state *pic, pic_id *id, pic_sym *uid, struct pic_env *env) +pic_value +pic_put_identifier(pic_state *pic, pic_id *id, pic_value uid, struct pic_env *env) { khiter_t it; int ret; it = kh_put(env, &env->map, id, &ret); - kh_val(&env->map, it) = uid; + kh_val(&env->map, it) = pic_sym_ptr(pic, uid); return uid; } -pic_sym * -search_scope(pic_state *pic, pic_id *id, struct pic_env *env) +static bool +search_scope(pic_state *pic, pic_id *id, struct pic_env *env, pic_value *uid) { khiter_t it; it = kh_get(env, &env->map, id); if (it == kh_end(&env->map)) { - return NULL; + return false; } - return kh_val(&env->map, it); + *uid = pic_obj_value(kh_val(&env->map, it)); + return true; } -static pic_sym * -search(pic_state *pic, pic_id *id, struct pic_env *env) +static bool +search(pic_state *pic, pic_id *id, struct pic_env *env, pic_value *uid) { - pic_sym *uid = NULL; - while (env != NULL) { - uid = search_scope(pic, id, env); - if (uid != NULL) { - break; + if (search_scope(pic, id, env, uid)) { + return true; } env = env->up; } - return uid; + return false; } -pic_sym * +pic_value pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) { - pic_sym *uid; + pic_value uid; - while ((uid = search(pic, id, env)) == NULL) { + while (! search(pic, id, env, &uid)) { if (pic_sym_p(pic, pic_obj_value(id))) { - break; + while (env->up != NULL) { + env = env->up; + } + return pic_add_identifier(pic, id, env); } env = id->env; /* do not overwrite id first */ id = id->u.id; } - if (uid == NULL) { - while (env->up != NULL) { - env = env->up; - } - uid = pic_add_identifier(pic, id, env); - } return uid; } @@ -107,28 +101,29 @@ pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) static void -define_macro(pic_state *pic, pic_sym *uid, pic_value mac) +define_macro(pic_state *pic, pic_value uid, pic_value mac) { - if (pic_weak_has(pic, pic->macros, pic_obj_value(uid))) { - pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(uid)); + if (pic_weak_has(pic, pic->macros, uid)) { + pic_warnf(pic, "redefining syntax variable: ~s", uid); } - pic_weak_set(pic, pic->macros, pic_obj_value(uid), mac); + pic_weak_set(pic, pic->macros, uid, mac); } -static pic_value -find_macro(pic_state *pic, pic_sym *uid) +static bool +find_macro(pic_state *pic, pic_value uid, pic_value *mac) { - if (! pic_weak_has(pic, pic->macros, pic_obj_value(uid))) { - return pic_false_value(pic); + if (! pic_weak_has(pic, pic->macros, uid)) { + return false; } - return pic_weak_ref(pic, pic->macros, pic_obj_value(uid)); + *mac = pic_weak_ref(pic, pic->macros, uid); + return true; } static void -shadow_macro(pic_state *pic, pic_sym *uid) +shadow_macro(pic_state *pic, pic_value uid) { - if (pic_weak_has(pic, pic->macros, pic_obj_value(uid))) { - pic_weak_del(pic, pic->macros, pic_obj_value(uid)); + if (pic_weak_has(pic, pic->macros, uid)) { + pic_weak_del(pic, pic->macros, uid); } } @@ -138,21 +133,20 @@ static pic_value expand_lambda(pic_state *, pic_value, struct pic_env *); static pic_value expand_var(pic_state *pic, pic_id *id, struct pic_env *env, pic_value deferred) { - pic_value mac; - pic_sym *functor; + pic_value mac, functor; functor = pic_find_identifier(pic, id, env); - if (! pic_false_p(pic, mac = find_macro(pic, functor))) { + if (find_macro(pic, functor, &mac)) { return expand(pic, pic_call(pic, mac, 2, pic_obj_value(id), pic_obj_value(env)), env, deferred); } - return pic_obj_value(functor); + return functor; } static pic_value expand_quote(pic_state *pic, pic_value expr) { - return pic_cons(pic, pic_obj_value(pic->sQUOTE), pic_cdr(pic, expr)); + return pic_cons(pic, pic->sQUOTE, pic_cdr(pic, expr)); } static pic_value @@ -226,25 +220,24 @@ expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env) expand_deferred(pic, deferred, in); - return pic_list(pic, 3, pic_obj_value(pic->sLAMBDA), formal, body); + return pic_list(pic, 3, pic->sLAMBDA, formal, body); } static pic_value expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) { - pic_sym *uid; + pic_value uid, val; pic_id *id; - pic_value val; id = pic_id_ptr(pic_cadr(pic, expr)); - if ((uid = search_scope(pic, id, env)) == NULL) { + if (! search_scope(pic, id, env, &uid)) { uid = pic_add_identifier(pic, id, env); } else { shadow_macro(pic, uid); } val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred); - return pic_list(pic, 3, pic_obj_value(pic->sDEFINE), pic_obj_value(uid), val); + return pic_list(pic, 3, pic->sDEFINE, uid, val); } static pic_value @@ -252,11 +245,10 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) { pic_value pic_compile(pic_state *, pic_value); pic_id *id; - pic_value val; - pic_sym *uid; + pic_value uid, val; id = pic_id_ptr(pic_cadr(pic, expr)); - if ((uid = search_scope(pic, id, env)) == NULL) { + if (! search_scope(pic, id, env, &uid)) { uid = pic_add_identifier(pic, id, env); } @@ -286,24 +278,24 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer } if (pic_id_p(pic, pic_car(pic, expr))) { - pic_sym *functor; + pic_value functor; functor = pic_find_identifier(pic, pic_id_ptr(pic_car(pic, expr)), env); - if (functor == pic->sDEFINE_MACRO) { + if (pic_eq_p(pic, functor, pic->sDEFINE_MACRO)) { return expand_defmacro(pic, expr, env); } - else if (functor == pic->sLAMBDA) { + else if (pic_eq_p(pic, functor, pic->sLAMBDA)) { return expand_defer(pic, expr, deferred); } - else if (functor == pic->sDEFINE) { + else if (pic_eq_p(pic, functor, pic->sDEFINE)) { return expand_define(pic, expr, env, deferred); } - else if (functor == pic->sQUOTE) { + else if (pic_eq_p(pic, functor, pic->sQUOTE)) { return expand_quote(pic, expr); } - if (! pic_false_p(pic, mac = find_macro(pic, functor))) { + if (find_macro(pic, functor, &mac)) { return expand(pic, pic_call(pic, mac, 2, expr, pic_obj_value(env)), env, deferred); } } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 69d7d34d..d678e813 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -20,7 +20,7 @@ * F double *, bool * float with exactness * c char * char * z char ** c string - * m pic_sym ** symbol + * m pic_value * symbol * v pic_value * vector object * s pic_value * string object * b pic_value * bytevector object @@ -147,13 +147,13 @@ pic_get_args(pic_state *pic, const char *format, ...) #define PTR_CASE(c, type, ctype) \ VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) - PTR_CASE('m', sym, pic_sym *) PTR_CASE('p', port, struct pic_port *) PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) + OBJ_CASE('m', sym) OBJ_CASE('s', str) OBJ_CASE('l', proc) OBJ_CASE('b', blob) @@ -180,18 +180,21 @@ pic_get_args(pic_state *pic, const char *format, ...) } static pic_value -vm_gref(pic_state *pic, pic_sym *uid) +vm_gref(pic_state *pic, pic_value uid) { - if (! pic_weak_has(pic, pic->globals, pic_obj_value(uid))) { - pic_errorf(pic, "uninitialized global variable: %s", pic_str(pic, pic_sym_name(pic, uid))); + pic_value val; + + val = pic_weak_ref(pic, pic->globals, uid);; + if (pic_invalid_p(pic, val)) { + pic_errorf(pic, "uninitialized global variable: ~s", uid); } - return pic_weak_ref(pic, pic->globals, pic_obj_value(uid)); + return val; } static void -vm_gset(pic_state *pic, pic_sym *uid, pic_value value) +vm_gset(pic_state *pic, pic_value uid, pic_value value) { - pic_weak_set(pic, pic->globals, pic_obj_value(uid), value); + pic_weak_set(pic, pic->globals, uid, value); } static void @@ -422,11 +425,11 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) NEXT; } CASE(OP_GREF) { - PUSH(vm_gref(pic, (pic_sym *)pic->ci->irep->pool[c.a])); + PUSH(vm_gref(pic, pic_obj_value(pic->ci->irep->pool[c.a]))); NEXT; } CASE(OP_GSET) { - vm_gset(pic, (pic_sym *)pic->ci->irep->pool[c.a], POP()); + vm_gset(pic, pic_obj_value(pic->ci->irep->pool[c.a]), POP()); PUSH(pic_undef_value(pic)); NEXT; } @@ -887,33 +890,32 @@ pic_defvar(pic_state *pic, const char *name, pic_value init, pic_value conv) void pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) { - pic_sym *sym, *uid; + pic_value sym, uid; struct pic_env *env; sym = pic_intern_cstr(pic, name); env = pic_library_environment(pic, lib); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { - uid = pic_add_identifier(pic, (pic_id *)sym, env); - } else { - if (pic_weak_has(pic, pic->globals, pic_obj_value(uid))) { - pic_warnf(pic, "redefining variable: ~s", pic_obj_value(uid)); - } - } - pic_set(pic, lib, name, val); + uid = pic_find_identifier(pic, pic_id_ptr(sym), env); + if (pic_weak_has(pic, pic->globals, uid)) { + pic_warnf(pic, "redefining variable: ~s", uid); + } + pic_weak_set(pic, pic->globals, uid, val); } pic_value pic_ref(pic_state *pic, const char *lib, const char *name) { - pic_sym *sym, *uid; + pic_value sym, uid; struct pic_env *env; sym = pic_intern_cstr(pic, name); env = pic_library_environment(pic, lib); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { + + uid = pic_find_identifier(pic, pic_id_ptr(sym), env); + if (! pic_weak_has(pic, pic->globals, uid)) { pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); } @@ -923,13 +925,15 @@ pic_ref(pic_state *pic, const char *lib, const char *name) void pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) { - pic_sym *sym, *uid; + pic_value sym, uid; struct pic_env *env; sym = pic_intern_cstr(pic, name); env = pic_library_environment(pic, lib); - if ((uid = pic_find_identifier(pic, (pic_id *)sym, env)) == NULL) { + + uid = pic_find_identifier(pic, pic_id_ptr(sym), env); + if (! pic_weak_has(pic, pic->globals, uid)) { pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 92fa0b60..077a8334 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -149,49 +149,49 @@ read_directive(pic_state *pic, xFILE *file, int c) static pic_value read_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic_obj_value(pic->sQUOTE), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic->sQUOTE, read(pic, file, next(pic, file))); } static pic_value read_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic_obj_value(pic->sQUASIQUOTE), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic->sQUASIQUOTE, read(pic, file, next(pic, file))); } static pic_value read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - pic_sym *tag = pic->sUNQUOTE; + pic_value tag = pic->sUNQUOTE; if (peek(pic, file) == '@') { tag = pic->sUNQUOTE_SPLICING; next(pic, file); } - return pic_list(pic, 2, pic_obj_value(tag), read(pic, file, next(pic, file))); + return pic_list(pic, 2, tag, read(pic, file, next(pic, file))); } static pic_value read_syntax_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic_obj_value(pic->sSYNTAX_QUOTE), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic->sSYNTAX_QUOTE, read(pic, file, next(pic, file))); } static pic_value read_syntax_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic_obj_value(pic->sSYNTAX_QUASIQUOTE), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic->sSYNTAX_QUASIQUOTE, read(pic, file, next(pic, file))); } static pic_value read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - pic_sym *tag = pic->sSYNTAX_UNQUOTE; + pic_value tag = pic->sSYNTAX_UNQUOTE; if (peek(pic, file) == '@') { tag = pic->sSYNTAX_UNQUOTE_SPLICING; next(pic, file); } - return pic_list(pic, 2, pic_obj_value(tag), read(pic, file, next(pic, file))); + return pic_list(pic, 2, tag, read(pic, file, next(pic, file))); } static pic_value @@ -199,7 +199,7 @@ read_symbol(pic_state *pic, xFILE *file, int c) { int len; char *buf; - pic_sym *sym; + pic_value sym; len = 1; buf = pic_malloc(pic, len + 1); @@ -217,7 +217,7 @@ read_symbol(pic_state *pic, xFILE *file, int c) sym = pic_intern_cstr(pic, buf); pic_free(pic, buf); - return pic_obj_value(sym); + return sym; } static unsigned @@ -320,10 +320,10 @@ read_minus(pic_state *pic, xFILE *file, int c) } else { sym = read_symbol(pic, file, c); - if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-inf.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "-inf.0")) { return pic_float_value(pic, -(1.0 / 0.0)); } - if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "-nan.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "-nan.0")) { return pic_float_value(pic, -(0.0 / 0.0)); } return sym; @@ -340,10 +340,10 @@ read_plus(pic_state *pic, xFILE *file, int c) } else { sym = read_symbol(pic, file, c); - if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+inf.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "+inf.0")) { return pic_float_value(pic, 1.0 / 0.0); } - if (strcaseeq(pic_str(pic, pic_sym_name(pic, pic_sym_ptr(sym))), "+nan.0")) { + if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "+nan.0")) { return pic_float_value(pic, 0.0 / 0.0); } return sym; @@ -453,7 +453,7 @@ read_pipe(pic_state *pic, xFILE *file, int c) { char *buf; int size, cnt; - pic_sym *sym; + pic_value sym; /* Currently supports only ascii chars */ char HEX_BUF[3]; size_t i = 0; @@ -489,7 +489,7 @@ read_pipe(pic_state *pic, xFILE *file, int c) sym = pic_intern_cstr(pic, buf); pic_free(pic, buf); - return pic_obj_value(sym); + return sym; } static pic_value diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 60ed6d9a..c0ecea1b 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -66,7 +66,7 @@ pic_init_features(pic_state *pic) void pic_add_feature(pic_state *pic, const char *feature) { - pic_push(pic, pic_obj_value(pic_intern_cstr(pic, feature)), pic->features); + pic_push(pic, pic_intern_cstr(pic, feature), pic->features); } static pic_value @@ -78,16 +78,16 @@ pic_features(pic_state *pic) } #define import_builtin_syntax(name) do { \ - pic_sym *nick, *real; \ + pic_value nick, real; \ nick = pic_intern_lit(pic, "builtin:" name); \ real = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, (pic_id *)nick, real, pic->lib->env); \ + pic_put_identifier(pic, pic_id_ptr(nick), real, pic->lib->env); \ } while (0) #define declare_vm_procedure(name) do { \ - pic_sym *sym; \ + pic_value sym; \ sym = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, (pic_id *)sym, sym, pic->lib->env); \ + pic_put_identifier(pic, pic_id_ptr(sym), sym, pic->lib->env); \ } while (0) void pic_init_bool(pic_state *); @@ -116,7 +116,6 @@ extern const char pic_boot[][80]; static void pic_init_core(pic_state *pic) { - struct pic_box *pic_vm_gref_slot(pic_state *, pic_sym *); size_t ai; pic_init_features(pic); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 2db1da56..3ef567d9 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -10,7 +10,7 @@ KHASH_DEFINE(oblist, struct pic_string *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) -pic_sym * +pic_value pic_intern(pic_state *pic, pic_value str) { khash_t(oblist) *h = &pic->oblist; @@ -22,16 +22,16 @@ pic_intern(pic_state *pic, pic_value str) if (ret == 0) { /* if exists */ sym = kh_val(h, it); pic_protect(pic, pic_obj_value(sym)); - return sym; + return pic_obj_value(sym); } - kh_val(h, it) = pic->sQUOTE; /* dummy */ + kh_val(h, it) = pic_sym_ptr(pic, pic->sQUOTE); /* dummy */ sym = (pic_sym *)pic_obj_alloc(pic, offsetof(pic_sym, env), PIC_TYPE_SYMBOL); sym->u.str = pic_str_ptr(pic, str); kh_val(h, it) = sym; - return sym; + return pic_obj_value(sym); } pic_id * @@ -46,9 +46,9 @@ pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env) } pic_value -pic_sym_name(pic_state PIC_UNUSED(*pic), pic_sym *sym) +pic_sym_name(pic_state PIC_UNUSED(*pic), pic_value sym) { - return pic_obj_value(sym->u.str); + return pic_obj_value(pic_sym_ptr(pic, sym)->u.str); } pic_value @@ -58,7 +58,7 @@ pic_id_name(pic_state *pic, pic_id *id) id = id->u.id; } - return pic_sym_name(pic, (pic_sym *)id); + return pic_sym_name(pic, pic_obj_value(id)); } static pic_value @@ -93,11 +93,11 @@ pic_symbol_symbol_eq_p(pic_state *pic) static pic_value pic_symbol_symbol_to_string(pic_state *pic) { - pic_sym *sym; + pic_value sym; pic_get_args(pic, "m", &sym); - return pic_obj_value(sym->u.str); + return pic_sym_name(pic, sym); } static pic_value @@ -107,7 +107,7 @@ pic_symbol_string_to_symbol(pic_state *pic) pic_get_args(pic, "s", &str); - return pic_obj_value(pic_intern(pic, str)); + return pic_intern(pic, str); } static pic_value diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 11de3960..cd363025 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -173,46 +173,46 @@ write_pair(struct writer_control *p, pic_value pair) { pic_state *pic = p->pic; xFILE *file = p->file; - pic_sym *tag; + pic_value tag; if (pic_pair_p(pic, pic_cdr(pic, pair)) && pic_nil_p(pic, pic_cddr(pic, pair)) && pic_sym_p(pic, pic_car(pic, pair))) { - tag = pic_sym_ptr(pic_car(pic, pair)); - if (tag == pic->sQUOTE) { + tag = pic_car(pic, pair); + if (pic_eq_p(pic, tag, pic->sQUOTE)) { xfprintf(pic, file, "'"); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sUNQUOTE) { + else if (pic_eq_p(pic, tag, pic->sUNQUOTE)) { xfprintf(pic, file, ","); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sUNQUOTE_SPLICING) { + else if (pic_eq_p(pic, tag, pic->sUNQUOTE_SPLICING)) { xfprintf(pic, file, ",@"); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sQUASIQUOTE) { + else if (pic_eq_p(pic, tag, pic->sQUASIQUOTE)) { xfprintf(pic, file, "`"); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sSYNTAX_QUOTE) { + else if (pic_eq_p(pic, tag, pic->sSYNTAX_QUOTE)) { xfprintf(pic, file, "#'"); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sSYNTAX_UNQUOTE) { + else if (pic_eq_p(pic, tag, pic->sSYNTAX_UNQUOTE)) { xfprintf(pic, file, "#,"); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sSYNTAX_UNQUOTE_SPLICING) { + else if (pic_eq_p(pic, tag, pic->sSYNTAX_UNQUOTE_SPLICING)) { xfprintf(pic, file, "#,@"); write_core(p, pic_cadr(pic, pair)); return; } - else if (tag == pic->sSYNTAX_QUASIQUOTE) { + else if (pic_eq_p(pic, tag, pic->sSYNTAX_QUASIQUOTE)) { xfprintf(pic, file, "#`"); write_core(p, pic_cadr(pic, pair)); return; @@ -245,8 +245,7 @@ write_dict(struct writer_control *p, pic_value dict) { pic_state *pic = p->pic; xFILE *file = p->file; - pic_sym *key; - pic_value val; + pic_value key, val; int it = 0; xfprintf(pic, file, "#.(dictionary"); @@ -303,7 +302,7 @@ write_core(struct writer_control *p, pic_value obj) write_float(pic, pic_float(pic, obj), file); break; case PIC_TYPE_SYMBOL: - xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, pic_sym_ptr(obj)))); + xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, obj))); break; case PIC_TYPE_BLOB: write_blob(pic, obj, file); From c57655c7acc466577ca40fab66383c03f920efb3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 15:34:49 +0900 Subject: [PATCH 062/119] don't use pic_false_p if possible --- extlib/benz/lib.c | 12 +++++++----- extlib/benz/pair.c | 14 ++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 73e29c11..99b98aa6 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -174,12 +174,13 @@ static pic_value pic_lib_library_import(pic_state *pic) { const char *lib; - pic_value name, alias = pic_false_value(pic), realname, uid; + pic_value name, alias, realname, uid; struct pic_lib *libp; + int n; - pic_get_args(pic, "zm|m", &lib, &name, &alias); + n = pic_get_args(pic, "zm|m", &lib, &name, &alias); - if (pic_false_p(pic, alias)) { + if (n == 2) { alias = name; } @@ -205,10 +206,11 @@ static pic_value pic_lib_library_export(pic_state *pic) { pic_value name, alias = pic_false_value(pic); + int n; - pic_get_args(pic, "m|m", &name, &alias); + n = pic_get_args(pic, "m|m", &name, &alias); - if (pic_false_p(pic, alias)) { + if (n == 1) { alias = name; } diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index ff12994a..a4856687 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -561,12 +561,13 @@ pic_pair_memv(pic_state *pic) static pic_value pic_pair_member(pic_state *pic) { - pic_value key, list, proc = pic_false_value(pic); + pic_value key, list, proc; + int n; - pic_get_args(pic, "oo|l", &key, &list, &proc); + n = pic_get_args(pic, "oo|l", &key, &list, &proc); while (! pic_nil_p(pic, list)) { - if (pic_false_p(pic, proc)) { + if (n == 2) { if (pic_equal_p(pic, key, pic_car(pic, list))) return list; } else { @@ -615,13 +616,14 @@ pic_pair_assv(pic_state *pic) static pic_value pic_pair_assoc(pic_state *pic) { - pic_value key, alist, proc = pic_false_value(pic), cell; + pic_value key, alist, proc, cell; + int n; - pic_get_args(pic, "oo|l", &key, &alist, &proc); + n = pic_get_args(pic, "oo|l", &key, &alist, &proc); while (! pic_nil_p(pic, alist)) { cell = pic_car(pic, alist); - if (pic_false_p(pic, proc)) { + if (n == 2) { if (pic_equal_p(pic, key, pic_car(pic, cell))) return cell; } else { From fc37af43b503b3317e209819bb623040c3e50272 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 15:59:06 +0900 Subject: [PATCH 063/119] pic_id * -> pic_value --- extlib/benz/bool.c | 8 +++--- extlib/benz/include/picrin/object.h | 12 ++++---- extlib/benz/lib.c | 16 +++++------ extlib/benz/macro.c | 44 ++++++++++++++--------------- extlib/benz/proc.c | 6 ++-- extlib/benz/state.c | 18 ++++++------ extlib/benz/symbol.c | 23 +++++++-------- extlib/benz/write.c | 2 +- 8 files changed, 64 insertions(+), 65 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 1a1a4310..15d857e6 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -97,11 +97,11 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) pic_id *id1, *id2; pic_value s1, s2; - id1 = pic_id_ptr(x); - id2 = pic_id_ptr(y); + id1 = pic_id_ptr(pic, x); + id2 = pic_id_ptr(pic, y); - s1 = pic_find_identifier(pic, id1->u.id, id1->env); - s2 = pic_find_identifier(pic, id2->u.id, id2->env); + s1 = pic_find_identifier(pic, pic_obj_value(id1->u.id), id1->env); + s2 = pic_find_identifier(pic, pic_obj_value(id2->u.id), id2->env); return pic_eq_p(pic, s1, s2); } diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 953b73d5..b0fd490b 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -115,6 +115,7 @@ struct pic_port { xFILE *file; }; +#define pic_id_ptr(pic, o) ((pic_id *)pic_obj_ptr(o)) #define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o)) #define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o)) #define pic_blob_ptr(pic, o) ((struct pic_blob *)pic_obj_ptr(o)) @@ -124,7 +125,6 @@ struct pic_port { #define pic_weak_ptr(pic, o) ((struct pic_weak *)pic_obj_ptr(o)) #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) -#define pic_id_ptr(v) ((pic_id *)pic_obj_ptr(v)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) #define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) @@ -151,17 +151,17 @@ struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); if (tolen - at < e - s) pic_errorf(pic, "invalid range"); \ } while (0) -pic_id *pic_make_identifier(pic_state *, pic_id *, struct pic_env *); +pic_value pic_make_identifier(pic_state *, pic_value id, struct pic_env *); pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); pic_value pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); struct pic_env *pic_make_env(pic_state *, struct pic_env *); -pic_value pic_add_identifier(pic_state *, pic_id *, struct pic_env *); -pic_value pic_put_identifier(pic_state *, pic_id *, pic_value uid, struct pic_env *); -pic_value pic_find_identifier(pic_state *, pic_id *, struct pic_env *); -pic_value pic_id_name(pic_state *, pic_id *); +pic_value pic_add_identifier(pic_state *, pic_value id, struct pic_env *); +pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, struct pic_env *); +pic_value pic_find_identifier(pic_state *, pic_value id, struct pic_env *); +pic_value pic_id_name(pic_state *, pic_value id); void pic_rope_incref(pic_state *, struct pic_rope *); void pic_rope_decref(pic_state *, struct pic_rope *); diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 99b98aa6..2a5d7d34 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -42,10 +42,10 @@ make_library_env(pic_state *pic, pic_value name) kh_init(env, &env->map); /* set up default environment */ - pic_put_identifier(pic, pic_id_ptr(pic->sDEFINE_LIBRARY), pic->sDEFINE_LIBRARY, env); - pic_put_identifier(pic, pic_id_ptr(pic->sIMPORT), pic->sIMPORT, env); - pic_put_identifier(pic, pic_id_ptr(pic->sEXPORT), pic->sEXPORT, env); - pic_put_identifier(pic, pic_id_ptr(pic->sCOND_EXPAND), pic->sCOND_EXPAND, env); + pic_put_identifier(pic, pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, env); + pic_put_identifier(pic, pic->sIMPORT, pic->sIMPORT, env); + pic_put_identifier(pic, pic->sEXPORT, pic->sEXPORT, env); + pic_put_identifier(pic, pic->sCOND_EXPAND, pic->sCOND_EXPAND, env); return env; } @@ -116,11 +116,11 @@ pic_import(pic_state *pic, const char *lib) libp = get_library(pic, lib); while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &realname)) { - uid = pic_find_identifier(pic, pic_id_ptr(realname), libp->env); + uid = pic_find_identifier(pic, realname, libp->env); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { pic_errorf(pic, "attempted to export undefined variable '~s'", realname); } - pic_put_identifier(pic, pic_id_ptr(name), uid, pic->lib->env); + pic_put_identifier(pic, name, uid, pic->lib->env); } } @@ -192,12 +192,12 @@ pic_lib_library_import(pic_state *pic) realname = pic_dict_ref(pic, pic_obj_value(libp->exports), name); } - uid = pic_find_identifier(pic, pic_id_ptr(realname), libp->env); + uid = pic_find_identifier(pic, realname, libp->env); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { pic_errorf(pic, "attempted to export undefined variable '~s'", realname); } - pic_put_identifier(pic, pic_id_ptr(alias), uid, pic->lib->env); + pic_put_identifier(pic, alias, uid, pic->lib->env); return pic_undef_value(pic); } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index aaf463ae..ff05c71b 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -22,14 +22,14 @@ pic_make_env(pic_state *pic, struct pic_env *up) } pic_value -pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) +pic_add_identifier(pic_state *pic, pic_value id, struct pic_env *env) { const char *name; pic_value uid, str; name = pic_str(pic, pic_id_name(pic, id)); - if (env->up == NULL && pic_sym_p(pic, pic_obj_value(id))) { /* toplevel & public */ + if (env->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ str = pic_strf_value(pic, "%s/%s", pic_str(pic, pic_obj_value(env->lib)), name); } else { str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); @@ -40,23 +40,23 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env) } pic_value -pic_put_identifier(pic_state *pic, pic_id *id, pic_value uid, struct pic_env *env) +pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, struct pic_env *env) { khiter_t it; int ret; - it = kh_put(env, &env->map, id, &ret); + it = kh_put(env, &env->map, pic_id_ptr(pic, id), &ret); kh_val(&env->map, it) = pic_sym_ptr(pic, uid); return uid; } static bool -search_scope(pic_state *pic, pic_id *id, struct pic_env *env, pic_value *uid) +search_scope(pic_state *pic, pic_value id, struct pic_env *env, pic_value *uid) { khiter_t it; - it = kh_get(env, &env->map, id); + it = kh_get(env, &env->map, pic_id_ptr(pic, id)); if (it == kh_end(&env->map)) { return false; } @@ -65,7 +65,7 @@ search_scope(pic_state *pic, pic_id *id, struct pic_env *env, pic_value *uid) } static bool -search(pic_state *pic, pic_id *id, struct pic_env *env, pic_value *uid) +search(pic_state *pic, pic_value id, struct pic_env *env, pic_value *uid) { while (env != NULL) { if (search_scope(pic, id, env, uid)) { @@ -77,19 +77,19 @@ search(pic_state *pic, pic_id *id, struct pic_env *env, pic_value *uid) } pic_value -pic_find_identifier(pic_state *pic, pic_id *id, struct pic_env *env) +pic_find_identifier(pic_state *pic, pic_value id, struct pic_env *env) { pic_value uid; while (! search(pic, id, env, &uid)) { - if (pic_sym_p(pic, pic_obj_value(id))) { + if (pic_sym_p(pic, id)) { while (env->up != NULL) { env = env->up; } return pic_add_identifier(pic, id, env); } - env = id->env; /* do not overwrite id first */ - id = id->u.id; + env = pic_id_ptr(pic, id)->env; /* do not overwrite id first */ + id = pic_obj_value(pic_id_ptr(pic, id)->u.id); } return uid; } @@ -131,14 +131,14 @@ static pic_value expand(pic_state *, pic_value, struct pic_env *, pic_value); static pic_value expand_lambda(pic_state *, pic_value, struct pic_env *); static pic_value -expand_var(pic_state *pic, pic_id *id, struct pic_env *env, pic_value deferred) +expand_var(pic_state *pic, pic_value id, struct pic_env *env, pic_value deferred) { pic_value mac, functor; functor = pic_find_identifier(pic, id, env); if (find_macro(pic, functor, &mac)) { - return expand(pic, pic_call(pic, mac, 2, pic_obj_value(id), pic_obj_value(env)), env, deferred); + return expand(pic, pic_call(pic, mac, 2, id, pic_obj_value(env)), env, deferred); } return functor; } @@ -207,10 +207,10 @@ expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env) in = pic_make_env(pic, env); for (a = pic_cadr(pic, expr); pic_pair_p(pic, a); a = pic_cdr(pic, a)) { - pic_add_identifier(pic, pic_id_ptr(pic_car(pic, a)), in); + pic_add_identifier(pic, pic_car(pic, a), in); } if (pic_id_p(pic, a)) { - pic_add_identifier(pic, pic_id_ptr(a), in); + pic_add_identifier(pic, a, in); } deferred = pic_list(pic, 1, pic_nil_value(pic)); @@ -226,10 +226,9 @@ expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env) static pic_value expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) { - pic_value uid, val; - pic_id *id; + pic_value id, uid, val; - id = pic_id_ptr(pic_cadr(pic, expr)); + id = pic_cadr(pic, expr); if (! search_scope(pic, id, env, &uid)) { uid = pic_add_identifier(pic, id, env); } else { @@ -244,10 +243,9 @@ static pic_value expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) { pic_value pic_compile(pic_state *, pic_value); - pic_id *id; - pic_value uid, val; + pic_value id, uid, val; - id = pic_id_ptr(pic_cadr(pic, expr)); + id = pic_cadr(pic, expr); if (! search_scope(pic, id, env, &uid)) { uid = pic_add_identifier(pic, id, env); } @@ -268,7 +266,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer switch (pic_type(pic, expr)) { case PIC_TYPE_ID: case PIC_TYPE_SYMBOL: { - return expand_var(pic, pic_id_ptr(expr), env, deferred); + return expand_var(pic, expr, env, deferred); } case PIC_TYPE_PAIR: { pic_value mac; @@ -280,7 +278,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer if (pic_id_p(pic, pic_car(pic, expr))) { pic_value functor; - functor = pic_find_identifier(pic, pic_id_ptr(pic_car(pic, expr)), env); + functor = pic_find_identifier(pic, pic_car(pic, expr), env); if (pic_eq_p(pic, functor, pic->sDEFINE_MACRO)) { return expand_defmacro(pic, expr, env); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index d678e813..f2b758d2 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -897,7 +897,7 @@ pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) env = pic_library_environment(pic, lib); - uid = pic_find_identifier(pic, pic_id_ptr(sym), env); + uid = pic_find_identifier(pic, sym, env); if (pic_weak_has(pic, pic->globals, uid)) { pic_warnf(pic, "redefining variable: ~s", uid); } @@ -914,7 +914,7 @@ pic_ref(pic_state *pic, const char *lib, const char *name) env = pic_library_environment(pic, lib); - uid = pic_find_identifier(pic, pic_id_ptr(sym), env); + uid = pic_find_identifier(pic, sym, env); if (! pic_weak_has(pic, pic->globals, uid)) { pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); } @@ -932,7 +932,7 @@ pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) env = pic_library_environment(pic, lib); - uid = pic_find_identifier(pic, pic_id_ptr(sym), env); + uid = pic_find_identifier(pic, sym, env); if (! pic_weak_has(pic, pic->globals, uid)) { pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index c0ecea1b..6e41150a 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -77,17 +77,17 @@ pic_features(pic_state *pic) return pic->features; } -#define import_builtin_syntax(name) do { \ - pic_value nick, real; \ - nick = pic_intern_lit(pic, "builtin:" name); \ - real = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, pic_id_ptr(nick), real, pic->lib->env); \ +#define import_builtin_syntax(name) do { \ + pic_value nick, real; \ + nick = pic_intern_lit(pic, "builtin:" name); \ + real = pic_intern_lit(pic, name); \ + pic_put_identifier(pic, nick, real, pic->lib->env); \ } while (0) -#define declare_vm_procedure(name) do { \ - pic_value sym; \ - sym = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, pic_id_ptr(sym), sym, pic->lib->env); \ +#define declare_vm_procedure(name) do { \ + pic_value sym; \ + sym = pic_intern_lit(pic, name); \ + pic_put_identifier(pic, sym, sym, pic->lib->env); \ } while (0) void pic_init_bool(pic_state *); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 3ef567d9..09578d7e 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -34,15 +34,16 @@ pic_intern(pic_state *pic, pic_value str) return pic_obj_value(sym); } -pic_id * -pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env) +pic_value +pic_make_identifier(pic_state *pic, pic_value id, struct pic_env *env) { pic_id *nid; nid = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); - nid->u.id = id; + nid->u.id = pic_id_ptr(pic, id); nid->env = env; - return nid; + + return pic_obj_value(nid); } pic_value @@ -52,13 +53,13 @@ pic_sym_name(pic_state PIC_UNUSED(*pic), pic_value sym) } pic_value -pic_id_name(pic_state *pic, pic_id *id) +pic_id_name(pic_state *pic, pic_value id) { - while (! pic_sym_p(pic, pic_obj_value(id))) { - id = id->u.id; + while (! pic_sym_p(pic, id)) { + id = pic_obj_value(pic_id_ptr(pic, id)->u.id); } - return pic_sym_name(pic, pic_obj_value(id)); + return pic_sym_name(pic, id); } static pic_value @@ -130,7 +131,7 @@ pic_symbol_make_identifier(pic_state *pic) pic_assert_type(pic, id, id); pic_assert_type(pic, env, env); - return pic_obj_value(pic_make_identifier(pic, pic_id_ptr(id), pic_env_ptr(env))); + return pic_make_identifier(pic, id, pic_env_ptr(env)); } static pic_value @@ -146,7 +147,7 @@ pic_symbol_identifier_variable(pic_state *pic) pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); } - return pic_obj_value(pic_id_ptr(id)->u.id); + return pic_obj_value(pic_id_ptr(pic, id)->u.id); } static pic_value @@ -162,7 +163,7 @@ pic_symbol_identifier_environment(pic_state *pic) pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); } - return pic_obj_value(pic_id_ptr(id)->env); + return pic_obj_value(pic_id_ptr(pic, id)->env); } static pic_value diff --git a/extlib/benz/write.c b/extlib/benz/write.c index cd363025..38700f58 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -290,7 +290,7 @@ write_core(struct writer_control *p, pic_value obj) xfprintf(pic, file, "#f"); break; case PIC_TYPE_ID: - xfprintf(pic, file, "#", pic_str(pic, pic_id_name(pic, pic_id_ptr(obj)))); + xfprintf(pic, file, "#", pic_str(pic, pic_id_name(pic, obj))); break; case PIC_TYPE_EOF: xfprintf(pic, file, "#.(eof-object)"); From 0f1c47c42107c2f7f89e4d9c764fa85fccdc755d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 16:00:37 +0900 Subject: [PATCH 064/119] s/identifier-variable/identifier-base/g --- contrib/10.macro/macro.scm | 4 ++-- extlib/benz/symbol.c | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/contrib/10.macro/macro.scm b/contrib/10.macro/macro.scm index b7c74388..759d410d 100644 --- a/contrib/10.macro/macro.scm +++ b/contrib/10.macro/macro.scm @@ -7,7 +7,7 @@ make-identifier identifier? identifier=? - identifier-variable + identifier-base identifier-environment) ;; simple macro @@ -74,7 +74,7 @@ (define (strip-syntax form) (letrec ((unwrap (lambda (var) - (identifier-variable var))) + (identifier-base var))) (walk (lambda (f form) (cond ((identifier? form) diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 09578d7e..f8dabf80 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -35,15 +35,15 @@ pic_intern(pic_state *pic, pic_value str) } pic_value -pic_make_identifier(pic_state *pic, pic_value id, struct pic_env *env) +pic_make_identifier(pic_state *pic, pic_value base, struct pic_env *env) { - pic_id *nid; + pic_id *id; - nid = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); - nid->u.id = pic_id_ptr(pic, id); - nid->env = env; + id = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); + id->u.id = pic_id_ptr(pic, base); + id->env = env; - return pic_obj_value(nid); + return pic_obj_value(id); } pic_value @@ -135,7 +135,7 @@ pic_symbol_make_identifier(pic_state *pic) } static pic_value -pic_symbol_identifier_variable(pic_state *pic) +pic_symbol_identifier_base(pic_state *pic) { pic_value id; @@ -196,6 +196,6 @@ pic_init_symbol(pic_state *pic) pic_defun(pic, "make-identifier", pic_symbol_make_identifier); pic_defun(pic, "identifier?", pic_symbol_identifier_p); pic_defun(pic, "identifier=?", pic_symbol_identifier_eq_p); - pic_defun(pic, "identifier-variable", pic_symbol_identifier_variable); + pic_defun(pic, "identifier-base", pic_symbol_identifier_base); pic_defun(pic, "identifier-environment", pic_symbol_identifier_environment); } From d91ec2847464c7a7dc6b3a3f94ae77cbfde36428 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 16:02:34 +0900 Subject: [PATCH 065/119] cosmetic change --- extlib/benz/macro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index ff05c71b..c9f69ec0 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -252,7 +252,7 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); if (! pic_proc_p(pic, val)) { - pic_errorf(pic, "macro definition \"%s\" evaluates to non-procedure object", pic_str(pic, pic_id_name(pic, id))); + pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", id); } define_macro(pic, uid, val); From 45879deafd8a382a78439f8f26f8d1969fabc7f7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 16:12:21 +0900 Subject: [PATCH 066/119] move the definiton of pic_checkpoint to object.h --- contrib/10.callcc/callcc.c | 5 +++-- extlib/benz/cont.c | 6 +++--- extlib/benz/include/picrin.h | 6 +++--- extlib/benz/include/picrin/cont.h | 4 ++-- extlib/benz/include/picrin/object.h | 17 +++++++++++++++++ extlib/benz/include/picrin/state.h | 10 +--------- extlib/benz/include/picrin/type.h | 15 ++------------- extlib/benz/pair.c | 4 ++-- extlib/benz/state.c | 2 +- extlib/benz/value.c | 1 + 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index bacfee60..280b8331 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -1,11 +1,12 @@ #include "picrin.h" +#include "picrin/object.h" struct pic_fullcont { jmp_buf jmp; struct pic_cont *prev_jmp; - pic_checkpoint *cp; + struct pic_checkpoint *cp; char *stk_pos, *stk_ptr; ptrdiff_t stk_len; @@ -50,7 +51,7 @@ static void cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value)) { struct pic_fullcont *cont = data; - pic_checkpoint *cp; + struct pic_checkpoint *cp; pic_value *stack; pic_callinfo *ci; struct pic_proc **xp; diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 755b02e7..ffe7e4d2 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -41,7 +41,7 @@ pic_load_point(pic_state *pic, struct pic_cont *cont) } void -pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) +pic_wind(pic_state *pic, struct pic_checkpoint *here, struct pic_checkpoint *there) { if (here == there) return; @@ -59,13 +59,13 @@ pic_wind(pic_state *pic, pic_checkpoint *here, pic_checkpoint *there) static pic_value pic_dynamic_wind(pic_state *pic, pic_value in, pic_value thunk, pic_value out) { - pic_checkpoint *here; + struct pic_checkpoint *here; pic_value val; pic_call(pic, in, 0); /* enter */ here = pic->cp; - pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TYPE_CP); + pic->cp = (struct pic_checkpoint *)pic_obj_alloc(pic, sizeof(struct pic_checkpoint), PIC_TYPE_CP); pic->cp->prev = here; pic->cp->depth = here->depth + 1; pic->cp->in = pic_proc_ptr(pic, in); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 51b0c8f5..d650f8db 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -56,9 +56,6 @@ struct pic_port; struct pic_error; struct pic_env; -typedef struct pic_identifier pic_id; -typedef pic_id pic_sym; - typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); pic_state *pic_open(pic_allocf f, void *userdata); @@ -266,6 +263,9 @@ int pic_str_hash(pic_state *, pic_value str); /* extra stuff */ +typedef struct pic_identifier pic_id; +typedef pic_id pic_sym; + #include "picrin/type.h" #include "picrin/state.h" #include "picrin/cont.h" diff --git a/extlib/benz/include/picrin/cont.h b/extlib/benz/include/picrin/cont.h index 46742b0c..8629d6f9 100644 --- a/extlib/benz/include/picrin/cont.h +++ b/extlib/benz/include/picrin/cont.h @@ -14,7 +14,7 @@ struct pic_cont { int id; - pic_checkpoint *cp; + struct pic_checkpoint *cp; ptrdiff_t sp_offset; ptrdiff_t ci_offset; ptrdiff_t xp_offset; @@ -33,7 +33,7 @@ void pic_load_point(pic_state *, struct pic_cont *); pic_value pic_make_cont(pic_state *, struct pic_cont *); -void pic_wind(pic_state *, pic_checkpoint *, pic_checkpoint *); +void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index b0fd490b..b9861401 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -13,6 +13,14 @@ KHASH_DECLARE(env, pic_id *, pic_sym *) KHASH_DECLARE(dict, pic_sym *, pic_value) KHASH_DECLARE(weak, struct pic_object *, pic_value) +#define PIC_OBJECT_HEADER \ + unsigned char tt; \ + char gc_mark; + +struct pic_basic { + PIC_OBJECT_HEADER +}; + struct pic_identifier { PIC_OBJECT_HEADER union { @@ -115,6 +123,14 @@ struct pic_port { xFILE *file; }; +struct pic_checkpoint { + PIC_OBJECT_HEADER + struct pic_proc *in; + struct pic_proc *out; + int depth; + struct pic_checkpoint *prev; +}; + #define pic_id_ptr(pic, o) ((pic_id *)pic_obj_ptr(o)) #define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o)) #define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o)) @@ -131,6 +147,7 @@ struct pic_port { #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) #define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) +#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) #define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) #define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index a3e9ce88..a6db373d 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -22,14 +22,6 @@ struct pic_lib { struct pic_dict *exports; }; -typedef struct pic_checkpoint { - PIC_OBJECT_HEADER - struct pic_proc *in; - struct pic_proc *out; - int depth; - struct pic_checkpoint *prev; -} pic_checkpoint; - typedef struct { int argc, retc; pic_code *ip; @@ -48,7 +40,7 @@ struct pic_state { pic_allocf allocf; void *userdata; - pic_checkpoint *cp; + struct pic_checkpoint *cp; struct pic_cont *cc; int ccnt; diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h index 0f03314e..53543933 100644 --- a/extlib/benz/include/picrin/type.h +++ b/extlib/benz/include/picrin/type.h @@ -14,6 +14,8 @@ extern "C" { * it is only used for repsenting internal special state */ +#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_INVALID) + #if PIC_NAN_BOXING /** @@ -96,19 +98,6 @@ pic_obj_ptr(pic_value v) #endif -#define PIC_OBJECT_HEADER \ - unsigned char tt; \ - char gc_mark; - -struct pic_basic { - PIC_OBJECT_HEADER -}; - -#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) -#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_INVALID) - -#define pic_test(pic, v) (! pic_false_p(pic, v)) - PIC_INLINE bool pic_valid_int(double v) { diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index a4856687..ec95a029 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -571,7 +571,7 @@ pic_pair_member(pic_state *pic) 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)))) + if (! pic_false_p(pic, pic_call(pic, proc, 2, key, pic_car(pic, list)))) return list; } list = pic_cdr(pic, list); @@ -627,7 +627,7 @@ pic_pair_assoc(pic_state *pic) 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)))) + if (! pic_false_p(pic, pic_call(pic, proc, 2, key, pic_car(pic, cell)))) return cell; } alist = pic_cdr(pic, alist); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 6e41150a..987d80ab 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -344,7 +344,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->macros = pic_make_weak(pic); /* root block */ - pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TYPE_CP); + pic->cp = (struct pic_checkpoint *)pic_obj_alloc(pic, sizeof(struct pic_checkpoint), PIC_TYPE_CP); pic->cp->prev = NULL; pic->cp->depth = 0; pic->cp->in = pic->cp->out = NULL; diff --git a/extlib/benz/value.c b/extlib/benz/value.c index e0857dfb..82fcf227 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/object.h" int pic_type(pic_state PIC_UNUSED(*pic), pic_value v) From 215e159598d8e749321140d19f6f28c98257615a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 16:33:51 +0900 Subject: [PATCH 067/119] struct pic_env * -> pic_value --- extlib/benz/bool.c | 4 +- extlib/benz/eval.c | 3 +- extlib/benz/include/picrin.h | 8 +-- extlib/benz/include/picrin/object.h | 14 +++-- extlib/benz/lib.c | 34 ++++++------ extlib/benz/macro.c | 85 ++++++++++++++++------------- extlib/benz/proc.c | 9 +-- extlib/benz/state.c | 18 +++--- extlib/benz/symbol.c | 6 +- 9 files changed, 93 insertions(+), 88 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 15d857e6..f01e65ac 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -100,8 +100,8 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) id1 = pic_id_ptr(pic, x); id2 = pic_id_ptr(pic, y); - s1 = pic_find_identifier(pic, pic_obj_value(id1->u.id), id1->env); - s2 = pic_find_identifier(pic, pic_obj_value(id2->u.id), id2->env); + s1 = pic_find_identifier(pic, pic_obj_value(id1->u.id), pic_obj_value(id1->env)); + s2 = pic_find_identifier(pic, pic_obj_value(id2->u.id), pic_obj_value(id2->env)); return pic_eq_p(pic, s1, s2); } diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index d8b86146..9c2638a1 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -876,8 +876,7 @@ pic_value pic_eval(pic_state *pic, pic_value program, const char *lib) { const char *prev_lib = pic_current_library(pic); - struct pic_env *env; - pic_value r; + pic_value env, r; env = pic_library_environment(pic, lib); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index d650f8db..d0e75128 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -51,10 +51,8 @@ typedef struct { } pic_value; #endif -struct pic_object; struct pic_port; struct pic_error; -struct pic_env; typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); @@ -291,9 +289,9 @@ void pic_close_port(pic_state *, struct pic_port *port); pic_value pic_read(pic_state *, struct pic_port *); pic_value pic_read_cstr(pic_state *, const char *); -pic_value pic_expand(pic_state *, pic_value, struct pic_env *); +pic_value pic_expand(pic_state *, pic_value program, pic_value env); -pic_value pic_eval(pic_state *, pic_value, const char *); +pic_value pic_eval(pic_state *, pic_value program, const char *lib); void pic_load(pic_state *, struct pic_port *); void pic_load_cstr(pic_state *, const char *); @@ -360,7 +358,7 @@ void pic_fprintf(pic_state *, struct pic_port *, const char *, ...); pic_value pic_display(pic_state *, pic_value); pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); -struct pic_env *pic_library_environment(pic_state *, const char *); +pic_value pic_library_environment(pic_state *, const char *); #if DEBUG # define pic_debug(pic,obj) pic_fwrite(pic,obj,xstderr) diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index b9861401..38a155bc 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -17,6 +17,8 @@ KHASH_DECLARE(weak, struct pic_object *, pic_value) unsigned char tt; \ char gc_mark; +struct pic_object; /* defined in gc.c */ + struct pic_basic { PIC_OBJECT_HEADER }; @@ -141,11 +143,11 @@ struct pic_checkpoint { #define pic_weak_ptr(pic, o) ((struct pic_weak *)pic_obj_ptr(o)) #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) +#define pic_env_ptr(pic, o) ((struct pic_env *)pic_obj_ptr(o)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) #define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) #define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) -#define pic_env_ptr(v) ((struct pic_env *)pic_obj_ptr(v)) #define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) @@ -168,16 +170,16 @@ struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); if (tolen - at < e - s) pic_errorf(pic, "invalid range"); \ } while (0) -pic_value pic_make_identifier(pic_state *, pic_value id, struct pic_env *); +pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); pic_value pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); -struct pic_env *pic_make_env(pic_state *, struct pic_env *); +pic_value pic_make_env(pic_state *, pic_value env); -pic_value pic_add_identifier(pic_state *, pic_value id, struct pic_env *); -pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, struct pic_env *); -pic_value pic_find_identifier(pic_state *, pic_value id, struct pic_env *); +pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env); +pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env); +pic_value pic_find_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_id_name(pic_state *, pic_value id); void pic_rope_incref(pic_state *, struct pic_rope *); diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 2a5d7d34..a7e9336a 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -31,23 +31,26 @@ get_library(pic_state *pic, const char *lib) return libp; } -static struct pic_env * +static pic_value make_library_env(pic_state *pic, pic_value name) { struct pic_env *env; + pic_value e; env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); env->up = NULL; env->lib = pic_str_ptr(pic, name); kh_init(env, &env->map); - /* set up default environment */ - pic_put_identifier(pic, pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, env); - pic_put_identifier(pic, pic->sIMPORT, pic->sIMPORT, env); - pic_put_identifier(pic, pic->sEXPORT, pic->sEXPORT, env); - pic_put_identifier(pic, pic->sCOND_EXPAND, pic->sCOND_EXPAND, env); + e = pic_obj_value(env); - return env; + /* set up default environment */ + pic_put_identifier(pic, pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, e); + pic_put_identifier(pic, pic->sIMPORT, pic->sIMPORT, e); + pic_put_identifier(pic, pic->sEXPORT, pic->sEXPORT, e); + pic_put_identifier(pic, pic->sCOND_EXPAND, pic->sCOND_EXPAND, e); + + return e; } void @@ -55,8 +58,7 @@ pic_make_library(pic_state *pic, const char *lib) { khash_t(ltable) *h = &pic->ltable; const char *old_lib; - struct pic_env *env; - pic_value name, exports; + pic_value name, env, exports; khiter_t it; int ret; @@ -74,7 +76,7 @@ pic_make_library(pic_state *pic, const char *lib) } kh_val(h, it).name = pic_str_ptr(pic, name); - kh_val(h, it).env = env; + kh_val(h, it).env = pic_env_ptr(pic, env); kh_val(h, it).exports = pic_dict_ptr(pic, exports); if (pic->lib) { @@ -100,10 +102,10 @@ pic_current_library(pic_state *pic) return pic_str(pic, pic_obj_value(pic->lib->name)); } -struct pic_env * +pic_value pic_library_environment(pic_state *pic, const char *lib) { - return get_library(pic, lib)->env; + return pic_obj_value(get_library(pic, lib)->env); } void @@ -116,11 +118,11 @@ pic_import(pic_state *pic, const char *lib) libp = get_library(pic, lib); while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &realname)) { - uid = pic_find_identifier(pic, realname, libp->env); + uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env)); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { pic_errorf(pic, "attempted to export undefined variable '~s'", realname); } - pic_put_identifier(pic, name, uid, pic->lib->env); + pic_put_identifier(pic, name, uid, pic_obj_value(pic->lib->env)); } } @@ -192,12 +194,12 @@ pic_lib_library_import(pic_state *pic) realname = pic_dict_ref(pic, pic_obj_value(libp->exports), name); } - uid = pic_find_identifier(pic, realname, libp->env); + uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env)); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { pic_errorf(pic, "attempted to export undefined variable '~s'", realname); } - pic_put_identifier(pic, alias, uid, pic->lib->env); + pic_put_identifier(pic, alias, uid, pic_obj_value(pic->lib->env)); return pic_undef_value(pic); } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index c9f69ec0..99ca4de8 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -7,30 +7,29 @@ KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal) -struct pic_env * -pic_make_env(pic_state *pic, struct pic_env *up) +pic_value +pic_make_env(pic_state *pic, pic_value up) { struct pic_env *env; - assert(up != NULL); - env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); - env->up = up; + env->up = pic_env_ptr(pic, up); env->lib = NULL; kh_init(env, &env->map); - return env; + + return pic_obj_value(env); } pic_value -pic_add_identifier(pic_state *pic, pic_value id, struct pic_env *env) +pic_add_identifier(pic_state *pic, pic_value id, pic_value env) { const char *name; pic_value uid, str; name = pic_str(pic, pic_id_name(pic, id)); - if (env->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ - str = pic_strf_value(pic, "%s/%s", pic_str(pic, pic_obj_value(env->lib)), name); + if (pic_env_ptr(pic, env)->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ + str = pic_strf_value(pic, "~a/%s", pic_obj_value(pic_env_ptr(pic, env)->lib), name); } else { str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); } @@ -40,55 +39,63 @@ pic_add_identifier(pic_state *pic, pic_value id, struct pic_env *env) } pic_value -pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, struct pic_env *env) +pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) { khiter_t it; int ret; - it = kh_put(env, &env->map, pic_id_ptr(pic, id), &ret); - kh_val(&env->map, it) = pic_sym_ptr(pic, uid); + it = kh_put(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id), &ret); + kh_val(&pic_env_ptr(pic, env)->map, it) = pic_sym_ptr(pic, uid); return uid; } static bool -search_scope(pic_state *pic, pic_value id, struct pic_env *env, pic_value *uid) +search_scope(pic_state *pic, pic_value id, pic_value env, pic_value *uid) { khiter_t it; - it = kh_get(env, &env->map, pic_id_ptr(pic, id)); - if (it == kh_end(&env->map)) { + it = kh_get(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id)); + if (it == kh_end(&pic_env_ptr(pic, env)->map)) { return false; } - *uid = pic_obj_value(kh_val(&env->map, it)); + *uid = pic_obj_value(kh_val(&pic_env_ptr(pic, env)->map, it)); return true; } static bool -search(pic_state *pic, pic_value id, struct pic_env *env, pic_value *uid) +search(pic_state *pic, pic_value id, pic_value env, pic_value *uid) { - while (env != NULL) { - if (search_scope(pic, id, env, uid)) { + struct pic_env *e; + + while (1) { + if (search_scope(pic, id, env, uid)) return true; - } - env = env->up; + e = pic_env_ptr(pic, env)->up; + if (e == NULL) + break; + env = pic_obj_value(e); } return false; } pic_value -pic_find_identifier(pic_state *pic, pic_value id, struct pic_env *env) +pic_find_identifier(pic_state *pic, pic_value id, pic_value env) { + struct pic_env *e; pic_value uid; while (! search(pic, id, env, &uid)) { if (pic_sym_p(pic, id)) { - while (env->up != NULL) { - env = env->up; + while (1) { + e = pic_env_ptr(pic, env); + if (e->up == NULL) + break; + env = pic_obj_value(e->up); } return pic_add_identifier(pic, id, env); } - env = pic_id_ptr(pic, id)->env; /* do not overwrite id first */ + env = pic_obj_value(pic_id_ptr(pic, id)->env); /* do not overwrite id first */ id = pic_obj_value(pic_id_ptr(pic, id)->u.id); } return uid; @@ -127,18 +134,18 @@ shadow_macro(pic_state *pic, pic_value uid) } } -static pic_value expand(pic_state *, pic_value, struct pic_env *, pic_value); -static pic_value expand_lambda(pic_state *, pic_value, struct pic_env *); +static pic_value expand(pic_state *, pic_value expr, pic_value env, pic_value deferred); +static pic_value expand_lambda(pic_state *, pic_value expr, pic_value env); static pic_value -expand_var(pic_state *pic, pic_value id, struct pic_env *env, pic_value deferred) +expand_var(pic_state *pic, pic_value id, pic_value env, pic_value deferred) { pic_value mac, functor; functor = pic_find_identifier(pic, id, env); if (find_macro(pic, functor, &mac)) { - return expand(pic, pic_call(pic, mac, 2, id, pic_obj_value(env)), env, deferred); + return expand(pic, pic_call(pic, mac, 2, id, env), env, deferred); } return functor; } @@ -150,7 +157,7 @@ expand_quote(pic_state *pic, pic_value expr) } static pic_value -expand_list(pic_state *pic, pic_value obj, struct pic_env *env, pic_value deferred) +expand_list(pic_state *pic, pic_value obj, pic_value env, pic_value deferred) { size_t ai = pic_enter(pic); pic_value x, head, tail; @@ -179,7 +186,7 @@ expand_defer(pic_state *pic, pic_value expr, pic_value deferred) } static void -expand_deferred(pic_state *pic, pic_value deferred, struct pic_env *env) +expand_deferred(pic_state *pic, pic_value deferred, pic_value env) { pic_value defer, val, src, dst, it; @@ -198,10 +205,10 @@ expand_deferred(pic_state *pic, pic_value deferred, struct pic_env *env) } static pic_value -expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env) +expand_lambda(pic_state *pic, pic_value expr, pic_value env) { pic_value formal, body; - struct pic_env *in; + pic_value in; pic_value a, deferred; in = pic_make_env(pic, env); @@ -224,7 +231,7 @@ expand_lambda(pic_state *pic, pic_value expr, struct pic_env *env) } static pic_value -expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) +expand_define(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) { pic_value id, uid, val; @@ -240,7 +247,7 @@ expand_define(pic_state *pic, pic_value expr, struct pic_env *env, pic_value def } static pic_value -expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) +expand_defmacro(pic_state *pic, pic_value expr, pic_value env) { pic_value pic_compile(pic_state *, pic_value); pic_value id, uid, val; @@ -261,7 +268,7 @@ expand_defmacro(pic_state *pic, pic_value expr, struct pic_env *env) } static pic_value -expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) +expand_node(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) { switch (pic_type(pic, expr)) { case PIC_TYPE_ID: @@ -294,7 +301,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer } if (find_macro(pic, functor, &mac)) { - return expand(pic, pic_call(pic, mac, 2, expr, pic_obj_value(env)), env, deferred); + return expand(pic, pic_call(pic, mac, 2, expr, env), env, deferred); } } return expand_list(pic, expr, env, deferred); @@ -305,7 +312,7 @@ expand_node(pic_state *pic, pic_value expr, struct pic_env *env, pic_value defer } static pic_value -expand(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) +expand(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) { size_t ai = pic_enter(pic); pic_value v; @@ -318,7 +325,7 @@ expand(pic_state *pic, pic_value expr, struct pic_env *env, pic_value deferred) } pic_value -pic_expand(pic_state *pic, pic_value expr, struct pic_env *env) +pic_expand(pic_state *pic, pic_value expr, pic_value env) { pic_value v, deferred; diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index f2b758d2..0a1f20db 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -890,8 +890,7 @@ pic_defvar(pic_state *pic, const char *name, pic_value init, pic_value conv) void pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) { - pic_value sym, uid; - struct pic_env *env; + pic_value sym, uid, env; sym = pic_intern_cstr(pic, name); @@ -907,8 +906,7 @@ pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) pic_value pic_ref(pic_state *pic, const char *lib, const char *name) { - pic_value sym, uid; - struct pic_env *env; + pic_value sym, uid, env; sym = pic_intern_cstr(pic, name); @@ -925,8 +923,7 @@ pic_ref(pic_state *pic, const char *lib, const char *name) void pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) { - pic_value sym, uid; - struct pic_env *env; + pic_value sym, uid, env; sym = pic_intern_cstr(pic, name); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 987d80ab..065ad248 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -77,17 +77,17 @@ pic_features(pic_state *pic) return pic->features; } -#define import_builtin_syntax(name) do { \ - pic_value nick, real; \ - nick = pic_intern_lit(pic, "builtin:" name); \ - real = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, nick, real, pic->lib->env); \ +#define import_builtin_syntax(name) do { \ + pic_value nick, real; \ + nick = pic_intern_lit(pic, "builtin:" name); \ + real = pic_intern_lit(pic, name); \ + pic_put_identifier(pic, nick, real, pic_obj_value(pic->lib->env)); \ } while (0) -#define declare_vm_procedure(name) do { \ - pic_value sym; \ - sym = pic_intern_lit(pic, name); \ - pic_put_identifier(pic, sym, sym, pic->lib->env); \ +#define declare_vm_procedure(name) do { \ + pic_value sym; \ + sym = pic_intern_lit(pic, name); \ + pic_put_identifier(pic, sym, sym, pic_obj_value(pic->lib->env)); \ } while (0) void pic_init_bool(pic_state *); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index f8dabf80..20941f70 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -35,13 +35,13 @@ pic_intern(pic_state *pic, pic_value str) } pic_value -pic_make_identifier(pic_state *pic, pic_value base, struct pic_env *env) +pic_make_identifier(pic_state *pic, pic_value base, pic_value env) { pic_id *id; id = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); id->u.id = pic_id_ptr(pic, base); - id->env = env; + id->env = pic_env_ptr(pic, env); return pic_obj_value(id); } @@ -131,7 +131,7 @@ pic_symbol_make_identifier(pic_state *pic) pic_assert_type(pic, id, id); pic_assert_type(pic, env, env); - return pic_make_identifier(pic, id, pic_env_ptr(env)); + return pic_make_identifier(pic, id, env); } static pic_value From d851273f60151de473a9d1fcf18891f332292d9d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 16:51:24 +0900 Subject: [PATCH 068/119] struct pic_port * -> pic_value --- contrib/20.r7rs/src/file.c | 6 +-- contrib/20.r7rs/src/load.c | 3 +- contrib/40.srfi/src/106.c | 6 +-- extlib/benz/error.c | 2 +- extlib/benz/include/picrin.h | 19 +++---- extlib/benz/include/picrin/object.h | 2 +- extlib/benz/load.c | 4 +- extlib/benz/port.c | 80 ++++++++++++++++------------- extlib/benz/proc.c | 4 +- extlib/benz/read.c | 8 +-- extlib/benz/write.c | 53 +++++++++++-------- 11 files changed, 103 insertions(+), 84 deletions(-) diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 83fd231f..2c0b080e 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -12,7 +12,7 @@ file_error(pic_state *pic, const char *msg) pic_error(pic, "file", msg, pic_nil_value(pic)); } -static struct pic_port * +static pic_value open_file(pic_state *pic, const char *fname, const char *mode) { FILE *fp; @@ -30,7 +30,7 @@ pic_file_open_input_file(pic_state *pic) pic_get_args(pic, "z", &fname); - return pic_obj_value(open_file(pic, fname, "r")); + return open_file(pic, fname, "r"); } pic_value @@ -40,7 +40,7 @@ pic_file_open_output_file(pic_state *pic) pic_get_args(pic, "z", &fname); - return pic_obj_value(open_file(pic, fname, "w")); + return open_file(pic, fname, "w"); } pic_value diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index 15004dc0..cc02bb2c 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -9,9 +9,8 @@ static pic_value pic_load_load(pic_state *pic) { - pic_value envid; + pic_value envid, port; char *fn; - struct pic_port *port; FILE *fp; pic_get_args(pic, "z|o", &fn, &envid); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index f673c088..0be9f6d9 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -320,7 +320,7 @@ xf_socket_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) return 0; } -static struct pic_port * +static pic_value make_socket_port(pic_state *pic, struct pic_socket_t *sock, const char *mode) { xFILE *fp; @@ -346,7 +346,7 @@ pic_socket_socket_input_port(pic_state *pic) sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); - return pic_obj_value(make_socket_port(pic, sock, "r")); + return make_socket_port(pic, sock, "r"); } static pic_value @@ -361,7 +361,7 @@ pic_socket_socket_output_port(pic_state *pic) sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); - return pic_obj_value(make_socket_port(pic, sock, "w")); + return make_socket_port(pic, sock, "w"); } static pic_value diff --git a/extlib/benz/error.c b/extlib/benz/error.c index dff5d0e5..4dde9009 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -28,7 +28,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...) err = pic_vstrf_value(pic, fmt, ap); va_end(ap); - xfprintf(pic, pic_stderr(pic)->file, "warn: %s\n", pic_str(pic, err)); + pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_str(pic, err)); } void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index d0e75128..1e87f907 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -51,7 +51,6 @@ typedef struct { } pic_value; #endif -struct pic_port; struct pic_error; typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); @@ -275,8 +274,8 @@ void *pic_default_allocf(void *, void *, size_t); pic_errorf(pic, "expected " #type ", but got ~s", v); \ } -struct pic_port *pic_make_port(pic_state *, xFILE *file); -void pic_close_port(pic_state *, struct pic_port *port); +pic_value pic_make_port(pic_state *, xFILE *file); +void pic_close_port(pic_state *, pic_value port); #define pic_void(exec) \ pic_void_(PIC_GENSYM(ai), exec) @@ -286,14 +285,14 @@ void pic_close_port(pic_state *, struct pic_port *port); pic_leave(pic, ai); \ } while (0) -pic_value pic_read(pic_state *, struct pic_port *); +pic_value pic_read(pic_state *, pic_value port); pic_value pic_read_cstr(pic_state *, const char *); pic_value pic_expand(pic_state *, pic_value program, pic_value env); pic_value pic_eval(pic_state *, pic_value program, const char *lib); -void pic_load(pic_state *, struct pic_port *); +void pic_load(pic_state *, pic_value port); void pic_load_cstr(pic_state *, const char *); pic_value pic_make_var(pic_state *, pic_value init, pic_value conv); @@ -347,14 +346,16 @@ void pic_warnf(pic_state *, const char *, ...); pic_value pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); -#define pic_stdin(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-input-port", 0)) -#define pic_stdout(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-output-port", 0)) -#define pic_stderr(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-error-port", 0)) +#define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0) +#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0) +#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0) + +xFILE *pic_fileno(pic_state *, pic_value port); pic_value pic_write(pic_state *, pic_value); /* returns given obj */ pic_value pic_fwrite(pic_state *, pic_value, xFILE *); void pic_printf(pic_state *, const char *, ...); -void pic_fprintf(pic_state *, struct pic_port *, const char *, ...); +void pic_fprintf(pic_state *, pic_value port, const char *, ...); pic_value pic_display(pic_state *, pic_value); pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 38a155bc..a96edb3d 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -144,10 +144,10 @@ struct pic_checkpoint { #define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_env_ptr(pic, o) ((struct pic_env *)pic_obj_ptr(o)) +#define pic_port_ptr(pic, o) ((struct pic_port *)pic_obj_ptr(o)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) #define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) -#define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v)) #define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) diff --git a/extlib/benz/load.c b/extlib/benz/load.c index 465ef80f..28ac61b0 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -5,7 +5,7 @@ #include "picrin.h" void -pic_load(pic_state *pic, struct pic_port *port) +pic_load(pic_state *pic, pic_value port) { pic_value form; size_t ai = pic_enter(pic); @@ -20,7 +20,7 @@ pic_load(pic_state *pic, struct pic_port *port) void pic_load_cstr(pic_state *pic, const char *str) { - struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); + pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); pic_try { pic_load(pic, port); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 2e94e9f9..f89abe56 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -8,23 +8,32 @@ #undef EOF #define EOF (-1) -struct pic_port * +pic_value pic_make_port(pic_state *pic, xFILE *file) { struct pic_port *port; port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); port->file = file; - return port; + + return pic_obj_value(port); +} + +xFILE * +pic_fileno(pic_state PIC_UNUSED(*pic), pic_value port) +{ + return pic_port_ptr(pic, port)->file; } void -pic_close_port(pic_state *pic, struct pic_port *port) +pic_close_port(pic_state *pic, pic_value port) { - if (port->file->flag == 0) { + xFILE *file = pic_fileno(pic, port); + + if (file->flag == 0) { return; } - if (xfclose(pic, port->file) == EOF) { + if (xfclose(pic, file) == EOF) { pic_errorf(pic, "close-port: failure"); } } @@ -36,7 +45,7 @@ pic_port_input_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_READ) != 0) { + if (pic_port_p(pic, v) && (pic_fileno(pic, v)->flag & X_READ) != 0) { return pic_true_value(pic); } else { return pic_false_value(pic); @@ -50,7 +59,7 @@ pic_port_output_port_p(pic_state *pic) pic_get_args(pic, "o", &v); - if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_WRITE) != 0) { + if (pic_port_p(pic, v) && (pic_fileno(pic, v)->flag & X_WRITE) != 0) { return pic_true_value(pic); } else { @@ -89,17 +98,17 @@ pic_port_eof_object(pic_state *pic) static pic_value pic_port_port_open_p(pic_state *pic) { - struct pic_port *port; + pic_value port; pic_get_args(pic, "p", &port); - return pic_bool_value(pic, port->file->flag != 0); + return pic_bool_value(pic, pic_fileno(pic, port)->flag != 0); } static pic_value pic_port_close_port(pic_state *pic) { - struct pic_port *port; + pic_value port; pic_get_args(pic, "p", &port); @@ -109,7 +118,7 @@ pic_port_close_port(pic_state *pic) } #define assert_port_profile(port, flags, caller) do { \ - if ((port->file->flag & (flags)) != (flags)) { \ + if ((pic_fileno(pic, port)->flag & (flags)) != (flags)) { \ switch (flags) { \ case X_WRITE: \ pic_errorf(pic, caller ": expected output port"); \ @@ -117,7 +126,7 @@ pic_port_close_port(pic_state *pic) pic_errorf(pic, caller ": expected input port"); \ } \ } \ - if (port->file->flag == 0) { \ + if (pic_fileno(pic, port)->flag == 0) { \ pic_errorf(pic, caller ": expected open port"); \ } \ } while (0) @@ -133,7 +142,7 @@ pic_port_open_input_bytevector(pic_state *pic) buf = pic_blob(pic, blob, &len); - return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r"))); + return pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r")); } static pic_value @@ -141,13 +150,13 @@ pic_port_open_output_bytevector(pic_state *pic) { pic_get_args(pic, ""); - return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w"))); + return pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w")); } static pic_value pic_port_get_output_bytevector(pic_state *pic) { - struct pic_port *port = pic_stdout(pic); + pic_value port = pic_stdout(pic); const char *buf; int len; @@ -155,7 +164,7 @@ pic_port_get_output_bytevector(pic_state *pic) assert_port_profile(port, X_WRITE, "get-output-bytevector"); - if (xfget_buf(pic, port->file, &buf, &len) < 0) { + if (xfget_buf(pic, pic_fileno(pic, port), &buf, &len) < 0) { pic_errorf(pic, "port was not created by open-output-bytevector"); } return pic_blob_value(pic, (unsigned char *)buf, len); @@ -163,12 +172,12 @@ pic_port_get_output_bytevector(pic_state *pic) static pic_value pic_port_read_u8(pic_state *pic){ - struct pic_port *port = pic_stdin(pic); + pic_value port = pic_stdin(pic); int c; pic_get_args(pic, "|p", &port); assert_port_profile(port, X_READ, "read-u8"); - if ((c = xfgetc(pic, port->file)) == EOF) { + if ((c = xfgetc(pic, pic_fileno(pic, port))) == EOF) { return pic_eof_object(pic); } @@ -179,18 +188,18 @@ static pic_value pic_port_peek_u8(pic_state *pic) { int c; - struct pic_port *port = pic_stdin(pic); + pic_value port = pic_stdin(pic); pic_get_args(pic, "|p", &port); assert_port_profile(port, X_READ, "peek-u8"); - c = xfgetc(pic, port->file); + c = xfgetc(pic, pic_fileno(pic, port)); if (c == EOF) { return pic_eof_object(pic); } else { - xungetc(c, port->file); + xungetc(c, pic_fileno(pic, port)); return pic_int_value(pic, c); } } @@ -198,7 +207,7 @@ pic_port_peek_u8(pic_state *pic) static pic_value pic_port_u8_ready_p(pic_state *pic) { - struct pic_port *port = pic_stdin(pic); + pic_value port = pic_stdin(pic); pic_get_args(pic, "|p", &port); @@ -211,7 +220,7 @@ pic_port_u8_ready_p(pic_state *pic) static pic_value pic_port_read_bytevector(pic_state *pic) { - struct pic_port *port = pic_stdin(pic); + pic_value port = pic_stdin(pic); unsigned char *buf; int k, i; @@ -221,7 +230,7 @@ pic_port_read_bytevector(pic_state *pic) buf = pic_blob(pic, pic_blob_value(pic, NULL, k), NULL); - i = xfread(pic, buf, sizeof(char), k, port->file); + i = xfread(pic, buf, sizeof(char), k, pic_fileno(pic, port)); if (i == 0) { return pic_eof_object(pic); } @@ -231,8 +240,7 @@ pic_port_read_bytevector(pic_state *pic) static pic_value pic_port_read_bytevector_ip(pic_state *pic) { - struct pic_port *port; - pic_value bv; + pic_value bv, port; unsigned char *buf; int n, start, end, i, len; @@ -252,7 +260,7 @@ pic_port_read_bytevector_ip(pic_state *pic) VALID_RANGE(pic, len, start, end); assert_port_profile(port, X_READ, "read-bytevector!"); - i = xfread(pic, buf + start, 1, end - start, port->file); + i = xfread(pic, buf + start, 1, end - start, pic_fileno(pic, port)); if (i == 0) { return pic_eof_object(pic); } @@ -263,13 +271,13 @@ static pic_value pic_port_write_u8(pic_state *pic) { int i; - struct pic_port *port = pic_stdout(pic); + pic_value port = pic_stdout(pic); pic_get_args(pic, "i|p", &i, &port); assert_port_profile(port, X_WRITE, "write-u8"); - xfputc(pic, i, port->file); + xfputc(pic, i, pic_fileno(pic, port)); return pic_undef_value(pic); } @@ -277,7 +285,7 @@ static pic_value pic_port_write_bytevector(pic_state *pic) { pic_value blob; - struct pic_port *port; + pic_value port; unsigned char *buf; int n, start, end, len, done; @@ -299,7 +307,7 @@ pic_port_write_bytevector(pic_state *pic) done = 0; while (done < end - start) { - done += xfwrite(pic, buf + start + done, 1, end - start - done, port->file); + done += xfwrite(pic, buf + start + done, 1, end - start - done, pic_fileno(pic, port)); /* FIXME: error check... */ } return pic_undef_value(pic); @@ -308,28 +316,28 @@ pic_port_write_bytevector(pic_state *pic) static pic_value pic_port_flush(pic_state *pic) { - struct pic_port *port = pic_stdout(pic); + pic_value port = pic_stdout(pic); pic_get_args(pic, "|p", &port); assert_port_profile(port, X_WRITE, "flush-output-port"); - xfflush(pic, port->file); + xfflush(pic, pic_fileno(pic, port)); return pic_undef_value(pic); } static pic_value coerce_port(pic_state *pic) { - struct pic_port *port; + pic_value port; pic_get_args(pic, "p", &port); - return pic_obj_value(port); + return port; } #define DEFINE_PORT(pic, name, file) \ - pic_defvar(pic, name, pic_obj_value(pic_make_port(pic, file)), coerce) + pic_defvar(pic, name, pic_make_port(pic, file), coerce) void pic_init_port(pic_state *pic) diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 0a1f20db..b985189d 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -25,7 +25,7 @@ * s pic_value * string object * b pic_value * bytevector object * l pic_value * lambda object - * p struct pic_port ** port object + * p pic_value * port object * d pic_value * dictionary object * e struct pic_error ** error object * r struct pic_record ** record object @@ -147,7 +147,6 @@ pic_get_args(pic_state *pic, const char *format, ...) #define PTR_CASE(c, type, ctype) \ VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) - PTR_CASE('p', port, struct pic_port *) PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) @@ -159,6 +158,7 @@ pic_get_args(pic_state *pic, const char *format, ...) OBJ_CASE('b', blob) OBJ_CASE('v', vec) OBJ_CASE('d', dict) + OBJ_CASE('p', port) default: pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 077a8334..ac3deb9f 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -818,11 +818,11 @@ pic_reader_destroy(pic_state *pic) } pic_value -pic_read(pic_state *pic, struct pic_port *port) +pic_read(pic_state *pic, pic_value port) { size_t ai = pic_enter(pic); pic_value val; - xFILE *file = port->file; + xFILE *file = pic_fileno(pic, port); int c; while ((c = skip(pic, file, next(pic, file))) != EOF) { @@ -844,7 +844,7 @@ pic_read(pic_state *pic, struct pic_port *port) pic_value pic_read_cstr(pic_state *pic, const char *str) { - struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); + pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); pic_value form; pic_try { @@ -863,7 +863,7 @@ pic_read_cstr(pic_state *pic, const char *str) static pic_value pic_read_read(pic_state *pic) { - struct pic_port *port = pic_stdin(pic); + pic_value port = pic_stdin(pic); pic_get_args(pic, "|p", &port); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 38700f58..8d2276ed 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -411,7 +411,7 @@ write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op) pic_value pic_write(pic_state *pic, pic_value obj) { - return pic_fwrite(pic, obj, pic_stdout(pic)->file); + return pic_fwrite(pic, obj, pic_fileno(pic, pic_stdout(pic))); } pic_value @@ -425,7 +425,7 @@ pic_fwrite(pic_state *pic, pic_value obj, xFILE *file) pic_value pic_display(pic_state *pic, pic_value obj) { - return pic_fdisplay(pic, obj, pic_stdout(pic)->file); + return pic_fdisplay(pic, obj, pic_fileno(pic, pic_stdout(pic))); } pic_value @@ -437,63 +437,74 @@ pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file) } void -pic_printf(pic_state *pic, const char *fmt, ...) +pic_vfprintf(pic_state *pic, pic_value port, const char *fmt, va_list ap) { - xFILE *file = pic_stdout(pic)->file; - va_list ap; + xFILE *file = pic_fileno(pic, port); pic_value str; - va_start(ap, fmt); - str = pic_vstrf_value(pic, fmt, ap); - va_end(ap); - xfprintf(pic, file, "%s", pic_str(pic, str)); xfflush(pic, file); } +void +pic_fprintf(pic_state *pic, pic_value port, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + pic_vfprintf(pic, port, fmt, ap); + va_end(ap); +} + +void +pic_printf(pic_state *pic, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + pic_vfprintf(pic, pic_stdout(pic), fmt, ap); + va_end(ap); +} + static pic_value pic_write_write(pic_state *pic) { - pic_value v; - struct pic_port *port = pic_stdout(pic); + pic_value v, port = pic_stdout(pic); pic_get_args(pic, "o|p", &v, &port); - write(pic, v, port->file, WRITE_MODE, OP_WRITE); + write(pic, v, pic_fileno(pic, port), WRITE_MODE, OP_WRITE); return pic_undef_value(pic); } static pic_value pic_write_write_simple(pic_state *pic) { - pic_value v; - struct pic_port *port = pic_stdout(pic); + pic_value v, port = pic_stdout(pic); pic_get_args(pic, "o|p", &v, &port); - write(pic, v, port->file, WRITE_MODE, OP_WRITE_SIMPLE); + write(pic, v, pic_fileno(pic, port), WRITE_MODE, OP_WRITE_SIMPLE); return pic_undef_value(pic); } static pic_value pic_write_write_shared(pic_state *pic) { - pic_value v; - struct pic_port *port = pic_stdout(pic); + pic_value v, port = pic_stdout(pic); pic_get_args(pic, "o|p", &v, &port); - write(pic, v, port->file, WRITE_MODE, OP_WRITE_SHARED); + write(pic, v, pic_fileno(pic, port), WRITE_MODE, OP_WRITE_SHARED); return pic_undef_value(pic); } static pic_value pic_write_display(pic_state *pic) { - pic_value v; - struct pic_port *port = pic_stdout(pic); + pic_value v, port = pic_stdout(pic); pic_get_args(pic, "o|p", &v, &port); - write(pic, v, port->file, DISPLAY_MODE, OP_WRITE); + write(pic, v, pic_fileno(pic, port), DISPLAY_MODE, OP_WRITE); return pic_undef_value(pic); } From da30be167b15eee8ec6ed9c31c0a49a433096c5e Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 16:59:05 +0900 Subject: [PATCH 069/119] struct pic_error * -> pic_value --- extlib/benz/debug.c | 2 +- extlib/benz/error.c | 34 +++++++++++++++-------------- extlib/benz/include/picrin.h | 2 -- extlib/benz/include/picrin/object.h | 6 ++--- extlib/benz/proc.c | 2 -- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 6ba134c9..21a5211d 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -45,7 +45,7 @@ pic_print_backtrace(pic_state *pic, xFILE *file) struct pic_error *e; pic_value elem, it; - e = pic_error_ptr(pic->err); + e = pic_error_ptr(pic, pic->err); if (! pic_eq_p(pic, pic_obj_value(e->type), pic_intern_lit(pic, ""))) { pic_fwrite(pic, pic_obj_value(e->type), file); xfprintf(pic, file, " "); diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 4dde9009..b854508f 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -88,7 +88,7 @@ pic_pop_handler(pic_state *pic) return pic_obj_value(*--pic->xp); } -struct pic_error * +pic_value pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { struct pic_error *e; @@ -102,7 +102,7 @@ pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs e->irrs = irrs; e->stack = pic_str_ptr(pic, stack); - return e; + return pic_obj_value(e); } pic_value @@ -136,11 +136,7 @@ pic_raise(pic_state *pic, pic_value err) void pic_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { - struct pic_error *e; - - e = pic_make_error(pic, type, msg, irrs); - - pic_raise(pic, pic_obj_value(e)); + pic_raise(pic, pic_make_error(pic, type, msg, irrs)); } static pic_value @@ -204,31 +200,37 @@ pic_error_error_object_p(pic_state *pic) static pic_value pic_error_error_object_message(pic_state *pic) { - struct pic_error *e; + pic_value e; - pic_get_args(pic, "e", &e); + pic_get_args(pic, "o", &e); - return pic_obj_value(e->msg); + pic_assert_type(pic, e, error); + + return pic_obj_value(pic_error_ptr(pic, e)->msg); } static pic_value pic_error_error_object_irritants(pic_state *pic) { - struct pic_error *e; + pic_value e; - pic_get_args(pic, "e", &e); + pic_get_args(pic, "o", &e); - return e->irrs; + pic_assert_type(pic, e, error); + + return pic_error_ptr(pic, e)->irrs; } static pic_value pic_error_error_object_type(pic_state *pic) { - struct pic_error *e; + pic_value e; - pic_get_args(pic, "e", &e); + pic_get_args(pic, "o", &e); - return pic_obj_value(e->type); + pic_assert_type(pic, e, error); + + return pic_obj_value(pic_error_ptr(pic, e)->type); } void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 1e87f907..961297fa 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -51,8 +51,6 @@ typedef struct { } pic_value; #endif -struct pic_error; - typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n); pic_state *pic_open(pic_allocf f, void *userdata); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index a96edb3d..3fd96b8f 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -145,9 +145,9 @@ struct pic_checkpoint { #define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) #define pic_env_ptr(pic, o) ((struct pic_env *)pic_obj_ptr(o)) #define pic_port_ptr(pic, o) ((struct pic_port *)pic_obj_ptr(o)) +#define pic_error_ptr(pic, o) ((struct pic_error *)pic_obj_ptr(o)) #define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) #define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) -#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v)) #define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) @@ -173,9 +173,9 @@ struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); pic_value pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); -struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); -struct pic_error *pic_make_error(pic_state *, const char *, const char *, pic_value); pic_value pic_make_env(pic_state *, pic_value env); +pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs); +struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index b985189d..25bb8f73 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -27,7 +27,6 @@ * l pic_value * lambda object * p pic_value * port object * d pic_value * dictionary object - * e struct pic_error ** error object * r struct pic_record ** record object * * | optional operator @@ -147,7 +146,6 @@ pic_get_args(pic_state *pic, const char *format, ...) #define PTR_CASE(c, type, ctype) \ VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) - PTR_CASE('e', error, struct pic_error *) PTR_CASE('r', rec, struct pic_record *) #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) From 73f25786481d3a8b2d65856c6ad4a41d0d47e1ef Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 17:27:13 +0900 Subject: [PATCH 070/119] struct pic_record * -> pic_value --- extlib/benz/eval.c | 19 ++++++++++------ extlib/benz/include/picrin/object.h | 5 ++--- extlib/benz/proc.c | 35 ++++++++++++----------------- extlib/benz/record.c | 14 ++++++------ 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 9c2638a1..8e62986e 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -137,16 +137,21 @@ find_var(pic_state *pic, analyze_scope *scope, pic_value sym) static void define_var(pic_state *pic, analyze_scope *scope, pic_value sym) { - if (search_scope(pic, scope, sym)) { - if (scope->depth > 0 || pic_weak_has(pic, pic->globals, sym)) { + if (scope->depth > 0) { + /* local */ + if (search_scope(pic, scope, sym)) { pic_warnf(pic, "redefining variable: ~s", sym); + return; } - return; + pic_dict_set(pic, scope->locals, sym, pic_true_value(pic)); + } else { + /* global */ + if (pic_weak_has(pic, pic->globals, sym)) { + pic_warnf(pic, "redefining variable: ~s", sym); + return; + } + pic_weak_set(pic, pic->globals, sym, pic_invalid_value()); } - - pic_weak_set(pic, pic->globals, sym, pic_invalid_value()); - - pic_dict_set(pic, scope->locals, sym, pic_true_value(pic)); } static pic_value analyze(pic_state *, analyze_scope *, pic_value); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 3fd96b8f..ddeac151 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -146,8 +146,7 @@ struct pic_checkpoint { #define pic_env_ptr(pic, o) ((struct pic_env *)pic_obj_ptr(o)) #define pic_port_ptr(pic, o) ((struct pic_port *)pic_obj_ptr(o)) #define pic_error_ptr(pic, o) ((struct pic_error *)pic_obj_ptr(o)) -#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o)) -#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v)) +#define pic_rec_ptr(pic, o) ((struct pic_record *)pic_obj_ptr(o)) #define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) @@ -175,7 +174,7 @@ pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); pic_value pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); pic_value pic_make_env(pic_state *, pic_value env); pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs); -struct pic_record *pic_make_rec(pic_state *, pic_value, pic_value); +pic_value pic_make_rec(pic_state *, pic_value type, pic_value datum); pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 25bb8f73..9a93e045 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -27,10 +27,11 @@ * l pic_value * lambda object * p pic_value * port object * d pic_value * dictionary object - * r struct pic_record ** record object + * r pic_value * record object * * | optional operator - * * int *, pic_value ** variable length operator + * * int *, pic_value ** variable length operator + * ---- ---- ---- */ int @@ -143,11 +144,6 @@ pic_get_args(pic_state *pic, const char *format, ...) VAL_CASE('c', char, char, pic_char(pic, v)) VAL_CASE('z', str, const char *, pic_str(pic, v)) -#define PTR_CASE(c, type, ctype) \ - VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) - - PTR_CASE('r', rec, struct pic_record *) - #define OBJ_CASE(c, type) VAL_CASE(c, type, pic_value, v) OBJ_CASE('m', sym) @@ -157,6 +153,7 @@ pic_get_args(pic_state *pic, const char *format, ...) OBJ_CASE('v', vec) OBJ_CASE('d', dict) OBJ_CASE('p', port) + OBJ_CASE('r', rec) default: pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); @@ -182,6 +179,9 @@ vm_gref(pic_state *pic, pic_value uid) { pic_value val; + if (! pic_weak_has(pic, pic->globals, uid)) { + pic_errorf(pic, "undefined variable ~s", uid); + } val = pic_weak_ref(pic, pic->globals, uid);; if (pic_invalid_p(pic, val)) { pic_errorf(pic, "uninitialized global variable: ~s", uid); @@ -192,6 +192,9 @@ vm_gref(pic_state *pic, pic_value uid) static void vm_gset(pic_state *pic, pic_value uid, pic_value value) { + if (! pic_weak_has(pic, pic->globals, uid)) { + pic_errorf(pic, "undefined variable ~s", uid); + } pic_weak_set(pic, pic->globals, uid, value); } @@ -904,35 +907,25 @@ pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) pic_value pic_ref(pic_state *pic, const char *lib, const char *name) { - pic_value sym, uid, env; + pic_value sym, env; sym = pic_intern_cstr(pic, name); env = pic_library_environment(pic, lib); - uid = pic_find_identifier(pic, sym, env); - if (! pic_weak_has(pic, pic->globals, uid)) { - pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); - } - - return vm_gref(pic, uid); + return vm_gref(pic, pic_find_identifier(pic, sym, env)); } void pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) { - pic_value sym, uid, env; + pic_value sym, env; sym = pic_intern_cstr(pic, name); env = pic_library_environment(pic, lib); - uid = pic_find_identifier(pic, sym, env); - if (! pic_weak_has(pic, pic->globals, uid)) { - pic_errorf(pic, "symbol \"%s\" not defined in library %s", name, lib); - } - - vm_gset(pic, uid, val); + vm_gset(pic, pic_find_identifier(pic, sym, env), val); } pic_value diff --git a/extlib/benz/record.c b/extlib/benz/record.c index 82d58fd8..dee14562 100644 --- a/extlib/benz/record.c +++ b/extlib/benz/record.c @@ -5,7 +5,7 @@ #include "picrin.h" #include "picrin/object.h" -struct pic_record * +pic_value pic_make_rec(pic_state *pic, pic_value type, pic_value datum) { struct pic_record *rec; @@ -14,7 +14,7 @@ pic_make_rec(pic_state *pic, pic_value type, pic_value datum) rec->type = type; rec->datum = datum; - return rec; + return pic_obj_value(rec); } static pic_value @@ -24,7 +24,7 @@ pic_rec_make_record(pic_state *pic) pic_get_args(pic, "oo", &type, &datum); - return pic_obj_value(pic_make_rec(pic, type, datum)); + return pic_make_rec(pic, type, datum); } static pic_value @@ -40,21 +40,21 @@ pic_rec_record_p(pic_state *pic) static pic_value pic_rec_record_type(pic_state *pic) { - struct pic_record *rec; + pic_value rec; pic_get_args(pic, "r", &rec); - return rec->type; + return pic_rec_ptr(pic, rec)->type; } static pic_value pic_rec_record_datum(pic_state *pic) { - struct pic_record *rec; + pic_value rec; pic_get_args(pic, "r", &rec); - return rec->datum; + return pic_rec_ptr(pic, rec)->datum; } void From d3b188e44dd5b96ad7d98fe81dcd82734f310898 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 17:59:46 +0900 Subject: [PATCH 071/119] cleanup file.h --- extlib/benz/file.c | 34 ++++++++++++------------ extlib/benz/include/picrin/file.h | 43 +++++++++++++------------------ extlib/benz/port.c | 2 +- extlib/benz/read.c | 2 +- 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index d5dc60b9..5032b480 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -35,6 +35,18 @@ int xfclose(pic_state *pic, xFILE *fp) { return fp->vtable.close(pic, fp->vtable.cookie); } +void xclearerr(pic_state PIC_UNUSED(*pic), xFILE *fp) { + fp->flag &= ~(X_EOF | X_ERR); +} + +int xfeof(pic_state PIC_UNUSED(*pic), xFILE *fp) { + return (fp->flag & X_EOF) != 0; +} + +int xferror(pic_state PIC_UNUSED(*pic), xFILE *fp) { + return (fp->flag & X_ERR) != 0; +} + int x_fillbuf(pic_state *pic, xFILE *fp) { int bufsize; @@ -187,7 +199,7 @@ char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { return (c == EOF && buf == s) ? NULL : s; } -int xungetc(int c, xFILE *fp) { +int xungetc(pic_state PIC_UNUSED(*pic), int c, xFILE *fp) { unsigned char uc = c; if (c == EOF || fp->base == fp->ptr) { @@ -211,7 +223,7 @@ size_t xfread(pic_state *pic, void *ptr, size_t size, size_t count, xFILE *fp) { if ((c = x_fillbuf(pic, fp)) == EOF) { return (size * count - nbytes) / size; } else { - xungetc(c, fp); + xungetc(pic, c, fp); } } memcpy(bptr, fp->ptr, nbytes); @@ -240,6 +252,10 @@ size_t xfwrite(pic_state *pic, const void *ptr, size_t size, size_t count, xFILE return count; } +#define XSEEK_CUR 0 +#define XSEEK_END 1 +#define XSEEK_SET 2 + long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { long s; @@ -535,17 +551,3 @@ xFILE *xfopen_null(pic_state PIC_UNUSED(*pic), const char *mode) { return xfunopen(pic, 0, 0, null_write, null_seek, null_close); } } - -#if 0 -int main() -{ - char buf[256]; - - xgets(buf); - - xprintf("%s\n", buf); - xprintf("hello\n"); - xprintf("hello\n"); - // xfflush(0); -} -#endif diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 2118934c..657fa2e6 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -25,10 +25,6 @@ typedef struct { int flag; /* mode of the file access */ } xFILE; -#define xstdin (&pic->files[0]) -#define xstdout (&pic->files[1]) -#define xstderr (&pic->files[2]) - enum { X_READ = 01, X_WRITE = 02, @@ -38,31 +34,28 @@ enum { X_LNBUF = 040 }; -#define xclearerr(p) ((p)->flag &= ~(X_EOF | X_ERR)) -#define xfeof(p) (((p)->flag & X_EOF) != 0) -#define xferror(p) (((p)->flag & X_ERR) != 0) +#define xstdin (&pic->files[0]) +#define xstdout (&pic->files[1]) +#define xstderr (&pic->files[2]) xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); +size_t xfread(pic_state *, void *ptr, size_t size, size_t count, xFILE *fp); +size_t xfwrite(pic_state *, const void *ptr, size_t size, size_t count, xFILE *fp); +long xfseek(pic_state *, xFILE *fp, long offset, int whence); /* 0:cur, 1:end, 2:set */ +int xfclose(pic_state *, xFILE *fp); -enum { - XSEEK_CUR, - XSEEK_END, - XSEEK_SET -}; +void xclearerr(pic_state *, xFILE *fp); +int xfeof(pic_state *, xFILE *fp); +int xferror(pic_state *, xFILE *fp); -size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); -size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); -long xfseek(pic_state *, xFILE *, long, int); -int xfflush(pic_state *, xFILE *); -int xfclose(pic_state *, xFILE *); - -int xfputc(pic_state *, int, xFILE *); -int xfgetc(pic_state *, xFILE *); -int xfputs(pic_state *, const char *, xFILE *); -char *xfgets(pic_state *, char *, int, xFILE *); -int xungetc(int, xFILE *); -int xfprintf(pic_state *, xFILE *, const char *, ...); -int xvfprintf(pic_state *, xFILE *, const char *, va_list); +int xfputc(pic_state *, int c, xFILE *fp); +int xfgetc(pic_state *, xFILE *fp); +int xfputs(pic_state *, const char *s, xFILE *fp); +char *xfgets(pic_state *, char *s, int size, xFILE *fp); +int xungetc(pic_state *, int c, xFILE *fp); +int xfflush(pic_state *, xFILE *fp); +int xfprintf(pic_state *, xFILE *fp, const char *fmt, ...); +int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); #if PIC_ENABLE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index f89abe56..69a564ff 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -199,7 +199,7 @@ pic_port_peek_u8(pic_state *pic) return pic_eof_object(pic); } else { - xungetc(c, pic_fileno(pic, port)); + xungetc(pic, c, pic_fileno(pic, port)); return pic_int_value(pic, c); } } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index ac3deb9f..6d3803fa 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -39,7 +39,7 @@ peek(pic_state *pic, xFILE *file) { int c; - xungetc((c = xfgetc(pic, file)), file); + xungetc(pic, (c = xfgetc(pic, file)), file); return c; } From fcb3182e087aae1dc113cd76640c0eb63be38f48 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 18:34:13 +0900 Subject: [PATCH 072/119] publish xfile API --- contrib/20.r7rs/src/file.c | 2 +- contrib/20.r7rs/src/load.c | 2 +- contrib/40.srfi/src/106.c | 2 +- extlib/benz/file.c | 4 --- extlib/benz/include/picrin.h | 53 ++++++++++++++++++++++++++---- extlib/benz/include/picrin/file.h | 33 ++----------------- extlib/benz/include/picrin/state.h | 2 +- extlib/benz/load.c | 2 +- extlib/benz/port.c | 8 ++--- extlib/benz/read.c | 2 +- 10 files changed, 59 insertions(+), 51 deletions(-) diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 2c0b080e..5cb99ffc 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -20,7 +20,7 @@ open_file(pic_state *pic, const char *fname, const char *mode) if ((fp = fopen(fname, mode)) == NULL) { file_error(pic, "could not open file..."); } - return pic_make_port(pic, xfopen_file(pic, fp, mode)); + return pic_open_port(pic, xfopen_file(pic, fp, mode)); } pic_value diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index cc02bb2c..b7459bb6 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -20,7 +20,7 @@ pic_load_load(pic_state *pic) pic_errorf(pic, "load: could not open file %s", fn); } - port = pic_make_port(pic, xfopen_file(pic, fp, "r")); + port = pic_open_port(pic, xfopen_file(pic, fp, "r")); pic_load(pic, port); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 0be9f6d9..73272825 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -331,7 +331,7 @@ make_socket_port(pic_state *pic, struct pic_socket_t *sock, const char *mode) fp = xfunopen(pic, sock, 0, xf_socket_write, xf_socket_seek, xf_socket_close); } - return pic_make_port(pic, fp); + return pic_open_port(pic, fp); } static pic_value diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 5032b480..66ec825c 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -252,10 +252,6 @@ size_t xfwrite(pic_state *pic, const void *ptr, size_t size, size_t count, xFILE return count; } -#define XSEEK_CUR 0 -#define XSEEK_END 1 -#define XSEEK_SET 2 - long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { long s; diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 961297fa..15604e5a 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -107,6 +107,12 @@ pic_value pic_vcall(pic_state *, pic_value proc, int, va_list); pic_value pic_apply(pic_state *, pic_value proc, int n, pic_value *argv); pic_value pic_applyk(pic_state *, pic_value proc, int n, pic_value *argv); +typedef struct xFILE xFILE; + +pic_value pic_open_port(pic_state *, xFILE *file); +xFILE *pic_fileno(pic_state *, pic_value port); +void pic_close_port(pic_state *, pic_value port); + PIC_INLINE int pic_int(pic_state *, pic_value i); PIC_INLINE double pic_float(pic_state *, pic_value f); PIC_INLINE char pic_char(pic_state *, pic_value c); @@ -255,6 +261,35 @@ int pic_str_cmp(pic_state *, pic_value str1, pic_value str2); int pic_str_hash(pic_state *, pic_value str); + +/* External I/O */ + +#define XSEEK_CUR 0 +#define XSEEK_END 1 +#define XSEEK_SET 2 + +xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); +size_t xfread(pic_state *, void *ptr, size_t size, size_t count, xFILE *fp); +size_t xfwrite(pic_state *, const void *ptr, size_t size, size_t count, xFILE *fp); +long xfseek(pic_state *, xFILE *fp, long offset, int whence); +int xfclose(pic_state *, xFILE *fp); + +void xclearerr(pic_state *, xFILE *fp); +int xfeof(pic_state *, xFILE *fp); +int xferror(pic_state *, xFILE *fp); + +int xfputc(pic_state *, int c, xFILE *fp); +int xfgetc(pic_state *, xFILE *fp); +int xfputs(pic_state *, const char *s, xFILE *fp); +char *xfgets(pic_state *, char *s, int size, xFILE *fp); +int xungetc(pic_state *, int c, xFILE *fp); +int xfflush(pic_state *, xFILE *fp); + +int xfprintf(pic_state *, xFILE *fp, const char *fmt, ...); +int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); + + + /* extra stuff */ @@ -272,15 +307,23 @@ void *pic_default_allocf(void *, void *, size_t); pic_errorf(pic, "expected " #type ", but got ~s", v); \ } -pic_value pic_make_port(pic_state *, xFILE *file); -void pic_close_port(pic_state *, pic_value port); +#define xstdin (&pic->files[0]) +#define xstdout (&pic->files[1]) +#define xstderr (&pic->files[2]) + +#if PIC_ENABLE_STDIO +xFILE *xfopen_file(pic_state *, FILE *, const char *mode); +#endif +xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); +int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len); +xFILE *xfopen_null(pic_state *, const char *mode); #define pic_void(exec) \ pic_void_(PIC_GENSYM(ai), exec) #define pic_void_(ai,exec) do { \ - size_t ai = pic_enter(pic); \ + size_t ai = pic_enter(pic); \ exec; \ - pic_leave(pic, ai); \ + pic_leave(pic, ai); \ } while (0) pic_value pic_read(pic_state *, pic_value port); @@ -348,8 +391,6 @@ void pic_print_backtrace(pic_state *, xFILE *); #define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0) #define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0) -xFILE *pic_fileno(pic_state *, pic_value port); - pic_value pic_write(pic_state *, pic_value); /* returns given obj */ pic_value pic_fwrite(pic_state *, pic_value, xFILE *); void pic_printf(pic_state *, const char *, ...); diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 657fa2e6..7595d4fb 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -8,7 +8,7 @@ extern "C" { #define XBUFSIZ 1024 #define XOPEN_MAX 1024 -typedef struct { +struct xFILE { /* buffer */ char buf[1]; /* fallback buffer */ long cnt; /* characters left */ @@ -23,7 +23,7 @@ typedef struct { int (*close)(pic_state *, void *); } vtable; int flag; /* mode of the file access */ -} xFILE; +}; enum { X_READ = 01, @@ -34,35 +34,6 @@ enum { X_LNBUF = 040 }; -#define xstdin (&pic->files[0]) -#define xstdout (&pic->files[1]) -#define xstderr (&pic->files[2]) - -xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); -size_t xfread(pic_state *, void *ptr, size_t size, size_t count, xFILE *fp); -size_t xfwrite(pic_state *, const void *ptr, size_t size, size_t count, xFILE *fp); -long xfseek(pic_state *, xFILE *fp, long offset, int whence); /* 0:cur, 1:end, 2:set */ -int xfclose(pic_state *, xFILE *fp); - -void xclearerr(pic_state *, xFILE *fp); -int xfeof(pic_state *, xFILE *fp); -int xferror(pic_state *, xFILE *fp); - -int xfputc(pic_state *, int c, xFILE *fp); -int xfgetc(pic_state *, xFILE *fp); -int xfputs(pic_state *, const char *s, xFILE *fp); -char *xfgets(pic_state *, char *s, int size, xFILE *fp); -int xungetc(pic_state *, int c, xFILE *fp); -int xfflush(pic_state *, xFILE *fp); -int xfprintf(pic_state *, xFILE *fp, const char *fmt, ...); -int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); - -#if PIC_ENABLE_STDIO -xFILE *xfopen_file(pic_state *, FILE *, const char *mode); -#endif -xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); -int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len); -xFILE *xfopen_null(pic_state *, const char *mode); #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index a6db373d..5246797c 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -10,9 +10,9 @@ extern "C" { #endif #include "picrin/khash.h" +#include "picrin/file.h" #include "picrin/irep.h" -#include "picrin/file.h" #include "picrin/read.h" #include "picrin/gc.h" diff --git a/extlib/benz/load.c b/extlib/benz/load.c index 28ac61b0..2ceefa11 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -20,7 +20,7 @@ pic_load(pic_state *pic, pic_value port) void pic_load_cstr(pic_state *pic, const char *str) { - pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); + pic_value port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r")); pic_try { pic_load(pic, port); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 69a564ff..f410f44f 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -9,7 +9,7 @@ #define EOF (-1) pic_value -pic_make_port(pic_state *pic, xFILE *file) +pic_open_port(pic_state *pic, xFILE *file) { struct pic_port *port; @@ -142,7 +142,7 @@ pic_port_open_input_bytevector(pic_state *pic) buf = pic_blob(pic, blob, &len); - return pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r")); + return pic_open_port(pic, xfopen_buf(pic, (char *)buf, len, "r")); } static pic_value @@ -150,7 +150,7 @@ pic_port_open_output_bytevector(pic_state *pic) { pic_get_args(pic, ""); - return pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w")); + return pic_open_port(pic, xfopen_buf(pic, NULL, 0, "w")); } static pic_value @@ -337,7 +337,7 @@ coerce_port(pic_state *pic) } #define DEFINE_PORT(pic, name, file) \ - pic_defvar(pic, name, pic_make_port(pic, file), coerce) + pic_defvar(pic, name, pic_open_port(pic, file), coerce) void pic_init_port(pic_state *pic) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 6d3803fa..b409264b 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -844,7 +844,7 @@ pic_read(pic_state *pic, pic_value port) pic_value pic_read_cstr(pic_state *pic, const char *str) { - pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r")); + pic_value port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r")); pic_value form; pic_try { From 4affb1c1ce3af2fe4e2c820f4945b465c605e1a5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 19:00:41 +0900 Subject: [PATCH 073/119] remove type.h and cont.h --- extlib/benz/eval.c | 4 +- extlib/benz/include/picrin.h | 37 ++-- extlib/benz/include/picrin/cont.h | 42 ----- extlib/benz/include/picrin/object.h | 5 +- extlib/benz/include/picrin/state.h | 19 ++ extlib/benz/include/picrin/type.h | 263 ---------------------------- extlib/benz/macro.c | 2 +- extlib/benz/number.c | 4 +- extlib/benz/read.c | 14 +- extlib/benz/state.c | 8 +- extlib/benz/value.c | 185 +++++++++++++++++++ 11 files changed, 246 insertions(+), 337 deletions(-) delete mode 100644 extlib/benz/include/picrin/cont.h delete mode 100644 extlib/benz/include/picrin/type.h diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 8e62986e..ace01513 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -150,7 +150,7 @@ define_var(pic_state *pic, analyze_scope *scope, pic_value sym) pic_warnf(pic, "redefining variable: ~s", sym); return; } - pic_weak_set(pic, pic->globals, sym, pic_invalid_value()); + pic_weak_set(pic, pic->globals, sym, pic_invalid_value(pic)); } } @@ -181,7 +181,7 @@ analyze_var(pic_state *pic, analyze_scope *scope, pic_value sym) static pic_value 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), pic_invalid_value(pic)); pic_set_car(pic, scope->defer, pic_cons(pic, pic_cons(pic, form, skel), pic_car(pic, scope->defer))); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 15604e5a..e3666444 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -113,9 +113,9 @@ pic_value pic_open_port(pic_state *, xFILE *file); xFILE *pic_fileno(pic_state *, pic_value port); void pic_close_port(pic_state *, pic_value port); -PIC_INLINE int pic_int(pic_state *, pic_value i); -PIC_INLINE double pic_float(pic_state *, pic_value f); -PIC_INLINE char pic_char(pic_state *, pic_value c); +int pic_int(pic_state *, pic_value i); +double pic_float(pic_state *, pic_value f); +char pic_char(pic_state *, pic_value c); #define pic_bool(pic,b) (! pic_false_p(pic, b)) const char *pic_str(pic_state *, pic_value str); unsigned char *pic_blob(pic_state *, pic_value blob, int *len); @@ -127,14 +127,15 @@ typedef struct { void (*mark)(pic_state *, void *, void (*)(pic_state *, pic_value)); } pic_data_type; -PIC_INLINE pic_value pic_undef_value(pic_state *); -PIC_INLINE pic_value pic_int_value(pic_state *, int); -PIC_INLINE pic_value pic_float_value(pic_state *, double); -PIC_INLINE pic_value pic_char_value(pic_state *, char); -PIC_INLINE pic_value pic_true_value(pic_state *); -PIC_INLINE pic_value pic_false_value(pic_state *); -PIC_INLINE pic_value pic_bool_value(pic_state *, bool); -PIC_INLINE pic_value pic_eof_object(pic_state *); +pic_value pic_invalid_value(pic_state *); +pic_value pic_undef_value(pic_state *); +pic_value pic_int_value(pic_state *, int); +pic_value pic_float_value(pic_state *, double); +pic_value pic_char_value(pic_state *, char); +pic_value pic_true_value(pic_state *); +pic_value pic_false_value(pic_state *); +#define pic_bool_value(pic, b) ((b) ? pic_true_value(pic) : pic_false_value(pic)) +pic_value pic_eof_object(pic_state *); pic_value pic_str_value(pic_state *, const char *str, int len); #define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr)) #define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1)) @@ -173,11 +174,12 @@ enum { PIC_TYPE_CP = 31 }; +#define pic_invalid_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INVALID) #define pic_undef_p(pic,v) (pic_type(pic,v) == PIC_TYPE_UNDEF) #define pic_int_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INT) #define pic_float_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FLOAT) #define pic_char_p(pic,v) (pic_type(pic,v) == PIC_TYPE_CHAR) -#define pic_eof_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_EOF) +#define pic_eof_p(pic, v) (pic_type(pic, v) == PIC_TYPE_EOF) #define pic_true_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE) #define pic_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE) #define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING) @@ -211,7 +213,7 @@ pic_value pic_cdar(pic_state *, pic_value); pic_value pic_cddr(pic_state *, pic_value); /* list */ -PIC_INLINE pic_value pic_nil_value(pic_state *); +pic_value pic_nil_value(pic_state *); 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, ...); @@ -296,9 +298,7 @@ int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); typedef struct pic_identifier pic_id; typedef pic_id pic_sym; -#include "picrin/type.h" #include "picrin/state.h" -#include "picrin/cont.h" void *pic_default_allocf(void *, void *, size_t); @@ -347,6 +347,13 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); pic_in_library(pic, lib); \ } while (0) +void pic_save_point(pic_state *, struct pic_cont *); +void pic_load_point(pic_state *, struct pic_cont *); + +pic_value pic_make_cont(pic_state *, struct pic_cont *); + +void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); + /* do not return from try block! */ #define pic_try \ diff --git a/extlib/benz/include/picrin/cont.h b/extlib/benz/include/picrin/cont.h deleted file mode 100644 index 8629d6f9..00000000 --- a/extlib/benz/include/picrin/cont.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_CONT_H -#define PICRIN_CONT_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct pic_cont { - PIC_JMPBUF jmp; - - int id; - - struct pic_checkpoint *cp; - ptrdiff_t sp_offset; - ptrdiff_t ci_offset; - ptrdiff_t xp_offset; - size_t arena_idx; - pic_value ptable; - pic_code *ip; - - int retc; - pic_value *retv; - - struct pic_cont *prev; -}; - -void pic_save_point(pic_state *, struct pic_cont *); -void pic_load_point(pic_state *, struct pic_cont *); - -pic_value pic_make_cont(pic_state *, struct pic_cont *); - -void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index ddeac151..39d9a919 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -133,6 +133,8 @@ struct pic_checkpoint { struct pic_checkpoint *prev; }; +struct pic_object *pic_obj_ptr(pic_value); + #define pic_id_ptr(pic, o) ((pic_id *)pic_obj_ptr(o)) #define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o)) #define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o)) @@ -148,12 +150,13 @@ struct pic_checkpoint { #define pic_error_ptr(pic, o) ((struct pic_error *)pic_obj_ptr(o)) #define pic_rec_ptr(pic, o) ((struct pic_record *)pic_obj_ptr(o)) -#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END) +#define pic_obj_p(pic,v) (pic_type(pic,v) > PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) #define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) #define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) #define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) +pic_value pic_obj_value(void *ptr); struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); #define VALID_INDEX(pic, len, i) do { \ diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 5246797c..8d7b6e4a 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -22,6 +22,25 @@ struct pic_lib { struct pic_dict *exports; }; +struct pic_cont { + PIC_JMPBUF jmp; + + int id; + + struct pic_checkpoint *cp; + ptrdiff_t sp_offset; + ptrdiff_t ci_offset; + ptrdiff_t xp_offset; + size_t arena_idx; + pic_value ptable; + pic_code *ip; + + int retc; + pic_value *retv; + + struct pic_cont *prev; +}; + typedef struct { int argc, retc; pic_code *ip; diff --git a/extlib/benz/include/picrin/type.h b/extlib/benz/include/picrin/type.h deleted file mode 100644 index 53543933..00000000 --- a/extlib/benz/include/picrin/type.h +++ /dev/null @@ -1,263 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_TYPE_H -#define PICRIN_TYPE_H - -#if defined(__cplusplus) -extern "C" { -#endif - -/** - * `invalid` value will never be seen from user-end: - * it is only used for repsenting internal special state - */ - -#define pic_invalid_p(pic, v) (pic_vtype(pic, v) == PIC_TYPE_INVALID) - -#if PIC_NAN_BOXING - -/** - * value representation by nan-boxing: - * float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF - * ptr : 111111111111TTTT PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP - * int : 111111111111TTTT 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII - * char : 111111111111TTTT 0000000000000000 CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC - */ - -#define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48))) - -PIC_INLINE int -pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) -{ - return 0xfff0 >= (v >> 48) ? PIC_TYPE_FLOAT : ((v >> 48) & 0xf); -} - -PIC_INLINE double -pic_float(pic_state PIC_UNUSED(*pic), pic_value v) -{ - union { double f; uint64_t i; } u; - u.i = v; - return u.f; -} - -PIC_INLINE int -pic_int(pic_state PIC_UNUSED(*pic), pic_value v) -{ - union { int i; unsigned u; } u; - u.u = v & 0xfffffffful; - return u.i; -} - -PIC_INLINE char -pic_char(pic_state PIC_UNUSED(*pic), pic_value v) -{ - return v & 0xfffffffful; -} - -PIC_INLINE struct pic_object * -pic_obj_ptr(pic_value v) -{ - return (struct pic_object *)(0xfffffffffffful & v); -} - -#else - -#define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) - -PIC_INLINE int -pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) -{ - return (int)(v.type); -} - -PIC_INLINE double -pic_float(pic_state PIC_UNUSED(*pic), pic_value v) -{ - return v.u.f; -} - -PIC_INLINE int -pic_int(pic_state PIC_UNUSED(*pic), pic_value v) -{ - return v.u.i; -} - -PIC_INLINE char -pic_char(pic_state PIC_UNUSED(*pic), pic_value v) -{ - return v.u.c; -} - -PIC_INLINE struct pic_object * -pic_obj_ptr(pic_value v) -{ - return (struct pic_object *)(v.u.data); -} - -#endif - -PIC_INLINE bool -pic_valid_int(double v) -{ - return INT_MIN <= v && v <= INT_MAX; -} - -PIC_INLINE pic_value -pic_nil_value(pic_state PIC_UNUSED(*pic)) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_NIL); - return v; -} - -PIC_INLINE pic_value -pic_eof_object(pic_state PIC_UNUSED(*pic)) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_EOF); - return v; -} - -PIC_INLINE pic_value -pic_true_value(pic_state PIC_UNUSED(*pic)) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_TRUE); - return v; -} - -PIC_INLINE pic_value -pic_false_value(pic_state PIC_UNUSED(*pic)) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_FALSE); - return v; -} - -PIC_INLINE pic_value -pic_bool_value(pic_state PIC_UNUSED(*pic), bool b) -{ - pic_value v; - - pic_init_value(v, b ? PIC_TYPE_TRUE : PIC_TYPE_FALSE); - return v; -} - -PIC_INLINE pic_value -pic_undef_value(pic_state PIC_UNUSED(*pic)) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_UNDEF); - return v; -} - -PIC_INLINE pic_value -pic_invalid_value() -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_INVALID); - return v; -} - -#if PIC_NAN_BOXING - -PIC_INLINE pic_value -pic_obj_value(void *ptr) -{ - pic_value v; - - pic_init_value(v, PIC_IVAL_END); - v |= 0xfffffffffffful & (uint64_t)ptr; - return v; -} - -PIC_INLINE pic_value -pic_float_value(pic_state PIC_UNUSED(*pic), double f) -{ - union { double f; uint64_t i; } u; - - if (f != f) { - return 0x7ff8000000000000ul; - } else { - u.f = f; - return u.i; - } -} - -PIC_INLINE pic_value -pic_int_value(pic_state PIC_UNUSED(*pic), int i) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_INT); - v |= (unsigned)i; - return v; -} - -PIC_INLINE pic_value -pic_char_value(pic_state PIC_UNUSED(*pic), char c) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_CHAR); - v |= (unsigned char)c; - return v; -} - -#else - -PIC_INLINE pic_value -pic_obj_value(void *ptr) -{ - pic_value v; - - pic_init_value(v, PIC_IVAL_END); - v.u.data = ptr; - return v; -} - -PIC_INLINE pic_value -pic_float_value(pic_state PIC_UNUSED(*pic), double f) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_FLOAT); - v.u.f = f; - return v; -} - -PIC_INLINE pic_value -pic_int_value(pic_state PIC_UNUSED(*pic), int i) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_INT); - v.u.i = i; - return v; -} - -PIC_INLINE pic_value -pic_char_value(pic_state PIC_UNUSED(*pic), char c) -{ - pic_value v; - - pic_init_value(v, PIC_TYPE_CHAR); - v.u.c = c; - return v; -} - -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 99ca4de8..6825aa62 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -178,7 +178,7 @@ expand_list(pic_state *pic, pic_value obj, pic_value env, pic_value deferred) static pic_value 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), pic_invalid_value(pic)); pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred))); diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 9cd69ad0..08a2a2a8 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -262,8 +262,8 @@ pic_number_string_to_number(pic_state *pic) num = strtol(str, &eptr, radix); if (*eptr == '\0') { - return pic_valid_int(num) - ? pic_int_value(pic, (int)num) + return INT_MIN <= num && num <= INT_MAX + ? pic_int_value(pic, num) : pic_float_value(pic, num); } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index b409264b..86991f82 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -92,7 +92,7 @@ read_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int c) c = next(pic, file); } while (! (c == EOF || c == '\n')); - return pic_invalid_value(); + return pic_invalid_value(pic); } static pic_value @@ -114,7 +114,7 @@ read_block_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int PIC_UNUSED(c)) } } - return pic_invalid_value(); + return pic_invalid_value(pic); } static pic_value @@ -122,7 +122,7 @@ read_datum_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { read(pic, file, next(pic, file)); - return pic_invalid_value(); + return pic_invalid_value(pic); } static pic_value @@ -132,13 +132,13 @@ read_directive(pic_state *pic, xFILE *file, int c) case 'n': if (expect(pic, file, "no-fold-case")) { pic->reader.typecase = PIC_CASE_DEFAULT; - return pic_invalid_value(); + return pic_invalid_value(pic); } break; case 'f': if (expect(pic, file, "fold-case")) { pic->reader.typecase = PIC_CASE_FOLD; - return pic_invalid_value(); + return pic_invalid_value(pic); } break; } @@ -289,8 +289,8 @@ read_unsigned(pic_state *pic, xFILE *file, int c) buf[idx] = 0; flt = PIC_CSTRING_TO_DOUBLE(buf); - if (dpe == 0 && pic_valid_int(flt)) - return pic_int_value(pic, (int )flt); + if (dpe == 0 && INT_MIN <= flt && flt <= INT_MAX) + return pic_int_value(pic, flt); return pic_float_value(pic, flt); } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 065ad248..58e35526 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -275,7 +275,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->ireps.prev = &pic->ireps; /* raised error object */ - pic->err = pic_invalid_value(); + pic->err = pic_invalid_value(pic); /* file pool */ memset(pic->files, 0, sizeof pic->files); @@ -392,9 +392,9 @@ pic_close(pic_state *pic) pic->ci = pic->cibase; pic->xp = pic->xpbase; pic->arena_idx = 0; - pic->err = pic_invalid_value(); - pic->globals = pic_invalid_value(); - pic->macros = pic_invalid_value(); + pic->err = pic_invalid_value(pic); + pic->globals = pic_invalid_value(pic); + pic->macros = pic_invalid_value(pic); pic->features = pic_nil_value(pic); /* free all libraries */ diff --git a/extlib/benz/value.c b/extlib/benz/value.c index 82fcf227..1a737256 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -5,6 +5,191 @@ #include "picrin.h" #include "picrin/object.h" +#if PIC_NAN_BOXING + +/** + * value representation by nan-boxing: + * float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF + * ptr : 111111111111TTTT PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP PPPPPPPPPPPPPPPP + * int : 111111111111TTTT 0000000000000000 IIIIIIIIIIIIIIII IIIIIIIIIIIIIIII + * char : 111111111111TTTT 0000000000000000 CCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCC + */ + +#define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48))) + +int +pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return 0xfff0 >= (v >> 48) ? PIC_TYPE_FLOAT : ((v >> 48) & 0xf); +} + +double +pic_float(pic_state PIC_UNUSED(*pic), pic_value v) +{ + union { double f; uint64_t i; } u; + u.i = v; + return u.f; +} + +int +pic_int(pic_state PIC_UNUSED(*pic), pic_value v) +{ + union { int i; unsigned u; } u; + u.u = v & 0xfffffffful; + return u.i; +} + +char +pic_char(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return v & 0xfffffffful; +} + +struct pic_object * +pic_obj_ptr(pic_value v) +{ + return (struct pic_object *)(0xfffffffffffful & v); +} + +#else + +#define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) + +int +pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return (int)(v.type); +} + +double +pic_float(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return v.u.f; +} + +int +pic_int(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return v.u.i; +} + +char +pic_char(pic_state PIC_UNUSED(*pic), pic_value v) +{ + return v.u.c; +} + +struct pic_object * +pic_obj_ptr(pic_value v) +{ + return (struct pic_object *)(v.u.data); +} + +#endif + +#if PIC_NAN_BOXING + +pic_value +pic_obj_value(void *ptr) +{ + pic_value v; + + pic_init_value(v, PIC_IVAL_END); + v |= 0xfffffffffffful & (uint64_t)ptr; + return v; +} + +pic_value +pic_float_value(pic_state PIC_UNUSED(*pic), double f) +{ + union { double f; uint64_t i; } u; + + if (f != f) { + return 0x7ff8000000000000ul; + } else { + u.f = f; + return u.i; + } +} + +pic_value +pic_int_value(pic_state PIC_UNUSED(*pic), int i) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_INT); + v |= (unsigned)i; + return v; +} + +pic_value +pic_char_value(pic_state PIC_UNUSED(*pic), char c) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_CHAR); + v |= (unsigned char)c; + return v; +} + +#else + +pic_value +pic_obj_value(void *ptr) +{ + pic_value v; + + pic_init_value(v, PIC_IVAL_END); + v.u.data = ptr; + return v; +} + +pic_value +pic_float_value(pic_state PIC_UNUSED(*pic), double f) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_FLOAT); + v.u.f = f; + return v; +} + +pic_value +pic_int_value(pic_state PIC_UNUSED(*pic), int i) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_INT); + v.u.i = i; + return v; +} + +pic_value +pic_char_value(pic_state PIC_UNUSED(*pic), char c) +{ + pic_value v; + + pic_init_value(v, PIC_TYPE_CHAR); + v.u.c = c; + return v; +} + +#endif + +#define DEFVAL(name, type) \ + pic_value name(pic_state PIC_UNUSED(*pic)) { \ + pic_value v; \ + pic_init_value(v, type); \ + return v; \ + } + +DEFVAL(pic_nil_value, PIC_TYPE_NIL) +DEFVAL(pic_eof_object, PIC_TYPE_EOF) +DEFVAL(pic_true_value, PIC_TYPE_TRUE) +DEFVAL(pic_false_value, PIC_TYPE_FALSE) +DEFVAL(pic_undef_value, PIC_TYPE_UNDEF) +DEFVAL(pic_invalid_value, PIC_TYPE_INVALID) + int pic_type(pic_state PIC_UNUSED(*pic), pic_value v) { From cafc7c88eab77932f7341f787111e213805acdcf Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 19:08:07 +0900 Subject: [PATCH 074/119] add pic_exit_point --- extlib/benz/cont.c | 8 +++++++- extlib/benz/file.c | 4 ++++ extlib/benz/include/picrin.h | 17 ++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index ffe7e4d2..e4c6aa62 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -40,6 +40,12 @@ pic_load_point(pic_state *pic, struct pic_cont *cont) pic->cc = cont->prev; } +void +pic_exit_point(pic_state *pic) +{ + pic->cc = pic->cc->prev; +} + void pic_wind(pic_state *pic, struct pic_checkpoint *here, struct pic_checkpoint *there) { @@ -140,7 +146,7 @@ pic_callcc(pic_state *pic, pic_value proc) val = pic_call(pic, proc, 1, pic_make_cont(pic, &cont)); - pic->cc = pic->cc->prev; + pic_exit_point(pic); return val; } diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 66ec825c..ac6f231b 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -355,6 +355,10 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { return cnt; } +xFILE *xfile_xstdin(pic_state *pic) { return &pic->files[0]; } +xFILE *xfile_xstdout(pic_state *pic) { return &pic->files[1]; } +xFILE *xfile_xstderr(pic_state *pic) { return &pic->files[2]; } + #if PIC_ENABLE_STDIO static int diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index e3666444..9975ad65 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -307,9 +307,13 @@ void *pic_default_allocf(void *, void *, size_t); pic_errorf(pic, "expected " #type ", but got ~s", v); \ } -#define xstdin (&pic->files[0]) -#define xstdout (&pic->files[1]) -#define xstderr (&pic->files[2]) +xFILE *xfile_xstdin(pic_state *); +xFILE *xfile_xstdout(pic_state *); +xFILE *xfile_xstderr(pic_state *); + +#define xstdin (xfile_xstdin(pic)) +#define xstdout (xfile_xstdout(pic)) +#define xstderr (xfile_xstderr(pic)) #if PIC_ENABLE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); @@ -347,9 +351,6 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); pic_in_library(pic, lib); \ } while (0) -void pic_save_point(pic_state *, struct pic_cont *); -void pic_load_point(pic_state *, struct pic_cont *); - pic_value pic_make_cont(pic_state *, struct pic_cont *); void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); @@ -365,6 +366,8 @@ void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); extern void pic_push_handler(pic_state *, pic_value proc); \ extern pic_value pic_pop_handler(pic_state *); \ extern pic_value pic_native_exception_handler(pic_state *); \ + extern void pic_save_point(pic_state *, struct pic_cont *); \ + extern void pic_exit_point(pic_state *); \ struct pic_cont cont; \ pic_save_point(pic, &cont); \ if (PIC_SETJMP(pic, cont.jmp) == 0) { \ @@ -375,7 +378,7 @@ void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); #define pic_catch_(label) \ pic_pop_handler(pic); \ } while (0); \ - pic->cc = pic->cc->prev; \ + pic_exit_point(pic); \ } else { \ goto label; \ } \ From d0dc5778107c715f8e7f4a682b390858bccbb60b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 19:10:51 +0900 Subject: [PATCH 075/119] move typedefs --- extlib/benz/include/picrin.h | 3 --- extlib/benz/include/picrin/object.h | 3 +++ extlib/benz/include/picrin/state.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 9975ad65..1784171a 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -295,9 +295,6 @@ int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); /* extra stuff */ -typedef struct pic_identifier pic_id; -typedef pic_id pic_sym; - #include "picrin/state.h" void *pic_default_allocf(void *, void *, size_t); diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 39d9a919..26b3c86b 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -9,6 +9,9 @@ extern "C" { #endif +typedef struct pic_identifier pic_id; +typedef pic_id pic_sym; + KHASH_DECLARE(env, pic_id *, pic_sym *) KHASH_DECLARE(dict, pic_sym *, pic_value) KHASH_DECLARE(weak, struct pic_object *, pic_value) diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 8d7b6e4a..2d1566da 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -52,7 +52,7 @@ typedef struct { struct pic_context *up; } pic_callinfo; -KHASH_DECLARE(oblist, struct pic_string *, pic_sym *) +KHASH_DECLARE(oblist, struct pic_string *, struct pic_identifier *) KHASH_DECLARE(ltable, const char *, struct pic_lib) struct pic_state { From b5a27437e34ce61a31a960f67d72fd1a3fa3ccdd Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 19:47:46 +0900 Subject: [PATCH 076/119] cleanup --- contrib/40.srfi/src/106.c | 24 ++++++++---------------- extlib/benz/include/picrin.h | 8 ++------ extlib/benz/include/picrin/object.h | 2 ++ extlib/benz/proc.c | 14 +++++++------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 73272825..c790447b 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -49,14 +49,6 @@ static const pic_data_type socket_type = { "socket", socket_dtor, NULL }; #define pic_socket_p(pic, o) (pic_data_type_p(pic, (o), &socket_type)) #define pic_socket_data(pic, o) ((struct pic_socket_t *)pic_data(pic, o)) -PIC_INLINE void -validate_socket_object(pic_state *pic, pic_value v) -{ - if (! pic_socket_p(pic, v)) { - pic_errorf(pic, "~s is not a socket object", v); - } -} - static pic_value pic_socket_socket_p(pic_state *pic) { @@ -152,7 +144,7 @@ pic_socket_socket_accept(pic_state *pic) struct pic_socket_t *sock, *new_sock; pic_get_args(pic, "o", &obj); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); @@ -191,7 +183,7 @@ pic_socket_socket_send(pic_state *pic) struct pic_socket_t *sock; pic_get_args(pic, "ob|i", &obj, &bv, &flags); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); @@ -229,7 +221,7 @@ pic_socket_socket_recv(pic_state *pic) struct pic_socket_t *sock; pic_get_args(pic, "oi|i", &obj, &size, &flags); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); if (size < 0) { pic_errorf(pic, "size must not be negative"); } @@ -263,7 +255,7 @@ pic_socket_socket_shutdown(pic_state *pic) struct pic_socket_t *sock; pic_get_args(pic, "oi", &obj, &how); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); sock = pic_socket_data(pic, obj); if (sock->fd != -1) { @@ -280,7 +272,7 @@ pic_socket_socket_close(pic_state *pic) pic_value obj; pic_get_args(pic, "o", &obj); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); socket_close(pic_socket_data(pic, obj)); @@ -341,7 +333,7 @@ pic_socket_socket_input_port(pic_state *pic) struct pic_socket_t *sock; pic_get_args(pic, "o", &obj); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); @@ -356,7 +348,7 @@ pic_socket_socket_output_port(pic_state *pic) struct pic_socket_t *sock; pic_get_args(pic, "o", &obj); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); @@ -371,7 +363,7 @@ pic_socket_call_with_socket(pic_state *pic) struct pic_socket_t *sock; pic_get_args(pic, "ol", &obj, &proc); - validate_socket_object(pic, obj); + pic_assert_type(pic, obj, socket); sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 1784171a..44f7e856 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -82,6 +82,7 @@ void pic_set(pic_state *, const char *lib, const char *name, pic_value v); pic_value pic_closure_ref(pic_state *, int i); void pic_closure_set(pic_state *, int i, pic_value v); pic_value pic_funcall(pic_state *, const char *lib, const char *name, int n, ...); +pic_value pic_make_var(pic_state *, pic_value init, pic_value conv); pic_value pic_return(pic_state *, int n, ...); pic_value pic_vreturn(pic_state *, int n, va_list); @@ -337,8 +338,6 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib); void pic_load(pic_state *, pic_value port); void pic_load_cstr(pic_state *, const char *); -pic_value pic_make_var(pic_state *, pic_value init, pic_value conv); - bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); #define pic_deflibrary(pic, lib) do { \ @@ -348,10 +347,6 @@ bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); pic_in_library(pic, lib); \ } while (0) -pic_value pic_make_cont(pic_state *, struct pic_cont *); - -void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); - /* do not return from try block! */ #define pic_try \ @@ -360,6 +355,7 @@ void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); pic_catch_(PIC_GENSYM(label)) #define pic_try_(cont, handler) \ do { \ + extern pic_value pic_make_cont(pic_state *, struct pic_cont *); \ extern void pic_push_handler(pic_state *, pic_value proc); \ extern pic_value pic_pop_handler(pic_state *); \ extern pic_value pic_native_exception_handler(pic_state *); \ diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 26b3c86b..397740d3 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -193,6 +193,8 @@ void pic_rope_decref(pic_state *, struct pic_rope *); #define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) #define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) +void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); + #if defined(__cplusplus) } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 9a93e045..4d5e843b 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -21,13 +21,13 @@ * c char * char * z char ** c string * m pic_value * symbol - * v pic_value * vector object - * s pic_value * string object - * b pic_value * bytevector object - * l pic_value * lambda object - * p pic_value * port object - * d pic_value * dictionary object - * r pic_value * record object + * v pic_value * vector + * s pic_value * string + * b pic_value * bytevector + * l pic_value * lambda + * p pic_value * port + * d pic_value * dictionary + * r pic_value * record * * | optional operator * * int *, pic_value ** variable length operator From 9e5f846787cb74022aea9b6f720e04ab7251f7d7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 21:16:10 +0900 Subject: [PATCH 077/119] make pic_get_args more useful --- contrib/30.regexp/src/regexp.c | 33 +++++-------- contrib/40.srfi/src/106.c | 49 +++++-------------- extlib/benz/blob.c | 40 +++++----------- extlib/benz/data.c | 7 ++- extlib/benz/include/picrin.h | 4 +- extlib/benz/port.c | 16 ++----- extlib/benz/proc.c | 88 ++++++++++++++++++++++++++-------- 7 files changed, 115 insertions(+), 122 deletions(-) diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 3253e449..c77da4e8 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -19,9 +19,6 @@ regexp_dtor(pic_state *pic, void *data) static const pic_data_type regexp_type = { "regexp", regexp_dtor, NULL }; -#define pic_regexp_p(pic, o) (pic_data_type_p(pic, (o), ®exp_type)) -#define pic_regexp_data(pic, v) ((struct pic_regexp_t *)pic_data(pic, v)) - static pic_value pic_regexp_regexp(pic_state *pic) { @@ -72,30 +69,28 @@ pic_regexp_regexp_p(pic_state *pic) pic_get_args(pic, "o", &obj); - return pic_bool_value(pic, pic_regexp_p(pic, obj)); + return pic_bool_value(pic, pic_data_p(pic, obj, ®exp_type)); } static pic_value pic_regexp_regexp_match(pic_state *pic) { - pic_value reg; + struct pic_regexp_t *reg; const char *input; regmatch_t match[100]; pic_value str, matches, positions; int i, offset; - pic_get_args(pic, "oz", ®, &input); - - pic_assert_type(pic, reg, regexp); + pic_get_args(pic, "uz", ®, ®exp_type, &input); matches = pic_nil_value(pic); positions = pic_nil_value(pic); - if (strchr(pic_regexp_data(pic, reg)->flags, 'g') != NULL) { + if (strchr(reg->flags, 'g') != NULL) { /* global search */ offset = 0; - while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, match, 0) != REG_NOMATCH) { + while (regexec(®->reg, input, 1, match, 0) != REG_NOMATCH) { pic_push(pic, pic_str_value(pic, input, match[0].rm_eo - match[0].rm_so), matches); pic_push(pic, pic_int_value(pic, offset), positions); @@ -105,7 +100,7 @@ pic_regexp_regexp_match(pic_state *pic) } else { /* local search */ - if (regexec(&pic_regexp_data(pic, reg)->reg, input, 100, match, 0) == 0) { + if (regexec(®->reg, input, 100, match, 0) == 0) { for (i = 0; i < 100; ++i) { if (match[i].rm_so == -1) { break; @@ -130,16 +125,14 @@ pic_regexp_regexp_match(pic_state *pic) static pic_value pic_regexp_regexp_split(pic_state *pic) { - pic_value reg; + struct pic_regexp_t *reg; const char *input; regmatch_t match; pic_value output = pic_nil_value(pic); - pic_get_args(pic, "oz", ®, &input); + pic_get_args(pic, "uz", ®, ®exp_type, &input); - pic_assert_type(pic, reg, regexp); - - while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { + while (regexec(®->reg, input, 1, &match, 0) != REG_NOMATCH) { pic_push(pic, pic_str_value(pic, input, match.rm_so), output); input += match.rm_eo; @@ -153,16 +146,14 @@ pic_regexp_regexp_split(pic_state *pic) static pic_value pic_regexp_regexp_replace(pic_state *pic) { - pic_value reg; + struct pic_regexp_t *reg; const char *input; regmatch_t match; pic_value txt, output = pic_lit_value(pic, ""); - pic_get_args(pic, "ozs", ®, &input, &txt); + pic_get_args(pic, "uzs", ®, ®exp_type, &input, &txt); - pic_assert_type(pic, reg, regexp); - - while (regexec(&pic_regexp_data(pic, reg)->reg, input, 1, &match, 0) != REG_NOMATCH) { + while (regexec(®->reg, input, 1, &match, 0) != REG_NOMATCH) { output = pic_str_cat(pic, output, pic_str_value(pic, input, match.rm_so)); output = pic_str_cat(pic, output, txt); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index c790447b..3d1a0cdd 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -46,16 +46,14 @@ socket_dtor(pic_state *pic, void *data) static const pic_data_type socket_type = { "socket", socket_dtor, NULL }; -#define pic_socket_p(pic, o) (pic_data_type_p(pic, (o), &socket_type)) -#define pic_socket_data(pic, o) ((struct pic_socket_t *)pic_data(pic, o)) - static pic_value pic_socket_socket_p(pic_state *pic) { pic_value obj; pic_get_args(pic, "o", &obj); - return pic_bool_value(pic, pic_socket_p(pic, obj)); + + return pic_bool_value(pic, pic_data_p(pic, obj, &socket_type)); } static pic_value @@ -139,14 +137,11 @@ pic_socket_make_socket(pic_state *pic) static pic_value pic_socket_socket_accept(pic_state *pic) { - pic_value obj; int fd = -1; struct pic_socket_t *sock, *new_sock; - pic_get_args(pic, "o", &obj); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "u", &sock, &socket_type); - sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); errno = 0; @@ -177,18 +172,14 @@ pic_socket_socket_accept(pic_state *pic) static pic_value pic_socket_socket_send(pic_state *pic) { - pic_value obj, bv; const unsigned char *cursor; int flags = 0, remain, written; struct pic_socket_t *sock; - pic_get_args(pic, "ob|i", &obj, &bv, &flags); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "ub|i", &sock, &socket_type, &cursor, &remain, &flags); - sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); - cursor = pic_blob(pic, bv, &remain); written = 0; errno = 0; while (remain > 0) { @@ -213,20 +204,18 @@ pic_socket_socket_send(pic_state *pic) static pic_value pic_socket_socket_recv(pic_state *pic) { - pic_value obj; void *buf; int size; int flags = 0; ssize_t len; struct pic_socket_t *sock; - pic_get_args(pic, "oi|i", &obj, &size, &flags); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "ui|i", &sock, &socket_type, &size, &flags); + if (size < 0) { pic_errorf(pic, "size must not be negative"); } - sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); buf = pic_blob(pic, pic_blob_value(pic, NULL, size), NULL); @@ -250,14 +239,11 @@ pic_socket_socket_recv(pic_state *pic) static pic_value pic_socket_socket_shutdown(pic_state *pic) { - pic_value obj; int how; struct pic_socket_t *sock; - pic_get_args(pic, "oi", &obj, &how); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "ui", &sock, &socket_type, &how); - sock = pic_socket_data(pic, obj); if (sock->fd != -1) { shutdown(sock->fd, how); sock->fd = -1; @@ -269,12 +255,11 @@ pic_socket_socket_shutdown(pic_state *pic) static pic_value pic_socket_socket_close(pic_state *pic) { - pic_value obj; + struct pic_socket_t *sock; - pic_get_args(pic, "o", &obj); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "u", &sock, &socket_type); - socket_close(pic_socket_data(pic, obj)); + socket_close(sock); return pic_undef_value(pic); } @@ -329,13 +314,10 @@ make_socket_port(pic_state *pic, struct pic_socket_t *sock, const char *mode) static pic_value pic_socket_socket_input_port(pic_state *pic) { - pic_value obj; struct pic_socket_t *sock; - pic_get_args(pic, "o", &obj); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "u", &sock, &socket_type); - sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); return make_socket_port(pic, sock, "r"); @@ -344,13 +326,10 @@ pic_socket_socket_input_port(pic_state *pic) static pic_value pic_socket_socket_output_port(pic_state *pic) { - pic_value obj; struct pic_socket_t *sock; - pic_get_args(pic, "o", &obj); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "u", &sock, &socket_type); - sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); return make_socket_port(pic, sock, "w"); @@ -362,10 +341,8 @@ pic_socket_call_with_socket(pic_state *pic) pic_value obj, proc, result; struct pic_socket_t *sock; - pic_get_args(pic, "ol", &obj, &proc); - pic_assert_type(pic, obj, socket); + pic_get_args(pic, "u+l", &sock, &socket_type, &obj, &proc); - sock = pic_socket_data(pic, obj); ensure_socket_is_open(pic, sock); result = pic_call(pic, proc, 1, obj); diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index ec96e4fe..db0d6079 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -89,12 +89,9 @@ pic_blob_make_bytevector(pic_state *pic) static pic_value pic_blob_bytevector_length(pic_state *pic) { - pic_value bv; int len; - pic_get_args(pic, "b", &bv); - - pic_blob(pic, bv, &len); + pic_get_args(pic, "b", NULL, &len); return pic_int_value(pic, len); } @@ -102,13 +99,10 @@ pic_blob_bytevector_length(pic_state *pic) static pic_value pic_blob_bytevector_u8_ref(pic_state *pic) { - pic_value bv; unsigned char *buf; - int k, len; + int len, k; - pic_get_args(pic, "bi", &bv, &k); - - buf = pic_blob(pic, bv, &len); + pic_get_args(pic, "bi", &buf, &len, &k); VALID_INDEX(pic, len, k); @@ -118,17 +112,14 @@ pic_blob_bytevector_u8_ref(pic_state *pic) static pic_value pic_blob_bytevector_u8_set(pic_state *pic) { - pic_value bv; unsigned char *buf; - int k, v, len; + int len, k, v; - pic_get_args(pic, "bii", &bv, &k, &v); + pic_get_args(pic, "bii", &buf, &len, &k, &v); if (v < 0 || v > 255) pic_errorf(pic, "byte out of range"); - buf = pic_blob(pic, bv, &len); - VALID_INDEX(pic, len, k); buf[k] = (unsigned char)v; @@ -139,14 +130,10 @@ pic_blob_bytevector_u8_set(pic_state *pic) static pic_value pic_blob_bytevector_copy_i(pic_state *pic) { - pic_value to, from; - unsigned char *tobuf, *frombuf; + unsigned char *to, *from; int n, at, start, end, tolen, fromlen; - n = pic_get_args(pic, "bib|ii", &to, &at, &from, &start, &end); - - tobuf = pic_blob(pic, to, &tolen); - frombuf = pic_blob(pic, from, &fromlen); + n = pic_get_args(pic, "bib|ii", &to, &tolen, &at, &from, &fromlen, &start, &end); switch (n) { case 3: @@ -157,7 +144,7 @@ pic_blob_bytevector_copy_i(pic_state *pic) VALID_ATRANGE(pic, tolen, at, fromlen, start, end); - memmove(tobuf + at, frombuf + start, end - start); + memmove(to + at, from + start, end - start); return pic_undef_value(pic); } @@ -165,13 +152,10 @@ pic_blob_bytevector_copy_i(pic_state *pic) static pic_value pic_blob_bytevector_copy(pic_state *pic) { - pic_value from; unsigned char *buf; int n, start, end, len; - n = pic_get_args(pic, "b|ii", &from, &start, &end); - - buf = pic_blob(pic, from, &len); + n = pic_get_args(pic, "b|ii", &buf, &len, &start, &end); switch (n) { case 1: @@ -241,13 +225,11 @@ pic_blob_list_to_bytevector(pic_state *pic) static pic_value pic_blob_bytevector_to_list(pic_state *pic) { - pic_value blob, list; + pic_value list; unsigned char *buf; int n, len, start, end, i; - n = pic_get_args(pic, "b|ii", &blob, &start, &end); - - buf = pic_blob(pic, blob, &len); + n = pic_get_args(pic, "b|ii", &buf, &len, &start, &end); switch (n) { case 1: diff --git a/extlib/benz/data.c b/extlib/benz/data.c index ce7c6530..a1a84dfd 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -2,9 +2,12 @@ #include "picrin/object.h" bool -pic_data_type_p(pic_state *pic, pic_value obj, const pic_data_type *type) +pic_data_p(pic_state *pic, pic_value obj, const pic_data_type *type) { - return pic_data_p(pic, obj) && pic_data_ptr(pic, obj)->type == type; + if (pic_type(pic, obj) != PIC_TYPE_DATA) { + return false; + } + return type == NULL || pic_data_ptr(pic, obj)->type == type; } void * diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 44f7e856..df9e8bce 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -186,7 +186,6 @@ enum { #define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING) #define pic_blob_p(pic,v) (pic_type(pic,v) == PIC_TYPE_BLOB) #define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PROC) -#define pic_data_p(pic,v) (pic_type(pic,v) == PIC_TYPE_DATA) #define pic_nil_p(pic,v) (pic_type(pic,v) == PIC_TYPE_NIL) #define pic_pair_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PAIR) #define pic_vec_p(pic,v) (pic_type(pic,v) == PIC_TYPE_VECTOR) @@ -194,6 +193,7 @@ enum { #define pic_weak_p(pic,v) (pic_type(pic,v) == PIC_TYPE_WEAK) #define pic_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT) #define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL) +bool pic_data_p(pic_state *, pic_value, const pic_data_type *); int pic_type(pic_state *, pic_value); const char *pic_typename(pic_state *, int); @@ -338,8 +338,6 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib); void pic_load(pic_state *, pic_value port); void pic_load_cstr(pic_state *, const char *); -bool pic_data_type_p(pic_state *, pic_value, const pic_data_type *); - #define pic_deflibrary(pic, lib) do { \ if (! pic_find_library(pic, lib)) { \ pic_make_library(pic, lib); \ diff --git a/extlib/benz/port.c b/extlib/benz/port.c index f410f44f..6933c07a 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -134,13 +134,10 @@ pic_port_close_port(pic_state *pic) static pic_value pic_port_open_input_bytevector(pic_state *pic) { - pic_value blob; unsigned char *buf; int len; - pic_get_args(pic, "b", &blob); - - buf = pic_blob(pic, blob, &len); + pic_get_args(pic, "b", &buf, &len); return pic_open_port(pic, xfopen_buf(pic, (char *)buf, len, "r")); } @@ -240,13 +237,11 @@ pic_port_read_bytevector(pic_state *pic) static pic_value pic_port_read_bytevector_ip(pic_state *pic) { - pic_value bv, port; + pic_value port; unsigned char *buf; int n, start, end, i, len; - n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end); - - buf = pic_blob(pic, bv, &len); + n = pic_get_args(pic, "b|pii", &buf, &len, &port, &start, &end); switch (n) { case 1: @@ -284,14 +279,11 @@ pic_port_write_u8(pic_state *pic) static pic_value pic_port_write_bytevector(pic_state *pic) { - pic_value blob; pic_value port; unsigned char *buf; int n, start, end, len, done; - n = pic_get_args(pic, "b|pii", &blob, &port, &start, &end); - - buf = pic_blob(pic, blob, &len); + n = pic_get_args(pic, "b|pii", &buf, &len, &port, &start, &end); switch (n) { case 1: diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 4d5e843b..3a0c70ae 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -20,15 +20,17 @@ * F double *, bool * float with exactness * c char * char * z char ** c string + * b unsigned char *, int * bytevector + * u void **, const pic_data_type * user data type * m pic_value * symbol * v pic_value * vector * s pic_value * string - * b pic_value * bytevector * l pic_value * lambda * p pic_value * port * d pic_value * dictionary * r pic_value * record * + * + aliasing operator * | optional operator * * int *, pic_value ** variable length operator * ---- ---- ---- @@ -38,34 +40,41 @@ int pic_get_args(pic_state *pic, const char *format, ...) { char c; + const char *p = format; int paramc = 0, optc = 0; int i, argc = pic->ci->argc - 1; va_list ap; - bool proc = false, rest = false, opt = false; + bool proc = 0, rest = 0, opt = 0; /* parse format */ - if ((c = *format) != '\0') { + if ((c = *p) != '\0') { if (c == '&') { - proc = true; - format++; /* forget about '&' */ + proc = 1; + p++; } - for (paramc = 0, c = *format; c; c = format[++paramc]) { + while ((c = *p++) != '\0') { + if (c == '+') + continue; if (c == '|') { - opt = true; - break; + opt = 1; break; } else if (c == '*') { - rest = true; - break; + rest = 1; break; + } + paramc++; + } + if (opt) { + while ((c = *p++) != '\0') { + if (c == '+') + continue; + if (c == '*') { + rest = 1; break; + } + optc++; } } - for (optc = 0; opt && c; c = format[paramc + opt + ++optc]) { - if (c == '*') { - rest = true; - break; - } - } - assert((opt ? 1 : 0) <= optc); /* at least 1 char after '|'? */ - assert(format[paramc + opt + optc + rest] == '\0'); /* no extra chars? */ + if (rest) c = *p++; + assert(opt <= optc); /* at least 1 char after '|'? */ + assert(c == '\0'); /* no extra chars? */ } if (argc < paramc || (paramc + optc < argc && ! rest)) { @@ -80,6 +89,7 @@ pic_get_args(pic_state *pic, const char *format, ...) proc = va_arg(ap, pic_value *); *proc = GET_OPERAND(pic, 0); + format++; /* skip '&' */ } for (i = 1; i <= MIN(paramc + optc, argc); ++i) { @@ -97,6 +107,41 @@ pic_get_args(pic_state *pic, const char *format, ...) break; } + case 'u': { + void **data; + const pic_data_type *type; + pic_value v; + + data = va_arg(ap, void **); + type = va_arg(ap, const pic_data_type *); + v = GET_OPERAND(pic, i); + if (pic_data_p(pic, v, type)) { + *data = pic_data(pic, v); + } + else { + pic_errorf(pic, "pic_get_args: expected data type \"%s\", but got ~s", type->type_name, v); + } + break; + } + + case 'b': { + unsigned char **buf; + int *len; + pic_value v; + + buf = va_arg(ap, unsigned char **); + len = va_arg(ap, int *); + v = GET_OPERAND(pic, i); + if (pic_blob_p(pic, v)) { + unsigned char *tmp = pic_blob(pic, v, len); + if (buf) *buf = tmp; + } + else { + pic_errorf(pic, "pic_get_args: expected bytevector, but got ~s", v); + } + break; + } + #define NUM_CASE(c1, c2, ctype) \ case c1: case c2: { \ ctype *n; \ @@ -149,7 +194,6 @@ pic_get_args(pic_state *pic, const char *format, ...) OBJ_CASE('m', sym) OBJ_CASE('s', str) OBJ_CASE('l', proc) - OBJ_CASE('b', blob) OBJ_CASE('v', vec) OBJ_CASE('d', dict) OBJ_CASE('p', port) @@ -158,6 +202,12 @@ pic_get_args(pic_state *pic, const char *format, ...) default: pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); } + + if (format[1] == '+') { + pic_value *p; + p = va_arg(ap, pic_value*); + *p = GET_OPERAND(pic, i); + } } if (rest) { int *n; From 4751131b4f0ab8465eb637e9fac312b7f2459dc7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 23:01:12 +0900 Subject: [PATCH 078/119] rename PIC_GC_MARK and PIC_GC_UNMARK --- extlib/benz/gc.c | 23 ++++++++++++++--------- extlib/benz/include/picrin/gc.h | 3 --- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index fa35d5be..d7813799 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -5,6 +5,11 @@ #include "picrin.h" #include "picrin/object.h" +enum { + WHITE = 0, + BLACK = 1 +}; + union header { struct { union header *ptr; @@ -269,10 +274,10 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) { loop: - if (obj->u.basic.gc_mark == PIC_GC_MARK) + if (obj->u.basic.gc_mark == BLACK) return; - obj->u.basic.gc_mark = PIC_GC_MARK; + obj->u.basic.gc_mark = BLACK; #define LOOP(o) obj = (struct pic_object *)(o); goto loop @@ -511,8 +516,8 @@ gc_mark_phase(pic_state *pic) continue; key = kh_key(h, it); val = kh_val(h, it); - if (key->u.basic.gc_mark == PIC_GC_MARK) { - if (pic_obj_p(pic, val) && pic_obj_ptr(val)->u.basic.gc_mark == PIC_GC_UNMARK) { + if (key->u.basic.gc_mark == BLACK) { + if (pic_obj_p(pic, val) && pic_obj_ptr(val)->u.basic.gc_mark == WHITE) { gc_mark(pic, val); ++j; } @@ -606,8 +611,8 @@ gc_sweep_page(pic_state *pic, struct heap_page *page) goto escape; } obj = (struct pic_object *)(p + 1); - if (obj->u.basic.gc_mark == PIC_GC_MARK) { - obj->u.basic.gc_mark = PIC_GC_UNMARK; + if (obj->u.basic.gc_mark == BLACK) { + obj->u.basic.gc_mark = WHITE; alive += p->s.size; } else { if (head == NULL) { @@ -652,7 +657,7 @@ gc_sweep_phase(pic_state *pic) if (! kh_exist(h, it)) continue; obj = kh_key(h, it); - if (obj->u.basic.gc_mark == PIC_GC_UNMARK) { + if (obj->u.basic.gc_mark == WHITE) { kh_del(weak, h, it); } } @@ -664,7 +669,7 @@ gc_sweep_phase(pic_state *pic) if (! kh_exist(s, it)) continue; sym = kh_val(s, it); - if (sym->gc_mark == PIC_GC_UNMARK) { + if (sym->gc_mark == WHITE) { kh_del(oblist, s, it); } } @@ -721,7 +726,7 @@ pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) pic_panic(pic, "GC memory exhausted"); } } - obj->u.basic.gc_mark = PIC_GC_UNMARK; + obj->u.basic.gc_mark = WHITE; obj->u.basic.tt = type; return obj; diff --git a/extlib/benz/include/picrin/gc.h b/extlib/benz/include/picrin/gc.h index c7ed0426..cc75a127 100644 --- a/extlib/benz/include/picrin/gc.h +++ b/extlib/benz/include/picrin/gc.h @@ -9,9 +9,6 @@ extern "C" { #endif -#define PIC_GC_UNMARK 0 -#define PIC_GC_MARK 1 - struct pic_heap; struct pic_heap *pic_heap_open(pic_state *); From 10aae770c9d97a298b7f2e6864a525ed3012dc4d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 20 Feb 2016 23:55:40 +0900 Subject: [PATCH 079/119] refactor pic_try/catch --- extlib/benz/cont.c | 57 +++++++++++++++++++++--------- extlib/benz/error.c | 14 ++++++-- extlib/benz/include/picrin.h | 29 ++++++++------- extlib/benz/include/picrin/state.h | 19 ---------- 4 files changed, 67 insertions(+), 52 deletions(-) diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index e4c6aa62..0df3d685 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -5,9 +5,32 @@ #include "picrin.h" #include "picrin/object.h" +struct pic_cont { + PIC_JMPBUF *jmp; + + int id; + + struct pic_checkpoint *cp; + ptrdiff_t sp_offset; + ptrdiff_t ci_offset; + ptrdiff_t xp_offset; + size_t arena_idx; + pic_value ptable; + pic_code *ip; + + int retc; + pic_value *retv; + + struct pic_cont *prev; +}; + +static const pic_data_type cont_type = { "pic_cont", NULL, NULL }; + void -pic_save_point(pic_state *pic, struct pic_cont *cont) +pic_save_point(pic_state *pic, struct pic_cont *cont, PIC_JMPBUF *jmp) { + cont->jmp = jmp; + /* save runtime context */ cont->cp = pic->cp; cont->sp_offset = pic->sp - pic->stbase; @@ -86,9 +109,6 @@ pic_dynamic_wind(pic_state *pic, pic_value in, pic_value thunk, pic_value out) return val; } -#define CV_ID 0 -#define CV_ESCAPE 1 - static pic_value cont_call(pic_state *pic) { @@ -99,7 +119,9 @@ cont_call(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - id = pic_int(pic, pic_closure_ref(pic, CV_ID)); + cont = pic_data(pic, pic_closure_ref(pic, 0)); + + id = cont->id; /* check if continuation is alive */ for (cc = pic->cc; cc != NULL; cc = cc->prev) { @@ -111,13 +133,12 @@ cont_call(pic_state *pic) pic_errorf(pic, "calling dead escape continuation"); } - cont = pic_data(pic, pic_closure_ref(pic, CV_ESCAPE)); cont->retc = argc; cont->retv = argv; pic_load_point(pic, cont); - PIC_LONGJMP(pic, cont->jmp, 1); + PIC_LONGJMP(pic, *cont->jmp, 1); PIC_UNREACHABLE(); } @@ -125,26 +146,30 @@ cont_call(pic_state *pic) pic_value pic_make_cont(pic_state *pic, struct pic_cont *cont) { - static const pic_data_type cont_type = { "cont", NULL, NULL }; + return pic_lambda(pic, cont_call, 1, pic_data_value(pic, cont, &cont_type)); +} - /* save the escape continuation in proc */ - return pic_lambda(pic, cont_call, 2, pic_int_value(pic, cont->id), pic_data_value(pic, cont, &cont_type)); +struct pic_cont * +pic_alloca_cont(pic_state *pic) +{ + return pic_alloca(pic, sizeof(struct pic_cont)); } static pic_value pic_callcc(pic_state *pic, pic_value proc) { - struct pic_cont cont; + PIC_JMPBUF jmp; + struct pic_cont *cont = pic_alloca_cont(pic); - pic_save_point(pic, &cont); - - if (PIC_SETJMP(pic, cont.jmp)) { - return pic_valuesk(pic, cont.retc, cont.retv); + if (PIC_SETJMP(pic, jmp)) { + return pic_valuesk(pic, cont->retc, cont->retv); } else { pic_value val; - val = pic_call(pic, proc, 1, pic_make_cont(pic, &cont)); + pic_save_point(pic, cont, &jmp); + + val = pic_call(pic, proc, 1, pic_make_cont(pic, cont)); pic_exit_point(pic); diff --git a/extlib/benz/error.c b/extlib/benz/error.c index b854508f..08b1808e 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -47,8 +47,8 @@ pic_errorf(pic_state *pic, const char *fmt, ...) pic_error(pic, "", msg, pic_nil_value(pic)); } -pic_value -pic_native_exception_handler(pic_state *pic) +static pic_value +native_exception_handler(pic_state *pic) { pic_value err; @@ -61,6 +61,16 @@ pic_native_exception_handler(pic_state *pic) PIC_UNREACHABLE(); } +void +pic_push_native_handler(pic_state *pic, struct pic_cont *cont) +{ + pic_value handler; + + handler = pic_lambda(pic, native_exception_handler, 1, pic_make_cont(pic, cont)); + + pic_push_handler(pic, handler); +} + void pic_push_handler(pic_state *pic, pic_value handler) { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index df9e8bce..72c82833 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -345,6 +345,14 @@ void pic_load_cstr(pic_state *, const char *); pic_in_library(pic, lib); \ } while (0) +struct pic_cont *pic_alloca_cont(pic_state *); +pic_value pic_make_cont(pic_state *, struct pic_cont *); +void pic_push_native_handler(pic_state *, struct pic_cont *); +void pic_push_handler(pic_state *, pic_value); +pic_value pic_pop_handler(pic_state *); +void pic_save_point(pic_state *, struct pic_cont *, PIC_JMPBUF *); +void pic_exit_point(pic_state *); + /* do not return from try block! */ #define pic_try \ @@ -353,22 +361,13 @@ void pic_load_cstr(pic_state *, const char *); pic_catch_(PIC_GENSYM(label)) #define pic_try_(cont, handler) \ do { \ - extern pic_value pic_make_cont(pic_state *, struct pic_cont *); \ - extern void pic_push_handler(pic_state *, pic_value proc); \ - extern pic_value pic_pop_handler(pic_state *); \ - extern pic_value pic_native_exception_handler(pic_state *); \ - extern void pic_save_point(pic_state *, struct pic_cont *); \ - extern void pic_exit_point(pic_state *); \ - struct pic_cont cont; \ - pic_save_point(pic, &cont); \ - if (PIC_SETJMP(pic, cont.jmp) == 0) { \ - pic_value handler; \ - handler = pic_lambda(pic, pic_native_exception_handler, 1, pic_make_cont(pic, &cont)); \ - do { \ - pic_push_handler(pic, handler); + PIC_JMPBUF jmp; \ + struct pic_cont *cont = pic_alloca_cont(pic); \ + if (PIC_SETJMP(pic, jmp) == 0) { \ + pic_save_point(pic, cont, &jmp); \ + pic_push_native_handler(pic, cont); #define pic_catch_(label) \ - pic_pop_handler(pic); \ - } while (0); \ + pic_pop_handler(pic); \ pic_exit_point(pic); \ } else { \ goto label; \ diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/state.h index 2d1566da..eb224086 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/state.h @@ -22,25 +22,6 @@ struct pic_lib { struct pic_dict *exports; }; -struct pic_cont { - PIC_JMPBUF jmp; - - int id; - - struct pic_checkpoint *cp; - ptrdiff_t sp_offset; - ptrdiff_t ci_offset; - ptrdiff_t xp_offset; - size_t arena_idx; - pic_value ptable; - pic_code *ip; - - int retc; - pic_value *retv; - - struct pic_cont *prev; -}; - typedef struct { int argc, retc; pic_code *ip; From 317369a9146e86dbd3d76b0861ba48a0486c8f73 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 00:30:40 +0900 Subject: [PATCH 080/119] don't include picrin/state.h by default --- contrib/10.callcc/callcc.c | 1 + etc/mkloader.pl | 2 +- extlib/benz/cont.c | 1 + extlib/benz/debug.c | 11 +++++++---- extlib/benz/error.c | 7 +++++++ extlib/benz/eval.c | 3 ++- extlib/benz/file.c | 5 +++++ extlib/benz/gc.c | 1 + extlib/benz/include/picrin.h | 7 ++++--- extlib/benz/include/picrin/object.h | 2 ++ extlib/benz/lib.c | 1 + extlib/benz/load.c | 2 +- extlib/benz/macro.c | 1 + extlib/benz/port.c | 1 + extlib/benz/proc.c | 1 + extlib/benz/read.c | 3 ++- extlib/benz/state.c | 1 + extlib/benz/symbol.c | 1 + extlib/benz/var.c | 1 + extlib/benz/write.c | 1 + 20 files changed, 42 insertions(+), 11 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 280b8331..cf1c310b 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -1,5 +1,6 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" struct pic_fullcont { jmp_buf jmp; diff --git a/etc/mkloader.pl b/etc/mkloader.pl index 527efd7a..088bd962 100755 --- a/etc/mkloader.pl +++ b/etc/mkloader.pl @@ -53,7 +53,7 @@ EOL pic_catch { /* error! */ xfputs(pic, "fatal error: failure in loading $dirname/$basename\\n", xstderr); - pic_raise(pic, pic->err); + pic_raise(pic, pic_err(pic)); } EOL } diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 0df3d685..21ffde89 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" struct pic_cont { PIC_JMPBUF *jmp; diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 21a5211d..bc96a209 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" pic_value pic_get_backtrace(pic_state *pic) @@ -36,16 +37,18 @@ pic_get_backtrace(pic_state *pic) void pic_print_backtrace(pic_state *pic, xFILE *file) { - assert(! pic_invalid_p(pic, pic->err)); + pic_value err = pic_err(pic); - if (! pic_error_p(pic, pic->err)) { + assert(! pic_invalid_p(pic, err)); + + if (! pic_error_p(pic, err)) { xfprintf(pic, file, "raise: "); - pic_fwrite(pic, pic->err, file); + pic_fwrite(pic, err, file); } else { struct pic_error *e; pic_value elem, it; - e = pic_error_ptr(pic, pic->err); + e = pic_error_ptr(pic, err); if (! pic_eq_p(pic, pic_obj_value(e->type), pic_intern_lit(pic, ""))) { pic_fwrite(pic, pic_obj_value(e->type), file); xfprintf(pic, file, " "); diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 08b1808e..b750a19a 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" void pic_panic(pic_state PIC_UNUSED(*pic), const char *msg) @@ -98,6 +99,12 @@ pic_pop_handler(pic_state *pic) return pic_obj_value(*--pic->xp); } +pic_value +pic_err(pic_state *pic) +{ + return pic->err; +} + pic_value pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index ace01513..02393339 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -5,6 +5,7 @@ #include "picrin.h" #include "picrin/object.h" #include "picrin/opcode.h" +#include "picrin/state.h" static pic_value optimize_beta(pic_state *pic, pic_value expr) @@ -891,7 +892,7 @@ pic_eval(pic_state *pic, pic_value program, const char *lib) } pic_catch { pic_in_library(pic, prev_lib); - pic_raise(pic, pic->err); + pic_raise(pic, pic_err(pic)); } pic_in_library(pic, prev_lib); diff --git a/extlib/benz/file.c b/extlib/benz/file.c index ac6f231b..bdb2e840 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -1,4 +1,9 @@ +/** + * See Copyright Notice in picrin.h + */ + #include "picrin.h" +#include "picrin/state.h" #ifndef EOF # define EOF (-1) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index d7813799..a7fe6338 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" enum { WHITE = 0, diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 72c82833..0bfd26e2 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -98,7 +98,6 @@ void pic_export(pic_state *, pic_value sym); PIC_NORETURN void pic_panic(pic_state *, const char *msg); PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); -PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); PIC_NORETURN void pic_raise(pic_state *, pic_value v); pic_value pic_lambda(pic_state *, pic_func_t f, int n, ...); @@ -296,8 +295,6 @@ int xvfprintf(pic_state *, xFILE *fp, const char *fmt, va_list); /* extra stuff */ -#include "picrin/state.h" - void *pic_default_allocf(void *, void *, size_t); #define pic_assert_type(pic, v, type) \ @@ -355,6 +352,8 @@ void pic_exit_point(pic_state *); /* do not return from try block! */ +pic_value pic_err(pic_state *); + #define pic_try \ pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) #define pic_catch \ @@ -376,6 +375,8 @@ void pic_exit_point(pic_state *); if (0) \ label: +PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); + #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) diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/object.h index 397740d3..761d13f9 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/object.h @@ -9,6 +9,8 @@ extern "C" { #endif +#include "picrin/khash.h" + typedef struct pic_identifier pic_id; typedef pic_id pic_sym; diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index a7e9336a..63ea7a93 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" KHASH_DEFINE(ltable, const char *, struct pic_lib, kh_str_hash_func, kh_str_cmp_func) diff --git a/extlib/benz/load.c b/extlib/benz/load.c index 2ceefa11..7272c000 100644 --- a/extlib/benz/load.c +++ b/extlib/benz/load.c @@ -27,7 +27,7 @@ pic_load_cstr(pic_state *pic, const char *str) } pic_catch { pic_close_port(pic, port); - pic_raise(pic, pic->err); + pic_raise(pic, pic_err(pic)); } pic_close_port(pic, port); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 6825aa62..2f8dc9cd 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal) diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 6933c07a..af8a1f5b 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/file.h" #undef EOF #define EOF (-1) diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 3a0c70ae..5182a6b5 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -5,6 +5,7 @@ #include "picrin.h" #include "picrin/object.h" #include "picrin/opcode.h" +#include "picrin/state.h" #define MIN(x,y) ((x) < (y) ? (x) : (y)) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 86991f82..39404653 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" #undef EOF #define EOF (-1) @@ -852,7 +853,7 @@ pic_read_cstr(pic_state *pic, const char *str) } pic_catch { pic_close_port(pic, port); - pic_raise(pic, pic->err); + pic_raise(pic, pic_err(pic)); } pic_close_port(pic, port); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 58e35526..afd0c412 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" static void pic_init_features(pic_state *pic) diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 20941f70..b073421e 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" #define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0) diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 6f59c0c7..df5ee003 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" static pic_value var_get(pic_state *pic, pic_value var) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 8d2276ed..d9822db3 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -4,6 +4,7 @@ #include "picrin.h" #include "picrin/object.h" +#include "picrin/state.h" KHASH_DECLARE(l, void *, int) KHASH_DECLARE(v, void *, int) From 53b760cfac39cc19740df26c854a3919c4e869da Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 00:58:58 +0900 Subject: [PATCH 081/119] add picrin/extra.h --- contrib/10.math/math.c | 1 + contrib/20.r7rs/src/file.c | 1 + contrib/20.r7rs/src/load.c | 1 + contrib/20.r7rs/src/mutable-string.c | 1 + contrib/20.r7rs/src/system.c | 1 + contrib/20.r7rs/src/time.c | 1 + contrib/30.random/src/random.c | 1 + contrib/30.readline/src/readline.c | 1 + contrib/30.regexp/src/regexp.c | 1 + contrib/40.srfi/src/106.c | 1 + contrib/60.repl/repl.c | 3 +- etc/mkloader.pl | 1 + extlib/benz/blob.c | 1 + extlib/benz/debug.c | 1 + extlib/benz/dict.c | 1 + extlib/benz/error.c | 1 + extlib/benz/eval.c | 1 + extlib/benz/include/picrin.h | 115 ----------------------- extlib/benz/include/picrin/extra.h | 131 +++++++++++++++++++++++++++ extlib/benz/lib.c | 1 + extlib/benz/load.c | 1 + extlib/benz/macro.c | 1 + extlib/benz/number.c | 1 + extlib/benz/pair.c | 1 + extlib/benz/port.c | 1 + extlib/benz/proc.c | 1 + extlib/benz/read.c | 1 + extlib/benz/state.c | 1 + extlib/benz/string.c | 1 + extlib/benz/symbol.c | 1 + extlib/benz/var.c | 1 + extlib/benz/vector.c | 1 + extlib/benz/write.c | 1 + src/main.c | 1 + 34 files changed, 163 insertions(+), 117 deletions(-) create mode 100644 extlib/benz/include/picrin/extra.h diff --git a/contrib/10.math/math.c b/contrib/10.math/math.c index cbaeec7a..187941ad 100644 --- a/contrib/10.math/math.c +++ b/contrib/10.math/math.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/extra.h" #include diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 5cb99ffc..62942ecd 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/extra.h" #include diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index b7459bb6..3503eda9 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/extra.h" #include diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c index bc688e10..539d4680 100644 --- a/contrib/20.r7rs/src/mutable-string.c +++ b/contrib/20.r7rs/src/mutable-string.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/extra.h" #include "picrin/object.h" void diff --git a/contrib/20.r7rs/src/system.c b/contrib/20.r7rs/src/system.c index fc5d2ecf..4d0fdb15 100644 --- a/contrib/20.r7rs/src/system.c +++ b/contrib/20.r7rs/src/system.c @@ -5,6 +5,7 @@ #include #include "picrin.h" +#include "picrin/extra.h" extern int picrin_argc; extern char **picrin_argv; diff --git a/contrib/20.r7rs/src/time.c b/contrib/20.r7rs/src/time.c index ac8585d3..5c325bbd 100644 --- a/contrib/20.r7rs/src/time.c +++ b/contrib/20.r7rs/src/time.c @@ -5,6 +5,7 @@ #include #include "picrin.h" +#include "picrin/extra.h" #define UTC_TAI_DIFF 35 diff --git a/contrib/30.random/src/random.c b/contrib/30.random/src/random.c index 95fb7a03..2c7f9d6f 100644 --- a/contrib/30.random/src/random.c +++ b/contrib/30.random/src/random.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/extra.h" double genrand_real3(void); diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index 1438b0a7..6153cdbb 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -6,6 +6,7 @@ forget to use the C++ extern "C" to get it to compile. */ #include "picrin.h" +#include "picrin/extra.h" #include diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index c77da4e8..8f7f0c89 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/extra.h" #include diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 3d1a0cdd..72633481 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -1,4 +1,5 @@ #include "picrin.h" +#include "picrin/extra.h" #include #include diff --git a/contrib/60.repl/repl.c b/contrib/60.repl/repl.c index e6e371de..a4585e73 100644 --- a/contrib/60.repl/repl.c +++ b/contrib/60.repl/repl.c @@ -1,12 +1,11 @@ #include "picrin.h" +#include "picrin/extra.h" #include - static pic_value pic_repl_tty_p(pic_state *pic) { - pic_get_args(pic, ""); return pic_bool_value(pic, (isatty(STDIN_FILENO))); diff --git a/etc/mkloader.pl b/etc/mkloader.pl index 088bd962..be2d7414 100755 --- a/etc/mkloader.pl +++ b/etc/mkloader.pl @@ -12,6 +12,7 @@ print < Date: Sun, 21 Feb 2016 01:04:55 +0900 Subject: [PATCH 082/119] integrate mutable-string.c into the core --- contrib/20.r7rs/nitro.mk | 1 - contrib/20.r7rs/scheme/base.scm | 1 - contrib/20.r7rs/src/mutable-string.c | 104 --------------------------- contrib/20.r7rs/src/r7rs.c | 2 - extlib/benz/string.c | 96 ++++++++++++++++++++++++- 5 files changed, 95 insertions(+), 109 deletions(-) delete mode 100644 contrib/20.r7rs/src/mutable-string.c diff --git a/contrib/20.r7rs/nitro.mk b/contrib/20.r7rs/nitro.mk index 235a68c8..085da6e6 100644 --- a/contrib/20.r7rs/nitro.mk +++ b/contrib/20.r7rs/nitro.mk @@ -4,7 +4,6 @@ CONTRIB_SRCS += \ contrib/20.r7rs/src/r7rs.c\ contrib/20.r7rs/src/file.c\ contrib/20.r7rs/src/load.c\ - contrib/20.r7rs/src/mutable-string.c\ contrib/20.r7rs/src/system.c\ contrib/20.r7rs/src/time.c diff --git a/contrib/20.r7rs/scheme/base.scm b/contrib/20.r7rs/scheme/base.scm index d8d487d1..162128f5 100644 --- a/contrib/20.r7rs/scheme/base.scm +++ b/contrib/20.r7rs/scheme/base.scm @@ -13,7 +13,6 @@ nan? infinite?) (picrin macro) - (picrin string) (scheme file)) ;; 4.1.2. Literal expressions diff --git a/contrib/20.r7rs/src/mutable-string.c b/contrib/20.r7rs/src/mutable-string.c deleted file mode 100644 index 539d4680..00000000 --- a/contrib/20.r7rs/src/mutable-string.c +++ /dev/null @@ -1,104 +0,0 @@ -#include "picrin.h" -#include "picrin/extra.h" -#include "picrin/object.h" - -void -pic_str_update(pic_state *pic, pic_value dst, pic_value src) -{ - pic_rope_incref(pic, pic_str_ptr(pic, src)->rope); - pic_rope_decref(pic, pic_str_ptr(pic, dst)->rope); - pic_str_ptr(pic, dst)->rope = pic_str_ptr(pic, src)->rope; -} - -static pic_value -pic_str_string_set(pic_state *pic) -{ - pic_value str, x, y, z; - char c; - int k, len; - - pic_get_args(pic, "sic", &str, &k, &c); - - len = pic_str_len(pic, str); - - VALID_INDEX(pic, len, k); - - x = pic_str_sub(pic, str, 0, k); - y = pic_str_value(pic, &c, 1); - z = pic_str_sub(pic, str, k + 1, len); - - pic_str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); - - return pic_undef_value(pic); -} - -static pic_value -pic_str_string_copy_ip(pic_state *pic) -{ - pic_value to, from, x, y, z; - int n, at, start, end, tolen, fromlen; - - n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end); - - tolen = pic_str_len(pic, to); - fromlen = pic_str_len(pic, from); - - switch (n) { - case 3: - start = 0; - case 4: - end = fromlen; - } - - VALID_ATRANGE(pic, tolen, at, fromlen, start, end); - - x = pic_str_sub(pic, to, 0, at); - y = pic_str_sub(pic, from, start, end); - z = pic_str_sub(pic, to, at + end - start, tolen); - - pic_str_update(pic, to, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); - - return pic_undef_value(pic); -} - -static pic_value -pic_str_string_fill_ip(pic_state *pic) -{ - pic_value str, x, y, z; - char c, *buf; - int n, start, end, len; - - n = pic_get_args(pic, "sc|ii", &str, &c, &start, &end); - - len = pic_str_len(pic, str); - - switch (n) { - case 2: - start = 0; - case 3: - end = len; - } - - VALID_RANGE(pic, len, start, end); - - buf = pic_alloca(pic, end - start); - memset(buf, c, end - start); - - x = pic_str_sub(pic, str, 0, start); - y = pic_str_value(pic, buf, end - start); - z = pic_str_sub(pic, str, end, len); - - pic_str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); - - return pic_undef_value(pic); -} - -void -pic_init_mutable_string(pic_state *pic) -{ - pic_deflibrary(pic, "picrin.string"); - - pic_defun(pic, "string-set!", pic_str_string_set); - pic_defun(pic, "string-copy!", pic_str_string_copy_ip); - pic_defun(pic, "string-fill!", pic_str_string_fill_ip); -} diff --git a/contrib/20.r7rs/src/r7rs.c b/contrib/20.r7rs/src/r7rs.c index 301f9152..43f98ed4 100644 --- a/contrib/20.r7rs/src/r7rs.c +++ b/contrib/20.r7rs/src/r7rs.c @@ -6,7 +6,6 @@ void pic_init_file(pic_state *); void pic_init_load(pic_state *); -void pic_init_mutable_string(pic_state *); void pic_init_system(pic_state *); void pic_init_time(pic_state *); @@ -15,7 +14,6 @@ pic_init_r7rs(pic_state *pic) { pic_init_file(pic); pic_init_load(pic); - pic_init_mutable_string(pic); pic_init_system(pic); pic_init_time(pic); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 7f834138..431d2fd2 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -239,6 +239,14 @@ rope_cstr(pic_state *pic, struct pic_rope *x) return c->str; } +static void +str_update(pic_state *pic, pic_value dst, pic_value src) +{ + pic_rope_incref(pic, pic_str_ptr(pic, src)->rope); + pic_rope_decref(pic, pic_str_ptr(pic, dst)->rope); + pic_str_ptr(pic, dst)->rope = pic_str_ptr(pic, src)->rope; +} + pic_value pic_str_value(pic_state *pic, const char *str, int len) { @@ -478,6 +486,28 @@ pic_str_string_ref(pic_state *pic) return pic_char_value(pic, pic_str_ref(pic, str, k)); } +static pic_value +pic_str_string_set(pic_state *pic) +{ + pic_value str, x, y, z; + char c; + int k, len; + + pic_get_args(pic, "sic", &str, &k, &c); + + len = pic_str_len(pic, str); + + VALID_INDEX(pic, len, k); + + x = pic_str_sub(pic, str, 0, k); + y = pic_str_value(pic, &c, 1); + z = pic_str_sub(pic, str, k + 1, len); + + str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); + + return pic_undef_value(pic); +} + #define DEFINE_STRING_CMP(name, op) \ static pic_value \ pic_str_string_##name(pic_state *pic) \ @@ -530,6 +560,67 @@ pic_str_string_copy(pic_state *pic) return pic_str_sub(pic, str, start, end); } +static pic_value +pic_str_string_copy_ip(pic_state *pic) +{ + pic_value to, from, x, y, z; + int n, at, start, end, tolen, fromlen; + + n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end); + + tolen = pic_str_len(pic, to); + fromlen = pic_str_len(pic, from); + + switch (n) { + case 3: + start = 0; + case 4: + end = fromlen; + } + + VALID_ATRANGE(pic, tolen, at, fromlen, start, end); + + x = pic_str_sub(pic, to, 0, at); + y = pic_str_sub(pic, from, start, end); + z = pic_str_sub(pic, to, at + end - start, tolen); + + str_update(pic, to, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); + + return pic_undef_value(pic); +} + +static pic_value +pic_str_string_fill_ip(pic_state *pic) +{ + pic_value str, x, y, z; + char c, *buf; + int n, start, end, len; + + n = pic_get_args(pic, "sc|ii", &str, &c, &start, &end); + + len = pic_str_len(pic, str); + + switch (n) { + case 2: + start = 0; + case 3: + end = len; + } + + VALID_RANGE(pic, len, start, end); + + buf = pic_alloca(pic, end - start); + memset(buf, c, end - start); + + x = pic_str_sub(pic, str, 0, start); + y = pic_str_value(pic, buf, end - start); + z = pic_str_sub(pic, str, end, len); + + str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z))); + + return pic_undef_value(pic); +} + static pic_value pic_str_string_append(pic_state *pic) { @@ -661,7 +752,7 @@ pic_str_string_to_list(pic_state *pic) } return pic_reverse(pic, list); } - + void pic_init_str(pic_state *pic) { @@ -670,7 +761,10 @@ pic_init_str(pic_state *pic) pic_defun(pic, "make-string", pic_str_make_string); pic_defun(pic, "string-length", pic_str_string_length); pic_defun(pic, "string-ref", pic_str_string_ref); + pic_defun(pic, "string-set!", pic_str_string_set); pic_defun(pic, "string-copy", pic_str_string_copy); + pic_defun(pic, "string-copy!", pic_str_string_copy_ip); + pic_defun(pic, "string-fill!", pic_str_string_fill_ip); pic_defun(pic, "string-append", pic_str_string_append); pic_defun(pic, "string-map", pic_str_string_map); pic_defun(pic, "string-for-each", pic_str_string_for_each); From 8dd423cdbc9c885d7fe86d4aa4c4195b87d27b39 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 01:13:16 +0900 Subject: [PATCH 083/119] add include/picrin/private dir --- contrib/10.callcc/callcc.c | 4 +- extlib/benz/blob.c | 2 +- extlib/benz/bool.c | 2 +- extlib/benz/cont.c | 4 +- extlib/benz/data.c | 2 +- extlib/benz/debug.c | 4 +- extlib/benz/dict.c | 2 +- extlib/benz/error.c | 4 +- extlib/benz/eval.c | 6 +- extlib/benz/file.c | 2 +- extlib/benz/gc.c | 4 +- extlib/benz/include/picrin/compat.h | 386 ------------------ .../benz/include/picrin/{ => private}/file.h | 0 extlib/benz/include/picrin/{ => private}/gc.h | 0 .../benz/include/picrin/{ => private}/irep.h | 0 .../benz/include/picrin/{ => private}/khash.h | 0 .../include/picrin/{ => private}/object.h | 2 +- .../include/picrin/{ => private}/opcode.h | 0 .../benz/include/picrin/{ => private}/read.h | 0 .../benz/include/picrin/{ => private}/state.h | 10 +- extlib/benz/include/picrin/setup.h | 372 ++++++++++++++++- extlib/benz/lib.c | 4 +- extlib/benz/macro.c | 4 +- extlib/benz/pair.c | 2 +- extlib/benz/port.c | 4 +- extlib/benz/proc.c | 6 +- extlib/benz/read.c | 4 +- extlib/benz/record.c | 2 +- extlib/benz/state.c | 4 +- extlib/benz/string.c | 2 +- extlib/benz/symbol.c | 4 +- extlib/benz/value.c | 2 +- extlib/benz/var.c | 4 +- extlib/benz/vector.c | 2 +- extlib/benz/weak.c | 2 +- extlib/benz/write.c | 4 +- 36 files changed, 420 insertions(+), 436 deletions(-) delete mode 100644 extlib/benz/include/picrin/compat.h rename extlib/benz/include/picrin/{ => private}/file.h (100%) rename extlib/benz/include/picrin/{ => private}/gc.h (100%) rename extlib/benz/include/picrin/{ => private}/irep.h (100%) rename extlib/benz/include/picrin/{ => private}/khash.h (100%) rename extlib/benz/include/picrin/{ => private}/object.h (99%) rename extlib/benz/include/picrin/{ => private}/opcode.h (100%) rename extlib/benz/include/picrin/{ => private}/read.h (100%) rename extlib/benz/include/picrin/{ => private}/state.h (92%) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index cf1c310b..3f73cdd5 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -1,6 +1,6 @@ #include "picrin.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" struct pic_fullcont { jmp_buf jmp; diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index f1367b04..04befd71 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -4,7 +4,7 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" +#include "picrin/private/object.h" pic_value pic_blob_value(pic_state *pic, const unsigned char *buf, int len) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index f01e65ac..7c6b2a54 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -3,7 +3,7 @@ */ #include "picrin.h" -#include "picrin/object.h" +#include "picrin/private/object.h" #if PIC_NAN_BOXING diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 21ffde89..a3d9f223 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -3,8 +3,8 @@ */ #include "picrin.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" struct pic_cont { PIC_JMPBUF *jmp; diff --git a/extlib/benz/data.c b/extlib/benz/data.c index a1a84dfd..23e0baa5 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -1,5 +1,5 @@ #include "picrin.h" -#include "picrin/object.h" +#include "picrin/private/object.h" bool pic_data_p(pic_state *pic, pic_value obj, const pic_data_type *type) diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index db0ef8b2..225433c4 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" pic_value pic_get_backtrace(pic_state *pic) diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index b8510034..2d468c09 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -4,7 +4,7 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" +#include "picrin/private/object.h" KHASH_DEFINE(dict, pic_sym *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 541744ea..744b7dab 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" void pic_panic(pic_state PIC_UNUSED(*pic), const char *msg) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 0b7976c2..c0aa5548 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -4,9 +4,9 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/opcode.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/opcode.h" +#include "picrin/private/state.h" static pic_value optimize_beta(pic_state *pic, pic_value expr) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index bdb2e840..0fb6fc83 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -3,7 +3,7 @@ */ #include "picrin.h" -#include "picrin/state.h" +#include "picrin/private/state.h" #ifndef EOF # define EOF (-1) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index a7fe6338..348f2ee9 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -3,8 +3,8 @@ */ #include "picrin.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" enum { WHITE = 0, diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h deleted file mode 100644 index e367a6ce..00000000 --- a/extlib/benz/include/picrin/compat.h +++ /dev/null @@ -1,386 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_COMPAT_H -#define PICRIN_COMPAT_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#if __STDC_VERSION__ >= 199901L -# include -#else -# define bool char -# define true 1 -# define false 0 -#endif - -#if __STDC_VERSION__ >= 199901L -# include -#elif ! defined(offsetof) -# define offsetof(s,m) ((size_t)&(((s *)NULL)->m)) -#endif - -#if __STDC_VERSION__ >= 199901L -# include -#else -# if INT_MAX > 2147483640L /* borrowed from luaconf.h */ -typedef int int32_t; -typedef unsigned int uint32_t; -# else -typedef long int32_t; -typedef unsigned long uint32_t; -# endif -#endif - -#if __STDC_VERSION__ >= 201112L -# include -# define PIC_NORETURN noreturn -#elif __GNUC__ || __clang__ -# define PIC_NORETURN __attribute__((noreturn)) -#else -# define PIC_NORETURN -#endif - -#if __STDC_VERSION__ >= 199901L -# define PIC_INLINE static inline -#elif __GNUC__ || __clang__ -# define PIC_INLINE static __inline__ -#else -# define PIC_INLINE static -#endif - -#define PIC_FALLTHROUGH ((void)0) - -#if __GNUC__ || __clang__ -# define PIC_UNUSED(v) __attribute__((unused)) v -#else -# define PIC_UNUSED(v) v -#endif - -#define PIC_GENSYM2_(x,y) PIC_G##x##_##y##_ -#define PIC_GENSYM1_(x,y) PIC_GENSYM2_(x,y) -#if defined(__COUNTER__) -# define PIC_GENSYM(x) PIC_GENSYM1_(__COUNTER__,x) -#else -# define PIC_GENSYM(x) PIC_GENSYM1_(__LINE__,x) -#endif - -#if __GNUC__ -# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) -#endif -#if GCC_VERSION >= 40500 || __clang__ -# define PIC_UNREACHABLE() (__builtin_unreachable()) -#else -# define PIC_UNREACHABLE() (assert(false)) -#endif -#if __GNUC__ -# undef GCC_VERSION -#endif - -#define PIC_SWAP(type,a,b) \ - PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b) -#define PIC_SWAP_HELPER_(type,tmp,a,b) \ - do { \ - type tmp = (a); \ - (a) = (b); \ - (b) = tmp; \ - } while (0) - - -#if PIC_ENABLE_LIBC - -#include -#include -#include -#include - -#else - -# define assert(v) (void)0 - -PIC_INLINE int -isspace(int c) -{ - return c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f' || c == '\n'; -} - -PIC_INLINE int -tolower(int c) -{ - return ('A' <= c && c <= 'Z') ? c - 'A' + 'a' : c; -} - -PIC_INLINE int -isdigit(int c) -{ - return '0' <= c && c <= '9'; -} - -PIC_INLINE char * -strchr(const char *s, int c) -{ - do { - if (*s == c) - return (char *)s; - } while (*s++ != '\0'); - return NULL; -} - -PIC_INLINE size_t -strlen(const char *s) -{ - size_t l = 0; - - while (*s++) { - l++; - } - return l; -} - -PIC_INLINE int -strcmp(const char *s1, const char *s2) -{ - while (*s1 && *s1 == *s2) { - s1++; - s2++; - } - return (unsigned)*s1 - (unsigned)*s2; -} - -PIC_INLINE long -strtol(const char *nptr, char **endptr, int base) -{ - long l = 0; - char c; - int n; - - while (1) { - c = *nptr; - if ('0' <= c && c <= '9') - n = c - '0'; - else if ('a' <= c && c <= 'z') - n = c - 'a' + 10; - else if ('A' <= c && c <= 'Z') - n = c - 'A' + 10; - else - goto exit; - - if (base <= n) - goto exit; - - l = l * base + n; - nptr++; - } - exit: - if (endptr) - *endptr = (char *)nptr; - return l; -} - -PIC_INLINE void * -memset(void *s, int n, size_t c) -{ - char *p = s; - - while (c-- > 0) { - *p++ = n; - } - return s; -} - -PIC_INLINE void * -memcpy(void *dst, const void *src, size_t n) -{ - const char *s = src; - char *d = dst; - - while (n-- > 0) { - *d++ = *s++; - } - return d; -} - -PIC_INLINE void * -memmove(void *dst, const void *src, size_t n) -{ - const char *s = src; - char *d = dst; - - if (d <= s || d >= s + n) { - memcpy(dst, src, n); - } else { - s += n; - d += n; - while (n-- > 0) { - *--d = *--s; - } - } - return d; -} - -PIC_INLINE int -memcmp(const void *b1, const void *b2, size_t n) -{ - const char *s1 = b1, *s2 = b2; - - while (*s1 == *s2 && n-- > 0) { - s1++; - s2++; - } - return (unsigned)*s1 - (unsigned)*s2; -} - -PIC_INLINE char * -strcpy(char *dst, const char *src) -{ - char *d = dst; - - while ((*dst++ = *src++) != 0); - - return d; -} - -PIC_INLINE double -atof(const char *nptr) -{ - int c; - double f, g, h; - int exp, s, i, e; - unsigned u; - - /* note that picrin_read always assures that *nptr is a digit, never a '+' or '-' */ - /* in other words, the result of atof will always be positive */ - - /* mantissa */ - /* pre '.' */ - u = *nptr++ - '0'; - while (isdigit(c = *nptr)) { - u = u * 10 + (*nptr++ - '0'); - } - if (c == '.') { - nptr++; - /* after '.' */ - g = 0, e = 0; - while (isdigit(c = *nptr)) { - g = g * 10 + (*nptr++ - '0'); - e++; - } - h = 1.0; - while (e-- > 0) { - h /= 10; - } - f = u + g * h; - } - else { - f = u; - } - /* suffix, i.e., exponent */ - s = 0; - exp = 0; - c = *nptr; - - if (c == 'e' && c == 'E') { - nptr++; - switch ((c = *nptr++)) { - case '-': - s = 1; - case '+': - c = *nptr++; - default: - exp = c - '0'; - while (isdigit(c = *nptr)) { - exp = exp * 10 + (*nptr++ - '0'); - } - } - } - e = 10; - for (i = 0; exp; ++i) { - if ((exp & 1) != 0) { - f = s ? f / e : (f * e); - } - e *= e; - exp >>= 1; - } - return f; -} - -#endif - -#if PIC_ENABLE_STDIO -# include - -PIC_INLINE void -pic_dtoa(double dval, char *buf) -{ - sprintf(buf, "%g", dval); -} - -#else - -PIC_INLINE void -pic_dtoa(double dval, char *buf) -{ -# define fabs(x) ((x) >= 0 ? (x) : -(x)) - long lval, tlval; - int ival; - int scnt, ecnt, cnt = 0; - if (dval < 0) { - dval = -dval; - buf[cnt++] = '-'; - } - lval = tlval = (long)dval; - scnt = cnt; - do { - buf[cnt++] = '0' + (tlval % 10); - } while ((tlval /= 10) != 0); - ecnt = cnt; - while (scnt < ecnt) { - char c = buf[scnt]; - buf[scnt++] = buf[--ecnt]; - buf[ecnt] = c; - } - buf[cnt++] = '.'; - dval -= lval; - if ((ival = fabs(dval) * 1e4 + 0.5) == 0) { - buf[cnt++] = '0'; - buf[cnt++] = '0'; - buf[cnt++] = '0'; - buf[cnt++] = '0'; - } else { - if (ival < 1000) buf[cnt++] = '0'; - if (ival < 100) buf[cnt++] = '0'; - if (ival < 10) buf[cnt++] = '0'; - scnt = cnt; - do { - buf[cnt++] = '0' + (ival % 10); - } while ((ival /= 10) != 0); - ecnt = cnt; - while (scnt < ecnt) { - char c = buf[scnt]; - buf[scnt++] = buf[--ecnt]; - buf[ecnt] = c; - } - } - buf[cnt] = 0; -} - -#endif - -#ifndef PIC_DOUBLE_TO_CSTRING -#define PIC_DOUBLE_TO_CSTRING pic_dtoa -#endif -void PIC_DOUBLE_TO_CSTRING(double, char *); - -#ifndef PIC_CSTRING_TO_DOUBLE -#define PIC_CSTRING_TO_DOUBLE atof -#endif -double PIC_CSTRING_TO_DOUBLE(const char *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/private/file.h similarity index 100% rename from extlib/benz/include/picrin/file.h rename to extlib/benz/include/picrin/private/file.h diff --git a/extlib/benz/include/picrin/gc.h b/extlib/benz/include/picrin/private/gc.h similarity index 100% rename from extlib/benz/include/picrin/gc.h rename to extlib/benz/include/picrin/private/gc.h diff --git a/extlib/benz/include/picrin/irep.h b/extlib/benz/include/picrin/private/irep.h similarity index 100% rename from extlib/benz/include/picrin/irep.h rename to extlib/benz/include/picrin/private/irep.h diff --git a/extlib/benz/include/picrin/khash.h b/extlib/benz/include/picrin/private/khash.h similarity index 100% rename from extlib/benz/include/picrin/khash.h rename to extlib/benz/include/picrin/private/khash.h diff --git a/extlib/benz/include/picrin/object.h b/extlib/benz/include/picrin/private/object.h similarity index 99% rename from extlib/benz/include/picrin/object.h rename to extlib/benz/include/picrin/private/object.h index 761d13f9..b3cc4b4a 100644 --- a/extlib/benz/include/picrin/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -9,7 +9,7 @@ extern "C" { #endif -#include "picrin/khash.h" +#include "picrin/private/khash.h" typedef struct pic_identifier pic_id; typedef pic_id pic_sym; diff --git a/extlib/benz/include/picrin/opcode.h b/extlib/benz/include/picrin/private/opcode.h similarity index 100% rename from extlib/benz/include/picrin/opcode.h rename to extlib/benz/include/picrin/private/opcode.h diff --git a/extlib/benz/include/picrin/read.h b/extlib/benz/include/picrin/private/read.h similarity index 100% rename from extlib/benz/include/picrin/read.h rename to extlib/benz/include/picrin/private/read.h diff --git a/extlib/benz/include/picrin/state.h b/extlib/benz/include/picrin/private/state.h similarity index 92% rename from extlib/benz/include/picrin/state.h rename to extlib/benz/include/picrin/private/state.h index eb224086..0c3e1970 100644 --- a/extlib/benz/include/picrin/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -9,12 +9,12 @@ extern "C" { #endif -#include "picrin/khash.h" -#include "picrin/file.h" +#include "picrin/private/khash.h" +#include "picrin/private/file.h" -#include "picrin/irep.h" -#include "picrin/read.h" -#include "picrin/gc.h" +#include "picrin/private/irep.h" +#include "picrin/private/read.h" +#include "picrin/private/gc.h" struct pic_lib { struct pic_string *name; diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 2fa429a7..901daf1b 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -91,4 +91,374 @@ # define GC_DEBUG_DETAIL 0 #endif -#include "picrin/compat.h" +/* check compatibility */ + +#if __STDC_VERSION__ >= 199901L +# include +#else +# define bool char +# define true 1 +# define false 0 +#endif + +#if __STDC_VERSION__ >= 199901L +# include +#elif ! defined(offsetof) +# define offsetof(s,m) ((size_t)&(((s *)NULL)->m)) +#endif + +#if __STDC_VERSION__ >= 199901L +# include +#else +# if INT_MAX > 2147483640L /* borrowed from luaconf.h */ +typedef int int32_t; +typedef unsigned int uint32_t; +# else +typedef long int32_t; +typedef unsigned long uint32_t; +# endif +#endif + +#if __STDC_VERSION__ >= 201112L +# include +# define PIC_NORETURN noreturn +#elif __GNUC__ || __clang__ +# define PIC_NORETURN __attribute__((noreturn)) +#else +# define PIC_NORETURN +#endif + +#if __STDC_VERSION__ >= 199901L +# define PIC_INLINE static inline +#elif __GNUC__ || __clang__ +# define PIC_INLINE static __inline__ +#else +# define PIC_INLINE static +#endif + +#define PIC_FALLTHROUGH ((void)0) + +#if __GNUC__ || __clang__ +# define PIC_UNUSED(v) __attribute__((unused)) v +#else +# define PIC_UNUSED(v) v +#endif + +#define PIC_GENSYM2_(x,y) PIC_G##x##_##y##_ +#define PIC_GENSYM1_(x,y) PIC_GENSYM2_(x,y) +#if defined(__COUNTER__) +# define PIC_GENSYM(x) PIC_GENSYM1_(__COUNTER__,x) +#else +# define PIC_GENSYM(x) PIC_GENSYM1_(__LINE__,x) +#endif + +#if __GNUC__ +# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#endif +#if GCC_VERSION >= 40500 || __clang__ +# define PIC_UNREACHABLE() (__builtin_unreachable()) +#else +# define PIC_UNREACHABLE() (assert(false)) +#endif +#if __GNUC__ +# undef GCC_VERSION +#endif + +#define PIC_SWAP(type,a,b) \ + PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b) +#define PIC_SWAP_HELPER_(type,tmp,a,b) \ + do { \ + type tmp = (a); \ + (a) = (b); \ + (b) = tmp; \ + } while (0) + + +#if PIC_ENABLE_LIBC + +#include +#include +#include +#include + +#else + +# define assert(v) (void)0 + +PIC_INLINE int +isspace(int c) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f' || c == '\n'; +} + +PIC_INLINE int +tolower(int c) +{ + return ('A' <= c && c <= 'Z') ? c - 'A' + 'a' : c; +} + +PIC_INLINE int +isdigit(int c) +{ + return '0' <= c && c <= '9'; +} + +PIC_INLINE char * +strchr(const char *s, int c) +{ + do { + if (*s == c) + return (char *)s; + } while (*s++ != '\0'); + return NULL; +} + +PIC_INLINE size_t +strlen(const char *s) +{ + size_t l = 0; + + while (*s++) { + l++; + } + return l; +} + +PIC_INLINE int +strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s1 == *s2) { + s1++; + s2++; + } + return (unsigned)*s1 - (unsigned)*s2; +} + +PIC_INLINE long +strtol(const char *nptr, char **endptr, int base) +{ + long l = 0; + char c; + int n; + + while (1) { + c = *nptr; + if ('0' <= c && c <= '9') + n = c - '0'; + else if ('a' <= c && c <= 'z') + n = c - 'a' + 10; + else if ('A' <= c && c <= 'Z') + n = c - 'A' + 10; + else + goto exit; + + if (base <= n) + goto exit; + + l = l * base + n; + nptr++; + } + exit: + if (endptr) + *endptr = (char *)nptr; + return l; +} + +PIC_INLINE void * +memset(void *s, int n, size_t c) +{ + char *p = s; + + while (c-- > 0) { + *p++ = n; + } + return s; +} + +PIC_INLINE void * +memcpy(void *dst, const void *src, size_t n) +{ + const char *s = src; + char *d = dst; + + while (n-- > 0) { + *d++ = *s++; + } + return d; +} + +PIC_INLINE void * +memmove(void *dst, const void *src, size_t n) +{ + const char *s = src; + char *d = dst; + + if (d <= s || d >= s + n) { + memcpy(dst, src, n); + } else { + s += n; + d += n; + while (n-- > 0) { + *--d = *--s; + } + } + return d; +} + +PIC_INLINE int +memcmp(const void *b1, const void *b2, size_t n) +{ + const char *s1 = b1, *s2 = b2; + + while (*s1 == *s2 && n-- > 0) { + s1++; + s2++; + } + return (unsigned)*s1 - (unsigned)*s2; +} + +PIC_INLINE char * +strcpy(char *dst, const char *src) +{ + char *d = dst; + + while ((*dst++ = *src++) != 0); + + return d; +} + +PIC_INLINE double +atof(const char *nptr) +{ + int c; + double f, g, h; + int exp, s, i, e; + unsigned u; + + /* note that picrin_read always assures that *nptr is a digit, never a '+' or '-' */ + /* in other words, the result of atof will always be positive */ + + /* mantissa */ + /* pre '.' */ + u = *nptr++ - '0'; + while (isdigit(c = *nptr)) { + u = u * 10 + (*nptr++ - '0'); + } + if (c == '.') { + nptr++; + /* after '.' */ + g = 0, e = 0; + while (isdigit(c = *nptr)) { + g = g * 10 + (*nptr++ - '0'); + e++; + } + h = 1.0; + while (e-- > 0) { + h /= 10; + } + f = u + g * h; + } + else { + f = u; + } + /* suffix, i.e., exponent */ + s = 0; + exp = 0; + c = *nptr; + + if (c == 'e' && c == 'E') { + nptr++; + switch ((c = *nptr++)) { + case '-': + s = 1; + case '+': + c = *nptr++; + default: + exp = c - '0'; + while (isdigit(c = *nptr)) { + exp = exp * 10 + (*nptr++ - '0'); + } + } + } + e = 10; + for (i = 0; exp; ++i) { + if ((exp & 1) != 0) { + f = s ? f / e : (f * e); + } + e *= e; + exp >>= 1; + } + return f; +} + +#endif + +#if PIC_ENABLE_STDIO +# include + +PIC_INLINE void +pic_dtoa(double dval, char *buf) +{ + sprintf(buf, "%g", dval); +} + +#else + +PIC_INLINE void +pic_dtoa(double dval, char *buf) +{ +# define fabs(x) ((x) >= 0 ? (x) : -(x)) + long lval, tlval; + int ival; + int scnt, ecnt, cnt = 0; + if (dval < 0) { + dval = -dval; + buf[cnt++] = '-'; + } + lval = tlval = (long)dval; + scnt = cnt; + do { + buf[cnt++] = '0' + (tlval % 10); + } while ((tlval /= 10) != 0); + ecnt = cnt; + while (scnt < ecnt) { + char c = buf[scnt]; + buf[scnt++] = buf[--ecnt]; + buf[ecnt] = c; + } + buf[cnt++] = '.'; + dval -= lval; + if ((ival = fabs(dval) * 1e4 + 0.5) == 0) { + buf[cnt++] = '0'; + buf[cnt++] = '0'; + buf[cnt++] = '0'; + buf[cnt++] = '0'; + } else { + if (ival < 1000) buf[cnt++] = '0'; + if (ival < 100) buf[cnt++] = '0'; + if (ival < 10) buf[cnt++] = '0'; + scnt = cnt; + do { + buf[cnt++] = '0' + (ival % 10); + } while ((ival /= 10) != 0); + ecnt = cnt; + while (scnt < ecnt) { + char c = buf[scnt]; + buf[scnt++] = buf[--ecnt]; + buf[ecnt] = c; + } + } + buf[cnt] = 0; +} + +#endif + +#ifndef PIC_DOUBLE_TO_CSTRING +#define PIC_DOUBLE_TO_CSTRING pic_dtoa +#endif +void PIC_DOUBLE_TO_CSTRING(double, char *); + +#ifndef PIC_CSTRING_TO_DOUBLE +#define PIC_CSTRING_TO_DOUBLE atof +#endif +double PIC_CSTRING_TO_DOUBLE(const char *); diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 7bcfbe50..f44505c9 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" KHASH_DEFINE(ltable, const char *, struct pic_lib, kh_str_hash_func, kh_str_cmp_func) diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 58f87f98..8a176081 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal) diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 9a418489..4fcfab40 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -4,7 +4,7 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" +#include "picrin/private/object.h" pic_value pic_cons(pic_state *pic, pic_value car, pic_value cdr) diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 3b4decb6..0f102ff4 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/file.h" +#include "picrin/private/object.h" +#include "picrin/private/file.h" #undef EOF #define EOF (-1) diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 9b6e0010..bc4863eb 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -4,9 +4,9 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/opcode.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/opcode.h" +#include "picrin/private/state.h" #define MIN(x,y) ((x) < (y) ? (x) : (y)) diff --git a/extlib/benz/read.c b/extlib/benz/read.c index baa13123..573d8b4e 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" #undef EOF #define EOF (-1) diff --git a/extlib/benz/record.c b/extlib/benz/record.c index dee14562..9d764b99 100644 --- a/extlib/benz/record.c +++ b/extlib/benz/record.c @@ -3,7 +3,7 @@ */ #include "picrin.h" -#include "picrin/object.h" +#include "picrin/private/object.h" pic_value pic_make_rec(pic_state *pic, pic_value type, pic_value datum) diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 9557963a..8ddd9dec 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" static void pic_init_features(pic_state *pic) diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 431d2fd2..54b9c41c 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -4,7 +4,7 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" +#include "picrin/private/object.h" struct pic_chunk { char *str; diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 0fa0ab63..81f02925 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" #define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0) diff --git a/extlib/benz/value.c b/extlib/benz/value.c index 1a737256..db8ccdf7 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -3,7 +3,7 @@ */ #include "picrin.h" -#include "picrin/object.h" +#include "picrin/private/object.h" #if PIC_NAN_BOXING diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 4eb048e0..8f4b2ef7 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" static pic_value var_get(pic_state *pic, pic_value var) diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 402f36c8..aa254917 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -4,7 +4,7 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" +#include "picrin/private/object.h" pic_value pic_make_vec(pic_state *pic, int len, pic_value *argv) diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 1ba978b1..2a294e4a 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -3,7 +3,7 @@ */ #include "picrin.h" -#include "picrin/object.h" +#include "picrin/private/object.h" KHASH_DEFINE(weak, struct pic_object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index c3b68ba5..810f1b12 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -4,8 +4,8 @@ #include "picrin.h" #include "picrin/extra.h" -#include "picrin/object.h" -#include "picrin/state.h" +#include "picrin/private/object.h" +#include "picrin/private/state.h" KHASH_DECLARE(l, void *, int) KHASH_DECLARE(v, void *, int) From 66717f2b43ab52b4b4944ae0b4e038c20590331b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 01:47:08 +0900 Subject: [PATCH 084/119] fix offsetof --- extlib/benz/include/picrin/setup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 901daf1b..810fd338 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -104,7 +104,7 @@ #if __STDC_VERSION__ >= 199901L # include #elif ! defined(offsetof) -# define offsetof(s,m) ((size_t)&(((s *)NULL)->m)) +# define offsetof(s,m) ((size_t)(&(((s *)0)->m) - 0)) #endif #if __STDC_VERSION__ >= 199901L From 864a17d0be7e98c4d7da82e7185f1ae364546338 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 01:52:34 +0900 Subject: [PATCH 085/119] khiter_t -> int --- extlib/benz/dict.c | 6 +-- extlib/benz/gc.c | 8 ++-- extlib/benz/include/picrin/private/khash.h | 43 ++++++++++------------ extlib/benz/lib.c | 4 +- extlib/benz/macro.c | 4 +- extlib/benz/read.c | 4 +- extlib/benz/symbol.c | 2 +- extlib/benz/weak.c | 6 +-- extlib/benz/write.c | 6 +-- 9 files changed, 39 insertions(+), 44 deletions(-) diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 2d468c09..164513e7 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -22,7 +22,7 @@ pic_value pic_dict_ref(pic_state *pic, pic_value dict, pic_value key) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; - khiter_t it; + int it; it = kh_get(dict, h, pic_sym_ptr(pic, key)); if (it == kh_end(h)) { @@ -36,7 +36,7 @@ pic_dict_set(pic_state *pic, pic_value dict, pic_value key, pic_value val) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int ret; - khiter_t it; + int it; it = kh_put(dict, h, pic_sym_ptr(pic, key), &ret); kh_val(h, it) = val; @@ -60,7 +60,7 @@ void pic_dict_del(pic_state *pic, pic_value dict, pic_value key) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; - khiter_t it; + int it; it = kh_get(dict, h, pic_sym_ptr(pic, key)); if (it == kh_end(h)) { diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 348f2ee9..4ab029ad 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -344,7 +344,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } case PIC_TYPE_ENV: { khash_t(env) *h = &obj->u.env.map; - khiter_t it; + int it; for (it = kh_begin(h); it != kh_end(h); ++it) { if (kh_exist(h, it)) { @@ -425,7 +425,7 @@ gc_mark_phase(pic_state *pic) pic_callinfo *ci; struct pic_proc **xhandler; struct pic_list *list; - khiter_t it; + int it; size_t j; assert(pic->heap->weaks == NULL); @@ -503,7 +503,7 @@ gc_mark_phase(pic_state *pic) do { struct pic_object *key; pic_value val; - khiter_t it; + int it; khash_t(weak) *h; struct pic_weak *weak; @@ -644,7 +644,7 @@ static void gc_sweep_phase(pic_state *pic) { struct heap_page *page; - khiter_t it; + int it; khash_t(weak) *h; khash_t(oblist) *s = &pic->oblist; pic_sym *sym; diff --git a/extlib/benz/include/picrin/private/khash.h b/extlib/benz/include/picrin/private/khash.h index a75a5cec..83c62860 100644 --- a/extlib/benz/include/picrin/private/khash.h +++ b/extlib/benz/include/picrin/private/khash.h @@ -24,13 +24,8 @@ SOFTWARE. */ -#ifndef AC_KHASH_H -#define AC_KHASH_H - -#include - -typedef int khint_t; -typedef khint_t khiter_t; +#ifndef PICRIN_KHASH_H +#define PICRIN_KHASH_H #define ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2) #define ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1) @@ -48,18 +43,18 @@ typedef khint_t khiter_t; #define KHASH_DECLARE(name, khkey_t, khval_t) \ typedef struct { \ - khint_t n_buckets, size, n_occupied, upper_bound; \ + int n_buckets, size, n_occupied, upper_bound; \ int *flags; \ khkey_t *keys; \ khval_t *vals; \ } kh_##name##_t; \ void kh_init_##name(kh_##name##_t *h); \ - void kh_destroy_##name(pic_state *, kh_##name##_t *h); \ + void kh_destroy_##name(pic_state *, kh_##name##_t *h); \ void kh_clear_##name(kh_##name##_t *h); \ - khint_t kh_get_##name(pic_state *, const kh_##name##_t *h, khkey_t key); \ - void kh_resize_##name(pic_state *, kh_##name##_t *h, khint_t new_n_buckets); \ - khint_t kh_put_##name(pic_state *, kh_##name##_t *h, khkey_t key, int *ret); \ - void kh_del_##name(kh_##name##_t *h, khint_t x); + int kh_get_##name(pic_state *, const kh_##name##_t *h, khkey_t key); \ + void kh_resize_##name(pic_state *, kh_##name##_t *h, int new_n_buckets); \ + int kh_put_##name(pic_state *, kh_##name##_t *h, khkey_t key, int *ret); \ + void kh_del_##name(kh_##name##_t *h, int x); #define KHASH_DEFINE(name, khkey_t, khval_t, hash_func, hash_equal) \ KHASH_DEFINE2(name, khkey_t, khval_t, 1, hash_func, hash_equal) @@ -80,11 +75,11 @@ typedef khint_t khiter_t; h->size = h->n_occupied = 0; \ } \ } \ - khint_t kh_get_##name(pic_state *pic, const kh_##name##_t *h, khkey_t key) \ + int kh_get_##name(pic_state *pic, const kh_##name##_t *h, khkey_t key) \ { \ (void)pic; \ if (h->n_buckets) { \ - khint_t k, i, last, mask, step = 0; \ + int k, i, last, mask, step = 0; \ mask = h->n_buckets - 1; \ k = hash_func(key); i = k & mask; \ last = i; \ @@ -95,10 +90,10 @@ typedef khint_t khiter_t; return ac_iseither(h->flags, i)? h->n_buckets : i; \ } else return 0; \ } \ - void kh_resize_##name(pic_state *pic, kh_##name##_t *h, khint_t new_n_buckets) \ + void kh_resize_##name(pic_state *pic, kh_##name##_t *h, int new_n_buckets) \ { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \ int *new_flags = 0; \ - khint_t j = 1; \ + int j = 1; \ { \ ac_roundup32(new_n_buckets); \ if (new_n_buckets < 4) new_n_buckets = 4; \ @@ -119,12 +114,12 @@ typedef khint_t khiter_t; if (ac_iseither(h->flags, j) == 0) { \ khkey_t key = h->keys[j]; \ khval_t val; \ - khint_t new_mask; \ + int new_mask; \ new_mask = new_n_buckets - 1; \ if (kh_is_map) val = h->vals[j]; \ ac_set_isdel_true(h->flags, j); \ while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \ - khint_t k, i, step = 0; \ + int k, i, step = 0; \ k = hash_func(key); \ i = k & new_mask; \ while (!ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \ @@ -152,9 +147,9 @@ typedef khint_t khiter_t; h->upper_bound = ac_hash_upper(h->n_buckets); \ } \ } \ - khint_t kh_put_##name(pic_state *pic, kh_##name##_t *h, khkey_t key, int *ret) \ + int kh_put_##name(pic_state *pic, kh_##name##_t *h, khkey_t key, int *ret) \ { \ - khint_t x; \ + int x; \ if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \ if (h->n_buckets > (h->size<<1)) { \ kh_resize_##name(pic, h, h->n_buckets - 1); /* clear "deleted" elements */ \ @@ -163,7 +158,7 @@ typedef khint_t khiter_t; } \ } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \ { \ - khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \ + int k, i, site, last, mask = h->n_buckets - 1, step = 0; \ x = site = h->n_buckets; k = hash_func(key); i = k & mask; \ if (ac_isempty(h->flags, i)) x = i; /* for speed up */ \ else { \ @@ -192,7 +187,7 @@ typedef khint_t khiter_t; } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \ return x; \ } \ - void kh_del_##name(kh_##name##_t *h, khint_t x) \ + void kh_del_##name(kh_##name##_t *h, int x) \ { \ if (x != h->n_buckets && !ac_iseither(h->flags, x)) { \ ac_set_isdel_true(h->flags, x); \ @@ -231,7 +226,7 @@ PIC_INLINE int kh_str_hash_func(const char *s) { #define kh_key(h, x) ((h)->keys[x]) #define kh_val(h, x) ((h)->vals[x]) #define kh_value(h, x) ((h)->vals[x]) -#define kh_begin(h) (khint_t)(0) +#define kh_begin(h) (0) #define kh_end(h) ((h)->n_buckets) #define kh_size(h) ((h)->size) #define kh_n_buckets(h) ((h)->n_buckets) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index f44505c9..00ef206c 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -13,7 +13,7 @@ static struct pic_lib * get_library_opt(pic_state *pic, const char *lib) { khash_t(ltable) *h = &pic->ltable; - khiter_t it; + int it; it = kh_get(ltable, h, lib); if (it == kh_end(h)) { @@ -61,7 +61,7 @@ pic_make_library(pic_state *pic, const char *lib) khash_t(ltable) *h = &pic->ltable; const char *old_lib; pic_value name, env, exports; - khiter_t it; + int it; int ret; if (pic->lib) { diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 8a176081..0de7e6b5 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -43,7 +43,7 @@ pic_add_identifier(pic_state *pic, pic_value id, pic_value env) pic_value pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) { - khiter_t it; + int it; int ret; it = kh_put(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id), &ret); @@ -55,7 +55,7 @@ pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) static bool search_scope(pic_state *pic, pic_value id, pic_value env, pic_value *uid) { - khiter_t it; + int it; it = kh_get(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id)); if (it == kh_end(&pic_env_ptr(pic, env)->map)) { diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 573d8b4e..157ef7b2 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -611,7 +611,7 @@ read_label_set(pic_state *pic, xFILE *file, int i) khash_t(read) *h = &pic->reader.labels; pic_value val; int c, ret; - khiter_t it; + int it; it = kh_put(read, h, i, &ret); @@ -665,7 +665,7 @@ static pic_value read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i) { khash_t(read) *h = &pic->reader.labels; - khiter_t it; + int it; it = kh_get(read, h, i); if (it == kh_end(h)) { diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 81f02925..795ce367 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -17,7 +17,7 @@ pic_intern(pic_state *pic, pic_value str) { khash_t(oblist) *h = &pic->oblist; pic_sym *sym; - khiter_t it; + int it; int ret; it = kh_put(oblist, h, pic_str_ptr(pic, str), &ret); diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 2a294e4a..6ab88f89 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -23,7 +23,7 @@ pic_value pic_weak_ref(pic_state *pic, pic_value weak, pic_value key) { khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; - khiter_t it; + int it; it = kh_get(weak, h, pic_obj_ptr(key)); if (it == kh_end(h)) { @@ -37,7 +37,7 @@ pic_weak_set(pic_state *pic, pic_value weak, pic_value key, pic_value val) { khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; int ret; - khiter_t it; + int it; it = kh_put(weak, h, pic_obj_ptr(key), &ret); kh_val(h, it) = val; @@ -55,7 +55,7 @@ void pic_weak_del(pic_state *pic, pic_value weak, pic_value key) { khash_t(weak) *h = &pic_weak_ptr(pic, weak)->hash; - khiter_t it; + int it; it = kh_get(weak, h, pic_obj_ptr(key)); if (it == kh_end(h)) { diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 810f1b12..382cff61 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -129,7 +129,7 @@ write_pair_help(struct writer_control *p, pic_value pair) pic_state *pic = p->pic; khash_t(l) *lh = &p->labels; khash_t(v) *vh = &p->visited; - khiter_t it; + int it; int ret; write_core(p, pic_car(pic, pair)); @@ -265,7 +265,7 @@ write_core(struct writer_control *p, pic_value obj) khash_t(l) *lh = &p->labels; khash_t(v) *vh = &p->visited; xFILE *file = p->file; - khiter_t it; + int it; int ret; /* shared objects */ @@ -351,7 +351,7 @@ traverse(struct writer_control *p, pic_value obj) case PIC_TYPE_VECTOR: case PIC_TYPE_DICT: { khash_t(l) *h = &p->labels; - khiter_t it; + int it; int ret; it = kh_put(l, h, pic_obj_ptr(obj), &ret); From f89a55c08211efe58937e5793e1480d73e53d06b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 04:34:26 +0900 Subject: [PATCH 086/119] remove pre-interned symbols --- extlib/benz/eval.c | 104 ++++++++++----------- extlib/benz/gc.c | 13 +-- extlib/benz/include/picrin/private/gc.h | 2 - extlib/benz/include/picrin/private/state.h | 8 -- extlib/benz/lib.c | 10 +- extlib/benz/macro.c | 17 ++-- extlib/benz/read.c | 20 ++-- extlib/benz/state.c | 53 +---------- extlib/benz/symbol.c | 2 +- extlib/benz/write.c | 19 ++-- 10 files changed, 95 insertions(+), 153 deletions(-) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index c0aa5548..1f3ac784 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -8,6 +8,9 @@ #include "picrin/private/opcode.h" #include "picrin/private/state.h" +#define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) +#define S(lit) (pic_intern_lit(pic, lit)) + static pic_value optimize_beta(pic_state *pic, pic_value expr) { @@ -23,10 +26,10 @@ optimize_beta(pic_state *pic, pic_value expr) if (pic_sym_p(pic, pic_list_ref(pic, expr, 0))) { pic_value sym = pic_list_ref(pic, expr, 0); - if (pic_eq_p(pic, sym, pic->sQUOTE)) { + if (EQ(sym, "quote")) { return expr; - } else if (pic_eq_p(pic, sym, pic->sLAMBDA)) { - return pic_list(pic, 3, pic->sLAMBDA, pic_list_ref(pic, expr, 1), optimize_beta(pic, pic_list_ref(pic, expr, 2))); + } else if (EQ(sym, "lambda")) { + return pic_list(pic, 3, S("lambda"), pic_list_ref(pic, expr, 1), optimize_beta(pic, pic_list_ref(pic, expr, 2))); } } @@ -40,7 +43,7 @@ optimize_beta(pic_state *pic, pic_value expr) pic_protect(pic, expr); functor = pic_list_ref(pic, expr, 0); - if (pic_pair_p(pic, functor) && pic_eq_p(pic, pic_car(pic, functor), pic->sLAMBDA)) { + if (pic_pair_p(pic, functor) && EQ(pic_car(pic, functor), "lambda")) { formals = pic_list_ref(pic, functor, 1); if (! pic_list_p(pic, formals)) goto exit; /* TODO: support ((lambda args x) 1 2) */ @@ -49,12 +52,12 @@ optimize_beta(pic_state *pic, pic_value expr) goto exit; defs = pic_nil_value(pic); pic_for_each (val, args, it) { - pic_push(pic, pic_list(pic, 3, pic->sDEFINE, pic_car(pic, formals), val), defs); + pic_push(pic, pic_list(pic, 3, S("define"), pic_car(pic, formals), val), defs); formals = pic_cdr(pic, formals); } expr = pic_list_ref(pic, functor, 2); pic_for_each (val, defs, it) { - expr = pic_list(pic, 3, pic->sBEGIN, val, expr); + expr = pic_list(pic, 3, S("begin"), val, expr); } } exit: @@ -159,11 +162,6 @@ define_var(pic_state *pic, analyze_scope *scope, pic_value sym) static pic_value analyze(pic_state *, analyze_scope *, pic_value); static pic_value analyze_lambda(pic_state *, analyze_scope *, pic_value); -#define GREF pic_intern_lit(pic, "gref") -#define LREF pic_intern_lit(pic, "lref") -#define CREF pic_intern_lit(pic, "cref") -#define CALL pic_intern_lit(pic, "call") - static pic_value analyze_var(pic_state *pic, analyze_scope *scope, pic_value sym) { @@ -172,11 +170,11 @@ analyze_var(pic_state *pic, analyze_scope *scope, pic_value sym) depth = find_var(pic, scope, sym); if (depth == scope->depth) { - return pic_list(pic, 2, GREF, sym); + return pic_list(pic, 2, S("gref"), sym); } else if (depth == 0) { - return pic_list(pic, 2, LREF, sym); + return pic_list(pic, 2, S("lref"), sym); } else { - return pic_list(pic, 3, CREF, pic_int_value(pic, depth), sym); + return pic_list(pic, 3, S("cref"), pic_int_value(pic, depth), sym); } } @@ -255,7 +253,7 @@ analyze_lambda(pic_state *pic, analyze_scope *up, pic_value form) analyzer_scope_destroy(pic, scope); - return pic_list(pic, 6, pic->sLAMBDA, rest, args, locals, captures, body); + return pic_list(pic, 6, S("lambda"), rest, args, locals, captures, body); } static pic_value @@ -281,7 +279,7 @@ analyze_define(pic_state *pic, analyze_scope *scope, pic_value obj) static pic_value analyze_call(pic_state *pic, analyze_scope *scope, pic_value obj) { - return pic_cons(pic, CALL, analyze_list(pic, scope, obj)); + return pic_cons(pic, S("call"), analyze_list(pic, scope, obj)); } static pic_value @@ -302,16 +300,16 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) if (pic_sym_p(pic, proc)) { pic_value sym = proc; - if (pic_eq_p(pic, sym, pic->sDEFINE)) { + if (EQ(sym, "define")) { return analyze_define(pic, scope, obj); } - else if (pic_eq_p(pic, sym, pic->sLAMBDA)) { + else if (EQ(sym, "lambda")) { return analyze_defer(pic, scope, obj); } - else if (pic_eq_p(pic, sym, pic->sQUOTE)) { + else if (EQ(sym, "quote")) { return obj; } - else if (pic_eq_p(pic, sym, pic->sBEGIN) || pic_eq_p(pic, sym, pic->sSETBANG) || pic_eq_p(pic, sym, pic->sIF)) { + else if (EQ(sym, "begin") || EQ(sym, "set!") || EQ(sym, "if")) { return pic_cons(pic, pic_car(pic, obj), analyze_list(pic, scope, pic_cdr(pic, obj))); } } @@ -319,7 +317,7 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) return analyze_call(pic, scope, obj); } default: - return pic_list(pic, 2, pic->sQUOTE, obj); + return pic_list(pic, 2, S("quote"), obj); } } @@ -547,14 +545,14 @@ codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) pic_value sym; sym = pic_car(pic, obj); - if (pic_eq_p(pic, sym, GREF)) { + if (EQ(sym, "gref")) { pic_value name; name = pic_list_ref(pic, obj, 1); emit_i(pic, cxt, OP_GREF, index_global(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } - else if (pic_eq_p(pic, sym, CREF)) { + else if (EQ(sym, "cref")) { pic_value name; int depth; @@ -563,7 +561,7 @@ codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) emit_r(pic, cxt, OP_CREF, depth, index_capture(pic, cxt, name, depth)); emit_ret(pic, cxt, tailpos); } - else if (pic_eq_p(pic, sym, LREF)) { + else if (EQ(sym, "lref")) { pic_value name; int i; @@ -589,14 +587,14 @@ codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) var = pic_list_ref(pic, obj, 1); type = pic_list_ref(pic, var, 0); - if (pic_eq_p(pic, type, GREF)) { + if (EQ(type, "gref")) { pic_value name; name = pic_list_ref(pic, var, 1); emit_i(pic, cxt, OP_GSET, index_global(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } - else if (pic_eq_p(pic, type, CREF)) { + else if (EQ(type, "cref")) { pic_value name; int depth; @@ -605,7 +603,7 @@ codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) emit_r(pic, cxt, OP_CSET, depth, index_capture(pic, cxt, name, depth)); emit_ret(pic, cxt, tailpos); } - else if (pic_eq_p(pic, type, LREF)) { + else if (EQ(type, "lref")) { pic_value name; int i; @@ -730,8 +728,8 @@ codegen_quote(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) emit_ret(pic, cxt, tailpos); } -#define VM(uid, op) \ - if (pic_eq_p(pic, sym, uid)) { \ +#define VM(name, op) \ + if (EQ(sym, name)) { \ emit_i(pic, cxt, op, len - 1); \ emit_ret(pic, cxt, tailpos); \ return; \ @@ -748,27 +746,27 @@ codegen_call(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) } functor = pic_list_ref(pic, obj, 1); - if (pic_eq_p(pic, pic_list_ref(pic, functor, 0), GREF)) { + if (EQ(pic_list_ref(pic, functor, 0), "gref")) { pic_value sym; sym = pic_list_ref(pic, functor, 1); - VM(pic->sCONS, OP_CONS) - VM(pic->sCAR, OP_CAR) - VM(pic->sCDR, OP_CDR) - VM(pic->sNILP, OP_NILP) - VM(pic->sSYMBOLP, OP_SYMBOLP) - VM(pic->sPAIRP, OP_PAIRP) - VM(pic->sNOT, OP_NOT) - VM(pic->sEQ, OP_EQ) - VM(pic->sLT, OP_LT) - VM(pic->sLE, OP_LE) - VM(pic->sGT, OP_GT) - VM(pic->sGE, OP_GE) - VM(pic->sADD, OP_ADD) - VM(pic->sSUB, OP_SUB) - VM(pic->sMUL, OP_MUL) - VM(pic->sDIV, OP_DIV) + VM("cons", OP_CONS) + VM("car", OP_CAR) + VM("cdr", OP_CDR) + VM("null?", OP_NILP) + VM("symbol?", OP_SYMBOLP) + VM("pair?", OP_PAIRP) + VM("not", OP_NOT) + VM("=", OP_EQ) + VM("<", OP_LT) + VM("<=", OP_LE) + VM(">", OP_GT) + VM(">=", OP_GE) + VM("+", OP_ADD) + VM("-", OP_SUB) + VM("*", OP_MUL) + VM("/", OP_DIV) } emit_i(pic, cxt, (tailpos ? OP_TAILCALL : OP_CALL), len - 1); @@ -780,25 +778,25 @@ codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) pic_value sym; sym = pic_car(pic, obj); - if (pic_eq_p(pic, sym, GREF) || pic_eq_p(pic, sym, CREF) || pic_eq_p(pic, sym, LREF)) { + if (EQ(sym, "gref") || EQ(sym, "cref") || EQ(sym, "lref")) { codegen_ref(pic, cxt, obj, tailpos); } - else if (pic_eq_p(pic, sym, pic->sSETBANG) || pic_eq_p(pic, sym, pic->sDEFINE)) { + else if (EQ(sym, "set!") || EQ(sym, "define")) { codegen_set(pic, cxt, obj, tailpos); } - else if (pic_eq_p(pic, sym, pic->sLAMBDA)) { + else if (EQ(sym, "lambda")) { codegen_lambda(pic, cxt, obj, tailpos); } - else if (pic_eq_p(pic, sym, pic->sIF)) { + else if (EQ(sym, "if")) { codegen_if(pic, cxt, obj, tailpos); } - else if (pic_eq_p(pic, sym, pic->sBEGIN)) { + else if (EQ(sym, "begin")) { codegen_begin(pic, cxt, obj, tailpos); } - else if (pic_eq_p(pic, sym, pic->sQUOTE)) { + else if (EQ(sym, "quote")) { codegen_quote(pic, cxt, obj, tailpos); } - else if (pic_eq_p(pic, sym, CALL)) { + else if (EQ(sym, "call")) { codegen_call(pic, cxt, obj, tailpos); } else { diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 4ab029ad..8dc5286b 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -416,8 +416,6 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } } -#define M(x) gc_mark(pic, pic->x) - static void gc_mark_phase(pic_state *pic) { @@ -465,15 +463,6 @@ gc_mark_phase(pic_state *pic) } } - /* mark reserved symbols */ - M(sDEFINE); M(sDEFINE_MACRO); M(sLAMBDA); M(sIF); M(sBEGIN); M(sSETBANG); - M(sQUOTE); M(sQUASIQUOTE); M(sUNQUOTE); M(sUNQUOTE_SPLICING); - M(sSYNTAX_QUOTE); M(sSYNTAX_QUASIQUOTE); M(sSYNTAX_UNQUOTE); M(sSYNTAX_UNQUOTE_SPLICING); - M(sDEFINE_LIBRARY); M(sIMPORT); M(sEXPORT); M(sCOND_EXPAND); - - M(sCONS); M(sCAR); M(sCDR); M(sNILP); M(sSYMBOLP); M(sPAIRP); - M(sADD); M(sSUB); M(sMUL); M(sDIV); M(sEQ); M(sLT); M(sLE); M(sGT); M(sGE); M(sNOT); - /* global variables */ gc_mark(pic, pic->globals); @@ -670,7 +659,7 @@ gc_sweep_phase(pic_state *pic) if (! kh_exist(s, it)) continue; sym = kh_val(s, it); - if (sym->gc_mark == WHITE) { + if (sym && sym->gc_mark == WHITE) { kh_del(oblist, s, it); } } diff --git a/extlib/benz/include/picrin/private/gc.h b/extlib/benz/include/picrin/private/gc.h index cc75a127..4e97e4d0 100644 --- a/extlib/benz/include/picrin/private/gc.h +++ b/extlib/benz/include/picrin/private/gc.h @@ -9,8 +9,6 @@ extern "C" { #endif -struct pic_heap; - struct pic_heap *pic_heap_open(pic_state *); void pic_heap_close(pic_state *, struct pic_heap *); diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index 0c3e1970..9c311bfd 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -59,14 +59,6 @@ struct pic_state { struct pic_lib *lib; - pic_value sDEFINE, sDEFINE_MACRO, sLAMBDA, sIF, sBEGIN, sSETBANG; - pic_value sQUOTE, sQUASIQUOTE, sUNQUOTE, sUNQUOTE_SPLICING; - pic_value sSYNTAX_QUOTE, sSYNTAX_QUASIQUOTE; - pic_value sSYNTAX_UNQUOTE, sSYNTAX_UNQUOTE_SPLICING; - pic_value sDEFINE_LIBRARY, sIMPORT, sEXPORT, sCOND_EXPAND; - pic_value sCONS, sCAR, sCDR, sNILP, sSYMBOLP, sPAIRP; - pic_value sADD, sSUB, sMUL, sDIV, sEQ, sLT, sLE, sGT, sGE, sNOT; - pic_value features; khash_t(oblist) oblist; /* string to symbol */ diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 00ef206c..62823155 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -46,11 +46,13 @@ make_library_env(pic_state *pic, pic_value name) e = pic_obj_value(env); +#define REGISTER(name) pic_put_identifier(pic, pic_intern_lit(pic, name), pic_intern_lit(pic, name), e) + /* set up default environment */ - pic_put_identifier(pic, pic->sDEFINE_LIBRARY, pic->sDEFINE_LIBRARY, e); - pic_put_identifier(pic, pic->sIMPORT, pic->sIMPORT, e); - pic_put_identifier(pic, pic->sEXPORT, pic->sEXPORT, e); - pic_put_identifier(pic, pic->sCOND_EXPAND, pic->sCOND_EXPAND, e); + REGISTER("define-library"); + REGISTER("import"); + REGISTER("export"); + REGISTER("cond-expand"); return e; } diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 0de7e6b5..52a2bad2 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -139,6 +139,9 @@ shadow_macro(pic_state *pic, pic_value uid) static pic_value expand(pic_state *, pic_value expr, pic_value env, pic_value deferred); static pic_value expand_lambda(pic_state *, pic_value expr, pic_value env); +#define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) +#define S(lit) (pic_intern_lit(pic, lit)) + static pic_value expand_var(pic_state *pic, pic_value id, pic_value env, pic_value deferred) { @@ -155,7 +158,7 @@ expand_var(pic_state *pic, pic_value id, pic_value env, pic_value deferred) static pic_value expand_quote(pic_state *pic, pic_value expr) { - return pic_cons(pic, pic->sQUOTE, pic_cdr(pic, expr)); + return pic_cons(pic, S("quote"), pic_cdr(pic, expr)); } static pic_value @@ -229,7 +232,7 @@ expand_lambda(pic_state *pic, pic_value expr, pic_value env) expand_deferred(pic, deferred, in); - return pic_list(pic, 3, pic->sLAMBDA, formal, body); + return pic_list(pic, 3, S("lambda"), formal, body); } static pic_value @@ -245,7 +248,7 @@ expand_define(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) } val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred); - return pic_list(pic, 3, pic->sDEFINE, uid, val); + return pic_list(pic, 3, S("define"), uid, val); } static pic_value @@ -289,16 +292,16 @@ expand_node(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) functor = pic_find_identifier(pic, pic_car(pic, expr), env); - if (pic_eq_p(pic, functor, pic->sDEFINE_MACRO)) { + if (EQ(functor, "define-macro")) { return expand_defmacro(pic, expr, env); } - else if (pic_eq_p(pic, functor, pic->sLAMBDA)) { + else if (EQ(functor, "lambda")) { return expand_defer(pic, expr, deferred); } - else if (pic_eq_p(pic, functor, pic->sDEFINE)) { + else if (EQ(functor, "define")) { return expand_define(pic, expr, env, deferred); } - else if (pic_eq_p(pic, functor, pic->sQUOTE)) { + else if (EQ(functor, "quote")) { return expand_quote(pic, expr); } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 157ef7b2..85f4fbaf 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -151,23 +151,25 @@ read_directive(pic_state *pic, xFILE *file, int c) static pic_value read_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic->sQUOTE, read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "quote"), read(pic, file, next(pic, file))); } static pic_value read_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic->sQUASIQUOTE, read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "quasiquote"), read(pic, file, next(pic, file))); } static pic_value read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - pic_value tag = pic->sUNQUOTE; + pic_value tag; if (peek(pic, file) == '@') { - tag = pic->sUNQUOTE_SPLICING; + tag = pic_intern_lit(pic, "unquote-splicing"); next(pic, file); + } else { + tag = pic_intern_lit(pic, "unquote"); } return pic_list(pic, 2, tag, read(pic, file, next(pic, file))); } @@ -175,23 +177,25 @@ read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) static pic_value read_syntax_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic->sSYNTAX_QUOTE, read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "syntax-quote"), read(pic, file, next(pic, file))); } static pic_value read_syntax_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - return pic_list(pic, 2, pic->sSYNTAX_QUASIQUOTE, read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "syntax-quasiquote"), read(pic, file, next(pic, file))); } static pic_value read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) { - pic_value tag = pic->sSYNTAX_UNQUOTE; + pic_value tag; if (peek(pic, file) == '@') { - tag = pic->sSYNTAX_UNQUOTE_SPLICING; + tag = pic_intern_lit(pic, "syntax-unquote-splicing"); next(pic, file); + } else { + tag = pic_intern_lit(pic, "syntax-unquote"); } return pic_list(pic, 2, tag, read(pic, file, next(pic, file))); } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 8ddd9dec..293e13de 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -193,7 +193,6 @@ pic_open(pic_allocf allocf, void *userdata) char t; pic_state *pic; - size_t ai; pic = allocf(userdata, NULL, sizeof(pic_state)); @@ -260,10 +259,10 @@ pic_open(pic_allocf allocf, void *userdata) pic->ucnt = 0; /* global variables */ - pic->globals = pic_make_weak(pic); + pic->globals = pic_invalid_value(pic); /* macros */ - pic->macros = pic_make_weak(pic); + pic->macros = pic_invalid_value(pic); /* features */ pic->features = pic_nil_value(pic); @@ -299,48 +298,6 @@ pic_open(pic_allocf allocf, void *userdata) /* native stack marker */ pic->native_stack_start = &t; - ai = pic_enter(pic); - -#define S(slot,name) pic->slot = pic_intern_lit(pic, name) - - S(sDEFINE, "define"); - S(sDEFINE_MACRO, "define-macro"); - S(sLAMBDA, "lambda"); - S(sIF, "if"); - S(sBEGIN, "begin"); - S(sSETBANG, "set!"); - S(sQUOTE, "quote"); - S(sQUASIQUOTE, "quasiquote"); - S(sUNQUOTE, "unquote"); - S(sUNQUOTE_SPLICING, "unquote-splicing"); - S(sSYNTAX_QUOTE, "syntax-quote"); - S(sSYNTAX_QUASIQUOTE, "syntax-quasiquote"); - S(sSYNTAX_UNQUOTE, "syntax-unquote"); - S(sSYNTAX_UNQUOTE_SPLICING, "syntax-unquote-splicing"); - S(sIMPORT, "import"); - S(sEXPORT, "export"); - S(sDEFINE_LIBRARY, "define-library"); - S(sCOND_EXPAND, "cond-expand"); - - S(sCONS, "cons"); - S(sCAR, "car"); - S(sCDR, "cdr"); - S(sNILP, "null?"); - S(sSYMBOLP, "symbol?"); - S(sPAIRP, "pair?"); - S(sADD, "+"); - S(sSUB, "-"); - S(sMUL, "*"); - S(sDIV, "/"); - S(sEQ, "="); - S(sLT, "<"); - S(sLE, "<="); - S(sGT, ">"); - S(sGE, ">="); - S(sNOT, "not"); - - pic_leave(pic, ai); - /* root tables */ pic->globals = pic_make_weak(pic); pic->macros = pic_make_weak(pic); @@ -355,20 +312,18 @@ pic_open(pic_allocf allocf, void *userdata) pic_reader_init(pic); /* parameter table */ - pic->ptable = pic_cons(pic, pic_make_weak(pic), pic->ptable); + pic->ptable = pic_cons(pic, pic_make_weak(pic), pic_nil_value(pic)); /* standard libraries */ pic_make_library(pic, "picrin.user"); pic_in_library(pic, "picrin.user"); - pic_leave(pic, ai); - /* turn on GC */ pic->gc_enable = true; pic_init_core(pic); - pic_leave(pic, ai); + pic_leave(pic, 0); /* empty arena */ return pic; diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 795ce367..bcd7dac0 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -27,7 +27,7 @@ pic_intern(pic_state *pic, pic_value str) return pic_obj_value(sym); } - kh_val(h, it) = pic_sym_ptr(pic, pic->sQUOTE); /* dummy */ + kh_val(h, it) = NULL; /* dummy */ sym = (pic_sym *)pic_obj_alloc(pic, offsetof(pic_sym, env), PIC_TYPE_SYMBOL); sym->u.str = pic_str_ptr(pic, str); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 382cff61..ffd8f0d5 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -5,7 +5,6 @@ #include "picrin.h" #include "picrin/extra.h" #include "picrin/private/object.h" -#include "picrin/private/state.h" KHASH_DECLARE(l, void *, int) KHASH_DECLARE(v, void *, int) @@ -170,6 +169,8 @@ write_pair_help(struct writer_control *p, pic_value pair) } } +#define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) + static void write_pair(struct writer_control *p, pic_value pair) { @@ -179,42 +180,42 @@ write_pair(struct writer_control *p, pic_value pair) if (pic_pair_p(pic, pic_cdr(pic, pair)) && pic_nil_p(pic, pic_cddr(pic, pair)) && pic_sym_p(pic, pic_car(pic, pair))) { tag = pic_car(pic, pair); - if (pic_eq_p(pic, tag, pic->sQUOTE)) { + if (EQ(tag, "quote")) { xfprintf(pic, file, "'"); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sUNQUOTE)) { + else if (EQ(tag, "unquote")) { xfprintf(pic, file, ","); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sUNQUOTE_SPLICING)) { + else if (EQ(tag, "unquote-splicing")) { xfprintf(pic, file, ",@"); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sQUASIQUOTE)) { + else if (EQ(tag, "quasiquote")) { xfprintf(pic, file, "`"); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sSYNTAX_QUOTE)) { + else if (EQ(tag, "syntax-quote")) { xfprintf(pic, file, "#'"); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sSYNTAX_UNQUOTE)) { + else if (EQ(tag, "syntax-unquote")) { xfprintf(pic, file, "#,"); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sSYNTAX_UNQUOTE_SPLICING)) { + else if (EQ(tag, "syntax-unquote-splicing")) { xfprintf(pic, file, "#,@"); write_core(p, pic_cadr(pic, pair)); return; } - else if (pic_eq_p(pic, tag, pic->sSYNTAX_QUASIQUOTE)) { + else if (EQ(tag, "syntax-quasiquote")) { xfprintf(pic, file, "#`"); write_core(p, pic_cadr(pic, pair)); return; From 07e7785241f81e3dff184827f094b462b42c708a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 05:39:15 +0900 Subject: [PATCH 087/119] insert assertion to pic_foo_ptr --- extlib/benz/include/picrin/private/object.h | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index b3cc4b4a..6f50bdfb 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -140,20 +140,20 @@ struct pic_checkpoint { struct pic_object *pic_obj_ptr(pic_value); -#define pic_id_ptr(pic, o) ((pic_id *)pic_obj_ptr(o)) -#define pic_sym_ptr(pic, o) ((pic_sym *)pic_obj_ptr(o)) -#define pic_str_ptr(pic, o) ((struct pic_string *)pic_obj_ptr(o)) -#define pic_blob_ptr(pic, o) ((struct pic_blob *)pic_obj_ptr(o)) -#define pic_pair_ptr(pic, o) ((struct pic_pair *)pic_obj_ptr(o)) -#define pic_vec_ptr(pic, o) ((struct pic_vector *)pic_obj_ptr(o)) -#define pic_dict_ptr(pic, o) ((struct pic_dict *)pic_obj_ptr(o)) -#define pic_weak_ptr(pic, o) ((struct pic_weak *)pic_obj_ptr(o)) -#define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o)) -#define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o)) -#define pic_env_ptr(pic, o) ((struct pic_env *)pic_obj_ptr(o)) -#define pic_port_ptr(pic, o) ((struct pic_port *)pic_obj_ptr(o)) -#define pic_error_ptr(pic, o) ((struct pic_error *)pic_obj_ptr(o)) -#define pic_rec_ptr(pic, o) ((struct pic_record *)pic_obj_ptr(o)) +#define pic_id_ptr(pic, o) (assert(pic_id_p(pic, o)), (pic_id *)pic_obj_ptr(o)) +#define pic_sym_ptr(pic, o) (assert(pic_sym_p(pic, o)), (pic_sym *)pic_obj_ptr(o)) +#define pic_str_ptr(pic, o) (assert(pic_str_p(pic, o)), (struct pic_string *)pic_obj_ptr(o)) +#define pic_blob_ptr(pic, o) (assert(pic_blob_p(pic, o)), (struct pic_blob *)pic_obj_ptr(o)) +#define pic_pair_ptr(pic, o) (assert(pic_pair_p(pic, o)), (struct pic_pair *)pic_obj_ptr(o)) +#define pic_vec_ptr(pic, o) (assert(pic_vec_p(pic, o)), (struct pic_vector *)pic_obj_ptr(o)) +#define pic_dict_ptr(pic, o) (assert(pic_dict_p(pic, o)), (struct pic_dict *)pic_obj_ptr(o)) +#define pic_weak_ptr(pic, o) (assert(pic_weak_p(pic, o)), (struct pic_weak *)pic_obj_ptr(o)) +#define pic_data_ptr(pic, o) (assert(pic_data_p(pic, o, NULL)), (struct pic_data *)pic_obj_ptr(o)) +#define pic_proc_ptr(pic, o) (assert(pic_proc_p(pic, o)), (struct pic_proc *)pic_obj_ptr(o)) +#define pic_env_ptr(pic, o) (assert(pic_env_p(pic, o)), (struct pic_env *)pic_obj_ptr(o)) +#define pic_port_ptr(pic, o) (assert(pic_port_p(pic, o)), (struct pic_port *)pic_obj_ptr(o)) +#define pic_error_ptr(pic, o) (assert(pic_error_p(pic, o)), (struct pic_error *)pic_obj_ptr(o)) +#define pic_rec_ptr(pic, o) (assert(pic_rec_p(pic, o)), (struct pic_record *)pic_obj_ptr(o)) #define pic_obj_p(pic,v) (pic_type(pic,v) > PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) From f1ef21be236021b357c1ab2cea0ab529b260b5d4 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 16:04:34 +0900 Subject: [PATCH 088/119] simplify writer_control --- extlib/benz/write.c | 108 +++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 61 deletions(-) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index ffd8f0d5..414aa878 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -12,8 +12,6 @@ KHASH_DEFINE2(l, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal) KHASH_DEFINE2(v, void *, int, 0, kh_ptr_hash_func, kh_ptr_hash_equal) struct writer_control { - pic_state *pic; - xFILE *file; int mode; int op; khash_t(l) labels; /* object -> int */ @@ -29,10 +27,8 @@ struct writer_control { #define OP_WRITE_SIMPLE 3 static void -writer_control_init(struct writer_control *p, pic_state *pic, xFILE *file, int mode, int op) +writer_control_init(struct writer_control *p, int mode, int op) { - p->pic = pic; - p->file = file; p->mode = mode; p->op = op; p->cnt = 0; @@ -41,9 +37,8 @@ writer_control_init(struct writer_control *p, pic_state *pic, xFILE *file, int m } static void -writer_control_destroy(struct writer_control *p) +writer_control_destroy(pic_state *pic, struct writer_control *p) { - pic_state *pic = p->pic; kh_destroy(l, &p->labels); kh_destroy(v, &p->visited); } @@ -67,9 +62,11 @@ write_blob(pic_state *pic, pic_value blob, xFILE *file) } static void -write_char(pic_state *pic, char c, xFILE *file, int mode) +write_char(pic_state *pic, pic_value ch, xFILE *file, struct writer_control *p) { - if (mode == DISPLAY_MODE) { + char c = pic_char(pic, ch); + + if (p->mode == DISPLAY_MODE) { xfputc(pic, c, file); return; } @@ -87,12 +84,12 @@ write_char(pic_state *pic, char c, xFILE *file, int mode) } static void -write_str(pic_state *pic, pic_value str, xFILE *file, int mode) +write_str(pic_state *pic, pic_value str, xFILE *file, struct writer_control *p) { int i; const char *cstr = pic_str(pic, str); - if (mode == DISPLAY_MODE) { + if (p->mode == DISPLAY_MODE) { xfprintf(pic, file, "%s", pic_str(pic, str)); return; } @@ -120,18 +117,17 @@ write_float(pic_state *pic, double f, xFILE *file) } } -static void write_core(struct writer_control *p, pic_value); +static void write_core(pic_state *, pic_value, xFILE *, struct writer_control *); static void -write_pair_help(struct writer_control *p, pic_value pair) +write_pair_help(pic_state *pic, pic_value pair, xFILE *file, struct writer_control *p) { - pic_state *pic = p->pic; khash_t(l) *lh = &p->labels; khash_t(v) *vh = &p->visited; int it; int ret; - write_core(p, pic_car(pic, pair)); + write_core(pic, pic_car(pic, pair), file, p); if (pic_nil_p(pic, pic_cdr(pic, pair))) { return; @@ -140,20 +136,20 @@ write_pair_help(struct writer_control *p, pic_value pair) /* shared objects */ if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) { - xfprintf(pic, p->file, " . "); + xfprintf(pic, file, " . "); kh_put(v, vh, pic_obj_ptr(pic_cdr(pic, pair)), &ret); if (ret == 0) { /* if exists */ - xfprintf(pic, p->file, "#%d#", kh_val(lh, it)); + xfprintf(pic, file, "#%d#", kh_val(lh, it)); return; } - xfprintf(pic, p->file, "#%d=", kh_val(lh, it)); + xfprintf(pic, file, "#%d=", kh_val(lh, it)); } else { - xfprintf(pic, p->file, " "); + xfprintf(pic, file, " "); } - write_pair_help(p, pic_cdr(pic, pair)); + write_pair_help(pic, pic_cdr(pic, pair), file, p); if (p->op == OP_WRITE) { if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) { @@ -164,78 +160,74 @@ write_pair_help(struct writer_control *p, pic_value pair) return; } else { - xfprintf(pic, p->file, " . "); - write_core(p, pic_cdr(pic, pair)); + xfprintf(pic, file, " . "); + write_core(pic, pic_cdr(pic, pair), file, p); } } #define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) static void -write_pair(struct writer_control *p, pic_value pair) +write_pair(pic_state *pic, pic_value pair, xFILE *file, struct writer_control *p) { - pic_state *pic = p->pic; - xFILE *file = p->file; pic_value tag; if (pic_pair_p(pic, pic_cdr(pic, pair)) && pic_nil_p(pic, pic_cddr(pic, pair)) && pic_sym_p(pic, pic_car(pic, pair))) { tag = pic_car(pic, pair); if (EQ(tag, "quote")) { xfprintf(pic, file, "'"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "unquote")) { xfprintf(pic, file, ","); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "unquote-splicing")) { xfprintf(pic, file, ",@"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "quasiquote")) { xfprintf(pic, file, "`"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "syntax-quote")) { xfprintf(pic, file, "#'"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "syntax-unquote")) { xfprintf(pic, file, "#,"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "syntax-unquote-splicing")) { xfprintf(pic, file, "#,@"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } else if (EQ(tag, "syntax-quasiquote")) { xfprintf(pic, file, "#`"); - write_core(p, pic_cadr(pic, pair)); + write_core(pic, pic_cadr(pic, pair), file, p); return; } } xfprintf(pic, file, "("); - write_pair_help(p, pair); + write_pair_help(pic, pair, file, p); xfprintf(pic, file, ")"); } static void -write_vec(struct writer_control *p, pic_value vec) +write_vec(pic_state *pic, pic_value vec, xFILE *file, struct writer_control *p) { - pic_state *pic = p->pic; - xFILE *file = p->file; int i, len = pic_vec_len(pic, vec); xfprintf(pic, file, "#("); for (i = 0; i < len; ++i) { - write_core(p, pic_vec_ref(pic, vec, i)); + write_core(pic, pic_vec_ref(pic, vec, i), file, p); if (i + 1 < len) { xfprintf(pic, file, " "); } @@ -244,28 +236,24 @@ write_vec(struct writer_control *p, pic_value vec) } static void -write_dict(struct writer_control *p, pic_value dict) +write_dict(pic_state *pic, pic_value dict, xFILE *file, struct writer_control *p) { - pic_state *pic = p->pic; - xFILE *file = p->file; pic_value key, val; int it = 0; xfprintf(pic, file, "#.(dictionary"); while (pic_dict_next(pic, dict, &it, &key, &val)) { xfprintf(pic, file, " '%s ", pic_str(pic, pic_sym_name(pic, key))); - write_core(p, val); + write_core(pic, val, file, p); } xfprintf(pic, file, ")"); } static void -write_core(struct writer_control *p, pic_value obj) +write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) { - pic_state *pic = p->pic; khash_t(l) *lh = &p->labels; khash_t(v) *vh = &p->visited; - xFILE *file = p->file; int it; int ret; @@ -311,19 +299,19 @@ write_core(struct writer_control *p, pic_value obj) write_blob(pic, obj, file); break; case PIC_TYPE_CHAR: - write_char(pic, pic_char(pic, obj), file, p->mode); + write_char(pic, obj, file, p); break; case PIC_TYPE_STRING: - write_str(pic, obj, file, p->mode); + write_str(pic, obj, file, p); break; case PIC_TYPE_PAIR: - write_pair(p, obj); + write_pair(pic, obj, file, p); break; case PIC_TYPE_VECTOR: - write_vec(p, obj); + write_vec(pic, obj, file, p); break; case PIC_TYPE_DICT: - write_dict(p, obj); + write_dict(pic, obj, file, p); break; default: xfprintf(pic, file, "#<%s %p>", pic_typename(pic, pic_type(pic, obj)), pic_obj_ptr(obj)); @@ -339,10 +327,8 @@ write_core(struct writer_control *p, pic_value obj) } static void -traverse(struct writer_control *p, pic_value obj) +traverse(pic_state *pic, pic_value obj, struct writer_control *p) { - pic_state *pic = p->pic; - if (p->op == OP_WRITE_SIMPLE) { return; } @@ -362,20 +348,20 @@ traverse(struct writer_control *p, pic_value obj) if (pic_pair_p(pic, obj)) { /* pair */ - traverse(p, pic_car(pic, obj)); - traverse(p, pic_cdr(pic, obj)); + traverse(pic, pic_car(pic, obj), p); + traverse(pic, pic_cdr(pic, obj), p); } else if (pic_vec_p(pic, obj)) { /* vector */ int i, len = pic_vec_len(pic, obj); for (i = 0; i < len; ++i) { - traverse(p, pic_vec_ref(pic, obj, i)); + traverse(pic, pic_vec_ref(pic, obj, i), p); } } else { /* dictionary */ int it = 0; pic_value val; while (pic_dict_next(pic, obj, &it, NULL, &val)) { - traverse(p, val); + traverse(pic, val, p); } } @@ -401,13 +387,13 @@ write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op) { struct writer_control p; - writer_control_init(&p, pic, file, mode, op); + writer_control_init(&p, mode, op); - traverse(&p, obj); + traverse(pic, obj, &p); - write_core(&p, obj); + write_core(pic, obj, file, &p); - writer_control_destroy(&p); + writer_control_destroy(pic, &p); } From efa15fd5ce3dc42a5e9ced4e14a3a222959649e4 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 17:35:28 +0900 Subject: [PATCH 089/119] fix write ``` (define a '#0=(1 . #0#)) (write (cons a a)) ``` spits strange display --- extlib/benz/write.c | 184 ++++++++++++++++++++------------------------ 1 file changed, 85 insertions(+), 99 deletions(-) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 414aa878..4b5515a4 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -9,13 +9,13 @@ KHASH_DECLARE(l, void *, int) KHASH_DECLARE(v, void *, int) KHASH_DEFINE2(l, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal) -KHASH_DEFINE2(v, void *, int, 0, kh_ptr_hash_func, kh_ptr_hash_equal) +KHASH_DEFINE2(v, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal) struct writer_control { int mode; int op; khash_t(l) labels; /* object -> int */ - khash_t(v) visited; /* object -> int */ + khash_t(v) visited; /* is object shared? (yes if >0) */ int cnt; }; @@ -43,6 +43,77 @@ writer_control_destroy(pic_state *pic, struct writer_control *p) kh_destroy(v, &p->visited); } +static void +traverse(pic_state *pic, pic_value obj, struct writer_control *p) +{ + if (p->op == OP_WRITE_SIMPLE) { + return; + } + + switch (pic_type(pic, obj)) { + case PIC_TYPE_PAIR: + case PIC_TYPE_VECTOR: + case PIC_TYPE_DICT: { + khash_t(v) *h = &p->visited; + int it; + int ret; + + it = kh_put(v, h, pic_obj_ptr(obj), &ret); + if (ret != 0) { + /* first time */ + kh_val(h, it) = 0; + + if (pic_pair_p(pic, obj)) { + /* pair */ + traverse(pic, pic_car(pic, obj), p); + traverse(pic, pic_cdr(pic, obj), p); + } else if (pic_vec_p(pic, obj)) { + /* vector */ + int i, len = pic_vec_len(pic, obj); + for (i = 0; i < len; ++i) { + traverse(pic, pic_vec_ref(pic, obj, i), p); + } + } else { + /* dictionary */ + int it = 0; + pic_value val; + while (pic_dict_next(pic, obj, &it, NULL, &val)) { + traverse(pic, val, p); + } + } + + if (p->op == OP_WRITE) { + it = kh_get(v, h, pic_obj_ptr(obj)); + if (kh_val(h, it) == 0) { + kh_del(v, h, it); + } + } + } else { + /* second time */ + kh_val(h, it) = 1; + } + break; + } + default: + break; + } +} + +static bool +is_shared_object(pic_state *pic, pic_value obj, struct writer_control *p) { + khash_t(v) *h = &p->visited; + int it; + + if (! pic_obj_p(pic, obj)) { + return false; + } + it = kh_get(v, h, pic_obj_ptr(obj)); + if (it == kh_end(h)) { + return false; + } + return kh_val(h, it) > 0; +} + static void write_blob(pic_state *pic, pic_value blob, xFILE *file) { @@ -122,46 +193,20 @@ static void write_core(pic_state *, pic_value, xFILE *, struct writer_control *) static void write_pair_help(pic_state *pic, pic_value pair, xFILE *file, struct writer_control *p) { - khash_t(l) *lh = &p->labels; - khash_t(v) *vh = &p->visited; - int it; - int ret; + pic_value cdr = pic_cdr(pic, pair); write_core(pic, pic_car(pic, pair), file, p); - if (pic_nil_p(pic, pic_cdr(pic, pair))) { + if (pic_nil_p(pic, cdr)) { return; } - else if (pic_pair_p(pic, pic_cdr(pic, pair))) { - - /* shared objects */ - if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) { - xfprintf(pic, file, " . "); - - kh_put(v, vh, pic_obj_ptr(pic_cdr(pic, pair)), &ret); - if (ret == 0) { /* if exists */ - xfprintf(pic, file, "#%d#", kh_val(lh, it)); - return; - } - xfprintf(pic, file, "#%d=", kh_val(lh, it)); - } - else { - xfprintf(pic, file, " "); - } - - write_pair_help(pic, pic_cdr(pic, pair), file, p); - - if (p->op == OP_WRITE) { - if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) { - it = kh_get(v, vh, pic_obj_ptr(pic_cdr(pic, pair))); - kh_del(v, vh, it); - } - } - return; + else if (pic_pair_p(pic, cdr) && ! is_shared_object(pic, cdr, p)) { + xfprintf(pic, file, " "); + write_pair_help(pic, cdr, file, p); } else { xfprintf(pic, file, " . "); - write_core(pic, pic_cdr(pic, pair), file, p); + write_core(pic, cdr, file, p); } } @@ -253,18 +298,16 @@ static void write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) { khash_t(l) *lh = &p->labels; - khash_t(v) *vh = &p->visited; - int it; - int ret; + int it, ret; /* shared objects */ - if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_obj_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { - kh_put(v, vh, pic_obj_ptr(obj), &ret); + if (is_shared_object(pic, obj, p)) { + it = kh_put(l, lh, pic_obj_ptr(obj), &ret); if (ret == 0) { /* if exists */ xfprintf(pic, file, "#%d#", kh_val(lh, it)); return; } - xfprintf(pic, file, "#%d=", kh_val(lh, it)); + xfprintf(pic, file, "#%d=", (kh_val(lh, it) = p->cnt++)); } switch (pic_type(pic, obj)) { @@ -319,69 +362,12 @@ write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) } if (p->op == OP_WRITE) { - if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_obj_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) { - it = kh_get(v, vh, pic_obj_ptr(obj)); - kh_del(v, vh, it); + if (is_shared_object(pic, obj, p)) { + kh_del(l, lh, kh_get(l, lh, pic_obj_ptr(obj))); } } } -static void -traverse(pic_state *pic, pic_value obj, struct writer_control *p) -{ - if (p->op == OP_WRITE_SIMPLE) { - return; - } - - switch (pic_type(pic, obj)) { - case PIC_TYPE_PAIR: - case PIC_TYPE_VECTOR: - case PIC_TYPE_DICT: { - khash_t(l) *h = &p->labels; - int it; - int ret; - - it = kh_put(l, h, pic_obj_ptr(obj), &ret); - if (ret != 0) { - /* first time */ - kh_val(h, it) = -1; - - if (pic_pair_p(pic, obj)) { - /* pair */ - traverse(pic, pic_car(pic, obj), p); - traverse(pic, pic_cdr(pic, obj), p); - } else if (pic_vec_p(pic, obj)) { - /* vector */ - int i, len = pic_vec_len(pic, obj); - for (i = 0; i < len; ++i) { - traverse(pic, pic_vec_ref(pic, obj, i), p); - } - } else { - /* dictionary */ - int it = 0; - pic_value val; - while (pic_dict_next(pic, obj, &it, NULL, &val)) { - traverse(pic, val, p); - } - } - - if (p->op == OP_WRITE) { - it = kh_get(l, h, pic_obj_ptr(obj)); - if (kh_val(h, it) == -1) { - kh_del(l, h, it); - } - } - } else if (kh_val(h, it) == -1) { - /* second time */ - kh_val(h, it) = p->cnt++; - } - break; - } - default: - break; - } -} - static void write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op) { From 5bd390aa79c8925e745aff0d176eceefe4d89856 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 18:01:41 +0900 Subject: [PATCH 090/119] use weak map to control writer --- extlib/benz/write.c | 61 ++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 4b5515a4..056e0812 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -14,9 +14,9 @@ KHASH_DEFINE2(v, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal) struct writer_control { int mode; int op; - khash_t(l) labels; /* object -> int */ - khash_t(v) visited; /* is object shared? (yes if >0) */ int cnt; + pic_value shared; /* is object shared? (yes if >0) */ + pic_value labels; /* object -> int */ }; #define WRITE_MODE 1 @@ -27,25 +27,20 @@ struct writer_control { #define OP_WRITE_SIMPLE 3 static void -writer_control_init(struct writer_control *p, int mode, int op) +writer_control_init(pic_state *pic, struct writer_control *p, int mode, int op) { p->mode = mode; p->op = op; p->cnt = 0; - kh_init(l, &p->labels); - kh_init(v, &p->visited); -} - -static void -writer_control_destroy(pic_state *pic, struct writer_control *p) -{ - kh_destroy(l, &p->labels); - kh_destroy(v, &p->visited); + p->shared = pic_make_weak(pic); + p->labels = pic_make_weak(pic); } static void traverse(pic_state *pic, pic_value obj, struct writer_control *p) { + pic_value shared = p->shared; + if (p->op == OP_WRITE_SIMPLE) { return; } @@ -54,14 +49,10 @@ traverse(pic_state *pic, pic_value obj, struct writer_control *p) case PIC_TYPE_PAIR: case PIC_TYPE_VECTOR: case PIC_TYPE_DICT: { - khash_t(v) *h = &p->visited; - int it; - int ret; - it = kh_put(v, h, pic_obj_ptr(obj), &ret); - if (ret != 0) { + if (! pic_weak_has(pic, shared, obj)) { /* first time */ - kh_val(h, it) = 0; + pic_weak_set(pic, shared, obj, pic_int_value(pic, 0)); if (pic_pair_p(pic, obj)) { /* pair */ @@ -83,14 +74,13 @@ traverse(pic_state *pic, pic_value obj, struct writer_control *p) } if (p->op == OP_WRITE) { - it = kh_get(v, h, pic_obj_ptr(obj)); - if (kh_val(h, it) == 0) { - kh_del(v, h, it); + if (pic_int(pic, pic_weak_ref(pic, shared, obj)) == 0) { + pic_weak_del(pic, shared, obj); } } } else { /* second time */ - kh_val(h, it) = 1; + pic_weak_set(pic, shared, obj, pic_int_value(pic, 1)); } break; } @@ -101,17 +91,15 @@ traverse(pic_state *pic, pic_value obj, struct writer_control *p) static bool is_shared_object(pic_state *pic, pic_value obj, struct writer_control *p) { - khash_t(v) *h = &p->visited; - int it; + pic_value shared = p->shared; if (! pic_obj_p(pic, obj)) { return false; } - it = kh_get(v, h, pic_obj_ptr(obj)); - if (it == kh_end(h)) { + if (! pic_weak_has(pic, shared, obj)) { return false; } - return kh_val(h, it) > 0; + return pic_int(pic, pic_weak_ref(pic, shared, obj)) > 0; } static void @@ -297,17 +285,18 @@ write_dict(pic_state *pic, pic_value dict, xFILE *file, struct writer_control *p static void write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) { - khash_t(l) *lh = &p->labels; - int it, ret; + pic_value labels = p->labels; + int i; /* shared objects */ if (is_shared_object(pic, obj, p)) { - it = kh_put(l, lh, pic_obj_ptr(obj), &ret); - if (ret == 0) { /* if exists */ - xfprintf(pic, file, "#%d#", kh_val(lh, it)); + if (pic_weak_has(pic, labels, obj)) { + xfprintf(pic, file, "#%d#", pic_int(pic, pic_weak_ref(pic, labels, obj))); return; } - xfprintf(pic, file, "#%d=", (kh_val(lh, it) = p->cnt++)); + i = p->cnt++; + xfprintf(pic, file, "#%d=", i); + pic_weak_set(pic, labels, obj, pic_int_value(pic, i)); } switch (pic_type(pic, obj)) { @@ -363,7 +352,7 @@ write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) if (p->op == OP_WRITE) { if (is_shared_object(pic, obj, p)) { - kh_del(l, lh, kh_get(l, lh, pic_obj_ptr(obj))); + pic_weak_del(pic, labels, obj); } } } @@ -373,13 +362,11 @@ write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op) { struct writer_control p; - writer_control_init(&p, mode, op); + writer_control_init(pic, &p, mode, op); traverse(pic, obj, &p); write_core(pic, obj, file, &p); - - writer_control_destroy(pic, &p); } From 2ee33b5d5751f22f2b8ea2c4172ab9cb21808a5b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 18:28:59 +0900 Subject: [PATCH 091/119] remove read.h --- extlib/benz/include/picrin/private/read.h | 33 --- extlib/benz/include/picrin/private/state.h | 2 - extlib/benz/read.c | 272 +++++++++++---------- extlib/benz/state.c | 6 - extlib/benz/write.c | 10 +- 5 files changed, 150 insertions(+), 173 deletions(-) delete mode 100644 extlib/benz/include/picrin/private/read.h diff --git a/extlib/benz/include/picrin/private/read.h b/extlib/benz/include/picrin/private/read.h deleted file mode 100644 index 4b1e0baa..00000000 --- a/extlib/benz/include/picrin/private/read.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_READ_H -#define PICRIN_READ_H - -#if defined(__cplusplus) -extern "C" { -#endif - -KHASH_DECLARE(read, int, pic_value) - -typedef pic_value (*pic_reader_t)(pic_state *, xFILE *file, int c); - -typedef struct { - enum pic_typecase { - PIC_CASE_DEFAULT, - PIC_CASE_FOLD - } typecase; - khash_t(read) labels; - pic_reader_t table[256]; - pic_reader_t dispatch[256]; -} pic_reader; - -void pic_reader_init(pic_state *); -void pic_reader_destroy(pic_state *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index 9c311bfd..48341157 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -13,7 +13,6 @@ extern "C" { #include "picrin/private/file.h" #include "picrin/private/irep.h" -#include "picrin/private/read.h" #include "picrin/private/gc.h" struct pic_lib { @@ -68,7 +67,6 @@ struct pic_state { khash_t(ltable) ltable; struct pic_list ireps; /* chain */ - pic_reader reader; xFILE files[XOPEN_MAX]; pic_code iseq[2]; /* for pic_apply_trampoline */ diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 85f4fbaf..1241be20 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -5,15 +5,28 @@ #include "picrin.h" #include "picrin/extra.h" #include "picrin/private/object.h" -#include "picrin/private/state.h" #undef EOF #define EOF (-1) +KHASH_DECLARE(read, int, pic_value) KHASH_DEFINE(read, int, pic_value, kh_int_hash_func, kh_int_hash_equal) -static pic_value read(pic_state *pic, xFILE *file, int c); -static pic_value read_nullable(pic_state *pic, xFILE *file, int c); +struct reader_control { + int typecase; + khash_t(read) labels; +}; + +#define CASE_DEFAULT 0 +#define CASE_FOLD 1 + +typedef pic_value (*pic_reader_t)(pic_state *, xFILE *file, int c, struct reader_control *); + +static pic_reader_t reader_table[256]; +static pic_reader_t reader_dispatch[256]; + +static pic_value read(pic_state *pic, xFILE *file, int c, struct reader_control *p); +static pic_value read_nullable(pic_state *pic, xFILE *file, int c, struct reader_control *p); PIC_NORETURN static void read_error(pic_state *pic, const char *msg, pic_value irritants) @@ -79,16 +92,16 @@ strcaseeq(const char *s1, const char *s2) } static int -case_fold(pic_state *pic, int c) +case_fold(int c, struct reader_control *p) { - if (pic->reader.typecase == PIC_CASE_FOLD) { + if (p->typecase == CASE_FOLD) { c = tolower(c); } return c; } static pic_value -read_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int c) +read_comment(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { do { c = next(pic, file); @@ -98,7 +111,7 @@ read_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int c) } static pic_value -read_block_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int PIC_UNUSED(c)) +read_block_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control PIC_UNUSED(*p)) { int x, y; int i = 1; @@ -120,48 +133,48 @@ read_block_comment(pic_state PIC_UNUSED(*pic), xFILE *file, int PIC_UNUSED(c)) } static pic_value -read_datum_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_datum_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { - read(pic, file, next(pic, file)); + read(pic, file, next(pic, file), p); return pic_invalid_value(pic); } static pic_value -read_directive(pic_state *pic, xFILE *file, int c) +read_directive(pic_state *pic, xFILE *file, int c, struct reader_control *p) { switch (peek(pic, file)) { case 'n': if (expect(pic, file, "no-fold-case")) { - pic->reader.typecase = PIC_CASE_DEFAULT; + p->typecase = CASE_DEFAULT; return pic_invalid_value(pic); } break; case 'f': if (expect(pic, file, "fold-case")) { - pic->reader.typecase = PIC_CASE_FOLD; + p->typecase = CASE_FOLD; return pic_invalid_value(pic); } break; } - return read_comment(pic, file, c); + return read_comment(pic, file, c, p); } static pic_value -read_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { - return pic_list(pic, 2, pic_intern_lit(pic, "quote"), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "quote"), read(pic, file, next(pic, file), p)); } static pic_value -read_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { - return pic_list(pic, 2, pic_intern_lit(pic, "quasiquote"), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "quasiquote"), read(pic, file, next(pic, file), p)); } static pic_value -read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { pic_value tag; @@ -171,23 +184,23 @@ read_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) } else { tag = pic_intern_lit(pic, "unquote"); } - return pic_list(pic, 2, tag, read(pic, file, next(pic, file))); + return pic_list(pic, 2, tag, read(pic, file, next(pic, file), p)); } static pic_value -read_syntax_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_syntax_quote(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { - return pic_list(pic, 2, pic_intern_lit(pic, "syntax-quote"), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "syntax-quote"), read(pic, file, next(pic, file), p)); } static pic_value -read_syntax_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_syntax_quasiquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { - return pic_list(pic, 2, pic_intern_lit(pic, "syntax-quasiquote"), read(pic, file, next(pic, file))); + return pic_list(pic, 2, pic_intern_lit(pic, "syntax-quasiquote"), read(pic, file, next(pic, file), p)); } static pic_value -read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) +read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *p) { pic_value tag; @@ -197,11 +210,11 @@ read_syntax_unquote(pic_state *pic, xFILE *file, int PIC_UNUSED(c)) } else { tag = pic_intern_lit(pic, "syntax-unquote"); } - return pic_list(pic, 2, tag, read(pic, file, next(pic, file))); + return pic_list(pic, 2, tag, read(pic, file, next(pic, file), p)); } static pic_value -read_symbol(pic_state *pic, xFILE *file, int c) +read_symbol(pic_state *pic, xFILE *file, int c, struct reader_control *p) { int len; char *buf; @@ -209,14 +222,14 @@ read_symbol(pic_state *pic, xFILE *file, int c) len = 1; buf = pic_malloc(pic, len + 1); - buf[0] = case_fold(pic, c); + buf[0] = case_fold(c, p); buf[1] = 0; while (! isdelim(peek(pic, file))) { c = next(pic, file); len += 1; buf = pic_realloc(pic, buf, len + 1); - buf[len - 1] = case_fold(pic, c); + buf[len - 1] = case_fold(c, p); buf[len] = 0; } @@ -227,7 +240,7 @@ read_symbol(pic_state *pic, xFILE *file, int c) } static unsigned -read_uinteger(pic_state *pic, xFILE *file, int c) +read_uinteger(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { unsigned u = 0; @@ -244,7 +257,7 @@ read_uinteger(pic_state *pic, xFILE *file, int c) } static pic_value -read_unsigned(pic_state *pic, xFILE *file, int c) +read_unsigned(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { #define ATOF_BUF_SIZE (64) char buf[ATOF_BUF_SIZE]; @@ -301,9 +314,9 @@ read_unsigned(pic_state *pic, xFILE *file, int c) } static pic_value -read_number(pic_state *pic, xFILE *file, int c) +read_number(pic_state *pic, xFILE *file, int c, struct reader_control *p) { - return read_unsigned(pic, file, c); + return read_unsigned(pic, file, c, p); } static pic_value @@ -317,15 +330,15 @@ negate(pic_state *pic, pic_value n) } static pic_value -read_minus(pic_state *pic, xFILE *file, int c) +read_minus(pic_state *pic, xFILE *file, int c, struct reader_control *p) { pic_value sym; if (isdigit(peek(pic, file))) { - return negate(pic, read_unsigned(pic, file, next(pic, file))); + return negate(pic, read_unsigned(pic, file, next(pic, file), p)); } else { - sym = read_symbol(pic, file, c); + sym = read_symbol(pic, file, c, p); if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "-inf.0")) { return pic_float_value(pic, -(1.0 / 0.0)); } @@ -337,15 +350,15 @@ read_minus(pic_state *pic, xFILE *file, int c) } static pic_value -read_plus(pic_state *pic, xFILE *file, int c) +read_plus(pic_state *pic, xFILE *file, int c, struct reader_control *p) { pic_value sym; if (isdigit(peek(pic, file))) { - return read_unsigned(pic, file, next(pic, file)); + return read_unsigned(pic, file, next(pic, file), p); } else { - sym = read_symbol(pic, file, c); + sym = read_symbol(pic, file, c, p); if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "+inf.0")) { return pic_float_value(pic, 1.0 / 0.0); } @@ -357,7 +370,7 @@ read_plus(pic_state *pic, xFILE *file, int c) } static pic_value -read_true(pic_state *pic, xFILE *file, int c) +read_true(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { if ((c = peek(pic, file)) == 'r') { if (! expect(pic, file, "rue")) { @@ -371,7 +384,7 @@ read_true(pic_state *pic, xFILE *file, int c) } static pic_value -read_false(pic_state *pic, xFILE *file, int c) +read_false(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { if ((c = peek(pic, file)) == 'a') { if (! expect(pic, file, "alse")) { @@ -385,7 +398,7 @@ read_false(pic_state *pic, xFILE *file, int c) } static pic_value -read_char(pic_state *pic, xFILE *file, int c) +read_char(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { c = next(pic, file); @@ -420,7 +433,7 @@ read_char(pic_state *pic, xFILE *file, int c) } static pic_value -read_string(pic_state *pic, xFILE *file, int c) +read_string(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { char *buf; int size, cnt; @@ -455,7 +468,7 @@ read_string(pic_state *pic, xFILE *file, int c) } static pic_value -read_pipe(pic_state *pic, xFILE *file, int c) +read_pipe(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) { char *buf; int size, cnt; @@ -499,7 +512,7 @@ read_pipe(pic_state *pic, xFILE *file, int c) } static pic_value -read_blob(pic_state *pic, xFILE *file, int c) +read_blob(pic_state *pic, xFILE *file, int c, struct reader_control *p) { int nbits, n; int len; @@ -524,7 +537,7 @@ read_blob(pic_state *pic, xFILE *file, int c) dat = NULL; c = next(pic, file); while ((c = skip(pic, file, c)) != ')') { - n = read_uinteger(pic, file, c); + n = read_uinteger(pic, file, c, p); if (n < 0 || (1 << nbits) <= n) { read_error(pic, "invalid element in bytevector literal", pic_list(pic, 1, pic_int_value(pic, n))); } @@ -541,7 +554,7 @@ read_blob(pic_state *pic, xFILE *file, int c) } static pic_value -read_undef_or_blob(pic_state *pic, xFILE *file, int c) +read_undef_or_blob(pic_state *pic, xFILE *file, int c, struct reader_control *p) { if ((c = peek(pic, file)) == 'n') { if (! expect(pic, file, "ndefined")) { @@ -552,11 +565,11 @@ read_undef_or_blob(pic_state *pic, xFILE *file, int c) if (! isdigit(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, file, 'u'); + return read_blob(pic, file, 'u', p); } static pic_value -read_pair(pic_state *pic, xFILE *file, int c) +read_pair(pic_state *pic, xFILE *file, int c, struct reader_control *p) { static const int tCLOSE = ')'; pic_value car, cdr; @@ -569,11 +582,11 @@ read_pair(pic_state *pic, xFILE *file, int c) return pic_nil_value(pic); } if (c == '.' && isdelim(peek(pic, file))) { - cdr = read(pic, file, next(pic, file)); + cdr = read(pic, file, next(pic, file), p); closing: if ((c = skip(pic, file, ' ')) != tCLOSE) { - if (pic_invalid_p(pic, read_nullable(pic, file, c))) { + if (pic_invalid_p(pic, read_nullable(pic, file, c, p))) { goto closing; } read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); @@ -581,24 +594,24 @@ read_pair(pic_state *pic, xFILE *file, int c) return cdr; } else { - car = read_nullable(pic, file, c); + car = read_nullable(pic, file, c, p); if (pic_invalid_p(pic, car)) { goto retry; } - cdr = read_pair(pic, file, '('); + cdr = read_pair(pic, file, '(', p); return pic_cons(pic, car, cdr); } } static pic_value -read_vector(pic_state *pic, xFILE *file, int c) +read_vector(pic_state *pic, xFILE *file, int c, struct reader_control *p) { pic_value list, it, elem, vec; int i = 0; - list = read(pic, file, c); + list = read(pic, file, c, p); vec = pic_make_vec(pic, pic_length(pic, list), NULL); @@ -610,12 +623,11 @@ read_vector(pic_state *pic, xFILE *file, int c) } static pic_value -read_label_set(pic_state *pic, xFILE *file, int i) +read_label_set(pic_state *pic, xFILE *file, int i, struct reader_control *p) { - khash_t(read) *h = &pic->reader.labels; + khash_t(read) *h = &p->labels; pic_value val; - int c, ret; - int it; + int c, ret, it; it = kh_put(read, h, i, &ret); @@ -626,7 +638,7 @@ read_label_set(pic_state *pic, xFILE *file, int i) kh_val(h, it) = val = pic_cons(pic, pic_undef_value(pic), pic_undef_value(pic)); - tmp = read(pic, file, c); + tmp = read(pic, file, c, p); pic_pair_ptr(pic, val)->car = pic_car(pic, tmp); pic_pair_ptr(pic, val)->cdr = pic_cdr(pic, tmp); @@ -647,7 +659,7 @@ read_label_set(pic_state *pic, xFILE *file, int i) kh_val(h, it) = val = pic_make_vec(pic, 0, NULL); - tmp = read(pic, file, c); + tmp = read(pic, file, c, p); PIC_SWAP(pic_value *, pic_vec_ptr(pic, tmp)->data, pic_vec_ptr(pic, val)->data); PIC_SWAP(int, pic_vec_ptr(pic, tmp)->len, pic_vec_ptr(pic, val)->len); @@ -658,7 +670,7 @@ read_label_set(pic_state *pic, xFILE *file, int i) } default: { - kh_val(h, it) = val = read(pic, file, c); + kh_val(h, it) = val = read(pic, file, c, p); return val; } @@ -666,9 +678,9 @@ read_label_set(pic_state *pic, xFILE *file, int i) } static pic_value -read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i) +read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i, struct reader_control *p) { - khash_t(read) *h = &pic->reader.labels; + khash_t(read) *h = &p->labels; int it; it = kh_get(read, h, i); @@ -679,7 +691,7 @@ read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i) } static pic_value -read_label(pic_state *pic, xFILE *file, int c) +read_label(pic_state *pic, xFILE *file, int c, struct reader_control *p) { int i; @@ -689,22 +701,22 @@ read_label(pic_state *pic, xFILE *file, int c) } while (isdigit(c = next(pic, file))); if (c == '=') { - return read_label_set(pic, file, i); + return read_label_set(pic, file, i, p); } if (c == '#') { - return read_label_ref(pic, file, i); + return read_label_ref(pic, file, i, p); } read_error(pic, "broken label expression", pic_nil_value(pic)); } static pic_value -read_unmatch(pic_state *pic, xFILE PIC_UNUSED(*file), int PIC_UNUSED(c)) +read_unmatch(pic_state *pic, xFILE PIC_UNUSED(*file), int PIC_UNUSED(c), struct reader_control PIC_UNUSED(*p)) { read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); } static pic_value -read_dispatch(pic_state *pic, xFILE *file, int c) +read_dispatch(pic_state *pic, xFILE *file, int c, struct reader_control *p) { c = next(pic, file); @@ -712,15 +724,15 @@ read_dispatch(pic_state *pic, xFILE *file, int c) read_error(pic, "unexpected EOF", pic_nil_value(pic)); } - if (pic->reader.dispatch[c] == NULL) { + if (reader_dispatch[c] == NULL) { read_error(pic, "invalid character at the seeker head", pic_list(pic, 1, pic_char_value(pic, c))); } - return pic->reader.dispatch[c](pic, file, c); + return reader_dispatch[c](pic, file, c, p); } static pic_value -read_nullable(pic_state *pic, xFILE *file, int c) +read_nullable(pic_state *pic, xFILE *file, int c, struct reader_control *p) { c = skip(pic, file, c); @@ -728,20 +740,20 @@ read_nullable(pic_state *pic, xFILE *file, int c) read_error(pic, "unexpected EOF", pic_nil_value(pic)); } - if (pic->reader.table[c] == NULL) { + if (reader_table[c] == NULL) { read_error(pic, "invalid character at the seeker head", pic_list(pic, 1, pic_char_value(pic, c))); } - return pic->reader.table[c](pic, file, c); + return reader_table[c](pic, file, c, p); } static pic_value -read(pic_state *pic, xFILE *file, int c) +read(pic_state *pic, xFILE *file, int c, struct reader_control *p) { pic_value val; retry: - val = read_nullable(pic, file, c); + val = read_nullable(pic, file, c, p); if (pic_invalid_p(pic, val)) { c = next(pic, file); @@ -752,95 +764,97 @@ read(pic_state *pic, xFILE *file, int c) } static void -reader_table_init(pic_reader *reader) +reader_table_init(void) { int c; - reader->table[0] = NULL; + for (c = 0; c < 256; ++c) { + reader_table[c] = NULL; + } + for (c = 0; c < 256; ++c) { + reader_dispatch[c] = NULL; + } /* default reader */ for (c = 1; c < 256; ++c) { - reader->table[c] = read_symbol; + reader_table[c] = read_symbol; } - reader->table[')'] = read_unmatch; - reader->table[';'] = read_comment; - reader->table['\''] = read_quote; - reader->table['`'] = read_quasiquote; - reader->table[','] = read_unquote; - reader->table['"'] = read_string; - reader->table['|'] = read_pipe; - reader->table['+'] = read_plus; - reader->table['-'] = read_minus; - reader->table['('] = read_pair; - reader->table['#'] = read_dispatch; + reader_table[')'] = read_unmatch; + reader_table[';'] = read_comment; + reader_table['\''] = read_quote; + reader_table['`'] = read_quasiquote; + reader_table[','] = read_unquote; + reader_table['"'] = read_string; + reader_table['|'] = read_pipe; + reader_table['+'] = read_plus; + reader_table['-'] = read_minus; + reader_table['('] = read_pair; + reader_table['#'] = read_dispatch; /* read number */ for (c = '0'; c <= '9'; ++c) { - reader->table[c] = read_number; + reader_table[c] = read_number; } - reader->dispatch['!'] = read_directive; - reader->dispatch['|'] = read_block_comment; - reader->dispatch[';'] = read_datum_comment; - reader->dispatch['t'] = read_true; - reader->dispatch['f'] = read_false; - reader->dispatch['\''] = read_syntax_quote; - reader->dispatch['`'] = read_syntax_quasiquote; - reader->dispatch[','] = read_syntax_unquote; - reader->dispatch['\\'] = read_char; - reader->dispatch['('] = read_vector; - reader->dispatch['u'] = read_undef_or_blob; + reader_dispatch['!'] = read_directive; + reader_dispatch['|'] = read_block_comment; + reader_dispatch[';'] = read_datum_comment; + reader_dispatch['t'] = read_true; + reader_dispatch['f'] = read_false; + reader_dispatch['\''] = read_syntax_quote; + reader_dispatch['`'] = read_syntax_quasiquote; + reader_dispatch[','] = read_syntax_unquote; + reader_dispatch['\\'] = read_char; + reader_dispatch['('] = read_vector; + reader_dispatch['u'] = read_undef_or_blob; /* read labels */ for (c = '0'; c <= '9'; ++c) { - reader->dispatch[c] = read_label; + reader_dispatch[c] = read_label; } } -void -pic_reader_init(pic_state *pic) +static void +reader_init(pic_state PIC_UNUSED(*pic), struct reader_control *p) { - int c; - - pic->reader.typecase = PIC_CASE_DEFAULT; - kh_init(read, &pic->reader.labels); - - for (c = 0; c < 256; ++c) { - pic->reader.table[c] = NULL; - } - - for (c = 0; c < 256; ++c) { - pic->reader.dispatch[c] = NULL; - } - - reader_table_init(&pic->reader); + p->typecase = CASE_DEFAULT; + kh_init(read, &p->labels); } -void -pic_reader_destroy(pic_state *pic) +static void +reader_destroy(pic_state *pic, struct reader_control *p) { - kh_destroy(read, &pic->reader.labels); + kh_destroy(read, &p->labels); } pic_value pic_read(pic_state *pic, pic_value port) { + struct reader_control p; size_t ai = pic_enter(pic); pic_value val; xFILE *file = pic_fileno(pic, port); int c; - while ((c = skip(pic, file, next(pic, file))) != EOF) { - val = read_nullable(pic, file, c); + reader_init(pic, &p); - if (! pic_invalid_p(pic, val)) { - break; + pic_try { + while ((c = skip(pic, file, next(pic, file))) != EOF) { + val = read_nullable(pic, file, c, &p); + + if (! pic_invalid_p(pic, val)) { + break; + } + pic_leave(pic, ai); + } + if (c == EOF) { + val = pic_eof_object(pic); } - pic_leave(pic, ai); } - if (c == EOF) { - return pic_eof_object(pic); + pic_catch { + reader_destroy(pic, &p); + pic_raise(pic, pic_err(pic)); } pic_leave(pic, ai); @@ -879,5 +893,7 @@ pic_read_read(pic_state *pic) void pic_init_read(pic_state *pic) { + reader_table_init(); + pic_defun(pic, "read", pic_read_read); } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 293e13de..4d002d4e 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -308,9 +308,6 @@ pic_open(pic_allocf allocf, void *userdata) pic->cp->depth = 0; pic->cp->in = pic->cp->out = NULL; - /* reader */ - pic_reader_init(pic); - /* parameter table */ pic->ptable = pic_cons(pic, pic_make_weak(pic), pic_nil_value(pic)); @@ -378,9 +375,6 @@ pic_close(pic_state *pic) /* free heaps */ pic_heap_close(pic, pic->heap); - /* free reader struct */ - pic_reader_destroy(pic); - /* free runtime context */ allocf(pic->userdata, pic->stbase, 0); allocf(pic->userdata, pic->cibase, 0); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 056e0812..603cf594 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -163,8 +163,10 @@ write_str(pic_state *pic, pic_value str, xFILE *file, struct writer_control *p) } static void -write_float(pic_state *pic, double f, xFILE *file) +write_float(pic_state *pic, pic_value flo, xFILE *file) { + double f = pic_float(pic, flo); + if (f != f) { xfprintf(pic, file, "+nan.0"); } else if (f == 1.0 / 0.0) { @@ -321,12 +323,12 @@ write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) case PIC_TYPE_INT: xfprintf(pic, file, "%d", pic_int(pic, obj)); break; - case PIC_TYPE_FLOAT: - write_float(pic, pic_float(pic, obj), file); - break; case PIC_TYPE_SYMBOL: xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, obj))); break; + case PIC_TYPE_FLOAT: + write_float(pic, obj, file); + break; case PIC_TYPE_BLOB: write_blob(pic, obj, file); break; From 7a440186c6e72963e290792283a36ac1a3230054 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 18:34:19 +0900 Subject: [PATCH 092/119] remove unused definitions --- extlib/benz/write.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 603cf594..ce56943c 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -6,11 +6,6 @@ #include "picrin/extra.h" #include "picrin/private/object.h" -KHASH_DECLARE(l, void *, int) -KHASH_DECLARE(v, void *, int) -KHASH_DEFINE2(l, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal) -KHASH_DEFINE2(v, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal) - struct writer_control { int mode; int op; From 54301ce98c54f4c181918edea73a9e4a60ad276f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 18:59:18 +0900 Subject: [PATCH 093/119] add pic_atpanic --- extlib/benz/error.c | 13 +++++++------ extlib/benz/include/picrin.h | 3 +++ extlib/benz/include/picrin/private/state.h | 1 + extlib/benz/state.c | 1 + 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 744b7dab..24acabfb 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -8,15 +8,16 @@ #include "picrin/private/state.h" void -pic_panic(pic_state PIC_UNUSED(*pic), const char *msg) +pic_panic(pic_state *pic, const char *msg) { - extern PIC_NORETURN void abort(); + if (pic->panicf) { + pic->panicf(pic, msg); + } -#if DEBUG - fprintf(stderr, "abort: %s\n", msg); -#else - (void)msg; +#if PIC_ENABLE_STDIO + fprintf(stderr, "picrin panic!: %s\n", msg); #endif + PIC_ABORT(pic); } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 330421b0..7147991f 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -96,6 +96,9 @@ const char *pic_current_library(pic_state *); void pic_import(pic_state *, const char *lib); void pic_export(pic_state *, pic_value sym); +typedef void (*pic_panicf)(pic_state *, const char *msg); + +pic_panicf pic_atpanic(pic_state *, pic_panicf f); PIC_NORETURN void pic_panic(pic_state *, const char *msg); PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); PIC_NORETURN void pic_raise(pic_state *, pic_value v); diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index 48341157..a2b859a0 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -76,6 +76,7 @@ struct pic_state { size_t arena_size, arena_idx; pic_value err; + pic_panicf panicf; char *native_stack_start; }; diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 4d002d4e..aac2910e 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -276,6 +276,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->ireps.prev = &pic->ireps; /* raised error object */ + pic->panicf = NULL; pic->err = pic_invalid_value(pic); /* file pool */ From f8b05efa7f5a87220f531230986d9b8de1ee4e89 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 19:02:23 +0900 Subject: [PATCH 094/119] don't call pic_panic on internal logic flaw --- contrib/40.srfi/src/106.c | 6 +----- extlib/benz/gc.c | 24 ++++-------------------- extlib/benz/state.c | 8 +------- extlib/benz/var.c | 2 +- 4 files changed, 7 insertions(+), 33 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 72633481..5f1e4c3a 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -219,11 +219,7 @@ pic_socket_socket_recv(pic_state *pic) ensure_socket_is_open(pic, sock); - buf = pic_blob(pic, pic_blob_value(pic, NULL, size), NULL); - if (buf == NULL && size > 0) { - /* XXX: Is it really OK? */ - pic_panic(pic, "memory exhausted"); - } + buf = pic_alloca(pic, size); errno = 0; do { diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 8dc5286b..375c4dab 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -403,16 +403,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } break; } - case PIC_TYPE_NIL: - case PIC_TYPE_TRUE: - case PIC_TYPE_FALSE: - case PIC_TYPE_FLOAT: - case PIC_TYPE_INT: - case PIC_TYPE_CHAR: - case PIC_TYPE_EOF: - case PIC_TYPE_UNDEF: - case PIC_TYPE_INVALID: - pic_panic(pic, "logic flaw"); + default: + PIC_UNREACHABLE(); } } @@ -574,16 +566,8 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) case PIC_TYPE_CP: break; - case PIC_TYPE_NIL: - case PIC_TYPE_TRUE: - case PIC_TYPE_FALSE: - case PIC_TYPE_FLOAT: - case PIC_TYPE_INT: - case PIC_TYPE_CHAR: - case PIC_TYPE_EOF: - case PIC_TYPE_UNDEF: - case PIC_TYPE_INVALID: - pic_panic(pic, "logic flaw"); + default: + PIC_UNREACHABLE(); } } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index aac2910e..b8d975d4 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -178,13 +178,7 @@ pic_init_core(pic_state *pic) pic_defun(pic, "features", pic_features); - pic_try { - pic_load_cstr(pic, &pic_boot[0][0]); - } - pic_catch { - pic_print_backtrace(pic, xstdout); - pic_panic(pic, ""); - } + pic_load_cstr(pic, &pic_boot[0][0]); } pic_state * diff --git a/extlib/benz/var.c b/extlib/benz/var.c index 8f4b2ef7..72105734 100644 --- a/extlib/benz/var.c +++ b/extlib/benz/var.c @@ -17,7 +17,7 @@ var_get(pic_state *pic, pic_value var) return pic_weak_ref(pic, weak, var); } } - pic_panic(pic, "logic flaw"); + PIC_UNREACHABLE(); } static pic_value From 2a0a7be40d4c831a3cc9f62d0891ce8c9318b2f8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 19:19:35 +0900 Subject: [PATCH 095/119] C++ mode of PIC_UNUSED --- contrib/40.srfi/src/106.c | 8 ++++---- extlib/benz/blob.c | 2 +- extlib/benz/bool.c | 8 ++++---- extlib/benz/data.c | 2 +- extlib/benz/dict.c | 4 ++-- extlib/benz/eval.c | 2 +- extlib/benz/file.c | 30 ++++++++++++++-------------- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin/setup.h | 4 +++- extlib/benz/port.c | 2 +- extlib/benz/proc.c | 2 +- extlib/benz/read.c | 24 +++++++++++----------- extlib/benz/string.c | 4 ++-- extlib/benz/symbol.c | 2 +- extlib/benz/value.c | 32 +++++++++++++++--------------- extlib/benz/vector.c | 6 +++--- 16 files changed, 68 insertions(+), 66 deletions(-) diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index 5f1e4c3a..e2cdf13a 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -262,7 +262,7 @@ pic_socket_socket_close(pic_state *pic) } static int -xf_socket_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) +xf_socket_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) { struct pic_socket_t *sock; @@ -272,7 +272,7 @@ xf_socket_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) } static int -xf_socket_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) +xf_socket_write(pic_state *PIC_UNUSED(pic), void *cookie, const char *ptr, int size) { struct pic_socket_t *sock; @@ -282,14 +282,14 @@ xf_socket_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int s } static long -xf_socket_seek(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) +xf_socket_seek(pic_state *PIC_UNUSED(pic), void *PIC_UNUSED(cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) { errno = EBADF; return -1; } static int -xf_socket_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) +xf_socket_close(pic_state *PIC_UNUSED(pic), void *PIC_UNUSED(cookie)) { return 0; } diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index 04befd71..292ea083 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -21,7 +21,7 @@ pic_blob_value(pic_state *pic, const unsigned char *buf, int len) } unsigned char * -pic_blob(pic_state PIC_UNUSED(*pic), pic_value blob, int *len) +pic_blob(pic_state *PIC_UNUSED(pic), pic_value blob, int *len) { if (len) { *len = pic_blob_ptr(pic, blob)->len; diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 7c6b2a54..35ec40d3 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -8,13 +8,13 @@ #if PIC_NAN_BOXING bool -pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +pic_eq_p(pic_state *PIC_UNUSED(pic), pic_value x, pic_value y) { return x == y; } bool -pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +pic_eqv_p(pic_state *PIC_UNUSED(pic), pic_value x, pic_value y) { return x == y; } @@ -22,7 +22,7 @@ pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) #else bool -pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +pic_eq_p(pic_state *PIC_UNUSED(pic), pic_value x, pic_value y) { if (pic_type(pic, x) != pic_type(pic, y)) return false; @@ -38,7 +38,7 @@ pic_eq_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) } bool -pic_eqv_p(pic_state PIC_UNUSED(*pic), pic_value x, pic_value y) +pic_eqv_p(pic_state *PIC_UNUSED(pic), pic_value x, pic_value y) { if (pic_type(pic, x) != pic_type(pic, y)) return false; diff --git a/extlib/benz/data.c b/extlib/benz/data.c index 23e0baa5..4ba3cb71 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -11,7 +11,7 @@ pic_data_p(pic_state *pic, pic_value obj, const pic_data_type *type) } void * -pic_data(pic_state PIC_UNUSED(*pic), pic_value data) +pic_data(pic_state *PIC_UNUSED(pic), pic_value data) { return pic_data_ptr(pic, data)->data; } diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 164513e7..cfbf73ff 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -43,7 +43,7 @@ pic_dict_set(pic_state *pic, pic_value dict, pic_value key, pic_value val) } int -pic_dict_size(pic_state PIC_UNUSED(*pic), pic_value dict) +pic_dict_size(pic_state *PIC_UNUSED(pic), pic_value dict) { return kh_size(&pic_dict_ptr(pic, dict)->hash); } @@ -70,7 +70,7 @@ pic_dict_del(pic_state *pic, pic_value dict, pic_value key) } bool -pic_dict_next(pic_state PIC_UNUSED(*pic), pic_value dict, int *iter, pic_value *key, pic_value *val) +pic_dict_next(pic_state *PIC_UNUSED(pic), pic_value dict, int *iter, pic_value *key, pic_value *val) { khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash; int it = *iter; diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 1f3ac784..148fbc04 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -110,7 +110,7 @@ analyzer_scope_init(pic_state *pic, analyze_scope *scope, pic_value formal, anal } static void -analyzer_scope_destroy(pic_state PIC_UNUSED(*pic), analyze_scope PIC_UNUSED(*scope)) +analyzer_scope_destroy(pic_state *PIC_UNUSED(pic), analyze_scope *PIC_UNUSED(scope)) { /* nothing here */ } diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 0fb6fc83..d828845d 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -40,15 +40,15 @@ int xfclose(pic_state *pic, xFILE *fp) { return fp->vtable.close(pic, fp->vtable.cookie); } -void xclearerr(pic_state PIC_UNUSED(*pic), xFILE *fp) { +void xclearerr(pic_state *PIC_UNUSED(pic), xFILE *fp) { fp->flag &= ~(X_EOF | X_ERR); } -int xfeof(pic_state PIC_UNUSED(*pic), xFILE *fp) { +int xfeof(pic_state *PIC_UNUSED(pic), xFILE *fp) { return (fp->flag & X_EOF) != 0; } -int xferror(pic_state PIC_UNUSED(*pic), xFILE *fp) { +int xferror(pic_state *PIC_UNUSED(pic), xFILE *fp) { return (fp->flag & X_ERR) != 0; } @@ -204,7 +204,7 @@ char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { return (c == EOF && buf == s) ? NULL : s; } -int xungetc(pic_state PIC_UNUSED(*pic), int c, xFILE *fp) { +int xungetc(pic_state *PIC_UNUSED(pic), int c, xFILE *fp) { unsigned char uc = c; if (c == EOF || fp->base == fp->ptr) { @@ -367,7 +367,7 @@ xFILE *xfile_xstderr(pic_state *pic) { return &pic->files[2]; } #if PIC_ENABLE_STDIO static int -file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { +file_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) { FILE *file = cookie; int r; @@ -384,7 +384,7 @@ file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { } static int -file_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) { +file_write(pic_state *PIC_UNUSED(pic), void *cookie, const char *ptr, int size) { FILE *file = cookie; int r; @@ -397,7 +397,7 @@ file_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) } static long -file_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { +file_seek(pic_state *PIC_UNUSED(pic), void *cookie, long pos, int whence) { switch (whence) { case XSEEK_CUR: whence = SEEK_CUR; @@ -416,7 +416,7 @@ file_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { } static int -file_close(pic_state PIC_UNUSED(*pic), void *cookie) { +file_close(pic_state *PIC_UNUSED(pic), void *cookie) { return fclose(cookie); } @@ -435,7 +435,7 @@ xFILE *xfopen_file(pic_state *pic, FILE *fp, const char *mode) { typedef struct { char *buf; long pos, end, capa; } xbuf_t; static int -string_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) +string_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) { xbuf_t *m = cookie; @@ -463,7 +463,7 @@ string_write(pic_state *pic, void *cookie, const char *ptr, int size) } static long -string_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) +string_seek(pic_state *PIC_UNUSED(pic), void *cookie, long pos, int whence) { xbuf_t *m = cookie; @@ -529,26 +529,26 @@ int xfget_buf(pic_state *pic, xFILE *file, const char **buf, int *len) { } static int -null_read(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), char PIC_UNUSED(*ptr), int PIC_UNUSED(size)) { +null_read(pic_state *PIC_UNUSED(pic), void *PIC_UNUSED(cookie), char *PIC_UNUSED(ptr), int PIC_UNUSED(size)) { return 0; } static int -null_write(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), const char PIC_UNUSED(*ptr), int size) { +null_write(pic_state *PIC_UNUSED(pic), void *PIC_UNUSED(cookie), const char *PIC_UNUSED(ptr), int size) { return size; } static long -null_seek(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) { +null_seek(pic_state *PIC_UNUSED(pic), void *PIC_UNUSED(cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) { return 0; } static int -null_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) { +null_close(pic_state *PIC_UNUSED(pic), void *PIC_UNUSED(cookie)) { return 0; } -xFILE *xfopen_null(pic_state PIC_UNUSED(*pic), const char *mode) { +xFILE *xfopen_null(pic_state *PIC_UNUSED(pic), const char *mode) { switch (*mode) { case 'r': return xfunopen(pic, 0, null_read, 0, null_seek, null_close); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 375c4dab..aaae14f9 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -84,7 +84,7 @@ pic_heap_close(pic_state *pic, struct pic_heap *heap) #if PIC_ENABLE_LIBC void * -pic_default_allocf(void PIC_UNUSED(*userdata), void *ptr, size_t size) +pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size) { if (size != 0) { return realloc(ptr, size); diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 810fd338..008c0390 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -138,7 +138,9 @@ typedef unsigned long uint32_t; #define PIC_FALLTHROUGH ((void)0) -#if __GNUC__ || __clang__ +#if __cplusplus +# define PIC_UNUSED(v) +#elif __GNUC__ || __clang__ # define PIC_UNUSED(v) __attribute__((unused)) v #else # define PIC_UNUSED(v) v diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 0f102ff4..016a52db 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -22,7 +22,7 @@ pic_open_port(pic_state *pic, xFILE *file) } xFILE * -pic_fileno(pic_state PIC_UNUSED(*pic), pic_value port) +pic_fileno(pic_state *PIC_UNUSED(pic), pic_value port) { return pic_port_ptr(pic, port)->file; } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index bc4863eb..3028bc67 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -1024,7 +1024,7 @@ pic_funcall(pic_state *pic, const char *lib, const char *name, int n, ...) } void -pic_irep_incref(pic_state PIC_UNUSED(*pic), struct pic_irep *irep) +pic_irep_incref(pic_state *PIC_UNUSED(pic), struct pic_irep *irep) { irep->refc++; } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 1241be20..ed0cd4a6 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -101,7 +101,7 @@ case_fold(int c, struct reader_control *p) } static pic_value -read_comment(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_comment(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { do { c = next(pic, file); @@ -111,7 +111,7 @@ read_comment(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSE } static pic_value -read_block_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control PIC_UNUSED(*p)) +read_block_comment(pic_state *pic, xFILE *file, int PIC_UNUSED(c), struct reader_control *PIC_UNUSED(p)) { int x, y; int i = 1; @@ -240,7 +240,7 @@ read_symbol(pic_state *pic, xFILE *file, int c, struct reader_control *p) } static unsigned -read_uinteger(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_uinteger(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { unsigned u = 0; @@ -257,7 +257,7 @@ read_uinteger(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUS } static pic_value -read_unsigned(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_unsigned(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { #define ATOF_BUF_SIZE (64) char buf[ATOF_BUF_SIZE]; @@ -370,7 +370,7 @@ read_plus(pic_state *pic, xFILE *file, int c, struct reader_control *p) } static pic_value -read_true(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_true(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { if ((c = peek(pic, file)) == 'r') { if (! expect(pic, file, "rue")) { @@ -384,7 +384,7 @@ read_true(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(* } static pic_value -read_false(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_false(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { if ((c = peek(pic, file)) == 'a') { if (! expect(pic, file, "alse")) { @@ -398,7 +398,7 @@ read_false(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED( } static pic_value -read_char(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_char(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { c = next(pic, file); @@ -433,7 +433,7 @@ read_char(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(* } static pic_value -read_string(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_string(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { char *buf; int size, cnt; @@ -468,7 +468,7 @@ read_string(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED } static pic_value -read_pipe(pic_state *pic, xFILE *file, int c, struct reader_control PIC_UNUSED(*p)) +read_pipe(pic_state *pic, xFILE *file, int c, struct reader_control *PIC_UNUSED(p)) { char *buf; int size, cnt; @@ -678,7 +678,7 @@ read_label_set(pic_state *pic, xFILE *file, int i, struct reader_control *p) } static pic_value -read_label_ref(pic_state *pic, xFILE PIC_UNUSED(*file), int i, struct reader_control *p) +read_label_ref(pic_state *pic, xFILE *PIC_UNUSED(file), int i, struct reader_control *p) { khash_t(read) *h = &p->labels; int it; @@ -710,7 +710,7 @@ read_label(pic_state *pic, xFILE *file, int c, struct reader_control *p) } static pic_value -read_unmatch(pic_state *pic, xFILE PIC_UNUSED(*file), int PIC_UNUSED(c), struct reader_control PIC_UNUSED(*p)) +read_unmatch(pic_state *pic, xFILE *PIC_UNUSED(file), int PIC_UNUSED(c), struct reader_control *PIC_UNUSED(p)) { read_error(pic, "unmatched parenthesis", pic_nil_value(pic)); } @@ -816,7 +816,7 @@ reader_table_init(void) } static void -reader_init(pic_state PIC_UNUSED(*pic), struct reader_control *p) +reader_init(pic_state *PIC_UNUSED(pic), struct reader_control *p) { p->typecase = CASE_DEFAULT; kh_init(read, &p->labels); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 54b9c41c..567951d1 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -33,7 +33,7 @@ struct pic_rope { } while (0) void -pic_rope_incref(pic_state PIC_UNUSED(*pic), struct pic_rope *x) { +pic_rope_incref(pic_state *PIC_UNUSED(pic), struct pic_rope *x) { x->refcnt++; } @@ -264,7 +264,7 @@ pic_str_value(pic_state *pic, const char *str, int len) } int -pic_str_len(pic_state PIC_UNUSED(*pic), pic_value str) +pic_str_len(pic_state *PIC_UNUSED(pic), pic_value str) { return rope_len(pic_str_ptr(pic, str)->rope); } diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index bcd7dac0..8ebd3913 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -49,7 +49,7 @@ pic_make_identifier(pic_state *pic, pic_value base, pic_value env) } pic_value -pic_sym_name(pic_state PIC_UNUSED(*pic), pic_value sym) +pic_sym_name(pic_state *PIC_UNUSED(pic), pic_value sym) { return pic_obj_value(pic_sym_ptr(pic, sym)->u.str); } diff --git a/extlib/benz/value.c b/extlib/benz/value.c index db8ccdf7..87dd9d7d 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -18,13 +18,13 @@ #define pic_init_value(v,vtype) (v = (0xfff0000000000000ul | ((uint64_t)(vtype) << 48))) int -pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) +pic_vtype(pic_state *PIC_UNUSED(pic), pic_value v) { return 0xfff0 >= (v >> 48) ? PIC_TYPE_FLOAT : ((v >> 48) & 0xf); } double -pic_float(pic_state PIC_UNUSED(*pic), pic_value v) +pic_float(pic_state *PIC_UNUSED(pic), pic_value v) { union { double f; uint64_t i; } u; u.i = v; @@ -32,7 +32,7 @@ pic_float(pic_state PIC_UNUSED(*pic), pic_value v) } int -pic_int(pic_state PIC_UNUSED(*pic), pic_value v) +pic_int(pic_state *PIC_UNUSED(pic), pic_value v) { union { int i; unsigned u; } u; u.u = v & 0xfffffffful; @@ -40,7 +40,7 @@ pic_int(pic_state PIC_UNUSED(*pic), pic_value v) } char -pic_char(pic_state PIC_UNUSED(*pic), pic_value v) +pic_char(pic_state *PIC_UNUSED(pic), pic_value v) { return v & 0xfffffffful; } @@ -56,25 +56,25 @@ pic_obj_ptr(pic_value v) #define pic_init_value(v,vtype) ((v).type = (vtype), (v).u.data = NULL) int -pic_vtype(pic_state PIC_UNUSED(*pic), pic_value v) +pic_vtype(pic_state *PIC_UNUSED(pic), pic_value v) { return (int)(v.type); } double -pic_float(pic_state PIC_UNUSED(*pic), pic_value v) +pic_float(pic_state *PIC_UNUSED(pic), pic_value v) { return v.u.f; } int -pic_int(pic_state PIC_UNUSED(*pic), pic_value v) +pic_int(pic_state *PIC_UNUSED(pic), pic_value v) { return v.u.i; } char -pic_char(pic_state PIC_UNUSED(*pic), pic_value v) +pic_char(pic_state *PIC_UNUSED(pic), pic_value v) { return v.u.c; } @@ -100,7 +100,7 @@ pic_obj_value(void *ptr) } pic_value -pic_float_value(pic_state PIC_UNUSED(*pic), double f) +pic_float_value(pic_state *PIC_UNUSED(pic), double f) { union { double f; uint64_t i; } u; @@ -113,7 +113,7 @@ pic_float_value(pic_state PIC_UNUSED(*pic), double f) } pic_value -pic_int_value(pic_state PIC_UNUSED(*pic), int i) +pic_int_value(pic_state *PIC_UNUSED(pic), int i) { pic_value v; @@ -123,7 +123,7 @@ pic_int_value(pic_state PIC_UNUSED(*pic), int i) } pic_value -pic_char_value(pic_state PIC_UNUSED(*pic), char c) +pic_char_value(pic_state *PIC_UNUSED(pic), char c) { pic_value v; @@ -145,7 +145,7 @@ pic_obj_value(void *ptr) } pic_value -pic_float_value(pic_state PIC_UNUSED(*pic), double f) +pic_float_value(pic_state *PIC_UNUSED(pic), double f) { pic_value v; @@ -155,7 +155,7 @@ pic_float_value(pic_state PIC_UNUSED(*pic), double f) } pic_value -pic_int_value(pic_state PIC_UNUSED(*pic), int i) +pic_int_value(pic_state *PIC_UNUSED(pic), int i) { pic_value v; @@ -165,7 +165,7 @@ pic_int_value(pic_state PIC_UNUSED(*pic), int i) } pic_value -pic_char_value(pic_state PIC_UNUSED(*pic), char c) +pic_char_value(pic_state *PIC_UNUSED(pic), char c) { pic_value v; @@ -177,7 +177,7 @@ pic_char_value(pic_state PIC_UNUSED(*pic), char c) #endif #define DEFVAL(name, type) \ - pic_value name(pic_state PIC_UNUSED(*pic)) { \ + pic_value name(pic_state *PIC_UNUSED(pic)) { \ pic_value v; \ pic_init_value(v, type); \ return v; \ @@ -191,7 +191,7 @@ DEFVAL(pic_undef_value, PIC_TYPE_UNDEF) DEFVAL(pic_invalid_value, PIC_TYPE_INVALID) int -pic_type(pic_state PIC_UNUSED(*pic), pic_value v) +pic_type(pic_state *PIC_UNUSED(pic), pic_value v) { int tt = pic_vtype(pic, v); diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index aa254917..71d12519 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -26,19 +26,19 @@ pic_make_vec(pic_state *pic, int len, pic_value *argv) } pic_value -pic_vec_ref(pic_state PIC_UNUSED(*pic), pic_value vec, int k) +pic_vec_ref(pic_state *PIC_UNUSED(pic), pic_value vec, int k) { return pic_vec_ptr(pic, vec)->data[k]; } void -pic_vec_set(pic_state PIC_UNUSED(*pic), pic_value vec, int k, pic_value val) +pic_vec_set(pic_state *PIC_UNUSED(pic), pic_value vec, int k, pic_value val) { pic_vec_ptr(pic, vec)->data[k] = val; } int -pic_vec_len(pic_state PIC_UNUSED(*pic), pic_value vec) +pic_vec_len(pic_state *PIC_UNUSED(pic), pic_value vec) { return pic_vec_ptr(pic, vec)->len; } From d49e25a5b44da52e121b749fce9b824cb9c8e29c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 19:53:31 +0900 Subject: [PATCH 096/119] remove opcode.h --- extlib/benz/eval.c | 2 +- extlib/benz/include/picrin/private/irep.h | 42 ---- extlib/benz/include/picrin/private/opcode.h | 215 -------------------- extlib/benz/include/picrin/private/state.h | 2 +- extlib/benz/include/picrin/private/vm.h | 85 ++++++++ extlib/benz/proc.c | 8 +- 6 files changed, 92 insertions(+), 262 deletions(-) delete mode 100644 extlib/benz/include/picrin/private/irep.h delete mode 100644 extlib/benz/include/picrin/private/opcode.h create mode 100644 extlib/benz/include/picrin/private/vm.h diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 148fbc04..f1b334c7 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -5,7 +5,7 @@ #include "picrin.h" #include "picrin/extra.h" #include "picrin/private/object.h" -#include "picrin/private/opcode.h" +#include "picrin/private/vm.h" #include "picrin/private/state.h" #define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) diff --git a/extlib/benz/include/picrin/private/irep.h b/extlib/benz/include/picrin/private/irep.h deleted file mode 100644 index fff6c249..00000000 --- a/extlib/benz/include/picrin/private/irep.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_IREP_H -#define PICRIN_IREP_H - -#if defined(__cplusplus) -extern "C" { -#endif - -typedef struct { - int insn; - int a; - int b; -} pic_code; - -struct pic_list { - struct pic_list *prev, *next; -}; - -struct pic_irep { - struct pic_list list; - unsigned refc; - int argc, localc, capturec; - bool varg; - pic_code *code; - struct pic_irep **irep; - int *ints; - double *nums; - struct pic_object **pool; - size_t ncode, nirep, nints, nnums, npool; -}; - -void pic_irep_incref(pic_state *, struct pic_irep *); -void pic_irep_decref(pic_state *, struct pic_irep *); - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/private/opcode.h b/extlib/benz/include/picrin/private/opcode.h deleted file mode 100644 index e27a4a12..00000000 --- a/extlib/benz/include/picrin/private/opcode.h +++ /dev/null @@ -1,215 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_OPCODE_H -#define PICRIN_OPCODE_H - -#if defined(__cplusplus) -extern "C" { -#endif - -enum pic_opcode { - OP_NOP, - OP_POP, - OP_PUSHUNDEF, - OP_PUSHNIL, - OP_PUSHTRUE, - OP_PUSHFALSE, - OP_PUSHINT, - OP_PUSHFLOAT, - OP_PUSHCHAR, - OP_PUSHEOF, - OP_PUSHCONST, - OP_GREF, - OP_GSET, - OP_LREF, - OP_LSET, - OP_CREF, - OP_CSET, - OP_JMP, - OP_JMPIF, - OP_NOT, - OP_CALL, - OP_TAILCALL, - OP_RET, - OP_LAMBDA, - OP_CONS, - OP_CAR, - OP_CDR, - OP_NILP, - OP_SYMBOLP, - OP_PAIRP, - OP_ADD, - OP_SUB, - OP_MUL, - OP_DIV, - OP_EQ, - OP_LT, - OP_LE, - OP_GT, - OP_GE, - OP_STOP -}; - -#define PIC_INIT_CODE_I(code, op, ival) do { \ - code.insn = op; \ - code.a = ival; \ - } while (0) - -#if DEBUG - -PIC_INLINE void -pic_dump_code(pic_code c) -{ - switch (c.insn) { - case OP_NOP: - puts("OP_NOP"); - break; - case OP_POP: - puts("OP_POP"); - break; - case OP_PUSHUNDEF: - puts("OP_PUSHUNDEF"); - break; - case OP_PUSHNIL: - puts("OP_PUSHNIL"); - break; - case OP_PUSHTRUE: - puts("OP_PUSHTRUE"); - break; - case OP_PUSHFALSE: - puts("OP_PUSHFALSE"); - break; - case OP_PUSHINT: - printf("OP_PUSHINT\t%d\n", c.a); - break; - case OP_PUSHFLOAT: - printf("OP_PUSHFLAOT\t%d\n", c.a); - break; - case OP_PUSHCHAR: - printf("OP_PUSHCHAR\t%c\n", c.a); - break; - case OP_PUSHEOF: - puts("OP_PUSHEOF"); - break; - case OP_PUSHCONST: - printf("OP_PUSHCONST\t%d\n", c.a); - break; - case OP_GREF: - printf("OP_GREF\t%i\n", c.a); - break; - case OP_GSET: - printf("OP_GSET\t%i\n", c.a); - break; - case OP_LREF: - printf("OP_LREF\t%d\n", c.a); - break; - case OP_LSET: - printf("OP_LSET\t%d\n", c.a); - break; - case OP_CREF: - printf("OP_CREF\t%d\t%d\n", c.a, c.b); - break; - case OP_CSET: - printf("OP_CSET\t%d\t%d\n", c.a, c.b); - break; - case OP_JMP: - printf("OP_JMP\t%x\n", c.a); - break; - case OP_JMPIF: - printf("OP_JMPIF\t%x\n", c.a); - break; - case OP_NOT: - puts("OP_NOT"); - break; - case OP_CALL: - printf("OP_CALL\t%d\n", c.a); - break; - case OP_TAILCALL: - printf("OP_TAILCALL\t%d\n", c.a); - break; - case OP_RET: - puts("OP_RET"); - break; - case OP_LAMBDA: - printf("OP_LAMBDA\t%d\n", c.a); - break; - case OP_CONS: - puts("OP_CONS"); - break; - case OP_CAR: - puts("OP_CAR"); - break; - case OP_NILP: - puts("OP_NILP"); - break; - case OP_SYMBOLP: - puts("OP_SYMBOLP"); - break; - case OP_PAIRP: - puts("OP_PAIRP"); - break; - case OP_CDR: - puts("OP_CDR"); - break; - case OP_ADD: - puts("OP_ADD"); - break; - case OP_SUB: - puts("OP_SUB"); - break; - case OP_MUL: - puts("OP_MUL"); - break; - case OP_DIV: - puts("OP_DIV"); - break; - case OP_EQ: - puts("OP_EQ"); - break; - case OP_LT: - puts("OP_LT"); - break; - case OP_LE: - puts("OP_LE"); - break; - case OP_GT: - puts("OP_GT"); - break; - case OP_GE: - puts("OP_GE"); - break; - case OP_STOP: - puts("OP_STOP"); - break; - } -} - -PIC_INLINE void -pic_dump_irep(struct pic_irep *irep) -{ - size_t i; - - printf("## irep %p\n", (void *)irep); - printf("# argc = %d\n", irep->argc); - printf("# localc = %d\n", irep->localc); - printf("# capturec = %d\n", irep->capturec); - - for (i = 0; i < irep->ncode; ++i) { - printf("%02x: ", i); - pic_dump_code(irep->u.s.code[i]); - } - - for (i = 0; i < irep->nirep; ++i) { - pic_dump_irep(irep->u.s.irep[i].i); - } -} - -#endif - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index a2b859a0..cf11b4d1 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -12,7 +12,7 @@ extern "C" { #include "picrin/private/khash.h" #include "picrin/private/file.h" -#include "picrin/private/irep.h" +#include "picrin/private/vm.h" #include "picrin/private/gc.h" struct pic_lib { diff --git a/extlib/benz/include/picrin/private/vm.h b/extlib/benz/include/picrin/private/vm.h new file mode 100644 index 00000000..068b7299 --- /dev/null +++ b/extlib/benz/include/picrin/private/vm.h @@ -0,0 +1,85 @@ +/** + * See Copyright Notice in picrin.h + */ + +#ifndef PICRIN_VM_H +#define PICRIN_VM_H + +#if defined(__cplusplus) +extern "C" { +#endif + +enum { + OP_NOP, + OP_POP, + OP_PUSHUNDEF, + OP_PUSHNIL, + OP_PUSHTRUE, + OP_PUSHFALSE, + OP_PUSHINT, + OP_PUSHFLOAT, + OP_PUSHCHAR, + OP_PUSHEOF, + OP_PUSHCONST, + OP_GREF, + OP_GSET, + OP_LREF, + OP_LSET, + OP_CREF, + OP_CSET, + OP_JMP, + OP_JMPIF, + OP_NOT, + OP_CALL, + OP_TAILCALL, + OP_RET, + OP_LAMBDA, + OP_CONS, + OP_CAR, + OP_CDR, + OP_NILP, + OP_SYMBOLP, + OP_PAIRP, + OP_ADD, + OP_SUB, + OP_MUL, + OP_DIV, + OP_EQ, + OP_LT, + OP_LE, + OP_GT, + OP_GE, + OP_STOP +}; + +struct code { + int insn; + int a; + int b; +} + +struct list_head { + struct list_head *prev, *next; +}; + +struct irep { + struct list_head list; + unsigned refc; + int argc, localc, capturec; + bool varg; + struct code *code; + struct irep **irep; + int *ints; + double *nums; + struct object **pool; + size_t ncode, nirep, nints, nnums, npool; +}; + +void pic_irep_incref(pic_state *, struct irep *); +void pic_irep_decref(pic_state *, struct irep *); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 3028bc67..59c44fe7 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -5,7 +5,7 @@ #include "picrin.h" #include "picrin/extra.h" #include "picrin/private/object.h" -#include "picrin/private/opcode.h" +#include "picrin/private/vm.h" #include "picrin/private/state.h" #define MIN(x,y) ((x) < (y) ? (x) : (y)) @@ -856,8 +856,10 @@ pic_applyk(pic_state *pic, pic_value proc, int argc, pic_value *args) pic_callinfo *ci; int i; - PIC_INIT_CODE_I(pic->iseq[0], OP_NOP, 0); - PIC_INIT_CODE_I(pic->iseq[1], OP_TAILCALL, -1); + pic->iseq[0].insn = OP_NOP; + pic->iseq[0].a = 0; + pic->iseq[1].insn = OP_TAILCALL; + pic->iseq[1].a = -1; *pic->sp++ = proc; From 5c7c5a69cf12b04ed4d75cd67bb7b3a48893c737 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 20:13:05 +0900 Subject: [PATCH 097/119] remove debug code --- etc/mkloader.pl | 4 -- extlib/benz/eval.c | 15 +---- extlib/benz/include/picrin/config.h | 9 --- extlib/benz/include/picrin/extra.h | 5 -- extlib/benz/include/picrin/private/vm.h | 22 +++---- extlib/benz/include/picrin/setup.h | 8 --- extlib/benz/macro.c | 12 ---- extlib/benz/proc.c | 83 +------------------------ 8 files changed, 15 insertions(+), 143 deletions(-) diff --git a/etc/mkloader.pl b/etc/mkloader.pl index be2d7414..f2c2566f 100755 --- a/etc/mkloader.pl +++ b/etc/mkloader.pl @@ -60,10 +60,6 @@ EOL } print < -# define GC_STRESS 0 -# define VM_DEBUG 1 -# define GC_DEBUG 0 -# define GC_DEBUG_DETAIL 0 -#endif - /* check compatibility */ #if __STDC_VERSION__ >= 199901L diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 52a2bad2..b1e710d5 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -334,23 +334,11 @@ pic_expand(pic_state *pic, pic_value expr, pic_value env) { pic_value v, deferred; -#if DEBUG - puts("before expand:"); - pic_debug(pic, expr); - puts(""); -#endif - deferred = pic_list(pic, 1, pic_nil_value(pic)); v = expand(pic, expr, env, deferred); expand_deferred(pic, deferred, env); -#if DEBUG - puts("after expand:"); - pic_debug(pic, v); - puts(""); -#endif - return v; } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 59c44fe7..d0b168ea 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -292,15 +292,9 @@ pic_vm_tear_off(pic_state *pic) } } -#if VM_DEBUG -# define OPCODE_EXEC_HOOK pic_dump_code(c) -#else -# define OPCODE_EXEC_HOOK ((void)0) -#endif - #if PIC_DIRECT_THREADED_VM # define VM_LOOP JUMP; -# define CASE(x) L_##x: OPCODE_EXEC_HOOK; +# define CASE(x) L_##x: # define NEXT pic->ip++; JUMP; # define JUMP c = *pic->ip; goto *oplabels[c.insn]; # define VM_LOOP_END @@ -318,69 +312,6 @@ pic_vm_tear_off(pic_state *pic) #define PUSHCI() (++pic->ci) #define POPCI() (pic->ci--) -#if VM_DEBUG -# define VM_BOOT_PRINT \ - do { \ - puts("### booting VM... ###"); \ - stbase = pic->sp; \ - cibase = pic->ci; \ - } while (0) -#else -# define VM_BOOT_PRINT -#endif - -#if VM_DEBUG -# define VM_END_PRINT \ - do { \ - puts("**VM END STATE**"); \ - printf("stbase\t= %p\nsp\t= %p\n", (void *)stbase, (void *)pic->sp); \ - printf("cibase\t= %p\nci\t= %p\n", (void *)cibase, (void *)pic->ci); \ - if (stbase < pic->sp - 1) { \ - pic_value *sp; \ - printf("* stack trace:"); \ - for (sp = stbase; pic->sp != sp; ++sp) { \ - pic_debug(pic, *sp); \ - puts(""); \ - } \ - } \ - if (stbase > pic->sp - 1) { \ - puts("*** stack underflow!"); \ - } \ - } while (0) -#else -# define VM_END_PRINT -#endif - -#if VM_DEBUG -# define VM_CALL_PRINT \ - do { \ - short i; \ - puts("\n== calling proc..."); \ - printf(" proc = "); \ - pic_debug(pic, pic_obj_value(proc)); \ - puts(""); \ - printf(" argv = ("); \ - for (i = 1; i < c.u.i; ++i) { \ - if (i > 1) \ - printf(" "); \ - pic_debug(pic, pic->sp[-c.u.i + i]); \ - } \ - puts(")"); \ - if (! pic_proc_func_p(proc)) { \ - printf(" irep = %p\n", proc->u.i.irep); \ - printf(" name = %s\n", pic_str(pic, pic_sym_name(pic, pic_proc_name(proc)))); \ - pic_dump_irep(proc->u.i.irep); \ - } \ - else { \ - printf(" cfunc = %p\n", (void *)proc->u.f.func); \ - printf(" name = %s\n", pic_str(pic, pic_sym_name(pic, pic_proc_name(proc)))); \ - } \ - puts("== end\n"); \ - } while (0) -#else -# define VM_CALL_PRINT -#endif - /* for arithmetic instructions */ pic_value pic_add(pic_state *, pic_value, pic_value); pic_value pic_sub(pic_state *, pic_value, pic_value); @@ -414,19 +345,12 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) }; #endif -#if VM_DEBUG - pic_value *stbase; - pic_callinfo *cibase; -#endif - PUSH(proc); for (i = 0; i < argc; ++i) { PUSH(argv[i]); } - VM_BOOT_PRINT; - /* boot! */ boot[0].insn = OP_CALL; boot[0].a = argc + 1; @@ -568,8 +492,6 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) } proc = pic_proc_ptr(pic, x); - VM_CALL_PRINT; - if (pic->sp >= pic->stend) { pic_panic(pic, "VM stack overflow"); } @@ -841,9 +763,6 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) } CASE(OP_STOP) { - - VM_END_PRINT; - return pic_protect(pic, POP()); } } VM_LOOP_END; From 684eb6502dda14bd4d45694a6b8e29acde3b2ef3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 20:18:31 +0900 Subject: [PATCH 098/119] pic_callinfo -> struct pic_callinfo, pic_code -> struct pic_code --- contrib/10.callcc/callcc.c | 12 ++++++------ extlib/benz/cont.c | 4 ++-- extlib/benz/debug.c | 2 +- extlib/benz/eval.c | 8 ++++---- extlib/benz/gc.c | 4 ++-- extlib/benz/include/picrin/private/state.h | 16 ++++++++-------- extlib/benz/include/picrin/private/vm.h | 12 ++++++------ extlib/benz/proc.c | 22 +++++++++++----------- extlib/benz/state.c | 4 ++-- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index 3f73cdd5..b1c57627 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -16,7 +16,7 @@ struct pic_fullcont { size_t sp_offset; ptrdiff_t st_len; - pic_callinfo *ci_ptr; + struct pic_callinfo *ci_ptr; size_t ci_offset; ptrdiff_t ci_len; @@ -24,7 +24,7 @@ struct pic_fullcont { size_t xp_offset; ptrdiff_t xp_len; - pic_code *ip; + struct pic_code *ip; pic_value ptable; @@ -54,7 +54,7 @@ cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value)) struct pic_fullcont *cont = data; struct pic_checkpoint *cp; pic_value *stack; - pic_callinfo *ci; + struct pic_callinfo *ci; struct pic_proc **xp; size_t i; @@ -141,8 +141,8 @@ save_cont(pic_state *pic, struct pic_fullcont **c) cont->ci_offset = pic->ci - pic->cibase; cont->ci_len = pic->ciend - pic->cibase; - cont->ci_ptr = pic_malloc(pic, sizeof(pic_callinfo) * cont->ci_len); - memcpy(cont->ci_ptr, pic->cibase, sizeof(pic_callinfo) * cont->ci_len); + cont->ci_ptr = pic_malloc(pic, sizeof(struct pic_callinfo) * cont->ci_len); + memcpy(cont->ci_ptr, pic->cibase, sizeof(struct pic_callinfo) * cont->ci_len); cont->xp_offset = pic->xp - pic->xpbase; cont->xp_len = pic->xpend - pic->xpbase; @@ -193,7 +193,7 @@ restore_cont(pic_state *pic, struct pic_fullcont *cont) pic->stend = pic->stbase + cont->st_len; assert(pic->ciend - pic->cibase >= cont->ci_len); - memcpy(pic->cibase, cont->ci_ptr, sizeof(pic_callinfo) * cont->ci_len); + memcpy(pic->cibase, cont->ci_ptr, sizeof(struct pic_callinfo) * cont->ci_len); pic->ci = pic->cibase + cont->ci_offset; pic->ciend = pic->cibase + cont->ci_len; diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index a3d9f223..4e6545f9 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -17,7 +17,7 @@ struct pic_cont { ptrdiff_t xp_offset; size_t arena_idx; pic_value ptable; - pic_code *ip; + struct pic_code *ip; int retc; pic_value *retv; @@ -218,7 +218,7 @@ pic_valuesk(pic_state *pic, int argc, pic_value *argv) int pic_receive(pic_state *pic, int n, pic_value *argv) { - pic_callinfo *ci; + struct pic_callinfo *ci; int i, retc; /* take info from discarded frame */ diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 225433c4..80fd6d95 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -11,7 +11,7 @@ pic_value pic_get_backtrace(pic_state *pic) { size_t ai = pic_enter(pic); - pic_callinfo *ci; + struct pic_callinfo *ci; pic_value trace; trace = pic_lit_value(pic, ""); diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index dfe4f3e4..279d29aa 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -354,7 +354,7 @@ typedef struct codegen_context { pic_value rest; pic_value args, locals, captures; /* actual bit code sequence */ - pic_code *code; + struct pic_code *code; size_t clen, ccapa; /* child ireps */ struct pic_irep **irep; @@ -382,7 +382,7 @@ codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, cxt->locals = locals; cxt->captures = captures; - cxt->code = pic_calloc(pic, PIC_ISEQ_SIZE, sizeof(pic_code)); + cxt->code = pic_calloc(pic, PIC_ISEQ_SIZE, sizeof(struct pic_code)); cxt->clen = 0; cxt->ccapa = PIC_ISEQ_SIZE; @@ -417,7 +417,7 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) irep->argc = pic_vec_len(pic, cxt->args) + 1; irep->localc = pic_vec_len(pic, cxt->locals); irep->capturec = pic_vec_len(pic, cxt->captures); - irep->code = pic_realloc(pic, cxt->code, sizeof(pic_code) * cxt->clen); + irep->code = pic_realloc(pic, cxt->code, sizeof(struct pic_code) * cxt->clen); irep->irep = pic_realloc(pic, cxt->irep, sizeof(struct pic_irep *) * cxt->ilen); irep->ints = pic_realloc(pic, cxt->ints, sizeof(int) * cxt->klen); irep->nums = pic_realloc(pic, cxt->nums, sizeof(double) * cxt->flen); @@ -443,7 +443,7 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) } \ } while (0) -#define check_code_size(pic, cxt) check_size(pic, cxt, c, code, pic_code) +#define check_code_size(pic, cxt) check_size(pic, cxt, c, code, struct pic_code) #define check_irep_size(pic, cxt) check_size(pic, cxt, i, irep, struct pic_irep *) #define check_pool_size(pic, cxt) check_size(pic, cxt, p, pool, struct pic_object *) #define check_ints_size(pic, cxt) check_size(pic, cxt, k, ints, int) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index aaae14f9..985ea214 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -412,9 +412,9 @@ static void gc_mark_phase(pic_state *pic) { pic_value *stack; - pic_callinfo *ci; + struct pic_callinfo *ci; struct pic_proc **xhandler; - struct pic_list *list; + struct pic_list_head *list; int it; size_t j; diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index cf11b4d1..072de9dd 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -21,16 +21,16 @@ struct pic_lib { struct pic_dict *exports; }; -typedef struct { +struct pic_callinfo { int argc, retc; - pic_code *ip; + struct pic_code *ip; pic_value *fp; struct pic_irep *irep; struct pic_context *cxt; int regc; pic_value *regs; struct pic_context *up; -} pic_callinfo; +}; KHASH_DECLARE(oblist, struct pic_string *, struct pic_identifier *) KHASH_DECLARE(ltable, const char *, struct pic_lib) @@ -46,13 +46,13 @@ struct pic_state { pic_value *sp; pic_value *stbase, *stend; - pic_callinfo *ci; - pic_callinfo *cibase, *ciend; + struct pic_callinfo *ci; + struct pic_callinfo *cibase, *ciend; struct pic_proc **xp; struct pic_proc **xpbase, **xpend; - pic_code *ip; + struct pic_code *ip; pic_value ptable; /* list of ephemerons */ @@ -65,10 +65,10 @@ struct pic_state { pic_value globals; /* weak */ pic_value macros; /* weak */ khash_t(ltable) ltable; - struct pic_list ireps; /* chain */ + struct pic_list_head ireps; /* chain */ xFILE files[XOPEN_MAX]; - pic_code iseq[2]; /* for pic_apply_trampoline */ + struct pic_code iseq[2]; /* for pic_apply_trampoline */ bool gc_enable; struct pic_heap *heap; diff --git a/extlib/benz/include/picrin/private/vm.h b/extlib/benz/include/picrin/private/vm.h index 62155029..a51ccc95 100644 --- a/extlib/benz/include/picrin/private/vm.h +++ b/extlib/benz/include/picrin/private/vm.h @@ -52,22 +52,22 @@ enum { OP_STOP }; -typedef struct { +struct pic_code { int insn; int a; int b; -} pic_code; +}; -struct pic_list { - struct pic_list *prev, *next; +struct pic_list_head { + struct pic_list_head *prev, *next; }; struct pic_irep { - struct pic_list list; + struct pic_list_head list; unsigned refc; int argc, localc, capturec; bool varg; - pic_code *code; + struct pic_code *code; struct pic_irep **irep; int *ints; double *nums; diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index d0b168ea..7445a402 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -253,7 +253,7 @@ vm_gset(pic_state *pic, pic_value uid, pic_value value) static void vm_push_cxt(pic_state *pic) { - pic_callinfo *ci = pic->ci; + struct pic_callinfo *ci = pic->ci; ci->cxt = (struct pic_context *)pic_obj_alloc(pic, offsetof(struct pic_context, storage) + sizeof(pic_value) * ci->regc, PIC_TYPE_CXT); ci->cxt->up = ci->up; @@ -262,7 +262,7 @@ vm_push_cxt(pic_state *pic) } static void -vm_tear_off(pic_callinfo *ci) +vm_tear_off(struct pic_callinfo *ci) { struct pic_context *cxt; int i; @@ -283,7 +283,7 @@ vm_tear_off(pic_callinfo *ci) void pic_vm_tear_off(pic_state *pic) { - pic_callinfo *ci; + struct pic_callinfo *ci; for (ci = pic->ci; ci > pic->cibase; ci--) { if (ci->cxt != NULL) { @@ -326,9 +326,9 @@ bool pic_ge(pic_state *, pic_value, pic_value); pic_value pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) { - pic_code c; + struct pic_code c; size_t ai = pic_enter(pic); - pic_code boot[2]; + struct pic_code boot[2]; int i; #if PIC_DIRECT_THREADED_VM @@ -411,7 +411,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) NEXT; } CASE(OP_LREF) { - pic_callinfo *ci = pic->ci; + struct pic_callinfo *ci = pic->ci; struct pic_irep *irep = ci->irep; if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) { @@ -424,7 +424,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) NEXT; } CASE(OP_LSET) { - pic_callinfo *ci = pic->ci; + struct pic_callinfo *ci = pic->ci; struct pic_irep *irep = ci->irep; if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) { @@ -477,7 +477,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) } CASE(OP_CALL) { pic_value x, v; - pic_callinfo *ci; + struct pic_callinfo *ci; struct pic_proc *proc; if (c.a == -1) { @@ -557,7 +557,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) CASE(OP_TAILCALL) { int i, argc; pic_value *argv; - pic_callinfo *ci; + struct pic_callinfo *ci; if (pic->ci->cxt != NULL) { vm_tear_off(pic->ci); @@ -583,7 +583,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) CASE(OP_RET) { int i, retc; pic_value *retv; - pic_callinfo *ci; + struct pic_callinfo *ci; if (pic->ci->cxt != NULL) { vm_tear_off(pic->ci); @@ -772,7 +772,7 @@ pic_value pic_applyk(pic_state *pic, pic_value proc, int argc, pic_value *args) { pic_value *sp; - pic_callinfo *ci; + struct pic_callinfo *ci; int i; pic->iseq[0].insn = OP_NOP; diff --git a/extlib/benz/state.c b/extlib/benz/state.c index b8d975d4..3cbeb95d 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -219,7 +219,7 @@ pic_open(pic_allocf allocf, void *userdata) } /* callinfo */ - pic->cibase = pic->ci = allocf(userdata, NULL, PIC_STACK_SIZE * sizeof(pic_callinfo)); + pic->cibase = pic->ci = allocf(userdata, NULL, PIC_STACK_SIZE * sizeof(struct pic_callinfo)); pic->ciend = pic->cibase + PIC_STACK_SIZE; if (! pic->ci) { @@ -356,7 +356,7 @@ pic_close(pic_state *pic) { /* FIXME */ int i = 0; - struct pic_list *list; + struct pic_list_head *list; for (list = pic->ireps.next; list != &pic->ireps; list = list->next) { i++; } From bfe6cef4c8bd6abebcd8b01e044dd3d740bf11bd Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 20:21:02 +0900 Subject: [PATCH 099/119] pic_sym -> symbol, pic_id -> identifier --- extlib/benz/bool.c | 2 +- extlib/benz/dict.c | 2 +- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin/private/object.h | 14 +++++++------- extlib/benz/macro.c | 2 +- extlib/benz/symbol.c | 10 +++++----- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 35ec40d3..09ea02a2 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -94,7 +94,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) switch (pic_type(pic, x)) { case PIC_TYPE_ID: { - pic_id *id1, *id2; + identifier *id1, *id2; pic_value s1, s2; id1 = pic_id_ptr(pic, x); diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index cfbf73ff..a4b209c4 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -6,7 +6,7 @@ #include "picrin/extra.h" #include "picrin/private/object.h" -KHASH_DEFINE(dict, pic_sym *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) +KHASH_DEFINE(dict, symbol *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) pic_value pic_make_dict(pic_state *pic) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 985ea214..b5030ae0 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -620,7 +620,7 @@ gc_sweep_phase(pic_state *pic) int it; khash_t(weak) *h; khash_t(oblist) *s = &pic->oblist; - pic_sym *sym; + symbol *sym; struct pic_object *obj; size_t total = 0, inuse = 0; diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index 6f50bdfb..e6e2035c 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -11,11 +11,11 @@ extern "C" { #include "picrin/private/khash.h" -typedef struct pic_identifier pic_id; -typedef pic_id pic_sym; +typedef struct pic_identifier identifier; +typedef identifier symbol; -KHASH_DECLARE(env, pic_id *, pic_sym *) -KHASH_DECLARE(dict, pic_sym *, pic_value) +KHASH_DECLARE(env, identifier *, symbol *) +KHASH_DECLARE(dict, symbol *, pic_value) KHASH_DECLARE(weak, struct pic_object *, pic_value) #define PIC_OBJECT_HEADER \ @@ -119,7 +119,7 @@ struct pic_record { struct pic_error { PIC_OBJECT_HEADER - pic_sym *type; + symbol *type; struct pic_string *msg; pic_value irrs; struct pic_string *stack; @@ -140,8 +140,8 @@ struct pic_checkpoint { struct pic_object *pic_obj_ptr(pic_value); -#define pic_id_ptr(pic, o) (assert(pic_id_p(pic, o)), (pic_id *)pic_obj_ptr(o)) -#define pic_sym_ptr(pic, o) (assert(pic_sym_p(pic, o)), (pic_sym *)pic_obj_ptr(o)) +#define pic_id_ptr(pic, o) (assert(pic_id_p(pic, o)), (identifier *)pic_obj_ptr(o)) +#define pic_sym_ptr(pic, o) (assert(pic_sym_p(pic, o)), (symbol *)pic_obj_ptr(o)) #define pic_str_ptr(pic, o) (assert(pic_str_p(pic, o)), (struct pic_string *)pic_obj_ptr(o)) #define pic_blob_ptr(pic, o) (assert(pic_blob_p(pic, o)), (struct pic_blob *)pic_obj_ptr(o)) #define pic_pair_ptr(pic, o) (assert(pic_pair_p(pic, o)), (struct pic_pair *)pic_obj_ptr(o)) diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index b1e710d5..8028f6cf 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -7,7 +7,7 @@ #include "picrin/private/object.h" #include "picrin/private/state.h" -KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal) +KHASH_DEFINE(env, identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal) pic_value pic_make_env(pic_state *pic, pic_value up) diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 8ebd3913..3f40ef43 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -10,13 +10,13 @@ #define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0) -KHASH_DEFINE(oblist, struct pic_string *, pic_sym *, kh_pic_str_hash, kh_pic_str_cmp) +KHASH_DEFINE(oblist, struct pic_string *, symbol *, kh_pic_str_hash, kh_pic_str_cmp) pic_value pic_intern(pic_state *pic, pic_value str) { khash_t(oblist) *h = &pic->oblist; - pic_sym *sym; + symbol *sym; int it; int ret; @@ -29,7 +29,7 @@ pic_intern(pic_state *pic, pic_value str) kh_val(h, it) = NULL; /* dummy */ - sym = (pic_sym *)pic_obj_alloc(pic, offsetof(pic_sym, env), PIC_TYPE_SYMBOL); + sym = (symbol *)pic_obj_alloc(pic, offsetof(symbol, env), PIC_TYPE_SYMBOL); sym->u.str = pic_str_ptr(pic, str); kh_val(h, it) = sym; @@ -39,9 +39,9 @@ pic_intern(pic_state *pic, pic_value str) pic_value pic_make_identifier(pic_state *pic, pic_value base, pic_value env) { - pic_id *id; + identifier *id; - id = (pic_id *)pic_obj_alloc(pic, sizeof(pic_id), PIC_TYPE_ID); + id = (identifier *)pic_obj_alloc(pic, sizeof(identifier), PIC_TYPE_ID); id->u.id = pic_id_ptr(pic, base); id->env = pic_env_ptr(pic, env); From 608569e87624c3639b81ea63a1b57a7385a1ca57 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 20:32:00 +0900 Subject: [PATCH 100/119] drop pic_ prefix of structs --- contrib/10.callcc/callcc.c | 60 +++++----- extlib/benz/blob.c | 4 +- extlib/benz/bool.c | 2 +- extlib/benz/cont.c | 12 +- extlib/benz/data.c | 4 +- extlib/benz/debug.c | 4 +- extlib/benz/dict.c | 4 +- extlib/benz/error.c | 6 +- extlib/benz/eval.c | 36 +++--- extlib/benz/gc.c | 124 ++++++++++---------- extlib/benz/include/picrin/private/gc.h | 4 +- extlib/benz/include/picrin/private/object.h | 109 +++++++++-------- extlib/benz/include/picrin/private/state.h | 44 +++---- extlib/benz/include/picrin/private/vm.h | 20 ++-- extlib/benz/lib.c | 18 +-- extlib/benz/macro.c | 10 +- extlib/benz/pair.c | 4 +- extlib/benz/port.c | 4 +- extlib/benz/proc.c | 56 ++++----- extlib/benz/record.c | 4 +- extlib/benz/state.c | 10 +- extlib/benz/string.c | 72 ++++++------ extlib/benz/symbol.c | 6 +- extlib/benz/value.c | 10 +- extlib/benz/vector.c | 4 +- extlib/benz/weak.c | 6 +- 26 files changed, 318 insertions(+), 319 deletions(-) diff --git a/contrib/10.callcc/callcc.c b/contrib/10.callcc/callcc.c index b1c57627..8664354c 100644 --- a/contrib/10.callcc/callcc.c +++ b/contrib/10.callcc/callcc.c @@ -2,12 +2,12 @@ #include "picrin/private/object.h" #include "picrin/private/state.h" -struct pic_fullcont { +struct fullcont { jmp_buf jmp; struct pic_cont *prev_jmp; - struct pic_checkpoint *cp; + struct checkpoint *cp; char *stk_pos, *stk_ptr; ptrdiff_t stk_len; @@ -16,19 +16,19 @@ struct pic_fullcont { size_t sp_offset; ptrdiff_t st_len; - struct pic_callinfo *ci_ptr; + struct callinfo *ci_ptr; size_t ci_offset; ptrdiff_t ci_len; - struct pic_proc **xp_ptr; + struct proc **xp_ptr; size_t xp_offset; ptrdiff_t xp_len; - struct pic_code *ip; + struct code *ip; pic_value ptable; - struct pic_object **arena; + struct object **arena; size_t arena_size, arena_idx; int retc; @@ -38,7 +38,7 @@ struct pic_fullcont { static void cont_dtor(pic_state *pic, void *data) { - struct pic_fullcont *cont = data; + struct fullcont *cont = data; pic_free(pic, cont->stk_ptr); pic_free(pic, cont->st_ptr); @@ -51,11 +51,11 @@ cont_dtor(pic_state *pic, void *data) static void cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value)) { - struct pic_fullcont *cont = data; - struct pic_checkpoint *cp; + struct fullcont *cont = data; + struct checkpoint *cp; pic_value *stack; - struct pic_callinfo *ci; - struct pic_proc **xp; + struct callinfo *ci; + struct proc **xp; size_t i; /* checkpoint */ @@ -96,8 +96,8 @@ cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value)) static const pic_data_type cont_type = { "continuation", cont_dtor, cont_mark }; -static void save_cont(pic_state *, struct pic_fullcont **); -static void restore_cont(pic_state *, struct pic_fullcont *); +static void save_cont(pic_state *, struct fullcont **); +static void restore_cont(pic_state *, struct fullcont *); static ptrdiff_t native_stack_length(pic_state *pic, char **pos) @@ -114,15 +114,15 @@ native_stack_length(pic_state *pic, char **pos) } static void -save_cont(pic_state *pic, struct pic_fullcont **c) +save_cont(pic_state *pic, struct fullcont **c) { void pic_vm_tear_off(pic_state *); - struct pic_fullcont *cont; + struct fullcont *cont; char *pos; pic_vm_tear_off(pic); /* tear off */ - cont = *c = pic_malloc(pic, sizeof(struct pic_fullcont)); + cont = *c = pic_malloc(pic, sizeof(struct fullcont)); cont->prev_jmp = pic->cc; @@ -141,13 +141,13 @@ save_cont(pic_state *pic, struct pic_fullcont **c) cont->ci_offset = pic->ci - pic->cibase; cont->ci_len = pic->ciend - pic->cibase; - cont->ci_ptr = pic_malloc(pic, sizeof(struct pic_callinfo) * cont->ci_len); - memcpy(cont->ci_ptr, pic->cibase, sizeof(struct pic_callinfo) * cont->ci_len); + cont->ci_ptr = pic_malloc(pic, sizeof(struct callinfo) * cont->ci_len); + memcpy(cont->ci_ptr, pic->cibase, sizeof(struct callinfo) * cont->ci_len); cont->xp_offset = pic->xp - pic->xpbase; cont->xp_len = pic->xpend - pic->xpbase; - cont->xp_ptr = pic_malloc(pic, sizeof(struct pic_proc *) * cont->xp_len); - memcpy(cont->xp_ptr, pic->xpbase, sizeof(struct pic_proc *) * cont->xp_len); + cont->xp_ptr = pic_malloc(pic, sizeof(struct proc *) * cont->xp_len); + memcpy(cont->xp_ptr, pic->xpbase, sizeof(struct proc *) * cont->xp_len); cont->ip = pic->ip; @@ -155,15 +155,15 @@ save_cont(pic_state *pic, struct pic_fullcont **c) cont->arena_idx = pic->arena_idx; cont->arena_size = pic->arena_size; - cont->arena = pic_malloc(pic, sizeof(struct pic_object *) * pic->arena_size); - memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * pic->arena_size); + cont->arena = pic_malloc(pic, sizeof(struct object *) * pic->arena_size); + memcpy(cont->arena, pic->arena, sizeof(struct object *) * pic->arena_size); cont->retc = 0; cont->retv = NULL; } static void -native_stack_extend(pic_state *pic, struct pic_fullcont *cont) +native_stack_extend(pic_state *pic, struct fullcont *cont) { volatile pic_value v[1024]; @@ -172,10 +172,10 @@ native_stack_extend(pic_state *pic, struct pic_fullcont *cont) } PIC_NORETURN static void -restore_cont(pic_state *pic, struct pic_fullcont *cont) +restore_cont(pic_state *pic, struct fullcont *cont) { char v; - struct pic_fullcont *tmp = cont; + struct fullcont *tmp = cont; if (&v < pic->native_stack_start) { if (&v > cont->stk_pos) native_stack_extend(pic, cont); @@ -193,12 +193,12 @@ restore_cont(pic_state *pic, struct pic_fullcont *cont) pic->stend = pic->stbase + cont->st_len; assert(pic->ciend - pic->cibase >= cont->ci_len); - memcpy(pic->cibase, cont->ci_ptr, sizeof(struct pic_callinfo) * cont->ci_len); + memcpy(pic->cibase, cont->ci_ptr, sizeof(struct callinfo) * cont->ci_len); pic->ci = pic->cibase + cont->ci_offset; pic->ciend = pic->cibase + cont->ci_len; assert(pic->xpend - pic->xpbase >= cont->xp_len); - memcpy(pic->xpbase, cont->xp_ptr, sizeof(struct pic_proc *) * cont->xp_len); + memcpy(pic->xpbase, cont->xp_ptr, sizeof(struct proc *) * cont->xp_len); pic->xp = pic->xpbase + cont->xp_offset; pic->xpend = pic->xpbase + cont->xp_len; @@ -207,7 +207,7 @@ restore_cont(pic_state *pic, struct pic_fullcont *cont) pic->ptable = cont->ptable; assert(pic->arena_size >= cont->arena_size); - memcpy(pic->arena, cont->arena, sizeof(struct pic_object *) * cont->arena_size); + memcpy(pic->arena, cont->arena, sizeof(struct object *) * cont->arena_size); pic->arena_size = cont->arena_size; pic->arena_idx = cont->arena_idx; @@ -221,7 +221,7 @@ cont_call(pic_state *pic) { int argc, i; pic_value *argv, *retv; - struct pic_fullcont *cont; + struct fullcont *cont; pic_get_args(pic, "*", &argc, &argv); @@ -243,7 +243,7 @@ cont_call(pic_state *pic) static pic_value pic_callcc(pic_state *pic, pic_value proc) { - struct pic_fullcont *cont; + struct fullcont *cont; save_cont(pic, &cont); if (setjmp(cont->jmp)) { diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index 292ea083..3747d66e 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -9,9 +9,9 @@ pic_value pic_blob_value(pic_state *pic, const unsigned char *buf, int len) { - struct pic_blob *bv; + struct blob *bv; - bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TYPE_BLOB); + bv = (struct blob *)pic_obj_alloc(pic, sizeof(struct blob), PIC_TYPE_BLOB); bv->data = pic_malloc(pic, len); bv->len = len; if (buf) { diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 09ea02a2..9de94abc 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -94,7 +94,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) switch (pic_type(pic, x)) { case PIC_TYPE_ID: { - identifier *id1, *id2; + struct identifier *id1, *id2; pic_value s1, s2; id1 = pic_id_ptr(pic, x); diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 4e6545f9..0149e00f 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -11,13 +11,13 @@ struct pic_cont { int id; - struct pic_checkpoint *cp; + struct checkpoint *cp; ptrdiff_t sp_offset; ptrdiff_t ci_offset; ptrdiff_t xp_offset; size_t arena_idx; pic_value ptable; - struct pic_code *ip; + struct code *ip; int retc; pic_value *retv; @@ -71,7 +71,7 @@ pic_exit_point(pic_state *pic) } void -pic_wind(pic_state *pic, struct pic_checkpoint *here, struct pic_checkpoint *there) +pic_wind(pic_state *pic, struct checkpoint *here, struct checkpoint *there) { if (here == there) return; @@ -89,13 +89,13 @@ pic_wind(pic_state *pic, struct pic_checkpoint *here, struct pic_checkpoint *the static pic_value pic_dynamic_wind(pic_state *pic, pic_value in, pic_value thunk, pic_value out) { - struct pic_checkpoint *here; + struct checkpoint *here; pic_value val; pic_call(pic, in, 0); /* enter */ here = pic->cp; - pic->cp = (struct pic_checkpoint *)pic_obj_alloc(pic, sizeof(struct pic_checkpoint), PIC_TYPE_CP); + pic->cp = (struct checkpoint *)pic_obj_alloc(pic, sizeof(struct checkpoint), PIC_TYPE_CP); pic->cp->prev = here; pic->cp->depth = here->depth + 1; pic->cp->in = pic_proc_ptr(pic, in); @@ -218,7 +218,7 @@ pic_valuesk(pic_state *pic, int argc, pic_value *argv) int pic_receive(pic_state *pic, int n, pic_value *argv) { - struct pic_callinfo *ci; + struct callinfo *ci; int i, retc; /* take info from discarded frame */ diff --git a/extlib/benz/data.c b/extlib/benz/data.c index 4ba3cb71..300bb162 100644 --- a/extlib/benz/data.c +++ b/extlib/benz/data.c @@ -19,9 +19,9 @@ pic_data(pic_state *PIC_UNUSED(pic), pic_value data) pic_value pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type) { - struct pic_data *data; + struct data *data; - data = (struct pic_data *)pic_obj_alloc(pic, sizeof(struct pic_data), PIC_TYPE_DATA); + data = (struct data *)pic_obj_alloc(pic, sizeof(struct data), PIC_TYPE_DATA); data->type = type; data->data = userdata; diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 80fd6d95..189f95dc 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -11,7 +11,7 @@ pic_value pic_get_backtrace(pic_state *pic) { size_t ai = pic_enter(pic); - struct pic_callinfo *ci; + struct callinfo *ci; pic_value trace; trace = pic_lit_value(pic, ""); @@ -46,7 +46,7 @@ pic_print_backtrace(pic_state *pic, xFILE *file) xfprintf(pic, file, "raise: "); pic_fwrite(pic, err, file); } else { - struct pic_error *e; + struct error *e; pic_value elem, it; e = pic_error_ptr(pic, err); diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index a4b209c4..47a92fd6 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -11,9 +11,9 @@ KHASH_DEFINE(dict, symbol *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) pic_value pic_make_dict(pic_state *pic) { - struct pic_dict *dict; + struct dict *dict; - dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TYPE_DICT); + dict = (struct dict *)pic_obj_alloc(pic, sizeof(struct dict), PIC_TYPE_DICT); kh_init(dict, &dict->hash); return pic_obj_value(dict); } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 24acabfb..56a0c39b 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -83,7 +83,7 @@ pic_push_handler(pic_state *pic, pic_value handler) if (pic->xp >= pic->xpend) { xp_len = (size_t)(pic->xpend - pic->xpbase) * 2; xp_offset = pic->xp - pic->xpbase; - pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len); + pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct proc *) * xp_len); pic->xp = pic->xpbase + xp_offset; pic->xpend = pic->xpbase + xp_len; } @@ -110,12 +110,12 @@ pic_err(pic_state *pic) pic_value pic_make_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) { - struct pic_error *e; + struct error *e; pic_value stack, ty = pic_intern_cstr(pic, type); stack = pic_get_backtrace(pic); - e = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR); + e = (struct error *)pic_obj_alloc(pic, sizeof(struct error), PIC_TYPE_ERROR); e->type = pic_sym_ptr(pic, ty); e->msg = pic_str_ptr(pic, pic_cstr_value(pic, msg)); e->irrs = irrs; diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 279d29aa..1717ae36 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -354,17 +354,17 @@ typedef struct codegen_context { pic_value rest; pic_value args, locals, captures; /* actual bit code sequence */ - struct pic_code *code; + struct code *code; size_t clen, ccapa; /* child ireps */ - struct pic_irep **irep; + struct irep **irep; size_t ilen, icapa; /* constant object pool */ int *ints; size_t klen, kcapa; double *nums; size_t flen, fcapa; - struct pic_object **pool; + struct object **pool; size_t plen, pcapa; struct codegen_context *up; @@ -382,15 +382,15 @@ codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, cxt->locals = locals; cxt->captures = captures; - cxt->code = pic_calloc(pic, PIC_ISEQ_SIZE, sizeof(struct pic_code)); + cxt->code = pic_calloc(pic, PIC_ISEQ_SIZE, sizeof(struct code)); cxt->clen = 0; cxt->ccapa = PIC_ISEQ_SIZE; - cxt->irep = pic_calloc(pic, PIC_IREP_SIZE, sizeof(struct pic_irep *)); + cxt->irep = pic_calloc(pic, PIC_IREP_SIZE, sizeof(struct irep *)); cxt->ilen = 0; cxt->icapa = PIC_IREP_SIZE; - cxt->pool = pic_calloc(pic, PIC_POOL_SIZE, sizeof(struct pic_object *)); + cxt->pool = pic_calloc(pic, PIC_POOL_SIZE, sizeof(struct object *)); cxt->plen = 0; cxt->pcapa = PIC_POOL_SIZE; @@ -405,23 +405,23 @@ codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, create_activation(pic, cxt); } -static struct pic_irep * +static struct irep * codegen_context_destroy(pic_state *pic, codegen_context *cxt) { - struct pic_irep *irep; + struct irep *irep; /* create irep */ - irep = pic_malloc(pic, sizeof(struct pic_irep)); + irep = pic_malloc(pic, sizeof(struct irep)); irep->refc = 1; irep->varg = pic_sym_p(pic, cxt->rest); irep->argc = pic_vec_len(pic, cxt->args) + 1; irep->localc = pic_vec_len(pic, cxt->locals); irep->capturec = pic_vec_len(pic, cxt->captures); - irep->code = pic_realloc(pic, cxt->code, sizeof(struct pic_code) * cxt->clen); - irep->irep = pic_realloc(pic, cxt->irep, sizeof(struct pic_irep *) * cxt->ilen); + irep->code = pic_realloc(pic, cxt->code, sizeof(struct code) * cxt->clen); + irep->irep = pic_realloc(pic, cxt->irep, sizeof(struct irep *) * cxt->ilen); irep->ints = pic_realloc(pic, cxt->ints, sizeof(int) * cxt->klen); irep->nums = pic_realloc(pic, cxt->nums, sizeof(double) * cxt->flen); - irep->pool = pic_realloc(pic, cxt->pool, sizeof(struct pic_object *) * cxt->plen); + irep->pool = pic_realloc(pic, cxt->pool, sizeof(struct object *) * cxt->plen); irep->ncode = cxt->clen; irep->nirep = cxt->ilen; irep->nints = cxt->klen; @@ -443,9 +443,9 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) } \ } while (0) -#define check_code_size(pic, cxt) check_size(pic, cxt, c, code, struct pic_code) -#define check_irep_size(pic, cxt) check_size(pic, cxt, i, irep, struct pic_irep *) -#define check_pool_size(pic, cxt) check_size(pic, cxt, p, pool, struct pic_object *) +#define check_code_size(pic, cxt) check_size(pic, cxt, c, code, struct code) +#define check_irep_size(pic, cxt) check_size(pic, cxt, i, irep, struct irep *) +#define check_pool_size(pic, cxt) check_size(pic, cxt, p, pool, struct object *) #define check_ints_size(pic, cxt) check_size(pic, cxt, k, ints, int) #define check_nums_size(pic, cxt) check_size(pic, cxt, f, nums, double) @@ -513,7 +513,7 @@ index_global(pic_state *pic, codegen_context *cxt, pic_value name) check_pool_size(pic, cxt); pidx = (int)cxt->plen++; - cxt->pool[pidx] = (struct pic_object *)pic_sym_ptr(pic, name); + cxt->pool[pidx] = (struct object *)pic_sym_ptr(pic, name); return pidx; } @@ -804,7 +804,7 @@ codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) } } -static struct pic_irep * +static struct irep * pic_codegen(pic_state *pic, pic_value obj) { pic_value empty = pic_make_vec(pic, 0, NULL); @@ -822,7 +822,7 @@ pic_codegen(pic_state *pic, pic_value obj) pic_value pic_compile(pic_state *pic, pic_value obj) { - struct pic_irep *irep; + struct irep *irep; pic_value proc; size_t ai = pic_enter(pic); diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index b5030ae0..00a34de5 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -23,39 +23,39 @@ struct heap_page { struct heap_page *next; }; -struct pic_object { +struct object { union { - struct pic_basic basic; - struct pic_identifier id; - struct pic_string str; - struct pic_blob blob; - struct pic_pair pair; - struct pic_vector vec; - struct pic_dict dict; - struct pic_weak weak; - struct pic_data data; - struct pic_record rec; - struct pic_env env; - struct pic_proc proc; - struct pic_context cxt; - struct pic_port port; - struct pic_error err; - struct pic_checkpoint cp; + struct basic basic; + struct identifier id; + struct string str; + struct blob blob; + struct pair pair; + struct vector vec; + struct dict dict; + struct weak weak; + struct data data; + struct record rec; + struct env env; + struct proc proc; + struct context cxt; + struct port port; + struct error err; + struct checkpoint cp; } u; }; -struct pic_heap { +struct heap { union header base, *freep; struct heap_page *pages; - struct pic_weak *weaks; /* weak map chain */ + struct weak *weaks; /* weak map chain */ }; -struct pic_heap * +struct heap * pic_heap_open(pic_state *pic) { - struct pic_heap *heap; + struct heap *heap; - heap = pic_malloc(pic, sizeof(struct pic_heap)); + heap = pic_malloc(pic, sizeof(struct heap)); heap->base.s.ptr = &heap->base; heap->base.s.size = 0; /* not 1, since it must never be used for allocation */ @@ -69,7 +69,7 @@ pic_heap_open(pic_state *pic) } void -pic_heap_close(pic_state *pic, struct pic_heap *heap) +pic_heap_close(pic_state *pic, struct heap *heap) { struct heap_page *page; @@ -137,11 +137,11 @@ pic_free(pic_state *pic, void *ptr) } static void -gc_protect(pic_state *pic, struct pic_object *obj) +gc_protect(pic_state *pic, struct object *obj) { if (pic->arena_idx >= pic->arena_size) { pic->arena_size = pic->arena_size * 2 + 1; - pic->arena = pic_realloc(pic, pic->arena, sizeof(struct pic_object *) * pic->arena_size); + pic->arena = pic_realloc(pic, pic->arena, sizeof(struct object *) * pic->arena_size); } pic->arena[pic->arena_idx++] = obj; } @@ -259,7 +259,7 @@ heap_morecore(pic_state *pic) /* MARK */ -static void gc_mark_object(pic_state *, struct pic_object *); +static void gc_mark_object(pic_state *, struct object *); static void gc_mark(pic_state *pic, pic_value v) @@ -271,7 +271,7 @@ gc_mark(pic_state *pic, pic_value v) } static void -gc_mark_object(pic_state *pic, struct pic_object *obj) +gc_mark_object(pic_state *pic, struct object *obj) { loop: @@ -280,7 +280,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) obj->u.basic.gc_mark = BLACK; -#define LOOP(o) obj = (struct pic_object *)(o); goto loop +#define LOOP(o) obj = (struct object *)(o); goto loop switch (obj->u.basic.tt) { case PIC_TYPE_PAIR: { @@ -318,8 +318,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TYPE_ERROR: { - gc_mark_object(pic, (struct pic_object *)obj->u.err.type); - gc_mark_object(pic, (struct pic_object *)obj->u.err.msg); + gc_mark_object(pic, (struct object *)obj->u.err.type); + gc_mark_object(pic, (struct object *)obj->u.err.msg); gc_mark(pic, obj->u.err.irrs); LOOP(obj->u.err.stack); break; @@ -338,7 +338,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TYPE_ID: { - gc_mark_object(pic, (struct pic_object *)obj->u.id.u.id); + gc_mark_object(pic, (struct object *)obj->u.id.u.id); LOOP(obj->u.id.env); break; } @@ -348,8 +348,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) for (it = kh_begin(h); it != kh_end(h); ++it) { if (kh_exist(h, it)) { - gc_mark_object(pic, (struct pic_object *)kh_key(h, it)); - gc_mark_object(pic, (struct pic_object *)kh_val(h, it)); + gc_mark_object(pic, (struct object *)kh_key(h, it)); + gc_mark_object(pic, (struct object *)kh_val(h, it)); } } if (obj->u.env.up) { @@ -385,7 +385,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) break; } case PIC_TYPE_WEAK: { - struct pic_weak *weak = (struct pic_weak *)obj; + struct weak *weak = (struct weak *)obj; weak->prev = pic->heap->weaks; pic->heap->weaks = weak; @@ -393,13 +393,13 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } case PIC_TYPE_CP: { if (obj->u.cp.prev) { - gc_mark_object(pic, (struct pic_object *)obj->u.cp.prev); + gc_mark_object(pic, (struct object *)obj->u.cp.prev); } if (obj->u.cp.in) { - gc_mark_object(pic, (struct pic_object *)obj->u.cp.in); + gc_mark_object(pic, (struct object *)obj->u.cp.in); } if (obj->u.cp.out) { - LOOP((struct pic_object *)obj->u.cp.out); + LOOP((struct object *)obj->u.cp.out); } break; } @@ -412,9 +412,9 @@ static void gc_mark_phase(pic_state *pic) { pic_value *stack; - struct pic_callinfo *ci; - struct pic_proc **xhandler; - struct pic_list_head *list; + struct callinfo *ci; + struct proc **xhandler; + struct list_head *list; int it; size_t j; @@ -422,7 +422,7 @@ gc_mark_phase(pic_state *pic) /* checkpoint */ if (pic->cp) { - gc_mark_object(pic, (struct pic_object *)pic->cp); + gc_mark_object(pic, (struct object *)pic->cp); } /* stack */ @@ -433,23 +433,23 @@ gc_mark_phase(pic_state *pic) /* callinfo */ for (ci = pic->ci; ci != pic->cibase; --ci) { if (ci->cxt) { - gc_mark_object(pic, (struct pic_object *)ci->cxt); + gc_mark_object(pic, (struct object *)ci->cxt); } } /* exception handlers */ for (xhandler = pic->xpbase; xhandler != pic->xp; ++xhandler) { - gc_mark_object(pic, (struct pic_object *)*xhandler); + gc_mark_object(pic, (struct object *)*xhandler); } /* arena */ for (j = 0; j < pic->arena_idx; ++j) { - gc_mark_object(pic, (struct pic_object *)pic->arena[j]); + gc_mark_object(pic, (struct object *)pic->arena[j]); } /* ireps */ for (list = pic->ireps.next; list != &pic->ireps; list = list->next) { - struct pic_irep *irep = (struct pic_irep *)list; + struct irep *irep = (struct irep *)list; for (j = 0; j < irep->npool; ++j) { gc_mark_object(pic, irep->pool[j]); } @@ -475,18 +475,18 @@ gc_mark_phase(pic_state *pic) if (! kh_exist(&pic->ltable, it)) { continue; } - gc_mark_object(pic, (struct pic_object *)kh_val(&pic->ltable, it).name); - gc_mark_object(pic, (struct pic_object *)kh_val(&pic->ltable, it).env); - gc_mark_object(pic, (struct pic_object *)kh_val(&pic->ltable, it).exports); + gc_mark_object(pic, (struct object *)kh_val(&pic->ltable, it).name); + gc_mark_object(pic, (struct object *)kh_val(&pic->ltable, it).env); + gc_mark_object(pic, (struct object *)kh_val(&pic->ltable, it).exports); } /* weak maps */ do { - struct pic_object *key; + struct object *key; pic_value val; int it; khash_t(weak) *h; - struct pic_weak *weak; + struct weak *weak; j = 0; weak = pic->heap->weaks; @@ -513,7 +513,7 @@ gc_mark_phase(pic_state *pic) /* SWEEP */ static void -gc_finalize_object(pic_state *pic, struct pic_object *obj) +gc_finalize_object(pic_state *pic, struct object *obj) { switch (obj->u.basic.tt) { case PIC_TYPE_VECTOR: { @@ -575,7 +575,7 @@ static size_t gc_sweep_page(pic_state *pic, struct heap_page *page) { union header *bp, *p, *head = NULL, *tail = NULL; - struct pic_object *obj; + struct object *obj; size_t alive = 0; for (bp = page->basep; ; bp = bp->s.ptr) { @@ -584,7 +584,7 @@ gc_sweep_page(pic_state *pic, struct heap_page *page) if (p < page->basep || page->endp <= p) { goto escape; } - obj = (struct pic_object *)(p + 1); + obj = (struct object *)(p + 1); if (obj->u.basic.gc_mark == BLACK) { obj->u.basic.gc_mark = WHITE; alive += p->s.size; @@ -606,7 +606,7 @@ gc_sweep_page(pic_state *pic, struct heap_page *page) while (head != NULL) { p = head; head = head->s.ptr; - gc_finalize_object(pic, (struct pic_object *)(p + 1)); + gc_finalize_object(pic, (struct object *)(p + 1)); heap_free(pic, p + 1); } @@ -621,7 +621,7 @@ gc_sweep_phase(pic_state *pic) khash_t(weak) *h; khash_t(oblist) *s = &pic->oblist; symbol *sym; - struct pic_object *obj; + struct object *obj; size_t total = 0, inuse = 0; /* weak maps */ @@ -680,22 +680,22 @@ pic_alloca(pic_state *pic, size_t n) return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t)); } -struct pic_object * +struct object * pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) { - struct pic_object *obj; + struct object *obj; #if GC_STRESS pic_gc(pic); #endif - obj = (struct pic_object *)heap_alloc(pic, size); + obj = (struct object *)heap_alloc(pic, size); if (obj == NULL) { pic_gc(pic); - obj = (struct pic_object *)heap_alloc(pic, size); + obj = (struct object *)heap_alloc(pic, size); if (obj == NULL) { heap_morecore(pic); - obj = (struct pic_object *)heap_alloc(pic, size); + obj = (struct object *)heap_alloc(pic, size); if (obj == NULL) pic_panic(pic, "GC memory exhausted"); } @@ -706,10 +706,10 @@ pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) return obj; } -struct pic_object * +struct object * pic_obj_alloc(pic_state *pic, size_t size, int type) { - struct pic_object *obj; + struct object *obj; obj = pic_obj_alloc_unsafe(pic, size, type); diff --git a/extlib/benz/include/picrin/private/gc.h b/extlib/benz/include/picrin/private/gc.h index 4e97e4d0..183cc6f8 100644 --- a/extlib/benz/include/picrin/private/gc.h +++ b/extlib/benz/include/picrin/private/gc.h @@ -9,8 +9,8 @@ extern "C" { #endif -struct pic_heap *pic_heap_open(pic_state *); -void pic_heap_close(pic_state *, struct pic_heap *); +struct heap *pic_heap_open(pic_state *); +void pic_heap_close(pic_state *, struct heap *); #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index e6e2035c..39751196 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -11,88 +11,87 @@ extern "C" { #include "picrin/private/khash.h" -typedef struct pic_identifier identifier; -typedef identifier symbol; +typedef struct identifier symbol; -KHASH_DECLARE(env, identifier *, symbol *) +KHASH_DECLARE(env, struct identifier *, symbol *) KHASH_DECLARE(dict, symbol *, pic_value) -KHASH_DECLARE(weak, struct pic_object *, pic_value) +KHASH_DECLARE(weak, struct object *, pic_value) #define PIC_OBJECT_HEADER \ unsigned char tt; \ char gc_mark; -struct pic_object; /* defined in gc.c */ +struct object; /* defined in gc.c */ -struct pic_basic { +struct basic { PIC_OBJECT_HEADER }; -struct pic_identifier { +struct identifier { PIC_OBJECT_HEADER union { - struct pic_string *str; - struct pic_identifier *id; + struct string *str; + struct identifier *id; } u; - struct pic_env *env; + struct env *env; }; -struct pic_env { +struct env { PIC_OBJECT_HEADER khash_t(env) map; - struct pic_env *up; - struct pic_string *lib; + struct env *up; + struct string *lib; }; -struct pic_pair { +struct pair { PIC_OBJECT_HEADER pic_value car; pic_value cdr; }; -struct pic_blob { +struct blob { PIC_OBJECT_HEADER unsigned char *data; int len; }; -struct pic_string { +struct string { PIC_OBJECT_HEADER - struct pic_rope *rope; + struct rope *rope; }; -struct pic_dict { +struct dict { PIC_OBJECT_HEADER khash_t(dict) hash; }; -struct pic_weak { +struct weak { PIC_OBJECT_HEADER khash_t(weak) hash; - struct pic_weak *prev; /* for GC */ + struct weak *prev; /* for GC */ }; -struct pic_vector { +struct vector { PIC_OBJECT_HEADER pic_value *data; int len; }; -struct pic_data { +struct data { PIC_OBJECT_HEADER const pic_data_type *type; void *data; }; -struct pic_context { +struct context { PIC_OBJECT_HEADER pic_value *regs; int regc; - struct pic_context *up; + struct context *up; pic_value storage[1]; }; -struct pic_proc { +struct proc { PIC_OBJECT_HEADER enum { PIC_PROC_TAG_IREP, @@ -104,56 +103,56 @@ struct pic_proc { int localc; } f; struct { - struct pic_irep *irep; - struct pic_context *cxt; + struct irep *irep; + struct context *cxt; } i; } u; pic_value locals[1]; }; -struct pic_record { +struct record { PIC_OBJECT_HEADER pic_value type; pic_value datum; }; -struct pic_error { +struct error { PIC_OBJECT_HEADER symbol *type; - struct pic_string *msg; + struct string *msg; pic_value irrs; - struct pic_string *stack; + struct string *stack; }; -struct pic_port { +struct port { PIC_OBJECT_HEADER xFILE *file; }; -struct pic_checkpoint { +struct checkpoint { PIC_OBJECT_HEADER - struct pic_proc *in; - struct pic_proc *out; + struct proc *in; + struct proc *out; int depth; - struct pic_checkpoint *prev; + struct checkpoint *prev; }; -struct pic_object *pic_obj_ptr(pic_value); +struct object *pic_obj_ptr(pic_value); -#define pic_id_ptr(pic, o) (assert(pic_id_p(pic, o)), (identifier *)pic_obj_ptr(o)) +#define pic_id_ptr(pic, o) (assert(pic_id_p(pic, o)), (struct identifier *)pic_obj_ptr(o)) #define pic_sym_ptr(pic, o) (assert(pic_sym_p(pic, o)), (symbol *)pic_obj_ptr(o)) -#define pic_str_ptr(pic, o) (assert(pic_str_p(pic, o)), (struct pic_string *)pic_obj_ptr(o)) -#define pic_blob_ptr(pic, o) (assert(pic_blob_p(pic, o)), (struct pic_blob *)pic_obj_ptr(o)) -#define pic_pair_ptr(pic, o) (assert(pic_pair_p(pic, o)), (struct pic_pair *)pic_obj_ptr(o)) -#define pic_vec_ptr(pic, o) (assert(pic_vec_p(pic, o)), (struct pic_vector *)pic_obj_ptr(o)) -#define pic_dict_ptr(pic, o) (assert(pic_dict_p(pic, o)), (struct pic_dict *)pic_obj_ptr(o)) -#define pic_weak_ptr(pic, o) (assert(pic_weak_p(pic, o)), (struct pic_weak *)pic_obj_ptr(o)) -#define pic_data_ptr(pic, o) (assert(pic_data_p(pic, o, NULL)), (struct pic_data *)pic_obj_ptr(o)) -#define pic_proc_ptr(pic, o) (assert(pic_proc_p(pic, o)), (struct pic_proc *)pic_obj_ptr(o)) -#define pic_env_ptr(pic, o) (assert(pic_env_p(pic, o)), (struct pic_env *)pic_obj_ptr(o)) -#define pic_port_ptr(pic, o) (assert(pic_port_p(pic, o)), (struct pic_port *)pic_obj_ptr(o)) -#define pic_error_ptr(pic, o) (assert(pic_error_p(pic, o)), (struct pic_error *)pic_obj_ptr(o)) -#define pic_rec_ptr(pic, o) (assert(pic_rec_p(pic, o)), (struct pic_record *)pic_obj_ptr(o)) +#define pic_str_ptr(pic, o) (assert(pic_str_p(pic, o)), (struct string *)pic_obj_ptr(o)) +#define pic_blob_ptr(pic, o) (assert(pic_blob_p(pic, o)), (struct blob *)pic_obj_ptr(o)) +#define pic_pair_ptr(pic, o) (assert(pic_pair_p(pic, o)), (struct pair *)pic_obj_ptr(o)) +#define pic_vec_ptr(pic, o) (assert(pic_vec_p(pic, o)), (struct vector *)pic_obj_ptr(o)) +#define pic_dict_ptr(pic, o) (assert(pic_dict_p(pic, o)), (struct dict *)pic_obj_ptr(o)) +#define pic_weak_ptr(pic, o) (assert(pic_weak_p(pic, o)), (struct weak *)pic_obj_ptr(o)) +#define pic_data_ptr(pic, o) (assert(pic_data_p(pic, o, NULL)), (struct data *)pic_obj_ptr(o)) +#define pic_proc_ptr(pic, o) (assert(pic_proc_p(pic, o)), (struct proc *)pic_obj_ptr(o)) +#define pic_env_ptr(pic, o) (assert(pic_env_p(pic, o)), (struct env *)pic_obj_ptr(o)) +#define pic_port_ptr(pic, o) (assert(pic_port_p(pic, o)), (struct port *)pic_obj_ptr(o)) +#define pic_error_ptr(pic, o) (assert(pic_error_p(pic, o)), (struct error *)pic_obj_ptr(o)) +#define pic_rec_ptr(pic, o) (assert(pic_rec_p(pic, o)), (struct record *)pic_obj_ptr(o)) #define pic_obj_p(pic,v) (pic_type(pic,v) > PIC_IVAL_END) #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) @@ -162,7 +161,7 @@ struct pic_object *pic_obj_ptr(pic_value); #define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) pic_value pic_obj_value(void *ptr); -struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); +struct object *pic_obj_alloc(pic_state *, size_t, int type); #define VALID_INDEX(pic, len, i) do { \ if (i < 0 || len <= i) pic_errorf(pic, "index out of range: %d", i); \ @@ -179,7 +178,7 @@ struct pic_object *pic_obj_alloc(pic_state *, size_t, int type); pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); -pic_value pic_make_proc_irep(pic_state *, struct pic_irep *, struct pic_context *); +pic_value pic_make_proc_irep(pic_state *, struct irep *, struct context *); pic_value pic_make_env(pic_state *, pic_value env); pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs); pic_value pic_make_rec(pic_state *, pic_value type, pic_value datum); @@ -189,13 +188,13 @@ pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value pic_value pic_find_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_id_name(pic_state *, pic_value id); -void pic_rope_incref(pic_state *, struct pic_rope *); -void pic_rope_decref(pic_state *, struct pic_rope *); +void pic_rope_incref(pic_state *, struct rope *); +void pic_rope_decref(pic_state *, struct rope *); #define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) #define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) -void pic_wind(pic_state *, struct pic_checkpoint *, struct pic_checkpoint *); +void pic_wind(pic_state *, struct checkpoint *, struct checkpoint *); #if defined(__cplusplus) diff --git a/extlib/benz/include/picrin/private/state.h b/extlib/benz/include/picrin/private/state.h index 072de9dd..a5ea7ae8 100644 --- a/extlib/benz/include/picrin/private/state.h +++ b/extlib/benz/include/picrin/private/state.h @@ -15,48 +15,48 @@ extern "C" { #include "picrin/private/vm.h" #include "picrin/private/gc.h" -struct pic_lib { - struct pic_string *name; - struct pic_env *env; - struct pic_dict *exports; +struct lib { + struct string *name; + struct env *env; + struct dict *exports; }; -struct pic_callinfo { +struct callinfo { int argc, retc; - struct pic_code *ip; + struct code *ip; pic_value *fp; - struct pic_irep *irep; - struct pic_context *cxt; + struct irep *irep; + struct context *cxt; int regc; pic_value *regs; - struct pic_context *up; + struct context *up; }; -KHASH_DECLARE(oblist, struct pic_string *, struct pic_identifier *) -KHASH_DECLARE(ltable, const char *, struct pic_lib) +KHASH_DECLARE(oblist, struct string *, struct identifier *) +KHASH_DECLARE(ltable, const char *, struct lib) struct pic_state { pic_allocf allocf; void *userdata; - struct pic_checkpoint *cp; + struct checkpoint *cp; struct pic_cont *cc; int ccnt; pic_value *sp; pic_value *stbase, *stend; - struct pic_callinfo *ci; - struct pic_callinfo *cibase, *ciend; + struct callinfo *ci; + struct callinfo *cibase, *ciend; - struct pic_proc **xp; - struct pic_proc **xpbase, **xpend; + struct proc **xp; + struct proc **xpbase, **xpend; - struct pic_code *ip; + struct code *ip; pic_value ptable; /* list of ephemerons */ - struct pic_lib *lib; + struct lib *lib; pic_value features; @@ -65,14 +65,14 @@ struct pic_state { pic_value globals; /* weak */ pic_value macros; /* weak */ khash_t(ltable) ltable; - struct pic_list_head ireps; /* chain */ + struct list_head ireps; /* chain */ xFILE files[XOPEN_MAX]; - struct pic_code iseq[2]; /* for pic_apply_trampoline */ + struct code iseq[2]; /* for pic_apply_trampoline */ bool gc_enable; - struct pic_heap *heap; - struct pic_object **arena; + struct heap *heap; + struct object **arena; size_t arena_size, arena_idx; pic_value err; diff --git a/extlib/benz/include/picrin/private/vm.h b/extlib/benz/include/picrin/private/vm.h index a51ccc95..2ad09095 100644 --- a/extlib/benz/include/picrin/private/vm.h +++ b/extlib/benz/include/picrin/private/vm.h @@ -52,31 +52,31 @@ enum { OP_STOP }; -struct pic_code { +struct code { int insn; int a; int b; }; -struct pic_list_head { - struct pic_list_head *prev, *next; +struct list_head { + struct list_head *prev, *next; }; -struct pic_irep { - struct pic_list_head list; +struct irep { + struct list_head list; unsigned refc; int argc, localc, capturec; bool varg; - struct pic_code *code; - struct pic_irep **irep; + struct code *code; + struct irep **irep; int *ints; double *nums; - struct pic_object **pool; + struct object **pool; size_t ncode, nirep, nints, nnums, npool; }; -void pic_irep_incref(pic_state *, struct pic_irep *); -void pic_irep_decref(pic_state *, struct pic_irep *); +void pic_irep_incref(pic_state *, struct irep *); +void pic_irep_decref(pic_state *, struct irep *); #if defined(__cplusplus) } diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 62823155..e5bca1ad 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -7,9 +7,9 @@ #include "picrin/private/object.h" #include "picrin/private/state.h" -KHASH_DEFINE(ltable, const char *, struct pic_lib, kh_str_hash_func, kh_str_cmp_func) +KHASH_DEFINE(ltable, const char *, struct lib, kh_str_hash_func, kh_str_cmp_func) -static struct pic_lib * +static struct lib * get_library_opt(pic_state *pic, const char *lib) { khash_t(ltable) *h = &pic->ltable; @@ -22,10 +22,10 @@ get_library_opt(pic_state *pic, const char *lib) return &kh_val(h, it); } -static struct pic_lib * +static struct lib * get_library(pic_state *pic, const char *lib) { - struct pic_lib *libp; + struct lib *libp; if ((libp = get_library_opt(pic, lib)) == NULL) { pic_errorf(pic, "library not found: %s", lib); @@ -36,10 +36,10 @@ get_library(pic_state *pic, const char *lib) static pic_value make_library_env(pic_state *pic, pic_value name) { - struct pic_env *env; + struct env *env; pic_value e; - env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); + env = (struct env *)pic_obj_alloc(pic, sizeof(struct env), PIC_TYPE_ENV); env->up = NULL; env->lib = pic_str_ptr(pic, name); kh_init(env, &env->map); @@ -117,7 +117,7 @@ pic_import(pic_state *pic, const char *lib) { pic_value name, realname, uid; int it = 0; - struct pic_lib *libp; + struct lib *libp; libp = get_library(pic, lib); @@ -181,7 +181,7 @@ pic_lib_library_import(pic_state *pic) { const char *lib; pic_value name, alias, realname, uid; - struct pic_lib *libp; + struct lib *libp; int n; n = pic_get_args(pic, "zm|m", &lib, &name, &alias); @@ -231,7 +231,7 @@ pic_lib_library_exports(pic_state *pic) const char *lib; pic_value sym, exports = pic_nil_value(pic); int it = 0; - struct pic_lib *libp; + struct lib *libp; pic_get_args(pic, "z", &lib); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 8028f6cf..f1372bef 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -7,14 +7,14 @@ #include "picrin/private/object.h" #include "picrin/private/state.h" -KHASH_DEFINE(env, identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal) +KHASH_DEFINE(env, struct identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal) pic_value pic_make_env(pic_state *pic, pic_value up) { - struct pic_env *env; + struct env *env; - env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TYPE_ENV); + env = (struct env *)pic_obj_alloc(pic, sizeof(struct env), PIC_TYPE_ENV); env->up = pic_env_ptr(pic, up); env->lib = NULL; kh_init(env, &env->map); @@ -68,7 +68,7 @@ search_scope(pic_state *pic, pic_value id, pic_value env, pic_value *uid) static bool search(pic_state *pic, pic_value id, pic_value env, pic_value *uid) { - struct pic_env *e; + struct env *e; while (1) { if (search_scope(pic, id, env, uid)) @@ -84,7 +84,7 @@ search(pic_state *pic, pic_value id, pic_value env, pic_value *uid) pic_value pic_find_identifier(pic_state *pic, pic_value id, pic_value env) { - struct pic_env *e; + struct env *e; pic_value uid; while (! search(pic, id, env, &uid)) { diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 4fcfab40..4d6f474a 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -9,9 +9,9 @@ pic_value pic_cons(pic_state *pic, pic_value car, pic_value cdr) { - struct pic_pair *pair; + struct pair *pair; - pair = (struct pic_pair *)pic_obj_alloc(pic, sizeof(struct pic_pair), PIC_TYPE_PAIR); + pair = (struct pair *)pic_obj_alloc(pic, sizeof(struct pair), PIC_TYPE_PAIR); pair->car = car; pair->cdr = cdr; diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 016a52db..a0c70e6a 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -13,9 +13,9 @@ pic_value pic_open_port(pic_state *pic, xFILE *file) { - struct pic_port *port; + struct port *port; - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT); + port = (struct port *)pic_obj_alloc(pic, sizeof(struct port), PIC_TYPE_PORT); port->file = file; return pic_obj_value(port); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 7445a402..301dd09d 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -253,18 +253,18 @@ vm_gset(pic_state *pic, pic_value uid, pic_value value) static void vm_push_cxt(pic_state *pic) { - struct pic_callinfo *ci = pic->ci; + struct callinfo *ci = pic->ci; - ci->cxt = (struct pic_context *)pic_obj_alloc(pic, offsetof(struct pic_context, storage) + sizeof(pic_value) * ci->regc, PIC_TYPE_CXT); + ci->cxt = (struct context *)pic_obj_alloc(pic, offsetof(struct context, storage) + sizeof(pic_value) * ci->regc, PIC_TYPE_CXT); ci->cxt->up = ci->up; ci->cxt->regc = ci->regc; ci->cxt->regs = ci->regs; } static void -vm_tear_off(struct pic_callinfo *ci) +vm_tear_off(struct callinfo *ci) { - struct pic_context *cxt; + struct context *cxt; int i; assert(ci->cxt != NULL); @@ -283,7 +283,7 @@ vm_tear_off(struct pic_callinfo *ci) void pic_vm_tear_off(pic_state *pic) { - struct pic_callinfo *ci; + struct callinfo *ci; for (ci = pic->ci; ci > pic->cibase; ci--) { if (ci->cxt != NULL) { @@ -326,9 +326,9 @@ bool pic_ge(pic_state *, pic_value, pic_value); pic_value pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) { - struct pic_code c; + struct code c; size_t ai = pic_enter(pic); - struct pic_code boot[2]; + struct code boot[2]; int i; #if PIC_DIRECT_THREADED_VM @@ -411,8 +411,8 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) NEXT; } CASE(OP_LREF) { - struct pic_callinfo *ci = pic->ci; - struct pic_irep *irep = ci->irep; + struct callinfo *ci = pic->ci; + struct irep *irep = ci->irep; if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) { if (c.a >= irep->argc + irep->localc) { @@ -424,8 +424,8 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) NEXT; } CASE(OP_LSET) { - struct pic_callinfo *ci = pic->ci; - struct pic_irep *irep = ci->irep; + struct callinfo *ci = pic->ci; + struct irep *irep = ci->irep; if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) { if (c.a >= irep->argc + irep->localc) { @@ -440,7 +440,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) } CASE(OP_CREF) { int depth = c.a; - struct pic_context *cxt; + struct context *cxt; cxt = pic->ci->up; while (--depth) { @@ -451,7 +451,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) } CASE(OP_CSET) { int depth = c.a; - struct pic_context *cxt; + struct context *cxt; cxt = pic->ci->up; while (--depth) { @@ -477,8 +477,8 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) } CASE(OP_CALL) { pic_value x, v; - struct pic_callinfo *ci; - struct pic_proc *proc; + struct callinfo *ci; + struct proc *proc; if (c.a == -1) { pic->sp += pic->ci[1].retc - 1; @@ -514,7 +514,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) goto L_RET; } else { - struct pic_irep *irep = proc->u.i.irep; + struct irep *irep = proc->u.i.irep; int i; pic_value rest; @@ -557,7 +557,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) CASE(OP_TAILCALL) { int i, argc; pic_value *argv; - struct pic_callinfo *ci; + struct callinfo *ci; if (pic->ci->cxt != NULL) { vm_tear_off(pic->ci); @@ -583,7 +583,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) CASE(OP_RET) { int i, retc; pic_value *retv; - struct pic_callinfo *ci; + struct callinfo *ci; if (pic->ci->cxt != NULL) { vm_tear_off(pic->ci); @@ -772,7 +772,7 @@ pic_value pic_applyk(pic_state *pic, pic_value proc, int argc, pic_value *args) { pic_value *sp; - struct pic_callinfo *ci; + struct callinfo *ci; int i; pic->iseq[0].insn = OP_NOP; @@ -904,7 +904,7 @@ pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) pic_value pic_closure_ref(pic_state *pic, int n) { - struct pic_proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); + struct proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); assert(pic_proc_func_p(self)); @@ -917,7 +917,7 @@ pic_closure_ref(pic_state *pic, int n) void pic_closure_set(pic_state *pic, int n, pic_value v) { - struct pic_proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); + struct proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); assert(pic_proc_func_p(self)); @@ -945,13 +945,13 @@ pic_funcall(pic_state *pic, const char *lib, const char *name, int n, ...) } void -pic_irep_incref(pic_state *PIC_UNUSED(pic), struct pic_irep *irep) +pic_irep_incref(pic_state *PIC_UNUSED(pic), struct irep *irep) { irep->refc++; } void -pic_irep_decref(pic_state *pic, struct pic_irep *irep) +pic_irep_decref(pic_state *pic, struct irep *irep) { size_t i; @@ -976,10 +976,10 @@ pic_irep_decref(pic_state *pic, struct pic_irep *irep) pic_value pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) { - struct pic_proc *proc; + struct proc *proc; int i; - proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals) + sizeof(pic_value) * n, PIC_TYPE_PROC); + proc = (struct proc *)pic_obj_alloc(pic, offsetof(struct proc, locals) + sizeof(pic_value) * n, PIC_TYPE_PROC); proc->tag = PIC_PROC_TAG_FUNC; proc->u.f.func = func; proc->u.f.localc = n; @@ -990,11 +990,11 @@ pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) } pic_value -pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cxt) +pic_make_proc_irep(pic_state *pic, struct irep *irep, struct context *cxt) { - struct pic_proc *proc; + struct proc *proc; - proc = (struct pic_proc *)pic_obj_alloc(pic, offsetof(struct pic_proc, locals), PIC_TYPE_PROC); + proc = (struct proc *)pic_obj_alloc(pic, offsetof(struct proc, locals), PIC_TYPE_PROC); proc->tag = PIC_PROC_TAG_IREP; proc->u.i.irep = irep; proc->u.i.cxt = cxt; diff --git a/extlib/benz/record.c b/extlib/benz/record.c index 9d764b99..36a9dc79 100644 --- a/extlib/benz/record.c +++ b/extlib/benz/record.c @@ -8,9 +8,9 @@ pic_value pic_make_rec(pic_state *pic, pic_value type, pic_value datum) { - struct pic_record *rec; + struct record *rec; - rec = (struct pic_record *)pic_obj_alloc(pic, sizeof(struct pic_record), PIC_TYPE_RECORD); + rec = (struct record *)pic_obj_alloc(pic, sizeof(struct record), PIC_TYPE_RECORD); rec->type = type; rec->datum = datum; diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 3cbeb95d..5f4c5adb 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -219,7 +219,7 @@ pic_open(pic_allocf allocf, void *userdata) } /* callinfo */ - pic->cibase = pic->ci = allocf(userdata, NULL, PIC_STACK_SIZE * sizeof(struct pic_callinfo)); + pic->cibase = pic->ci = allocf(userdata, NULL, PIC_STACK_SIZE * sizeof(struct callinfo)); pic->ciend = pic->cibase + PIC_STACK_SIZE; if (! pic->ci) { @@ -227,7 +227,7 @@ pic_open(pic_allocf allocf, void *userdata) } /* exception handler */ - pic->xpbase = pic->xp = allocf(userdata, NULL, PIC_RESCUE_SIZE * sizeof(struct pic_proc *)); + pic->xpbase = pic->xp = allocf(userdata, NULL, PIC_RESCUE_SIZE * sizeof(struct proc *)); pic->xpend = pic->xpbase + PIC_RESCUE_SIZE; if (! pic->xp) { @@ -235,7 +235,7 @@ pic_open(pic_allocf allocf, void *userdata) } /* GC arena */ - pic->arena = allocf(userdata, NULL, PIC_ARENA_SIZE * sizeof(struct pic_object *)); + pic->arena = allocf(userdata, NULL, PIC_ARENA_SIZE * sizeof(struct object *)); pic->arena_size = PIC_ARENA_SIZE; pic->arena_idx = 0; @@ -298,7 +298,7 @@ pic_open(pic_allocf allocf, void *userdata) pic->macros = pic_make_weak(pic); /* root block */ - pic->cp = (struct pic_checkpoint *)pic_obj_alloc(pic, sizeof(struct pic_checkpoint), PIC_TYPE_CP); + pic->cp = (struct checkpoint *)pic_obj_alloc(pic, sizeof(struct checkpoint), PIC_TYPE_CP); pic->cp->prev = NULL; pic->cp->depth = 0; pic->cp->in = pic->cp->out = NULL; @@ -356,7 +356,7 @@ pic_close(pic_state *pic) { /* FIXME */ int i = 0; - struct pic_list_head *list; + struct list_head *list; for (list = pic->ireps.next; list != &pic->ireps; list = list->next) { i++; } diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 567951d1..75a703db 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -6,19 +6,19 @@ #include "picrin/extra.h" #include "picrin/private/object.h" -struct pic_chunk { +struct chunk { char *str; int refcnt; size_t len; char buf[1]; }; -struct pic_rope { +struct rope { int refcnt; size_t weight; - struct pic_chunk *chunk; + struct chunk *chunk; size_t offset; - struct pic_rope *left, *right; + struct rope *left, *right; }; #define CHUNK_INCREF(c) do { \ @@ -26,19 +26,19 @@ struct pic_rope { } while (0) #define CHUNK_DECREF(c) do { \ - struct pic_chunk *c_ = (c); \ + struct chunk *c_ = (c); \ if (! --c_->refcnt) { \ pic_free(pic, c_); \ } \ } while (0) void -pic_rope_incref(pic_state *PIC_UNUSED(pic), struct pic_rope *x) { +pic_rope_incref(pic_state *PIC_UNUSED(pic), struct rope *x) { x->refcnt++; } void -pic_rope_decref(pic_state *pic, struct pic_rope *x) { +pic_rope_decref(pic_state *pic, struct rope *x) { if (! --x->refcnt) { if (x->chunk) { CHUNK_DECREF(x->chunk); @@ -51,12 +51,12 @@ pic_rope_decref(pic_state *pic, struct pic_rope *x) { } } -static struct pic_chunk * +static struct chunk * pic_make_chunk(pic_state *pic, const char *str, size_t len) { - struct pic_chunk *c; + struct chunk *c; - c = pic_malloc(pic, offsetof(struct pic_chunk, buf) + len + 1); + c = pic_malloc(pic, offsetof(struct chunk, buf) + len + 1); c->refcnt = 1; c->str = c->buf; c->len = len; @@ -66,12 +66,12 @@ pic_make_chunk(pic_state *pic, const char *str, size_t len) return c; } -static struct pic_chunk * +static struct chunk * pic_make_chunk_lit(pic_state *pic, const char *str, size_t len) { - struct pic_chunk *c; + struct chunk *c; - c = pic_malloc(pic, sizeof(struct pic_chunk)); + c = pic_malloc(pic, sizeof(struct chunk)); c->refcnt = 1; c->str = (char *)str; c->len = len; @@ -79,12 +79,12 @@ pic_make_chunk_lit(pic_state *pic, const char *str, size_t len) return c; } -static struct pic_rope * -pic_make_rope(pic_state *pic, struct pic_chunk *c) +static struct rope * +pic_make_rope(pic_state *pic, struct chunk *c) { - struct pic_rope *x; + struct rope *x; - x = pic_malloc(pic, sizeof(struct pic_rope)); + x = pic_malloc(pic, sizeof(struct rope)); x->refcnt = 1; x->left = NULL; x->right = NULL; @@ -96,24 +96,24 @@ pic_make_rope(pic_state *pic, struct pic_chunk *c) } static pic_value -pic_make_str(pic_state *pic, struct pic_rope *rope) +pic_make_str(pic_state *pic, struct rope *rope) { - struct pic_string *str; + struct string *str; - str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TYPE_STRING); + str = (struct string *)pic_obj_alloc(pic, sizeof(struct string), PIC_TYPE_STRING); str->rope = rope; /* delegate ownership */ return pic_obj_value(str); } static size_t -rope_len(struct pic_rope *x) +rope_len(struct rope *x) { return x->weight; } static char -rope_at(struct pic_rope *x, size_t i) +rope_at(struct rope *x, size_t i) { while (i < x->weight) { if (x->chunk) { @@ -129,12 +129,12 @@ rope_at(struct pic_rope *x, size_t i) return -1; } -static struct pic_rope * -rope_cat(pic_state *pic, struct pic_rope *x, struct pic_rope *y) +static struct rope * +rope_cat(pic_state *pic, struct rope *x, struct rope *y) { - struct pic_rope *z; + struct rope *z; - z = pic_malloc(pic, sizeof(struct pic_rope)); + z = pic_malloc(pic, sizeof(struct rope)); z->refcnt = 1; z->left = x; z->right = y; @@ -148,8 +148,8 @@ rope_cat(pic_state *pic, struct pic_rope *x, struct pic_rope *y) return z; } -static struct pic_rope * -rope_sub(pic_state *pic, struct pic_rope *x, size_t i, size_t j) +static struct rope * +rope_sub(pic_state *pic, struct rope *x, size_t i, size_t j) { assert(i <= j); assert(j <= x->weight); @@ -160,9 +160,9 @@ rope_sub(pic_state *pic, struct pic_rope *x, size_t i, size_t j) } if (x->chunk) { - struct pic_rope *y; + struct rope *y; - y = pic_malloc(pic, sizeof(struct pic_rope)); + y = pic_malloc(pic, sizeof(struct rope)); y->refcnt = 1; y->left = NULL; y->right = NULL; @@ -182,7 +182,7 @@ rope_sub(pic_state *pic, struct pic_rope *x, size_t i, size_t j) return rope_sub(pic, x->right, i - x->left->weight, j - x->left->weight); } else { - struct pic_rope *r, *l; + struct rope *r, *l; l = rope_sub(pic, x->left, i, x->left->weight); r = rope_sub(pic, x->right, 0, j - x->left->weight); @@ -196,7 +196,7 @@ rope_sub(pic_state *pic, struct pic_rope *x, size_t i, size_t j) } static void -flatten(pic_state *pic, struct pic_rope *x, struct pic_chunk *c, size_t offset) +flatten(pic_state *pic, struct rope *x, struct chunk *c, size_t offset) { if (x->chunk) { memcpy(c->str + offset, x->chunk->str + x->offset, x->weight); @@ -219,15 +219,15 @@ flatten(pic_state *pic, struct pic_rope *x, struct pic_chunk *c, size_t offset) } static const char * -rope_cstr(pic_state *pic, struct pic_rope *x) +rope_cstr(pic_state *pic, struct rope *x) { - struct pic_chunk *c; + struct chunk *c; if (x->chunk && x->offset == 0 && x->weight == x->chunk->len) { return x->chunk->str; /* reuse cached chunk */ } - c = pic_malloc(pic, offsetof(struct pic_chunk, buf) + x->weight + 1); + c = pic_malloc(pic, offsetof(struct chunk, buf) + x->weight + 1); c->refcnt = 1; c->len = x->weight; c->str = c->buf; @@ -250,7 +250,7 @@ str_update(pic_state *pic, pic_value dst, pic_value src) pic_value pic_str_value(pic_state *pic, const char *str, int len) { - struct pic_chunk *c; + struct chunk *c; if (len > 0) { c = pic_make_chunk(pic, str, len); diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index 3f40ef43..cff34df8 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -10,7 +10,7 @@ #define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a))) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0) -KHASH_DEFINE(oblist, struct pic_string *, symbol *, kh_pic_str_hash, kh_pic_str_cmp) +KHASH_DEFINE(oblist, struct string *, symbol *, kh_pic_str_hash, kh_pic_str_cmp) pic_value pic_intern(pic_state *pic, pic_value str) @@ -39,9 +39,9 @@ pic_intern(pic_state *pic, pic_value str) pic_value pic_make_identifier(pic_state *pic, pic_value base, pic_value env) { - identifier *id; + struct identifier *id; - id = (identifier *)pic_obj_alloc(pic, sizeof(identifier), PIC_TYPE_ID); + id = (struct identifier *)pic_obj_alloc(pic, sizeof(struct identifier), PIC_TYPE_ID); id->u.id = pic_id_ptr(pic, base); id->env = pic_env_ptr(pic, env); diff --git a/extlib/benz/value.c b/extlib/benz/value.c index 87dd9d7d..bd2d1e8e 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -45,10 +45,10 @@ pic_char(pic_state *PIC_UNUSED(pic), pic_value v) return v & 0xfffffffful; } -struct pic_object * +struct object * pic_obj_ptr(pic_value v) { - return (struct pic_object *)(0xfffffffffffful & v); + return (struct object *)(0xfffffffffffful & v); } #else @@ -79,10 +79,10 @@ pic_char(pic_state *PIC_UNUSED(pic), pic_value v) return v.u.c; } -struct pic_object * +struct object * pic_obj_ptr(pic_value v) { - return (struct pic_object *)(v.u.data); + return (struct object *)(v.u.data); } #endif @@ -198,7 +198,7 @@ pic_type(pic_state *PIC_UNUSED(pic), pic_value v) if (tt < PIC_IVAL_END) { return tt; } - return ((struct pic_basic *)pic_obj_ptr(v))->tt; + return ((struct basic *)pic_obj_ptr(v))->tt; } const char * diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 71d12519..978cfeb1 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -9,10 +9,10 @@ pic_value pic_make_vec(pic_state *pic, int len, pic_value *argv) { - struct pic_vector *vec; + struct vector *vec; int i; - vec = (struct pic_vector *)pic_obj_alloc(pic, sizeof(struct pic_vector), PIC_TYPE_VECTOR); + vec = (struct vector *)pic_obj_alloc(pic, sizeof(struct vector), PIC_TYPE_VECTOR); vec->len = len; vec->data = (pic_value *)pic_malloc(pic, sizeof(pic_value) * len); if (argv == NULL) { diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 6ab88f89..3064873c 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -5,14 +5,14 @@ #include "picrin.h" #include "picrin/private/object.h" -KHASH_DEFINE(weak, struct pic_object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) +KHASH_DEFINE(weak, struct object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) pic_value pic_make_weak(pic_state *pic) { - struct pic_weak *weak; + struct weak *weak; - weak = (struct pic_weak *)pic_obj_alloc(pic, sizeof(struct pic_weak), PIC_TYPE_WEAK); + weak = (struct weak *)pic_obj_alloc(pic, sizeof(struct weak), PIC_TYPE_WEAK); weak->prev = NULL; kh_init(weak, &weak->hash); From 2a17a2a9c2ee5d33878681539251e2971012cb63 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 20:42:41 +0900 Subject: [PATCH 101/119] add PIC_TYPE_FUNC and PIC_TYPE_IREP --- extlib/benz/debug.c | 2 +- extlib/benz/gc.c | 27 ++++++++++----------- extlib/benz/include/picrin.h | 8 +++--- extlib/benz/include/picrin/private/object.h | 9 ++----- extlib/benz/proc.c | 24 +++++++++--------- extlib/benz/value.c | 3 ++- 6 files changed, 34 insertions(+), 39 deletions(-) diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 189f95dc..ae3ca9b3 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -22,7 +22,7 @@ pic_get_backtrace(pic_state *pic) trace = pic_str_cat(pic, trace, pic_lit_value(pic, " at ")); trace = pic_str_cat(pic, trace, pic_lit_value(pic, "(anonymous lambda)")); - if (pic_proc_func_p(pic_proc_ptr(pic, proc))) { + if (pic_func_p(proc)) { trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (native function)\n")); } else { trace = pic_str_cat(pic, trace, pic_lit_value(pic, " (unknown location)\n")); /* TODO */ diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 00a34de5..f5ecd292 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -301,16 +301,16 @@ gc_mark_object(pic_state *pic, struct object *obj) } break; } - case PIC_TYPE_PROC: { - if (pic_proc_irep_p(&obj->u.proc)) { - if (obj->u.proc.u.i.cxt) { - LOOP(obj->u.proc.u.i.cxt); - } - } else { - int i; - for (i = 0; i < obj->u.proc.u.f.localc; ++i) { - gc_mark(pic, obj->u.proc.locals[i]); - } + case PIC_TYPE_FUNC: { + int i; + for (i = 0; i < obj->u.proc.u.f.localc; ++i) { + gc_mark(pic, obj->u.proc.locals[i]); + } + break; + } + case PIC_TYPE_IREP: { + if (obj->u.proc.u.i.cxt) { + LOOP(obj->u.proc.u.i.cxt); } break; } @@ -550,10 +550,8 @@ gc_finalize_object(pic_state *pic, struct object *obj) kh_destroy(weak, &obj->u.weak.hash); break; } - case PIC_TYPE_PROC: { - if (pic_proc_irep_p(&obj->u.proc)) { - pic_irep_decref(pic, obj->u.proc.u.i.irep); - } + case PIC_TYPE_IREP: { + pic_irep_decref(pic, obj->u.proc.u.i.irep); break; } @@ -564,6 +562,7 @@ gc_finalize_object(pic_state *pic, struct object *obj) case PIC_TYPE_ID: case PIC_TYPE_RECORD: case PIC_TYPE_CP: + case PIC_TYPE_FUNC: break; default: diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 7147991f..c422833e 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -162,7 +162,6 @@ enum { PIC_TYPE_STRING = 16, PIC_TYPE_VECTOR = 17, PIC_TYPE_BLOB = 18, - PIC_TYPE_PROC = 19, PIC_TYPE_PORT = 20, PIC_TYPE_ERROR = 21, PIC_TYPE_ID = 22, @@ -174,7 +173,9 @@ enum { PIC_TYPE_SYMBOL = 28, PIC_TYPE_PAIR = 29, PIC_TYPE_CXT = 30, - PIC_TYPE_CP = 31 + PIC_TYPE_CP = 31, + PIC_TYPE_FUNC = 32, + PIC_TYPE_IREP = 33, }; #define pic_invalid_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INVALID) @@ -185,9 +186,10 @@ enum { #define pic_eof_p(pic, v) (pic_type(pic, v) == PIC_TYPE_EOF) #define pic_true_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE) #define pic_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE) +#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) #define pic_str_p(pic,v) (pic_type(pic,v) == PIC_TYPE_STRING) #define pic_blob_p(pic,v) (pic_type(pic,v) == PIC_TYPE_BLOB) -#define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PROC) +#define pic_proc_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FUNC || pic_type(pic, v) == PIC_TYPE_IREP) #define pic_nil_p(pic,v) (pic_type(pic,v) == PIC_TYPE_NIL) #define pic_pair_p(pic,v) (pic_type(pic,v) == PIC_TYPE_PAIR) #define pic_vec_p(pic,v) (pic_type(pic,v) == PIC_TYPE_VECTOR) diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index 39751196..64b1d6d9 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -93,10 +93,6 @@ struct context { struct proc { PIC_OBJECT_HEADER - enum { - PIC_PROC_TAG_IREP, - PIC_PROC_TAG_FUNC - } tag; union { struct { pic_func_t func; @@ -158,7 +154,6 @@ struct object *pic_obj_ptr(pic_value); #define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV) #define pic_error_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ERROR) #define pic_rec_p(pic, v) (pic_type(pic, v) == PIC_TYPE_RECORD) -#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) pic_value pic_obj_value(void *ptr); struct object *pic_obj_alloc(pic_state *, size_t, int type); @@ -191,8 +186,8 @@ pic_value pic_id_name(pic_state *, pic_value id); void pic_rope_incref(pic_state *, struct rope *); void pic_rope_decref(pic_state *, struct rope *); -#define pic_proc_func_p(proc) ((proc)->tag == PIC_PROC_TAG_FUNC) -#define pic_proc_irep_p(proc) ((proc)->tag == PIC_PROC_TAG_IREP) +#define pic_func_p(proc) (pic_type(pic, proc) == PIC_TYPE_FUNC) +#define pic_irep_p(proc) (pic_type(pic, proc) == PIC_TYPE_IREP) void pic_wind(pic_state *, struct checkpoint *, struct checkpoint *); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 301dd09d..9b9070d2 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -503,7 +503,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) ci->fp = pic->sp - c.a; ci->irep = NULL; ci->cxt = NULL; - if (pic_proc_func_p(proc)) { + if (proc->tt == PIC_TYPE_FUNC) { /* invoke! */ v = proc->u.f.func(pic); @@ -904,27 +904,27 @@ pic_set(pic_state *pic, const char *lib, const char *name, pic_value val) pic_value pic_closure_ref(pic_state *pic, int n) { - struct proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); + pic_value self = GET_OPERAND(pic, 0); - assert(pic_proc_func_p(self)); + assert(pic_func_p(self)); - if (n < 0 || self->u.f.localc <= n) { + if (n < 0 || pic_proc_ptr(pic, self)->u.f.localc <= n) { pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); } - return self->locals[n]; + return pic_proc_ptr(pic, self)->locals[n]; } void pic_closure_set(pic_state *pic, int n, pic_value v) { - struct proc *self = pic_proc_ptr(pic, GET_OPERAND(pic, 0)); + pic_value self = GET_OPERAND(pic, 0); - assert(pic_proc_func_p(self)); + assert(pic_func_p(self)); - if (n < 0 || self->u.f.localc <= n) { + if (n < 0 || pic_proc_ptr(pic, self)->u.f.localc <= n) { pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); } - self->locals[n] = v; + pic_proc_ptr(pic, self)->locals[n] = v; } pic_value @@ -979,8 +979,7 @@ pic_make_proc(pic_state *pic, pic_func_t func, int n, pic_value *env) struct proc *proc; int i; - proc = (struct proc *)pic_obj_alloc(pic, offsetof(struct proc, locals) + sizeof(pic_value) * n, PIC_TYPE_PROC); - proc->tag = PIC_PROC_TAG_FUNC; + proc = (struct proc *)pic_obj_alloc(pic, offsetof(struct proc, locals) + sizeof(pic_value) * n, PIC_TYPE_FUNC); proc->u.f.func = func; proc->u.f.localc = n; for (i = 0; i < n; ++i) { @@ -994,8 +993,7 @@ pic_make_proc_irep(pic_state *pic, struct irep *irep, struct context *cxt) { struct proc *proc; - proc = (struct proc *)pic_obj_alloc(pic, offsetof(struct proc, locals), PIC_TYPE_PROC); - proc->tag = PIC_PROC_TAG_IREP; + proc = (struct proc *)pic_obj_alloc(pic, offsetof(struct proc, locals), PIC_TYPE_IREP); proc->u.i.irep = irep; proc->u.i.cxt = cxt; pic_irep_incref(pic, irep); diff --git a/extlib/benz/value.c b/extlib/benz/value.c index bd2d1e8e..a41d7d52 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -240,7 +240,8 @@ pic_typename(pic_state *pic, int type) return "identifier"; case PIC_TYPE_CXT: return "context"; - case PIC_TYPE_PROC: + case PIC_TYPE_FUNC: + case PIC_TYPE_IREP: return "procedure"; case PIC_TYPE_ENV: return "environment"; From f61fc19692addae8ed7b5296face166182275940 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 21:12:24 +0900 Subject: [PATCH 102/119] missing abort declaration --- extlib/benz/error.c | 2 ++ extlib/benz/include/picrin.h | 2 +- extlib/benz/include/picrin/setup.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 56a0c39b..51d15a2f 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -19,6 +19,8 @@ pic_panic(pic_state *pic, const char *msg) #endif PIC_ABORT(pic); + + PIC_UNREACHABLE(); } void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index c422833e..cd1bf61d 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -175,7 +175,7 @@ enum { PIC_TYPE_CXT = 30, PIC_TYPE_CP = 31, PIC_TYPE_FUNC = 32, - PIC_TYPE_IREP = 33, + PIC_TYPE_IREP = 33 }; #define pic_invalid_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INVALID) diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index c04b7f42..49706ba7 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -40,6 +40,7 @@ #endif #ifndef PIC_ABORT +void abort(void); # define PIC_ABORT(pic) abort() #endif From 81dd765d8d5b128cbf09fe8ee38923fcd07b23d8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 21:23:45 +0900 Subject: [PATCH 103/119] libbenz.a -> libbenz.so --- Makefile | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 58eadf39..05bf5079 100644 --- a/Makefile +++ b/Makefile @@ -33,8 +33,8 @@ debug: bin/picrin include $(sort $(wildcard contrib/*/nitro.mk)) -bin/picrin: $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libbenz.a - $(CC) $(CFLAGS) -o $@ $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libbenz.a $(LDFLAGS) +bin/picrin: $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libbenz.so + $(CC) $(CFLAGS) -o $@ $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libbenz.so $(LDFLAGS) src/load_piclib.c: $(CONTRIB_LIBS) perl etc/mkloader.pl $(CONTRIB_LIBS) > $@ @@ -42,8 +42,8 @@ src/load_piclib.c: $(CONTRIB_LIBS) src/init_contrib.c: perl etc/mkinit.pl $(CONTRIB_INITS) > $@ -lib/libbenz.a: $(BENZ_OBJS) - $(AR) $(ARFLAGS) $@ $(BENZ_OBJS) +lib/libbenz.so: $(BENZ_OBJS) + $(CC) -shared $(CFLAGS) -o $@ $(BENZ_OBJS) $(LDFLAGS) extlib/benz/boot.o: extlib/benz/boot.c cd extlib/benz; perl boot.c @@ -70,8 +70,10 @@ test: test-contribs test-nostdlib test-issue test-contribs: bin/picrin $(CONTRIB_TESTS) test-nostdlib: - $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_FLOAT=0' -D'PIC_ENABLE_STDIO=0' -ffreestanding -nostdlib -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector - rm -f lib/libbenz.so + $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_STDIO=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector + strip lib/libbenz-tiny.so + ls -lh lib/libbenz-tiny.so + rm -f lib/libbenz-tiny.so test-issue: test-picrin-issue test-repl-issue @@ -90,7 +92,7 @@ install: all clean: rm -f src/load_piclib.c src/init_contrib.c - rm -f lib/libbenz.a + rm -f lib/libbenz.so rm -f $(BENZ_OBJS) rm -f $(PICRIN_OBJS) rm -f $(CONTRIB_OBJS) From 4c376650ca07427a291bd9cf1e7ae893336e2dca Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 21:32:52 +0900 Subject: [PATCH 104/119] fix build --- Makefile | 9 +++++---- extlib/benz/file.c | 2 +- extlib/benz/include/picrin/setup.h | 4 ++-- extlib/benz/lib.c | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 05bf5079..6c80d105 100644 --- a/Makefile +++ b/Makefile @@ -20,12 +20,12 @@ REPL_ISSUE_TESTS = $(wildcard t/issue/*.sh) TEST_RUNNER = bin/picrin -CFLAGS += -I./extlib/benz/include -Wall -Wextra $(CONTRIB_DEFS) +CFLAGS += -I./extlib/benz/include -Wall -Wextra LDFLAGS += -lm prefix ?= /usr/local -all: CFLAGS += -O2 -DNDEBUG=1 +all: CFLAGS += -O2 -flto -DNDEBUG=1 all: bin/picrin debug: CFLAGS += -O0 -g @@ -33,8 +33,9 @@ debug: bin/picrin include $(sort $(wildcard contrib/*/nitro.mk)) -bin/picrin: $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libbenz.so - $(CC) $(CFLAGS) -o $@ $(PICRIN_OBJS) $(CONTRIB_OBJS) lib/libbenz.so $(LDFLAGS) +bin/picrin: CFLAGS += $(CONTRIB_DEFS) +bin/picrin: $(PICRIN_OBJS) $(CONTRIB_OBJS) $(BENZ_OBJS) + $(CC) $(CFLAGS) -o $@ $(PICRIN_OBJS) $(CONTRIB_OBJS) $(BENZ_OBJS) $(LDFLAGS) src/load_piclib.c: $(CONTRIB_LIBS) perl etc/mkloader.pl $(CONTRIB_LIBS) > $@ diff --git a/extlib/benz/file.c b/extlib/benz/file.c index d828845d..9a844acd 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -186,7 +186,7 @@ int xfputs(pic_state *pic, const char *s, xFILE *stream) { } char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { - int c; + int c = 0; char *buf; xfflush(pic, NULL); diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 49706ba7..34e36a49 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -131,10 +131,10 @@ typedef unsigned long uint32_t; #define PIC_FALLTHROUGH ((void)0) -#if __cplusplus +#if defined(__cplusplus) # define PIC_UNUSED(v) #elif __GNUC__ || __clang__ -# define PIC_UNUSED(v) __attribute__((unused)) v +# define PIC_UNUSED(v) v __attribute__((unused)) #else # define PIC_UNUSED(v) v #endif diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index e5bca1ad..c5bb5cad 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -61,7 +61,7 @@ void pic_make_library(pic_state *pic, const char *lib) { khash_t(ltable) *h = &pic->ltable; - const char *old_lib; + const char *old_lib = NULL; pic_value name, env, exports; int it; int ret; From b436207ebaf4c9d52bd30968e63999ada7e3a557 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 21 Feb 2016 23:32:18 +0900 Subject: [PATCH 105/119] PIC_DIRECT_THREADED_VM and PIC_NAN_BOXING are now unconfigurable --- extlib/benz/include/picrin/config.h | 19 +------------------ extlib/benz/include/picrin/setup.h | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index 867f33e2..e928f95f 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -2,43 +2,26 @@ * See Copyright Notice in picrin.h */ -/** switch normal VM and direct threaded VM */ -/* #define PIC_DIRECT_THREADED_VM 1 */ - -/** switch internal value representation */ -/* #define PIC_NAN_BOXING 1 */ - /** no dependency on libc */ /* #define PIC_ENABLE_LIBC 1 */ /** use stdio or not */ /* #define PIC_ENABLE_STDIO 1 */ -/** custom setjmp/longjmp */ +/** essential external functions */ /* #define PIC_JMPBUF jmp_buf */ /* #define PIC_SETJMP(pic, buf) setjmp(buf) */ /* #define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val)) */ - -/** custom abort */ /* #define PIC_ABORT(pic) abort() */ /** initial memory size (to be dynamically extended if necessary) */ /* #define PIC_ARENA_SIZE 1000 */ - /* #define PIC_HEAP_PAGE_SIZE 10000 */ - /* #define PIC_PAGE_REQUEST_THRESHOLD(total) ((total) * 77 / 100) */ - /* #define PIC_STACK_SIZE 1024 */ - /* #define PIC_RESCUE_SIZE 30 */ - /* #define PIC_SYM_POOL_SIZE 128 */ - /* #define PIC_IREP_SIZE 8 */ - /* #define PIC_POOL_SIZE 8 */ - /* #define PIC_SYMS_SIZE 32 */ - /* #define PIC_ISEQ_SIZE 1024 */ diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 34e36a49..86aee1b2 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -4,18 +4,6 @@ #include "picrin/config.h" -#ifndef PIC_DIRECT_THREADED_VM -# if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__) -# define PIC_DIRECT_THREADED_VM 1 -# endif -#endif - -#ifndef PIC_NAN_BOXING -# if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__) -# define PIC_NAN_BOXING 1 -# endif -#endif - #ifndef PIC_ENABLE_LIBC # define PIC_ENABLE_LIBC 1 #endif @@ -457,3 +445,17 @@ void PIC_DOUBLE_TO_CSTRING(double, char *); #define PIC_CSTRING_TO_DOUBLE atof #endif double PIC_CSTRING_TO_DOUBLE(const char *); + +/* optional features available? */ + +#if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__) +# define PIC_DIRECT_THREADED_VM 1 +#else +# define PIC_DIRECT_THREADED_VM 0 +#endif + +#if __x86_64__ && (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__) +# define PIC_NAN_BOXING 1 +#else +# define PIC_NAN_BOXING 0 +#endif From e6382965efe5062a7011a7fd255683c9d6471ea5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 22 Feb 2016 00:02:44 +0900 Subject: [PATCH 106/119] cleanup extra.h --- extlib/benz/error.c | 48 ++++++++-------- extlib/benz/include/picrin/extra.h | 91 +++++++++++++++--------------- extlib/benz/include/picrin/setup.h | 3 +- 3 files changed, 69 insertions(+), 73 deletions(-) diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 51d15a2f..71050cb0 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -52,30 +52,6 @@ pic_errorf(pic_state *pic, const char *fmt, ...) pic_error(pic, "", msg, pic_nil_value(pic)); } -static pic_value -native_exception_handler(pic_state *pic) -{ - pic_value err; - - pic_get_args(pic, "o", &err); - - pic->err = err; - - pic_call(pic, pic_closure_ref(pic, 0), 1, pic_false_value(pic)); - - PIC_UNREACHABLE(); -} - -void -pic_push_native_handler(pic_state *pic, struct pic_cont *cont) -{ - pic_value handler; - - handler = pic_lambda(pic, native_exception_handler, 1, pic_make_cont(pic, cont)); - - pic_push_handler(pic, handler); -} - void pic_push_handler(pic_state *pic, pic_value handler) { @@ -103,6 +79,30 @@ pic_pop_handler(pic_state *pic) return pic_obj_value(*--pic->xp); } +static pic_value +native_exception_handler(pic_state *pic) +{ + pic_value err; + + pic_get_args(pic, "o", &err); + + pic->err = err; + + pic_call(pic, pic_closure_ref(pic, 0), 1, pic_false_value(pic)); + + PIC_UNREACHABLE(); +} + +void +pic_push_native_handler(pic_state *pic, struct pic_cont *cont) +{ + pic_value handler; + + handler = pic_lambda(pic, native_exception_handler, 1, pic_make_cont(pic, cont)); + + pic_push_handler(pic, handler); +} + pic_value pic_err(pic_state *pic) { diff --git a/extlib/benz/include/picrin/extra.h b/extlib/benz/include/picrin/extra.h index b497dd7c..244d5b23 100644 --- a/extlib/benz/include/picrin/extra.h +++ b/extlib/benz/include/picrin/extra.h @@ -14,20 +14,23 @@ extern "C" { void *pic_default_allocf(void *, void *, size_t); #endif +pic_value pic_read(pic_state *, pic_value port); +pic_value pic_read_cstr(pic_state *, const char *); -#define pic_assert_type(pic, v, type) \ - if (! pic_##type##_p(pic, v)) { \ - pic_errorf(pic, "expected " #type ", but got ~s", v); \ - } +pic_value pic_expand(pic_state *, pic_value program, pic_value env); +pic_value pic_eval(pic_state *, pic_value program, const char *lib); + +void pic_load(pic_state *, pic_value port); +void pic_load_cstr(pic_state *, const char *); + +/* extra xfile methods */ xFILE *xfile_xstdin(pic_state *); xFILE *xfile_xstdout(pic_state *); xFILE *xfile_xstderr(pic_state *); - #define xstdin (xfile_xstdin(pic)) #define xstdout (xfile_xstdout(pic)) #define xstderr (xfile_xstderr(pic)) - #if PIC_ENABLE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); #endif @@ -35,24 +38,39 @@ xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len); xFILE *xfopen_null(pic_state *, const char *mode); -#define pic_void(exec) \ - pic_void_(PIC_GENSYM(ai), exec) +/* port manipulation */ + +#define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0) +#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0) +#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0) +pic_value pic_write(pic_state *, pic_value); /* returns given obj */ +pic_value pic_fwrite(pic_state *, pic_value, xFILE *); +void pic_printf(pic_state *, const char *, ...); +void pic_fprintf(pic_state *, pic_value port, const char *, ...); +pic_value pic_display(pic_state *, pic_value); +pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); + +/* utility macros */ + +#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)) + +#define pic_assert_type(pic, v, type) do { \ + if (! pic_##type##_p(pic, v)) \ + pic_errorf(pic, "expected " #type ", but got ~s", v); \ + } while (0) + +#define pic_void(exec) pic_void_(PIC_GENSYM(ai), exec) #define pic_void_(ai,exec) do { \ size_t ai = pic_enter(pic); \ exec; \ pic_leave(pic, ai); \ } while (0) -pic_value pic_read(pic_state *, pic_value port); -pic_value pic_read_cstr(pic_state *, const char *); - -pic_value pic_expand(pic_state *, pic_value program, pic_value env); - -pic_value pic_eval(pic_state *, pic_value program, const char *lib); - -void pic_load(pic_state *, pic_value port); -void pic_load_cstr(pic_state *, const char *); - #define pic_deflibrary(pic, lib) do { \ if (! pic_find_library(pic, lib)) { \ pic_make_library(pic, lib); \ @@ -60,29 +78,23 @@ void pic_load_cstr(pic_state *, const char *); pic_in_library(pic, lib); \ } while (0) +/* for pic_try & pic_catch macros */ struct pic_cont *pic_alloca_cont(pic_state *); pic_value pic_make_cont(pic_state *, struct pic_cont *); void pic_push_native_handler(pic_state *, struct pic_cont *); -void pic_push_handler(pic_state *, pic_value); pic_value pic_pop_handler(pic_state *); void pic_save_point(pic_state *, struct pic_cont *, PIC_JMPBUF *); void pic_exit_point(pic_state *); -/* do not return from try block! */ - -pic_value pic_err(pic_state *); - -#define pic_try \ - pic_try_(PIC_GENSYM(cont), PIC_GENSYM(handler)) -#define pic_catch \ - pic_catch_(PIC_GENSYM(label)) -#define pic_try_(cont, handler) \ +#define pic_try pic_try_(PIC_GENSYM(cont), PIC_GENSYM(jmp)) +#define pic_try_(cont, jmp) \ do { \ PIC_JMPBUF jmp; \ struct pic_cont *cont = pic_alloca_cont(pic); \ if (PIC_SETJMP(pic, jmp) == 0) { \ pic_save_point(pic, cont, &jmp); \ pic_push_native_handler(pic, cont); +#define pic_catch pic_catch_(PIC_GENSYM(label)) #define pic_catch_(label) \ pic_pop_handler(pic); \ pic_exit_point(pic); \ @@ -93,30 +105,15 @@ pic_value pic_err(pic_state *); if (0) \ label: +pic_value pic_err(pic_state *); + +/* for debug */ + PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); - -#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 *, ...); pic_value pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); -#define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0) -#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0) -#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0) - -pic_value pic_write(pic_state *, pic_value); /* returns given obj */ -pic_value pic_fwrite(pic_state *, pic_value, xFILE *); -void pic_printf(pic_state *, const char *, ...); -void pic_fprintf(pic_state *, pic_value port, const char *, ...); -pic_value pic_display(pic_state *, pic_value); -pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); - pic_value pic_library_environment(pic_state *, const char *); #if defined(__cplusplus) diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 86aee1b2..999a6f96 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -147,8 +147,7 @@ typedef unsigned long uint32_t; # undef GCC_VERSION #endif -#define PIC_SWAP(type,a,b) \ - PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b) +#define PIC_SWAP(type,a,b) PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b) #define PIC_SWAP_HELPER_(type,tmp,a,b) \ do { \ type tmp = (a); \ From 1a8bc0bc66f618a1f18804cb0ccf0603267d61a5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 22 Feb 2016 23:19:30 +0900 Subject: [PATCH 107/119] update readme --- extlib/benz/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extlib/benz/README.md b/extlib/benz/README.md index 71b1e62b..bdeef48d 100644 --- a/extlib/benz/README.md +++ b/extlib/benz/README.md @@ -10,6 +10,7 @@ Originally, Benz used to be the core component of [Picrin Scheme](https://github #include #include "picrin.h" +#include "picrin/extra.h" /* Simple REPL program */ @@ -26,11 +27,11 @@ main(int argc, char *argv[]) expr = pic_read(pic, pic_stdin(pic)); - if (pic_eof_p(expr)) { + if (pic_eof_p(pic, expr)) { break; } - pic_printf(pic, "~s\n", pic_eval(pic, expr, pic->lib)); + pic_printf(pic, "~s\n", pic_eval(pic, expr, "picrin.user")); } pic_close(pic); @@ -45,6 +46,7 @@ Function binding is also easy. `pic_defun` defines a scheme procedure converting ```c #include "picrin.h" +#include "picrin/extra.h" int fact(int i) { return i == 1 ? 1 : i * fact(i - 1); From 8d17bf3175f112a576765bf90edb77ad17d2699a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 22 Feb 2016 23:49:39 +0900 Subject: [PATCH 108/119] change pic_add_identifier's behavior --- extlib/benz/eval.c | 6 +- extlib/benz/include/picrin/private/object.h | 2 +- extlib/benz/macro.c | 84 ++++++++++----------- extlib/benz/state.c | 12 --- 4 files changed, 45 insertions(+), 59 deletions(-) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 1717ae36..7b648909 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -116,7 +116,7 @@ analyzer_scope_destroy(pic_state *PIC_UNUSED(pic), analyze_scope *PIC_UNUSED(sco } static bool -search_scope(pic_state *pic, analyze_scope *scope, pic_value sym) +find_local_var(pic_state *pic, analyze_scope *scope, pic_value sym) { return pic_dict_has(pic, scope->args, sym) || pic_dict_has(pic, scope->locals, sym) || scope->depth == 0; } @@ -127,7 +127,7 @@ find_var(pic_state *pic, analyze_scope *scope, pic_value sym) int depth = 0; while (scope) { - if (search_scope(pic, scope, sym)) { + if (find_local_var(pic, scope, sym)) { if (depth > 0) { pic_dict_set(pic, scope->captures, sym, pic_true_value(pic)); /* capture! */ } @@ -144,7 +144,7 @@ define_var(pic_state *pic, analyze_scope *scope, pic_value sym) { if (scope->depth > 0) { /* local */ - if (search_scope(pic, scope, sym)) { + if (find_local_var(pic, scope, sym)) { pic_warnf(pic, "redefining variable: ~s", sym); return; } diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index 64b1d6d9..128a5fd2 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -179,7 +179,7 @@ pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_val pic_value pic_make_rec(pic_state *, pic_value type, pic_value datum); pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env); -pic_value pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env); +void pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env); pic_value pic_find_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_id_name(pic_state *, pic_value id); diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index f1372bef..e0ce2185 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -22,36 +22,6 @@ pic_make_env(pic_state *pic, pic_value up) return pic_obj_value(env); } -pic_value -pic_add_identifier(pic_state *pic, pic_value id, pic_value env) -{ - const char *name; - pic_value uid, str; - - name = pic_str(pic, pic_id_name(pic, id)); - - if (pic_env_ptr(pic, env)->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ - str = pic_strf_value(pic, "~a/%s", pic_obj_value(pic_env_ptr(pic, env)->lib), name); - } else { - str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); - } - uid = pic_intern(pic, str); - - return pic_put_identifier(pic, id, uid, env); -} - -pic_value -pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) -{ - int it; - int ret; - - it = kh_put(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id), &ret); - kh_val(&pic_env_ptr(pic, env)->map, it) = pic_sym_ptr(pic, uid); - - return uid; -} - static bool search_scope(pic_state *pic, pic_value id, pic_value env, pic_value *uid) { @@ -103,6 +73,39 @@ pic_find_identifier(pic_state *pic, pic_value id, pic_value env) return uid; } +pic_value +pic_add_identifier(pic_state *pic, pic_value id, pic_value env) +{ + const char *name; + pic_value uid, str; + + if (search_scope(pic, id, env, &uid)) { + return uid; + } + + name = pic_str(pic, pic_id_name(pic, id)); + + if (pic_env_ptr(pic, env)->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ + str = pic_strf_value(pic, "~a/%s", pic_obj_value(pic_env_ptr(pic, env)->lib), name); + } else { + str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); + } + uid = pic_intern(pic, str); + + pic_put_identifier(pic, id, uid, env); + + return uid; +} + +void +pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) +{ + int it, ret; + + it = kh_put(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id), &ret); + kh_val(&pic_env_ptr(pic, env)->map, it) = pic_sym_ptr(pic, uid); +} + /** * macro expander @@ -238,14 +241,12 @@ expand_lambda(pic_state *pic, pic_value expr, pic_value env) static pic_value expand_define(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) { - pic_value id, uid, val; + pic_value uid, val; + + uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env); + + shadow_macro(pic, uid); - id = pic_cadr(pic, expr); - if (! search_scope(pic, id, env, &uid)) { - uid = pic_add_identifier(pic, id, env); - } else { - shadow_macro(pic, uid); - } val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred); return pic_list(pic, 3, S("define"), uid, val); @@ -255,16 +256,13 @@ static pic_value expand_defmacro(pic_state *pic, pic_value expr, pic_value env) { pic_value pic_compile(pic_state *, pic_value); - pic_value id, uid, val; + pic_value uid, val; - id = pic_cadr(pic, expr); - if (! search_scope(pic, id, env, &uid)) { - uid = pic_add_identifier(pic, id, env); - } + uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env); val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); if (! pic_proc_p(pic, val)) { - pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", id); + pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_list_ref(pic, expr, 1)); } define_macro(pic, uid, val); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 5f4c5adb..373412b8 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -352,18 +352,6 @@ pic_close(pic_state *pic) /* free all heap objects */ pic_gc(pic); -#if 0 - { - /* FIXME */ - int i = 0; - struct list_head *list; - for (list = pic->ireps.next; list != &pic->ireps; list = list->next) { - i++; - } - printf("%d\n", i); - } -#endif - /* flush all xfiles */ xfflush(pic, NULL); From c34f24e58cf268130e2b8f9d662ab3cb87e5273f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 22 Feb 2016 23:57:07 +0900 Subject: [PATCH 109/119] remove macro.c --- extlib/benz/eval.c | 233 +++++++++++++++++++++++++++++- extlib/benz/lib.c | 98 +++++++++++++ extlib/benz/macro.c | 342 -------------------------------------------- 3 files changed, 326 insertions(+), 347 deletions(-) delete mode 100644 extlib/benz/macro.c diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 7b648909..8fd8adbc 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -8,9 +8,236 @@ #include "picrin/private/vm.h" #include "picrin/private/state.h" +static pic_value pic_compile(pic_state *, pic_value); + #define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) #define S(lit) (pic_intern_lit(pic, lit)) +static void +define_macro(pic_state *pic, pic_value uid, pic_value mac) +{ + if (pic_weak_has(pic, pic->macros, uid)) { + pic_warnf(pic, "redefining syntax variable: ~s", uid); + } + pic_weak_set(pic, pic->macros, uid, mac); +} + +static bool +find_macro(pic_state *pic, pic_value uid, pic_value *mac) +{ + if (! pic_weak_has(pic, pic->macros, uid)) { + return false; + } + *mac = pic_weak_ref(pic, pic->macros, uid); + return true; +} + +static void +shadow_macro(pic_state *pic, pic_value uid) +{ + if (pic_weak_has(pic, pic->macros, uid)) { + pic_weak_del(pic, pic->macros, uid); + } +} + +static pic_value expand(pic_state *, pic_value expr, pic_value env, pic_value deferred); +static pic_value expand_lambda(pic_state *, pic_value expr, pic_value env); + +static pic_value +expand_var(pic_state *pic, pic_value id, pic_value env, pic_value deferred) +{ + pic_value mac, functor; + + functor = pic_find_identifier(pic, id, env); + + if (find_macro(pic, functor, &mac)) { + return expand(pic, pic_call(pic, mac, 2, id, env), env, deferred); + } + return functor; +} + +static pic_value +expand_quote(pic_state *pic, pic_value expr) +{ + return pic_cons(pic, S("quote"), pic_cdr(pic, expr)); +} + +static pic_value +expand_list(pic_state *pic, pic_value obj, pic_value env, pic_value deferred) +{ + size_t ai = pic_enter(pic); + pic_value x, head, tail; + + if (pic_pair_p(pic, obj)) { + head = expand(pic, pic_car(pic, obj), env, deferred); + tail = expand_list(pic, pic_cdr(pic, obj), env, deferred); + x = pic_cons(pic, head, tail); + } else { + x = expand(pic, obj, env, deferred); + } + + pic_leave(pic, ai); + pic_protect(pic, x); + return x; +} + +static pic_value +expand_defer(pic_state *pic, pic_value expr, pic_value deferred) +{ + pic_value skel = pic_cons(pic, pic_invalid_value(pic), pic_invalid_value(pic)); + + pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred))); + + return skel; +} + +static void +expand_deferred(pic_state *pic, pic_value deferred, pic_value env) +{ + pic_value defer, val, src, dst, it; + + deferred = pic_car(pic, deferred); + + pic_for_each (defer, pic_reverse(pic, deferred), it) { + src = pic_car(pic, defer); + dst = pic_cdr(pic, defer); + + val = expand_lambda(pic, src, env); + + /* copy */ + pic_set_car(pic, dst, pic_car(pic, val)); + pic_set_cdr(pic, dst, pic_cdr(pic, val)); + } +} + +static pic_value +expand_lambda(pic_state *pic, pic_value expr, pic_value env) +{ + pic_value formal, body; + pic_value in; + pic_value a, deferred; + + in = pic_make_env(pic, env); + + for (a = pic_cadr(pic, expr); pic_pair_p(pic, a); a = pic_cdr(pic, a)) { + pic_add_identifier(pic, pic_car(pic, a), in); + } + if (pic_id_p(pic, a)) { + pic_add_identifier(pic, a, in); + } + + deferred = pic_list(pic, 1, pic_nil_value(pic)); + + formal = expand_list(pic, pic_list_ref(pic, expr, 1), in, deferred); + body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred); + + expand_deferred(pic, deferred, in); + + return pic_list(pic, 3, S("lambda"), formal, body); +} + +static pic_value +expand_define(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) +{ + pic_value uid, val; + + uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env); + + shadow_macro(pic, uid); + + val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred); + + return pic_list(pic, 3, S("define"), uid, val); +} + +static pic_value +expand_defmacro(pic_state *pic, pic_value expr, pic_value env) +{ + pic_value uid, val; + + uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env); + + val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); + if (! pic_proc_p(pic, val)) { + pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_list_ref(pic, expr, 1)); + } + + define_macro(pic, uid, val); + + return pic_undef_value(pic); +} + +static pic_value +expand_node(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) +{ + switch (pic_type(pic, expr)) { + case PIC_TYPE_ID: + case PIC_TYPE_SYMBOL: { + return expand_var(pic, expr, env, deferred); + } + case PIC_TYPE_PAIR: { + pic_value mac; + + if (! pic_list_p(pic, expr)) { + pic_errorf(pic, "cannot expand improper list: ~s", expr); + } + + if (pic_id_p(pic, pic_car(pic, expr))) { + pic_value functor; + + functor = pic_find_identifier(pic, pic_car(pic, expr), env); + + if (EQ(functor, "define-macro")) { + return expand_defmacro(pic, expr, env); + } + else if (EQ(functor, "lambda")) { + return expand_defer(pic, expr, deferred); + } + else if (EQ(functor, "define")) { + return expand_define(pic, expr, env, deferred); + } + else if (EQ(functor, "quote")) { + return expand_quote(pic, expr); + } + + if (find_macro(pic, functor, &mac)) { + return expand(pic, pic_call(pic, mac, 2, expr, env), env, deferred); + } + } + return expand_list(pic, expr, env, deferred); + } + default: + return expr; + } +} + +static pic_value +expand(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) +{ + size_t ai = pic_enter(pic); + pic_value v; + + v = expand_node(pic, expr, env, deferred); + + pic_leave(pic, ai); + pic_protect(pic, v); + return v; +} + +pic_value +pic_expand(pic_state *pic, pic_value expr, pic_value env) +{ + pic_value v, deferred; + + deferred = pic_list(pic, 1, pic_nil_value(pic)); + + v = expand(pic, expr, env, deferred); + + expand_deferred(pic, deferred, env); + + return v; +} + static pic_value optimize_beta(pic_state *pic, pic_value expr) { @@ -73,10 +300,6 @@ pic_optimize(pic_state *pic, pic_value expr) return optimize_beta(pic, expr); } -/** - * TODO: don't use khash_t, use kvec_t instead - */ - typedef struct analyze_scope { int depth; pic_value rest; /* Nullable */ @@ -819,7 +1042,7 @@ pic_codegen(pic_state *pic, pic_value obj) #define SAVE(pic, ai, obj) pic_leave(pic, ai); pic_protect(pic, obj) -pic_value +static pic_value pic_compile(pic_state *pic, pic_value obj) { struct irep *irep; diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index c5bb5cad..c058bcd1 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -7,8 +7,106 @@ #include "picrin/private/object.h" #include "picrin/private/state.h" +KHASH_DEFINE(env, struct identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal) KHASH_DEFINE(ltable, const char *, struct lib, kh_str_hash_func, kh_str_cmp_func) +pic_value +pic_make_env(pic_state *pic, pic_value up) +{ + struct env *env; + + env = (struct env *)pic_obj_alloc(pic, sizeof(struct env), PIC_TYPE_ENV); + env->up = pic_env_ptr(pic, up); + env->lib = NULL; + kh_init(env, &env->map); + + return pic_obj_value(env); +} + +static bool +search_scope(pic_state *pic, pic_value id, pic_value env, pic_value *uid) +{ + int it; + + it = kh_get(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id)); + if (it == kh_end(&pic_env_ptr(pic, env)->map)) { + return false; + } + *uid = pic_obj_value(kh_val(&pic_env_ptr(pic, env)->map, it)); + return true; +} + +static bool +search(pic_state *pic, pic_value id, pic_value env, pic_value *uid) +{ + struct env *e; + + while (1) { + if (search_scope(pic, id, env, uid)) + return true; + e = pic_env_ptr(pic, env)->up; + if (e == NULL) + break; + env = pic_obj_value(e); + } + return false; +} + +pic_value +pic_find_identifier(pic_state *pic, pic_value id, pic_value env) +{ + struct env *e; + pic_value uid; + + while (! search(pic, id, env, &uid)) { + if (pic_sym_p(pic, id)) { + while (1) { + e = pic_env_ptr(pic, env); + if (e->up == NULL) + break; + env = pic_obj_value(e->up); + } + return pic_add_identifier(pic, id, env); + } + env = pic_obj_value(pic_id_ptr(pic, id)->env); /* do not overwrite id first */ + id = pic_obj_value(pic_id_ptr(pic, id)->u.id); + } + return uid; +} + +pic_value +pic_add_identifier(pic_state *pic, pic_value id, pic_value env) +{ + const char *name; + pic_value uid, str; + + if (search_scope(pic, id, env, &uid)) { + return uid; + } + + name = pic_str(pic, pic_id_name(pic, id)); + + if (pic_env_ptr(pic, env)->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ + str = pic_strf_value(pic, "~a/%s", pic_obj_value(pic_env_ptr(pic, env)->lib), name); + } else { + str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); + } + uid = pic_intern(pic, str); + + pic_put_identifier(pic, id, uid, env); + + return uid; +} + +void +pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) +{ + int it, ret; + + it = kh_put(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id), &ret); + kh_val(&pic_env_ptr(pic, env)->map, it) = pic_sym_ptr(pic, uid); +} + static struct lib * get_library_opt(pic_state *pic, const char *lib) { diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c deleted file mode 100644 index e0ce2185..00000000 --- a/extlib/benz/macro.c +++ /dev/null @@ -1,342 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#include "picrin.h" -#include "picrin/extra.h" -#include "picrin/private/object.h" -#include "picrin/private/state.h" - -KHASH_DEFINE(env, struct identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal) - -pic_value -pic_make_env(pic_state *pic, pic_value up) -{ - struct env *env; - - env = (struct env *)pic_obj_alloc(pic, sizeof(struct env), PIC_TYPE_ENV); - env->up = pic_env_ptr(pic, up); - env->lib = NULL; - kh_init(env, &env->map); - - return pic_obj_value(env); -} - -static bool -search_scope(pic_state *pic, pic_value id, pic_value env, pic_value *uid) -{ - int it; - - it = kh_get(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id)); - if (it == kh_end(&pic_env_ptr(pic, env)->map)) { - return false; - } - *uid = pic_obj_value(kh_val(&pic_env_ptr(pic, env)->map, it)); - return true; -} - -static bool -search(pic_state *pic, pic_value id, pic_value env, pic_value *uid) -{ - struct env *e; - - while (1) { - if (search_scope(pic, id, env, uid)) - return true; - e = pic_env_ptr(pic, env)->up; - if (e == NULL) - break; - env = pic_obj_value(e); - } - return false; -} - -pic_value -pic_find_identifier(pic_state *pic, pic_value id, pic_value env) -{ - struct env *e; - pic_value uid; - - while (! search(pic, id, env, &uid)) { - if (pic_sym_p(pic, id)) { - while (1) { - e = pic_env_ptr(pic, env); - if (e->up == NULL) - break; - env = pic_obj_value(e->up); - } - return pic_add_identifier(pic, id, env); - } - env = pic_obj_value(pic_id_ptr(pic, id)->env); /* do not overwrite id first */ - id = pic_obj_value(pic_id_ptr(pic, id)->u.id); - } - return uid; -} - -pic_value -pic_add_identifier(pic_state *pic, pic_value id, pic_value env) -{ - const char *name; - pic_value uid, str; - - if (search_scope(pic, id, env, &uid)) { - return uid; - } - - name = pic_str(pic, pic_id_name(pic, id)); - - if (pic_env_ptr(pic, env)->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ - str = pic_strf_value(pic, "~a/%s", pic_obj_value(pic_env_ptr(pic, env)->lib), name); - } else { - str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); - } - uid = pic_intern(pic, str); - - pic_put_identifier(pic, id, uid, env); - - return uid; -} - -void -pic_put_identifier(pic_state *pic, pic_value id, pic_value uid, pic_value env) -{ - int it, ret; - - it = kh_put(env, &pic_env_ptr(pic, env)->map, pic_id_ptr(pic, id), &ret); - kh_val(&pic_env_ptr(pic, env)->map, it) = pic_sym_ptr(pic, uid); -} - - -/** - * macro expander - */ - - -static void -define_macro(pic_state *pic, pic_value uid, pic_value mac) -{ - if (pic_weak_has(pic, pic->macros, uid)) { - pic_warnf(pic, "redefining syntax variable: ~s", uid); - } - pic_weak_set(pic, pic->macros, uid, mac); -} - -static bool -find_macro(pic_state *pic, pic_value uid, pic_value *mac) -{ - if (! pic_weak_has(pic, pic->macros, uid)) { - return false; - } - *mac = pic_weak_ref(pic, pic->macros, uid); - return true; -} - -static void -shadow_macro(pic_state *pic, pic_value uid) -{ - if (pic_weak_has(pic, pic->macros, uid)) { - pic_weak_del(pic, pic->macros, uid); - } -} - -static pic_value expand(pic_state *, pic_value expr, pic_value env, pic_value deferred); -static pic_value expand_lambda(pic_state *, pic_value expr, pic_value env); - -#define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) -#define S(lit) (pic_intern_lit(pic, lit)) - -static pic_value -expand_var(pic_state *pic, pic_value id, pic_value env, pic_value deferred) -{ - pic_value mac, functor; - - functor = pic_find_identifier(pic, id, env); - - if (find_macro(pic, functor, &mac)) { - return expand(pic, pic_call(pic, mac, 2, id, env), env, deferred); - } - return functor; -} - -static pic_value -expand_quote(pic_state *pic, pic_value expr) -{ - return pic_cons(pic, S("quote"), pic_cdr(pic, expr)); -} - -static pic_value -expand_list(pic_state *pic, pic_value obj, pic_value env, pic_value deferred) -{ - size_t ai = pic_enter(pic); - pic_value x, head, tail; - - if (pic_pair_p(pic, obj)) { - head = expand(pic, pic_car(pic, obj), env, deferred); - tail = expand_list(pic, pic_cdr(pic, obj), env, deferred); - x = pic_cons(pic, head, tail); - } else { - x = expand(pic, obj, env, deferred); - } - - pic_leave(pic, ai); - pic_protect(pic, x); - return x; -} - -static pic_value -expand_defer(pic_state *pic, pic_value expr, pic_value deferred) -{ - pic_value skel = pic_cons(pic, pic_invalid_value(pic), pic_invalid_value(pic)); - - pic_set_car(pic, deferred, pic_cons(pic, pic_cons(pic, expr, skel), pic_car(pic, deferred))); - - return skel; -} - -static void -expand_deferred(pic_state *pic, pic_value deferred, pic_value env) -{ - pic_value defer, val, src, dst, it; - - deferred = pic_car(pic, deferred); - - pic_for_each (defer, pic_reverse(pic, deferred), it) { - src = pic_car(pic, defer); - dst = pic_cdr(pic, defer); - - val = expand_lambda(pic, src, env); - - /* copy */ - pic_set_car(pic, dst, pic_car(pic, val)); - pic_set_cdr(pic, dst, pic_cdr(pic, val)); - } -} - -static pic_value -expand_lambda(pic_state *pic, pic_value expr, pic_value env) -{ - pic_value formal, body; - pic_value in; - pic_value a, deferred; - - in = pic_make_env(pic, env); - - for (a = pic_cadr(pic, expr); pic_pair_p(pic, a); a = pic_cdr(pic, a)) { - pic_add_identifier(pic, pic_car(pic, a), in); - } - if (pic_id_p(pic, a)) { - pic_add_identifier(pic, a, in); - } - - deferred = pic_list(pic, 1, pic_nil_value(pic)); - - formal = expand_list(pic, pic_list_ref(pic, expr, 1), in, deferred); - body = expand(pic, pic_list_ref(pic, expr, 2), in, deferred); - - expand_deferred(pic, deferred, in); - - return pic_list(pic, 3, S("lambda"), formal, body); -} - -static pic_value -expand_define(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) -{ - pic_value uid, val; - - uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env); - - shadow_macro(pic, uid); - - val = expand(pic, pic_list_ref(pic, expr, 2), env, deferred); - - return pic_list(pic, 3, S("define"), uid, val); -} - -static pic_value -expand_defmacro(pic_state *pic, pic_value expr, pic_value env) -{ - pic_value pic_compile(pic_state *, pic_value); - pic_value uid, val; - - uid = pic_add_identifier(pic, pic_list_ref(pic, expr, 1), env); - - val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); - if (! pic_proc_p(pic, val)) { - pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_list_ref(pic, expr, 1)); - } - - define_macro(pic, uid, val); - - return pic_undef_value(pic); -} - -static pic_value -expand_node(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) -{ - switch (pic_type(pic, expr)) { - case PIC_TYPE_ID: - case PIC_TYPE_SYMBOL: { - return expand_var(pic, expr, env, deferred); - } - case PIC_TYPE_PAIR: { - pic_value mac; - - if (! pic_list_p(pic, expr)) { - pic_errorf(pic, "cannot expand improper list: ~s", expr); - } - - if (pic_id_p(pic, pic_car(pic, expr))) { - pic_value functor; - - functor = pic_find_identifier(pic, pic_car(pic, expr), env); - - if (EQ(functor, "define-macro")) { - return expand_defmacro(pic, expr, env); - } - else if (EQ(functor, "lambda")) { - return expand_defer(pic, expr, deferred); - } - else if (EQ(functor, "define")) { - return expand_define(pic, expr, env, deferred); - } - else if (EQ(functor, "quote")) { - return expand_quote(pic, expr); - } - - if (find_macro(pic, functor, &mac)) { - return expand(pic, pic_call(pic, mac, 2, expr, env), env, deferred); - } - } - return expand_list(pic, expr, env, deferred); - } - default: - return expr; - } -} - -static pic_value -expand(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) -{ - size_t ai = pic_enter(pic); - pic_value v; - - v = expand_node(pic, expr, env, deferred); - - pic_leave(pic, ai); - pic_protect(pic, v); - return v; -} - -pic_value -pic_expand(pic_state *pic, pic_value expr, pic_value env) -{ - pic_value v, deferred; - - deferred = pic_list(pic, 1, pic_nil_value(pic)); - - v = expand(pic, expr, env, deferred); - - expand_deferred(pic, deferred, env); - - return v; -} From 368fa17de3f4dc7b700730f453b69e3c35d1d616 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 02:15:37 +0900 Subject: [PATCH 110/119] do not use '~s' style format specifier with pic_strf_value --- extlib/benz/lib.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index c058bcd1..c1266d77 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -77,7 +77,7 @@ pic_find_identifier(pic_state *pic, pic_value id, pic_value env) pic_value pic_add_identifier(pic_state *pic, pic_value id, pic_value env) { - const char *name; + const char *name, *lib; pic_value uid, str; if (search_scope(pic, id, env, &uid)) { @@ -87,7 +87,8 @@ pic_add_identifier(pic_state *pic, pic_value id, pic_value env) name = pic_str(pic, pic_id_name(pic, id)); if (pic_env_ptr(pic, env)->up == NULL && pic_sym_p(pic, id)) { /* toplevel & public */ - str = pic_strf_value(pic, "~a/%s", pic_obj_value(pic_env_ptr(pic, env)->lib), name); + lib = pic_str(pic, pic_obj_value(pic_env_ptr(pic, env)->lib)); + str = pic_strf_value(pic, "%s/%s", lib, name); } else { str = pic_strf_value(pic, ".%s.%d", name, pic->ucnt++); } From 229555fa838f1c7c023d714749fed8222691f7c8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 02:23:22 +0900 Subject: [PATCH 111/119] remove pic_error --- contrib/20.r7rs/src/file.c | 2 +- extlib/benz/error.c | 10 ++-------- extlib/benz/include/picrin.h | 1 + extlib/benz/include/picrin/extra.h | 1 - extlib/benz/include/picrin/private/object.h | 1 - extlib/benz/read.c | 2 +- 6 files changed, 5 insertions(+), 12 deletions(-) diff --git a/contrib/20.r7rs/src/file.c b/contrib/20.r7rs/src/file.c index 62942ecd..60e23508 100644 --- a/contrib/20.r7rs/src/file.c +++ b/contrib/20.r7rs/src/file.c @@ -10,7 +10,7 @@ PIC_NORETURN static void file_error(pic_state *pic, const char *msg) { - pic_error(pic, "file", msg, pic_nil_value(pic)); + pic_raise(pic, pic_make_error(pic, "file", msg, pic_nil_value(pic))); } static pic_value diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 71050cb0..1111567f 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -49,7 +49,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) msg = pic_str(pic, err); - pic_error(pic, "", msg, pic_nil_value(pic)); + pic_raise(pic, pic_make_error(pic, "", msg, pic_nil_value(pic))); } void @@ -154,12 +154,6 @@ pic_raise(pic_state *pic, pic_value err) pic_errorf(pic, "error handler returned with ~s on error ~s", val, err); } -void -pic_error(pic_state *pic, const char *type, const char *msg, pic_value irrs) -{ - pic_raise(pic, pic_make_error(pic, type, msg, irrs)); -} - static pic_value pic_error_with_exception_handler(pic_state *pic) { @@ -205,7 +199,7 @@ pic_error_error(pic_state *pic) pic_get_args(pic, "z*", &str, &argc, &argv); - pic_error(pic, "", str, pic_make_list(pic, argc, argv)); + pic_raise(pic, pic_make_error(pic, "", str, pic_make_list(pic, argc, argv))); } static pic_value diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index cd1bf61d..1d5e1ad9 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -102,6 +102,7 @@ pic_panicf pic_atpanic(pic_state *, pic_panicf f); PIC_NORETURN void pic_panic(pic_state *, const char *msg); PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); PIC_NORETURN void pic_raise(pic_state *, pic_value v); +pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs); pic_value pic_lambda(pic_state *, pic_func_t f, int n, ...); pic_value pic_vlambda(pic_state *, pic_func_t f, int n, va_list); diff --git a/extlib/benz/include/picrin/extra.h b/extlib/benz/include/picrin/extra.h index 244d5b23..ed8ea6cb 100644 --- a/extlib/benz/include/picrin/extra.h +++ b/extlib/benz/include/picrin/extra.h @@ -109,7 +109,6 @@ pic_value pic_err(pic_state *); /* for debug */ -PIC_NORETURN void pic_error(pic_state *, const char *type, const char *msg, pic_value irrs); void pic_warnf(pic_state *, const char *, ...); pic_value pic_get_backtrace(pic_state *); void pic_print_backtrace(pic_state *, xFILE *); diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index 128a5fd2..ec0ec402 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -175,7 +175,6 @@ pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); pic_value pic_make_proc_irep(pic_state *, struct irep *, struct context *); pic_value pic_make_env(pic_state *, pic_value env); -pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs); pic_value pic_make_rec(pic_state *, pic_value type, pic_value datum); pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env); diff --git a/extlib/benz/read.c b/extlib/benz/read.c index ed0cd4a6..2325e0f8 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -31,7 +31,7 @@ static pic_value read_nullable(pic_state *pic, xFILE *file, int c, struct reader PIC_NORETURN static void read_error(pic_state *pic, const char *msg, pic_value irritants) { - pic_error(pic, "read", msg, irritants); + pic_raise(pic, pic_make_error(pic, "read", msg, irritants)); } static int From e050da8af1c1dc4c6e1686382a4a1b3867029f61 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 02:37:33 +0900 Subject: [PATCH 112/119] add pic_sym macro --- extlib/benz/eval.c | 2 +- extlib/benz/include/picrin.h | 1 + extlib/benz/read.c | 8 ++++---- extlib/benz/write.c | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 8fd8adbc..7a55ab54 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -10,7 +10,7 @@ static pic_value pic_compile(pic_state *, pic_value); -#define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) +#define EQ(sym, lit) (strcmp(pic_sym(pic, sym), lit) == 0) #define S(lit) (pic_intern_lit(pic, lit)) static void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 1d5e1ad9..f255d2e3 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -122,6 +122,7 @@ double pic_float(pic_state *, pic_value f); char pic_char(pic_state *, pic_value c); #define pic_bool(pic,b) (! pic_false_p(pic, b)) const char *pic_str(pic_state *, pic_value str); +#define pic_sym(pic,s) (pic_str(pic, pic_sym_name(pic, (s)))) unsigned char *pic_blob(pic_state *, pic_value blob, int *len); void *pic_data(pic_state *, pic_value data); diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 2325e0f8..5db65c72 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -339,10 +339,10 @@ read_minus(pic_state *pic, xFILE *file, int c, struct reader_control *p) } else { sym = read_symbol(pic, file, c, p); - if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "-inf.0")) { + if (strcaseeq(pic_sym(pic, sym), "-inf.0")) { return pic_float_value(pic, -(1.0 / 0.0)); } - if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "-nan.0")) { + if (strcaseeq(pic_sym(pic, sym), "-nan.0")) { return pic_float_value(pic, -(0.0 / 0.0)); } return sym; @@ -359,10 +359,10 @@ read_plus(pic_state *pic, xFILE *file, int c, struct reader_control *p) } else { sym = read_symbol(pic, file, c, p); - if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "+inf.0")) { + if (strcaseeq(pic_sym(pic, sym), "+inf.0")) { return pic_float_value(pic, 1.0 / 0.0); } - if (strcaseeq(pic_str(pic, pic_sym_name(pic, sym)), "+nan.0")) { + if (strcaseeq(pic_sym(pic, sym), "+nan.0")) { return pic_float_value(pic, 0.0 / 0.0); } return sym; diff --git a/extlib/benz/write.c b/extlib/benz/write.c index ce56943c..bb993503 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -195,7 +195,7 @@ write_pair_help(pic_state *pic, pic_value pair, xFILE *file, struct writer_contr } } -#define EQ(sym, lit) (strcmp(pic_str(pic, pic_sym_name(pic, sym)), lit) == 0) +#define EQ(sym, lit) (strcmp(pic_sym(pic, sym), lit) == 0) static void write_pair(pic_state *pic, pic_value pair, xFILE *file, struct writer_control *p) @@ -273,7 +273,7 @@ write_dict(pic_state *pic, pic_value dict, xFILE *file, struct writer_control *p xfprintf(pic, file, "#.(dictionary"); while (pic_dict_next(pic, dict, &it, &key, &val)) { - xfprintf(pic, file, " '%s ", pic_str(pic, pic_sym_name(pic, key))); + xfprintf(pic, file, " '%s ", pic_sym(pic, key)); write_core(pic, val, file, p); } xfprintf(pic, file, ")"); @@ -319,7 +319,7 @@ write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p) xfprintf(pic, file, "%d", pic_int(pic, obj)); break; case PIC_TYPE_SYMBOL: - xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, obj))); + xfprintf(pic, file, "%s", pic_sym(pic, obj)); break; case PIC_TYPE_FLOAT: write_float(pic, obj, file); From 42d0ecc6336ac98a4a34ec366c8e851396faac49 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 02:39:33 +0900 Subject: [PATCH 113/119] don't use '~s' format specifier with pic_warnf --- extlib/benz/eval.c | 6 +++--- extlib/benz/proc.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 7a55ab54..41e60d4b 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -17,7 +17,7 @@ static void define_macro(pic_state *pic, pic_value uid, pic_value mac) { if (pic_weak_has(pic, pic->macros, uid)) { - pic_warnf(pic, "redefining syntax variable: ~s", uid); + pic_warnf(pic, "redefining syntax variable: %s", pic_sym(pic, uid)); } pic_weak_set(pic, pic->macros, uid, mac); } @@ -368,14 +368,14 @@ define_var(pic_state *pic, analyze_scope *scope, pic_value sym) if (scope->depth > 0) { /* local */ if (find_local_var(pic, scope, sym)) { - pic_warnf(pic, "redefining variable: ~s", sym); + pic_warnf(pic, "redefining variable: %s", pic_sym(pic, sym)); return; } pic_dict_set(pic, scope->locals, sym, pic_true_value(pic)); } else { /* global */ if (pic_weak_has(pic, pic->globals, sym)) { - pic_warnf(pic, "redefining variable: ~s", sym); + pic_warnf(pic, "redefining variable: %s", pic_sym(pic, sym)); return; } pic_weak_set(pic, pic->globals, sym, pic_invalid_value(pic)); diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 9b9070d2..570f1730 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -872,7 +872,7 @@ pic_define(pic_state *pic, const char *lib, const char *name, pic_value val) uid = pic_find_identifier(pic, sym, env); if (pic_weak_has(pic, pic->globals, uid)) { - pic_warnf(pic, "redefining variable: ~s", uid); + pic_warnf(pic, "redefining variable: %s", pic_sym(pic, uid)); } pic_weak_set(pic, pic->globals, uid, val); } From fae7ef0376a11e0c3c56be9dea3d900f5e520c76 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 04:03:42 +0900 Subject: [PATCH 114/119] pic_errorf -> pic_error --- contrib/20.r7rs/src/load.c | 2 +- contrib/30.readline/src/readline.c | 6 +-- contrib/30.regexp/src/regexp.c | 2 +- contrib/40.srfi/src/106.c | 16 ++++---- extlib/benz/blob.c | 10 ++--- extlib/benz/bool.c | 2 +- extlib/benz/char.c | 17 ++++---- extlib/benz/cont.c | 2 +- extlib/benz/dict.c | 4 +- extlib/benz/error.c | 15 +++---- extlib/benz/eval.c | 8 ++-- extlib/benz/include/picrin.h | 2 +- extlib/benz/include/picrin/extra.h | 6 +-- extlib/benz/include/picrin/private/object.h | 14 +++---- extlib/benz/lib.c | 10 ++--- extlib/benz/number.c | 12 +++--- extlib/benz/pair.c | 15 +++---- extlib/benz/port.c | 10 ++--- extlib/benz/proc.c | 44 +++++++++++++-------- extlib/benz/string.c | 8 ++-- extlib/benz/symbol.c | 4 +- extlib/benz/value.c | 2 +- extlib/benz/vector.c | 6 +-- extlib/benz/weak.c | 6 +-- 24 files changed, 112 insertions(+), 111 deletions(-) diff --git a/contrib/20.r7rs/src/load.c b/contrib/20.r7rs/src/load.c index 3503eda9..f6f68b63 100644 --- a/contrib/20.r7rs/src/load.c +++ b/contrib/20.r7rs/src/load.c @@ -18,7 +18,7 @@ pic_load_load(pic_state *pic) fp = fopen(fn, "r"); if (fp == NULL) { - pic_errorf(pic, "load: could not open file %s", fn); + pic_error(pic, "load: could not open file", 1, pic_cstr_value(pic, fn)); } port = pic_open_port(pic, xfopen_file(pic, fp, "r")); diff --git a/contrib/30.readline/src/readline.c b/contrib/30.readline/src/readline.c index 6153cdbb..2a58b2c6 100644 --- a/contrib/30.readline/src/readline.c +++ b/contrib/30.readline/src/readline.c @@ -194,7 +194,7 @@ pic_rl_read_history(pic_state *pic) pic_get_args(pic, "z", &filename); if(read_history(filename)) - pic_errorf(pic, "cannot read history file : %s", filename); + pic_error(pic, "cannot read history file", 1, pic_cstr_value(pic, filename)); return pic_undef_value(pic); } @@ -207,7 +207,7 @@ pic_rl_write_history(pic_state *pic) pic_get_args(pic, "z", &filename); if(write_history(filename)) - pic_errorf(pic, "cannot write history file: %s", filename); + pic_error(pic, "cannot write history file", 1, pic_cstr_value(pic, filename)); return pic_undef_value(pic); } @@ -235,7 +235,7 @@ pic_rl_history_expand(pic_state *pic) status = history_expand(input, &result); if(status == -1 || status == 2) - pic_errorf(pic, "%s\n", result); + pic_error(pic, result, 0); return pic_cstr_value(pic, result); } diff --git a/contrib/30.regexp/src/regexp.c b/contrib/30.regexp/src/regexp.c index 8f7f0c89..c428ad1c 100644 --- a/contrib/30.regexp/src/regexp.c +++ b/contrib/30.regexp/src/regexp.c @@ -57,7 +57,7 @@ pic_regexp_regexp(pic_state *pic) regerror(err, ®->reg, errbuf, sizeof errbuf); regexp_dtor(pic, ®->reg); - pic_errorf(pic, "regexp compilation error: %s", errbuf); + pic_error(pic, "regexp compilation error", 1, pic_cstr_value(pic, errbuf)); } return pic_data_value(pic, reg, ®exp_type); diff --git a/contrib/40.srfi/src/106.c b/contrib/40.srfi/src/106.c index e2cdf13a..2ffec60a 100644 --- a/contrib/40.srfi/src/106.c +++ b/contrib/40.srfi/src/106.c @@ -31,7 +31,7 @@ PIC_INLINE void ensure_socket_is_open(pic_state *pic, struct pic_socket_t *sock) { if (sock != NULL && sock->fd == -1) { - pic_errorf(pic, "the socket is already closed"); + pic_error(pic, "the socket is already closed", 0); } } @@ -87,9 +87,9 @@ pic_socket_make_socket(pic_state *pic) } while (result == EAI_AGAIN); if (result) { if (result == EAI_SYSTEM) { - pic_errorf(pic, "%s", strerror(errno)); + pic_error(pic, strerror(errno), 0); } - pic_errorf(pic, "%s", gai_strerror(result)); + pic_error(pic, gai_strerror(result), 0); } for (it = ai; it != NULL; it = it->ai_next) { @@ -129,7 +129,7 @@ pic_socket_make_socket(pic_state *pic) freeaddrinfo(ai); if (sock->fd == -1) { - pic_errorf(pic, "%s", strerror(errno)); + pic_error(pic, strerror(errno), 0); } return pic_data_value(pic, sock, &socket_type); @@ -158,7 +158,7 @@ pic_socket_socket_accept(pic_state *pic) } else if (errno == EAGAIN || errno == EWOULDBLOCK) { continue; } else { - pic_errorf(pic, "%s", strerror(errno)); + pic_error(pic, strerror(errno), 0); } } else { break; @@ -191,7 +191,7 @@ pic_socket_socket_send(pic_state *pic) } else if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else { - pic_errorf(pic, "%s", strerror(errno)); + pic_error(pic, strerror(errno), 0); } } cursor += len; @@ -214,7 +214,7 @@ pic_socket_socket_recv(pic_state *pic) pic_get_args(pic, "ui|i", &sock, &socket_type, &size, &flags); if (size < 0) { - pic_errorf(pic, "size must not be negative"); + pic_error(pic, "size must not be negative", 0); } ensure_socket_is_open(pic, sock); @@ -227,7 +227,7 @@ pic_socket_socket_recv(pic_state *pic) } while (len < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)); if (len < 0) { - pic_errorf(pic, "%s", strerror(errno)); + pic_error(pic, strerror(errno), 0); } return pic_blob_value(pic, buf, len); diff --git a/extlib/benz/blob.c b/extlib/benz/blob.c index 3747d66e..bff9d67b 100644 --- a/extlib/benz/blob.c +++ b/extlib/benz/blob.c @@ -56,7 +56,7 @@ pic_blob_bytevector(pic_state *pic) pic_assert_type(pic, argv[i], int); if (pic_int(pic, argv[i]) < 0 || pic_int(pic, argv[i]) > 255) { - pic_errorf(pic, "byte out of range"); + pic_error(pic, "byte out of range", 0); } *data++ = (unsigned char)pic_int(pic, argv[i]); @@ -74,10 +74,10 @@ pic_blob_make_bytevector(pic_state *pic) pic_get_args(pic, "i|i", &k, &b); if (b < 0 || b > 255) - pic_errorf(pic, "byte out of range"); + pic_error(pic, "byte out of range", 0); if (k < 0) { - pic_errorf(pic, "make-bytevector: negative length given %d", k); + pic_error(pic, "make-bytevector: negative length given", 1, pic_int_value(pic, k)); } blob = pic_blob_value(pic, 0, k); @@ -119,7 +119,7 @@ pic_blob_bytevector_u8_set(pic_state *pic) pic_get_args(pic, "bii", &buf, &len, &k, &v); if (v < 0 || v > 255) - pic_errorf(pic, "byte out of range"); + pic_error(pic, "byte out of range", 0); VALID_INDEX(pic, len, k); @@ -216,7 +216,7 @@ pic_blob_list_to_bytevector(pic_state *pic) pic_assert_type(pic, e, int); if (pic_int(pic, e) < 0 || pic_int(pic, e) > 255) - pic_errorf(pic, "byte out of range"); + pic_error(pic, "byte out of range", 0); *data++ = (unsigned char)pic_int(pic, e); } diff --git a/extlib/benz/bool.c b/extlib/benz/bool.c index 9de94abc..ee9c52a3 100644 --- a/extlib/benz/bool.c +++ b/extlib/benz/bool.c @@ -72,7 +72,7 @@ internal_equal_p(pic_state *pic, pic_value x, pic_value y, int depth, khash_t(m) if (depth > 10) { if (depth > 200) { - pic_errorf(pic, "Stack overflow in equal\n"); + pic_error(pic, "stack overflow in equal", 0); } if (pic_pair_p(pic, x) || pic_vec_p(pic, x)) { int ret; diff --git a/extlib/benz/char.c b/extlib/benz/char.c index d4d4b499..7f9d7666 100644 --- a/extlib/benz/char.c +++ b/extlib/benz/char.c @@ -3,6 +3,7 @@ */ #include "picrin.h" +#include "picrin/extra.h" static pic_value pic_char_char_p(pic_state *pic) @@ -32,7 +33,7 @@ pic_char_integer_to_char(pic_state *pic) pic_get_args(pic, "i", &i); if (i < 0 || i > 255) { - pic_errorf(pic, "integer->char: integer out of char range: %d", i); + pic_error(pic, "integer->char: integer out of char range", 1, pic_int_value(pic, i)); } return pic_char_value(pic, (char)i); @@ -49,15 +50,13 @@ pic_char_integer_to_char(pic_state *pic) pic_get_args(pic, "cc*", &c, &d, &argc, &argv); \ \ if (! (c op d)) \ - return pic_false_value(pic); \ - \ - for (i = 0; i < argc; ++i) { \ + return pic_false_value(pic); \ + \ + for (i = 0; i < argc; ++i) { \ c = d; \ - if (pic_char_p(pic, argv[i])) \ - d = pic_char(pic, argv[i]); \ - else \ - pic_errorf(pic, #op ": char required"); \ - \ + pic_assert_type(pic, argv[i], char); \ + d = pic_char(pic, argv[i]); \ + \ if (! (c op d)) \ return pic_false_value(pic); \ } \ diff --git a/extlib/benz/cont.c b/extlib/benz/cont.c index 0149e00f..64f269a2 100644 --- a/extlib/benz/cont.c +++ b/extlib/benz/cont.c @@ -131,7 +131,7 @@ cont_call(pic_state *pic) } } if (cc == NULL) { - pic_errorf(pic, "calling dead escape continuation"); + pic_error(pic, "calling dead escape continuation", 0); } cont->retc = argc; diff --git a/extlib/benz/dict.c b/extlib/benz/dict.c index 47a92fd6..34f40de7 100644 --- a/extlib/benz/dict.c +++ b/extlib/benz/dict.c @@ -26,7 +26,7 @@ pic_dict_ref(pic_state *pic, pic_value dict, pic_value key) it = kh_get(dict, h, pic_sym_ptr(pic, key)); if (it == kh_end(h)) { - pic_errorf(pic, "element not found for a key: ~s", key); + pic_error(pic, "element not found for given key", 1, key); } return kh_val(h, it); } @@ -64,7 +64,7 @@ pic_dict_del(pic_state *pic, pic_value dict, pic_value key) it = kh_get(dict, h, pic_sym_ptr(pic, key)); if (it == kh_end(h)) { - pic_errorf(pic, "no slot named ~s found in dictionary", key); + pic_error(pic, "element not found for given key", 1, key); } kh_del(dict, h, it); } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 1111567f..3012fc58 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -37,19 +37,16 @@ pic_warnf(pic_state *pic, const char *fmt, ...) } void -pic_errorf(pic_state *pic, const char *fmt, ...) +pic_error(pic_state *pic, const char *msg, int n, ...) { va_list ap; - const char *msg; - pic_value err; + pic_value irrs; - va_start(ap, fmt); - err = pic_vstrf_value(pic, fmt, ap); + va_start(ap, n); + irrs = pic_vlist(pic, n, ap); va_end(ap); - msg = pic_str(pic, err); - - pic_raise(pic, pic_make_error(pic, "", msg, pic_nil_value(pic))); + pic_raise(pic, pic_make_error(pic, "", msg, irrs)); } void @@ -151,7 +148,7 @@ pic_raise(pic_state *pic, pic_value err) pic_pop_handler(pic); - pic_errorf(pic, "error handler returned with ~s on error ~s", val, err); + pic_error(pic, "error handler returned", 2, val, err); } static pic_value diff --git a/extlib/benz/eval.c b/extlib/benz/eval.c index 41e60d4b..2564f60c 100644 --- a/extlib/benz/eval.c +++ b/extlib/benz/eval.c @@ -159,7 +159,7 @@ expand_defmacro(pic_state *pic, pic_value expr, pic_value env) val = pic_call(pic, pic_compile(pic, pic_expand(pic, pic_list_ref(pic, expr, 2), env)), 0); if (! pic_proc_p(pic, val)) { - pic_errorf(pic, "macro definition \"~s\" evaluates to non-procedure object", pic_list_ref(pic, expr, 1)); + pic_error(pic, "macro definition evaluates to non-procedure object", 1, pic_list_ref(pic, expr, 1)); } define_macro(pic, uid, val); @@ -179,7 +179,7 @@ expand_node(pic_state *pic, pic_value expr, pic_value env, pic_value deferred) pic_value mac; if (! pic_list_p(pic, expr)) { - pic_errorf(pic, "cannot expand improper list: ~s", expr); + pic_error(pic, "cannot expand improper list", 1, expr); } if (pic_id_p(pic, pic_car(pic, expr))) { @@ -516,7 +516,7 @@ analyze_node(pic_state *pic, analyze_scope *scope, pic_value obj) pic_value proc; if (! pic_list_p(pic, obj)) { - pic_errorf(pic, "invalid expression given: ~s", obj); + pic_error(pic, "invalid expression given", 1, obj); } proc = pic_list_ref(pic, obj, 0); @@ -1023,7 +1023,7 @@ codegen(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) codegen_call(pic, cxt, obj, tailpos); } else { - pic_errorf(pic, "codegen: unknown AST type ~s", obj); + pic_error(pic, "codegen: unknown AST type", 1, obj); } } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index f255d2e3..b5e63b05 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -100,7 +100,7 @@ typedef void (*pic_panicf)(pic_state *, const char *msg); pic_panicf pic_atpanic(pic_state *, pic_panicf f); PIC_NORETURN void pic_panic(pic_state *, const char *msg); -PIC_NORETURN void pic_errorf(pic_state *, const char *fmt, ...); +PIC_NORETURN void pic_error(pic_state *, const char *msg, int n, ...); PIC_NORETURN void pic_raise(pic_state *, pic_value v); pic_value pic_make_error(pic_state *, const char *type, const char *msg, pic_value irrs); diff --git a/extlib/benz/include/picrin/extra.h b/extlib/benz/include/picrin/extra.h index ed8ea6cb..a7cd6f88 100644 --- a/extlib/benz/include/picrin/extra.h +++ b/extlib/benz/include/picrin/extra.h @@ -59,9 +59,9 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #define pic_push(pic, item, place) (place = pic_cons(pic, item, place)) #define pic_pop(pic, place) (place = pic_cdr(pic, place)) -#define pic_assert_type(pic, v, type) do { \ - if (! pic_##type##_p(pic, v)) \ - pic_errorf(pic, "expected " #type ", but got ~s", v); \ +#define pic_assert_type(pic, v, type) do { \ + if (! pic_##type##_p(pic, v)) \ + pic_error(pic, #type " required", 1, v); \ } while (0) #define pic_void(exec) pic_void_(PIC_GENSYM(ai), exec) diff --git a/extlib/benz/include/picrin/private/object.h b/extlib/benz/include/picrin/private/object.h index ec0ec402..2dedfcec 100644 --- a/extlib/benz/include/picrin/private/object.h +++ b/extlib/benz/include/picrin/private/object.h @@ -159,16 +159,16 @@ pic_value pic_obj_value(void *ptr); struct object *pic_obj_alloc(pic_state *, size_t, int type); #define VALID_INDEX(pic, len, i) do { \ - if (i < 0 || len <= i) pic_errorf(pic, "index out of range: %d", i); \ + if (i < 0 || len <= i) pic_error(pic, "index out of range", 1, pic_int_value(pic, i)); \ } while (0) #define VALID_RANGE(pic, len, s, e) do { \ - if (s < 0 || len < s) pic_errorf(pic, "invalid start index: %d", s); \ - if (e < s || len < e) pic_errorf(pic, "invalid end index: %d", e); \ + if (s < 0 || len < s) pic_error(pic, "invalid start index", 1, pic_int_value(pic, s)); \ + if (e < s || len < e) pic_error(pic, "invalid end index", 1, pic_int_value(pic, e)); \ } while (0) -#define VALID_ATRANGE(pic, tolen, at, fromlen, s, e) do { \ - VALID_INDEX(pic, tolen, at); \ - VALID_RANGE(pic, fromlen, s, e); \ - if (tolen - at < e - s) pic_errorf(pic, "invalid range"); \ +#define VALID_ATRANGE(pic, tolen, at, fromlen, s, e) do { \ + VALID_INDEX(pic, tolen, at); \ + VALID_RANGE(pic, fromlen, s, e); \ + if (tolen - at < e - s) pic_error(pic, "invalid range", 0); \ } while (0) pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env); diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index c1266d77..b200b4cb 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -127,7 +127,7 @@ get_library(pic_state *pic, const char *lib) struct lib *libp; if ((libp = get_library_opt(pic, lib)) == NULL) { - pic_errorf(pic, "library not found: %s", lib); + pic_error(pic, "library not found", 1, pic_cstr_value(pic, lib)); } return libp; } @@ -175,7 +175,7 @@ pic_make_library(pic_state *pic, const char *lib) it = kh_put(ltable, h, pic_str(pic, name), &ret); if (ret == 0) { /* if exists */ - pic_errorf(pic, "library name already in use: %s", lib); + pic_error(pic, "library name already in use", pic_cstr_value(pic, lib)); } kh_val(h, it).name = pic_str_ptr(pic, name); @@ -223,7 +223,7 @@ pic_import(pic_state *pic, const char *lib) while (pic_dict_next(pic, pic_obj_value(libp->exports), &it, &name, &realname)) { uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env)); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { - pic_errorf(pic, "attempted to export undefined variable '~s'", realname); + pic_error(pic, "attempted to export undefined variable", 1, realname); } pic_put_identifier(pic, name, uid, pic_obj_value(pic->lib->env)); } @@ -292,14 +292,14 @@ pic_lib_library_import(pic_state *pic) libp = get_library(pic, lib); if (! pic_dict_has(pic, pic_obj_value(libp->exports), name)) { - pic_errorf(pic, "library-import: variable is not exported '~s'", name); + pic_error(pic, "library-import: variable is not exported", 1, name); } else { realname = pic_dict_ref(pic, pic_obj_value(libp->exports), name); } uid = pic_find_identifier(pic, realname, pic_obj_value(libp->env)); if (! pic_weak_has(pic, pic->globals, uid) && ! pic_weak_has(pic, pic->macros, uid)) { - pic_errorf(pic, "attempted to export undefined variable '~s'", realname); + pic_error(pic, "attempted to export undefined variable", 1, realname); } pic_put_identifier(pic, alias, uid, pic_obj_value(pic->lib->env)); diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 2c6d8d37..2ecbe2d9 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -59,7 +59,6 @@ pic_number_exact(pic_state *pic) pic_value \ name(pic_state *pic, pic_value a, pic_value b) \ { \ - PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ double f; \ if (pic_int_p(pic, a) && pic_int_p(pic, b)) { \ f = (double)pic_int(pic, a) op (double)pic_int(pic, b); \ @@ -73,7 +72,7 @@ pic_number_exact(pic_state *pic) } else if (pic_float_p(pic, a) && pic_int_p(pic, b)) { \ return pic_float_value(pic, pic_float(pic, a) op pic_int(pic, b)); \ } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ + pic_error(pic, #name ": non-number operand given", 0); \ } \ PIC_UNREACHABLE(); \ } @@ -87,7 +86,6 @@ pic_define_aop(pic_div, /, f == (int)f) bool \ name(pic_state *pic, pic_value a, pic_value b) \ { \ - PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); \ if (pic_int_p(pic, a) && pic_int_p(pic, b)) { \ return pic_int(pic, a) op pic_int(pic, b); \ } else if (pic_float_p(pic, a) && pic_float_p(pic, b)) { \ @@ -97,7 +95,7 @@ pic_define_aop(pic_div, /, f == (int)f) } else if (pic_float_p(pic, a) && pic_int_p(pic, b)) { \ return pic_float(pic, a) op pic_int(pic, b); \ } else { \ - pic_errorf(pic, #name ": non-number operand given"); \ + pic_error(pic, #name ": non-number operand given", 0); \ } \ PIC_UNREACHABLE(); \ } @@ -165,10 +163,10 @@ DEFINE_AOP(mul, argv[0], do { return pic_int_value(pic, 1); } while (0)) DEFINE_AOP(sub, pic_sub(pic, pic_int_value(pic, 0), argv[0]), do { - pic_errorf(pic, "-: at least one argument required"); + pic_error(pic, "-: at least one argument required", 0); } while (0)) DEFINE_AOP(div, pic_div(pic, pic_int_value(pic, 1), argv[0]), do { - pic_errorf(pic, "/: at least one argument required"); + pic_error(pic, "/: at least one argument required", 0); } while (0)) static int @@ -224,7 +222,7 @@ pic_number_number_to_string(pic_state *pic) pic_get_args(pic, "F|i", &f, &e, &radix); if (radix < 2 || radix > 36) { - pic_errorf(pic, "number->string: invalid radix %d (between 2 and 36, inclusive)", radix); + pic_error(pic, "number->string: invalid radix (between 2 and 36, inclusive)", 1, pic_int_value(pic, radix)); } if (e) { diff --git a/extlib/benz/pair.c b/extlib/benz/pair.c index 4d6f474a..4e7c27b3 100644 --- a/extlib/benz/pair.c +++ b/extlib/benz/pair.c @@ -22,7 +22,7 @@ pic_value pic_car(pic_state *pic, pic_value obj) { if (! pic_pair_p(pic, obj)) { - pic_errorf(pic, "car: pair required, but got ~s", obj); + pic_error(pic, "car: pair required", 1, obj); } return pic_pair_ptr(pic, obj)->car; } @@ -31,7 +31,7 @@ pic_value pic_cdr(pic_state *pic, pic_value obj) { if (! pic_pair_p(pic, obj)) { - pic_errorf(pic, "cdr: pair required, but got ~s", obj); + pic_error(pic, "cdr: pair required", 1, obj); } return pic_pair_ptr(pic, obj)->cdr; } @@ -40,7 +40,7 @@ void pic_set_car(pic_state *pic, pic_value obj, pic_value val) { if (! pic_pair_p(pic, obj)) { - pic_errorf(pic, "pair required"); + pic_error(pic, "pair required", 0); } pic_pair_ptr(pic, obj)->car = val; } @@ -49,7 +49,7 @@ void pic_set_cdr(pic_state *pic, pic_value obj, pic_value val) { if (! pic_pair_p(pic, obj)) { - pic_errorf(pic, "pair required"); + pic_error(pic, "pair required", 0); } pic_pair_ptr(pic, obj)->cdr = val; } @@ -171,15 +171,10 @@ pic_length(pic_state *pic, pic_value obj) { int c = 0; - if (! pic_list_p(pic, obj)) { - pic_errorf(pic, "length: expected list, but got ~s", obj); - } - while (! pic_nil_p(pic, obj)) { obj = pic_cdr(pic, obj); ++c; } - return c; } @@ -477,7 +472,7 @@ pic_pair_map(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &args); if (argc == 0) - pic_errorf(pic, "map: wrong number of arguments (1 for at least 2)"); + pic_error(pic, "map: wrong number of arguments (1 for at least 2)", 0); arg_list = pic_alloca(pic, sizeof(pic_value) * argc); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index a0c70e6a..f4d80092 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -36,7 +36,7 @@ pic_close_port(pic_state *pic, pic_value port) return; } if (xfclose(pic, file) == EOF) { - pic_errorf(pic, "close-port: failure"); + pic_error(pic, "close-port: failure", 0); } } @@ -123,13 +123,13 @@ pic_port_close_port(pic_state *pic) if ((pic_fileno(pic, port)->flag & (flags)) != (flags)) { \ switch (flags) { \ case X_WRITE: \ - pic_errorf(pic, caller ": expected output port"); \ + pic_error(pic, caller ": output port required", 0); \ case X_READ: \ - pic_errorf(pic, caller ": expected input port"); \ + pic_error(pic, caller ": input port required", 0); \ } \ } \ if (pic_fileno(pic, port)->flag == 0) { \ - pic_errorf(pic, caller ": expected open port"); \ + pic_error(pic, caller ": open port required", 0); \ } \ } while (0) @@ -164,7 +164,7 @@ pic_port_get_output_bytevector(pic_state *pic) assert_port_profile(port, X_WRITE, "get-output-bytevector"); if (xfget_buf(pic, pic_fileno(pic, port), &buf, &len) < 0) { - pic_errorf(pic, "port was not created by open-output-bytevector"); + pic_error(pic, "port was not created by open-output-bytevector", 0); } return pic_blob_value(pic, (unsigned char *)buf, len); } diff --git a/extlib/benz/proc.c b/extlib/benz/proc.c index 570f1730..66061ade 100644 --- a/extlib/benz/proc.c +++ b/extlib/benz/proc.c @@ -10,6 +10,16 @@ #define MIN(x,y) ((x) < (y) ? (x) : (y)) +PIC_NORETURN static void +arg_error(pic_state *pic, int actual, bool varg, int expected) +{ + const char *msg; + + msg = pic_str(pic, pic_strf_value(pic, "wrong number of arguments (%d for %s%d)", actual, (varg ? "at least " : ""), expected)); + + pic_error(pic, msg, 0); +} + #define GET_OPERAND(pic,n) ((pic)->ci->fp[(n)]) /** @@ -80,7 +90,7 @@ pic_get_args(pic_state *pic, const char *format, ...) } if (argc < paramc || (paramc + optc < argc && ! rest)) { - pic_errorf(pic, "pic_get_args: wrong number of arguments (%d for %s%d)", argc, rest? "at least " : "", paramc); + arg_error(pic, argc, rest, paramc); } va_start(ap, format); @@ -121,7 +131,9 @@ pic_get_args(pic_state *pic, const char *format, ...) *data = pic_data(pic, v); } else { - pic_errorf(pic, "pic_get_args: expected data type \"%s\", but got ~s", type->type_name, v); + const char *msg; + msg = pic_str(pic, pic_strf_value(pic, "pic_get_args: data type \"%s\" required", type->type_name)); + pic_error(pic, msg, 1, v); } break; } @@ -139,7 +151,7 @@ pic_get_args(pic_state *pic, const char *format, ...) if (buf) *buf = tmp; } else { - pic_errorf(pic, "pic_get_args: expected bytevector, but got ~s", v); + pic_error(pic, "pic_get_args: bytevector required", 1, v); } break; } @@ -155,16 +167,16 @@ pic_get_args(pic_state *pic, const char *format, ...) \ v = GET_OPERAND(pic, i); \ switch (pic_type(pic, v)) { \ - case PIC_TYPE_FLOAT: \ + case PIC_TYPE_FLOAT: \ *n = pic_float(pic, v); \ *e = false; \ break; \ - case PIC_TYPE_INT: \ + case PIC_TYPE_INT: \ *n = pic_int(pic, v); \ *e = true; \ break; \ default: \ - pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v); \ + pic_error(pic, "pic_get_args: float or int required", 1, v); \ } \ break; \ } @@ -183,7 +195,7 @@ pic_get_args(pic_state *pic, const char *format, ...) *ptr = conv; \ } \ else { \ - pic_errorf(pic, "pic_get_args: expected " #type ", but got ~s", v); \ + pic_error(pic, "pic_get_args: " #type " required", 1, v); \ } \ break; \ } @@ -202,7 +214,7 @@ pic_get_args(pic_state *pic, const char *format, ...) OBJ_CASE('r', rec) default: - pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); + pic_error(pic, "pic_get_args: invalid argument specifier given", 1, pic_char_value(pic, c)); } if (format[1] == '+') { @@ -232,11 +244,11 @@ vm_gref(pic_state *pic, pic_value uid) pic_value val; if (! pic_weak_has(pic, pic->globals, uid)) { - pic_errorf(pic, "undefined variable ~s", uid); + pic_error(pic, "undefined variable", 1, uid); } val = pic_weak_ref(pic, pic->globals, uid);; if (pic_invalid_p(pic, val)) { - pic_errorf(pic, "uninitialized global variable: ~s", uid); + pic_error(pic, "uninitialized global variable", 1, uid); } return val; } @@ -245,7 +257,7 @@ static void vm_gset(pic_state *pic, pic_value uid, pic_value value) { if (! pic_weak_has(pic, pic->globals, uid)) { - pic_errorf(pic, "undefined variable ~s", uid); + pic_error(pic, "undefined variable", 1, uid); } pic_weak_set(pic, pic->globals, uid, value); } @@ -488,7 +500,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) L_CALL: x = pic->sp[-c.a]; if (! pic_proc_p(pic, x)) { - pic_errorf(pic, "invalid application: ~s", x); + pic_error(pic, "invalid application", 1, x); } proc = pic_proc_ptr(pic, x); @@ -521,7 +533,7 @@ pic_apply(pic_state *pic, pic_value proc, int argc, pic_value *argv) ci->irep = irep; if (ci->argc != irep->argc) { if (! (irep->varg && ci->argc >= irep->argc)) { - pic_errorf(pic, "wrong number of arguments (%d for %s%d)", ci->argc - 1, (irep->varg ? "at least " : ""), irep->argc - 1); + arg_error(pic, ci->argc - 1, irep->varg, irep->argc - 1); } } /* prepare rest args */ @@ -909,7 +921,7 @@ pic_closure_ref(pic_state *pic, int n) assert(pic_func_p(self)); if (n < 0 || pic_proc_ptr(pic, self)->u.f.localc <= n) { - pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); + pic_error(pic, "pic_closure_ref: index out of range", 1, pic_int_value(pic, n)); } return pic_proc_ptr(pic, self)->locals[n]; } @@ -922,7 +934,7 @@ pic_closure_set(pic_state *pic, int n, pic_value v) assert(pic_func_p(self)); if (n < 0 || pic_proc_ptr(pic, self)->u.f.localc <= n) { - pic_errorf(pic, "pic_closure_ref: index out of range (%d)", n); + pic_error(pic, "pic_closure_ref: index out of range", 1, pic_int_value(pic, n)); } pic_proc_ptr(pic, self)->locals[n] = v; } @@ -1019,7 +1031,7 @@ pic_proc_apply(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &args); if (argc == 0) { - pic_errorf(pic, "apply: wrong number of arguments"); + pic_error(pic, "apply: wrong number of arguments", 0); } n = argc - 1 + pic_length(pic, args[argc - 1]); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 75a703db..d08f6178 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -276,7 +276,7 @@ pic_str_ref(pic_state *pic, pic_value str, int i) c = rope_at(pic_str_ptr(pic, str)->rope, i); if (c == -1) { - pic_errorf(pic, "index out of range %d", i); + pic_error(pic, "index out of range", 1, pic_int_value(pic, i)); } return (char)c; } @@ -453,7 +453,7 @@ pic_str_make_string(pic_state *pic) pic_get_args(pic, "i|c", &len, &c); if (len < 0) { - pic_errorf(pic, "make-string: negative length given %d", len); + pic_error(pic, "make-string: negative length given", 1, pic_int_value(pic, len)); } buf = pic_alloca(pic, len); @@ -647,7 +647,7 @@ pic_str_string_map(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { - pic_errorf(pic, "string-map: one or more strings expected, but got zero"); + pic_error(pic, "string-map: one or more strings expected, but got zero", 0); } len = INT_MAX; @@ -684,7 +684,7 @@ pic_str_string_for_each(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { - pic_errorf(pic, "string-map: one or more strings expected, but got zero"); + pic_error(pic, "string-map: one or more strings expected, but got zero", 0); } len = INT_MAX; diff --git a/extlib/benz/symbol.c b/extlib/benz/symbol.c index cff34df8..f67f7593 100644 --- a/extlib/benz/symbol.c +++ b/extlib/benz/symbol.c @@ -146,7 +146,7 @@ pic_symbol_identifier_base(pic_state *pic) pic_assert_type(pic, id, id); if (pic_sym_p(pic, id)) { - pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); + pic_error(pic, "non-symbol identifier required", 1, id); } return pic_obj_value(pic_id_ptr(pic, id)->u.id); @@ -162,7 +162,7 @@ pic_symbol_identifier_environment(pic_state *pic) pic_assert_type(pic, id, id); if (pic_sym_p(pic, id)) { - pic_errorf(pic, "expected non-symbol identifier, but got symbol ~s", id); + pic_error(pic, "non-symbol identifier required", 1, id); } return pic_obj_value(pic_id_ptr(pic, id)->env); diff --git a/extlib/benz/value.c b/extlib/benz/value.c index a41d7d52..32b17d65 100644 --- a/extlib/benz/value.c +++ b/extlib/benz/value.c @@ -256,6 +256,6 @@ pic_typename(pic_state *pic, int type) case PIC_TYPE_CP: return "checkpoint"; default: - pic_errorf(pic, "pic_typename: invalid type given %d", type); + pic_error(pic, "pic_typename: invalid type given", 1, pic_int_value(pic, type)); } } diff --git a/extlib/benz/vector.c b/extlib/benz/vector.c index 978cfeb1..781d138a 100644 --- a/extlib/benz/vector.c +++ b/extlib/benz/vector.c @@ -73,7 +73,7 @@ pic_vec_make_vector(pic_state *pic) n = pic_get_args(pic, "i|o", &k, &init); if (k < 0) { - pic_errorf(pic, "make-vector: negative length given %d", k); + pic_error(pic, "make-vector: negative length given", 1, pic_int_value(pic, k)); } vec = pic_make_vec(pic, k, NULL); @@ -231,7 +231,7 @@ pic_vec_vector_map(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { - pic_errorf(pic, "vector-map: wrong number of arguments (1 for at least 2)"); + pic_error(pic, "vector-map: wrong number of arguments (1 for at least 2)", 0); } len = INT_MAX; @@ -265,7 +265,7 @@ pic_vec_vector_for_each(pic_state *pic) pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { - pic_errorf(pic, "vector-for-each: wrong number of arguments (1 for at least 2)"); + pic_error(pic, "vector-for-each: wrong number of arguments (1 for at least 2)", 0); } len = INT_MAX; diff --git a/extlib/benz/weak.c b/extlib/benz/weak.c index 3064873c..722d153b 100644 --- a/extlib/benz/weak.c +++ b/extlib/benz/weak.c @@ -27,7 +27,7 @@ pic_weak_ref(pic_state *pic, pic_value weak, pic_value key) it = kh_get(weak, h, pic_obj_ptr(key)); if (it == kh_end(h)) { - pic_errorf(pic, "element not found for a key: ~s", key); + pic_error(pic, "element not found for given key", 1, key); } return kh_val(h, it); } @@ -59,7 +59,7 @@ pic_weak_del(pic_state *pic, pic_value weak, pic_value key) it = kh_get(weak, h, pic_obj_ptr(key)); if (it == kh_end(h)) { - pic_errorf(pic, "no slot named ~s found in ephemeron", key); + pic_error(pic, "element not found for given key", 1, key); } kh_del(weak, h, it); } @@ -74,7 +74,7 @@ weak_call(pic_state *pic) n = pic_get_args(pic, "o|o", &key, &val); if (! pic_obj_p(pic, key)) { - pic_errorf(pic, "attempted to set a non-object key '~s' in an ephemeron", key); + pic_error(pic, "attempted to set a non-object key", 1, key); } weak = pic_closure_ref(pic, 0); From 571fa0993c2b191d9b2e75a855f45400fed7e81a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 04:03:59 +0900 Subject: [PATCH 115/119] improve repl error message --- contrib/60.repl/repl.scm | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/contrib/60.repl/repl.scm b/contrib/60.repl/repl.scm index 698c77c5..f358daa5 100644 --- a/contrib/60.repl/repl.scm +++ b/contrib/60.repl/repl.scm @@ -36,6 +36,34 @@ (picrin macro)) "picrin.user")) + (define (repeat x) + (let ((p (list x))) + (set-cdr! p p) + p)) + + (define (join xs delim) + (cdr (apply append (map list (repeat delim) xs)))) + + (define (string-join strings delim) + (apply string-append (join strings delim))) + + (define (->string x) + (call-with-port (open-output-string) + (lambda (port) + (write x port) + (get-output-string port)))) + + (define (print-error-object e) + (display "error: ") + (display (error-object-message e)) + (display ".") + (define irritants (error-object-irritants e)) + (unless (null? irritants) + (display " (irritants: ") + (display (string-join (map ->string irritants) ", ")) + (display ")")) + (newline)) + (define (repl) (init-env) (let loop ((buf "")) @@ -50,9 +78,7 @@ (lambda (condition) (if (error-object? condition) (unless (equal? (error-object-message condition) "unexpected EOF") - (display "error: ") - (display (error-object-message condition)) - (newline) + (print-error-object condition) (set! str "")) (begin (display "raised: ") From 4e1aaf8b897c514951e93251224eab6b701c0ec9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 04:24:42 +0900 Subject: [PATCH 116/119] pic_strf_value does not interpret '~s' format specifier --- contrib/60.repl/repl.scm | 6 +- extlib/benz/debug.c | 13 ++--- extlib/benz/include/picrin/extra.h | 12 ++-- extlib/benz/string.c | 68 +--------------------- extlib/benz/write.c | 92 +++++++++++++++++++----------- src/main.c | 2 +- 6 files changed, 77 insertions(+), 116 deletions(-) diff --git a/contrib/60.repl/repl.scm b/contrib/60.repl/repl.scm index f358daa5..b2b9323f 100644 --- a/contrib/60.repl/repl.scm +++ b/contrib/60.repl/repl.scm @@ -54,6 +54,10 @@ (get-output-string port)))) (define (print-error-object e) + (define type (error-object-type e)) + (unless (eq? type '||) + (display type) + (display "-")) (display "error: ") (display (error-object-message e)) (display ".") @@ -81,7 +85,7 @@ (print-error-object condition) (set! str "")) (begin - (display "raised: ") + (display "raise: ") (write condition) (newline) (set! str ""))) diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index ae3ca9b3..043571b9 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -36,30 +36,29 @@ pic_get_backtrace(pic_state *pic) } void -pic_print_backtrace(pic_state *pic, xFILE *file) +pic_print_error(pic_state *pic, xFILE *file) { - pic_value err = pic_err(pic); + pic_value err = pic_err(pic), port = pic_open_port(pic, file); assert(! pic_invalid_p(pic, err)); if (! pic_error_p(pic, err)) { xfprintf(pic, file, "raise: "); - pic_fwrite(pic, err, file); + pic_fprintf(pic, port, "~s", err); } else { struct error *e; pic_value elem, it; e = pic_error_ptr(pic, err); if (! pic_eq_p(pic, pic_obj_value(e->type), pic_intern_lit(pic, ""))) { - pic_fwrite(pic, pic_obj_value(e->type), file); + pic_fprintf(pic, port, "~s", pic_obj_value(e->type)); xfprintf(pic, file, " "); } xfprintf(pic, file, "error: "); - pic_fwrite(pic, pic_obj_value(e->msg), file); + pic_fprintf(pic, port, "~s", pic_obj_value(e->msg)); pic_for_each (elem, e->irrs, it) { /* print error irritants */ - xfprintf(pic, file, " "); - pic_fwrite(pic, elem, file); + pic_fprintf(pic, port, " ~s", elem); } xfprintf(pic, file, "\n"); diff --git a/extlib/benz/include/picrin/extra.h b/extlib/benz/include/picrin/extra.h index a7cd6f88..955be4b1 100644 --- a/extlib/benz/include/picrin/extra.h +++ b/extlib/benz/include/picrin/extra.h @@ -23,6 +23,10 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib); void pic_load(pic_state *, pic_value port); void pic_load_cstr(pic_state *, const char *); +void pic_printf(pic_state *, const char *fmt, ...); +void pic_fprintf(pic_state *, pic_value port, const char *fmt, ...); +void pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap); + /* extra xfile methods */ xFILE *xfile_xstdin(pic_state *); @@ -43,12 +47,6 @@ xFILE *xfopen_null(pic_state *, const char *mode); #define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0) #define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0) #define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0) -pic_value pic_write(pic_state *, pic_value); /* returns given obj */ -pic_value pic_fwrite(pic_state *, pic_value, xFILE *); -void pic_printf(pic_state *, const char *, ...); -void pic_fprintf(pic_state *, pic_value port, const char *, ...); -pic_value pic_display(pic_state *, pic_value); -pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); /* utility macros */ @@ -111,7 +109,7 @@ pic_value pic_err(pic_state *); void pic_warnf(pic_state *, const char *, ...); pic_value pic_get_backtrace(pic_state *); -void pic_print_backtrace(pic_state *, xFILE *); +void pic_print_error(pic_state *, xFILE *); pic_value pic_library_environment(pic_state *, const char *); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index d08f6178..eff041ed 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -318,72 +318,6 @@ pic_str(pic_state *pic, pic_value str) return rope_cstr(pic, pic_str_ptr(pic, str)->rope); } -static void -vfstrf(pic_state *pic, xFILE *file, const char *fmt, va_list ap) -{ - char c; - - while ((c = *fmt++) != '\0') { - switch (c) { - default: - xfputc(pic, c, file); - break; - case '%': - c = *fmt++; - if (! c) - goto exit; - switch (c) { - default: - xfputc(pic, c, file); - break; - case '%': - xfputc(pic, '%', file); - break; - case 'c': - xfprintf(pic, file, "%c", va_arg(ap, int)); - break; - case 's': - xfprintf(pic, file, "%s", va_arg(ap, const char *)); - break; - case 'd': - xfprintf(pic, file, "%d", va_arg(ap, int)); - break; - case 'p': - xfprintf(pic, file, "%p", va_arg(ap, void *)); - break; - case 'f': - xfprintf(pic, file, "%f", va_arg(ap, double)); - break; - } - break; - case '~': - c = *fmt++; - if (! c) - goto exit; - switch (c) { - default: - xfputc(pic, c, file); - break; - case '~': - xfputc(pic, '~', file); - break; - case '%': - xfputc(pic, '\n', file); - break; - case 'a': - pic_fdisplay(pic, va_arg(ap, pic_value), file); - break; - case 's': - pic_fwrite(pic, va_arg(ap, pic_value), file); - break; - } - break; - } - } - exit: - return; -} - pic_value pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap) { @@ -394,7 +328,7 @@ pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap) file = xfopen_buf(pic, NULL, 0, "w"); - vfstrf(pic, file, fmt, ap); + xvfprintf(pic, file, fmt, ap); xfget_buf(pic, file, &buf, &len); str = pic_str_value(pic, buf, len); xfclose(pic, file); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index bb993503..78cccb02 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -366,44 +366,70 @@ write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op) write_core(pic, obj, file, &p); } - -pic_value -pic_write(pic_state *pic, pic_value obj) -{ - return pic_fwrite(pic, obj, pic_fileno(pic, pic_stdout(pic))); -} - -pic_value -pic_fwrite(pic_state *pic, pic_value obj, xFILE *file) -{ - write(pic, obj, file, WRITE_MODE, OP_WRITE); - xfflush(pic, file); - return obj; -} - -pic_value -pic_display(pic_state *pic, pic_value obj) -{ - return pic_fdisplay(pic, obj, pic_fileno(pic, pic_stdout(pic))); -} - -pic_value -pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file) -{ - write(pic, obj, file, DISPLAY_MODE, OP_WRITE); - xfflush(pic, file); - return obj; -} - void pic_vfprintf(pic_state *pic, pic_value port, const char *fmt, va_list ap) { xFILE *file = pic_fileno(pic, port); - pic_value str; + char c; - str = pic_vstrf_value(pic, fmt, ap); - - xfprintf(pic, file, "%s", pic_str(pic, str)); + while ((c = *fmt++) != '\0') { + switch (c) { + default: + xfputc(pic, c, file); + break; + case '%': + c = *fmt++; + if (! c) + goto exit; + switch (c) { + default: + xfputc(pic, c, file); + break; + case '%': + xfputc(pic, '%', file); + break; + case 'c': + xfprintf(pic, file, "%c", va_arg(ap, int)); + break; + case 's': + xfprintf(pic, file, "%s", va_arg(ap, const char *)); + break; + case 'd': + xfprintf(pic, file, "%d", va_arg(ap, int)); + break; + case 'p': + xfprintf(pic, file, "%p", va_arg(ap, void *)); + break; + case 'f': + xfprintf(pic, file, "%f", va_arg(ap, double)); + break; + } + break; + case '~': + c = *fmt++; + if (! c) + goto exit; + switch (c) { + default: + xfputc(pic, c, file); + break; + case '~': + xfputc(pic, '~', file); + break; + case '%': + xfputc(pic, '\n', file); + break; + case 'a': + write(pic, va_arg(ap, pic_value), file, DISPLAY_MODE, OP_WRITE); + break; + case 's': + write(pic, va_arg(ap, pic_value), file, WRITE_MODE, OP_WRITE); + break; + } + break; + } + } + exit: xfflush(pic, file); } diff --git a/src/main.c b/src/main.c index 4878812f..9b45df9a 100644 --- a/src/main.c +++ b/src/main.c @@ -39,7 +39,7 @@ main(int argc, char *argv[], char **envp) status = 0; } pic_catch { - pic_print_backtrace(pic, xstderr); + pic_print_error(pic, xstderr); status = 1; } From 2ca7e630f0f4be74f0528721c099c1af00bfc758 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 04:37:58 +0900 Subject: [PATCH 117/119] write is now pluggable --- Makefile | 2 +- extlib/benz/debug.c | 4 ++++ extlib/benz/error.c | 5 +++-- extlib/benz/file.c | 2 +- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin/config.h | 9 ++++++--- extlib/benz/include/picrin/extra.h | 8 ++++++-- extlib/benz/include/picrin/setup.h | 16 ++++++++++------ extlib/benz/lib.c | 2 +- extlib/benz/state.c | 7 +++++-- extlib/benz/write.c | 4 ++++ 11 files changed, 42 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 6c80d105..e498d0fd 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ test: test-contribs test-nostdlib test-issue test-contribs: bin/picrin $(CONTRIB_TESTS) test-nostdlib: - $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_STDIO=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector + $(CC) -I extlib/benz/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector strip lib/libbenz-tiny.so ls -lh lib/libbenz-tiny.so rm -f lib/libbenz-tiny.so diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 043571b9..0a0efa0a 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -35,6 +35,8 @@ pic_get_backtrace(pic_state *pic) return trace; } +#if PIC_USE_WRITE + void pic_print_error(pic_state *pic, xFILE *file) { @@ -65,3 +67,5 @@ pic_print_error(pic_state *pic, xFILE *file) xfputs(pic, pic_str(pic, pic_obj_value(e->stack)), file); } } + +#endif diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 3012fc58..0105215f 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -14,7 +14,7 @@ pic_panic(pic_state *pic, const char *msg) pic->panicf(pic, msg); } -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO fprintf(stderr, "picrin panic!: %s\n", msg); #endif @@ -26,6 +26,7 @@ pic_panic(pic_state *pic, const char *msg) void pic_warnf(pic_state *pic, const char *fmt, ...) { + xFILE *file = pic_fileno(pic, pic_stderr(pic)); va_list ap; pic_value err; @@ -33,7 +34,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...) err = pic_vstrf_value(pic, fmt, ap); va_end(ap); - pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_str(pic, err)); + xfprintf(pic, file, "warn: %s\n", pic_str(pic, err)); } void diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 9a844acd..7b846b00 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -364,7 +364,7 @@ xFILE *xfile_xstdin(pic_state *pic) { return &pic->files[0]; } xFILE *xfile_xstdout(pic_state *pic) { return &pic->files[1]; } xFILE *xfile_xstderr(pic_state *pic) { return &pic->files[2]; } -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO static int file_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) { diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index f5ecd292..23ddd5e0 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -82,7 +82,7 @@ pic_heap_close(pic_state *pic, struct heap *heap) pic_free(pic, heap); } -#if PIC_ENABLE_LIBC +#if PIC_USE_LIBC void * pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size) { diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index e928f95f..eb1c3643 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -2,11 +2,14 @@ * See Copyright Notice in picrin.h */ -/** no dependency on libc */ -/* #define PIC_ENABLE_LIBC 1 */ +/** no dependency on libc? */ +/* #define PIC_USE_LIBC 1 */ /** use stdio or not */ -/* #define PIC_ENABLE_STDIO 1 */ +/* #define PIC_USE_STDIO 1 */ + +/** enable some specific features? */ +/* #define PIC_USE_WRITE 1 */ /** essential external functions */ /* #define PIC_JMPBUF jmp_buf */ diff --git a/extlib/benz/include/picrin/extra.h b/extlib/benz/include/picrin/extra.h index 955be4b1..e574e24c 100644 --- a/extlib/benz/include/picrin/extra.h +++ b/extlib/benz/include/picrin/extra.h @@ -10,7 +10,7 @@ extern "C" { #endif -#if PIC_ENABLE_LIBC +#if PIC_USE_LIBC void *pic_default_allocf(void *, void *, size_t); #endif @@ -23,9 +23,11 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib); void pic_load(pic_state *, pic_value port); void pic_load_cstr(pic_state *, const char *); +#if PIC_USE_WRITE void pic_printf(pic_state *, const char *fmt, ...); void pic_fprintf(pic_state *, pic_value port, const char *fmt, ...); void pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap); +#endif /* extra xfile methods */ @@ -35,7 +37,7 @@ xFILE *xfile_xstderr(pic_state *); #define xstdin (xfile_xstdin(pic)) #define xstdout (xfile_xstdout(pic)) #define xstderr (xfile_xstderr(pic)) -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); #endif xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); @@ -109,7 +111,9 @@ pic_value pic_err(pic_state *); void pic_warnf(pic_state *, const char *, ...); pic_value pic_get_backtrace(pic_state *); +#if PIC_USE_WRITE void pic_print_error(pic_state *, xFILE *); +#endif pic_value pic_library_environment(pic_state *, const char *); diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 999a6f96..4fb9e36c 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -4,12 +4,16 @@ #include "picrin/config.h" -#ifndef PIC_ENABLE_LIBC -# define PIC_ENABLE_LIBC 1 +#ifndef PIC_USE_LIBC +# define PIC_USE_LIBC 1 #endif -#ifndef PIC_ENABLE_STDIO -# define PIC_ENABLE_STDIO 1 +#ifndef PIC_USE_STDIO +# define PIC_USE_STDIO 1 +#endif + +#ifndef PIC_USE_WRITE +# define PIC_USE_WRITE 1 #endif #ifndef PIC_JMPBUF @@ -156,7 +160,7 @@ typedef unsigned long uint32_t; } while (0) -#if PIC_ENABLE_LIBC +#if PIC_USE_LIBC #include #include @@ -375,7 +379,7 @@ atof(const char *nptr) #endif -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO # include PIC_INLINE void diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index b200b4cb..b65fa3b0 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -175,7 +175,7 @@ pic_make_library(pic_state *pic, const char *lib) it = kh_put(ltable, h, pic_str(pic, name), &ret); if (ret == 0) { /* if exists */ - pic_error(pic, "library name already in use", pic_cstr_value(pic, lib)); + pic_error(pic, "library name already in use", 1, pic_cstr_value(pic, lib)); } kh_val(h, it).name = pic_str_ptr(pic, name); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 373412b8..bbbc28d7 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -168,7 +168,6 @@ pic_init_core(pic_state *pic) pic_init_error(pic); DONE; pic_init_str(pic); DONE; pic_init_var(pic); DONE; - pic_init_write(pic); DONE; pic_init_read(pic); DONE; pic_init_dict(pic); DONE; pic_init_record(pic); DONE; @@ -176,6 +175,10 @@ pic_init_core(pic_state *pic) pic_init_lib(pic); DONE; pic_init_weak(pic); DONE; +#if PIC_USE_WRITE + pic_init_write(pic); DONE; +#endif + pic_defun(pic, "features", pic_features); pic_load_cstr(pic, &pic_boot[0][0]); @@ -275,7 +278,7 @@ pic_open(pic_allocf allocf, void *userdata) /* file pool */ memset(pic->files, 0, sizeof pic->files); -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO xfopen_file(pic, stdin, "r"); xfopen_file(pic, stdout, "w"); xfopen_file(pic, stderr, "w"); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 78cccb02..b76914d0 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -6,6 +6,8 @@ #include "picrin/extra.h" #include "picrin/private/object.h" +#if PIC_USE_WRITE + struct writer_control { int mode; int op; @@ -501,3 +503,5 @@ pic_init_write(pic_state *pic) pic_defun(pic, "write-shared", pic_write_write_shared); pic_defun(pic, "display", pic_write_display); } + +#endif From 4a0d0770440853bdc7aafbdfb9a66d8b1d57e12c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 14:09:15 +0900 Subject: [PATCH 118/119] picrin/config.h -> picconf.h --- extlib/benz/include/{picrin/config.h => picconf.h} | 0 extlib/benz/include/picrin/setup.h | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename extlib/benz/include/{picrin/config.h => picconf.h} (100%) diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picconf.h similarity index 100% rename from extlib/benz/include/picrin/config.h rename to extlib/benz/include/picconf.h diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 4fb9e36c..099de355 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -2,7 +2,7 @@ * See Copyright Notice in picrin.h */ -#include "picrin/config.h" +#include "picconf.h" #ifndef PIC_USE_LIBC # define PIC_USE_LIBC 1 From a96e5a0bebae37bad3115ffd84f97d3c14201a8a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 14:15:42 +0900 Subject: [PATCH 119/119] cosmetic changes --- extlib/benz/debug.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 0a0efa0a..d47ed186 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -45,26 +45,21 @@ pic_print_error(pic_state *pic, xFILE *file) assert(! pic_invalid_p(pic, err)); if (! pic_error_p(pic, err)) { - xfprintf(pic, file, "raise: "); - pic_fprintf(pic, port, "~s", err); + pic_fprintf(pic, port, "raise: ~s", err); } else { struct error *e; pic_value elem, it; e = pic_error_ptr(pic, err); if (! pic_eq_p(pic, pic_obj_value(e->type), pic_intern_lit(pic, ""))) { - pic_fprintf(pic, port, "~s", pic_obj_value(e->type)); - xfprintf(pic, file, " "); + pic_fprintf(pic, port, "~s-", pic_obj_value(e->type)); } - xfprintf(pic, file, "error: "); - pic_fprintf(pic, port, "~s", pic_obj_value(e->msg)); + pic_fprintf(pic, port, "error: ~s", pic_obj_value(e->msg)); pic_for_each (elem, e->irrs, it) { /* print error irritants */ pic_fprintf(pic, port, " ~s", elem); } - xfprintf(pic, file, "\n"); - - xfputs(pic, pic_str(pic, pic_obj_value(e->stack)), file); + pic_fprintf(pic, port, "\n%s", pic_str(pic, pic_obj_value(e->stack))); } }