More State constructors
This commit is contained in:
14
state.go
14
state.go
@@ -19,8 +19,14 @@ type State struct {
|
|||||||
stack []*StackFrame
|
stack []*StackFrame
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState(byteCodes [][]byte) (*State, error) {
|
func NewState(functions [][]*Instruction) (*State, error) {
|
||||||
state := &State{}
|
return &State{
|
||||||
|
functions: functions,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStateFromByteCode(byteCodes [][]byte) (*State, error) {
|
||||||
|
functions := [][]*Instruction{}
|
||||||
|
|
||||||
for i, byteCode := range byteCodes {
|
for i, byteCode := range byteCodes {
|
||||||
instrs, err := NewInstructionsFromByteCode(byteCode)
|
instrs, err := NewInstructionsFromByteCode(byteCode)
|
||||||
@@ -28,10 +34,10 @@ func NewState(byteCodes [][]byte) (*State, error) {
|
|||||||
return nil, errors.Wrapf(err, "At function index %d", i)
|
return nil, errors.Wrapf(err, "At function index %d", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
state.functions = append(state.functions, instrs)
|
functions = append(functions, instrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return state, nil
|
return NewState(functions)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Execute() {
|
func (state *State) Execute() {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func TestFirst(t *testing.T) {
|
|||||||
functionByteCode = append(functionByteCode, byteCode)
|
functionByteCode = append(functionByteCode, byteCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := NewState(functionByteCode)
|
state, err := NewStateFromByteCode(functionByteCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user