Common call and startup code

This commit is contained in:
Ian Gulliver
2021-11-14 20:37:33 -10:00
parent f0411e9555
commit b6d7f66def

15
main.go
View File

@@ -81,18 +81,14 @@ var OpHandlers = map[uint32]OpHandler{
} }
func NewState(functionByteCode [][]byte) *State { func NewState(functionByteCode [][]byte) *State {
stackFrame := &StackFrame{}
return &State{ return &State{
FunctionByteCode: functionByteCode, FunctionByteCode: functionByteCode,
Stack: []*StackFrame{
stackFrame,
},
StackFrame: stackFrame,
} }
} }
func (state *State) Execute() { func (state *State) Execute() {
state.call(0)
for len(state.Stack) > 0 && state.Error == nil { for len(state.Stack) > 0 && state.Error == nil {
fnc := state.FunctionByteCode[state.FunctionIndex] fnc := state.FunctionByteCode[state.FunctionIndex]
start := state.InstructionIndex * InstructionBytes start := state.InstructionIndex * InstructionBytes
@@ -169,13 +165,18 @@ func (state *State) NoOp(instr *Instruction) {
func (state *State) Call(instr *Instruction) { func (state *State) Call(instr *Instruction) {
fmt.Printf("Call\n") fmt.Printf("Call\n")
in := state.ReadSigned(&instr.Operand1)
state.call(in)
}
func (state *State) call(functionOffset int64) {
stackFrame := &StackFrame{ stackFrame := &StackFrame{
PreviousFunctionIndex: state.FunctionIndex, PreviousFunctionIndex: state.FunctionIndex,
PreviousInstructionIndex: state.InstructionIndex, PreviousInstructionIndex: state.InstructionIndex,
} }
state.Stack = append(state.Stack, stackFrame) state.Stack = append(state.Stack, stackFrame)
state.StackFrame = stackFrame state.StackFrame = stackFrame
state.FunctionIndex += state.ReadSigned(&instr.Operand1) state.FunctionIndex += functionOffset
state.InstructionIndex = 0 state.InstructionIndex = 0
} }