picrin/src/proc.c

243 lines
4.9 KiB
C
Raw Normal View History

2014-01-17 06:58:31 -05:00
/**
* See Copyright Notice in picrin.h
*/
2013-10-21 04:35:14 -04:00
#include "picrin.h"
2013-12-03 09:40:50 -05:00
#include "picrin/pair.h"
2013-10-21 04:35:14 -04:00
#include "picrin/proc.h"
#include "picrin/irep.h"
#include "picrin/dict.h"
2013-10-21 04:35:14 -04:00
struct pic_proc *
2014-03-26 08:20:06 -04:00
pic_proc_new(pic_state *pic, pic_func_t func, const char *name)
2013-10-21 04:35:14 -04:00
{
struct pic_proc *proc;
2014-03-26 08:20:06 -04:00
assert(name != NULL);
proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC);
proc->kind = PIC_PROC_KIND_FUNC;
proc->u.func.f = func;
2014-03-26 08:20:06 -04:00
proc->u.func.name = pic_intern_cstr(pic, name);
2014-01-08 08:44:53 -05:00
proc->env = NULL;
proc->attr = NULL;
2013-10-21 04:35:14 -04:00
return proc;
}
struct pic_proc *
2014-01-08 08:44:53 -05:00
pic_proc_new_irep(pic_state *pic, struct pic_irep *irep, struct pic_env *env)
2013-10-21 04:35:14 -04:00
{
struct pic_proc *proc;
proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC);
proc->kind = PIC_PROC_KIND_IREP;
2014-01-08 08:44:53 -05:00
proc->u.irep = irep;
proc->env = env;
proc->attr = NULL;
2013-10-21 04:35:14 -04:00
return proc;
}
2013-10-24 11:37:20 -04:00
2014-03-27 23:34:54 -04:00
pic_sym
pic_proc_name(struct pic_proc *proc)
{
switch (proc->kind) {
case PIC_PROC_KIND_FUNC:
return proc->u.func.name;
case PIC_PROC_KIND_IREP:
return proc->u.irep->name;
}
UNREACHABLE();
}
struct pic_dict *
pic_proc_attr(pic_state *pic, struct pic_proc *proc)
{
if (proc->attr == NULL) {
proc->attr = pic_dict_new(pic);
}
return proc->attr;
}
void
pic_proc_cv_init(pic_state *pic, struct pic_proc *proc, size_t cv_size)
{
struct pic_env *env;
if (proc->env != NULL) {
pic_error(pic, "env slot already in use");
}
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV);
2014-03-22 23:45:36 -04:00
env->regc = cv_size;
env->regs = (pic_value *)pic_calloc(pic, cv_size, sizeof(pic_value));
env->up = NULL;
proc->env = env;
}
int
pic_proc_cv_size(pic_state *pic, struct pic_proc *proc)
{
2014-01-30 04:15:59 -05:00
UNUSED(pic);
2014-03-22 23:45:36 -04:00
return proc->env ? proc->env->regc : 0;
}
pic_value
pic_proc_cv_ref(pic_state *pic, struct pic_proc *proc, size_t i)
{
if (proc->env == NULL) {
pic_error(pic, "no closed env");
}
2014-03-22 23:45:36 -04:00
return proc->env->regs[i];
}
void
pic_proc_cv_set(pic_state *pic, struct pic_proc *proc, size_t i, pic_value v)
{
if (proc->env == NULL) {
pic_error(pic, "no closed env");
}
2014-03-22 23:45:36 -04:00
proc->env->regs[i] = v;
}
2014-02-11 11:17:05 -05:00
static pic_value
papply_call(pic_state *pic)
{
size_t argc;
pic_value *argv, arg, arg_list;
struct pic_proc *proc;
pic_get_args(pic, "*", &argc, &argv);
proc = pic_proc_ptr(pic_proc_cv_ref(pic, pic_get_proc(pic), 0));
arg = pic_proc_cv_ref(pic, pic_get_proc(pic), 1);
arg_list = pic_list_by_array(pic, argc, argv);
arg_list = pic_cons(pic, arg, arg_list);
return pic_apply(pic, proc, arg_list);
}
struct pic_proc *
pic_papply(pic_state *pic, struct pic_proc *proc, pic_value arg)
{
struct pic_proc *pa_proc;
2014-03-26 08:20:06 -04:00
pa_proc = pic_proc_new(pic, papply_call, "<partial-applied-procedure>");
2014-02-11 11:17:05 -05:00
pic_proc_cv_init(pic, pa_proc, 2);
pic_proc_cv_set(pic, pa_proc, 0, pic_obj_value(proc));
pic_proc_cv_set(pic, pa_proc, 1, arg);
return pa_proc;
}
2013-10-24 11:37:20 -04:00
static pic_value
pic_proc_proc_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic_proc_p(v));
}
2013-11-09 10:41:59 -05:00
static pic_value
pic_proc_apply(pic_state *pic)
{
2014-01-08 06:53:28 -05:00
struct pic_proc *proc;
pic_value *args;
2013-12-03 09:40:50 -05:00
size_t argc;
2014-02-06 00:22:42 -05:00
pic_value arg_list;
2013-11-09 10:41:59 -05:00
2014-01-08 06:53:28 -05:00
pic_get_args(pic, "l*", &proc, &argc, &args);
2013-11-09 10:41:59 -05:00
2013-12-03 09:40:50 -05:00
if (argc == 0) {
pic_error(pic, "apply: wrong number of arguments");
}
2014-02-04 00:33:36 -05:00
arg_list = args[--argc];
while (argc--) {
arg_list = pic_cons(pic, args[argc], arg_list);
}
return pic_apply_trampoline(pic, proc, arg_list);
2014-01-22 08:18:25 -05:00
}
static pic_value
pic_proc_map(pic_state *pic)
{
struct pic_proc *proc;
size_t argc;
pic_value *args;
int i;
pic_value cars, ret;
pic_get_args(pic, "l*", &proc, &argc, &args);
ret = pic_nil_value();
do {
cars = pic_nil_value();
for (i = argc - 1; i >= 0; --i) {
if (! pic_pair_p(args[i])) {
break;
}
cars = pic_cons(pic, pic_car(pic, args[i]), cars);
args[i] = pic_cdr(pic, args[i]);
}
if (i >= 0)
break;
ret = pic_cons(pic, pic_apply(pic, proc, cars), ret);
} while (1);
return pic_reverse(pic, ret);
2013-11-09 10:41:59 -05:00
}
2014-01-22 08:21:48 -05:00
static pic_value
pic_proc_for_each(pic_state *pic)
{
struct pic_proc *proc;
size_t argc;
pic_value *args;
int i;
pic_value cars;
pic_get_args(pic, "l*", &proc, &argc, &args);
do {
cars = pic_nil_value();
for (i = argc - 1; i >= 0; --i) {
if (! pic_pair_p(args[i])) {
break;
}
cars = pic_cons(pic, pic_car(pic, args[i]), cars);
args[i] = pic_cdr(pic, args[i]);
}
if (i >= 0)
break;
pic_apply(pic, proc, cars);
} while (1);
return pic_none_value();
}
2014-07-12 22:01:23 -04:00
static pic_value
pic_proc_attribute(pic_state *pic)
{
struct pic_proc *proc;
pic_get_args(pic, "l", &proc);
return pic_obj_value(pic_proc_attr(pic, proc));
}
2013-10-24 11:37:20 -04:00
void
pic_init_proc(pic_state *pic)
{
pic_defun(pic, "procedure?", pic_proc_proc_p);
2013-11-09 10:41:59 -05:00
pic_defun(pic, "apply", pic_proc_apply);
2014-01-22 08:18:25 -05:00
pic_defun(pic, "map", pic_proc_map);
2014-01-22 08:21:48 -05:00
pic_defun(pic, "for-each", pic_proc_for_each);
2014-07-12 22:01:23 -04:00
pic_deflibrary ("(picrin attribute)") {
pic_defun(pic, "attribute", pic_proc_attribute);
}
2013-10-24 11:37:20 -04:00
}