This commit is contained in:
Yuichi Nishiwaki 2013-10-12 13:06:02 +09:00
parent 5f90f49604
commit 8d23ef666f
2 changed files with 50 additions and 47 deletions

View File

@ -37,7 +37,7 @@ main()
pic = pic_open(); pic = pic_open();
test_object_creation(pic); //test_object_creation(pic);
while (1) { while (1) {
printf("> "); printf("> ");
@ -52,13 +52,16 @@ main()
} }
line[char_index] = '\0'; line[char_index] = '\0';
/* echo */ /* read */
v = pic_parse(pic, line); v = pic_parse(pic, line);
//pic_debug(pic, pic_eval(pic, v, pic->global_env)); /* eval */
proc = pic_codegen(pic, v, pic->global_env); proc = pic_codegen(pic, v, pic->global_env);
v = pic_run(pic, proc, pic_nil_value()); v = pic_run(pic, proc, pic_nil_value());
/* print */
pic_debug(pic, v); pic_debug(pic, v);
printf("\n"); printf("\n");
} }

View File

@ -22,50 +22,7 @@ struct pic_irep {
size_t clen, ccapa; size_t clen, ccapa;
}; };
pic_value static void
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
pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *env) pic_gen(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_env *env)
{ {
pic_value sCONS, sADD; pic_value sCONS, sADD;
@ -137,3 +94,46 @@ pic_codegen(pic_state *pic, pic_value obj, struct pic_env *env)
return proc; 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;
}