From 8d23ef666fe1769fcb20ac693f1e106be7e7ff07 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 12 Oct 2013 13:06:02 +0900 Subject: [PATCH] cleanup --- src/main.c | 9 ++++-- src/vm.c | 88 +++++++++++++++++++++++++++--------------------------- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/main.c b/src/main.c index a14e4452..a8c38895 100644 --- a/src/main.c +++ b/src/main.c @@ -37,7 +37,7 @@ main() pic = pic_open(); - test_object_creation(pic); + //test_object_creation(pic); while (1) { printf("> "); @@ -52,13 +52,16 @@ main() } line[char_index] = '\0'; - /* echo */ + /* read */ v = pic_parse(pic, line); - //pic_debug(pic, pic_eval(pic, v, pic->global_env)); + /* eval */ proc = pic_codegen(pic, v, pic->global_env); v = pic_run(pic, proc, pic_nil_value()); + + /* print */ pic_debug(pic, v); + printf("\n"); } diff --git a/src/vm.c b/src/vm.c index f8d9a93f..d8f514b7 100644 --- a/src/vm.c +++ b/src/vm.c @@ -22,50 +22,7 @@ struct pic_irep { size_t clen, ccapa; }; -pic_value -pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) -{ - struct pic_code *pc; - pic_value *sp; - - pc = proc->u.irep->code; - sp = pic->sp; - - while (1) { - switch (pc->insn) { - case OP_PUSHNIL: { - *++sp = pic_nil_value(); - break; - } - case OP_PUSHI: { - *++sp = pic_int_value(pc->u.i); - break; - } - case OP_CONS: { - pic_value a, b; - a = *sp--; - b = *sp--; - *++sp = pic_cons(pic, a, b); - break; - } - case OP_ADD: { - pic_value a, b; - a = *sp--; - b = *sp--; - *++sp = pic_int_value(pic_int(a) + pic_int(b)); - break; - } - case OP_STOP: - goto STOP; - } - pc++; - } - - STOP: - return *sp; -} - -void +static void pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *env) { pic_value sCONS, sADD; @@ -137,3 +94,46 @@ pic_codegen(pic_state *pic, pic_value obj, struct pic_env *env) return proc; } + +pic_value +pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) +{ + struct pic_code *pc; + pic_value *sp; + + pc = proc->u.irep->code; + sp = pic->sp; + + while (1) { + switch (pc->insn) { + case OP_PUSHNIL: { + *++sp = pic_nil_value(); + break; + } + case OP_PUSHI: { + *++sp = pic_int_value(pc->u.i); + break; + } + case OP_CONS: { + pic_value a, b; + a = *sp--; + b = *sp--; + *++sp = pic_cons(pic, a, b); + break; + } + case OP_ADD: { + pic_value a, b; + a = *sp--; + b = *sp--; + *++sp = pic_int_value(pic_int(a) + pic_int(b)); + break; + } + case OP_STOP: + goto STOP; + } + pc++; + } + + STOP: + return *sp; +}