Full test (which fails)

This commit is contained in:
Ian Gulliver
2021-11-18 17:53:38 -10:00
parent 8e34c67f11
commit e99387beb4
5 changed files with 27 additions and 22 deletions

View File

@@ -13,14 +13,14 @@ type State struct {
instructionIndex int64
comparisonResult bool
globalMemory *memory
globalMemory *Memory
stack []*stackFrame
}
func NewState(functions [][]*Instruction) (*State, error) {
return &State{
functions: functions,
globalMemory: newMemory(16),
globalMemory: NewMemory(16),
}, nil
}
@@ -51,6 +51,10 @@ func (state *State) Execute() error {
return state.err
}
func (state *State) GlobalMemory() *Memory {
return state.globalMemory
}
func (state *State) stackFrame() *stackFrame {
return state.stack[len(state.stack)-1]
}
@@ -83,6 +87,10 @@ func (state *State) processInstruction() {
instr := fnc[state.instructionIndex]
state.instructionIndex += 1
instr.opHandler(state, instr)
if state.instructionIndex >= int64(len(fnc)) {
state.ret()
}
}
func (state *State) readUnsigned(op *Operand) uint64 {
@@ -91,14 +99,14 @@ func (state *State) readUnsigned(op *Operand) uint64 {
return op.Value
case FunctionMemoryIndex:
value, err := state.stackFrame().functionMemory.readUnsigned(op.Value)
value, err := state.stackFrame().functionMemory.ReadUnsigned(op.Value)
if err != nil {
state.setError(err)
}
return value
case GlobalMemoryIndex:
value, err := state.globalMemory.readUnsigned(op.Value)
value, err := state.globalMemory.ReadUnsigned(op.Value)
if err != nil {
state.setError(err)
}
@@ -120,13 +128,13 @@ func (state *State) writeUnsigned(op *Operand, value uint64) {
state.setError(fmt.Errorf("Write to literal operand"))
case FunctionMemoryIndex:
err := state.stackFrame().functionMemory.writeUnsigned(op.Value, value)
err := state.stackFrame().functionMemory.WriteUnsigned(op.Value, value)
if err != nil {
state.setError(err)
}
case GlobalMemoryIndex:
err := state.globalMemory.writeUnsigned(op.Value, value)
err := state.globalMemory.WriteUnsigned(op.Value, value)
if err != nil {
state.setError(err)
}