2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2016-02-06 10:09:40 -05:00
|
|
|
#include "picrin/opcode.h"
|
|
|
|
|
2016-02-06 10:48:02 -05:00
|
|
|
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
2016-02-06 10:09:40 -05:00
|
|
|
|
2016-02-06 10:48:02 -05:00
|
|
|
#define GET_OPERAND(pic,n) ((pic)->ci->fp[(n)])
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* char type desc.
|
|
|
|
* ---- ---- ----
|
|
|
|
* o pic_value * object
|
|
|
|
* i int * int
|
|
|
|
* I int *, bool * int with exactness
|
|
|
|
* f double * float
|
|
|
|
* F double *, bool * float with exactness
|
|
|
|
* c char * char
|
|
|
|
* z char ** c string
|
|
|
|
* s pic_str ** string object
|
|
|
|
* m pic_sym ** symbol
|
|
|
|
* v pic_vec ** vector object
|
|
|
|
* b pic_blob ** bytevector object
|
|
|
|
* l struct pic_proc ** lambda object
|
|
|
|
* p struct pic_port ** port object
|
|
|
|
* d struct pic_dict ** dictionary object
|
|
|
|
* e struct pic_error ** error object
|
2016-02-06 12:58:18 -05:00
|
|
|
* r struct pic_record ** record object
|
2016-02-06 10:09:40 -05:00
|
|
|
*
|
|
|
|
* | optional operator
|
|
|
|
* * int *, pic_value ** variable length operator
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_get_args(pic_state *pic, const char *format, ...)
|
|
|
|
{
|
|
|
|
char c;
|
2016-02-06 10:48:02 -05:00
|
|
|
int paramc = 0, optc = 0;
|
2016-02-06 10:09:40 -05:00
|
|
|
int i, argc = pic->ci->argc - 1;
|
|
|
|
va_list ap;
|
2016-02-06 10:48:02 -05:00
|
|
|
bool proc = false, rest = false, opt = false;
|
2016-02-06 10:09:40 -05:00
|
|
|
|
2016-02-06 10:48:02 -05:00
|
|
|
/* parse format */
|
|
|
|
if ((c = *format) != '\0') {
|
|
|
|
if (c == '&') {
|
|
|
|
proc = true;
|
|
|
|
format++; /* forget about '&' */
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
2016-02-06 10:48:02 -05:00
|
|
|
for (paramc = 0, c = *format; c; c = format[++paramc]) {
|
|
|
|
if (c == '|') {
|
|
|
|
opt = true;
|
|
|
|
break;
|
|
|
|
} else if (c == '*') {
|
|
|
|
rest = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
2016-02-06 10:48:02 -05:00
|
|
|
for (optc = 0; opt && c; c = format[paramc + opt + ++optc]) {
|
|
|
|
if (c == '*') {
|
|
|
|
rest = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
2016-02-06 10:48:02 -05:00
|
|
|
assert((opt ? 1 : 0) <= optc); /* at least 1 char after '|'? */
|
|
|
|
assert(format[paramc + opt + optc + rest] == '\0'); /* no extra chars? */
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < paramc || (paramc + optc < argc && ! rest)) {
|
|
|
|
pic_errorf(pic, "pic_get_args: wrong number of arguments (%d for %s%d)", argc, rest? "at least " : "", paramc);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2016-02-06 10:48:02 -05:00
|
|
|
|
|
|
|
/* dispatch */
|
|
|
|
if (proc) {
|
|
|
|
struct pic_proc **proc;
|
|
|
|
|
|
|
|
proc = va_arg(ap, struct pic_proc **);
|
|
|
|
*proc = pic_proc_ptr(GET_OPERAND(pic, 0));
|
|
|
|
}
|
|
|
|
for (i = 1; i <= MIN(paramc + optc, argc); ++i) {
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
c = *format++;
|
2016-02-06 10:48:02 -05:00
|
|
|
if (c == '|') {
|
|
|
|
c = *format++;
|
|
|
|
}
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'o': {
|
|
|
|
pic_value *p;
|
|
|
|
|
|
|
|
p = va_arg(ap, pic_value*);
|
2016-02-06 10:48:02 -05:00
|
|
|
*p = GET_OPERAND(pic, i);
|
2016-02-06 10:09:40 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NUM_CASE(c1, c2, ctype) \
|
|
|
|
case c1: case c2: { \
|
|
|
|
ctype *n; \
|
|
|
|
bool *e, dummy; \
|
|
|
|
pic_value v; \
|
|
|
|
\
|
|
|
|
n = va_arg(ap, ctype *); \
|
|
|
|
e = (c == c2 ? va_arg(ap, bool *) : &dummy); \
|
|
|
|
\
|
|
|
|
v = GET_OPERAND(pic, i); \
|
|
|
|
switch (pic_type(v)) { \
|
|
|
|
case PIC_TT_FLOAT: \
|
|
|
|
*n = pic_float(v); \
|
|
|
|
*e = false; \
|
|
|
|
break; \
|
|
|
|
case PIC_TT_INT: \
|
|
|
|
*n = pic_int(v); \
|
|
|
|
*e = true; \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
NUM_CASE('i', 'I', int)
|
|
|
|
NUM_CASE('f', 'F', double)
|
|
|
|
|
|
|
|
#define VAL_CASE(c, type, ctype, conv) \
|
|
|
|
case c: { \
|
|
|
|
ctype *ptr; \
|
|
|
|
pic_value v; \
|
|
|
|
\
|
|
|
|
ptr = va_arg(ap, ctype *); \
|
|
|
|
v = GET_OPERAND(pic, i); \
|
|
|
|
if (pic_## type ##_p(v)) { \
|
|
|
|
*ptr = conv; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
pic_errorf(pic, "pic_get_args: expected " #type ", but got ~s", v); \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
VAL_CASE('c', char, char, pic_char(v))
|
|
|
|
VAL_CASE('z', str, const char *, pic_str_cstr(pic, pic_str_ptr(v)))
|
|
|
|
|
|
|
|
#define PTR_CASE(c, type, ctype) \
|
|
|
|
VAL_CASE(c, type, ctype, pic_## type ##_ptr(v))
|
|
|
|
|
|
|
|
PTR_CASE('s', str, pic_str *)
|
|
|
|
PTR_CASE('m', sym, pic_sym *)
|
|
|
|
PTR_CASE('v', vec, pic_vec *)
|
|
|
|
PTR_CASE('b', blob, pic_blob *)
|
|
|
|
PTR_CASE('l', proc, struct pic_proc *)
|
|
|
|
PTR_CASE('p', port, struct pic_port *)
|
|
|
|
PTR_CASE('d', dict, struct pic_dict *)
|
|
|
|
PTR_CASE('e', error, struct pic_error *)
|
2016-02-06 12:58:18 -05:00
|
|
|
PTR_CASE('r', rec, struct pic_record *)
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rest) {
|
2016-02-06 10:48:02 -05:00
|
|
|
int *n;
|
|
|
|
pic_value **argv;
|
2016-02-06 10:09:40 -05:00
|
|
|
|
2016-02-06 10:48:02 -05:00
|
|
|
n = va_arg(ap, int *);
|
|
|
|
argv = va_arg(ap, pic_value **);
|
|
|
|
*n = argc - (i - 1);
|
|
|
|
*argv = &GET_OPERAND(pic, i);
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
2016-02-06 10:48:02 -05:00
|
|
|
|
2016-02-06 10:09:40 -05:00
|
|
|
va_end(ap);
|
2016-02-06 10:48:02 -05:00
|
|
|
|
2016-02-06 10:09:40 -05:00
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2016-02-07 08:17:56 -05:00
|
|
|
vm_gref(pic_state *pic, pic_sym *uid)
|
2016-02-06 10:09:40 -05:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
if (! pic_weak_has(pic, pic->globals, uid)) {
|
|
|
|
pic_weak_set(pic, pic->globals, uid, pic_invalid_value());
|
2016-02-07 08:17:56 -05:00
|
|
|
|
2016-02-06 10:09:40 -05:00
|
|
|
pic_errorf(pic, "uninitialized global variable: %s", pic_symbol_name(pic, uid));
|
2016-02-07 08:17:56 -05:00
|
|
|
|
|
|
|
return pic_invalid_value();
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
2016-02-07 08:17:56 -05:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
return pic_weak_ref(pic, pic->globals, uid);
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-07 08:17:56 -05:00
|
|
|
vm_gset(pic_state *pic, pic_sym *uid, pic_value value)
|
2016-02-06 10:09:40 -05:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_set(pic, pic->globals, uid, value);
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vm_push_cxt(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_callinfo *ci = pic->ci;
|
|
|
|
|
|
|
|
ci->cxt = (struct pic_context *)pic_obj_alloc(pic, sizeof(struct pic_context) + sizeof(pic_value) * ci->regc, PIC_TT_CXT);
|
|
|
|
ci->cxt->up = ci->up;
|
|
|
|
ci->cxt->regc = ci->regc;
|
|
|
|
ci->cxt->regs = ci->regs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vm_tear_off(pic_callinfo *ci)
|
|
|
|
{
|
|
|
|
struct pic_context *cxt;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(ci->cxt != NULL);
|
|
|
|
|
|
|
|
cxt = ci->cxt;
|
|
|
|
|
|
|
|
if (cxt->regs == cxt->storage) {
|
|
|
|
return; /* is torn off */
|
|
|
|
}
|
|
|
|
for (i = 0; i < cxt->regc; ++i) {
|
|
|
|
cxt->storage[i] = cxt->regs[i];
|
|
|
|
}
|
|
|
|
cxt->regs = cxt->storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_vm_tear_off(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_callinfo *ci;
|
|
|
|
|
|
|
|
for (ci = pic->ci; ci > pic->cibase; ci--) {
|
|
|
|
if (ci->cxt != NULL) {
|
|
|
|
vm_tear_off(ci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if VM_DEBUG
|
|
|
|
# define OPCODE_EXEC_HOOK pic_dump_code(c)
|
|
|
|
#else
|
|
|
|
# define OPCODE_EXEC_HOOK ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PIC_DIRECT_THREADED_VM
|
|
|
|
# define VM_LOOP JUMP;
|
|
|
|
# define CASE(x) L_##x: OPCODE_EXEC_HOOK;
|
|
|
|
# define NEXT pic->ip++; JUMP;
|
|
|
|
# define JUMP c = *pic->ip; goto *oplabels[c.insn];
|
|
|
|
# define VM_LOOP_END
|
|
|
|
#else
|
|
|
|
# define VM_LOOP for (;;) { c = *pic->ip; switch (c.insn) {
|
|
|
|
# define CASE(x) case x:
|
|
|
|
# define NEXT pic->ip++; break
|
|
|
|
# define JUMP break
|
|
|
|
# define VM_LOOP_END } }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PUSH(v) (*pic->sp++ = (v))
|
|
|
|
#define POP() (*--pic->sp)
|
|
|
|
|
|
|
|
#define PUSHCI() (++pic->ci)
|
|
|
|
#define POPCI() (pic->ci--)
|
|
|
|
|
|
|
|
#if VM_DEBUG
|
|
|
|
# define VM_BOOT_PRINT \
|
|
|
|
do { \
|
|
|
|
puts("### booting VM... ###"); \
|
|
|
|
stbase = pic->sp; \
|
|
|
|
cibase = pic->ci; \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
# define VM_BOOT_PRINT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if VM_DEBUG
|
|
|
|
# define VM_END_PRINT \
|
|
|
|
do { \
|
|
|
|
puts("**VM END STATE**"); \
|
|
|
|
printf("stbase\t= %p\nsp\t= %p\n", (void *)stbase, (void *)pic->sp); \
|
|
|
|
printf("cibase\t= %p\nci\t= %p\n", (void *)cibase, (void *)pic->ci); \
|
|
|
|
if (stbase < pic->sp - 1) { \
|
|
|
|
pic_value *sp; \
|
|
|
|
printf("* stack trace:"); \
|
|
|
|
for (sp = stbase; pic->sp != sp; ++sp) { \
|
|
|
|
pic_debug(pic, *sp); \
|
|
|
|
puts(""); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (stbase > pic->sp - 1) { \
|
|
|
|
puts("*** stack underflow!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
# define VM_END_PRINT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if VM_DEBUG
|
|
|
|
# define VM_CALL_PRINT \
|
|
|
|
do { \
|
|
|
|
short i; \
|
|
|
|
puts("\n== calling proc..."); \
|
|
|
|
printf(" proc = "); \
|
|
|
|
pic_debug(pic, pic_obj_value(proc)); \
|
|
|
|
puts(""); \
|
|
|
|
printf(" argv = ("); \
|
|
|
|
for (i = 1; i < c.u.i; ++i) { \
|
|
|
|
if (i > 1) \
|
|
|
|
printf(" "); \
|
|
|
|
pic_debug(pic, pic->sp[-c.u.i + i]); \
|
|
|
|
} \
|
|
|
|
puts(")"); \
|
|
|
|
if (! pic_proc_func_p(proc)) { \
|
|
|
|
printf(" irep = %p\n", proc->u.i.irep); \
|
|
|
|
printf(" name = %s\n", pic_symbol_name(pic, pic_proc_name(proc))); \
|
|
|
|
pic_dump_irep(proc->u.i.irep); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
printf(" cfunc = %p\n", (void *)proc->u.f.func); \
|
|
|
|
printf(" name = %s\n", pic_symbol_name(pic, pic_proc_name(proc))); \
|
|
|
|
} \
|
|
|
|
puts("== end\n"); \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
# define VM_CALL_PRINT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_apply(pic_state *pic, struct pic_proc *proc, int argc, pic_value *argv)
|
|
|
|
{
|
|
|
|
pic_code c;
|
|
|
|
size_t ai = pic_gc_arena_preserve(pic);
|
|
|
|
pic_code boot[2];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#if PIC_DIRECT_THREADED_VM
|
|
|
|
static const void *oplabels[] = {
|
|
|
|
&&L_OP_NOP, &&L_OP_POP, &&L_OP_PUSHUNDEF, &&L_OP_PUSHNIL, &&L_OP_PUSHTRUE,
|
|
|
|
&&L_OP_PUSHFALSE, &&L_OP_PUSHINT, &&L_OP_PUSHFLOAT,
|
|
|
|
&&L_OP_PUSHCHAR, &&L_OP_PUSHEOF, &&L_OP_PUSHCONST,
|
|
|
|
&&L_OP_GREF, &&L_OP_GSET, &&L_OP_LREF, &&L_OP_LSET, &&L_OP_CREF, &&L_OP_CSET,
|
|
|
|
&&L_OP_JMP, &&L_OP_JMPIF, &&L_OP_NOT, &&L_OP_CALL, &&L_OP_TAILCALL, &&L_OP_RET,
|
|
|
|
&&L_OP_LAMBDA, &&L_OP_CONS, &&L_OP_CAR, &&L_OP_CDR, &&L_OP_NILP,
|
|
|
|
&&L_OP_SYMBOLP, &&L_OP_PAIRP,
|
|
|
|
&&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, &&L_OP_DIV,
|
|
|
|
&&L_OP_EQ, &&L_OP_LT, &&L_OP_LE, &&L_OP_GT, &&L_OP_GE, &&L_OP_STOP
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if VM_DEBUG
|
|
|
|
pic_value *stbase;
|
|
|
|
pic_callinfo *cibase;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PUSH(pic_obj_value(proc));
|
|
|
|
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
PUSH(argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
VM_BOOT_PRINT;
|
|
|
|
|
|
|
|
/* boot! */
|
|
|
|
boot[0].insn = OP_CALL;
|
|
|
|
boot[0].a = argc + 1;
|
|
|
|
boot[1].insn = OP_STOP;
|
|
|
|
pic->ip = boot;
|
|
|
|
|
|
|
|
VM_LOOP {
|
|
|
|
CASE(OP_NOP) {
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_POP) {
|
|
|
|
(void)(POP());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHUNDEF) {
|
|
|
|
PUSH(pic_undef_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHNIL) {
|
|
|
|
PUSH(pic_nil_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHTRUE) {
|
|
|
|
PUSH(pic_true_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHFALSE) {
|
|
|
|
PUSH(pic_false_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHINT) {
|
2016-02-07 23:51:41 -05:00
|
|
|
PUSH(pic_int_value(pic->ci->irep->ints[c.a]));
|
2016-02-06 10:09:40 -05:00
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHFLOAT) {
|
2016-02-07 23:51:41 -05:00
|
|
|
PUSH(pic_float_value(pic->ci->irep->nums[c.a]));
|
2016-02-06 10:09:40 -05:00
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHCHAR) {
|
2016-02-07 23:51:41 -05:00
|
|
|
PUSH(pic_char_value(pic->ci->irep->ints[c.a]));
|
2016-02-06 10:09:40 -05:00
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHEOF) {
|
|
|
|
PUSH(pic_eof_object());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PUSHCONST) {
|
|
|
|
PUSH(pic_obj_value(pic->ci->irep->pool[c.a]));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_GREF) {
|
2016-02-07 08:17:56 -05:00
|
|
|
PUSH(vm_gref(pic, (pic_sym *)pic->ci->irep->pool[c.a]));
|
2016-02-06 10:09:40 -05:00
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_GSET) {
|
2016-02-07 08:17:56 -05:00
|
|
|
vm_gset(pic, (pic_sym *)pic->ci->irep->pool[c.a], POP());
|
2016-02-06 10:09:40 -05:00
|
|
|
PUSH(pic_undef_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_LREF) {
|
|
|
|
pic_callinfo *ci = pic->ci;
|
|
|
|
struct pic_irep *irep = ci->irep;
|
|
|
|
|
|
|
|
if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) {
|
|
|
|
if (c.a >= irep->argc + irep->localc) {
|
|
|
|
PUSH(ci->cxt->regs[c.a - (ci->regs - ci->fp)]);
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PUSH(pic->ci->fp[c.a]);
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_LSET) {
|
|
|
|
pic_callinfo *ci = pic->ci;
|
|
|
|
struct pic_irep *irep = ci->irep;
|
|
|
|
|
|
|
|
if (ci->cxt != NULL && ci->cxt->regs == ci->cxt->storage) {
|
|
|
|
if (c.a >= irep->argc + irep->localc) {
|
|
|
|
ci->cxt->regs[c.a - (ci->regs - ci->fp)] = POP();
|
|
|
|
PUSH(pic_undef_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pic->ci->fp[c.a] = POP();
|
|
|
|
PUSH(pic_undef_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_CREF) {
|
|
|
|
int depth = c.a;
|
|
|
|
struct pic_context *cxt;
|
|
|
|
|
|
|
|
cxt = pic->ci->up;
|
|
|
|
while (--depth) {
|
|
|
|
cxt = cxt->up;
|
|
|
|
}
|
|
|
|
PUSH(cxt->regs[c.b]);
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_CSET) {
|
|
|
|
int depth = c.a;
|
|
|
|
struct pic_context *cxt;
|
|
|
|
|
|
|
|
cxt = pic->ci->up;
|
|
|
|
while (--depth) {
|
|
|
|
cxt = cxt->up;
|
|
|
|
}
|
|
|
|
cxt->regs[c.b] = POP();
|
|
|
|
PUSH(pic_undef_value());
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_JMP) {
|
|
|
|
pic->ip += c.a;
|
|
|
|
JUMP;
|
|
|
|
}
|
|
|
|
CASE(OP_JMPIF) {
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
v = POP();
|
|
|
|
if (! pic_false_p(v)) {
|
|
|
|
pic->ip += c.a;
|
|
|
|
JUMP;
|
|
|
|
}
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_CALL) {
|
|
|
|
pic_value x, v;
|
|
|
|
pic_callinfo *ci;
|
|
|
|
|
|
|
|
if (c.a == -1) {
|
|
|
|
pic->sp += pic->ci[1].retc - 1;
|
|
|
|
c.a = pic->ci[1].retc + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
L_CALL:
|
|
|
|
x = pic->sp[-c.a];
|
|
|
|
if (! pic_proc_p(x)) {
|
|
|
|
pic_errorf(pic, "invalid application: ~s", x);
|
|
|
|
}
|
|
|
|
proc = pic_proc_ptr(x);
|
|
|
|
|
|
|
|
VM_CALL_PRINT;
|
|
|
|
|
|
|
|
if (pic->sp >= pic->stend) {
|
|
|
|
pic_panic(pic, "VM stack overflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
ci = PUSHCI();
|
|
|
|
ci->argc = c.a;
|
|
|
|
ci->retc = 1;
|
|
|
|
ci->ip = pic->ip;
|
|
|
|
ci->fp = pic->sp - c.a;
|
|
|
|
ci->irep = NULL;
|
|
|
|
ci->cxt = NULL;
|
|
|
|
if (pic_proc_func_p(proc)) {
|
|
|
|
|
|
|
|
/* invoke! */
|
|
|
|
v = proc->u.f.func(pic);
|
|
|
|
pic->sp[0] = v;
|
|
|
|
pic->sp += pic->ci->retc;
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
goto L_RET;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct pic_irep *irep = proc->u.i.irep;
|
|
|
|
int i;
|
|
|
|
pic_value rest;
|
|
|
|
|
|
|
|
ci->irep = irep;
|
|
|
|
if (ci->argc != irep->argc) {
|
|
|
|
if (! (irep->varg && ci->argc >= irep->argc)) {
|
|
|
|
pic_errorf(pic, "wrong number of arguments (%d for %s%d)", ci->argc - 1, (irep->varg ? "at least " : ""), irep->argc - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* prepare rest args */
|
|
|
|
if (irep->varg) {
|
|
|
|
rest = pic_nil_value();
|
|
|
|
for (i = 0; i < ci->argc - irep->argc; ++i) {
|
|
|
|
pic_gc_protect(pic, v = POP());
|
|
|
|
rest = pic_cons(pic, v, rest);
|
|
|
|
}
|
|
|
|
PUSH(rest);
|
|
|
|
}
|
|
|
|
/* prepare local variable area */
|
|
|
|
if (irep->localc > 0) {
|
|
|
|
int l = irep->localc;
|
|
|
|
if (irep->varg) {
|
|
|
|
--l;
|
|
|
|
}
|
|
|
|
for (i = 0; i < l; ++i) {
|
|
|
|
PUSH(pic_undef_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare cxt */
|
|
|
|
ci->up = proc->u.i.cxt;
|
|
|
|
ci->regc = irep->capturec;
|
|
|
|
ci->regs = ci->fp + irep->argc + irep->localc;
|
|
|
|
|
2016-02-07 23:51:41 -05:00
|
|
|
pic->ip = irep->code;
|
2016-02-06 10:09:40 -05:00
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
JUMP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CASE(OP_TAILCALL) {
|
|
|
|
int i, argc;
|
|
|
|
pic_value *argv;
|
|
|
|
pic_callinfo *ci;
|
|
|
|
|
|
|
|
if (pic->ci->cxt != NULL) {
|
|
|
|
vm_tear_off(pic->ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c.a == -1) {
|
|
|
|
pic->sp += pic->ci[1].retc - 1;
|
|
|
|
c.a = pic->ci[1].retc + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
argc = c.a;
|
|
|
|
argv = pic->sp - argc;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
pic->ci->fp[i] = argv[i];
|
|
|
|
}
|
|
|
|
ci = POPCI();
|
|
|
|
pic->sp = ci->fp + argc;
|
|
|
|
pic->ip = ci->ip;
|
|
|
|
|
|
|
|
/* c is not changed */
|
|
|
|
goto L_CALL;
|
|
|
|
}
|
|
|
|
CASE(OP_RET) {
|
|
|
|
int i, retc;
|
|
|
|
pic_value *retv;
|
|
|
|
pic_callinfo *ci;
|
|
|
|
|
|
|
|
if (pic->ci->cxt != NULL) {
|
|
|
|
vm_tear_off(pic->ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(pic->ci->retc == 1);
|
|
|
|
|
|
|
|
L_RET:
|
|
|
|
retc = pic->ci->retc;
|
|
|
|
retv = pic->sp - retc;
|
|
|
|
if (retc == 0) {
|
|
|
|
pic->ci->fp[0] = retv[0]; /* copy at least once */
|
|
|
|
}
|
|
|
|
for (i = 0; i < retc; ++i) {
|
|
|
|
pic->ci->fp[i] = retv[i];
|
|
|
|
}
|
|
|
|
ci = POPCI();
|
|
|
|
pic->sp = ci->fp + 1; /* advance only one! */
|
|
|
|
pic->ip = ci->ip;
|
|
|
|
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_LAMBDA) {
|
|
|
|
if (pic->ci->cxt == NULL) {
|
|
|
|
vm_push_cxt(pic);
|
|
|
|
}
|
|
|
|
|
2016-02-07 23:51:41 -05:00
|
|
|
proc = pic_make_proc_irep(pic, pic->ci->irep->irep[c.a], pic->ci->cxt);
|
2016-02-06 10:09:40 -05:00
|
|
|
PUSH(pic_obj_value(proc));
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define check_condition(name, n) do { \
|
|
|
|
if (c.a != n + 1) \
|
|
|
|
goto L_CALL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CASE(OP_CONS) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(CONS, 2);
|
|
|
|
pic_gc_protect(pic, b = POP());
|
|
|
|
pic_gc_protect(pic, a = POP());
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_cons(pic, a, b));
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_CAR) {
|
|
|
|
pic_value p;
|
|
|
|
check_condition(CAR, 1);
|
|
|
|
p = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_car(pic, p));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_CDR) {
|
|
|
|
pic_value p;
|
|
|
|
check_condition(CDR, 1);
|
|
|
|
p = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_cdr(pic, p));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_NILP) {
|
|
|
|
pic_value p;
|
|
|
|
check_condition(NILP, 1);
|
|
|
|
p = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_nil_p(p)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_SYMBOLP) {
|
|
|
|
pic_value p;
|
|
|
|
check_condition(SYMBOLP, 1);
|
|
|
|
p = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_sym_p(p)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_PAIRP) {
|
|
|
|
pic_value p;
|
|
|
|
check_condition(PAIRP, 1);
|
|
|
|
p = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_pair_p(p)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_NOT) {
|
|
|
|
pic_value v;
|
|
|
|
check_condition(NOT, 1);
|
|
|
|
v = pic_false_p(POP()) ? pic_true_value() : pic_false_value();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(v);
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
CASE(OP_ADD) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(ADD, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_add(pic, a, b));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_SUB) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(SUB, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_sub(pic, a, b));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_MUL) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(MUL, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_mul(pic, a, b));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_DIV) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(DIV, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_div(pic, a, b));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_EQ) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(EQ, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_eq(pic, a, b)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_LE) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(LT, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_le(pic, a, b)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_LT) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(LE, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_lt(pic, a, b)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_GE) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(LT, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_ge(pic, a, b)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
CASE(OP_GT) {
|
|
|
|
pic_value a, b;
|
|
|
|
check_condition(LE, 2);
|
|
|
|
b = POP();
|
|
|
|
a = POP();
|
|
|
|
(void)POP();
|
|
|
|
PUSH(pic_bool_value(pic_gt(pic, a, b)));
|
|
|
|
NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
CASE(OP_STOP) {
|
|
|
|
|
|
|
|
VM_END_PRINT;
|
|
|
|
|
|
|
|
return pic_gc_protect(pic, POP());
|
|
|
|
}
|
|
|
|
} VM_LOOP_END;
|
|
|
|
}
|
|
|
|
|
2016-02-13 23:33:15 -05:00
|
|
|
pic_value
|
|
|
|
pic_call(pic_state *pic, struct pic_proc *proc, int n, ...)
|
|
|
|
{
|
|
|
|
pic_value r;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, n);
|
|
|
|
r = pic_vcall(pic, proc, n, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_vcall(pic_state *pic, struct pic_proc *proc, int n, va_list ap)
|
|
|
|
{
|
|
|
|
pic_value *args = pic_alloca(pic, sizeof(pic_value) * n);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
args[i] = va_arg(ap, pic_value);
|
|
|
|
}
|
|
|
|
return pic_apply(pic, proc, n, args);
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:09:40 -05:00
|
|
|
pic_value
|
|
|
|
pic_apply_list(pic_state *pic, struct pic_proc *proc, pic_value list)
|
|
|
|
{
|
|
|
|
int n, i = 0;
|
|
|
|
pic_vec *args;
|
|
|
|
pic_value x, it;
|
|
|
|
|
|
|
|
n = pic_length(pic, list);
|
|
|
|
|
|
|
|
args = pic_make_vec(pic, n);
|
|
|
|
|
|
|
|
pic_for_each (x, list, it) {
|
|
|
|
args->data[i++] = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_apply(pic, proc, n, args->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, int argc, pic_value *args)
|
|
|
|
{
|
|
|
|
pic_value *sp;
|
|
|
|
pic_callinfo *ci;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
PIC_INIT_CODE_I(pic->iseq[0], OP_NOP, 0);
|
|
|
|
PIC_INIT_CODE_I(pic->iseq[1], OP_TAILCALL, -1);
|
|
|
|
|
|
|
|
*pic->sp++ = pic_obj_value(proc);
|
|
|
|
|
|
|
|
sp = pic->sp;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
*sp++ = args[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ci = PUSHCI();
|
|
|
|
ci->ip = pic->iseq;
|
|
|
|
ci->fp = pic->sp;
|
|
|
|
ci->retc = (int)argc;
|
|
|
|
|
|
|
|
if (ci->retc == 0) {
|
|
|
|
return pic_undef_value();
|
|
|
|
} else {
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_apply_trampoline_list(pic_state *pic, struct pic_proc *proc, pic_value args)
|
|
|
|
{
|
|
|
|
int i, argc = pic_length(pic, args);
|
|
|
|
pic_value val, it;
|
|
|
|
pic_vec *argv = pic_make_vec(pic, argc);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
pic_for_each (val, args, it) {
|
|
|
|
argv->data[i++] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_apply_trampoline(pic, proc, argc, argv->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-13 23:25:18 -05:00
|
|
|
pic_define(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val)
|
2016-02-06 10:09:40 -05:00
|
|
|
{
|
|
|
|
pic_sym *sym, *uid;
|
|
|
|
|
2016-02-07 14:10:22 -05:00
|
|
|
sym = pic_intern_cstr(pic, name);
|
2016-02-06 10:09:40 -05:00
|
|
|
|
2016-02-13 23:25:18 -05:00
|
|
|
if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) {
|
|
|
|
uid = pic_add_identifier(pic, (pic_id *)sym, lib->env);
|
2016-02-06 10:09:40 -05:00
|
|
|
} else {
|
2016-02-10 07:50:39 -05:00
|
|
|
if (pic_weak_has(pic, pic->globals, uid)) {
|
2016-02-06 10:09:40 -05:00
|
|
|
pic_warnf(pic, "redefining variable: ~s", pic_obj_value(uid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 23:25:18 -05:00
|
|
|
pic_set(pic, lib, name, val);
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_defun(pic_state *pic, const char *name, pic_func_t cfunc)
|
|
|
|
{
|
2016-02-13 23:25:18 -05:00
|
|
|
pic_define(pic, pic->lib, name, pic_obj_value(pic_make_proc(pic, cfunc)));
|
2016-02-07 14:10:22 -05:00
|
|
|
pic_export(pic, pic_intern_cstr(pic, name));
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_defvar(pic_state *pic, const char *name, pic_value init, struct pic_proc *conv)
|
|
|
|
{
|
2016-02-13 23:25:18 -05:00
|
|
|
pic_define(pic, pic->lib, name, pic_obj_value(pic_make_var(pic, init, conv)));
|
2016-02-07 14:10:22 -05:00
|
|
|
pic_export(pic, pic_intern_cstr(pic, name));
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_ref(pic_state *pic, struct pic_lib *lib, const char *name)
|
|
|
|
{
|
|
|
|
pic_sym *sym, *uid;
|
|
|
|
|
2016-02-07 14:10:22 -05:00
|
|
|
sym = pic_intern_cstr(pic, name);
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) {
|
|
|
|
pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name);
|
|
|
|
}
|
|
|
|
|
2016-02-07 08:17:56 -05:00
|
|
|
return vm_gref(pic, uid);
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_set(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val)
|
|
|
|
{
|
|
|
|
pic_sym *sym, *uid;
|
|
|
|
|
2016-02-07 14:10:22 -05:00
|
|
|
sym = pic_intern_cstr(pic, name);
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
if ((uid = pic_find_identifier(pic, (pic_id *)sym, lib->env)) == NULL) {
|
|
|
|
pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name);
|
|
|
|
}
|
|
|
|
|
2016-02-07 08:17:56 -05:00
|
|
|
vm_gset(pic, uid, val);
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
|
|
|
|
2016-02-13 23:33:15 -05:00
|
|
|
pic_value
|
|
|
|
pic_funcall(pic_state *pic, struct pic_lib *lib, const char *name, int n, ...)
|
2016-02-06 10:09:40 -05:00
|
|
|
{
|
2016-02-13 23:33:15 -05:00
|
|
|
pic_value proc, r;
|
|
|
|
va_list ap;
|
2016-02-06 10:09:40 -05:00
|
|
|
|
|
|
|
proc = pic_ref(pic, lib, name);
|
|
|
|
|
|
|
|
pic_assert_type(pic, proc, proc);
|
|
|
|
|
2016-02-13 23:33:15 -05:00
|
|
|
va_start(ap, n);
|
|
|
|
r = pic_vcall(pic, pic_proc_ptr(proc), n, ap);
|
|
|
|
va_end(ap);
|
2016-02-06 10:09:40 -05:00
|
|
|
|
2016-02-13 23:33:15 -05:00
|
|
|
return r;
|
2016-02-06 10:09:40 -05:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-04 10:30:11 -05:00
|
|
|
void
|
|
|
|
pic_irep_incref(pic_state PIC_UNUSED(*pic), struct pic_irep *irep)
|
|
|
|
{
|
|
|
|
irep->refc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_irep_decref(pic_state *pic, struct pic_irep *irep)
|
|
|
|
{
|
2016-02-06 04:15:15 -05:00
|
|
|
size_t i;
|
2016-02-04 10:30:11 -05:00
|
|
|
|
|
|
|
if (--irep->refc == 0) {
|
2016-02-07 23:51:41 -05:00
|
|
|
pic_free(pic, irep->code);
|
|
|
|
pic_free(pic, irep->ints);
|
|
|
|
pic_free(pic, irep->nums);
|
2016-02-04 10:30:11 -05:00
|
|
|
pic_free(pic, irep->pool);
|
|
|
|
|
|
|
|
/* unchain before decref children ireps */
|
|
|
|
irep->list.prev->next = irep->list.next;
|
|
|
|
irep->list.next->prev = irep->list.prev;
|
|
|
|
|
2016-02-06 04:15:15 -05:00
|
|
|
for (i = 0; i < irep->nirep; ++i) {
|
2016-02-07 23:51:41 -05:00
|
|
|
pic_irep_decref(pic, irep->irep[i]);
|
2016-02-04 10:30:11 -05:00
|
|
|
}
|
2016-02-07 23:51:41 -05:00
|
|
|
pic_free(pic, irep->irep);
|
2016-02-04 10:30:11 -05:00
|
|
|
pic_free(pic, irep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
struct pic_proc *
|
2015-06-27 06:02:18 -04:00
|
|
|
pic_make_proc(pic_state *pic, pic_func_t func)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
2015-05-27 10:01:35 -04:00
|
|
|
|
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;
|
2015-05-31 07:46:44 -04:00
|
|
|
proc->u.f.env = NULL;
|
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;
|
2016-02-04 10:30:11 -05:00
|
|
|
pic_irep_incref(pic, irep);
|
2014-08-25 00:38:09 -04:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2015-05-31 07:46:44 -04:00
|
|
|
struct pic_dict *
|
|
|
|
pic_proc_env(pic_state *pic, struct pic_proc *proc)
|
|
|
|
{
|
|
|
|
assert(pic_proc_func_p(proc));
|
|
|
|
|
|
|
|
if (! proc->u.f.env) {
|
|
|
|
proc->u.f.env = pic_make_dict(pic);
|
|
|
|
}
|
|
|
|
return proc->u.f.env;
|
|
|
|
}
|
|
|
|
|
2015-06-08 09:28:17 -04:00
|
|
|
bool
|
|
|
|
pic_proc_env_has(pic_state *pic, struct pic_proc *proc, const char *key)
|
|
|
|
{
|
2016-02-07 14:10:22 -05:00
|
|
|
return pic_dict_has(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key));
|
2015-06-08 09:28:17 -04:00
|
|
|
}
|
|
|
|
|
2015-05-31 07:46:44 -04:00
|
|
|
pic_value
|
|
|
|
pic_proc_env_ref(pic_state *pic, struct pic_proc *proc, const char *key)
|
|
|
|
{
|
2016-02-07 14:10:22 -05:00
|
|
|
return pic_dict_ref(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key));
|
2015-05-31 07:46:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_proc_env_set(pic_state *pic, struct pic_proc *proc, const char *key, pic_value val)
|
|
|
|
{
|
2016-02-07 14:10:22 -05:00
|
|
|
pic_dict_set(pic, pic_proc_env(pic, proc), pic_intern_cstr(pic, key), val);
|
2015-05-31 07:46:44 -04:00
|
|
|
}
|
|
|
|
|
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;
|
2015-08-26 06:04:27 -04:00
|
|
|
int argc;
|
2014-08-25 00:38:09 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-07-04 05:01:30 -04:00
|
|
|
return pic_apply_trampoline_list(pic, proc, arg_list);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_proc(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_defun(pic, "procedure?", pic_proc_proc_p);
|
|
|
|
pic_defun(pic, "apply", pic_proc_apply);
|
|
|
|
}
|