Program wrapper struct
This commit is contained in:
5
vm/program.go
Normal file
5
vm/program.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package vm
|
||||
|
||||
type Program struct {
|
||||
Functions [][]*Instruction
|
||||
}
|
||||
12
vm/state.go
12
vm/state.go
@@ -6,7 +6,7 @@ type State struct {
|
||||
running bool
|
||||
err error
|
||||
|
||||
functions [][]*Instruction
|
||||
program *Program
|
||||
functionIndex int64
|
||||
instructionIndex int64
|
||||
|
||||
@@ -15,9 +15,9 @@ type State struct {
|
||||
stack []*stackFrame
|
||||
}
|
||||
|
||||
func NewState(functions [][]*Instruction) (*State, error) {
|
||||
func NewState(prog *Program) (*State, error) {
|
||||
return &State{
|
||||
functions: functions,
|
||||
program: prog,
|
||||
globalMemory: NewMemory(16),
|
||||
}, nil
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func (state *State) stackFrame() *stackFrame {
|
||||
}
|
||||
|
||||
func (state *State) function() []*Instruction {
|
||||
return state.functions[state.functionIndex]
|
||||
return state.program.Functions[state.functionIndex]
|
||||
}
|
||||
|
||||
func (state *State) setError(err error) {
|
||||
@@ -52,7 +52,7 @@ func (state *State) setError(err error) {
|
||||
}
|
||||
|
||||
func (state *State) setHandlers() {
|
||||
for _, fnc := range state.functions {
|
||||
for _, fnc := range state.program.Functions {
|
||||
for _, instr := range fnc {
|
||||
handler, found := opHandlers[instr.OpCode]
|
||||
if !found {
|
||||
@@ -71,7 +71,7 @@ func (state *State) processInstruction() {
|
||||
state.instructionIndex += 1
|
||||
instr.opHandler(state, instr)
|
||||
|
||||
if state.functionIndex >= int64(len(state.functions)) {
|
||||
if state.functionIndex >= int64(len(state.program.Functions)) {
|
||||
state.ret()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user