Conditional return instructions

This commit is contained in:
Ian Gulliver
2021-11-19 16:30:11 -10:00
parent 7a24eee583
commit 50f986fb13
4 changed files with 94 additions and 30 deletions

View File

@@ -3,8 +3,7 @@ package vm
type opHandler func(*State, *Instruction)
var opHandlers = map[OpCodeType]opHandler{
OpNoOp: (*State).handleNoOp,
OpReturn: (*State).handleReturn,
OpNoOp: (*State).handleNoOp,
OpMove: (*State).handleMove,
@@ -31,15 +30,15 @@ var opHandlers = map[OpCodeType]opHandler{
OpCall: (*State).handleCall,
OpCallIfTrue: (*State).handleCallIfTrue,
OpCallIfFalse: (*State).handleCallIfFalse,
OpReturn: (*State).handleReturn,
OpReturnIfTrue: (*State).handleReturnIfTrue,
OpReturnIfFalse: (*State).handleReturnIfFalse,
}
func (state *State) handleNoOp(instr *Instruction) {
}
func (state *State) handleReturn(instr *Instruction) {
state.ret()
}
func (state *State) handleMove(instr *Instruction) {
in := state.readUnsigned(&instr.Operand2)
state.writeUnsigned(&instr.Operand1, in)
@@ -162,3 +161,19 @@ func (state *State) handleCallIfFalse(instr *Instruction) {
state.handleCall(instr)
}
}
func (state *State) handleReturn(instr *Instruction) {
state.ret()
}
func (state *State) handleReturnIfTrue(instr *Instruction) {
if state.comparisonResult == true {
state.handleReturn(instr)
}
}
func (state *State) handleReturnIfFalse(instr *Instruction) {
if state.comparisonResult == false {
state.handleReturn(instr)
}
}