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 {
|
type State struct {
|
||||||
Error error
|
Error error
|
||||||
|
|
||||||
FunctionByteCode [][]byte
|
FunctionByteCode [][]byte
|
||||||
FunctionIndex int64
|
FunctionIndex int64
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user