2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
|
|
|
struct pic_proc *
|
2014-09-12 06:46:54 -04:00
|
|
|
pic_make_proc(pic_state *pic, pic_func_t func, const char *name)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
2015-05-27 10:01:35 -04:00
|
|
|
pic_sym *sym;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
assert(name != NULL);
|
|
|
|
|
2015-05-27 10:01:35 -04:00
|
|
|
sym = pic_intern_cstr(pic, name);
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC);
|
2015-05-31 07:22:46 -04:00
|
|
|
proc->tag = PIC_PROC_TAG_FUNC;
|
|
|
|
proc->u.f.func = func;
|
|
|
|
proc->u.f.name = sym;
|
2014-08-25 00:38:09 -04:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_proc *
|
2015-05-30 09:34:51 -04:00
|
|
|
pic_make_proc_irep(pic_state *pic, struct pic_irep *irep, struct pic_context *cxt)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
proc = (struct pic_proc *)pic_obj_alloc(pic, sizeof(struct pic_proc), PIC_TT_PROC);
|
2015-05-31 07:22:46 -04:00
|
|
|
proc->tag = PIC_PROC_TAG_IREP;
|
2015-05-31 07:19:07 -04:00
|
|
|
proc->u.i.irep = irep;
|
|
|
|
proc->u.i.cxt = cxt;
|
2014-08-25 00:38:09 -04:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *
|
2014-08-25 00:38:09 -04:00
|
|
|
pic_proc_name(struct pic_proc *proc)
|
|
|
|
{
|
2015-05-31 07:22:46 -04:00
|
|
|
switch (proc->tag) {
|
|
|
|
case PIC_PROC_TAG_FUNC:
|
|
|
|
return proc->u.f.name;
|
|
|
|
case PIC_PROC_TAG_IREP:
|
2015-05-31 07:19:07 -04:00
|
|
|
return proc->u.i.irep->name;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-01-07 16:11:48 -05:00
|
|
|
PIC_UNREACHABLE();
|
2014-08-25 00:38:09 -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));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_proc_apply(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
|
|
|
pic_value *args;
|
|
|
|
size_t argc;
|
|
|
|
pic_value arg_list;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l*", &proc, &argc, &args);
|
|
|
|
|
|
|
|
if (argc == 0) {
|
2014-09-16 10:43:15 -04:00
|
|
|
pic_errorf(pic, "apply: wrong number of arguments");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
arg_list = args[--argc];
|
|
|
|
while (argc--) {
|
|
|
|
arg_list = pic_cons(pic, args[argc], arg_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_apply_trampoline(pic, proc, arg_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_proc(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_defun(pic, "procedure?", pic_proc_proc_p);
|
|
|
|
pic_defun(pic, "apply", pic_proc_apply);
|
|
|
|
}
|