More State constructors

This commit is contained in:
Ian Gulliver
2021-11-16 15:36:34 -10:00
parent 69e69acc6f
commit f2d552e076
2 changed files with 11 additions and 5 deletions

View File

@@ -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() {

View File

@@ -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)
} }