picrin/src/proc.c

69 lines
1.4 KiB
C
Raw Normal View History

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"
struct pic_proc *
2013-10-23 13:02:07 -04:00
pic_proc_new(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);
2013-10-21 04:35:14 -04:00
proc->cfunc_p = false;
proc->u.irep = irep;
2013-10-23 13:02:07 -04:00
proc->env = env;
2013-10-21 04:35:14 -04:00
return proc;
}
struct pic_proc *
pic_proc_new_cfunc(pic_state *pic, pic_func_t cfunc)
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);
2013-10-21 04:35:14 -04:00
proc->cfunc_p = true;
proc->u.cfunc = cfunc;
2013-10-23 13:02:07 -04:00
proc->env = NULL;
2013-10-21 04:35:14 -04:00
return 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)
{
2013-12-03 09:40:50 -05:00
pic_value proc, *args, v;
size_t argc;
int i;
2013-11-09 10:41:59 -05:00
2013-12-03 09:40:50 -05:00
pic_get_args(pic, "o*", &proc, &argc, &args);
2013-11-09 10:41:59 -05:00
if (! pic_proc_p(proc)) {
pic_error(pic, "apply: expected procedure");
}
2013-12-03 09:40:50 -05:00
if (argc == 0) {
pic_error(pic, "apply: wrong number of arguments");
}
v = args[argc - 1];
for (i = argc - 2; i >= 0; --i) {
v = pic_cons(pic, args[i], v);
}
2013-12-03 09:40:50 -05:00
return pic_apply(pic, pic_proc_ptr(proc), v);
2013-11-09 10:41:59 -05:00
}
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);
2013-10-24 11:37:20 -04:00
}