Common call and startup code
This commit is contained in:
19
main.go
19
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user