From cd3d36ec5d50fda44ce36c7e62da517a44032b53 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Wed, 23 Oct 2024 14:46:27 -0400 Subject: [PATCH] wip --- pkg/runtime/frame.go | 3 ++- pkg/runtime/vm.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/runtime/frame.go b/pkg/runtime/frame.go index ed344a42..7c54c0de 100644 --- a/pkg/runtime/frame.go +++ b/pkg/runtime/frame.go @@ -4,7 +4,8 @@ type Frame struct { Operands *Stack Variables *Stack State *Stack - ReturnPC int + Parent *Frame + PC int } func NewFrame(size int) *Frame { diff --git a/pkg/runtime/vm.go b/pkg/runtime/vm.go index 6eeff957..85c1241f 100644 --- a/pkg/runtime/vm.go +++ b/pkg/runtime/vm.go @@ -10,10 +10,11 @@ import ( ) type VM struct { - pc int - frames []*Frame - globals map[string]core.Value - env *Environment + env *Environment + globals map[string]core.Value + frames []*Frame + currentFrame *Frame + pc int } func NewVM(opts ...EnvironmentOption) *VM { @@ -36,15 +37,16 @@ func (vm *VM) Run(ctx context.Context, program *Program) ([]byte, error) { // TODO: Add code analysis to calculate the number of operands and variables frame := NewFrame(32) + vm.currentFrame = frame vm.frames = []*Frame{frame} vm.globals = make(map[string]core.Value) vm.pc = 0 loop: for vm.pc < len(program.Bytecode) { - stack := frame.Operands - variables := frame.Variables - state := frame.State + stack := vm.currentFrame.Operands + variables := vm.currentFrame.Variables + state := vm.currentFrame.State op := program.Bytecode[vm.pc] arg := program.Arguments[vm.pc] vm.pc++