Fix 'if' special form.

This commit is contained in:
Calvin Rose 2017-02-09 17:12:01 -05:00
parent 3d7e574e05
commit 715c239fc1
3 changed files with 22 additions and 4 deletions

View File

@ -878,7 +878,7 @@ static Slot CompileIf(Compiler * c, FormOptions opts, Array * form) {
BufferPushUInt16(gc, buffer, condition.index);
BufferPushUInt32(gc, buffer, 0);
/* Compile true path */
left = CompileValue(c, resOpts, form->data[1]);
left = CompileValue(c, resOpts, form->data[2]);
if (opts.isTail && !left.isDud) {
BufferPushUInt16(gc, buffer, VM_OP_RET);
BufferPushUInt16(gc, buffer, left.index);
@ -899,7 +899,7 @@ static Slot CompileIf(Compiler * c, FormOptions opts, Array * form) {
buffer->count = countAfterFirstBranch;
/* Compile false path */
if (form->count == 4) {
right = CompileValue(c, resOpts, form->data[1]);
right = CompileValue(c, resOpts, form->data[3]);
if (opts.isTail && !right.isDud) {
BufferPushUInt16(gc, buffer, VM_OP_RET);
BufferPushUInt16(gc, buffer, right.index);

BIN
interp Executable file

Binary file not shown.

22
value.c
View File

@ -6,6 +6,20 @@
#include "value.h"
#include "buffer.h"
/* Print the bytecode for a FuncDef */
static void FuncDefBytecodePrint(FuncDef * def) {
uint32_t count, i;
count = def->byteCodeLen;
printf("(bytecode)[");
if (count) {
for (i = 0; i < count - 1; ++i) {
printf("%04x ", def->byteCode[i]);
}
printf("%04x", def->byteCode[i]);
}
printf("]");
}
/* Print a value recursively. Used for debugging */
void ValuePrint(Value * x, uint32_t indent) {
uint32_t i;
@ -41,7 +55,9 @@ void ValuePrint(Value * x, uint32_t indent) {
printf("<cfunction>");
break;
case TYPE_FUNCTION:
printf("<function>");
printf("<function ");
FuncDefBytecodePrint(x->data.func->def);
printf(">");
break;
case TYPE_DICTIONARY:
printf("<dictionary>");
@ -50,7 +66,9 @@ void ValuePrint(Value * x, uint32_t indent) {
printf("<bytebuffer>");
break;
case TYPE_FUNCDEF:
printf("<funcdef>");
printf("<funcdef ");
FuncDefBytecodePrint(x->data.funcdef);
printf(">");
break;
case TYPE_FUNCENV:
printf("<funcenv>");