From 46356365270b71be94097b3c408d5f35a9ebd6ed Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 26 Oct 2021 08:40:59 +0200 Subject: Add initial function call procedure --- src/bytecode/vm.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/bytecode/vm.h') diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index f7852de..63eedfa 100755 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h @@ -136,8 +136,9 @@ vm_reset(VM *vm) { void vm_interpret(VM *vm) { - CallFrame *frame = &vm->frames[array_size(vm->frames) - 1]; + CallFrame *frame = &vm->frames[0]; vm->pc = frame->chunk->code; + frame->rp = NULL; if (frame->chunk->code == NULL || array_size(frame->chunk->code) == 0) { error_push((Error){ @@ -252,12 +253,20 @@ vm_interpret(VM *vm) { case OP_NEWLINE: { printf("\n"); } break; + case OP_CALL: { + Object proc = array_pop(vm->stack); + CallFrame new_frame = (CallFrame){proc.chunk, vm->pc}; + array_push(vm->frames, new_frame); + frame = &vm->frames[array_size(vm->frames) - 1]; + vm->pc = frame->chunk->code; + } break; case OP_RETURN: { - if (frame->rp != NULL) { - // TODO: restore previous call frame. - vm->pc = frame->rp; + if (frame->rp == NULL) { + return; } - return; + vm->pc = frame->rp; + array_head(vm->frames)->size--; + frame = &vm->frames[array_size(vm->frames) - 1]; } break; default: { error_push((Error){ -- cgit v1.2.1