add some object constructors/destructors

This commit is contained in:
Yuichi Nishiwaki 2016-02-18 23:49:16 +09:00
parent 08652df612
commit ada84f48d6
26 changed files with 130 additions and 126 deletions

View File

@ -246,7 +246,7 @@ pic_callcc_full(pic_state *pic, struct pic_proc *proc)
struct pic_proc *c; struct pic_proc *c;
/* save the continuation object in proc */ /* 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)); return pic_call(pic, proc, 1, pic_obj_value(c));
} }
@ -269,7 +269,7 @@ pic_callcc_callcc(pic_state *pic)
pic_value args[1]; pic_value args[1];
/* save the continuation object in proc */ /* 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); args[0] = pic_obj_value(c);
return pic_applyk(pic, proc, 1, args); return pic_applyk(pic, proc, 1, args);

View File

@ -13,7 +13,7 @@ pic_str_set(pic_state *pic, struct pic_string *str, int i, char c)
buf[0] = c; buf[0] = c;
x = pic_str_sub(pic, str, 0, i); 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)); 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)); tmp = pic_str_cat(pic, x, pic_str_cat(pic, y, z));

View File

@ -21,7 +21,7 @@ pic_system_cmdline(pic_state *pic)
for (i = 0; i < picrin_argc; ++i) { for (i = 0; i < picrin_argc; ++i) {
size_t ai = pic_gc_arena_preserve(pic); 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); pic_gc_arena_restore(pic, ai);
} }
@ -88,7 +88,7 @@ pic_system_getenv(pic_state *pic)
if (val == NULL) if (val == NULL)
return pic_nil_value(pic); return pic_nil_value(pic);
else else
return pic_obj_value(pic_make_cstr(pic, val)); return pic_obj_value(pic_cstr_value(pic, val));
} }
static pic_value static pic_value
@ -111,8 +111,8 @@ pic_system_getenvs(pic_state *pic)
for (i = 0; (*envp)[i] != '='; ++i) for (i = 0; (*envp)[i] != '='; ++i)
; ;
key = pic_make_str(pic, *envp, i); key = pic_str_value(pic, *envp, i);
val = pic_make_cstr(pic, getenv(pic_str_cstr(pic, key))); val = pic_cstr_value(pic, getenv(pic_str(pic, key)));
/* push */ /* push */
data = pic_acons(pic, pic_obj_value(key), pic_obj_value(val), data); data = pic_acons(pic, pic_obj_value(key), pic_obj_value(val), data);

View File

@ -19,7 +19,7 @@ pic_rl_readline(pic_state *pic)
result = readline(prompt); result = readline(prompt);
if(result) if(result)
return pic_obj_value(pic_make_cstr(pic, result)); return pic_obj_value(pic_cstr_value(pic, result));
else else
return pic_eof_object(pic); return pic_eof_object(pic);
} }
@ -87,7 +87,7 @@ pic_rl_current_history(pic_state *pic)
{ {
pic_get_args(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 static pic_value
@ -100,7 +100,7 @@ pic_rl_history_get(pic_state *pic)
e = history_get(i); 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); : pic_false_value(pic);
} }
@ -114,7 +114,7 @@ pic_rl_remove_history(pic_state *pic)
e = remove_history(i); 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); : pic_false_value(pic);
} }
@ -148,7 +148,7 @@ pic_rl_previous_history(pic_state *pic)
e = previous_history(); 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); : pic_false_value(pic);
} }
@ -161,7 +161,7 @@ pic_rl_next_history(pic_state *pic)
e = next_history(); 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); : pic_false_value(pic);
} }
@ -240,7 +240,7 @@ pic_rl_history_expand(pic_state *pic)
if(status == -1 || status == 2) if(status == -1 || status == 2)
pic_errorf(pic, "%s\n", result); 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 void

View File

@ -62,7 +62,7 @@ pic_regexp_regexp(pic_state *pic)
pic_errorf(pic, "regexp compilation error: %s", errbuf); pic_errorf(pic, "regexp compilation error: %s", errbuf);
} }
return pic_obj_value(pic_data_alloc(pic, &regexp_type, reg)); return pic_obj_value(pic_data_value(pic, reg, &regexp_type));
} }
static pic_value static pic_value
@ -97,7 +97,7 @@ pic_regexp_regexp_match(pic_state *pic)
offset = 0; offset = 0;
while (regexec(&pic_regexp_data_ptr(reg)->reg, input, 1, match, 0) != REG_NOMATCH) { 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); pic_push(pic, pic_int_value(pic, offset), positions);
offset += match[0].rm_eo; offset += match[0].rm_eo;
@ -111,7 +111,7 @@ pic_regexp_regexp_match(pic_state *pic)
if (match[i].rm_so == -1) { if (match[i].rm_so == -1) {
break; 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_obj_value(str), matches);
pic_push(pic, pic_int_value(pic, match[i].rm_so), positions); 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); 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_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; 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); return pic_reverse(pic, output);
} }
@ -157,20 +157,20 @@ pic_regexp_regexp_replace(pic_state *pic)
pic_value reg; pic_value reg;
const char *input; const char *input;
regmatch_t match; 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", &reg, &input, &txt); pic_get_args(pic, "ozs", &reg, &input, &txt);
pic_assert_type(pic, reg, regexp); 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_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); output = pic_str_cat(pic, output, txt);
input += match.rm_eo; 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); return pic_obj_value(output);
} }

View File

@ -80,10 +80,10 @@ pic_socket_make_socket(pic_state *pic)
node = service = NULL; node = service = NULL;
if (pic_str_p(pic, n)) { 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)) { 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)); 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)); 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 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 = pic_malloc(pic, sizeof(struct pic_socket_t));
new_sock->fd = fd; 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 static pic_value

View File

@ -89,7 +89,7 @@ When you use dynamic memory allocation inside C APIs, you must be caseful about
f = create_foo(); f = create_foo();
data = pic_data_alloc(pic, &foo_type, md); data = pic_data_value(pic, md, &foo_type);
return pic_obj_value(data); return pic_obj_value(data);
} }

View File

@ -124,7 +124,7 @@ pic_make_cont(pic_state *pic, struct pic_cont *cont)
struct pic_proc *c; struct pic_proc *c;
/* save the escape continuation in proc */ /* 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; return c;
} }

View File

@ -1,13 +1,2 @@
#include "picrin.h" #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;
}

View File

@ -11,18 +11,18 @@ pic_get_backtrace(pic_state *pic)
pic_callinfo *ci; pic_callinfo *ci;
struct pic_string *trace; struct pic_string *trace;
trace = pic_make_lit(pic, ""); trace = pic_lit_value(pic, "");
for (ci = pic->ci; ci != pic->cibase; --ci) { for (ci = pic->ci; ci != pic->cibase; --ci) {
struct pic_proc *proc = pic_proc_ptr(ci->fp[0]); 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_lit_value(pic, " at "));
trace = pic_str_cat(pic, trace, pic_make_lit(pic, "(anonymous lambda)")); trace = pic_str_cat(pic, trace, pic_lit_value(pic, "(anonymous lambda)"));
if (pic_proc_func_p(proc)) { 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)) { } 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"); xfprintf(pic, file, "\n");
xfputs(pic, pic_str_cstr(pic, e->stack), file); xfputs(pic, pic_str(pic, e->stack), file);
} }
} }

View File

@ -24,10 +24,10 @@ pic_warnf(pic_state *pic, const char *fmt, ...)
struct pic_string *err; struct pic_string *err;
va_start(ap, fmt); va_start(ap, fmt);
err = pic_vformat(pic, fmt, ap); err = pic_vstrf_value(pic, fmt, ap);
va_end(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 void
@ -38,10 +38,10 @@ pic_errorf(pic_state *pic, const char *fmt, ...)
struct pic_string *err; struct pic_string *err;
va_start(ap, fmt); va_start(ap, fmt);
err = pic_vformat(pic, fmt, ap); err = pic_vstrf_value(pic, fmt, ap);
va_end(ap); va_end(ap);
msg = pic_str_cstr(pic, err); msg = pic_str(pic, err);
pic_error(pic, msg, pic_nil_value(pic)); 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 = (struct pic_error *)pic_obj_alloc(pic, sizeof(struct pic_error), PIC_TYPE_ERROR);
e->type = type; e->type = type;
e->msg = pic_make_cstr(pic, msg); e->msg = pic_cstr_value(pic, msg);
e->irrs = irrs; e->irrs = irrs;
e->stack = stack; e->stack = stack;

View File

@ -702,7 +702,7 @@ pic_alloca(pic_state *pic, size_t n)
static const pic_data_type t = { "pic_alloca", pic_free, 0 }; static const pic_data_type t = { "pic_alloca", pic_free, 0 };
/* TODO: optimize */ /* 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 * struct pic_object *

View File

@ -61,6 +61,7 @@ struct pic_proc;
struct pic_port; struct pic_port;
struct pic_error; struct pic_error;
struct pic_env; struct pic_env;
struct pic_data;
typedef struct pic_symbol pic_sym; typedef struct pic_symbol pic_sym;
typedef struct pic_id pic_id; 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_port_p(pic, v) (pic_type(pic, v) == PIC_TYPE_PORT)
#define pic_sym_p(pic,v) (pic_type(pic,v) == PIC_TYPE_SYMBOL) #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_undef_value(pic_state *);
pic_value pic_int_value(pic_state *, int); pic_value pic_int_value(pic_state *, int);
pic_value pic_float_value(pic_state *, double); 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_false_value(pic_state *);
pic_value pic_bool_value(pic_state *, bool); pic_value pic_bool_value(pic_state *, bool);
pic_value pic_eof_object(pic_state *); pic_value pic_eof_object(pic_state *);
struct pic_string *pic_str_value(pic_state *, const char *str, int len);
int pic_int(pic_state *, pic_value); #define pic_cstr_value(pic, cstr) pic_str_value(pic, (cstr), strlen(cstr))
double pic_float(pic_state *, pic_value); #define pic_lit_value(pic, lit) pic_str_value(pic, "" lit, -((int)sizeof lit - 1))
char pic_char(pic_state *, pic_value); struct pic_string *pic_strf_value(pic_state *, const char *fmt, ...);
bool pic_bool(pic_state *, pic_value); struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap);
/* const char *pic_str(pic_state *, pic_value); */ struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len);
/* unsigned char *pic_blob(pic_state *, pic_value, int *len); */ struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type);
/* void *pic_data(pic_state *, pic_value); */
int pic_type(pic_state *, pic_value); int pic_type(pic_state *, pic_value);
const char *pic_typename(pic_state *, int); const char *pic_typename(pic_state *, int);
@ -227,10 +241,10 @@ bool pic_weak_has(pic_state *, struct pic_weak *, void *);
/* symbol */ /* symbol */
pic_sym *pic_intern(pic_state *, struct pic_string *); 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_str(pic,s,i) pic_intern(pic, pic_str_value(pic, (s), (i)))
#define pic_intern_cstr(pic,s) pic_intern(pic, pic_make_cstr(pic, (s))) #define pic_intern_cstr(pic,s) pic_intern(pic, pic_cstr_value(pic, (s)))
#define pic_intern_lit(pic,lit) pic_intern(pic, pic_make_lit(pic, lit)) #define pic_intern_lit(pic,lit) pic_intern(pic, pic_lit_value(pic, lit))
const char *pic_symbol_name(pic_state *, pic_sym *); struct pic_string *pic_sym_name(pic_state *, pic_sym *);
/* string */ /* string */
int pic_str_len(pic_state *, struct pic_string *); int pic_str_len(pic_state *, struct pic_string *);

View File

@ -9,12 +9,6 @@
extern "C" { extern "C" {
#endif #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 { struct pic_data {
PIC_OBJECT_HEADER PIC_OBJECT_HEADER
const pic_data_type *type; 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; 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) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -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)) #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) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -61,11 +61,11 @@ pic_make_library(pic_state *pic, const char *lib)
old_lib = pic_current_library(pic); old_lib = pic_current_library(pic);
} }
name = pic_make_cstr(pic, lib); name = pic_cstr_value(pic, lib);
env = make_library_env(pic, name); env = make_library_env(pic, name);
exports = pic_make_dict(pic); 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 */ if (ret == 0) { /* if exists */
pic_errorf(pic, "library name already in use: %s", lib); 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 * const char *
pic_current_library(pic_state *pic) pic_current_library(pic_state *pic)
{ {
return pic_str_cstr(pic, pic->lib->name); return pic_str(pic, pic->lib->name);
} }
struct pic_env * struct pic_env *

View File

@ -42,9 +42,9 @@ pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
name = pic_identifier_name(pic, id); name = pic_identifier_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, 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 { } 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); uid = pic_intern(pic, str);

View File

@ -234,7 +234,7 @@ pic_number_number_to_string(pic_state *pic)
number_string(ival, radix, ilen, buf); 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); pic_free(pic, buf);
} }

View File

@ -108,7 +108,7 @@ pic_open_file(pic_state *pic, const char *name, int flags) {
mode = 'w'; mode = 'w';
} }
if ((file = file_open(pic, name, &mode)) == NULL) { 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); 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; s = port->file->vtable.cookie;
return pic_make_str(pic, s->buf, s->end); return pic_str_value(pic, s->buf, s->end);
} }
void void

View File

@ -141,7 +141,7 @@ pic_get_args(pic_state *pic, const char *format, ...)
} }
VAL_CASE('c', char, char, pic_char(pic, v)) 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) \ #define PTR_CASE(c, type, ctype) \
VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) VAL_CASE(c, type, ctype, pic_## type ##_ptr(v))
@ -179,7 +179,7 @@ static pic_value
vm_gref(pic_state *pic, pic_sym *uid) vm_gref(pic_state *pic, pic_sym *uid)
{ {
if (! pic_weak_has(pic, pic->globals, 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); return pic_weak_ref(pic, pic->globals, uid);
} }
@ -308,12 +308,12 @@ pic_vm_tear_off(pic_state *pic)
puts(")"); \ puts(")"); \
if (! pic_proc_func_p(proc)) { \ if (! pic_proc_func_p(proc)) { \
printf(" irep = %p\n", proc->u.i.irep); \ 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); \ pic_dump_irep(proc->u.i.irep); \
} \ } \
else { \ else { \
printf(" cfunc = %p\n", (void *)proc->u.f.func); \ 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"); \ puts("== end\n"); \
} while (0) } while (0)

View File

@ -282,7 +282,7 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
} }
if (idx >= ATOF_BUF_SIZE) if (idx >= ATOF_BUF_SIZE)
read_error(pic, "number too large", 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)) 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_list1(pic, pic_char_value(pic, c)));
@ -321,10 +321,10 @@ read_minus(pic_state *pic, struct pic_port *port, int c)
} }
else { else {
sym = read_symbol(pic, port, c); 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)); 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 pic_float_value(pic, -(0.0 / 0.0));
} }
return sym; return sym;
@ -341,10 +341,10 @@ read_plus(pic_state *pic, struct pic_port *port, int c)
} }
else { else {
sym = read_symbol(pic, port, c); 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); 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 pic_float_value(pic, 0.0 / 0.0);
} }
return sym; return sym;
@ -444,7 +444,7 @@ read_string(pic_state *pic, struct pic_port *port, int c)
} }
buf[cnt] = '\0'; buf[cnt] = '\0';
str = pic_make_str(pic, buf, cnt); str = pic_str_value(pic, buf, cnt);
pic_free(pic, buf); pic_free(pic, buf);
return pic_obj_value(str); return pic_obj_value(str);
} }

View File

@ -94,7 +94,7 @@ pic_make_rope(pic_state *pic, struct pic_chunk *c)
} }
static struct pic_string * 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; struct pic_string *str;
@ -237,7 +237,7 @@ rope_cstr(pic_state *pic, struct pic_rope *x)
} }
struct pic_string * 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; 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); 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 int
@ -273,19 +273,19 @@ pic_str_ref(pic_state *pic, struct pic_string *str, int i)
struct pic_string * struct pic_string *
pic_str_cat(pic_state *pic, struct pic_string *a, struct pic_string *b) 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 * struct pic_string *
pic_str_sub(pic_state *pic, struct pic_string *str, int s, int e) 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 int
pic_str_cmp(pic_state *pic, struct pic_string *str1, struct pic_string *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)); return strcmp(pic_str(pic, str1), pic_str(pic, str2));
} }
int int
@ -294,7 +294,7 @@ pic_str_hash(pic_state *pic, struct pic_string *str)
const char *s; const char *s;
int h = 0; int h = 0;
s = pic_str_cstr(pic, str); s = pic_str(pic, str);
while (*s) { while (*s) {
h = (h << 5) - h + *s++; h = (h << 5) - h + *s++;
} }
@ -302,7 +302,7 @@ pic_str_hash(pic_state *pic, struct pic_string *str)
} }
const char * 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); 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 * 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_port *port;
struct pic_string *str; struct pic_string *str;
@ -389,13 +389,13 @@ pic_vformat(pic_state *pic, const char *fmt, va_list ap)
} }
struct pic_string * struct pic_string *
pic_format(pic_state *pic, const char *fmt, ...) pic_strf_value(pic_state *pic, const char *fmt, ...)
{ {
va_list ap; va_list ap;
struct pic_string *str; struct pic_string *str;
va_start(ap, fmt); va_start(ap, fmt);
str = pic_vformat(pic, fmt, ap); str = pic_vstrf_value(pic, fmt, ap);
va_end(ap); va_end(ap);
return str; return str;
@ -428,7 +428,7 @@ pic_str_string(pic_state *pic)
buf[i] = pic_char(pic, argv[i]); 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); pic_free(pic, buf);
return pic_obj_value(str); return pic_obj_value(str);
@ -447,7 +447,7 @@ pic_str_make_string(pic_state *pic)
buf = pic_malloc(pic, len); buf = pic_malloc(pic, len);
memset(buf, c, 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); pic_free(pic, buf);
return ret; return ret;
@ -536,7 +536,7 @@ pic_str_string_append(pic_state *pic)
pic_get_args(pic, "*", &argc, &argv); pic_get_args(pic, "*", &argc, &argv);
str = pic_make_lit(pic, ""); str = pic_lit_value(pic, "");
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
if (! pic_str_p(pic, argv[i])) { if (! pic_str_p(pic, argv[i])) {
pic_errorf(pic, "type error"); pic_errorf(pic, "type error");
@ -583,7 +583,7 @@ pic_str_string_map(pic_state *pic)
pic_assert_type(pic, val, char); pic_assert_type(pic, val, char);
buf[i] = pic_char(pic, val); buf[i] = pic_char(pic, val);
} }
str = pic_make_str(pic, buf, len); str = pic_str_value(pic, buf, len);
} }
pic_catch { pic_catch {
pic_free(pic, buf); pic_free(pic, buf);
@ -640,7 +640,7 @@ pic_str_list_to_string(pic_state *pic)
pic_get_args(pic, "o", &list); pic_get_args(pic, "o", &list);
if (pic_length(pic, list) == 0) { 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)); 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); buf[i++] = pic_char(pic, e);
} }
str = pic_make_str(pic, buf, i); str = pic_str_value(pic, buf, i);
} }
pic_catch { pic_catch {
pic_free(pic, buf); pic_free(pic, buf);

View File

@ -44,10 +44,10 @@ pic_make_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
return nid; return nid;
} }
const char * struct pic_string *
pic_symbol_name(pic_state *pic, pic_sym *sym) pic_sym_name(pic_state PIC_UNUSED(*pic), pic_sym *sym)
{ {
return pic_str_cstr(pic, sym->str); return sym->str;
} }
const char * const char *
@ -57,7 +57,7 @@ pic_identifier_name(pic_state *pic, pic_id *id)
id = id->u.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 static pic_value

View File

@ -72,3 +72,21 @@ pic_typename(pic_state *pic, int type)
pic_errorf(pic, "pic_typename: invalid type given %d", 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;
}

View File

@ -346,7 +346,7 @@ pic_vec_vector_to_string(pic_state *pic)
buf[i - start] = pic_char(pic, vec->data[i]); 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); pic_free(pic, buf);
return pic_obj_value(str); return pic_obj_value(str);

View File

@ -85,10 +85,10 @@ static void
write_str(pic_state *pic, struct pic_string *str, xFILE *file, int mode) write_str(pic_state *pic, struct pic_string *str, xFILE *file, int mode)
{ {
int i; int i;
const char *cstr = pic_str_cstr(pic, str); const char *cstr = pic_str(pic, str);
if (mode == DISPLAY_MODE) { if (mode == DISPLAY_MODE) {
xfprintf(pic, file, "%s", pic_str_cstr(pic, str)); xfprintf(pic, file, "%s", pic_str(pic, str));
return; return;
} }
xfprintf(pic, file, "\""); xfprintf(pic, file, "\"");
@ -246,7 +246,7 @@ write_dict(struct writer_control *p, struct pic_dict *dict)
xfprintf(pic, file, "#.(dictionary"); xfprintf(pic, file, "#.(dictionary");
pic_dict_for_each (sym, dict, it) { 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)); write_core(p, pic_dict_ref(pic, dict, sym));
} }
xfprintf(pic, file, ")"); xfprintf(pic, file, ")");
@ -298,7 +298,7 @@ write_core(struct writer_control *p, pic_value obj)
write_float(pic, pic_float(pic, obj), file); write_float(pic, pic_float(pic, obj), file);
break; break;
case PIC_TYPE_SYMBOL: 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; break;
case PIC_TYPE_BLOB: case PIC_TYPE_BLOB:
write_blob(pic, pic_blob_ptr(obj), file); write_blob(pic, pic_blob_ptr(obj), file);
@ -440,11 +440,11 @@ pic_printf(pic_state *pic, const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
str = pic_vformat(pic, fmt, ap); str = pic_vstrf_value(pic, fmt, ap);
va_end(ap); va_end(ap);
xfprintf(pic, file, "%s", pic_str_cstr(pic, str)); xfprintf(pic, file, "%s", pic_str(pic, str));
xfflush(pic, file); xfflush(pic, file);
} }