Function to bytecode, fix function calling

This commit is contained in:
2026-07-04 13:11:01 +10:00
parent 6385a726cf
commit 396a6d9a1b
3 changed files with 148 additions and 21 deletions

View File

@@ -31,8 +31,11 @@ static void printValue(GroundBytecodeValue* val) {
case GroundType_String:
printf("%s", val->as.String.cstr);
break;
case GroundType_Function:
printf("<function>");
break;
default:
printf("<fixme>");
printf("<fixme opt=%d>", val->type.type);
break;
}
}
@@ -714,22 +717,22 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
Ground.Flags.error = true;
return CONTINUE;
}
} else {
free(function->closure->heap);
}
GroundBytecodeHeap newHeap = {
.capacity = heap->capacity,
.len = heap->len,
.heap = malloc(sizeof(GroundBytecodeValue) * heap->capacity)
};
function->closure->capacity = heap->capacity;
function->closure->len = heap->len;
function->closure->heap = malloc(sizeof(GroundBytecodeValue) * heap->capacity);
if (newHeap.heap == NULL) {
if (function->closure->heap == NULL) {
Ground.Log.Error("malloc failed in Ground.Bytecode.Instruction.execute");
Ground.Flags.error = true;
return CONTINUE;
}
for (GroundSize i = 0; i < heap->len; i++) {
newHeap.heap[i] = Ground.Copy.BytecodeValue(&heap->heap[i]);
function->closure->heap[i] = Ground.Copy.BytecodeValue(&heap->heap[i]);
}
return CONTINUE;
@@ -766,14 +769,14 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
return CONTINUE;
}
// load function arguments into the heap
// load function arguments into the heap
for (GroundSize i = 0; i < function->args.count; i++) {
newHeap.heap[i] = Ground.Copy.BytecodeValue(HEAP_GET(heap, i + 1)); // +1 because arg 1 is where fn args start
newHeap.heap[i] = Ground.Copy.BytecodeValue(HEAP_GET(heap, instruction->args.at[i + 1]));
}
// function arguments are the first indexes, copy after
// function arguments are the first indexes, copy closure after
for (GroundSize i = function->args.count; i < newHeap.len; i++) {
newHeap.heap[i] = Ground.Copy.BytecodeValue(&function->closure->heap[i]);
newHeap.heap[i] = Ground.Copy.BytecodeValue(&function->closure->heap[i - function->args.count]);
}
// Now we run the thingy