From f39a8563619f274d1e189f3fbc5c5a031cd7b3bd Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 16 Nov 2021 15:13:30 -1000 Subject: [PATCH] Make private --- ophandler.go | 74 ++++++++++++++++++++++++++-------------------------- state.go | 42 ++++++++++++++--------------- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/ophandler.go b/ophandler.go index 90e2167..0dc266a 100644 --- a/ophandler.go +++ b/ophandler.go @@ -34,7 +34,7 @@ func (state *State) NoOp(instr *Instruction) { } func (state *State) Call(instr *Instruction) { - in := state.ReadSigned(&instr.Operand1) + in := state.readSigned(&instr.Operand1) state.call(in) } @@ -43,96 +43,96 @@ func (state *State) Return(instr *Instruction) { } func (state *State) Move(instr *Instruction) { - in := state.ReadUnsigned(&instr.Operand2) - state.WriteUnsigned(&instr.Operand1, in) + in := state.readUnsigned(&instr.Operand2) + state.writeUnsigned(&instr.Operand1, in) } func (state *State) Add(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) - state.WriteUnsigned(&instr.Operand1, in1+in2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) + state.writeUnsigned(&instr.Operand1, in1+in2) } func (state *State) Subtract(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) - state.WriteUnsigned(&instr.Operand1, in1-in2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) + state.writeUnsigned(&instr.Operand1, in1-in2) } func (state *State) Multiply(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) - state.WriteUnsigned(&instr.Operand1, in1*in2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) + state.writeUnsigned(&instr.Operand1, in1*in2) } func (state *State) DivideUnsigned(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) - state.WriteUnsigned(&instr.Operand1, in1/in2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) + state.writeUnsigned(&instr.Operand1, in1/in2) } func (state *State) DivideSigned(instr *Instruction) { - in1 := state.ReadSigned(&instr.Operand1) - in2 := state.ReadSigned(&instr.Operand2) - state.WriteSigned(&instr.Operand1, in1/in2) + in1 := state.readSigned(&instr.Operand1) + in2 := state.readSigned(&instr.Operand2) + state.writeSigned(&instr.Operand1, in1/in2) } func (state *State) IsEqual(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) state.comparisonResult = (in1 == in2) } func (state *State) IsLessThanUnsigned(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) state.comparisonResult = (in1 < in2) } func (state *State) IsLessThanSigned(instr *Instruction) { - in1 := state.ReadSigned(&instr.Operand1) - in2 := state.ReadSigned(&instr.Operand2) + in1 := state.readSigned(&instr.Operand1) + in2 := state.readSigned(&instr.Operand2) state.comparisonResult = (in1 < in2) } func (state *State) IsGreaterThanUnsigned(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) state.comparisonResult = (in1 > in2) } func (state *State) IsGreaterThanSigned(instr *Instruction) { - in1 := state.ReadSigned(&instr.Operand1) - in2 := state.ReadSigned(&instr.Operand2) + in1 := state.readSigned(&instr.Operand1) + in2 := state.readSigned(&instr.Operand2) state.comparisonResult = (in1 > in2) } func (state *State) IsLessThanOrEqualUnsigned(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) state.comparisonResult = (in1 <= in2) } func (state *State) IsLessThanOrEqualSigned(instr *Instruction) { - in1 := state.ReadSigned(&instr.Operand1) - in2 := state.ReadSigned(&instr.Operand2) + in1 := state.readSigned(&instr.Operand1) + in2 := state.readSigned(&instr.Operand2) state.comparisonResult = (in1 <= in2) } func (state *State) IsGreaterThanOrEqualUnsigned(instr *Instruction) { - in1 := state.ReadUnsigned(&instr.Operand1) - in2 := state.ReadUnsigned(&instr.Operand2) + in1 := state.readUnsigned(&instr.Operand1) + in2 := state.readUnsigned(&instr.Operand2) state.comparisonResult = (in1 >= in2) } func (state *State) IsGreaterThanOrEqualSigned(instr *Instruction) { - in1 := state.ReadSigned(&instr.Operand1) - in2 := state.ReadSigned(&instr.Operand2) + in1 := state.readSigned(&instr.Operand1) + in2 := state.readSigned(&instr.Operand2) state.comparisonResult = (in1 >= in2) } func (state *State) Jump(instr *Instruction) { - in := state.ReadSigned(&instr.Operand1) + in := state.readSigned(&instr.Operand1) state.instructionIndex += in - 1 } diff --git a/state.go b/state.go index 4b39451..2fd02c7 100644 --- a/state.go +++ b/state.go @@ -44,24 +44,24 @@ func NewState(byteCodes [][]byte) (*State, error) { return state, nil } -func (state *State) StackFrame() *StackFrame { - return state.stack[len(state.stack)-1] -} - -func (state *State) Function() []*Instruction { - return state.functions[state.functionIndex] -} - func (state *State) Execute() { state.setHandlers() state.call(0) state.running = true for state.running { - state.ProcessInstruction() + state.processInstruction() } } +func (state *State) stackFrame() *StackFrame { + return state.stack[len(state.stack)-1] +} + +func (state *State) function() []*Instruction { + return state.functions[state.functionIndex] +} + func (state *State) setError(err error) { state.err = err state.running = false @@ -81,14 +81,14 @@ func (state *State) setHandlers() { } } -func (state *State) ProcessInstruction() { - fnc := state.Function() +func (state *State) processInstruction() { + fnc := state.function() instr := fnc[state.instructionIndex] state.instructionIndex += 1 instr.opHandler(state, instr) } -func (state *State) ReadUnsigned(op *Operand) uint64 { +func (state *State) readUnsigned(op *Operand) uint64 { switch op.Type { case Literal: return op.Value @@ -98,7 +98,7 @@ func (state *State) ReadUnsigned(op *Operand) uint64 { state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value)) return 0 } - return state.StackFrame().FunctionMemory[op.Value] + return state.stackFrame().FunctionMemory[op.Value] case GlobalMemoryIndex: if op.Value >= globalMemoryEntries { @@ -113,11 +113,11 @@ func (state *State) ReadUnsigned(op *Operand) uint64 { } } -func (state *State) ReadSigned(op *Operand) int64 { - return int64(state.ReadUnsigned(op)) +func (state *State) readSigned(op *Operand) int64 { + return int64(state.readUnsigned(op)) } -func (state *State) WriteUnsigned(op *Operand, value uint64) { +func (state *State) writeUnsigned(op *Operand, value uint64) { switch op.Type { case Literal: state.setError(fmt.Errorf("Write to literal operand")) @@ -127,7 +127,7 @@ func (state *State) WriteUnsigned(op *Operand, value uint64) { state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value)) return } - state.StackFrame().FunctionMemory[op.Value] = value + state.stackFrame().FunctionMemory[op.Value] = value case GlobalMemoryIndex: if op.Value >= globalMemoryEntries { @@ -141,8 +141,8 @@ func (state *State) WriteUnsigned(op *Operand, value uint64) { } } -func (state *State) WriteSigned(op *Operand, value int64) { - state.WriteUnsigned(op, uint64(value)) +func (state *State) writeSigned(op *Operand, value int64) { + state.writeUnsigned(op, uint64(value)) } func (state *State) call(functionOffset int64) { @@ -161,8 +161,8 @@ func (state *State) call(functionOffset int64) { } func (state *State) ret() { - state.functionIndex = state.StackFrame().PreviousFunctionIndex - state.instructionIndex = state.StackFrame().PreviousInstructionIndex + state.functionIndex = state.stackFrame().PreviousFunctionIndex + state.instructionIndex = state.stackFrame().PreviousInstructionIndex state.stack = state.stack[:len(state.stack)-1] if len(state.stack) == 0 { state.running = false