add pic_apply function (and remove pic_run)

This commit is contained in:
Yuichi Nishiwaki 2013-10-28 17:45:13 +09:00
parent 67c13b841c
commit 4fa2bad5fa
5 changed files with 35 additions and 13 deletions

View File

@ -80,7 +80,8 @@ pic_value pic_str_new_cstr(pic_state *, const char *);
bool pic_parse_file(pic_state *, FILE *file, pic_value *); bool pic_parse_file(pic_state *, FILE *file, pic_value *);
bool pic_parse_cstr(pic_state *, const char *, pic_value *); bool pic_parse_cstr(pic_state *, const char *, pic_value *);
pic_value pic_run(pic_state *, struct pic_proc *, pic_value); pic_value pic_apply(pic_state *pic, struct pic_proc *, pic_value);
pic_value pic_apply_argv(pic_state *pic, struct pic_proc *, size_t, ...);
struct pic_proc *pic_codegen(pic_state *, pic_value); struct pic_proc *pic_codegen(pic_state *, pic_value);
void pic_abort(pic_state *, const char *); void pic_abort(pic_state *, const char *);

View File

@ -619,6 +619,8 @@ pic_codegen(pic_state *pic, pic_value obj)
{ {
struct pic_proc *proc; struct pic_proc *proc;
codegen_state *state; codegen_state *state;
struct pic_env *env;
int i;
state = new_codegen_state(pic); state = new_codegen_state(pic);
@ -635,10 +637,20 @@ pic_codegen(pic_state *pic, pic_value obj)
} }
} }
state->irep = new_irep(pic); state->irep = new_irep(pic);
state->irep->argc = 1;
codegen(state, obj); codegen(state, obj);
state->irep->code[state->irep->clen].insn = OP_STOP; state->irep->code[state->irep->clen].insn = OP_STOP;
state->irep->clen++; state->irep->clen++;
proc = pic_proc_new(pic, state->irep, NULL);
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV);
env->num_val = state->irep->argc;
env->values = (pic_value *)pic_alloc(pic, sizeof(pic_value) * env->num_val);
for (i = 0; i < env->num_val; ++i) {
env->values[i] = pic_undef_value();
}
env->up = NULL;
proc = pic_proc_new(pic, state->irep, env);
destroy_codegen_state(pic, state); destroy_codegen_state(pic, state);

View File

@ -38,8 +38,9 @@ pic_load_stdlib(pic_state *pic)
abort(); abort();
} }
v = pic_run(pic, proc, pic_nil_value()); v = pic_apply(pic, proc, pic_nil_value());
if (pic_undef_p(v)) { if (pic_undef_p(v)) {
fputs(pic->errmsg, stderr);
fputs("fatal error: built-in.scm evaluation failure", stderr); fputs("fatal error: built-in.scm evaluation failure", stderr);
abort(); abort();
} }

View File

@ -178,11 +178,13 @@ pic_get_args(pic_state *pic, const char *format, ...)
#define POPCI() (pic->ci--) #define POPCI() (pic->ci--)
pic_value pic_value
pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv)
{ {
struct pic_code *pc; struct pic_code *pc;
int ai = pic_gc_arena_preserve(pic); int ai = pic_gc_arena_preserve(pic);
jmp_buf jmp; jmp_buf jmp;
size_t argc, i;
struct pic_code boot;
#if PIC_DIRECT_THREADED_VM #if PIC_DIRECT_THREADED_VM
static void *oplabels[] = { static void *oplabels[] = {
@ -195,8 +197,6 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
}; };
#endif #endif
pc = proc->u.irep->code;
if (setjmp(jmp) == 0) { if (setjmp(jmp) == 0) {
pic->jmp = &jmp; pic->jmp = &jmp;
} }
@ -204,12 +204,19 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
goto L_RAISE; goto L_RAISE;
} }
/* adjust call frame */ argc = pic_length(pic, argv) + 1;
pic->sp[0] = pic_obj_value(proc);
pic->ci->argc = 1; PUSH(pic_obj_value(proc));
pic->ci->pc = NULL; for (i = 1; i < argc; ++i) {
pic->ci->fp = pic->sp; PUSH(pic_car(pic, argv));
pic->sp++; argv = pic_cdr(pic, argv);
}
/* boot! */
boot.insn = OP_CALL;
boot.u.i = argc;
pc = &boot;
goto L_CALL;
VM_LOOP { VM_LOOP {
CASE(OP_POP) { CASE(OP_POP) {
@ -297,6 +304,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args)
pic_callinfo *ci; pic_callinfo *ci;
struct pic_proc *proc; struct pic_proc *proc;
L_CALL:
c = pic->sp[-pc->u.i]; c = pic->sp[-pc->u.i];
if (! pic_proc_p(c)) { if (! pic_proc_p(c)) {
pic->errmsg = "invalid application"; pic->errmsg = "invalid application";

View File

@ -117,7 +117,7 @@ main(int argc, char *argv[], char **envp)
pic->errmsg = NULL; pic->errmsg = NULL;
goto next; goto next;
} }
v = pic_run(pic, proc, pic_nil_value()); v = pic_apply(pic, proc, pic_nil_value());
if (pic_undef_p(v)) { if (pic_undef_p(v)) {
printf("runtime error: %s\n", pic->errmsg); printf("runtime error: %s\n", pic->errmsg);
pic->errmsg = NULL; pic->errmsg = NULL;