diff --git a/asm/assemble.go b/asm/assemble.go index 6076ec1..08abce4 100644 --- a/asm/assemble.go +++ b/asm/assemble.go @@ -7,27 +7,27 @@ import "strings" import "github.com/firestuff/subcoding/vm" import "github.com/pkg/errors" -func Assemble(src []byte) ([][]*vm.Instruction, error) { - prog, err := parse(src) +func Assemble(src []byte) (*vm.Program, error) { + parsed, err := parse(src) if err != nil { return nil, err } - fncs := [][]*vm.Instruction{} + ret := &vm.Program{} - for f, fnc := range prog.Functions { + for f, fnc := range parsed.Functions { instrs, err := assembleFunction(fnc) if err != nil { return nil, errors.Wrapf(err, "At function index %d\n", f) } - fncs = append(fncs, instrs) + ret.Functions = append(ret.Functions, instrs) } - return fncs, nil + return ret, nil } -func AssembleString(src string) ([][]*vm.Instruction, error) { +func AssembleString(src string) (*vm.Program, error) { return Assemble([]byte(src)) } diff --git a/vm/program.go b/vm/program.go new file mode 100644 index 0000000..c24fdd9 --- /dev/null +++ b/vm/program.go @@ -0,0 +1,5 @@ +package vm + +type Program struct { + Functions [][]*Instruction +} diff --git a/vm/state.go b/vm/state.go index 2635096..a5b66f2 100644 --- a/vm/state.go +++ b/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() }