aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/vm.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-26 08:40:59 +0200
committerBad Diode <bd@badd10de.dev>2021-10-26 08:40:59 +0200
commit46356365270b71be94097b3c408d5f35a9ebd6ed (patch)
tree59ae01f6108a94e818670de1bda37ae12a5fa2fe /src/bytecode/vm.h
parentd54e595644fcaf6756d53d368213ad3129c49327 (diff)
downloadbdl-46356365270b71be94097b3c408d5f35a9ebd6ed.tar.gz
bdl-46356365270b71be94097b3c408d5f35a9ebd6ed.zip
Add initial function call procedure
Diffstat (limited to 'src/bytecode/vm.h')
-rwxr-xr-xsrc/bytecode/vm.h19
1 files changed, 14 insertions, 5 deletions
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) {
136 136
137void 137void
138vm_interpret(VM *vm) { 138vm_interpret(VM *vm) {
139 CallFrame *frame = &vm->frames[array_size(vm->frames) - 1]; 139 CallFrame *frame = &vm->frames[0];
140 vm->pc = frame->chunk->code; 140 vm->pc = frame->chunk->code;
141 frame->rp = NULL;
141 142
142 if (frame->chunk->code == NULL || array_size(frame->chunk->code) == 0) { 143 if (frame->chunk->code == NULL || array_size(frame->chunk->code) == 0) {
143 error_push((Error){ 144 error_push((Error){
@@ -252,12 +253,20 @@ vm_interpret(VM *vm) {
252 case OP_NEWLINE: { 253 case OP_NEWLINE: {
253 printf("\n"); 254 printf("\n");
254 } break; 255 } break;
256 case OP_CALL: {
257 Object proc = array_pop(vm->stack);
258 CallFrame new_frame = (CallFrame){proc.chunk, vm->pc};
259 array_push(vm->frames, new_frame);
260 frame = &vm->frames[array_size(vm->frames) - 1];
261 vm->pc = frame->chunk->code;
262 } break;
255 case OP_RETURN: { 263 case OP_RETURN: {
256 if (frame->rp != NULL) { 264 if (frame->rp == NULL) {
257 // TODO: restore previous call frame. 265 return;
258 vm->pc = frame->rp;
259 } 266 }
260 return; 267 vm->pc = frame->rp;
268 array_head(vm->frames)->size--;
269 frame = &vm->frames[array_size(vm->frames) - 1];
261 } break; 270 } break;
262 default: { 271 default: {
263 error_push((Error){ 272 error_push((Error){