From b6d7f66def2235f27791b7b6bf5ddd38ea325416 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 14 Nov 2021 20:37:33 -1000 Subject: [PATCH] Common call and startup code --- main.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 8790cf6..54edfc7 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ type Instruction struct { } type State struct { - Error error + Error error FunctionByteCode [][]byte FunctionIndex int64 @@ -81,18 +81,14 @@ var OpHandlers = map[uint32]OpHandler{ } func NewState(functionByteCode [][]byte) *State { - stackFrame := &StackFrame{} - return &State{ FunctionByteCode: functionByteCode, - Stack: []*StackFrame{ - stackFrame, - }, - StackFrame: stackFrame, } } func (state *State) Execute() { + state.call(0) + for len(state.Stack) > 0 && state.Error == nil { fnc := state.FunctionByteCode[state.FunctionIndex] start := state.InstructionIndex * InstructionBytes @@ -169,13 +165,18 @@ func (state *State) NoOp(instr *Instruction) { func (state *State) Call(instr *Instruction) { fmt.Printf("Call\n") + in := state.ReadSigned(&instr.Operand1) + state.call(in) +} + +func (state *State) call(functionOffset int64) { stackFrame := &StackFrame{ - PreviousFunctionIndex: state.FunctionIndex, + PreviousFunctionIndex: state.FunctionIndex, PreviousInstructionIndex: state.InstructionIndex, } state.Stack = append(state.Stack, stackFrame) state.StackFrame = stackFrame - state.FunctionIndex += state.ReadSigned(&instr.Operand1) + state.FunctionIndex += functionOffset state.InstructionIndex = 0 }