From ada84f48d65a47aac1a493f6a3e98d8c914f88d5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Feb 2016 23:49:16 +0900 Subject: [PATCH] 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); }