picrin/src/proc.c

59 lines
1.1 KiB
C
Raw Normal View History

2013-10-21 04:35:14 -04:00
#include "picrin.h"
#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);
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);
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)
{
pic_value proc, args;
pic_get_args(pic, "oo", &proc, &args);
if (! pic_proc_p(proc)) {
pic_error(pic, "apply: expected procedure");
}
2013-11-09 10:41:59 -05:00
return pic_apply(pic, pic_proc_ptr(proc), args);
}
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
}